From b826ad94393c878db7a982b1de8e4bdb9972eed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 7 Jun 2021 23:17:19 +0300 Subject: [PATCH] downgrade glibc to 2.19 and update README --- README.md | 55 ++++++++++++++++++----------------------- test/BUILD | 31 +++++++++++++++-------- test/c/BUILD | 13 ---------- test/c/exception.cpp | 12 --------- test/c/hello.cpp | 5 ---- test/go/BUILD | 21 ---------------- test/{go => }/hello.go | 0 zig-toolchains/defs.bzl | 8 +++--- 8 files changed, 49 insertions(+), 96 deletions(-) delete mode 100644 test/c/BUILD delete mode 100644 test/c/exception.cpp delete mode 100644 test/c/hello.cpp delete mode 100644 test/go/BUILD rename test/{go => }/hello.go (100%) diff --git a/README.md b/README.md index 953f125..3e345bb 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,32 @@ -To run the example locally, first make sure `bazelisk` is on your PATH. Then +# Bazel zig cc toolchain for Go -Test that things work at all: +This is a prototype zig-cc toolchain for cgo programs with: + +- glibc 2.19 +- musl. + +# Testing + +Building a cgo binary with glibc: ``` -$ bazelisk run //test:hello +$ bazel build //test:gognu ... -Hello World! +$ ../bazel-bin/test/gognu_/gognu +hello, world +$ file ../bazel-bin/test/gognu_/gognu +../bazel-bin/test/gognu_/gognu: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.0.0, Go BuildID=redacted, with debug_info, not stripped ``` -Then test that you can run docker images: +Build a cgo binary with musl: + ``` -$ bazelisk run //test:hello_image +$ bazel build --platforms @com_github_ziglang_zig//:platform_linux-x86_64-musl :gomusl ... -Hello World! -``` - -Now we can check if C++ exceptions work locally: -``` -$ bazelisk run //test:exception -... -will throw and expect to catch an error... -caught: error -done -``` - -And whether or not they work in a docker container: -``` -$ bazelisk run //test:exception_image -``` - -If they *work*, then you'll see the same output as above. If not, you'll see: - -``` -will throw and expect to catch an error... -libc++abi: terminating with uncaught exception of type char const* +$ 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 +hello, world ``` If you want to try the above in a transient docker environment, you can do: @@ -43,6 +36,6 @@ $ docker run --rm -it -v $(pwd):/workspace debian:buster-slim # apt update && apt install curl ca-certificates --no-install-recommends -y && curl -L https://github.com/bazelbuild/bazelisk/releases/download/v1.7.5/bazelisk-linux-amd64 > /usr/bin/bazel && chmod +x /usr/bin/bazel # cd /workspace # export CC=/usr/bin/false -# bazel run --platforms=@com_github_ziglang_zig//:x86_64-linux-gnu.2.28 //test:hello -# bazel run --platforms=@com_github_ziglang_zig//:x86_64-linux-gnu.2.28 //test:exception -``` \ No newline at end of file +# bazel run --platforms @com_github_ziglang_zig//:platform_linux-x86_64-musl :gomusl +# bazel run --platforms @com_github_ziglang_zig//:platform_linux-x86_64-glibc :gomusl +``` diff --git a/test/BUILD b/test/BUILD index a98f4d4..6b3da2e 100644 --- a/test/BUILD +++ b/test/BUILD @@ -1,15 +1,26 @@ -cc_binary( - name = "hello", - srcs = ["hello.cpp"], +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") + +go_library( + name = "go_lib", + srcs = ["hello.go"], + cgo = True, + importpath = "github.com/motiejus/bazel-zig-cc/test", + visibility = ["//visibility:private"], +) + +go_binary( + name = "gognu", + embed = [":go_lib"], + visibility = ["//visibility:public"], +) + +go_binary( + name = "gomusl", + embed = [":go_lib"], + static = "on", target_compatible_with = select({ "@platforms//os:linux": ["@com_github_ziglang_zig//:musl"], "//conditions:default": [], }), + visibility = ["//visibility:public"], ) - -cc_binary( - name = "exception", - srcs = ["exception.cpp"], -) - -# go build -ldflags "-linkmode external -extldflags -static" hello.go diff --git a/test/c/BUILD b/test/c/BUILD deleted file mode 100644 index 2c48a9c..0000000 --- a/test/c/BUILD +++ /dev/null @@ -1,13 +0,0 @@ -cc_binary( - name = "hello", - srcs = ["hello.cpp"], - target_compatible_with = select({ - "@platforms//os:linux": ["@com_github_ziglang_zig//:musl"], - "//conditions:default": [], - }), -) - -cc_binary( - name = "exception", - srcs = ["exception.cpp"], -) diff --git a/test/c/exception.cpp b/test/c/exception.cpp deleted file mode 100644 index 7be1ee2..0000000 --- a/test/c/exception.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include - -int main() { - std::cerr << "will throw and expect to catch an error..." << std::endl; - - try { - throw "error"; - } catch (const char* msg) { - std::cerr << "caught: " << msg << std::endl; - } - std::cerr << "done" << std::endl; -} diff --git a/test/c/hello.cpp b/test/c/hello.cpp deleted file mode 100644 index ccbda56..0000000 --- a/test/c/hello.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main() { - std::cout << "Hello World!" << std::endl; -} \ No newline at end of file diff --git a/test/go/BUILD b/test/go/BUILD deleted file mode 100644 index 04833fa..0000000 --- a/test/go/BUILD +++ /dev/null @@ -1,21 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") - -# go build -ldflags "-linkmode external -extldflags -static" hello.go -go_library( - name = "go_lib", - srcs = ["hello.go"], - cgo = True, - importpath = "github.com/motiejus/bazel-zig-cc/test/go", - visibility = ["//visibility:private"], - target_compatible_with = select({ - "@platforms//os:linux": ["@com_github_ziglang_zig//:musl"], - "//conditions:default": [], - }), -) - -go_binary( - name = "go", - embed = [":go_lib"], - visibility = ["//visibility:public"], - static = "on", -) diff --git a/test/go/hello.go b/test/hello.go similarity index 100% rename from test/go/hello.go rename to test/hello.go diff --git a/zig-toolchains/defs.bzl b/zig-toolchains/defs.bzl index 2a8cc0b..2c242c2 100644 --- a/zig-toolchains/defs.bzl +++ b/zig-toolchains/defs.bzl @@ -39,7 +39,7 @@ TARGET_CONFIGS = [ # the platform is explicitly set to: # --platforms=@com_github_ziglang_zig//:x86_64-linux-musl struct( - target="x86_64-linux-gnu.2.28", + target="x86_64-linux-gnu.2.19", includes=[ "libunwind/include", "libc/include/generic-glibc", @@ -53,7 +53,7 @@ TARGET_CONFIGS = [ constraint_values=[ "@platforms//os:linux", "@platforms//cpu:x86_64", - ":glibc_2_28", + ":glibc_2_19", ], tool_paths={"ld": "ld.lld"}, ), @@ -126,7 +126,7 @@ zig_build_macro(absolute_path={absolute_path}, zig_include_root={zig_include_roo constraint_setting(name = "libc") constraint_value( - name = "glibc_2_28", + name = "glibc_2_19", constraint_setting = ":libc", ) @@ -150,7 +150,7 @@ platform( constraint_values = [ "@platforms//os:linux", "@platforms//cpu:x86_64", - ":glibc_2_28", + ":glibc_2_19", ], )