2009-11-04 15:17:36 -08:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Cgo enables the creation of Go packages that call C code.
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
Using cgo with the go command
|
2009-11-04 15:17:36 -08:00
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
To use cgo write normal Go code that imports a pseudo-package "C".
|
|
|
|
|
The Go code can then refer to types such as C.size_t, variables such
|
|
|
|
|
as C.stdout, or functions such as C.putchar.
|
2009-11-04 15:17:36 -08:00
|
|
|
|
|
|
|
|
If the import of "C" is immediately preceded by a comment, that
|
2012-02-19 13:32:55 -05:00
|
|
|
comment, called the preamble, is used as a header when compiling
|
|
|
|
|
the C parts of the package. For example:
|
2009-11-04 15:17:36 -08:00
|
|
|
|
|
|
|
|
// #include <stdio.h>
|
|
|
|
|
// #include <errno.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
See $GOROOT/misc/cgo/stdio and $GOROOT/misc/cgo/gmp for examples. See
|
|
|
|
|
"C? Go? Cgo!" for an introduction to using cgo:
|
|
|
|
|
http://golang.org/doc/articles/c_go_cgo.html.
|
|
|
|
|
|
|
|
|
|
CFLAGS, CPPFLAGS, CXXFLAGS and LDFLAGS may be defined with pseudo #cgo
|
|
|
|
|
directives within these comments to tweak the behavior of the C or C++
|
|
|
|
|
compiler. Values defined in multiple directives are concatenated
|
2013-09-23 16:29:53 -04:00
|
|
|
together. The directive can include a list of build constraints limiting its
|
|
|
|
|
effect to systems satisfying one of the constraints
|
|
|
|
|
(see http://golang.org/pkg/go/build/#hdr-Build_Constraints for details about the constraint syntax).
|
|
|
|
|
For example:
|
2011-02-01 08:44:18 -05:00
|
|
|
|
|
|
|
|
// #cgo CFLAGS: -DPNG_DEBUG=1
|
2013-09-23 16:29:53 -04:00
|
|
|
// #cgo amd64 386 CFLAGS: -DX86=1
|
2011-02-01 08:44:18 -05:00
|
|
|
// #cgo LDFLAGS: -lpng
|
|
|
|
|
// #include <png.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
cmd/go: Add support for including C++ files in packages
* Add a CXXFiles field to Package, which includes .cc, .cpp and .cxx files.
* CXXFiles are compiled using g++, which can be overridden using the CXX environment variable.
* Include .hh, .hpp and .hxx files in HFiles.
* Add support for CPPFLAGS (used for both C and C++) and CXXFLAGS (used only for C++) in cgo directive.
* Changed pkg-config cgo directive to modify CPPFLAGS rather than CFLAGS, so both C and C++ files get any flag returned by pkg-config --cflags.
Fixes #1476.
R=iant, r
CC=bradfitz, gobot, golang-dev, iant, minux.ma, remyoudompheng, seb.binet
https://golang.org/cl/8248043
2013-05-31 11:33:36 -07:00
|
|
|
Alternatively, CPPFLAGS and LDFLAGS may be obtained via the pkg-config
|
2011-05-26 22:19:23 -03:00
|
|
|
tool using a '#cgo pkg-config:' directive followed by the package names.
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
// #cgo pkg-config: png cairo
|
|
|
|
|
// #include <png.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
When building, the CGO_CFLAGS, CGO_CPPFLAGS, CGO_CXXFLAGS and
|
|
|
|
|
CGO_LDFLAGS environment variables are added to the flags derived from
|
|
|
|
|
these directives. Package-specific flags should be set using the
|
|
|
|
|
directives, not the environment variables, so that builds work in
|
|
|
|
|
unmodified environments.
|
|
|
|
|
|
cmd/cgo: explicitly state that #cgo directives across multiple files are concatenated
This is a quick documentation change/clarification, as this
confused me before: in my own cgo-based projects, I currently have
identical #cgo directives in each relevant source file, and I notice
with go build -x that cgo is combining the directives, leading to
pkg-config invocations with the same package name (gtk+-3.0, in my
case) repeated several times, or on Mac OS X, LDFLAGS listing
-framework Foundation -framework AppKit multiple times. Since I am
about to add a CFLAGS as well, I checked the source to cmd/cgo and
go/build (where the work is actually done) to see if that still holds
true there. Hopefully other people who have made the same mistake I
have (I don't know if anyone has) can remove the excess declarations
now; this should make things slightly easier to manage as well.
LGTM=iant
R=golang-codereviews, gobot, iant
CC=golang-codereviews
https://golang.org/cl/91520046
2014-05-21 16:01:54 -07:00
|
|
|
All the cgo CPPFLAGS and CFLAGS directives in a package are concatenated and
|
|
|
|
|
used to compile C files in that package. All the CPPFLAGS and CXXFLAGS
|
|
|
|
|
directives in a package are concatenated and used to compile C++ files in that
|
|
|
|
|
package. All the LDFLAGS directives in any package in the program are
|
|
|
|
|
concatenated and used at link time. All the pkg-config directives are
|
|
|
|
|
concatenated and sent to pkg-config simultaneously to add to each appropriate
|
|
|
|
|
set of command-line flags.
|
|
|
|
|
|
2014-12-08 07:28:40 -08:00
|
|
|
When the cgo directives are parsed, any occurrence of the string ${SRCDIR}
|
|
|
|
|
will be replaced by the absolute path to the directory containing the source
|
|
|
|
|
file. This allows pre-compiled static libraries to be included in the package
|
|
|
|
|
directory and linked properly.
|
|
|
|
|
For example if package foo is in the directory /go/src/foo:
|
|
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -L${SRCDIR}/libs -lfoo
|
|
|
|
|
|
|
|
|
|
Will be expanded to:
|
|
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -L/go/src/foo/libs -lfoo
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
When the Go tool sees that one or more Go files use the special import
|
|
|
|
|
"C", it will look for other non-Go files in the directory and compile
|
|
|
|
|
them as part of the Go package. Any .c, .s, or .S files will be
|
|
|
|
|
compiled with the C compiler. Any .cc, .cpp, or .cxx files will be
|
|
|
|
|
compiled with the C++ compiler. Any .h, .hh, .hpp, or .hxx files will
|
|
|
|
|
not be compiled separately, but, if these header files are changed,
|
|
|
|
|
the C and C++ files will be recompiled. The default C and C++
|
|
|
|
|
compilers may be changed by the CC and CXX environment variables,
|
|
|
|
|
respectively; those environment variables may include command line
|
|
|
|
|
options.
|
|
|
|
|
|
2014-05-20 01:32:31 -04:00
|
|
|
To enable cgo during cross compiling builds, set the CGO_ENABLED
|
|
|
|
|
environment variable to 1 when building the Go tools with make.bash.
|
|
|
|
|
Also, set CC_FOR_TARGET to the C cross compiler for the target. CC will
|
|
|
|
|
be used for compiling for the host.
|
|
|
|
|
|
|
|
|
|
After the Go tools are built, when running the go command, CC_FOR_TARGET is
|
|
|
|
|
ignored. The value of CC_FOR_TARGET when running make.bash is the default
|
|
|
|
|
compiler. However, you can set the environment variable CC, not CC_FOR_TARGET,
|
|
|
|
|
to control the compiler when running the go tool.
|
|
|
|
|
|
|
|
|
|
CXX_FOR_TARGET works in a similar way for C++ code.
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
Go references to C
|
2012-03-07 11:44:47 -05:00
|
|
|
|
2013-07-12 04:34:04 +08:00
|
|
|
Within the Go file, C's struct field names that are keywords in Go
|
2011-05-26 22:19:23 -03:00
|
|
|
can be accessed by prefixing them with an underscore: if x points at a C
|
|
|
|
|
struct with a field named "type", x._type accesses the field.
|
2014-05-12 23:48:20 -04:00
|
|
|
C struct fields that cannot be expressed in Go, such as bit fields
|
|
|
|
|
or misaligned data, are omitted in the Go struct, replaced by
|
|
|
|
|
appropriate padding to reach the next field or the end of the struct.
|
2010-07-14 17:17:53 -07:00
|
|
|
|
2010-09-21 22:41:19 -04:00
|
|
|
The standard C numeric types are available under the names
|
|
|
|
|
C.char, C.schar (signed char), C.uchar (unsigned char),
|
|
|
|
|
C.short, C.ushort (unsigned short), C.int, C.uint (unsigned int),
|
|
|
|
|
C.long, C.ulong (unsigned long), C.longlong (long long),
|
|
|
|
|
C.ulonglong (unsigned long long), C.float, C.double.
|
2011-07-28 12:39:50 -04:00
|
|
|
The C type void* is represented by Go's unsafe.Pointer.
|
2010-09-21 22:41:19 -04:00
|
|
|
|
2010-07-14 17:17:53 -07:00
|
|
|
To access a struct, union, or enum type directly, prefix it with
|
|
|
|
|
struct_, union_, or enum_, as in C.struct_stat.
|
|
|
|
|
|
2013-07-12 04:34:04 +08:00
|
|
|
As Go doesn't have support for C's union type in the general case,
|
|
|
|
|
C's union types are represented as a Go byte array with the same length.
|
|
|
|
|
|
2012-02-19 13:32:55 -05:00
|
|
|
Go structs cannot embed fields with C types.
|
|
|
|
|
|
2014-05-31 00:51:46 -07:00
|
|
|
Cgo translates C types into equivalent unexported Go types.
|
|
|
|
|
Because the translations are unexported, a Go package should not
|
|
|
|
|
expose C types in its exported API: a C type used in one Go package
|
|
|
|
|
is different from the same C type used in another.
|
|
|
|
|
|
2013-01-22 02:52:34 +08:00
|
|
|
Any C function (even void functions) may be called in a multiple
|
|
|
|
|
assignment context to retrieve both the return value (if any) and the
|
|
|
|
|
C errno variable as an error (use _ to skip the result value if the
|
|
|
|
|
function returns void). For example:
|
2010-07-14 17:17:53 -07:00
|
|
|
|
2013-03-21 20:06:25 -07:00
|
|
|
n, err := C.sqrt(-1)
|
2013-01-22 02:52:34 +08:00
|
|
|
_, err := C.voidFunc()
|
2010-07-14 17:17:53 -07:00
|
|
|
|
2013-08-13 12:42:21 -04:00
|
|
|
Calling C function pointers is currently not supported, however you can
|
|
|
|
|
declare Go variables which hold C function pointers and pass them
|
|
|
|
|
back and forth between Go and C. C code may call function pointers
|
|
|
|
|
received from Go. For example:
|
|
|
|
|
|
|
|
|
|
package main
|
2013-09-17 07:11:40 -07:00
|
|
|
|
2013-08-13 12:42:21 -04:00
|
|
|
// typedef int (*intFunc) ();
|
|
|
|
|
//
|
|
|
|
|
// int
|
|
|
|
|
// bridge_int_func(intFunc f)
|
|
|
|
|
// {
|
|
|
|
|
// return f();
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// int fortytwo()
|
|
|
|
|
// {
|
|
|
|
|
// return 42;
|
|
|
|
|
// }
|
|
|
|
|
import "C"
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
f := C.intFunc(C.fortytwo)
|
|
|
|
|
fmt.Println(int(C.bridge_int_func(f)))
|
|
|
|
|
// Output: 42
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-21 22:41:19 -04:00
|
|
|
In C, a function argument written as a fixed size array
|
|
|
|
|
actually requires a pointer to the first element of the array.
|
|
|
|
|
C compilers are aware of this calling convention and adjust
|
|
|
|
|
the call accordingly, but Go cannot. In Go, you must pass
|
2014-11-10 08:12:43 -08:00
|
|
|
the pointer to the first element explicitly: C.f(&C.x[0]).
|
2010-09-21 22:41:19 -04:00
|
|
|
|
2011-07-28 12:39:50 -04:00
|
|
|
A few special functions convert between Go and C types
|
|
|
|
|
by making copies of the data. In pseudo-Go definitions:
|
|
|
|
|
|
2011-08-31 08:17:01 -04:00
|
|
|
// Go string to C string
|
|
|
|
|
// The C string is allocated in the C heap using malloc.
|
|
|
|
|
// It is the caller's responsibility to arrange for it to be
|
2013-01-22 02:52:34 +08:00
|
|
|
// freed, such as by calling C.free (be sure to include stdlib.h
|
|
|
|
|
// if C.free is needed).
|
2011-07-28 12:39:50 -04:00
|
|
|
func C.CString(string) *C.char
|
|
|
|
|
|
|
|
|
|
// C string to Go string
|
|
|
|
|
func C.GoString(*C.char) string
|
|
|
|
|
|
|
|
|
|
// C string, length to Go string
|
|
|
|
|
func C.GoStringN(*C.char, C.int) string
|
|
|
|
|
|
|
|
|
|
// C pointer, length to Go []byte
|
|
|
|
|
func C.GoBytes(unsafe.Pointer, C.int) []byte
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
C references to Go
|
|
|
|
|
|
2011-11-18 18:24:59 -05:00
|
|
|
Go functions can be exported for use by C code in the following way:
|
|
|
|
|
|
|
|
|
|
//export MyFunction
|
|
|
|
|
func MyFunction(arg1, arg2 int, arg3 string) int64 {...}
|
|
|
|
|
|
|
|
|
|
//export MyFunction2
|
2011-12-06 08:16:39 -08:00
|
|
|
func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
|
2011-11-18 18:24:59 -05:00
|
|
|
|
|
|
|
|
They will be available in the C code as:
|
|
|
|
|
|
|
|
|
|
extern int64 MyFunction(int arg1, int arg2, GoString arg3);
|
|
|
|
|
extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
found in the _cgo_export.h generated header, after any preambles
|
2012-02-19 13:32:55 -05:00
|
|
|
copied from the cgo input files. Functions with multiple
|
2011-11-18 18:24:59 -05:00
|
|
|
return values are mapped to functions returning a struct.
|
|
|
|
|
Not all Go types can be mapped to C types in a useful way.
|
|
|
|
|
|
2013-02-01 08:33:52 -08:00
|
|
|
Using //export in a file places a restriction on the preamble:
|
|
|
|
|
since it is copied into two different C output files, it must not
|
|
|
|
|
contain any definitions, only declarations. Definitions must be
|
|
|
|
|
placed in preambles in other files, or in C source files.
|
|
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
Using cgo directly
|
|
|
|
|
|
|
|
|
|
Usage:
|
2015-01-06 15:08:02 -08:00
|
|
|
go tool cgo [cgo options] [-- compiler options] gofiles...
|
2013-09-17 07:11:40 -07:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
Cgo transforms the specified input Go source files into several output
|
|
|
|
|
Go and C source files.
|
2009-11-04 15:17:36 -08:00
|
|
|
|
2013-09-17 07:11:40 -07:00
|
|
|
The compiler options are passed through uninterpreted when
|
|
|
|
|
invoking the C compiler to compile the C parts of the package.
|
2009-11-04 15:17:36 -08:00
|
|
|
|
2013-09-03 21:23:52 -07:00
|
|
|
The following options are available when running cgo directly:
|
|
|
|
|
|
|
|
|
|
-dynimport file
|
|
|
|
|
Write list of symbols imported by file. Write to
|
|
|
|
|
-dynout argument or to standard output. Used by go
|
|
|
|
|
build when building a cgo package.
|
|
|
|
|
-dynout file
|
|
|
|
|
Write -dynimport output to file.
|
2015-01-06 15:08:02 -08:00
|
|
|
-dynpackage package
|
|
|
|
|
Set Go package for -dynimport output.
|
2013-09-03 21:23:52 -07:00
|
|
|
-dynlinker
|
|
|
|
|
Write dynamic linker as part of -dynimport output.
|
|
|
|
|
-godefs
|
|
|
|
|
Write out input file in Go syntax replacing C package
|
|
|
|
|
names with real values. Used to generate files in the
|
|
|
|
|
syscall package when bootstrapping a new target.
|
|
|
|
|
-objdir directory
|
|
|
|
|
Put all generated files in directory.
|
2015-05-05 18:19:28 -07:00
|
|
|
-importpath string
|
|
|
|
|
The import path for the Go package. Optional; used for
|
|
|
|
|
nicer comments in the generated files.
|
2013-09-03 21:23:52 -07:00
|
|
|
-gccgo
|
|
|
|
|
Generate output for the gccgo compiler rather than the
|
|
|
|
|
gc compiler.
|
|
|
|
|
-gccgoprefix prefix
|
|
|
|
|
The -fgo-prefix option to be used with gccgo.
|
|
|
|
|
-gccgopkgpath path
|
|
|
|
|
The -fgo-pkgpath option to be used with gccgo.
|
|
|
|
|
-import_runtime_cgo
|
|
|
|
|
If set (which it is by default) import runtime/cgo in
|
|
|
|
|
generated output.
|
|
|
|
|
-import_syscall
|
|
|
|
|
If set (which it is by default) import syscall in
|
|
|
|
|
generated output.
|
|
|
|
|
-debug-define
|
|
|
|
|
Debugging option. Print #defines.
|
|
|
|
|
-debug-gcc
|
|
|
|
|
Debugging option. Trace C compiler execution and output.
|
2009-11-04 15:17:36 -08:00
|
|
|
*/
|
2013-02-19 11:19:58 -08:00
|
|
|
package main
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Implementation details.
|
|
|
|
|
|
|
|
|
|
Cgo provides a way for Go programs to call C code linked into the same
|
|
|
|
|
address space. This comment explains the operation of cgo.
|
|
|
|
|
|
|
|
|
|
Cgo reads a set of Go source files and looks for statements saying
|
|
|
|
|
import "C". If the import has a doc comment, that comment is
|
|
|
|
|
taken as literal C code to be used as a preamble to any C code
|
|
|
|
|
generated by cgo. A typical preamble #includes necessary definitions:
|
|
|
|
|
|
|
|
|
|
// #include <stdio.h>
|
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
|
|
For more details about the usage of cgo, see the documentation
|
|
|
|
|
comment at the top of this file.
|
|
|
|
|
|
|
|
|
|
Understanding C
|
|
|
|
|
|
|
|
|
|
Cgo scans the Go source files that import "C" for uses of that
|
|
|
|
|
package, such as C.puts. It collects all such identifiers. The next
|
|
|
|
|
step is to determine each kind of name. In C.xxx the xxx might refer
|
|
|
|
|
to a type, a function, a constant, or a global variable. Cgo must
|
|
|
|
|
decide which.
|
|
|
|
|
|
|
|
|
|
The obvious thing for cgo to do is to process the preamble, expanding
|
|
|
|
|
#includes and processing the corresponding C code. That would require
|
|
|
|
|
a full C parser and type checker that was also aware of any extensions
|
|
|
|
|
known to the system compiler (for example, all the GNU C extensions) as
|
|
|
|
|
well as the system-specific header locations and system-specific
|
|
|
|
|
pre-#defined macros. This is certainly possible to do, but it is an
|
|
|
|
|
enormous amount of work.
|
|
|
|
|
|
|
|
|
|
Cgo takes a different approach. It determines the meaning of C
|
|
|
|
|
identifiers not by parsing C code but by feeding carefully constructed
|
|
|
|
|
programs into the system C compiler and interpreting the generated
|
|
|
|
|
error messages, debug information, and object files. In practice,
|
|
|
|
|
parsing these is significantly less work and more robust than parsing
|
|
|
|
|
C source.
|
|
|
|
|
|
|
|
|
|
Cgo first invokes gcc -E -dM on the preamble, in order to find out
|
|
|
|
|
about simple #defines for constants and the like. These are recorded
|
|
|
|
|
for later use.
|
|
|
|
|
|
|
|
|
|
Next, cgo needs to identify the kinds for each identifier. For the
|
|
|
|
|
identifiers C.foo and C.bar, cgo generates this C program:
|
|
|
|
|
|
|
|
|
|
<preamble>
|
cmd/cgo: stop using compiler error message text to analyze C names
The old approach to determining whether "name" was a type, constant,
or expression was to compile the C program
name;
and scan the errors and warnings generated by the compiler.
This requires looking for specific substrings in the errors and warnings,
which ties the implementation to specific compiler versions.
As compilers change their errors or drop warnings, cgo breaks.
This happens slowly but it does happen.
Clang in particular (now required on OS X) has a significant churn rate.
The new approach compiles a slightly more complex program
that is either valid C or not valid C depending on what kind of
thing "name" is. It uses only the presence or absence of an error
message on a particular line, not the error text itself. The program is:
// error if and only if name is undeclared
void f1(void) { typeof(name) *x; }
// error if and only if name is not a type
void f2(void) { name *x; }
// error if and only if name is not an integer constant
void f3(void) { enum { x = (name)*1 }; }
I had not been planning to do this until Go 1.3, because it is a
non-trivial change, but it fixes a real Xcode 5 problem in Go 1.2,
and the new code is easier to understand than the old code.
It should be significantly more robust.
Fixes #6596.
Fixes #6612.
R=golang-dev, r, james, iant
CC=golang-dev
https://golang.org/cl/15070043
2013-10-18 15:56:25 -04:00
|
|
|
#line 1 "not-declared"
|
2013-10-22 18:33:23 -04:00
|
|
|
void __cgo_f_xxx_1(void) { __typeof__(foo) *__cgo_undefined__; }
|
cmd/cgo: stop using compiler error message text to analyze C names
The old approach to determining whether "name" was a type, constant,
or expression was to compile the C program
name;
and scan the errors and warnings generated by the compiler.
This requires looking for specific substrings in the errors and warnings,
which ties the implementation to specific compiler versions.
As compilers change their errors or drop warnings, cgo breaks.
This happens slowly but it does happen.
Clang in particular (now required on OS X) has a significant churn rate.
The new approach compiles a slightly more complex program
that is either valid C or not valid C depending on what kind of
thing "name" is. It uses only the presence or absence of an error
message on a particular line, not the error text itself. The program is:
// error if and only if name is undeclared
void f1(void) { typeof(name) *x; }
// error if and only if name is not a type
void f2(void) { name *x; }
// error if and only if name is not an integer constant
void f3(void) { enum { x = (name)*1 }; }
I had not been planning to do this until Go 1.3, because it is a
non-trivial change, but it fixes a real Xcode 5 problem in Go 1.2,
and the new code is easier to understand than the old code.
It should be significantly more robust.
Fixes #6596.
Fixes #6612.
R=golang-dev, r, james, iant
CC=golang-dev
https://golang.org/cl/15070043
2013-10-18 15:56:25 -04:00
|
|
|
#line 1 "not-type"
|
|
|
|
|
void __cgo_f_xxx_2(void) { foo *__cgo_undefined__; }
|
|
|
|
|
#line 1 "not-const"
|
|
|
|
|
void __cgo_f_xxx_3(void) { enum { __cgo_undefined__ = (foo)*1 }; }
|
|
|
|
|
#line 2 "not-declared"
|
2013-10-22 18:33:23 -04:00
|
|
|
void __cgo_f_xxx_1(void) { __typeof__(bar) *__cgo_undefined__; }
|
cmd/cgo: stop using compiler error message text to analyze C names
The old approach to determining whether "name" was a type, constant,
or expression was to compile the C program
name;
and scan the errors and warnings generated by the compiler.
This requires looking for specific substrings in the errors and warnings,
which ties the implementation to specific compiler versions.
As compilers change their errors or drop warnings, cgo breaks.
This happens slowly but it does happen.
Clang in particular (now required on OS X) has a significant churn rate.
The new approach compiles a slightly more complex program
that is either valid C or not valid C depending on what kind of
thing "name" is. It uses only the presence or absence of an error
message on a particular line, not the error text itself. The program is:
// error if and only if name is undeclared
void f1(void) { typeof(name) *x; }
// error if and only if name is not a type
void f2(void) { name *x; }
// error if and only if name is not an integer constant
void f3(void) { enum { x = (name)*1 }; }
I had not been planning to do this until Go 1.3, because it is a
non-trivial change, but it fixes a real Xcode 5 problem in Go 1.2,
and the new code is easier to understand than the old code.
It should be significantly more robust.
Fixes #6596.
Fixes #6612.
R=golang-dev, r, james, iant
CC=golang-dev
https://golang.org/cl/15070043
2013-10-18 15:56:25 -04:00
|
|
|
#line 2 "not-type"
|
|
|
|
|
void __cgo_f_xxx_2(void) { bar *__cgo_undefined__; }
|
|
|
|
|
#line 2 "not-const"
|
|
|
|
|
void __cgo_f_xxx_3(void) { enum { __cgo_undefined__ = (bar)*1 }; }
|
|
|
|
|
|
|
|
|
|
This program will not compile, but cgo can use the presence or absence
|
|
|
|
|
of an error message on a given line to deduce the information it
|
|
|
|
|
needs. The program is syntactically valid regardless of whether each
|
|
|
|
|
name is a type or an ordinary identifier, so there will be no syntax
|
|
|
|
|
errors that might stop parsing early.
|
|
|
|
|
|
|
|
|
|
An error on not-declared:1 indicates that foo is undeclared.
|
|
|
|
|
An error on not-type:1 indicates that foo is not a type (if declared at all, it is an identifier).
|
|
|
|
|
An error on not-const:1 indicates that foo is not an integer constant.
|
|
|
|
|
|
|
|
|
|
The line number specifies the name involved. In the example, 1 is foo and 2 is bar.
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
Next, cgo must learn the details of each type, variable, function, or
|
|
|
|
|
constant. It can do this by reading object files. If cgo has decided
|
|
|
|
|
that t1 is a type, v2 and v3 are variables or functions, and c4, c5,
|
|
|
|
|
and c6 are constants, it generates:
|
|
|
|
|
|
|
|
|
|
<preamble>
|
2013-10-22 18:33:23 -04:00
|
|
|
__typeof__(t1) *__cgo__1;
|
|
|
|
|
__typeof__(v2) *__cgo__2;
|
|
|
|
|
__typeof__(v3) *__cgo__3;
|
|
|
|
|
__typeof__(c4) *__cgo__4;
|
2013-02-27 20:55:01 -08:00
|
|
|
enum { __cgo_enum__4 = c4 };
|
2013-10-22 18:33:23 -04:00
|
|
|
__typeof__(c5) *__cgo__5;
|
2013-02-27 20:55:01 -08:00
|
|
|
enum { __cgo_enum__5 = c5 };
|
2013-10-22 18:33:23 -04:00
|
|
|
__typeof__(c6) *__cgo__6;
|
2013-02-27 20:55:01 -08:00
|
|
|
enum { __cgo_enum__6 = c6 };
|
|
|
|
|
|
|
|
|
|
long long __cgo_debug_data[] = {
|
|
|
|
|
0, // t1
|
|
|
|
|
0, // v2
|
|
|
|
|
0, // v3
|
|
|
|
|
c4,
|
|
|
|
|
c5,
|
|
|
|
|
c6,
|
|
|
|
|
1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
and again invokes the system C compiler, to produce an object file
|
|
|
|
|
containing debug information. Cgo parses the DWARF debug information
|
|
|
|
|
for __cgo__N to learn the type of each identifier. (The types also
|
|
|
|
|
distinguish functions from global variables.) If using a standard gcc,
|
|
|
|
|
cgo can parse the DWARF debug information for the __cgo_enum__N to
|
|
|
|
|
learn the identifier's value. The LLVM-based gcc on OS X emits
|
|
|
|
|
incomplete DWARF information for enums; in that case cgo reads the
|
|
|
|
|
constant values from the __cgo_debug_data from the object file's data
|
|
|
|
|
segment.
|
|
|
|
|
|
|
|
|
|
At this point cgo knows the meaning of each C.xxx well enough to start
|
|
|
|
|
the translation process.
|
|
|
|
|
|
|
|
|
|
Translating Go
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
[The rest of this comment refers to 6g, the Go compiler that is part
|
|
|
|
|
of the amd64 port of the gc Go toolchain. Everything here applies to
|
|
|
|
|
another architecture's compilers as well.]
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
Given the input Go files x.go and y.go, cgo generates these source
|
|
|
|
|
files:
|
|
|
|
|
|
|
|
|
|
x.cgo1.go # for 6g
|
|
|
|
|
y.cgo1.go # for 6g
|
|
|
|
|
_cgo_gotypes.go # for 6g
|
2015-01-06 15:08:02 -08:00
|
|
|
_cgo_import.go # for 6g (if -dynout _cgo_import.go)
|
2013-02-27 20:55:01 -08:00
|
|
|
x.cgo2.c # for gcc
|
|
|
|
|
y.cgo2.c # for gcc
|
2015-01-06 15:08:02 -08:00
|
|
|
_cgo_defun.c # for gcc (if -gccgo)
|
2013-02-27 20:55:01 -08:00
|
|
|
_cgo_export.c # for gcc
|
2015-01-06 15:08:02 -08:00
|
|
|
_cgo_export.h # for gcc
|
2013-02-27 20:55:01 -08:00
|
|
|
_cgo_main.c # for gcc
|
2015-01-06 15:08:02 -08:00
|
|
|
_cgo_flags # for alternative build tools
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
The file x.cgo1.go is a copy of x.go with the import "C" removed and
|
|
|
|
|
references to C.xxx replaced with names like _Cfunc_xxx or _Ctype_xxx.
|
|
|
|
|
The definitions of those identifiers, written as Go functions, types,
|
|
|
|
|
or variables, are provided in _cgo_gotypes.go.
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
Here is a _cgo_gotypes.go containing definitions for needed C types:
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
type _Ctype_char int8
|
|
|
|
|
type _Ctype_int int32
|
|
|
|
|
type _Ctype_void [0]byte
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
The _cgo_gotypes.go file also contains the definitions of the
|
|
|
|
|
functions. They all have similar bodies that invoke runtime·cgocall
|
|
|
|
|
to make a switch from the Go runtime world to the system C (GCC-based)
|
|
|
|
|
world.
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
For example, here is the definition of _Cfunc_puts:
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_static _cgo_be59f0f25121_Cfunc_puts
|
|
|
|
|
//go:linkname __cgofn__cgo_be59f0f25121_Cfunc_puts _cgo_be59f0f25121_Cfunc_puts
|
|
|
|
|
var __cgofn__cgo_be59f0f25121_Cfunc_puts byte
|
|
|
|
|
var _cgo_be59f0f25121_Cfunc_puts = unsafe.Pointer(&__cgofn__cgo_be59f0f25121_Cfunc_puts)
|
2013-02-27 20:55:01 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
func _Cfunc_puts(p0 *_Ctype_char) (r1 _Ctype_int) {
|
|
|
|
|
_cgo_runtime_cgocall_errno(_cgo_be59f0f25121_Cfunc_puts, uintptr(unsafe.Pointer(&p0)))
|
|
|
|
|
return
|
2013-02-27 20:55:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
The hexadecimal number is a hash of cgo's input, chosen to be
|
|
|
|
|
deterministic yet unlikely to collide with other uses. The actual
|
2013-02-28 11:31:52 -08:00
|
|
|
function _cgo_be59f0f25121_Cfunc_puts is implemented in a C source
|
2013-02-27 20:55:01 -08:00
|
|
|
file compiled by gcc, the file x.cgo2.c:
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_cgo_be59f0f25121_Cfunc_puts(void *v)
|
|
|
|
|
{
|
2015-03-25 17:50:35 -07:00
|
|
|
_cgo_wait_runtime_init_done();
|
2013-02-27 20:55:01 -08:00
|
|
|
struct {
|
|
|
|
|
char* p0;
|
|
|
|
|
int r;
|
|
|
|
|
char __pad12[4];
|
2013-06-09 22:06:29 +08:00
|
|
|
} __attribute__((__packed__, __gcc_struct__)) *a = v;
|
2013-02-27 20:55:01 -08:00
|
|
|
a->r = puts((void*)a->p0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-25 17:50:35 -07:00
|
|
|
It waits for Go runtime to be initialized (required for shared libraries),
|
|
|
|
|
extracts the arguments from the pointer to _Cfunc_puts's argument
|
2013-02-27 20:55:01 -08:00
|
|
|
frame, invokes the system C function (in this case, puts), stores the
|
|
|
|
|
result in the frame, and returns.
|
|
|
|
|
|
|
|
|
|
Linking
|
|
|
|
|
|
|
|
|
|
Once the _cgo_export.c and *.cgo2.c files have been compiled with gcc,
|
|
|
|
|
they need to be linked into the final binary, along with the libraries
|
|
|
|
|
they might depend on (in the case of puts, stdio). 6l has been
|
|
|
|
|
extended to understand basic ELF files, but it does not understand ELF
|
|
|
|
|
in the full complexity that modern C libraries embrace, so it cannot
|
|
|
|
|
in general generate direct references to the system libraries.
|
|
|
|
|
|
|
|
|
|
Instead, the build process generates an object file using dynamic
|
|
|
|
|
linkage to the desired libraries. The main function is provided by
|
|
|
|
|
_cgo_main.c:
|
|
|
|
|
|
|
|
|
|
int main() { return 0; }
|
|
|
|
|
void crosscall2(void(*fn)(void*, int), void *a, int c) { }
|
2015-03-25 17:50:35 -07:00
|
|
|
void _cgo_wait_runtime_init_done() { }
|
2013-02-27 20:55:01 -08:00
|
|
|
void _cgo_allocate(void *a, int c) { }
|
|
|
|
|
void _cgo_panic(void *a, int c) { }
|
|
|
|
|
|
|
|
|
|
The extra functions here are stubs to satisfy the references in the C
|
|
|
|
|
code generated for gcc. The build process links this stub, along with
|
|
|
|
|
_cgo_export.c and *.cgo2.c, into a dynamic executable and then lets
|
|
|
|
|
cgo examine the executable. Cgo records the list of shared library
|
|
|
|
|
references and resolved names and writes them into a new file
|
2015-01-06 15:08:02 -08:00
|
|
|
_cgo_import.go, which looks like:
|
2013-02-27 20:55:01 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_dynamic_linker "/lib64/ld-linux-x86-64.so.2"
|
|
|
|
|
//go:cgo_import_dynamic puts puts#GLIBC_2.2.5 "libc.so.6"
|
|
|
|
|
//go:cgo_import_dynamic __libc_start_main __libc_start_main#GLIBC_2.2.5 "libc.so.6"
|
|
|
|
|
//go:cgo_import_dynamic stdout stdout#GLIBC_2.2.5 "libc.so.6"
|
|
|
|
|
//go:cgo_import_dynamic fflush fflush#GLIBC_2.2.5 "libc.so.6"
|
|
|
|
|
//go:cgo_import_dynamic _ _ "libpthread.so.0"
|
|
|
|
|
//go:cgo_import_dynamic _ _ "libc.so.6"
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
In the end, the compiled Go package, which will eventually be
|
|
|
|
|
presented to 6l as part of a larger program, contains:
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
_go_.6 # 6g-compiled object for _cgo_gotypes.go, _cgo_import.go, *.cgo1.go
|
2013-02-27 20:55:01 -08:00
|
|
|
_all.o # gcc-compiled object for _cgo_export.c, *.cgo2.c
|
|
|
|
|
|
|
|
|
|
The final program will be a dynamic executable, so that 6l can avoid
|
|
|
|
|
needing to process arbitrary .o files. It only needs to process the .o
|
|
|
|
|
files generated from C files that cgo writes, and those are much more
|
|
|
|
|
limited in the ELF or other features that they use.
|
|
|
|
|
|
|
|
|
|
In essence, the _cgo_import.6 file includes the extra linking
|
|
|
|
|
directives that 6l is not sophisticated enough to derive from _all.o
|
|
|
|
|
on its own. Similarly, the _all.o uses dynamic references to real
|
|
|
|
|
system object code because 6l is not sophisticated enough to process
|
|
|
|
|
the real code.
|
|
|
|
|
|
|
|
|
|
The main benefits of this system are that 6l remains relatively simple
|
|
|
|
|
(it does not need to implement a complete ELF and Mach-O linker) and
|
|
|
|
|
that gcc is not needed after the package is compiled. For example,
|
|
|
|
|
package net uses cgo for access to name resolution functions provided
|
|
|
|
|
by libc. Although gcc is needed to compile package net, gcc is not
|
|
|
|
|
needed to link programs that import package net.
|
|
|
|
|
|
|
|
|
|
Runtime
|
|
|
|
|
|
|
|
|
|
When using cgo, Go must not assume that it owns all details of the
|
|
|
|
|
process. In particular it needs to coordinate with C in the use of
|
2015-01-06 15:08:02 -08:00
|
|
|
threads and thread-local storage. The runtime package declares a few
|
2013-02-27 20:55:01 -08:00
|
|
|
variables:
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
var (
|
|
|
|
|
iscgo bool
|
|
|
|
|
_cgo_init unsafe.Pointer
|
|
|
|
|
_cgo_thread_start unsafe.Pointer
|
|
|
|
|
)
|
2013-02-27 20:55:01 -08:00
|
|
|
|
|
|
|
|
Any package using cgo imports "runtime/cgo", which provides
|
2015-01-06 15:08:02 -08:00
|
|
|
initializations for these variables. It sets iscgo to true, _cgo_init
|
|
|
|
|
to a gcc-compiled function that can be called early during program
|
|
|
|
|
startup, and _cgo_thread_start to a gcc-compiled function that can be
|
|
|
|
|
used to create a new thread, in place of the runtime's usual direct
|
|
|
|
|
system calls.
|
2013-02-27 20:55:01 -08:00
|
|
|
|
2013-02-28 10:44:29 -08:00
|
|
|
Internal and External Linking
|
|
|
|
|
|
|
|
|
|
The text above describes "internal" linking, in which 6l parses and
|
|
|
|
|
links host object files (ELF, Mach-O, PE, and so on) into the final
|
|
|
|
|
executable itself. Keeping 6l simple means we cannot possibly
|
|
|
|
|
implement the full semantics of the host linker, so the kinds of
|
|
|
|
|
objects that can be linked directly into the binary is limited (other
|
|
|
|
|
code can only be used as a dynamic library). On the other hand, when
|
|
|
|
|
using internal linking, 6l can generate Go binaries by itself.
|
|
|
|
|
|
|
|
|
|
In order to allow linking arbitrary object files without requiring
|
2015-01-06 15:08:02 -08:00
|
|
|
dynamic libraries, cgo supports an "external" linking mode too. In
|
|
|
|
|
external linking mode, 6l does not process any host object files.
|
|
|
|
|
Instead, it collects all the Go code and writes a single go.o object
|
|
|
|
|
file containing it. Then it invokes the host linker (usually gcc) to
|
|
|
|
|
combine the go.o object file and any supporting non-Go code into a
|
|
|
|
|
final executable. External linking avoids the dynamic library
|
2013-02-28 10:44:29 -08:00
|
|
|
requirement but introduces a requirement that the host linker be
|
|
|
|
|
present to create such a binary.
|
|
|
|
|
|
|
|
|
|
Most builds both compile source code and invoke the linker to create a
|
|
|
|
|
binary. When cgo is involved, the compile step already requires gcc, so
|
|
|
|
|
it is not problematic for the link step to require gcc too.
|
|
|
|
|
|
|
|
|
|
An important exception is builds using a pre-compiled copy of the
|
|
|
|
|
standard library. In particular, package net uses cgo on most systems,
|
|
|
|
|
and we want to preserve the ability to compile pure Go code that
|
|
|
|
|
imports net without requiring gcc to be present at link time. (In this
|
|
|
|
|
case, the dynamic library requirement is less significant, because the
|
|
|
|
|
only library involved is libc.so, which can usually be assumed
|
|
|
|
|
present.)
|
|
|
|
|
|
|
|
|
|
This conflict between functionality and the gcc requirement means we
|
|
|
|
|
must support both internal and external linking, depending on the
|
|
|
|
|
circumstances: if net is the only cgo-using package, then internal
|
|
|
|
|
linking is probably fine, but if other packages are involved, so that there
|
|
|
|
|
are dependencies on libraries beyond libc, external linking is likely
|
|
|
|
|
to work better. The compilation of a package records the relevant
|
|
|
|
|
information to support both linking modes, leaving the decision
|
|
|
|
|
to be made when linking the final binary.
|
|
|
|
|
|
|
|
|
|
Linking Directives
|
|
|
|
|
|
|
|
|
|
In either linking mode, package-specific directives must be passed
|
2015-01-06 15:08:02 -08:00
|
|
|
through to 6l. These are communicated by writing //go: directives in a
|
|
|
|
|
Go source file compiled by 6g. The directives are copied into the .6
|
|
|
|
|
object file and then processed by the linker.
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
The directives are:
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_dynamic <local> [<remote> ["<library>"]]
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
In internal linking mode, allow an unresolved reference to
|
|
|
|
|
<local>, assuming it will be resolved by a dynamic library
|
|
|
|
|
symbol. The optional <remote> specifies the symbol's name and
|
|
|
|
|
possibly version in the dynamic library, and the optional "<library>"
|
|
|
|
|
names the specific library where the symbol should be found.
|
|
|
|
|
|
|
|
|
|
In the <remote>, # or @ can be used to introduce a symbol version.
|
|
|
|
|
|
|
|
|
|
Examples:
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_dynamic puts
|
|
|
|
|
//go:cgo_import_dynamic puts puts#GLIBC_2.2.5
|
|
|
|
|
//go:cgo_import_dynamic puts puts#GLIBC_2.2.5 "libc.so.6"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2013-05-01 14:31:42 -07:00
|
|
|
A side effect of the cgo_import_dynamic directive with a
|
2013-02-28 10:44:29 -08:00
|
|
|
library is to make the final binary depend on that dynamic
|
|
|
|
|
library. To get the dependency without importing any specific
|
|
|
|
|
symbols, use _ for local and remote.
|
|
|
|
|
|
|
|
|
|
Example:
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_dynamic _ _ "libc.so.6"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
For compatibility with current versions of SWIG,
|
2015-01-06 15:08:02 -08:00
|
|
|
#pragma dynimport is an alias for //go:cgo_import_dynamic.
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_dynamic_linker "<path>"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
In internal linking mode, use "<path>" as the dynamic linker
|
|
|
|
|
in the final binary. This directive is only needed from one
|
|
|
|
|
package when constructing a binary; by convention it is
|
|
|
|
|
supplied by runtime/cgo.
|
|
|
|
|
|
|
|
|
|
Example:
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_dynamic_linker "/lib/ld-linux.so.2"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_export_dynamic <local> <remote>
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2013-03-06 16:57:14 -05:00
|
|
|
In internal linking mode, put the Go symbol
|
2013-02-28 10:44:29 -08:00
|
|
|
named <local> into the program's exported symbol table as
|
|
|
|
|
<remote>, so that C code can refer to it by that name. This
|
|
|
|
|
mechanism makes it possible for C code to call back into Go or
|
|
|
|
|
to share Go's data.
|
|
|
|
|
|
|
|
|
|
For compatibility with current versions of SWIG,
|
2015-01-06 15:08:02 -08:00
|
|
|
#pragma dynexport is an alias for //go:cgo_export_dynamic.
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_static <local>
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
In external linking mode, allow unresolved references to
|
|
|
|
|
<local> in the go.o object file prepared for the host linker,
|
|
|
|
|
under the assumption that <local> will be supplied by the
|
|
|
|
|
other object files that will be linked with go.o.
|
|
|
|
|
|
|
|
|
|
Example:
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_static puts_wrapper
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_export_static <local> <remote>
|
2013-03-06 16:57:14 -05:00
|
|
|
|
|
|
|
|
In external linking mode, put the Go symbol
|
|
|
|
|
named <local> into the program's exported symbol table as
|
|
|
|
|
<remote>, so that C code can refer to it by that name. This
|
|
|
|
|
mechanism makes it possible for C code to call back into Go or
|
|
|
|
|
to share Go's data.
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_ldflag "<arg>"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
In external linking mode, invoke the host linker (usually gcc)
|
|
|
|
|
with "<arg>" as a command-line argument following the .o files.
|
|
|
|
|
Note that the arguments are for "gcc", not "ld".
|
|
|
|
|
|
|
|
|
|
Example:
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_ldflag "-lpthread"
|
|
|
|
|
//go:cgo_ldflag "-L/usr/local/sqlite3/lib"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
A package compiled with cgo will include directives for both
|
|
|
|
|
internal and external linking; the linker will select the appropriate
|
|
|
|
|
subset for the chosen linking mode.
|
|
|
|
|
|
|
|
|
|
Example
|
|
|
|
|
|
|
|
|
|
As a simple example, consider a package that uses cgo to call C.sin.
|
|
|
|
|
The following code will be generated by cgo:
|
|
|
|
|
|
|
|
|
|
// compiled by 6g
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_ldflag "-lm"
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
type _Ctype_double float64
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
//go:cgo_import_static _cgo_gcc_Cfunc_sin
|
|
|
|
|
//go:linkname __cgo_gcc_Cfunc_sin _cgo_gcc_Cfunc_sin
|
|
|
|
|
var __cgo_gcc_Cfunc_sin byte
|
|
|
|
|
var _cgo_gcc_Cfunc_sin = unsafe.Pointer(&__cgo_gcc_Cfunc_sin)
|
2013-02-28 10:44:29 -08:00
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
func _Cfunc_sin(p0 _Ctype_double) (r1 _Ctype_double) {
|
|
|
|
|
_cgo_runtime_cgocall_errno(_cgo_gcc_Cfunc_sin, uintptr(unsafe.Pointer(&p0)))
|
|
|
|
|
return
|
2013-02-28 10:44:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compiled by gcc, into foo.cgo2.o
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_cgo_gcc_Cfunc_sin(void *v)
|
|
|
|
|
{
|
|
|
|
|
struct {
|
|
|
|
|
double p0;
|
|
|
|
|
double r;
|
|
|
|
|
} __attribute__((__packed__)) *a = v;
|
|
|
|
|
a->r = sin(a->p0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
What happens at link time depends on whether the final binary is linked
|
|
|
|
|
using the internal or external mode. If other packages are compiled in
|
|
|
|
|
"external only" mode, then the final link will be an external one.
|
|
|
|
|
Otherwise the link will be an internal one.
|
|
|
|
|
|
2015-01-06 15:08:02 -08:00
|
|
|
The linking directives are used according to the kind of final link
|
|
|
|
|
used.
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
In internal mode, 6l itself processes all the host object files, in
|
2013-05-01 14:31:42 -07:00
|
|
|
particular foo.cgo2.o. To do so, it uses the cgo_import_dynamic and
|
2013-02-28 10:44:29 -08:00
|
|
|
cgo_dynamic_linker directives to learn that the otherwise undefined
|
|
|
|
|
reference to sin in foo.cgo2.o should be rewritten to refer to the
|
|
|
|
|
symbol sin with version GLIBC_2.2.5 from the dynamic library
|
|
|
|
|
"libm.so.6", and the binary should request "/lib/ld-linux.so.2" as its
|
|
|
|
|
runtime dynamic linker.
|
|
|
|
|
|
|
|
|
|
In external mode, 6l does not process any host object files, in
|
2015-01-06 15:08:02 -08:00
|
|
|
particular foo.cgo2.o. It links together the 6g-generated object
|
|
|
|
|
files, along with any other Go code, into a go.o file. While doing
|
|
|
|
|
that, 6l will discover that there is no definition for
|
|
|
|
|
_cgo_gcc_Cfunc_sin, referred to by the 6g-compiled source file. This
|
2013-03-01 00:27:57 -05:00
|
|
|
is okay, because 6l also processes the cgo_import_static directive and
|
2013-02-28 10:44:29 -08:00
|
|
|
knows that _cgo_gcc_Cfunc_sin is expected to be supplied by a host
|
|
|
|
|
object file, so 6l does not treat the missing symbol as an error when
|
|
|
|
|
creating go.o. Indeed, the definition for _cgo_gcc_Cfunc_sin will be
|
|
|
|
|
provided to the host linker by foo2.cgo.o, which in turn will need the
|
|
|
|
|
symbol 'sin'. 6l also processes the cgo_ldflag directives, so that it
|
|
|
|
|
knows that the eventual host link command must include the -lm
|
|
|
|
|
argument, so that the host linker will be able to find 'sin' in the
|
|
|
|
|
math library.
|
|
|
|
|
|
|
|
|
|
6l Command Line Interface
|
|
|
|
|
|
|
|
|
|
The go command and any other Go-aware build systems invoke 6l
|
|
|
|
|
to link a collection of packages into a single binary. By default, 6l will
|
|
|
|
|
present the same interface it does today:
|
|
|
|
|
|
|
|
|
|
6l main.a
|
|
|
|
|
|
|
|
|
|
produces a file named 6.out, even if 6l does so by invoking the host
|
|
|
|
|
linker in external linking mode.
|
|
|
|
|
|
|
|
|
|
By default, 6l will decide the linking mode as follows: if the only
|
|
|
|
|
packages using cgo are those on a whitelist of standard library
|
|
|
|
|
packages (net, os/user, runtime/cgo), 6l will use internal linking
|
|
|
|
|
mode. Otherwise, there are non-standard cgo packages involved, and 6l
|
|
|
|
|
will use external linking mode. The first rule means that a build of
|
|
|
|
|
the godoc binary, which uses net but no other cgo, can run without
|
|
|
|
|
needing gcc available. The second rule means that a build of a
|
|
|
|
|
cgo-wrapped library like sqlite3 can generate a standalone executable
|
|
|
|
|
instead of needing to refer to a dynamic library. The specific choice
|
2013-09-17 07:11:40 -07:00
|
|
|
can be overridden using a command line flag: 6l -linkmode=internal or
|
|
|
|
|
6l -linkmode=external.
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
In an external link, 6l will create a temporary directory, write any
|
|
|
|
|
host object files found in package archives to that directory (renamed
|
|
|
|
|
to avoid conflicts), write the go.o file to that directory, and invoke
|
|
|
|
|
the host linker. The default value for the host linker is $CC, split
|
|
|
|
|
into fields, or else "gcc". The specific host linker command line can
|
2013-09-17 07:11:40 -07:00
|
|
|
be overridden using command line flags: 6l -extld=clang
|
|
|
|
|
-extldflags='-ggdb -O3'. If any package in a build includes a .cc or
|
|
|
|
|
other file compiled by the C++ compiler, the go tool will use the
|
|
|
|
|
-extld option to set the host linker to the C++ compiler.
|
2013-02-28 10:44:29 -08:00
|
|
|
|
|
|
|
|
These defaults mean that Go-aware build systems can ignore the linking
|
|
|
|
|
changes and keep running plain '6l' and get reasonable results, but
|
|
|
|
|
they can also control the linking details if desired.
|
|
|
|
|
|
2013-02-27 20:55:01 -08:00
|
|
|
*/
|