1
Fork 0

downgrade glibc to 2.19 and update README

This commit is contained in:
Motiejus Jakštys 2021-06-07 23:17:19 +03:00 committed by Motiejus Jakštys
parent 6ed6563a37
commit b826ad9439
8 changed files with 49 additions and 96 deletions

View File

@ -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! $ 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
Now we can check if C++ exceptions work locally: hello, world
```
$ 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*
``` ```
If you want to try the above in a transient docker environment, you can do: 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 # 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 # cd /workspace
# export CC=/usr/bin/false # 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//:platform_linux-x86_64-musl :gomusl
# bazel run --platforms=@com_github_ziglang_zig//:x86_64-linux-gnu.2.28 //test:exception # bazel run --platforms @com_github_ziglang_zig//:platform_linux-x86_64-glibc :gomusl
``` ```

View File

@ -1,15 +1,26 @@
cc_binary( load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
name = "hello",
srcs = ["hello.cpp"], 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({ target_compatible_with = select({
"@platforms//os:linux": ["@com_github_ziglang_zig//:musl"], "@platforms//os:linux": ["@com_github_ziglang_zig//:musl"],
"//conditions:default": [], "//conditions:default": [],
}), }),
visibility = ["//visibility:public"],
) )
cc_binary(
name = "exception",
srcs = ["exception.cpp"],
)
# go build -ldflags "-linkmode external -extldflags -static" hello.go

View File

@ -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"],
)

View File

@ -1,12 +0,0 @@
#include <iostream>
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;
}

View File

@ -1,5 +0,0 @@
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
}

View File

@ -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",
)

View File

@ -39,7 +39,7 @@ TARGET_CONFIGS = [
# the platform is explicitly set to: # the platform is explicitly set to:
# --platforms=@com_github_ziglang_zig//:x86_64-linux-musl # --platforms=@com_github_ziglang_zig//:x86_64-linux-musl
struct( struct(
target="x86_64-linux-gnu.2.28", target="x86_64-linux-gnu.2.19",
includes=[ includes=[
"libunwind/include", "libunwind/include",
"libc/include/generic-glibc", "libc/include/generic-glibc",
@ -53,7 +53,7 @@ TARGET_CONFIGS = [
constraint_values=[ constraint_values=[
"@platforms//os:linux", "@platforms//os:linux",
"@platforms//cpu:x86_64", "@platforms//cpu:x86_64",
":glibc_2_28", ":glibc_2_19",
], ],
tool_paths={"ld": "ld.lld"}, 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_setting(name = "libc")
constraint_value( constraint_value(
name = "glibc_2_28", name = "glibc_2_19",
constraint_setting = ":libc", constraint_setting = ":libc",
) )
@ -150,7 +150,7 @@ platform(
constraint_values = [ constraint_values = [
"@platforms//os:linux", "@platforms//os:linux",
"@platforms//cpu:x86_64", "@platforms//cpu:x86_64",
":glibc_2_28", ":glibc_2_19",
], ],
) )