forked from bazel-contrib/rules_go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Combine extldflags lists when linking
_emit_go_link_action now scans gc_linkopts for -extldflags arguments. The Go linker will only use the last list passed to it (earlier lists are silently ignored), so we combine these into one external flags list. Fixes bazel-contrib#305
- Loading branch information
Jay Conrod
committed
Mar 14, 2017
1 parent
f6952a9
commit 100060e
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
load("//go:def.bzl", "go_binary", "cgo_library") | ||
|
||
cgo_library( | ||
name = "cgo_default_library", | ||
srcs = ["main.go"], | ||
) | ||
|
||
# Go binaries with Cgo code are dynamically linked by default. | ||
go_binary( | ||
name = "dynamic_binary", | ||
library = ":cgo_default_library", | ||
) | ||
|
||
go_binary( | ||
name = "static_binary", | ||
gc_linkopts = [ | ||
"-extldflags", | ||
"-static", | ||
], | ||
library = ":cgo_default_library", | ||
) | ||
|
||
sh_test( | ||
name = "extldflags_link_test", | ||
size = "small", | ||
srcs = ["extldflags_link_test.sh"], | ||
args = [ | ||
"$(location :dynamic_binary)", | ||
"'dynamically linked'", | ||
"$(location :static_binary)", | ||
"'statically linked'", | ||
], | ||
data = [ | ||
":dynamic_binary", | ||
":static_binary", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
This test checks that flags passed to the external linker with -extldflags | ||
through gc_linkopts are correctly combined with other flags passed through | ||
-extldflags. | ||
|
||
`go tool link` only uses the last set of flags passed with -extldflags. If more | ||
than one set of flags is passed, the last set is passed to the external linker, | ||
and the rest are silently ignored. | ||
|
||
`go_binary` and `go_test` should look for -extldflags in gc_linkopts and | ||
combine the flags into a single list passed to `go tool link`. This includes | ||
flags generated by the rules themselves. | ||
|
||
This test checks this behavior by passing "-extldflags -static", which has an | ||
observable effect on a `go_binary` with cgo code (which is linked dynamically by | ||
default). This set of flags is combined with the linker flags generated by the | ||
Go rules themselves. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/bin/sh | ||
|
||
result=0 | ||
while [ $# -gt 0 ]; do | ||
path=$1 | ||
pattern=$2 | ||
shift 2 | ||
if ! file --dereference "$path" | grep --quiet "$pattern"; then | ||
echo "file '$path' did not match pattern '$pattern'" >&1 | ||
result=1 | ||
fi | ||
done | ||
|
||
exit $result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
/* | ||
int foo() { | ||
return 42; | ||
} | ||
*/ | ||
import "C" | ||
import "fmt" | ||
|
||
func main() { | ||
fmt.Printf("%d\n", int(C.foo())) | ||
} |