1
Fork 0

wip macos

motiejus@mtwork:/code/bazel-zig-cc$ bazel build  --spawn_strategy=local  --platforms @zig_sdk//libc_aware/platform:macos_aarch64_sdk.13.1 test/c:which_libc
    INFO: Analyzed target //test/c:which_libc (0 packages loaded, 0 targets configured).
    INFO: Found 1 target...
    Target //test/c:which_libc up-to-date:
      bazel-bin/test/c/which_libc
    INFO: Elapsed time: 0.071s, Critical Path: 0.00s
    INFO: 1 process: 1 internal.
    INFO: Build completed successfully, 1 total action
    motiejus@mtwork:/code/bazel-zig-cc$ file bazel-bin/test/c/which_libc
    bazel-bin/test/c/which_libc: Mach-O 64-bit arm64 executable, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL|PIE>
    motiejus@mtwork:/code/bazel-zig-cc$
macos
Motiejus Jakštys 2023-03-28 08:52:20 +03:00
parent 041d7f26ab
commit 27fe6c9ffb
19 changed files with 252 additions and 116 deletions

View File

@ -44,18 +44,27 @@ gazelle_dependencies(go_repository_default_config = "@//:WORKSPACE")
load( load(
"//toolchain:defs.bzl", "//toolchain:defs.bzl",
"macos_sdk",
zig_toolchains = "toolchains", zig_toolchains = "toolchains",
) )
zig_toolchains() zig_toolchains(
macos_sdks = [
macos_sdk(
version = "13.1",
urls = [ "https://dl.jakstys.lt/ntpad/x/MacOSX13.1.sdk.tar.zst" ],
sha256 = "9b65f80a142dfb0b7d295636ad8b8f9b9b3450957f6d101f1076836463e729a9",
)
],
)
register_toolchains( register_toolchains(
# if no `--platform` is specified, these toolchains will be used for # if no `--platform` is specified, these toolchains will be used for
# (linux,darwin,windows)x(amd64,arm64) # (linux,darwin,windows)x(amd64,arm64)
"@zig_sdk//toolchain:linux_amd64_gnu.2.19", "@zig_sdk//toolchain:linux_amd64_gnu.2.19",
"@zig_sdk//toolchain:linux_arm64_gnu.2.28", "@zig_sdk//toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//toolchain:darwin_amd64", "@zig_sdk//toolchain:darwin_amd64_sdk.13.1",
"@zig_sdk//toolchain:darwin_arm64", "@zig_sdk//toolchain:darwin_arm64_sdk.13.1",
"@zig_sdk//toolchain:windows_amd64", "@zig_sdk//toolchain:windows_amd64",
"@zig_sdk//toolchain:windows_arm64", "@zig_sdk//toolchain:windows_arm64",
@ -67,6 +76,9 @@ register_toolchains(
# arm64 toolchains for libc-aware platforms: # arm64 toolchains for libc-aware platforms:
"@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.28", "@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl", "@zig_sdk//libc_aware/toolchain:linux_arm64_musl",
"@zig_sdk//libc_aware/toolchain:darwin_amd64_sdk.13.1",
"@zig_sdk//libc_aware/toolchain:darwin_arm64_sdk.13.1",
) )
http_archive( http_archive(

View File

@ -3,9 +3,6 @@ load("@bazel-zig-cc//rules:platform.bzl", "platform_binary")
cc_binary( cc_binary(
name = "which_libc", name = "which_libc",
srcs = ["main.c"], srcs = ["main.c"],
target_compatible_with = [
"@platforms//os:linux",
],
) )
[ [

View File

@ -2,13 +2,8 @@
// Licensed under the Apache License, Version 2.0 // Licensed under the Apache License, Version 2.0
#include <stdio.h> #include <stdio.h>
#include <features.h>
int main() { int main() {
#ifdef __GLIBC__
printf("glibc_%d.%d\n", __GLIBC__, __GLIBC_MINOR__);
#else
printf("non-glibc\n"); printf("non-glibc\n");
#endif
return 0; return 0;
} }

View File

@ -0,0 +1,10 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain:defs.bzl", "declare_macos_sdk_files")
package(
default_visibility = ["//visibility:public"],
)
declare_macos_sdk_files()

View File

@ -10,6 +10,7 @@ package(
declare_files( declare_files(
os = {os}, os = {os},
macos_sdk_versions = {macos_sdk_versions},
) )
exports_files([ exports_files([
@ -22,4 +23,5 @@ exports_files([
declare_cc_toolchains( declare_cc_toolchains(
os = {os}, os = {os},
zig_sdk_path = {zig_sdk_path}, zig_sdk_path = {zig_sdk_path},
macos_sdk_versions = {macos_sdk_versions},
) )

View File

@ -24,14 +24,14 @@ URL_FORMAT_NIGHTLY = "https://ziglang.org/builds/zig-{host_platform}-{version}.{
# generous enough to host the artifacts, which we use. # generous enough to host the artifacts, which we use.
URL_FORMAT_BAZELMIRROR = "https://mirror.bazel.build/" + URL_FORMAT_NIGHTLY.lstrip("https://") URL_FORMAT_BAZELMIRROR = "https://mirror.bazel.build/" + URL_FORMAT_NIGHTLY.lstrip("https://")
_VERSION = "0.11.0-dev.1796+c9e02d3e6" _VERSION = "0.11.0-dev.2312+dd66e0add"
_HOST_PLATFORM_SHA256 = { _HOST_PLATFORM_SHA256 = {
"linux-aarch64": "5902b34b463635b25c11555650d095eb5030e2a05d8a4570c091313cd1a38b12", "linux-aarch64": "adb62d5616e803d4174a818c6180ba34995f55f8d1d7e9282dc3e71cae3a3ee7",
"linux-x86_64": "aa9da2305fad89f648db2fd1fade9f0f9daf01d06f3b07887ad3098402794778", "linux-x86_64": "e3c05b0a820a137c199bbd06b705b578d9a91b6ce64caadb87b15a0d97f7c9aa",
"macos-aarch64": "51b4e88123d6cbb102f2a6665dd0d61467341f36b07bb0a8d46a37ea367b60d5", "macos-aarch64": "327e851c44efe870aefeddd94be181d5a942658e6e1ae74672ffa6fb5c06f96d",
"macos-x86_64": "dd8eeae5249aa21f9e51ff4ff536a3e7bf2c0686ee78bf6032d18e74c8416c56", "macos-x86_64": "e21a3e5fa7368f5bafdd469702d0b12e3d7da09ee6338b3eb6688cbf91884c7a",
"windows-x86_64": "260f34d0d5312d2642097bb33c14ac552cd57c59a15383364df6764d01f0bfc9", "windows-x86_64": "371e3567b757061b4bbc27de09de98e631f15cf312b45d9e81398f68ed8c89ea",
} }
_HOST_PLATFORM_EXT = { _HOST_PLATFORM_EXT = {
@ -61,23 +61,68 @@ $ <... re-run your command ...>
... and proceed with your life. ... and proceed with your life.
""" """
_want_format = """
Unexpected MacOS SDK definition. Expected format:
zig_toolchain(
macos_sdks = [
struct(
version = "13.1",
urls = [ "https://<...>", ... ],
sha256 = "<...>",
),
...
],
)
"""
def toolchains( def toolchains(
version = _VERSION, version = _VERSION,
url_formats = [URL_FORMAT_BAZELMIRROR, URL_FORMAT_NIGHTLY], url_formats = [URL_FORMAT_BAZELMIRROR, URL_FORMAT_NIGHTLY],
host_platform_sha256 = _HOST_PLATFORM_SHA256, host_platform_sha256 = _HOST_PLATFORM_SHA256,
host_platform_ext = _HOST_PLATFORM_EXT): host_platform_ext = _HOST_PLATFORM_EXT,
macos_sdks = []):
""" """
Download zig toolchain and declare bazel toolchains. Download zig toolchain and declare bazel toolchains.
The platforms are not registered automatically, that should be done by The platforms are not registered automatically, that should be done by
the user with register_toolchains() in the WORKSPACE file. See README the user with register_toolchains() in the WORKSPACE file. See README
for possible choices. for possible choices.
""" """
macos_sdk_versions = []
for sdk in macos_sdks:
if not(bool(sdk.version) and bool(sdk.urls) and bool(sdk.sha256)):
fail(_want_format)
macos_sdk_versions.append(sdk.version)
if not (len(sdk.version) == 4 and
sdk.version[2] == "." and
sdk.version[0:2].isdigit() and
sdk.version[3].isdigit()):
fail("unexpected macos SDK version {}, want DD.D".format(version))
macos_sdk_repository(
name = "macos_sdk_{}".format(sdk.version),
urls = sdk.urls,
sha256 = sdk.sha256,
)
zig_repository( zig_repository(
name = "zig_sdk", name = "zig_sdk",
version = version, version = version,
url_formats = url_formats, url_formats = url_formats,
host_platform_sha256 = host_platform_sha256, host_platform_sha256 = host_platform_sha256,
host_platform_ext = host_platform_ext, host_platform_ext = host_platform_ext,
macos_sdk_versions = macos_sdk_versions,
)
def macos_sdk(version, urls, sha256):
return struct(
version = version,
urls = urls,
sha256 = sha256,
) )
_ZIG_TOOLS = [ _ZIG_TOOLS = [
@ -145,26 +190,31 @@ def _zig_repository_impl(repository_ctx):
# https://bazel.build/extending/repo#when_is_the_implementation_function_executed # https://bazel.build/extending/repo#when_is_the_implementation_function_executed
# and a related rules_go PR: # and a related rules_go PR:
# https://github.com/bazelbuild/bazel-gazelle/pull/1206 # https://github.com/bazelbuild/bazel-gazelle/pull/1206
for dest, src in { macos_sdk_versions_str = _macos_versions(repository_ctx.attr.macos_sdk_versions)
"platform/BUILD": "//toolchain/platform:BUILD", repository_ctx.symlink(Label("//toolchain/platform:BUILD"), "platform/BUILD")
"toolchain/BUILD": "//toolchain/toolchain:BUILD", repository_ctx.template(
"libc/BUILD": "//toolchain/libc:BUILD", "BUILD",
"libc_aware/platform/BUILD": "//toolchain/libc_aware/platform:BUILD", Label("//toolchain:BUILD.sdk.bazel"),
"libc_aware/toolchain/BUILD": "//toolchain/libc_aware/toolchain:BUILD", executable = False,
}.items(): substitutions = {
repository_ctx.symlink(Label(src), dest) "{zig_sdk_path}": _quote("external/zig_sdk"),
"{os}": _quote(os),
"{macos_sdk_versions}": macos_sdk_versions_str,
},
)
for dest, src in { for dest, src in {
"BUILD": "//toolchain:BUILD.sdk.bazel", "toolchain/BUILD": "//toolchain/toolchain:BUILD",
# "private/BUILD": "//toolchain/private:BUILD.sdk.bazel", "libc/BUILD": "//toolchain/libc:BUILD.sdk.bazel",
"libc_aware/platform/BUILD": "//toolchain/libc_aware/platform:BUILD.sdk.bazel",
"libc_aware/toolchain/BUILD": "//toolchain/libc_aware/toolchain:BUILD.sdk.bazel",
}.items(): }.items():
repository_ctx.template( repository_ctx.template(
dest, dest,
Label(src), Label(src),
executable = False, executable = False,
substitutions = { substitutions = {
"{zig_sdk_path}": _quote("external/zig_sdk"), "{macos_sdk_versions}": macos_sdk_versions_str,
"{os}": _quote(os),
}, },
) )
@ -219,7 +269,7 @@ def _zig_repository_impl(repository_ctx):
)) ))
exe = ".exe" if os == "windows" else "" exe = ".exe" if os == "windows" else ""
for target_config in target_structs(): for target_config in target_structs(repository_ctx.attr.macos_sdk_versions):
for zig_tool in _ZIG_TOOLS + target_config.tool_paths.values(): for zig_tool in _ZIG_TOOLS + target_config.tool_paths.values():
tool_path = zig_tool_path(os).format( tool_path = zig_tool_path(os).format(
zig_tool = zig_tool, zig_tool = zig_tool,
@ -243,16 +293,40 @@ zig_repository = repository_rule(
"host_platform_sha256": attr.string_dict(), "host_platform_sha256": attr.string_dict(),
"url_formats": attr.string_list(allow_empty = False), "url_formats": attr.string_list(allow_empty = False),
"host_platform_ext": attr.string_dict(), "host_platform_ext": attr.string_dict(),
"macos_sdk_versions": attr.string_list(),
}, },
environ = ["BAZEL_ZIG_CC_CACHE_PREFIX"], environ = ["BAZEL_ZIG_CC_CACHE_PREFIX"],
implementation = _zig_repository_impl, implementation = _zig_repository_impl,
) )
def _macos_sdk_repository_impl(repository_ctx):
urls = repository_ctx.attr.urls
sha256 = repository_ctx.attr.sha256
repository_ctx.symlink(Label("//toolchain:BUILD.macos.bazel"), "BUILD.bazel")
repository_ctx.download_and_extract(
auth = use_netrc(read_user_netrc(repository_ctx), urls, {}),
url = urls,
sha256 = sha256,
)
macos_sdk_repository = repository_rule(
attrs = {
"urls": attr.string_list(allow_empty = False, mandatory = True),
"sha256": attr.string(mandatory = True),
},
implementation = _macos_sdk_repository_impl,
)
def filegroup(name, **kwargs): def filegroup(name, **kwargs):
native.filegroup(name = name, **kwargs) native.filegroup(name = name, **kwargs)
return ":" + name return ":" + name
def declare_files(os): def declare_macos_sdk_files():
filegroup(name = "usr_include", srcs = native.glob(["usr/include/**"]))
filegroup(name = "usr_lib", srcs = native.glob(["usr/lib/**"]))
def declare_files(os, macos_sdk_versions):
filegroup(name = "all", srcs = native.glob(["**"])) filegroup(name = "all", srcs = native.glob(["**"]))
filegroup(name = "empty") filegroup(name = "empty")
if os == "windows": if os == "windows":
@ -263,27 +337,28 @@ def declare_files(os):
filegroup(name = "lib/std", srcs = native.glob(["lib/std/**"])) filegroup(name = "lib/std", srcs = native.glob(["lib/std/**"]))
lazy_filegroups = {} lazy_filegroups = {}
for target_config in target_structs(): for target_config in target_structs(macos_sdk_versions):
all_includes = [native.glob(["lib/{}/**".format(i)]) for i in target_config.includes]
all_includes.append(getattr(target_config, "compiler_extra_includes", []))
cxx_tool_label = ":" + zig_tool_path(os).format( cxx_tool_label = ":" + zig_tool_path(os).format(
zig_tool = "c++", zig_tool = "c++",
zigtarget = target_config.zigtarget, zigtarget = target_config.zigtarget,
) )
all_includes = [native.glob(["lib/{}/**".format(i)]) for i in target_config.includes]
all_includes.append(getattr(target_config, "compiler_extra_includes", []))
filegroup( filegroup(
name = "{}_includes".format(target_config.zigtarget), name = "{}_includes".format(target_config.zigtarget),
srcs = _flatten(all_includes), srcs = _flatten(all_includes)
) )
filegroup( filegroup(
name = "{}_compiler_files".format(target_config.zigtarget), name = "{}_compiler_files".format(target_config.zigtarget),
srcs = [ srcs = [
":zig", ":zig",
":lib/std",
":{}_includes".format(target_config.zigtarget), ":{}_includes".format(target_config.zigtarget),
cxx_tool_label, cxx_tool_label,
], ] + getattr(target_config, "sdk_include_files", [])
) )
filegroup( filegroup(
@ -301,7 +376,7 @@ def declare_files(os):
"lib/std/**", "lib/std/**",
"lib/*.zig", "lib/*.zig",
"lib/*.h", "lib/*.h",
]), ]) + getattr(target_config, "sdk_lib_files", []),
) )
filegroup( filegroup(
@ -324,13 +399,18 @@ def declare_files(os):
], ],
) )
for d in _DEFAULT_INCLUDE_DIRECTORIES + target_config.includes: for d in _DEFAULT_INCLUDE_DIRECTORIES + getattr(target_config, "includes", []):
d = "lib/" + d d = "lib/" + d
if d not in lazy_filegroups: if d not in lazy_filegroups:
lazy_filegroups[d] = filegroup(name = d, srcs = native.glob([d + "/**"])) lazy_filegroups[d] = filegroup(name = d, srcs = native.glob([d + "/**"]))
def _flatten(iterable): def _flatten(iterable):
result = [] result = []
for element in iterable: for element in iterable:
result += element result += element
return result return result
def _macos_versions(versions):
return "[{}]".format(", ".join([_quote(v) for v in versions]))

View File

@ -253,23 +253,34 @@ fn getTarget(self_exe: []const u8) error{BadParent}!?[]const u8 {
// having fun. // having fun.
var it = mem.split(u8, triple, "-"); var it = mem.split(u8, triple, "-");
const arch = it.next() orelse return error.BadParent; const got_arch = it.next() orelse return error.BadParent;
if (mem.indexOf(u8, "aarch64,x86_64", arch) == null) if (mem.indexOf(u8, "aarch64,x86_64", got_arch) == null)
return error.BadParent; return error.BadParent;
const got_os = it.next() orelse return error.BadParent; const got_os = it.next() orelse return error.BadParent;
if (mem.indexOf(u8, "linux,macos,windows", got_os) == null) if (mem.indexOf(u8, "linux,macos,windows", got_os) == null)
return error.BadParent; return error.BadParent;
const got_abi = it.next();
// ABI triple is too much of a moving target // ABI triple is too much of a moving target
if (it.next() == null) return error.BadParent; if (got_abi == null) return error.BadParent;
// but the target needs to have 3 dashes. // but the target needs to have 3 dashes.
if (it.next() != null) return error.BadParent; if (it.next() != null) return error.BadParent;
if (mem.eql(u8, "c++" ++ EXE, fs.path.basename(self_exe))) if (!mem.eql(u8, "c++" ++ EXE, fs.path.basename(self_exe)))
return triple
else
return null; return null;
// HACK: if OS is macos, then force the ABI triple to "none".
if (!mem.eql(u8, got_os, "macos"))
return triple;
if (mem.eql(u8, got_arch, "aarch64"))
return "aarch64-macos-none";
if (mem.eql(u8, got_arch, "x86_64"))
return "x86_64-macos-none";
unreachable;
} }
const testing = std.testing; const testing = std.testing;

View File

@ -1,20 +0,0 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain/libc:defs.bzl", "declare_libcs")
package(
default_visibility = ["//visibility:public"],
)
constraint_setting(
name = "variant",
default_constraint_value = "unconstrained",
)
constraint_value(
name = "unconstrained",
constraint_setting = "variant",
)
declare_libcs()

View File

@ -0,0 +1,22 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain/libc:defs.bzl", "declare_libcs")
package(
default_visibility = ["//visibility:public"],
)
constraint_setting(
name = "variant",
default_constraint_value = "unconstrained",
)
constraint_value(
name = "unconstrained",
constraint_setting = "variant",
)
declare_libcs(
macos_sdk_versions = {macos_sdk_versions}
)

View File

@ -3,8 +3,8 @@
load("@bazel-zig-cc//toolchain/private:defs.bzl", "LIBCS") load("@bazel-zig-cc//toolchain/private:defs.bzl", "LIBCS")
def declare_libcs(): def declare_libcs(macos_sdk_versions):
for libc in LIBCS: for libc in LIBCS + ["macos." + v for v in macos_sdk_versions]:
native.constraint_value( native.constraint_value(
name = libc, name = libc,
constraint_setting = "variant", constraint_setting = "variant",

View File

@ -1,10 +0,0 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain/platform:defs.bzl", "declare_libc_aware_platforms")
package(
default_visibility = ["//visibility:public"],
)
declare_libc_aware_platforms()

View File

@ -0,0 +1,12 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain/platform:defs.bzl", "declare_libc_aware_platforms")
package(
default_visibility = ["//visibility:public"],
)
declare_libc_aware_platforms(
macos_sdk_versions = {macos_sdk_versions}
)

View File

@ -1,10 +0,0 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain/toolchain:defs.bzl", "declare_libc_aware_toolchains")
package(
default_visibility = ["//visibility:public"],
)
declare_libc_aware_toolchains()

View File

@ -0,0 +1,12 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0
load("@bazel-zig-cc//toolchain/toolchain:defs.bzl", "declare_libc_aware_toolchains")
package(
default_visibility = ["//visibility:public"],
)
declare_libc_aware_toolchains(
macos_sdk_versions = {macos_sdk_versions}
)

View File

@ -14,7 +14,7 @@ def declare_platforms():
for os in oss: for os in oss:
declare_platform(gocpu, zigcpu, bzlos, os) declare_platform(gocpu, zigcpu, bzlos, os)
def declare_libc_aware_platforms(): def declare_libc_aware_platforms(macos_sdk_versions):
# create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions # create @zig_sdk//{os}_{arch}_platform entries with zig and go conventions
# with libc specified # with libc specified
for zigcpu, gocpu in _CPUS: for zigcpu, gocpu in _CPUS:
@ -27,6 +27,15 @@ def declare_libc_aware_platforms():
suffix = "_{}".format(libc), suffix = "_{}".format(libc),
extra_constraints = ["@zig_sdk//libc:{}".format(libc)], extra_constraints = ["@zig_sdk//libc:{}".format(libc)],
) )
for macos_sdk_version in macos_sdk_versions:
declare_platform(
gocpu,
zigcpu,
"macos",
"macos",
suffix = "_sdk.{}".format(macos_sdk_version),
extra_constraints = ["@zig_sdk//libc:macos.{}".format(macos_sdk_version)],
)
def declare_platform(gocpu, zigcpu, bzlos, os, suffix = "", extra_constraints = []): def declare_platform(gocpu, zigcpu, bzlos, os, suffix = "", extra_constraints = []):
constraint_values = [ constraint_values = [

View File

@ -11,12 +11,11 @@ DEFAULT_TOOL_PATHS = {
"strip": "/usr/bin/false", "strip": "/usr/bin/false",
}.items() }.items()
def declare_cc_toolchains(os, zig_sdk_path): def declare_cc_toolchains(os, zig_sdk_path, macos_sdk_versions):
for target_config in target_structs(): for target_config in target_structs(macos_sdk_versions):
gotarget = target_config.gotarget gotarget = target_config.gotarget
zigtarget = target_config.zigtarget zigtarget = target_config.zigtarget
cxx_builtin_include_directories = []
absolute_tool_paths = {} absolute_tool_paths = {}
for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS: for name, path in target_config.tool_paths.items() + DEFAULT_TOOL_PATHS:
if path[0] == "/": if path[0] == "/":
@ -30,9 +29,9 @@ def declare_cc_toolchains(os, zig_sdk_path):
dynamic_library_linkopts = target_config.dynamic_library_linkopts dynamic_library_linkopts = target_config.dynamic_library_linkopts
copts = target_config.copts copts = target_config.copts
linkopts = [] linkopts = target_config.linkopts
for s in getattr(target_config, "linker_version_scripts", []): for s in getattr(target_config, "linker_version_scripts", []):
linkopts = ["-Wl,--version-script,%s/%s" % (zig_sdk_path, s)] linkopts.append("-Wl,--version-script,%s/%s" % (zig_sdk_path, s))
for incl in getattr(target_config, "compiler_extra_includes", []): for incl in getattr(target_config, "compiler_extra_includes", []):
copts = copts + ["-include", zig_sdk_path + "/" + incl] copts = copts + ["-include", zig_sdk_path + "/" + incl]
@ -44,7 +43,11 @@ def declare_cc_toolchains(os, zig_sdk_path):
name = zigtarget + "_cc_config", name = zigtarget + "_cc_config",
target = zigtarget, target = zigtarget,
tool_paths = absolute_tool_paths, tool_paths = absolute_tool_paths,
cxx_builtin_include_directories = cxx_builtin_include_directories, cxx_builtin_include_directories = getattr(
target_config,
"cxx_builtin_include_directories",
[],
),
copts = copts, copts = copts,
linkopts = linkopts, linkopts = linkopts,
dynamic_library_linkopts = dynamic_library_linkopts, dynamic_library_linkopts = dynamic_library_linkopts,

View File

@ -36,36 +36,41 @@ def zig_tool_path(os):
else: else:
return _ZIG_TOOL_PATH return _ZIG_TOOL_PATH
def target_structs(): def target_structs(macos_sdk_versions):
ret = [] ret = []
for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")): for zigcpu, gocpu in (("x86_64", "amd64"), ("aarch64", "arm64")):
ret.append(_target_macos(gocpu, zigcpu))
ret.append(_target_windows(gocpu, zigcpu)) ret.append(_target_windows(gocpu, zigcpu))
ret.append(_target_linux_musl(gocpu, zigcpu)) ret.append(_target_linux_musl(gocpu, zigcpu))
for glibc in _GLIBCS: for glibc in _GLIBCS:
ret.append(_target_linux_gnu(gocpu, zigcpu, glibc)) ret.append(_target_linux_gnu(gocpu, zigcpu, glibc))
for macos_sdk_version in macos_sdk_versions:
ret.append(_target_macos(gocpu, zigcpu, macos_sdk_version))
return ret return ret
def _target_macos(gocpu, zigcpu): def _target_macos(gocpu, zigcpu, macos_sdk_version):
min_os = "11" macos_sdk_opts = [
copts = [] "--sysroot",
"external/macos_sdk_{}".format(macos_sdk_version),
"-F",
"/System/Library/Frameworks",
]
copts = macos_sdk_opts
if zigcpu == "aarch64": if zigcpu == "aarch64":
copts = ["-mcpu=apple_m1"] copts.append("-mcpu=apple_m1")
return struct( return struct(
gotarget = "darwin_{}".format(gocpu), gotarget = "darwin_{}_sdk.{}".format(gocpu, macos_sdk_version),
zigtarget = "{}-macos-none".format(zigcpu), zigtarget = "{}-macos-sdk.{}".format(zigcpu, macos_sdk_version),
includes = [ includes = [],
"libunwind/include", linkopts = macos_sdk_opts,
# TODO: Define a toolchain for each minimum OS version
"libc/include/{}-macos.{}-none".format(zigcpu, min_os),
"libc/include/any-macos.{}-any".format(min_os),
"libc/include/any-macos-any",
] + _INCLUDE_TAIL,
dynamic_library_linkopts = ["-Wl,-undefined=dynamic_lookup"], dynamic_library_linkopts = ["-Wl,-undefined=dynamic_lookup"],
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)],
copts = copts, copts = copts,
libc = "darwin", libc = "macos",
bazel_target_cpu = "darwin", bazel_target_cpu = "darwin",
constraint_values = [ constraint_values = [
"@platforms//os:macos", "@platforms//os:macos",
@ -79,6 +84,7 @@ def _target_macos(gocpu, zigcpu):
"extension": ".dylib", "extension": ".dylib",
}, },
], ],
libc_constraint = "@zig_sdk//libc:macos.{}".format(macos_sdk_version),
) )
def _target_windows(gocpu, zigcpu): def _target_windows(gocpu, zigcpu):
@ -90,6 +96,7 @@ def _target_windows(gocpu, zigcpu):
"libunwind/include", "libunwind/include",
"libc/include/any-windows-any", "libc/include/any-windows-any",
] + _INCLUDE_TAIL, ] + _INCLUDE_TAIL,
linkopts = [],
dynamic_library_linkopts = [], dynamic_library_linkopts = [],
copts = [], copts = [],
libc = "mingw", libc = "mingw",
@ -145,6 +152,7 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version):
] + _INCLUDE_TAIL, ] + _INCLUDE_TAIL,
compiler_extra_includes = compiler_extra_includes, compiler_extra_includes = compiler_extra_includes,
linker_version_scripts = linker_version_scripts, linker_version_scripts = linker_version_scripts,
linkopts = [],
dynamic_library_linkopts = [], dynamic_library_linkopts = [],
copts = [], copts = [],
libc = "glibc", libc = "glibc",
@ -171,6 +179,7 @@ def _target_linux_musl(gocpu, zigcpu):
(["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [ (["libc/include/{}-linux-any".format(zigcpu)] if zigcpu != "x86_64" else []) + [
"libc/include/any-linux-any", "libc/include/any-linux-any",
] + _INCLUDE_TAIL, ] + _INCLUDE_TAIL,
linkopts = [],
dynamic_library_linkopts = [], dynamic_library_linkopts = [],
copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"], copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"],
libc = "musl", libc = "musl",

View File

@ -4,4 +4,6 @@ package(
default_visibility = ["//visibility:public"], default_visibility = ["//visibility:public"],
) )
declare_toolchains() declare_toolchains(
macos_sdk_versions = {macos_sdk_versions},
)

View File

@ -1,7 +1,7 @@
load("@bazel-zig-cc//toolchain/private:defs.bzl", "target_structs") load("@bazel-zig-cc//toolchain/private:defs.bzl", "target_structs")
def declare_toolchains(): def declare_toolchains(macos_sdk_versions):
for target_config in target_structs(): for target_config in target_structs(macos_sdk_versions):
gotarget = target_config.gotarget gotarget = target_config.gotarget
zigtarget = target_config.zigtarget zigtarget = target_config.zigtarget
@ -14,8 +14,8 @@ def declare_toolchains():
_declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints) _declare_toolchain(gotarget, zigtarget, target_config.constraint_values + extra_constraints)
def declare_libc_aware_toolchains(): def declare_libc_aware_toolchains(macos_sdk_versions):
for target_config in target_structs(): for target_config in target_structs(macos_sdk_versions):
gotarget = target_config.gotarget gotarget = target_config.gotarget
zigtarget = target_config.zigtarget zigtarget = target_config.zigtarget