Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Attempt at Mac OS compilation #184

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a9f4ea6
wip macos
motiejus Mar 28, 2023
adc3cfb
got some output using 14.2
chrisirhc Jun 15, 2024
db2b76f
found the right libc location
chrisirhc Jun 15, 2024
206507d
add back zig-wrapper hack (Fix UnknownApplicationBinaryInterface)
chrisirhc Jun 15, 2024
47b1b9e
linking seems to work
chrisirhc Jun 15, 2024
1f7c195
seems to work with the hermetic_cc_toolchain prefix in path
chrisirhc Jun 16, 2024
e60f446
also seems to work
chrisirhc Jun 16, 2024
012b0f7
fix bazel test failure
chrisirhc Jun 16, 2024
ddf7d1d
attempt to fix tests
chrisirhc Jun 16, 2024
be7c7cf
another attempt at adding sdk targets
chrisirhc Jun 16, 2024
840b73a
fix target name
chrisirhc Jun 17, 2024
1ef74bc
NOT WORKING hack to get deps
chrisirhc Jun 27, 2024
46dfd43
NOT WORKING: add sysroot with skylib directory
chrisirhc Jun 27, 2024
15af3a1
cleanup code back to not finding the Frameworks folder
chrisirhc Jun 27, 2024
8c3382e
seems to work again
chrisirhc Jun 28, 2024
500b184
remove expansion
chrisirhc Jun 29, 2024
162f697
remove deadcode
chrisirhc Jun 29, 2024
4f8b860
register corrct name
chrisirhc Jun 29, 2024
e1a9f87
first stab, not working due to symlink
chrisirhc Jul 10, 2024
071666b
fix recursive symlink issue
chrisirhc Jul 11, 2024
e5e5d27
fix path to Frameworks
chrisirhc Jul 11, 2024
bc2fca5
fix path to usr/lib
chrisirhc Jul 11, 2024
19e79e2
remove get_auth, only avilable in newer bazel versions
chrisirhc Jul 11, 2024
f46d1f9
upgrade to bazel 7.1 for is_dir
chrisirhc Jul 12, 2024
d5fe51f
switch to tar.gz
chrisirhc Jul 14, 2024
24dce21
make sure usr_include is included
chrisirhc Jul 14, 2024
7af1503
clean up changes to ease review
chrisirhc Nov 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ register_toolchains(
# (linux,darwin,windows)x(amd64,arm64)
"@zig_sdk//toolchain:linux_amd64_gnu.2.28",
"@zig_sdk//toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//toolchain:darwin_amd64_sdk.14.2",
"@zig_sdk//toolchain:darwin_arm64_sdk.14.2",
"@zig_sdk//toolchain:windows_amd64",
"@zig_sdk//toolchain:windows_arm64",

Expand All @@ -47,6 +49,10 @@ register_toolchains(
# arm64 toolchains for libc-aware platforms:
"@zig_sdk//libc_aware/toolchain:linux_arm64_gnu.2.28",
"@zig_sdk//libc_aware/toolchain:linux_arm64_musl",

"@zig_sdk//libc_aware/toolchain:darwin_amd64_sdk.14.2",
"@zig_sdk//libc_aware/toolchain:darwin_arm64_sdk.14.2",

# wasm/wasi toolchains
"@zig_sdk//toolchain:wasip1_wasm",
)
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 10 additions & 0 deletions toolchain/BUILD.macos.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0

load("@hermetic_cc_toolchain//toolchain:defs.bzl", "declare_macos_sdk_files")

package(
default_visibility = ["//visibility:public"],
)

declare_macos_sdk_files()
2 changes: 2 additions & 0 deletions toolchain/BUILD.sdk.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ package(

declare_files(
os = {os},
macos_sdk_versions = {macos_sdk_versions},
)

declare_cc_toolchains(
os = {os},
zig_sdk_path = {zig_sdk_path},
macos_sdk_versions = {macos_sdk_versions},
)
126 changes: 104 additions & 22 deletions toolchain/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,54 @@ are. Thus we need a bit of your collaboration to get to the bottom of it.
After commenting on the issue, `rm -fr {cache_prefix}` and re-run your command.
"""

_want_format = """
Unexpected MacOS SDK definition. Expected format:

zig_toolchain(
macos_sdks = [
struct(
version = "14.2",
urls = [ "https://<...>", ... ],
sha256 = "<...>",
),
...
],
)

"""

def toolchains(
version = VERSION,
url_formats = [],
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.
The platforms are not registered automatically, that should be done by
the user with register_toolchains() in the WORKSPACE file. See README
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,
)

if not url_formats:
if "dev" in version:
original_format = URL_FORMAT_NIGHTLY
Expand All @@ -83,6 +119,14 @@ def toolchains(
url_formats = url_formats,
host_platform_sha256 = host_platform_sha256,
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,
)

def _quote(s):
Expand Down Expand Up @@ -115,25 +159,31 @@ def _zig_repository_impl(repository_ctx):
# https://bazel.build/extending/repo#when_is_the_implementation_function_executed
# and a related rules_go PR:
# https://github.com/bazelbuild/bazel-gazelle/pull/1206
for dest, src in {
"platform/BUILD": "//toolchain/platform:BUILD",
"toolchain/BUILD": "//toolchain/toolchain:BUILD",
"libc/BUILD": "//toolchain/libc:BUILD",
"libc_aware/platform/BUILD": "//toolchain/libc_aware/platform:BUILD",
"libc_aware/toolchain/BUILD": "//toolchain/libc_aware/toolchain:BUILD",
}.items():
repository_ctx.symlink(Label(src), dest)
macos_sdk_versions_str = _macos_versions(repository_ctx.attr.macos_sdk_versions)
repository_ctx.symlink(Label("//toolchain/platform:BUILD"), "platform/BUILD")
repository_ctx.template(
"BUILD",
Label("//toolchain:BUILD.sdk.bazel"),
executable = False,
substitutions = {
"{zig_sdk_path}": _quote("external/zig_sdk"),
"{os}": _quote(os),
"{macos_sdk_versions}": macos_sdk_versions_str,
},
)

for dest, src in {
"BUILD": "//toolchain:BUILD.sdk.bazel",
"toolchain/BUILD": "//toolchain/toolchain:BUILD",
"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():
repository_ctx.template(
dest,
Label(src),
executable = False,
substitutions = {
"{zig_sdk_path}": _quote("external/zig_sdk"),
"{os}": _quote(os),
"{macos_sdk_versions}": macos_sdk_versions_str,
},
)

Expand Down Expand Up @@ -216,8 +266,7 @@ def _zig_repository_impl(repository_ctx):
exe = ".exe" if os == "windows" else ""
for t in _BUILTIN_TOOLS:
repository_ctx.symlink("tools/zig-wrapper{}".format(exe), "tools/{}{}".format(t, exe))

for target_config in target_structs():
for target_config in target_structs(repository_ctx.attr.macos_sdk_versions):
tool_path = zig_tool_path(os).format(
zig_tool = "c++",
zigtarget = target_config.zigtarget,
Expand All @@ -230,16 +279,42 @@ zig_repository = repository_rule(
"host_platform_sha256": attr.string_dict(),
"url_formats": attr.string_list(allow_empty = False),
"host_platform_ext": attr.string_dict(),
"macos_sdk_versions": attr.string_list(),
},
environ = ["HERMETIC_CC_TOOLCHAIN_CACHE_PREFIX"],
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,
stripPrefix = 'xcode-frameworks-122b43323db27b2082a2d44ed2121de21c9ccf75',
)

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):
native.filegroup(name = name, **kwargs)
return ":" + name

def declare_files(os):
def declare_macos_sdk_files():
filegroup(name = "Frameworks", srcs = native.glob(["Frameworks/**"]))
filegroup(name = "usr_include", srcs = native.glob(["include/**"]))
filegroup(name = "usr_lib", srcs = native.glob(["lib/**"]))

def declare_files(os, macos_sdk_versions):
exe = ".exe" if os == "windows" else ""

native.exports_files(["zig{}".format(exe)], visibility = ["//visibility:public"])
Expand All @@ -253,14 +328,14 @@ def declare_files(os):
filegroup(name = "empty")
lazy_filegroups = {}

for target_config in target_structs():
all_includes = [native.glob(["lib/{}/**".format(i)]) for i in target_config.includes]

for target_config in target_structs(macos_sdk_versions):
cxx_tool_label = ":" + zig_tool_path(os).format(
zig_tool = "c++",
zigtarget = target_config.zigtarget,
)

all_includes = [native.glob(["lib/{}/**".format(i)]) for i in target_config.includes]

filegroup(
name = "{}_includes".format(target_config.zigtarget),
srcs = _flatten(all_includes),
Expand All @@ -270,9 +345,10 @@ def declare_files(os):
name = "{}_compiler_files".format(target_config.zigtarget),
srcs = [
":zig",
":lib/std",
":{}_includes".format(target_config.zigtarget),
cxx_tool_label,
],
] + getattr(target_config, "sdk_include_files", [])
)

filegroup(
Expand All @@ -282,7 +358,7 @@ def declare_files(os):
":{}_includes".format(target_config.zigtarget),
cxx_tool_label,
] + native.glob([
"lib/libc/{}/**".format(target_config.libc),
"lib/libc/{}/**".format('darwin'), # figure out how to pass in the right value: (target_config.libc),
"lib/libcxx/**",
"lib/libcxxabi/**",
"lib/libunwind/**",
Expand All @@ -291,7 +367,7 @@ def declare_files(os):
"lib/tsan/**",
"lib/*.zig",
"lib/*.h",
]),
]) + getattr(target_config, "sdk_lib_files", []),
)

filegroup(
Expand All @@ -308,17 +384,23 @@ 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
if d not in lazy_filegroups:
lazy_filegroups[d] = filegroup(name = d, srcs = native.glob([d + "/**"]))


def _flatten(iterable):
result = []
for element in iterable:
result += element
return result


def _macos_versions(versions):
return "[{}]".format(", ".join([_quote(v) for v in versions]))


## Copied from https://github.com/bazelbuild/bazel-skylib/blob/1.4.1/lib/paths.bzl#L59-L98
def _paths_is_absolute(path):
return path.startswith("/") or (len(path) > 2 and path[1] == ":")
Expand Down
13 changes: 11 additions & 2 deletions toolchain/ext.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
load("@hermetic_cc_toolchain//toolchain:defs.bzl", zig_toolchains = "toolchains")
load("@hermetic_cc_toolchain//toolchain:defs.bzl", "macos_sdk", zig_toolchains = "toolchains")

def _toolchains_impl(ctx):
zig_toolchains()
zig_toolchains(
macos_sdks = [
macos_sdk(
version = "14.2",
urls = [ "https://github.com/hexops/xcode-frameworks/archive/122b43323db27b2082a2d44ed2121de21c9ccf75.zip" ],
sha256 = "e774c140fe476e7a030aefb6f782e58ed79a18d0223bb88fee6a89d6d6cf8d30",
# strip_prefix = "xcode-frameworks-122b43323db27b2082a2d44ed2121de21c9ccf75",
)
],
)

toolchains = module_extension(implementation = _toolchains_impl)
20 changes: 0 additions & 20 deletions toolchain/libc/BUILD
Original file line number Diff line number Diff line change
@@ -1,20 +0,0 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the MIT License

load("@hermetic_cc_toolchain//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()
22 changes: 22 additions & 0 deletions toolchain/libc/BUILD.sdk.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0

load("@hermetic_cc_toolchain//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}
)
4 changes: 2 additions & 2 deletions toolchain/libc/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

load("@hermetic_cc_toolchain//toolchain/private:defs.bzl", "LIBCS")

def declare_libcs():
for libc in LIBCS:
def declare_libcs(macos_sdk_versions):
for libc in LIBCS + ["macos." + v for v in macos_sdk_versions]:
native.constraint_value(
name = libc,
constraint_setting = "variant",
Expand Down
10 changes: 0 additions & 10 deletions toolchain/libc_aware/platform/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the MIT License

load("@hermetic_cc_toolchain//toolchain/platform:defs.bzl", "declare_libc_aware_platforms")

package(
default_visibility = ["//visibility:public"],
)

declare_libc_aware_platforms()
12 changes: 12 additions & 0 deletions toolchain/libc_aware/platform/BUILD.sdk.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the Apache License, Version 2.0

load("@hermetic_cc_toolchain//toolchain/platform:defs.bzl", "declare_libc_aware_platforms")

package(
default_visibility = ["//visibility:public"],
)

declare_libc_aware_platforms(
macos_sdk_versions = {macos_sdk_versions}
)
10 changes: 0 additions & 10 deletions toolchain/libc_aware/toolchain/BUILD
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
# Copyright 2023 Uber Technologies, Inc.
# Licensed under the MIT License

load("@hermetic_cc_toolchain//toolchain/toolchain:defs.bzl", "declare_libc_aware_toolchains")

package(
default_visibility = ["//visibility:public"],
)

declare_libc_aware_toolchains()
Loading
Loading