From 3c9b7f2383773a18ae5920bd9911adbe5f755f45 Mon Sep 17 00:00:00 2001 From: yuyawk <46620009+yuyawk@users.noreply.github.com> Date: Sat, 13 Apr 2024 05:03:37 +0900 Subject: [PATCH] feat: Mac OS support (#23) --- .bazelrc | 3 ++- .bcr/presubmit.yml | 1 + .github/workflows/tests.yml | 4 ++++ MODULE.bazel | 2 ++ examples/MODULE.bazel | 2 ++ lang/cc/build_error.bzl | 22 ++++++++++++++++++++-- 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.bazelrc b/.bazelrc index 0d1ef09..e101fb8 100644 --- a/.bazelrc +++ b/.bazelrc @@ -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 \ No newline at end of file +build:macos --sandbox_add_mount_pair=/var/tmp +build:macos --build_tag_filters=-darwin_c diff --git a/.bcr/presubmit.yml b/.bcr/presubmit.yml index eb45c39..cedaf23 100644 --- a/.bcr/presubmit.yml +++ b/.bcr/presubmit.yml @@ -5,6 +5,7 @@ bcr_test_module: platform: - "debian10" - "ubuntu2004" + - "macos" bazel: - "6.x" - "7.x" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 657336b..b4b823e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,6 +20,8 @@ jobs: runner: - ubuntu-20.04 - ubuntu-22.04 + - macos-13 + - macos-14 runs-on: ${{ matrix.runner }} steps: - name: Checkout @@ -42,6 +44,8 @@ jobs: runner: - ubuntu-20.04 - ubuntu-22.04 + - macos-13 + - macos-14 runs-on: ${{ matrix.runner }} steps: - name: Checkout diff --git a/MODULE.bazel b/MODULE.bazel index 5d2c909..d96bfdb 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -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, ) diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index b60024a..75e3c04 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -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", ) diff --git a/lang/cc/build_error.bzl b/lang/cc/build_error.bzl index 0ddc53f..6205911 100644 --- a/lang/cc/build_error.bzl +++ b/lang/cc/build_error.bzl @@ -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. @@ -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)