type ExpFunc struct {
Func *ast.FuncDecl
ExpName string // name to use from C
+ Doc string
}
// A TypeRepr contains the string representation of a type.
var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
-// These flags are for bootstrapping a new Go implementation,
+// This flag is for bootstrapping a new Go implementation,
// to generate Go types that match the data layout and
// constant values used in the host's C libraries and system calls.
var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
+
var objDir = flag.String("objdir", "", "object directory")
+var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
fgcc := creat(*objDir + "_cgo_export.c")
fgcch := creat(*objDir + "_cgo_export.h")
- fmt.Fprintf(fgcch, "/* Created by cgo - DO NOT EDIT. */\n/* This file is arch-specific. */\n")
- fmt.Fprintf(fgcch, "%s\n", p.Preamble)
- fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
+ p.writeExportHeader(fgcch)
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n\n")
s += fmt.Sprintf("%s p%d", p.cgoType(atype).C, i)
})
s += ")"
+
+ if len(exp.Doc) > 0 {
+ fmt.Fprintf(fgcch, "\n%s", exp.Doc)
+ }
fmt.Fprintf(fgcch, "\nextern %s;\n", s)
fmt.Fprintf(fgcc, "extern void _cgoexp%s_%s(void *, int);\n", cPrefix, exp.ExpName)
gccgoSymbolPrefix := p.gccgoSymbolPrefix()
- fmt.Fprintf(fgcch, "/* Created by cgo - DO NOT EDIT. */\n")
- fmt.Fprintf(fgcch, "%s\n", p.Preamble)
- fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
+ p.writeExportHeader(fgcch)
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
fmt.Fprintf(fgcc, "#include \"_cgo_export.h\"\n")
})
default:
// Declare a result struct.
+ fmt.Fprintf(fgcch, "\n/* Return type for %s */\n", exp.ExpName)
fmt.Fprintf(fgcch, "struct %s_result {\n", exp.ExpName)
forFieldList(fntype.Results,
func(i int, atype ast.Expr) {
fmt.Fprintf(cdeclBuf, ")")
cParams := cdeclBuf.String()
+ if len(exp.Doc) > 0 {
+ fmt.Fprintf(fgcch, "\n%s", exp.Doc)
+ }
+
// We need to use a name that will be exported by the
// Go code; otherwise gccgo will make it static and we
// will not be able to link against it from the C
}
}
+// writeExportHeader writes out the start of the _cgo_export.h file.
+func (p *Package) writeExportHeader(fgcch io.Writer) {
+ fmt.Fprintf(fgcch, "/* Created by \"go tool cgo\" - DO NOT EDIT. */\n\n")
+ pkg := *importPath
+ if pkg == "" {
+ pkg = p.PackagePath
+ }
+ fmt.Fprintf(fgcch, "/* package %s */\n\n", pkg)
+
+ fmt.Fprintf(fgcch, "/* Start of preamble from import \"C\" comments. */\n\n")
+ fmt.Fprintf(fgcch, "%s\n", p.Preamble)
+ fmt.Fprintf(fgcch, "\n/* End of preamble from import \"C\" comments. */\n\n")
+
+ fmt.Fprintf(fgcch, "%s\n", p.gccExportHeaderProlog())
+}
+
// Return the package prefix when using gccgo.
func (p *Package) gccgoSymbolPrefix() string {
if !*gccgo {
}
const gccExportHeaderProlog = `
+/* Start of boilerplate cgo prologue. */
+
typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
+
+/* End of boilerplate cgo prologue. */
`
// gccgoExportFileProlog is written to the _cgo_export.c file when