Skip to content

Commit

Permalink
feat: Mac OS support (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyawk authored Apr 12, 2024
1 parent 2ab2d7a commit 3c9b7f2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ test --test_output=errors
# For `hermetic_cc_toolchain`
common --enable_platform_specific_config
build:linux --sandbox_add_mount_pair=/tmp
build:macos --sandbox_add_mount_pair=/var/tmp
build:macos --sandbox_add_mount_pair=/var/tmp
build:macos --build_tag_filters=-darwin_c
1 change: 1 addition & 0 deletions .bcr/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ bcr_test_module:
platform:
- "debian10"
- "ubuntu2004"
- "macos"
bazel:
- "6.x"
- "7.x"
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
runner:
- ubuntu-20.04
- ubuntu-22.04
- macos-13
- macos-14
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
Expand All @@ -42,6 +44,8 @@ jobs:
runner:
- ubuntu-20.04
- ubuntu-22.04
- macos-13
- macos-14
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout
Expand Down
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use_repo(cc_toolchains, "zig_sdk")
register_toolchains(
"@zig_sdk//toolchain:linux_amd64_gnu.2.31",
"@zig_sdk//toolchain:linux_arm64_gnu.2.31",
"@zig_sdk//toolchain:darwin_amd64",
"@zig_sdk//toolchain:darwin_arm64",
dev_dependency = True,
)

Expand Down
2 changes: 2 additions & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ use_repo(cc_toolchains, "zig_sdk")
register_toolchains(
"@zig_sdk//toolchain:linux_amd64_gnu.2.31",
"@zig_sdk//toolchain:linux_arm64_gnu.2.31",
"@zig_sdk//toolchain:darwin_amd64",
"@zig_sdk//toolchain:darwin_arm64",
)
22 changes: 20 additions & 2 deletions lang/cc/build_error.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ def _try_compile(ctx):
stderr = compile_stderr,
)

def _get_library_link_option(library_path):
"""Get library link option, such as `-lfoo`.
Args:
library_path(str): Library path
Returns:
list[str]: Link option for the library.
"""
if not library_path.startswith("lib"):
return ["-l", ":" + library_path]

for ext in [".a", ".so"]:
if library_path.endswith(ext):
return ["-l", library_path.removeprefix("lib").removesuffix(ext)]

return ["-l", ":" + library_path]

def _try_link(ctx, compile_output):
"""Try linking the object file.
Expand Down Expand Up @@ -250,8 +268,8 @@ def _try_link(ctx, compile_output):
# TODO: Ideally the way of linking should be controlled by the attributes
library = library_to_link.static_library if library_to_link.static_library else library_to_link.dynamic_library
if library:
args.add("-L" + library.dirname)
args.add("-l:" + library.basename)
args.add("-L", library.dirname)
args.add(*_get_library_link_option(library.basename))
inputs.append(library)

args.add("-o", link_output)
Expand Down

0 comments on commit 3c9b7f2

Please sign in to comment.