1
Fork 0

register only gnu toolchain by default

musl can be specified with --extra_toolchains.
This commit is contained in:
Motiejus Jakštys 2021-06-19 16:32:07 +03:00 committed by Motiejus Jakštys
parent e94447bd83
commit a3606b7404
6 changed files with 35 additions and 51 deletions

1
.bazelversion Normal file
View File

@ -0,0 +1 @@
4.1.0

View File

@ -12,36 +12,36 @@ tasks:
curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.9.0/bazelisk-linux-amd64 \ curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.9.0/bazelisk-linux-amd64 \
-o bazel -o bazel
chmod +x bazel chmod +x bazel
#- test_default: | - test_default: |
# cd bazel-zig-cc; ../bazel run \ cd bazel-zig-cc; ../bazel run \
# //test:gognu //test:hello
- test_x86_64-linux-gnu: | - test_x86_64-linux-gnu: |
cd bazel-zig-cc; ../bazel run \ cd bazel-zig-cc; ../bazel run \
--platforms @zig_sdk//:x86_64-linux-gnu //test:gognu --platforms @zig_sdk//:x86_64-linux-gnu //test:hello
file $(./getpath @zig_sdk//:x86_64-linux-gnu //test:gognu) | \ file $(./getpath @zig_sdk//:x86_64-linux-gnu //test:hello) | \
tee /dev/stderr | grep -q "ELF 64-bit.* x86-64.* dynamically linked" tee /dev/stderr | grep -q "ELF 64-bit.* x86-64.* dynamically linked"
- test_x86_64-linux-musl: | - test_x86_64-linux-musl: |
cd bazel-zig-cc; ../bazel run \ cd bazel-zig-cc; ../bazel run \
--platforms @zig_sdk//:x86_64-linux-musl //test:gomusl --platforms @zig_sdk//:x86_64-linux-musl //test:hello
file $(./getpath @zig_sdk//:x86_64-linux-musl //test:gomusl) | \ file $(./getpath @zig_sdk//:x86_64-linux-musl //test:hello) | \
tee /dev/stderr | grep -q "ELF 64-bit.* x86-64.* statically linked" tee /dev/stderr | grep -q "ELF 64-bit.* x86-64.* statically linked"
- test_aarch64-linux-gnu: | - test_aarch64-linux-gnu: |
cd bazel-zig-cc; ../bazel build \ cd bazel-zig-cc; ../bazel build \
--platforms @zig_sdk//:aarch64-linux-gnu //test:gognu --platforms @zig_sdk//:aarch64-linux-gnu //test:hello
file $(./getpath @zig_sdk//:aarch64-linux-gnu //test:gognu) | \ file $(./getpath @zig_sdk//:aarch64-linux-gnu //test:hello) | \
tee /dev/stderr | grep -q "ELF 64-bit.* ARM aarch64.* dynamically linked" tee /dev/stderr | grep -q "ELF 64-bit.* ARM aarch64.* dynamically linked"
- test_aarch64-linux-musl: | - test_aarch64-linux-musl: |
cd bazel-zig-cc; ../bazel build \ cd bazel-zig-cc; ../bazel build \
--platforms @zig_sdk//:aarch64-linux-musl //test:gomusl --platforms @zig_sdk//:aarch64-linux-musl //test:hello
file $(./getpath @zig_sdk//:aarch64-linux-musl //test:gomusl) | \ file $(./getpath @zig_sdk//:aarch64-linux-musl //test:hello) | \
tee /dev/stderr | grep -q "ELF 64-bit.* ARM aarch64.* statically linked" tee /dev/stderr | grep -q "ELF 64-bit.* ARM aarch64.* statically linked"
- test_x86_64-macos-gnu: | - test_x86_64-macos-gnu: |
cd bazel-zig-cc; ../bazel build \ cd bazel-zig-cc; ../bazel build \
--platforms @zig_sdk//:x86_64-macos-gnu //test:gognu --platforms @zig_sdk//:x86_64-macos-gnu //test:hello
file $(./getpath @zig_sdk//:x86_64-macos-gnu //test:gognu) | \ file $(./getpath @zig_sdk//:x86_64-macos-gnu //test:hello) | \
tee /dev/stderr | grep -q "Mach-O 64-bit x86_64 executable" tee /dev/stderr | grep -q "Mach-O 64-bit x86_64 executable"
- test_aarch64-macos-gnu: | - test_aarch64-macos-gnu: |
cd bazel-zig-cc; ../bazel build \ cd bazel-zig-cc; ../bazel build \
--platforms @zig_sdk//:aarch64-macos-gnu //test:gognu --platforms @zig_sdk//:aarch64-macos-gnu //test:hello
file $(./getpath @zig_sdk//:aarch64-macos-gnu //test:gognu) | \ file $(./getpath @zig_sdk//:aarch64-macos-gnu //test:hello) | \
tee /dev/stderr | grep -q "Mach-O 64-bit arm64 executable" tee /dev/stderr | grep -q "Mach-O 64-bit arm64 executable"

3
.gitignore vendored
View File

@ -2,7 +2,8 @@
*.sw[op] *.sw[op]
/bazel /bazel
/bazel-bin
/bazel-bazel-zig-cc /bazel-bazel-zig-cc
/bazel-bin
/bazel-out /bazel-out
/bazel-testlogs /bazel-testlogs
/bazel-x

View File

@ -28,11 +28,11 @@ bazel-bin/test/gognu_/gognu: ELF 64-bit LSB executable, x86-64, version 1 (SYSV)
## linux cgo + musl ## linux cgo + musl
``` ```
$ bazel build --platforms @zig_sdk//:x86_64-linux-musl //test:gomusl $ bazel build --extra_toolchains @zig_sdk//:x86_64-linux-musl_toolchain //test:hello
... ...
$ file ../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/gomusl_/gomusl $ file ../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/hello_/hello
../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/gomusl_/gomusl: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=redacted, with debug_info, not stripped ../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/hello_/hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=redacted, with debug_info, not stripped
$ ../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/gomusl_/gomusl $ ../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/hello_/hello
hello, world hello, world
``` ```

View File

@ -1,7 +1,7 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
go_library( go_library(
name = "go_lib", name = "hello_lib",
srcs = ["hello.go"], srcs = ["hello.go"],
cgo = True, cgo = True,
importpath = "github.com/motiejus/bazel-zig-cc/test", importpath = "github.com/motiejus/bazel-zig-cc/test",
@ -9,22 +9,16 @@ go_library(
) )
go_binary( go_binary(
name = "gognu", name = "hello",
embed = [":go_lib"], embed = [":hello_lib"],
gc_linkopts = select({ gc_linkopts = select({
"@platforms//os:macos": ["-s", "-w", "-buildmode=pie"], "@platforms//os:macos": [
"-s",
"-w",
"-buildmode=pie",
],
"//conditions:default": [], "//conditions:default": [],
}), }),
visibility = ["//visibility:public"],
)
go_binary(
name = "gomusl",
embed = [":go_lib"],
static = "on", static = "on",
target_compatible_with = select({
"@platforms//os:linux": ["@zig_sdk//:libc_musl"],
"//conditions:default": [],
}),
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )

View File

@ -53,7 +53,6 @@ TARGET_CONFIGS_LISTOFLISTS = [[
constraint_values = [ constraint_values = [
"@platforms//os:linux", "@platforms//os:linux",
"@platforms//cpu:{}".format(cpu), "@platforms//cpu:{}".format(cpu),
":libc_gnu",
], ],
tool_paths = {"ld": "ld.lld"}, tool_paths = {"ld": "ld.lld"},
), ),
@ -71,9 +70,9 @@ TARGET_CONFIGS_LISTOFLISTS = [[
constraint_values = [ constraint_values = [
"@platforms//os:linux", "@platforms//os:linux",
"@platforms//cpu:{}".format(cpu), "@platforms//cpu:{}".format(cpu),
":libc_musl",
], ],
tool_paths = {"ld": "ld.lld"}, tool_paths = {"ld": "ld.lld"},
skip_register = True,
), ),
] for cpu in ("x86_64", "aarch64")] ] for cpu in ("x86_64", "aarch64")]
@ -101,6 +100,7 @@ def toolchain_repositories():
def register_all_toolchains(): def register_all_toolchains():
for target_config in TARGET_CONFIGS: for target_config in TARGET_CONFIGS:
if not getattr(target_config, "skip_register", False):
native.register_toolchains( native.register_toolchains(
"@zig_sdk//:%s_toolchain" % target_config.target, "@zig_sdk//:%s_toolchain" % target_config.target,
) )
@ -188,18 +188,6 @@ def zig_build_macro(absolute_path, zig_include_root):
filegroup(name = "zig_compiler", srcs = ["zig"]) filegroup(name = "zig_compiler", srcs = ["zig"])
filegroup(name = "lib/std", srcs = native.glob(["lib/std/**"])) filegroup(name = "lib/std", srcs = native.glob(["lib/std/**"]))
native.constraint_setting(name = "libc")
native.constraint_value(
name = "libc_musl",
constraint_setting = ":libc",
)
native.constraint_value(
name = "libc_gnu",
constraint_setting = ":libc",
)
lazy_filegroups = {} lazy_filegroups = {}
for target_config in TARGET_CONFIGS: for target_config in TARGET_CONFIGS: