[Go] Add -go-pkgpath option.

This commit is contained in:
Ian Lance Taylor 2013-12-17 13:50:39 -08:00
commit 532da6989c
4 changed files with 85 additions and 25 deletions

View file

@ -5,6 +5,9 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.0 (in progress) Version 3.0.0 (in progress)
============================ ============================
2013-12-17: iant
[Go] Add -go-pkgpath option.
2013-12-16: iant 2013-12-16: iant
[Go] Update for Go 1.2 release. Add support for linking [Go] Update for Go 1.2 release. Add support for linking
SWIG code directly into executable, rather than using a SWIG code directly into executable, rather than using a

View file

@ -134,10 +134,18 @@ swig -go -help
option implies the -use-shlib option.</td> option implies the -use-shlib option.</td>
</tr> </tr>
<tr>
<td>-go-pkgpath &lt;pkgpath&gt;</td>
<td>When generating code for gccgo, set the pkgpath to use. This
corresponds to the <tt>-fgo-pkgpath</tt> option to gccgo.</td>
</tr>
<tr> <tr>
<td>-go-prefix &lt;prefix&gt;</td> <td>-go-prefix &lt;prefix&gt;</td>
<td>When generating code for gccgo, set the prefix to use. This <td>When generating code for gccgo, set the prefix to use. This
corresponds to the <tt>-fgo-prefix</tt> option to gccgo.</td> corresponds to the <tt>-fgo-prefix</tt> option to gccgo.
If <tt>-go-pkgpath</tt> is used, <tt>-go-prefix</tt> will be
ignored.</td>
</tr> </tr>
<tr> <tr>

View file

@ -115,13 +115,6 @@ extern void _cgo_panic(const char *);
version. We assume that the version of gcc used to compile this version. We assume that the version of gcc used to compile this
file is the same as the version of gccgo. */ file is the same as the version of gccgo. */
#define SWIGCONCAT2(s1, s2) s1 ## s2
#define SWIGCONCAT1(s1, s2) SWIGCONCAT2(s1, s2)
#define SwigCgocall SWIGCONCAT1(SWIGMODULE, SwigCgocall)
#define SwigCgocallDone SWIGCONCAT1(SWIGMODULE, SwigCgocallDone)
#define SwigCgocallBack SWIGCONCAT1(SWIGMODULE, SwigCgocallBack)
#define SwigCgocallBackDone SWIGCONCAT1(SWIGMODULE, SwigCgocallBackDone)
#define SWIG_GCC_VERSION \ #define SWIG_GCC_VERSION \
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC__PATH_LEVEL__) (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC__PATH_LEVEL__)
@ -142,22 +135,36 @@ void SwigDoCgocallBack(void) __asm__("syscall.CgocallBack");
void SwigDoCgocallBackDone(void) __asm__("syscall.CgocallBackDone"); void SwigDoCgocallBackDone(void) __asm__("syscall.CgocallBackDone");
#endif #endif
#define SWIGSTRINGIFY2(s) #s
#define SWIGSTRINGIFY(s) SWIGSTRINGIFY2(s)
void SwigCgocall()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocall");
void SwigCgocall() { void SwigCgocall() {
SwigDoCgocall(); SwigDoCgocall();
} }
void SwigCgocallDone()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallDone");
void SwigCgocallDone() { void SwigCgocallDone() {
SwigDoCgocallDone(); SwigDoCgocallDone();
} }
void SwigCgocallBack()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallBack");
void SwigCgocallBack() { void SwigCgocallBack() {
SwigDoCgocallBack(); SwigDoCgocallBack();
} }
void SwigCgocallBackDone()
__asm__(SWIGSTRINGIFY(SWIGGO_PREFIX) ".SwigCgocallBackDone");
void SwigCgocallBackDone() { void SwigCgocallBackDone() {
SwigDoCgocallBackDone(); SwigDoCgocallBackDone();
} }
#undef SWIGSTRINGIFY
#undef SWIGSTRINGIFY2
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -22,6 +22,10 @@ class GO:public Language {
bool gccgo_flag; bool gccgo_flag;
// Prefix to use with gccgo. // Prefix to use with gccgo.
String *go_prefix; String *go_prefix;
// -fgo-prefix option.
String *prefix_option;
// -fgo-pkgpath option.
String *pkgpath_option;
// Whether to use a shared library. // Whether to use a shared library.
bool use_shlib; bool use_shlib;
// Name of shared library to import. // Name of shared library to import.
@ -88,6 +92,8 @@ public:
module(NULL), module(NULL),
gccgo_flag(false), gccgo_flag(false),
go_prefix(NULL), go_prefix(NULL),
prefix_option(NULL),
pkgpath_option(NULL),
use_shlib(false), use_shlib(false),
soname(NULL), soname(NULL),
long_type_size(32), long_type_size(32),
@ -149,7 +155,16 @@ private:
gccgo_flag = true; gccgo_flag = true;
} else if (strcmp(argv[i], "-go-prefix") == 0) { } else if (strcmp(argv[i], "-go-prefix") == 0) {
if (argv[i + 1]) { if (argv[i + 1]) {
go_prefix = NewString(argv[i + 1]); prefix_option = NewString(argv[i + 1]);
Swig_mark_arg(i);
Swig_mark_arg(i + 1);
i++;
} else {
Swig_arg_error();
}
} else if (strcmp(argv[i], "-go-pkgpath") == 0) {
if (argv[i + 1]) {
pkgpath_option = NewString(argv[i + 1]);
Swig_mark_arg(i); Swig_mark_arg(i);
Swig_mark_arg(i + 1); Swig_mark_arg(i + 1);
i++; i++;
@ -201,8 +216,8 @@ private:
} }
} }
if (gccgo_flag && !go_prefix) { if (gccgo_flag && !pkgpath_option && !prefix_option) {
go_prefix = NewString("go"); prefix_option = NewString("go");
} }
// Add preprocessor symbol to parser. // Add preprocessor symbol to parser.
@ -285,6 +300,27 @@ private:
Append(soname, ".so"); Append(soname, ".so");
} }
if (gccgo_flag) {
String *pref;
if (pkgpath_option) {
pref = pkgpath_option;
} else {
pref = prefix_option;
}
go_prefix = NewString("");
for (char *p = Char(pref); *p != '\0'; p++) {
if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z') || (*p >= '0' && *p <= '9') || *p == '.' || *p == '$') {
Putc(*p, go_prefix);
} else {
Putc('_', go_prefix);
}
}
if (!pkgpath_option) {
Append(go_prefix, ".");
Append(go_prefix, package);
}
}
// Get filenames. // Get filenames.
String *swig_filename = Getattr(n, "infile"); String *swig_filename = Getattr(n, "infile");
@ -374,6 +410,9 @@ private:
} }
Printf(f_c_runtime, "#define SWIGMODULE %s\n", module); Printf(f_c_runtime, "#define SWIGMODULE %s\n", module);
if (gccgo_flag) {
Printf(f_c_runtime, "#define SWIGGO_PREFIX %s\n", go_prefix);
}
if (directorsEnabled()) { if (directorsEnabled()) {
Printf(f_c_runtime, "#define SWIG_DIRECTORS\n"); Printf(f_c_runtime, "#define SWIG_DIRECTORS\n");
@ -404,14 +443,12 @@ private:
Printf(f_go_begin, "\npackage %s\n\n", package); Printf(f_go_begin, "\npackage %s\n\n", package);
Printf(f_go_runtime, "//extern %sSwigCgocall\n", module); if (gccgo_flag) {
Printf(f_go_runtime, "func SwigCgocall()\n"); Printf(f_go_runtime, "func SwigCgocall()\n");
Printf(f_go_runtime, "//extern %sSwigCgocallDone\n", module); Printf(f_go_runtime, "func SwigCgocallDone()\n");
Printf(f_go_runtime, "func SwigCgocallDone()\n"); Printf(f_go_runtime, "func SwigCgocallBack()\n");
Printf(f_go_runtime, "//extern %sSwigCgocallBack\n", module); Printf(f_go_runtime, "func SwigCgocallBackDone()\n\n");
Printf(f_go_runtime, "func SwigCgocallBack()\n"); }
Printf(f_go_runtime, "//extern %sSwigCgocallBackDone\n", module);
Printf(f_go_runtime, "func SwigCgocallBackDone()\n\n");
// All the C++ wrappers should be extern "C". // All the C++ wrappers should be extern "C".
@ -1340,7 +1377,7 @@ private:
// Start the function definition. // Start the function definition.
String *fnname = NewString(""); String *fnname = NewString("");
Printv(fnname, go_prefix, "_", wname, "(", NULL); Printv(fnname, "go_", wname, "(", NULL);
if (parm_count > required_count) { if (parm_count > required_count) {
Printv(fnname, "intgo _swig_optargc", NULL); Printv(fnname, "intgo _swig_optargc", NULL);
@ -1369,17 +1406,21 @@ private:
Printv(fnname, ")", NULL); Printv(fnname, ")", NULL);
String *fndef = NewString("");
if (SwigType_type(result) == T_VOID) { if (SwigType_type(result) == T_VOID) {
Printv(f->def, "void ", fnname, NULL); Printv(fndef, "void ", fnname, NULL);
} else { } else {
String *ct = gccgoCTypeForGoValue(n, result, fnname); String *ct = gccgoCTypeForGoValue(n, result, fnname);
Printv(f->def, ct, NULL); Printv(fndef, ct, NULL);
Delete(ct); Delete(ct);
} }
Printv(f->def, " {\n", NULL); Printv(f->def, fndef, " __asm__(\"", go_prefix, "_", wname, "\");\n", NULL);
Printv(f->def, fndef, " {\n", NULL);
Delete(fnname); Delete(fnname);
Delete(fndef);
if (SwigType_type(result) != T_VOID) { if (SwigType_type(result) != T_VOID) {
String *ln = NewString("go_result"); String *ln = NewString("go_result");
@ -2811,7 +2852,7 @@ private:
if (!gccgo_flag) { if (!gccgo_flag) {
Printv(f_c_directors, "extern \"C\" void ", wname, "(void*, int);\n", NULL); Printv(f_c_directors, "extern \"C\" void ", wname, "(void*, int);\n", NULL);
} else { } else {
Printv(f_c_directors, "extern \"C\" void ", wname, "(void*) __asm__(\"", go_prefix, ".", package, ".", go_name, "\");\n", NULL); Printv(f_c_directors, "extern \"C\" void ", wname, "(void*) __asm__(\"", go_prefix, ".", go_name, "\");\n", NULL);
} }
} }
@ -3453,7 +3494,7 @@ private:
Delete(fnname); Delete(fnname);
Printv(f_c_directors, " __asm__(\"", go_prefix, ".", package, ".", callback_name, "\");\n", NULL); Printv(f_c_directors, " __asm__(\"", go_prefix, ".", callback_name, "\");\n", NULL);
} }
Delete(go_with_over_name); Delete(go_with_over_name);
@ -4924,6 +4965,7 @@ extern "C" Language *swig_go(void) {
const char * const GO::usage = (char *) "\ const char * const GO::usage = (char *) "\
Go Options (available with -go)\n\ Go Options (available with -go)\n\
-gccgo - Generate code for gccgo rather than 6g/8g\n\ -gccgo - Generate code for gccgo rather than 6g/8g\n\
-go-pkgpath <p> - Like gccgo -fgo-pkgpath option\n\
-go-prefix <p> - Like gccgo -fgo-prefix option\n\ -go-prefix <p> - Like gccgo -fgo-prefix option\n\
-longsize <s> - Set size of C/C++ long type--32 or 64 bits\n\ -longsize <s> - Set size of C/C++ long type--32 or 64 bits\n\
-intgosize <s> - Set size of Go int type--32 or 64 bits\n\ -intgosize <s> - Set size of Go int type--32 or 64 bits\n\