When generating Go code, make -cgo the default.

Add new -no-cgo option to disable the default.
Keep -cgo as a no-op for existing users.
This commit is contained in:
Ian Lance Taylor 2019-04-19 12:49:56 -07:00
commit 77f075b6e8
3 changed files with 38 additions and 29 deletions

View file

@ -125,16 +125,15 @@ Go distribution. To generate code for gccgo, you should also use the
</p> </p>
<p> <p>
When using the <tt>-cgo</tt> option, SWIG will generate files that can be used By default SWIG will generate files that can be used directly
directly by <tt>go build</tt>. Starting with the Go 1.5 distribution the by <tt>go build</tt>. This requires Go 1.2 or later. Put your SWIG
<tt>-cgo</tt> option has to be given. Put your SWIG interface file in a interface file in a directory under GOPATH/src, and give it a name
directory under GOPATH/src, and give it a name that does <b>not</b> end in the that does <b>not</b> end in the .swig or .swigcxx extension.
.swig or .swigcxx extension. Typically the SWIG interface file extension is .i Typically the SWIG interface file extension is .i in this case.
in this case.
</p> </p>
<div class="code"><pre> <div class="code"><pre>
% swig -go -cgo example.i % swig -go example.i
% go install % go install
</pre></div> </pre></div>
@ -144,15 +143,16 @@ usual.
</p> </p>
<p> <p>
To use SWIG without the <tt>-cgo</tt> option, more steps are required. Recall SWIG can be used without cgo, via the <tt>-no-cgo</tt> option, but
that this only works with Go versions before 1.5. When using Go version 1.2 or more steps are required. This only works with Go versions before 1.5.
later, or when using gccgo, the code generated by SWIG can be linked directly When using Go version 1.2 or later, or when using gccgo, the code
into the Go program. A typical command sequence when using the Go compiler of generated by SWIG can be linked directly into the Go program. A
the Go distribution would look like this: typical command sequence when using the Go compiler of the Go
distribution would look like this:
</p> </p>
<div class="code"><pre> <div class="code"><pre>
% swig -go example.i % swig -go -no-cgo example.i
% gcc -c code.c # The C library being wrapped. % gcc -c code.c # The C library being wrapped.
% gcc -c example_wrap.c % gcc -c example_wrap.c
% go tool 6g example.go % go tool 6g example.go
@ -169,7 +169,7 @@ sequence for this approach would look like this:
</p> </p>
<div class="code"><pre> <div class="code"><pre>
% swig -go -use-shlib example.i % swig -go -no-cgo -use-shlib example.i
% gcc -c -fpic example.c % gcc -c -fpic example.c
% gcc -c -fpic example_wrap.c % gcc -c -fpic example_wrap.c
% gcc -shared example.o example_wrap.o -o example.so % gcc -shared example.o example_wrap.o -o example.so
@ -200,10 +200,15 @@ swig -go -help
<tr> <tr>
<td>-cgo</td> <td>-cgo</td>
<td>Generate files to be used as input for the Go cgo tool. This <td>Generate files to be used as input for the Go cgo tool. This is
option is required for Go 1.5 and later, and works for Go 1.2 and the default.</td>
later. In the future this option will likely become the </tr>
default.</td>
<tr>
<td>-no-cgo</td>
<td>Generate files that can be used directly, rather than via the Go
cgo tool. This option does not work with Go 1.5 or later. It is
required for versions of Go before 1.2.</td>
</tr> </tr>
<tr> <tr>
@ -268,11 +273,11 @@ swig -go -help
<p>There are two different approaches to generating wrapper files, <p>There are two different approaches to generating wrapper files,
controlled by SWIG's <tt>-cgo</tt> option. The <tt>-cgo</tt> option controlled by SWIG's <tt>-no-cgo</tt> option. The <tt>-no-cgo</tt>
works with Go version 1.2 or later. It is required when using Go option only works with version of Go before 1.5. It is required
version 1.5 or later.</p> when using versions of Go before 1.2.</p>
<p>With or without the <tt>-cgo</tt> option, SWIG will generate the <p>With or without the <tt>-no-cgo</tt> option, SWIG will generate the
following files when generating wrapper code:</p> following files when generating wrapper code:</p>
<ul> <ul>
@ -296,8 +301,8 @@ or C++ compiler.
</li> </li>
</ul> </ul>
<p>When neither the <tt>-cgo</tt> nor the <tt>-gccgo</tt> option is <p>When the <tt>-no-cgo</tt> option is used, and the <tt>-gccgo</tt>
used, SWIG will also generate an additional file:</p> option is not used, SWIG will also generate an additional file:</p>
<ul> <ul>
<li> <li>

View file

@ -1468,7 +1468,7 @@ GOGCCOBJS = $(GOSRCS:.go=.@OBJEXT@)
# ---------------------------------------------------------------- # ----------------------------------------------------------------
go_nocgo: $(SRCDIR_SRCS) go_nocgo: $(SRCDIR_SRCS)
$(SWIG) -go $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH) $(SWIG) -go -no-cgo $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH)
if $(GO12) || $(GO13) || $(GO15) || $(GOGCC); then \ if $(GO12) || $(GO13) || $(GO15) || $(GOGCC); then \
$(CC) -g -c $(CPPFLAGS) $(CFLAGS) $(SRCDIR_SRCS) $(ISRCS) $(INCLUDES); \ $(CC) -g -c $(CPPFLAGS) $(CFLAGS) $(SRCDIR_SRCS) $(ISRCS) $(INCLUDES); \
else \ else \
@ -1504,7 +1504,7 @@ go_nocgo: $(SRCDIR_SRCS)
fi fi
go: $(SRCDIR_SRCS) go: $(SRCDIR_SRCS)
$(SWIG) -go -cgo $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH) $(SWIG) -go $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH)
@mkdir gopath 2>/dev/null || true @mkdir gopath 2>/dev/null || true
@mkdir gopath/src 2>/dev/null || true @mkdir gopath/src 2>/dev/null || true
@mkdir gopath/src/$(INTERFACE:.i=) 2>/dev/null || true @mkdir gopath/src/$(INTERFACE:.i=) 2>/dev/null || true
@ -1548,7 +1548,7 @@ go: $(SRCDIR_SRCS)
# ---------------------------------------------------------------- # ----------------------------------------------------------------
go_cpp_nocgo: $(SRCDIR_SRCS) go_cpp_nocgo: $(SRCDIR_SRCS)
$(SWIG) -go -c++ $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH) $(SWIG) -go -c++ -no-cgo $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH)
if $(GO12) || $(GO13) || $(GO15) || $(GOGCC); then \ if $(GO12) || $(GO13) || $(GO15) || $(GOGCC); then \
if test -n "$(SRCDIR_CXXSRCS)$(SRCDIR_SRCS)"; then \ if test -n "$(SRCDIR_CXXSRCS)$(SRCDIR_SRCS)"; then \
$(CXX) -g -c $(CPPFLAGS) $(CXXFLAGS) $(SRCDIR_CXXSRCS) $(SRCDIR_SRCS) $(INCLUDES); \ $(CXX) -g -c $(CPPFLAGS) $(CXXFLAGS) $(SRCDIR_CXXSRCS) $(SRCDIR_SRCS) $(INCLUDES); \
@ -1596,7 +1596,7 @@ go_cpp_nocgo: $(SRCDIR_SRCS)
fi fi
go_cpp: $(SRCDIR_SRCS) go_cpp: $(SRCDIR_SRCS)
$(SWIG) -go -c++ -cgo $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH) $(SWIG) -go -c++ $(GOOPT) $(GOSWIGARG) $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH)
@mkdir gopath 2>/dev/null || true @mkdir gopath 2>/dev/null || true
@mkdir gopath/src 2>/dev/null || true @mkdir gopath/src 2>/dev/null || true
@mkdir gopath/src/$(INTERFACE:.i=) 2>/dev/null || true @mkdir gopath/src/$(INTERFACE:.i=) 2>/dev/null || true

View file

@ -199,7 +199,7 @@ class GO:public Language {
public: public:
GO():package(NULL), GO():package(NULL),
module(NULL), module(NULL),
cgo_flag(false), cgo_flag(true),
gccgo_flag(false), gccgo_flag(false),
go_prefix(NULL), go_prefix(NULL),
prefix_option(NULL), prefix_option(NULL),
@ -269,6 +269,9 @@ private:
} else if (strcmp(argv[i], "-cgo") == 0) { } else if (strcmp(argv[i], "-cgo") == 0) {
Swig_mark_arg(i); Swig_mark_arg(i);
cgo_flag = true; cgo_flag = true;
} else if (strcmp(argv[i], "-no-cgo") == 0) {
Swig_mark_arg(i);
cgo_flag = false;
} else if (strcmp(argv[i], "-gccgo") == 0) { } else if (strcmp(argv[i], "-gccgo") == 0) {
Swig_mark_arg(i); Swig_mark_arg(i);
gccgo_flag = true; gccgo_flag = true;
@ -6979,6 +6982,7 @@ extern "C" Language *swig_go(void) {
const char * const GO::usage = "\ const char * const GO::usage = "\
Go Options (available with -go)\n\ Go Options (available with -go)\n\
-cgo - Generate cgo input files\n\ -cgo - Generate cgo input files\n\
-no-cgo - Do not generate cgo input files\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-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\