From 1aab7b9626b8bc735d7340c1eb47d8d46560290f Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 25 Jan 2016 11:44:02 -0800 Subject: [PATCH] cmd/link: add -extar option to set ar program for c-archive People who want to use -buildmode=c-archive in unusual cross-compilation setups will need something like this. It could also be done via (yet another) environment variable but I use -extar by analogy with the existing -extld. Change-Id: I354cfabc4c470603affd13cd946997b3a24c0e6c Reviewed-on: https://go-review.googlesource.com/18913 Reviewed-by: Russ Cox Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- misc/cgo/testcarchive/test.bash | 18 ++++++++++++++++++ src/cmd/link/doc.go | 3 +++ src/cmd/link/internal/ld/lib.go | 7 ++++++- src/cmd/link/internal/ld/pobj.go | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/misc/cgo/testcarchive/test.bash b/misc/cgo/testcarchive/test.bash index f4b4a3079fe..f4e7c458ec6 100755 --- a/misc/cgo/testcarchive/test.bash +++ b/misc/cgo/testcarchive/test.bash @@ -85,4 +85,22 @@ if ! $bin; then fi rm -rf libgo4.a libgo4.h testp pkg +rm -f testar +cat >testar </dev/null; do + shift +done +echo "testar" > \$1 +echo "testar" > $(pwd)/testar.ran +EOF +chmod +x testar +rm -f testar.ran +GOPATH=$(pwd) go build -buildmode=c-archive -ldflags=-extar=$(pwd)/testar -o libgo4.a libgo4 +if ! test -f testar.ran; then + echo "FAIL test5" + status=1 +fi +rm -rf libgo4.a libgo4.h testar testar.ran pkg + exit $status diff --git a/src/cmd/link/doc.go b/src/cmd/link/doc.go index 69f9b578590..ffaead7ba0d 100644 --- a/src/cmd/link/doc.go +++ b/src/cmd/link/doc.go @@ -52,6 +52,9 @@ Flags: The dynamic header is on by default, even without any references to dynamic libraries, because many common system tools now assume the presence of the header. + -extar ar + Set the external archive program (default "ar"). + Used only for -buildmode=c-archive. -extld linker Set the external linker (default "clang" or "gcc"). -extldflags flags diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index a23a437e3d0..bdfa0563c3d 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -207,6 +207,7 @@ var ( tmpdir string extld string extldflags string + extar string libgccfile string debug_s int // backup old value of debug['s'] Ctxt *Link @@ -1015,8 +1016,12 @@ func archive() { return } + if extar == "" { + extar = "ar" + } + mayberemoveoutfile() - argv := []string{"ar", "-q", "-c", "-s", outfile} + argv := []string{extar, "-q", "-c", "-s", outfile} argv = append(argv, fmt.Sprintf("%s/go.o", tmpdir)) argv = append(argv, hostobjCopy()...) diff --git a/src/cmd/link/internal/ld/pobj.go b/src/cmd/link/internal/ld/pobj.go index 319e8504670..808d377f8a0 100644 --- a/src/cmd/link/internal/ld/pobj.go +++ b/src/cmd/link/internal/ld/pobj.go @@ -89,6 +89,7 @@ func Ldmain() { flag.Var(&Buildmode, "buildmode", "set build `mode`") obj.Flagcount("c", "dump call graph", &Debug['c']) obj.Flagcount("d", "disable dynamic executable", &Debug['d']) + obj.Flagstr("extar", "archive program for buildmode=c-archive", &extar) obj.Flagstr("extld", "use `linker` when linking in external mode", &extld) obj.Flagstr("extldflags", "pass `flags` to external linker", &extldflags) obj.Flagcount("f", "ignore version mismatch", &Debug['f'])