From a3606b74042c069c4eb6a9b2fb18048963000f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Sat, 19 Jun 2021 16:32:07 +0300 Subject: [PATCH] register only gnu toolchain by default musl can be specified with --extra_toolchains. --- .bazelversion | 1 + .build.yml | 30 +++++++++++++++--------------- .gitignore | 3 ++- README.md | 8 ++++---- test/BUILD | 22 ++++++++-------------- toolchain/defs.bzl | 22 +++++----------------- 6 files changed, 35 insertions(+), 51 deletions(-) create mode 100644 .bazelversion diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..ee74734 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +4.1.0 diff --git a/.build.yml b/.build.yml index 251e33c..b3cbd59 100644 --- a/.build.yml +++ b/.build.yml @@ -12,36 +12,36 @@ tasks: curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.9.0/bazelisk-linux-amd64 \ -o bazel chmod +x bazel - #- test_default: | - # cd bazel-zig-cc; ../bazel run \ - # //test:gognu + - test_default: | + cd bazel-zig-cc; ../bazel run \ + //test:hello - test_x86_64-linux-gnu: | cd bazel-zig-cc; ../bazel run \ - --platforms @zig_sdk//:x86_64-linux-gnu //test:gognu - file $(./getpath @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:hello) | \ tee /dev/stderr | grep -q "ELF 64-bit.* x86-64.* dynamically linked" - test_x86_64-linux-musl: | cd bazel-zig-cc; ../bazel run \ - --platforms @zig_sdk//:x86_64-linux-musl //test:gomusl - file $(./getpath @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:hello) | \ tee /dev/stderr | grep -q "ELF 64-bit.* x86-64.* statically linked" - test_aarch64-linux-gnu: | cd bazel-zig-cc; ../bazel build \ - --platforms @zig_sdk//:aarch64-linux-gnu //test:gognu - file $(./getpath @zig_sdk//:aarch64-linux-gnu //test:gognu) | \ + --platforms @zig_sdk//:aarch64-linux-gnu //test:hello + file $(./getpath @zig_sdk//:aarch64-linux-gnu //test:hello) | \ tee /dev/stderr | grep -q "ELF 64-bit.* ARM aarch64.* dynamically linked" - test_aarch64-linux-musl: | cd bazel-zig-cc; ../bazel build \ - --platforms @zig_sdk//:aarch64-linux-musl //test:gomusl - file $(./getpath @zig_sdk//:aarch64-linux-musl //test:gomusl) | \ + --platforms @zig_sdk//:aarch64-linux-musl //test:hello + file $(./getpath @zig_sdk//:aarch64-linux-musl //test:hello) | \ tee /dev/stderr | grep -q "ELF 64-bit.* ARM aarch64.* statically linked" - test_x86_64-macos-gnu: | cd bazel-zig-cc; ../bazel build \ - --platforms @zig_sdk//:x86_64-macos-gnu //test:gognu - file $(./getpath @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:hello) | \ tee /dev/stderr | grep -q "Mach-O 64-bit x86_64 executable" - test_aarch64-macos-gnu: | cd bazel-zig-cc; ../bazel build \ - --platforms @zig_sdk//:aarch64-macos-gnu //test:gognu - file $(./getpath @zig_sdk//:aarch64-macos-gnu //test:gognu) | \ + --platforms @zig_sdk//:aarch64-macos-gnu //test:hello + file $(./getpath @zig_sdk//:aarch64-macos-gnu //test:hello) | \ tee /dev/stderr | grep -q "Mach-O 64-bit arm64 executable" diff --git a/.gitignore b/.gitignore index 9f934f3..bc03aa4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,8 @@ *.sw[op] /bazel -/bazel-bin /bazel-bazel-zig-cc +/bazel-bin /bazel-out /bazel-testlogs +/bazel-x diff --git a/README.md b/README.md index 4e11f77..b15656f 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,11 @@ bazel-bin/test/gognu_/gognu: ELF 64-bit LSB executable, x86-64, version 1 (SYSV) ## 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 -../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/gomusl_/gomusl +$ file ../bazel-out/k8-fastbuild-ST-d17813c235ce/bin/test/hello_/hello +../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/hello_/hello hello, world ``` diff --git a/test/BUILD b/test/BUILD index 6b2afce..3d045a8 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,7 +1,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_library( - name = "go_lib", + name = "hello_lib", srcs = ["hello.go"], cgo = True, importpath = "github.com/motiejus/bazel-zig-cc/test", @@ -9,22 +9,16 @@ go_library( ) go_binary( - name = "gognu", - embed = [":go_lib"], + name = "hello", + embed = [":hello_lib"], gc_linkopts = select({ - "@platforms//os:macos": ["-s", "-w", "-buildmode=pie"], + "@platforms//os:macos": [ + "-s", + "-w", + "-buildmode=pie", + ], "//conditions:default": [], }), - visibility = ["//visibility:public"], -) - -go_binary( - name = "gomusl", - embed = [":go_lib"], static = "on", - target_compatible_with = select({ - "@platforms//os:linux": ["@zig_sdk//:libc_musl"], - "//conditions:default": [], - }), visibility = ["//visibility:public"], ) diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index 83b1adb..94de682 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -53,7 +53,6 @@ TARGET_CONFIGS_LISTOFLISTS = [[ constraint_values = [ "@platforms//os:linux", "@platforms//cpu:{}".format(cpu), - ":libc_gnu", ], tool_paths = {"ld": "ld.lld"}, ), @@ -71,9 +70,9 @@ TARGET_CONFIGS_LISTOFLISTS = [[ constraint_values = [ "@platforms//os:linux", "@platforms//cpu:{}".format(cpu), - ":libc_musl", ], tool_paths = {"ld": "ld.lld"}, + skip_register = True, ), ] for cpu in ("x86_64", "aarch64")] @@ -101,9 +100,10 @@ def toolchain_repositories(): def register_all_toolchains(): for target_config in TARGET_CONFIGS: - native.register_toolchains( - "@zig_sdk//:%s_toolchain" % target_config.target, - ) + if not getattr(target_config, "skip_register", False): + native.register_toolchains( + "@zig_sdk//:%s_toolchain" % target_config.target, + ) ZIG_TOOL_PATH = "tools/{zig_tool}" ZIG_TOOL_WRAPPER = """#!/bin/bash @@ -188,18 +188,6 @@ def zig_build_macro(absolute_path, zig_include_root): filegroup(name = "zig_compiler", srcs = ["zig"]) 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 = {} for target_config in TARGET_CONFIGS: