2022-10-01 14:26:45 +03:00
|
|
|
_ZIG_TOOL_PATH = "tools/{zigtarget}/{zig_tool}"
|
2022-04-06 23:10:45 +03:00
|
|
|
|
|
|
|
# Zig supports even older glibcs than defined below, but we have tested only
|
|
|
|
# down to 2.17.
|
|
|
|
# $ zig targets | jq -r '.glibc[]' | sort -V
|
|
|
|
_GLIBCS = [
|
|
|
|
"2.17",
|
|
|
|
"2.18",
|
|
|
|
"2.19",
|
|
|
|
"2.22",
|
|
|
|
"2.23",
|
|
|
|
"2.24",
|
|
|
|
"2.25",
|
|
|
|
"2.26",
|
|
|
|
"2.27",
|
|
|
|
"2.28",
|
|
|
|
"2.29",
|
|
|
|
"2.30",
|
|
|
|
"2.31",
|
|
|
|
"2.32",
|
|
|
|
"2.33",
|
|
|
|
"2.34",
|
|
|
|
]
|
|
|
|
|
2022-12-14 00:02:23 +02:00
|
|
|
_INCLUDE_TAIL = [
|
|
|
|
"libcxx/include",
|
|
|
|
"libcxxabi/include",
|
|
|
|
"include",
|
|
|
|
]
|
2022-12-13 17:19:42 +02:00
|
|
|
|
2022-04-15 15:20:42 +03:00
|
|
|
LIBCS = ["musl"] + ["gnu.{}".format(glibc) for glibc in _GLIBCS]
|
2022-04-13 17:52:25 +03:00
|
|
|
|
2022-05-29 19:35:16 +03:00
|
|
|
def zig_tool_path(os):
|
|
|
|
if os == "windows":
|
zig launcher: replace shell wrappers with a binary
Until now we needed to maintain two versions of the zig launcher: one
for Windows and one for everything else. This was problematic for two
reasons:
1. I do not know powershell and thus keep breaking the Windows wrapper
all the time (see git history of Fabian fixing stuff that I broke).
2. This makes bazel-zig-cc dependent on the system shell, making it not
really hermetic. So the recently added
`--experimental_use_hermetic_linux_sandbox` does not work with
bazel-zig-cc, unless we bind-mount a bunch of stuff: `/usr`, `/bin`,
`/lib`, `/usr/lib:/lib`, `/usr/lib64:/lib64` and `/proc`.
Switching to a Zig-based wrapper solves both issues, and we can do this:
bazel build "$@" \
--experimental_use_hermetic_linux_sandbox \
--sandbox_add_mount_pair=/proc \
<...>
Zig itself still depends on `/proc` for `/proc/self/exe`, so we need to
keep that. I will look into reducing even that dependency separately.
Not all is nice and shiny though: this commit replaces ~80 LOC worth of
shell scripts wrappers with a singe ~300 LOC zig program, which is
arguably harder to understand. However, it is easier to change, at least
for me, because it's a single file with unit tests! Most importantly,
the gnarly code (which resolves paths and sets environment variables) is
cross-platform.
Thanks to Fabian Hahn for testing this on Windows and pointing out
errors.
2022-12-20 17:00:50 +02:00
|
|
|
return _ZIG_TOOL_PATH + ".exe"
|
2022-05-29 19:35:16 +03:00
|
|
|
else:
|
|
|
|
return _ZIG_TOOL_PATH
|
|
|
|
|
2023-03-28 08:52:20 +03:00
|
|
|
def target_structs(macos_sdk_versions):
|
2022-04-06 23:10:45 +03:00
|
|
|
ret = []
|
|
|
|
for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")):
|
2022-05-29 19:35:17 +03:00
|
|
|
ret.append(_target_windows(gocpu, zigcpu))
|
2022-04-06 23:10:45 +03:00
|
|
|
ret.append(_target_linux_musl(gocpu, zigcpu))
|
2022-04-18 11:44:51 +03:00
|
|
|
for glibc in _GLIBCS:
|
2022-04-06 23:10:45 +03:00
|
|
|
ret.append(_target_linux_gnu(gocpu, zigcpu, glibc))
|
2023-03-28 08:52:20 +03:00
|
|
|
for macos_sdk_version in macos_sdk_versions:
|
|
|
|
ret.append(_target_macos(gocpu, zigcpu, macos_sdk_version))
|
2022-04-06 23:10:45 +03:00
|
|
|
return ret
|
|
|
|
|
2023-03-28 08:52:20 +03:00
|
|
|
def _target_macos(gocpu, zigcpu, macos_sdk_version):
|
|
|
|
macos_sdk_opts = [
|
|
|
|
"--sysroot",
|
|
|
|
"external/macos_sdk_{}".format(macos_sdk_version),
|
|
|
|
"-F",
|
|
|
|
"/System/Library/Frameworks",
|
|
|
|
]
|
|
|
|
|
|
|
|
copts = macos_sdk_opts
|
2022-12-17 20:26:04 +02:00
|
|
|
|
|
|
|
if zigcpu == "aarch64":
|
2023-03-28 08:52:20 +03:00
|
|
|
copts.append("-mcpu=apple_m1")
|
2022-12-17 20:26:04 +02:00
|
|
|
|
2022-04-06 23:10:45 +03:00
|
|
|
return struct(
|
2023-03-28 08:52:20 +03:00
|
|
|
gotarget = "darwin_{}_sdk.{}".format(gocpu, macos_sdk_version),
|
|
|
|
zigtarget = "{}-macos-sdk.{}".format(zigcpu, macos_sdk_version),
|
|
|
|
includes = [],
|
|
|
|
linkopts = macos_sdk_opts,
|
2022-05-25 03:17:33 +03:00
|
|
|
dynamic_library_linkopts = ["-Wl,-undefined=dynamic_lookup"],
|
2023-03-28 08:52:20 +03:00
|
|
|
cxx_builtin_include_directories = ["external/macos_sdk_{}/usr/include".format(macos_sdk_version)],
|
|
|
|
sdk_include_files = ["@macos_sdk_{}//:usr_include".format(macos_sdk_version)],
|
|
|
|
sdk_lib_files = ["@macos_sdk_{}//:usr_lib".format(macos_sdk_version)],
|
2022-12-17 20:26:04 +02:00
|
|
|
copts = copts,
|
2023-03-28 08:52:20 +03:00
|
|
|
libc = "macos",
|
2022-04-06 23:10:45 +03:00
|
|
|
bazel_target_cpu = "darwin",
|
|
|
|
constraint_values = [
|
|
|
|
"@platforms//os:macos",
|
|
|
|
"@platforms//cpu:{}".format(zigcpu),
|
|
|
|
],
|
|
|
|
tool_paths = {"ld": "ld64.lld"},
|
2022-12-18 01:27:21 +02:00
|
|
|
artifact_name_patterns = [
|
|
|
|
{
|
|
|
|
"category_name": "dynamic_library",
|
|
|
|
"prefix": "lib",
|
|
|
|
"extension": ".dylib",
|
|
|
|
},
|
|
|
|
],
|
2023-03-28 08:52:20 +03:00
|
|
|
libc_constraint = "@zig_sdk//libc:macos.{}".format(macos_sdk_version),
|
2022-04-06 23:10:45 +03:00
|
|
|
)
|
|
|
|
|
2022-05-29 19:35:17 +03:00
|
|
|
def _target_windows(gocpu, zigcpu):
|
|
|
|
return struct(
|
|
|
|
gotarget = "windows_{}".format(gocpu),
|
|
|
|
zigtarget = "{}-windows-gnu".format(zigcpu),
|
|
|
|
includes = [
|
2022-12-13 14:38:51 +02:00
|
|
|
"libc/mingw",
|
2022-05-29 19:35:17 +03:00
|
|
|
"libunwind/include",
|
|
|
|
"libc/include/any-windows-any",
|
2022-12-13 17:19:42 +02:00
|
|
|
] + _INCLUDE_TAIL,
|
2023-03-28 08:52:20 +03:00
|
|
|
linkopts = [],
|
2022-06-02 05:47:51 +03:00
|
|
|
dynamic_library_linkopts = [],
|
2022-05-29 19:35:17 +03:00
|
|
|
copts = [],
|
2022-12-13 14:38:51 +02:00
|
|
|
libc = "mingw",
|
2022-05-29 19:35:17 +03:00
|
|
|
bazel_target_cpu = "x64_windows",
|
|
|
|
constraint_values = [
|
|
|
|
"@platforms//os:windows",
|
|
|
|
"@platforms//cpu:{}".format(zigcpu),
|
|
|
|
],
|
|
|
|
tool_paths = {"ld": "ld64.lld"},
|
2022-12-18 01:27:21 +02:00
|
|
|
artifact_name_patterns = [
|
|
|
|
{
|
|
|
|
"category_name": "static_library",
|
|
|
|
"prefix": "",
|
|
|
|
"extension": ".lib",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"category_name": "dynamic_library",
|
|
|
|
"prefix": "",
|
|
|
|
"extension": ".dll",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"category_name": "executable",
|
|
|
|
"prefix": "",
|
|
|
|
"extension": ".exe",
|
|
|
|
},
|
|
|
|
],
|
2022-05-29 19:35:17 +03:00
|
|
|
)
|
|
|
|
|
2022-04-18 11:44:51 +03:00
|
|
|
def _target_linux_gnu(gocpu, zigcpu, glibc_version):
|
|
|
|
glibc_suffix = "gnu.{}".format(glibc_version)
|
2022-04-06 23:10:45 +03:00
|
|
|
|
2023-01-18 15:42:39 +02:00
|
|
|
compiler_extra_includes = []
|
|
|
|
linker_version_scripts = []
|
|
|
|
if glibc_version < "2.28":
|
|
|
|
# https://github.com/ziglang/zig/issues/5882#issuecomment-888250676
|
|
|
|
compiler_extra_includes.append("glibc-hacks/fcntl.h")
|
|
|
|
linker_version_scripts.append("glibc-hacks/fcntl.map")
|
|
|
|
if glibc_version < "2.34":
|
2023-01-18 22:39:35 +02:00
|
|
|
compiler_extra_includes.append("glibc-hacks/res_search-{}.h".format(gocpu))
|
|
|
|
linker_version_scripts.append("glibc-hacks/res_search-{}.map".format(gocpu))
|
2022-04-06 23:10:45 +03:00
|
|
|
|
|
|
|
return struct(
|
|
|
|
gotarget = "linux_{}_{}".format(gocpu, glibc_suffix),
|
|
|
|
zigtarget = "{}-linux-{}".format(zigcpu, glibc_suffix),
|
|
|
|
includes = [
|
2022-09-26 10:14:08 +03:00
|
|
|
"libc/include/{}-linux-gnu".format(zigcpu),
|
|
|
|
"libc/include/generic-glibc",
|
|
|
|
] +
|
|
|
|
# x86_64-linux-any is x86_64-linux and x86-linux combined.
|
|
|
|
(["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []) +
|
|
|
|
(["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [
|
2022-04-06 23:10:45 +03:00
|
|
|
"libc/include/any-linux-any",
|
2022-12-13 17:19:42 +02:00
|
|
|
] + _INCLUDE_TAIL,
|
2023-01-18 15:42:39 +02:00
|
|
|
compiler_extra_includes = compiler_extra_includes,
|
|
|
|
linker_version_scripts = linker_version_scripts,
|
2023-03-28 08:52:20 +03:00
|
|
|
linkopts = [],
|
2022-05-25 03:14:35 +03:00
|
|
|
dynamic_library_linkopts = [],
|
2022-04-06 23:10:45 +03:00
|
|
|
copts = [],
|
2022-12-13 14:38:51 +02:00
|
|
|
libc = "glibc",
|
2022-04-06 23:10:45 +03:00
|
|
|
bazel_target_cpu = "k8",
|
|
|
|
constraint_values = [
|
|
|
|
"@platforms//os:linux",
|
|
|
|
"@platforms//cpu:{}".format(zigcpu),
|
|
|
|
],
|
2022-04-13 17:52:25 +03:00
|
|
|
libc_constraint = "@zig_sdk//libc:{}".format(glibc_suffix),
|
2022-04-06 23:10:45 +03:00
|
|
|
tool_paths = {"ld": "ld.lld"},
|
2022-12-18 01:27:21 +02:00
|
|
|
artifact_name_patterns = [],
|
2022-04-06 23:10:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
def _target_linux_musl(gocpu, zigcpu):
|
|
|
|
return struct(
|
|
|
|
gotarget = "linux_{}_musl".format(gocpu),
|
|
|
|
zigtarget = "{}-linux-musl".format(zigcpu),
|
|
|
|
includes = [
|
2022-09-26 10:14:08 +03:00
|
|
|
"libc/include/{}-linux-musl".format(zigcpu),
|
|
|
|
"libc/include/generic-musl",
|
|
|
|
] +
|
|
|
|
# x86_64-linux-any is x86_64-linux and x86-linux combined.
|
|
|
|
(["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []) +
|
|
|
|
(["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [
|
2022-04-06 23:10:45 +03:00
|
|
|
"libc/include/any-linux-any",
|
2022-12-13 17:19:42 +02:00
|
|
|
] + _INCLUDE_TAIL,
|
2023-03-28 08:52:20 +03:00
|
|
|
linkopts = [],
|
2022-05-25 03:14:35 +03:00
|
|
|
dynamic_library_linkopts = [],
|
2022-04-06 23:10:45 +03:00
|
|
|
copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"],
|
2022-12-13 14:38:51 +02:00
|
|
|
libc = "musl",
|
2022-04-06 23:10:45 +03:00
|
|
|
bazel_target_cpu = "k8",
|
|
|
|
constraint_values = [
|
|
|
|
"@platforms//os:linux",
|
|
|
|
"@platforms//cpu:{}".format(zigcpu),
|
|
|
|
],
|
2022-04-13 17:52:25 +03:00
|
|
|
libc_constraint = "@zig_sdk//libc:musl",
|
2022-04-06 23:10:45 +03:00
|
|
|
tool_paths = {"ld": "ld.lld"},
|
2022-12-18 01:27:21 +02:00
|
|
|
artifact_name_patterns = [],
|
2022-04-06 23:10:45 +03:00
|
|
|
)
|