Commit 5e94c65b authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/cgo: add -exportheader option

The -exportheader option tells cgo to generate a header file declaring
expoted functions.  The header file is only created if there are, in
fact, some exported functions, so it also serves as a signal as to
whether there were any.

In future CLs the go tool will use this option to install header files
for packages that use cgo and export functions.

Change-Id: I5b04357d453a9a8f0e70d37f8f18274cf40d74c9
Reviewed-on: https://go-review.googlesource.com/9796Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent 965d00f8
......@@ -242,6 +242,10 @@ The following options are available when running cgo directly:
-importpath string
The import path for the Go package. Optional; used for
nicer comments in the generated files.
-exportheader file
If there are any exported functions, write the
generated export declarations to file.
C code can #include this to see the declarations.
-gccgo
Generate output for the gccgo compiler rather than the
gc compiler.
......
......@@ -169,6 +169,7 @@ var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for
var objDir = flag.String("objdir", "", "object directory")
var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
......
......@@ -166,10 +166,33 @@ func (p *Package) writeDefs() {
}
}
fgcc := creat(*objDir + "_cgo_export.c")
fgcch := creat(*objDir + "_cgo_export.h")
if *gccgo {
p.writeGccgoExports(fgo2, fm)
p.writeGccgoExports(fgo2, fm, fgcc, fgcch)
} else {
p.writeExports(fgo2, fm)
p.writeExports(fgo2, fm, fgcc, fgcch)
}
if err := fgcc.Close(); err != nil {
fatalf("%s", err)
}
if err := fgcch.Close(); err != nil {
fatalf("%s", err)
}
if *exportHeader != "" && len(p.ExpFunc) > 0 {
fexp := creat(*exportHeader)
fgcch, err := os.Open(*objDir + "_cgo_export.h")
if err != nil {
fatalf("%s", err)
}
_, err = io.Copy(fexp, fgcch)
if err != nil {
fatalf("%s", err)
}
if err = fexp.Close(); err != nil {
fatalf("%s", err)
}
}
init := gccgoInit.String()
......@@ -634,10 +657,7 @@ func (p *Package) packedAttribute() string {
// Write out the various stubs we need to support functions exported
// from Go so that they are callable from C.
func (p *Package) writeExports(fgo2, fm io.Writer) {
fgcc := creat(*objDir + "_cgo_export.c")
fgcch := creat(*objDir + "_cgo_export.h")
func (p *Package) writeExports(fgo2, fm, fgcc, fgcch io.Writer) {
p.writeExportHeader(fgcch)
fmt.Fprintf(fgcc, "/* Created by cgo - DO NOT EDIT. */\n")
......@@ -829,10 +849,7 @@ func (p *Package) writeExports(fgo2, fm io.Writer) {
}
// Write out the C header allowing C code to call exported gccgo functions.
func (p *Package) writeGccgoExports(fgo2, fm io.Writer) {
fgcc := creat(*objDir + "_cgo_export.c")
fgcch := creat(*objDir + "_cgo_export.h")
func (p *Package) writeGccgoExports(fgo2, fm, fgcc, fgcch io.Writer) {
gccgoSymbolPrefix := p.gccgoSymbolPrefix()
p.writeExportHeader(fgcch)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment