-
-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compilepkg: cgo assembly uses the C compiler (#3648)
* compilepkg: cgo assembly uses the C compiler This changes compilepkg to use the C compiler for .S and .s files to use the C compiler, like go build does. Previously it would use the Go assembler, which is used for pure Go packages. This should help fix issue: #3411 I have added a cgo test for this issue that is intended to work with both arm64 and amd64 machines. I have only tested it on Linux amd64 and Darwin arm64. Without this change it fails to build with the following output. This fails to parse the assembly file because it uses the Go assembler. ERROR: rules_go/tests/core/cgo/asm/BUILD.bazel:3:11: GoCompilePkg tests/core/cgo/asm/asm.a failed: (Exit 1): builder failed: error executing command (from target //tests/core/cgo/asm:asm) bazel-out/k8-opt-exec-2B5CBBC6/bin/external/go_sdk/builder_reset/builder compilepkg -sdk external/go_sdk -installsuffix linux_amd64 -src tests/core/cgo/asm/cgoasm.go -src tests/core/cgo/asm/asm_amd64.S ... tests/core/cgo/asm/asm_amd64.S:4: expected identifier, found "." asm: assembly of tests/core/cgo/asm/asm_amd64.S failed compilepkg: error running subcommand external/go_sdk/pkg/tool/linux_amd64/asm: exit status 1 * add build constraint: unix && (amd64 || arm64) * run buildifier; make asm conditional * assembly fixes for Mac OS X and Linux * Change build constraint to (amd64 || arm64): Should work on Windows The tests were failing on Windows because rules_go incorrectly believed this was a "native" Go package. I think this should work on Windows. --------- Co-authored-by: Fabian Meumertzheim <[email protected]>
- Loading branch information
Showing
6 changed files
with
112 additions
and
16 deletions.
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,20 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "asm", | ||
srcs = [ | ||
"asm_amd64.S", | ||
"asm_arm64.S", | ||
"cgoasm.go", | ||
], | ||
cgo = True, | ||
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/asm", | ||
) | ||
|
||
go_test( | ||
name = "asm_test", | ||
srcs = [ | ||
"cgoasm_test.go", | ||
], | ||
embed = [":asm"], | ||
) |
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,22 @@ | ||
/* | ||
https://stackoverflow.com/questions/73435637/how-can-i-fix-usr-bin-ld-warning-trap-o-missing-note-gnu-stack-section-imp | ||
*/ | ||
#if defined(__ELF__) && defined(__GNUC__) | ||
.section .note.GNU-stack,"",@progbits | ||
#endif | ||
|
||
/* | ||
define this symbol both with and without underscore. | ||
It seems like Mac OS X wants the underscore, but Linux does not. | ||
*/ | ||
.global example_asm_func | ||
.global _example_asm_func | ||
.text | ||
example_asm_func: | ||
_example_asm_func: | ||
mov $42, %rax | ||
ret | ||
|
||
#if NOT_DEFINED | ||
#error "should not fail" | ||
#endif |
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,15 @@ | ||
/* | ||
define this symbol both with and without underscore. | ||
It seems like Mac OS X wants the underscore, but Linux does not. | ||
*/ | ||
.global example_asm_func | ||
.global _example_asm_func | ||
.p2align 2 /* ld: warning: arm64 function not 4-byte aligned */ | ||
example_asm_func: | ||
_example_asm_func: | ||
mov x0, #44 | ||
ret | ||
|
||
#if NOT_DEFINED | ||
#error "should not fail" | ||
#endif |
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,12 @@ | ||
//go:build amd64 || arm64 | ||
|
||
package asm | ||
|
||
/* | ||
extern int example_asm_func(); | ||
*/ | ||
import "C" | ||
|
||
func callAssembly() int { | ||
return int(C.example_asm_func()) | ||
} |
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,23 @@ | ||
//go:build amd64 || arm64 | ||
|
||
package asm | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestNativeAssembly(t *testing.T) { | ||
expectedGOARCH := map[string]int{ | ||
"amd64": 42, | ||
"arm64": 44, | ||
} | ||
expected := expectedGOARCH[runtime.GOARCH] | ||
if expected == 0 { | ||
t.Fatalf("expected=0 for GOARCH=%s; missing value?", runtime.GOARCH) | ||
} | ||
actual := callAssembly() | ||
if actual != expected { | ||
t.Errorf("callAssembly()=%d; expected=%d", actual, expected) | ||
} | ||
} |