cmd/cgo: add -srcdir option

This is convenient for direct use of `go tool cgo`. We can also use it
from the go tool to reduce the length of the file names that cgo
generates.

Update #17070.

Change-Id: I8466a0a2cc68a732d17d07319e303497715bac8c
Reviewed-on: https://go-review.googlesource.com/32354
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Ian Lance Taylor 2016-10-30 12:30:47 -04:00
parent ba048f7ce4
commit f135106ec7
3 changed files with 11 additions and 1 deletions

View file

@ -326,6 +326,9 @@ The following options are available when running cgo directly:
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.
-srcdir directory
Find the Go input files, listed on the command line,
in directory.
-objdir directory
Put all generated files in directory.
-importpath string

View file

@ -178,6 +178,7 @@ var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information
// 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 srcDir = flag.String("srcdir", "", "source directory")
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")
@ -256,6 +257,9 @@ func main() {
// Use the beginning of the md5 of the input to disambiguate.
h := md5.New()
for _, input := range goFiles {
if *srcDir != "" {
input = filepath.Join(*srcDir, input)
}
f, err := os.Open(input)
if err != nil {
fatalf("%s", err)
@ -267,6 +271,9 @@ func main() {
fs := make([]*File, len(goFiles))
for i, input := range goFiles {
if *srcDir != "" {
input = filepath.Join(*srcDir, input)
}
f := new(File)
f.ReadGo(input)
f.DiscardCgoDirectives()