From 39535dadc53b4cdf7f06a216e51157d00d7930b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 16 Jun 2021 12:44:16 +0300 Subject: [PATCH] add macos support + buildifier --- .build.yml | 16 ++++++--- test/BUILD | 13 +++++++ toolchain/defs.bzl | 86 ++++++++++++++++++++++------------------------ 3 files changed, 67 insertions(+), 48 deletions(-) diff --git a/.build.yml b/.build.yml index 34e8183..2239353 100644 --- a/.build.yml +++ b/.build.yml @@ -34,7 +34,15 @@ tasks: --platforms @zig_sdk//:aarch64-linux-musl //test:gomusl file $(./getpath @zig_sdk//:aarch64-linux-musl //test:gomusl) | \ tee /dev/stderr | grep -q "statically linked" - #- test_macos-gnu: | - # cd bazel-zig-cc; ../bazel build --toolchain_resolution_debug=true \ - # --platforms @zig_sdk//:x86_64-macos-gnu \ - # //test:gognu + - test_x86_64-macos-gnu: | + cd bazel-zig-cc; ../bazel build --toolchain_resolution_debug=true \ + --platforms @zig_sdk//:x86_64-macos-gnu \ + //test:gomacos + file $(./getpath @zig_sdk//:x86_64-macos-gnu //test:gomacos) | \ + tee /dev/stderr | grep -q "Mach-O 64-bit arm64 executable" + - test_aarch64-macos-gnu: | + cd bazel-zig-cc; ../bazel build --toolchain_resolution_debug=true \ + --platforms @zig_sdk//:aarch64-macos-gnu \ + //test:gomacos + file $(./getpath @zig_sdk//:aarch64-macos-gnu //test:gomacos) | \ + tee /dev/stderr | grep -q "Mach-O 64-bit x86_64 executable" diff --git a/test/BUILD b/test/BUILD index 3f85c59..862b7df 100644 --- a/test/BUILD +++ b/test/BUILD @@ -11,6 +11,19 @@ go_library( go_binary( name = "gognu", embed = [":go_lib"], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:public"], +) + +go_binary( + name = "gomacos", + embed = [":go_lib"], + gc_linkopts = [ + "-s", + "-w", + "-buildmode=pie", + ], + target_compatible_with = ["@platforms//os:macos"], visibility = ["//visibility:public"], ) diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index 5c1d396..b6c1c1e 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -3,8 +3,7 @@ load(":zig_toolchain.bzl", "zig_cc_toolchain_config") DEFAULT_TOOL_PATHS = { "ar": "ar", - "gcc": "c++", # https://github.com/bazelbuild/bazel/issues/4644 - + "gcc": "c++", # https://github.com/bazelbuild/bazel/issues/4644 "cpp": "/usr/bin/false", "gcov": "/usr/bin/false", "nm": "/usr/bin/false", @@ -21,60 +20,60 @@ DEFAULT_INCLUDE_DIRECTORIES = [ # https://github.com/ziglang/zig/blob/0cfa39304b18c6a04689bd789f5dc4d035ec43b0/src/main.zig#L2962-L2966 TARGET_CONFIGS_LISTOFLISTS = [[ struct( - target="{}-macos-gnu".format(cpu), - includes=[ + target = "{}-macos-gnu".format(cpu), + includes = [ "libunwind/include", "libc/include/any-macos-any", "libc/include/{}-macos-any".format(cpu), "libc/include/{}-macos-gnu".format(cpu), ], # linkopts=["-lc++", "-lc++abi"], - linkopts=[], - copts=[], - bazel_target_cpu="darwin", - constraint_values=[ + linkopts = [], + copts = [], + bazel_target_cpu = "darwin", + constraint_values = [ "@platforms//os:macos", "@platforms//cpu:{}".format(cpu), ], - tool_paths={"ld": "ld64.lld"}, + tool_paths = {"ld": "ld64.lld"}, ), struct( - target="{}-linux-gnu".format(cpu), + target = "{}-linux-gnu".format(cpu), target_suffix = ".2.19", - includes=[ + includes = [ "libunwind/include", "libc/include/generic-glibc", "libc/include/any-linux-any", "libc/include/{}-linux-gnu".format(cpu), "libc/include/{}-linux-any".format(cpu), ], - linkopts=["-lc++", "-lc++abi"], - copts=[], - bazel_target_cpu="k8", - constraint_values=[ + linkopts = ["-lc++", "-lc++abi"], + copts = [], + bazel_target_cpu = "k8", + constraint_values = [ "@platforms//os:linux", "@platforms//cpu:{}".format(cpu), ":libc_gnu", ], - tool_paths={"ld": "ld.lld"}, + tool_paths = {"ld": "ld.lld"}, ), struct( - target="{}-linux-musl".format(cpu), - includes=[ + target = "{}-linux-musl".format(cpu), + includes = [ "libc/include/generic-musl", "libc/include/any-linux-any", "libc/include/{}-linux-musl".format(cpu), "libc/include/{}-linux-any".format(cpu), ], - linkopts=["-s", "-w"], - copts=["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"], - bazel_target_cpu="k8", - constraint_values=[ + linkopts = ["-s", "-w"], + copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"], + bazel_target_cpu = "k8", + constraint_values = [ "@platforms//os:linux", "@platforms//cpu:{}".format(cpu), ":libc_musl", ], - tool_paths={"ld": "ld.lld"}, + tool_paths = {"ld": "ld.lld"}, ), ] for cpu in ("x86_64", "aarch64")] @@ -94,11 +93,10 @@ def toolchain_repositories(): "linux-x86_64": "3e5b4fa3b346e2eae6829dd7ef90e9a0f6e6297cee62017e3d0f0f7c9edfa21e", "macos-x86_64": "9b5e3fefa6ae0b1ab26821323df0641f818e72bffc343e194dc60829005d3055", }, - host_platform_include_root = { "macos-x86_64": "lib/zig/", "linux-x86_64": "lib/", - } + }, ) def register_all_toolchains(): @@ -128,10 +126,10 @@ ZIG_TOOLS = [ "ar", # List of ld tools: https://github.com/ziglang/zig/blob/0cfa39304b18c6a04689bd789f5dc4d035ec43b0/src/main.zig#L2962-L2966 # and also: https://github.com/ziglang/zig/issues/3257 - "ld.lld", # ELF - "ld64.lld", # Mach-O - "lld-link", # COFF - "wasm-ld", # WebAssembly + "ld.lld", # ELF + "ld64.lld", # Mach-O + "lld-link", # COFF + "wasm-ld", # WebAssembly ] def _zig_repository_impl(repository_ctx): @@ -143,8 +141,8 @@ def _zig_repository_impl(repository_ctx): zig_include_root = repository_ctx.attr.host_platform_include_root[host_platform] zig_sha256 = repository_ctx.attr.host_platform_sha256[host_platform] format_vars = { - "version" : repository_ctx.attr.version, - "host_platform" : host_platform, + "version": repository_ctx.attr.version, + "host_platform": host_platform, } zig_url = repository_ctx.attr.url_format.format(**format_vars) @@ -156,8 +154,8 @@ def _zig_repository_impl(repository_ctx): for zig_tool in ZIG_TOOLS: repository_ctx.file( - ZIG_TOOL_PATH.format(zig_tool=zig_tool), - ZIG_TOOL_WRAPPER.format(zig=str(repository_ctx.path("zig")), zig_tool=zig_tool), + ZIG_TOOL_PATH.format(zig_tool = zig_tool), + ZIG_TOOL_WRAPPER.format(zig = str(repository_ctx.path("zig")), zig_tool = zig_tool), ) absolute_path = json.encode(str(repository_ctx.path(""))) @@ -186,9 +184,9 @@ def filegroup(name, **kwargs): return ":" + name def zig_build_macro(absolute_path, zig_include_root): - filegroup(name="empty") - filegroup(name="zig_compiler", srcs=["zig"]) - filegroup(name="lib/std", srcs=native.glob(["lib/std/**"])) + filegroup(name = "empty") + filegroup(name = "zig_compiler", srcs = ["zig"]) + filegroup(name = "lib/std", srcs = native.glob(["lib/std/**"])) native.constraint_setting(name = "libc") @@ -221,7 +219,7 @@ def zig_build_macro(absolute_path, zig_include_root): for d in DEFAULT_INCLUDE_DIRECTORIES + target_config.includes: d = zig_include_root + d 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 + "/**"])) compiler_srcs.append(lazy_filegroups[d]) cxx_builtin_include_directories.append(absolute_path + "/" + d) @@ -230,19 +228,19 @@ def zig_build_macro(absolute_path, zig_include_root): if path[0] == "/": absolute_tool_paths[name] = path continue - tool_path = ZIG_TOOL_PATH.format(zig_tool=path) + tool_path = ZIG_TOOL_PATH.format(zig_tool = path) absolute_tool_paths[name] = "%s/%s" % (absolute_path, tool_path) tool_srcs[name].append(tool_path) - ar_files = filegroup(name=target + "_ar_files", srcs=ar_srcs) - linker_files = filegroup(name=target + "_linker_files", srcs=linker_srcs) - compiler_files = filegroup(name=target + "_compiler_files", srcs=compiler_srcs) - all_files = filegroup(name=target + "_all_files", srcs=all_srcs + [ar_files, linker_files, compiler_files]) + ar_files = filegroup(name = target + "_ar_files", srcs = ar_srcs) + linker_files = filegroup(name = target + "_linker_files", srcs = linker_srcs) + compiler_files = filegroup(name = target + "_compiler_files", srcs = compiler_srcs) + all_files = filegroup(name = target + "_all_files", srcs = all_srcs + [ar_files, linker_files, compiler_files]) zig_cc_toolchain_config( name = target + "_cc_toolchain_config", target = target, - target_suffix = getattr(target_config, 'target_suffix', ''), + target_suffix = getattr(target_config, "target_suffix", ""), tool_paths = absolute_tool_paths, cxx_builtin_include_directories = cxx_builtin_include_directories, copts = target_config.copts, @@ -274,7 +272,7 @@ def zig_build_macro(absolute_path, zig_include_root): toolchains = { target_config.bazel_target_cpu: ":%s_cc_toolchain" % target, }, - tags = ["manual"] + tags = ["manual"], ) native.toolchain(