-
-
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.
cgo packages with assembly: Support CGO_ENABLED=0 (#3661)
* cgo packages with assembly: Support CGO_ENABLED=0 Go supports building cgo packages both with and without Cgo. It uses build constraints to exclude the appropriate files. When building a Cgo package with assembly files, we must exclude them. Previous to this change, rules_go would run the Go assembler on them. This meant that packages which had "conditional" imports of cgo libraries with assembly would fail when compiled with cgo disabled. Add tests for these two cases: * asm_conditional_cgo: A Go package that compiles both with and without Cgo. * asm_dep_conditional_cgo: A Go package that conditionally imports a package with Cgo. Fixes: #3411 * code review improvements: remove unused main; clarify comment
- Loading branch information
Showing
15 changed files
with
224 additions
and
8 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
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
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,33 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "asm_conditional_cgo", | ||
srcs = [ | ||
"asm_amd64.S", | ||
"asm_arm64.S", | ||
"asm_cgo.go", | ||
"asm_nocgo.go", | ||
], | ||
cgo = True, | ||
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/asm_conditional_cgo", | ||
deps = ["//tests/core/cgo/asm"], | ||
) | ||
|
||
# this is a "native" target: it uses cgo and calls the assembly function as expected | ||
go_test( | ||
name = "asm_conditional_cgo_test", | ||
srcs = [ | ||
"asm_conditional_cgo_test.go", | ||
], | ||
embed = [":asm_conditional_cgo"], | ||
) | ||
|
||
# this is a CGO_ENABLED=0 target: it does not import the cgo target | ||
go_test( | ||
name = "asm_conditional_nocgo_test", | ||
srcs = [ | ||
"asm_conditional_cgo_test.go", | ||
], | ||
embed = [":asm_conditional_cgo"], | ||
pure = "on", | ||
) |
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,16 @@ | ||
//go:build amd64 || arm64 | ||
|
||
// build constraints must match the constraints in the tests/core/cgo/asm package | ||
|
||
package main | ||
|
||
/* | ||
extern int example_asm_func(); | ||
*/ | ||
import "C" | ||
|
||
func callASM() (int, error) { | ||
return int(C.example_asm_func()), nil | ||
} | ||
|
||
const builtWithCgo = true |
22 changes: 22 additions & 0 deletions
22
tests/core/cgo/asm_conditional_cgo/asm_conditional_cgo_test.go
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 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestConditionalCgo(t *testing.T) { | ||
// Uses build constraints to depend on assembly code when cgo is available, or a | ||
// pure go version if it is not. This can be run both with and without cgo. E.g.: | ||
// CGO_ENABLED=0 go test . && CGO_ENABLED=1 go test . | ||
result, err := callASM() | ||
if builtWithCgo { | ||
if err != nil { | ||
t.Errorf("builtWithCgo=%t; err must be nil, was %s", builtWithCgo, err.Error()) | ||
} else if result <= 0 { | ||
t.Errorf("builtWithCgo=%t; result must be > 0 was %d", builtWithCgo, result) | ||
} | ||
} else { | ||
// does not support calling the cgo library | ||
if !(result == 0 && err != nil) { | ||
t.Errorf("builtWithCgo=%t; expected result=0, err != nil; result=%d, err=%#v", builtWithCgo, result, err) | ||
} | ||
} | ||
} |
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,11 @@ | ||
//go:build !(cgo && (amd64 || arm64)) | ||
|
||
package main | ||
|
||
import "errors" | ||
|
||
func callASM() (int, error) { | ||
return 0, errors.New("cgo disabled and/or unsupported platform") | ||
} | ||
|
||
const builtWithCgo = false |
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,30 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "asm_dep_conditional_cgo", | ||
srcs = [ | ||
"asm_dep_cgo.go", | ||
"asm_dep_nocgo.go", | ||
], | ||
importpath = "github.com/bazelbuild/rules_go/tests/core/cgo/asm_dep_conditional_cgo", | ||
deps = ["//tests/core/cgo/asm"], | ||
) | ||
|
||
# this is a "native" target: it uses cgo and calls the assembly function as expected | ||
go_test( | ||
name = "asm_dep_conditional_cgo_test", | ||
srcs = [ | ||
"asm_dep_conditional_cgo_test.go", | ||
], | ||
embed = [":asm_dep_conditional_cgo"], | ||
) | ||
|
||
# this is a CGO_ENABLED=0 target: it does not import the cgo target | ||
go_test( | ||
name = "asm_dep_conditional_nocgo_test", | ||
srcs = [ | ||
"asm_dep_conditional_cgo_test.go", | ||
], | ||
embed = [":asm_dep_conditional_cgo"], | ||
pure = "on", | ||
) |
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 @@ | ||
//go:build cgo && (amd64 || arm64) | ||
|
||
// build constraints must match the constraints in the tests/core/cgo/asm package | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/bazelbuild/rules_go/tests/core/cgo/asm" | ||
) | ||
|
||
func callASMPackage() (int, error) { | ||
return asm.CallAssembly(), nil | ||
} | ||
|
||
const builtWithCgo = true |
22 changes: 22 additions & 0 deletions
22
tests/core/cgo/asm_dep_conditional_cgo/asm_dep_conditional_cgo_test.go
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 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestConditionalCgo(t *testing.T) { | ||
// Uses build constraints to depend on a native Cgo package when cgo is available, or a | ||
// pure go version if it is not. This can be run both with and without cgo. E.g.: | ||
// CGO_ENABLED=0 go test . && CGO_ENABLED=1 go test . | ||
result, err := callASMPackage() | ||
if builtWithCgo { | ||
if err != nil { | ||
t.Errorf("builtWithCgo=%t; err must be nil, was %s", builtWithCgo, err.Error()) | ||
} else if result <= 0 { | ||
t.Errorf("builtWithCgo=%t; result must be > 0 was %d", builtWithCgo, result) | ||
} | ||
} else { | ||
// does not support calling the cgo library | ||
if !(result == 0 && err != nil) { | ||
t.Errorf("builtWithCgo=%t; expected result=0, err != nil; result=%d, err=%#v", builtWithCgo, result, err) | ||
} | ||
} | ||
} |
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 @@ | ||
//go:build !(cgo && (amd64 || arm64)) | ||
|
||
// build constraints must match the constraints in the tests/core/cgo/asm package | ||
|
||
package main | ||
|
||
import "errors" | ||
|
||
func callASMPackage() (int, error) { | ||
return 0, errors.New("cgo disabled and/or unsupported platform") | ||
} | ||
|
||
const builtWithCgo = false |