Skip to content

Commit f135106

Browse files
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent ba048f7 commit f135106

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

misc/cgo/testgodefs/test.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ FILE_PREFIXES="anonunion issue8478"
1212
RM=
1313
for FP in $FILE_PREFIXES
1414
do
15-
go tool cgo -godefs ${FP}.go > ${FP}_defs.go
15+
go tool cgo -godefs -srcdir . ${FP}.go > ${FP}_defs.go
1616
RM="${RM} ${FP}_defs.go"
1717
done
1818

src/cmd/cgo/doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ The following options are available when running cgo directly:
326326
Write out input file in Go syntax replacing C package
327327
names with real values. Used to generate files in the
328328
syscall package when bootstrapping a new target.
329+
-srcdir directory
330+
Find the Go input files, listed on the command line,
331+
in directory.
329332
-objdir directory
330333
Put all generated files in directory.
331334
-importpath string

src/cmd/cgo/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information
178178
// constant values used in the host's C libraries and system calls.
179179
var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
180180

181+
var srcDir = flag.String("srcdir", "", "source directory")
181182
var objDir = flag.String("objdir", "", "object directory")
182183
var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
183184
var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
@@ -256,6 +257,9 @@ func main() {
256257
// Use the beginning of the md5 of the input to disambiguate.
257258
h := md5.New()
258259
for _, input := range goFiles {
260+
if *srcDir != "" {
261+
input = filepath.Join(*srcDir, input)
262+
}
259263
f, err := os.Open(input)
260264
if err != nil {
261265
fatalf("%s", err)
@@ -267,6 +271,9 @@ func main() {
267271

268272
fs := make([]*File, len(goFiles))
269273
for i, input := range goFiles {
274+
if *srcDir != "" {
275+
input = filepath.Join(*srcDir, input)
276+
}
270277
f := new(File)
271278
f.ReadGo(input)
272279
f.DiscardCgoDirectives()

0 commit comments

Comments
 (0)