1

Use artifact_name_pattern to specify proper macos and windows artifact names.

On macos, dynamic libraries are generated as "libfoo.dylib".

On windows, executables end with ".exe", static libraries end with ".lib",
and dynamic libraries end with ".dll".
This commit is contained in:
Jeremy Volkman 2022-12-17 15:27:21 -08:00 committed by Motiejus Jakštys
parent 6ee01496be
commit cc8f113489
4 changed files with 39 additions and 11 deletions

View File

@ -338,17 +338,6 @@ is currently not implemented.
target macos.10 (Catalina), macos.11 (Big Sur) or macos.12 (Monterey). It target macos.10 (Catalina), macos.11 (Big Sur) or macos.12 (Monterey). It
currently targets the lowest version, without ability to change it. currently targets the lowest version, without ability to change it.
## Windows only: output file extensions
For Windows targets Bazel uses Unix extensions for output binaries. Those may
need to be renamed before deploying to the Windows system. Here is a primer:
| Binary type | Bazel extension | Windows extension |
|----------------|-----------------|-------------------|
| Static library | .a | .lib |
| Shared library | .so | .dll |
| Executable | (no extension) | .exe |
# Known Issues In Upstream # Known Issues In Upstream
This section lists issues that I've stumbled into when using `zig cc`, and is This section lists issues that I've stumbled into when using `zig cc`, and is

View File

@ -39,6 +39,10 @@ def declare_cc_toolchains(os, zig_sdk_path):
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]
# We can't pass a list of structs to a rule, so we use json encoding.
artifact_name_patterns = getattr(target_config, "artifact_name_patterns", [])
artifact_name_pattern_strings = [json.encode(p) for p in artifact_name_patterns]
zig_cc_toolchain_config( zig_cc_toolchain_config(
name = zigtarget + "_cc_config", name = zigtarget + "_cc_config",
target = zigtarget, target = zigtarget,
@ -53,6 +57,7 @@ def declare_cc_toolchains(os, zig_sdk_path):
compiler = "clang", compiler = "clang",
abi_version = "unknown", abi_version = "unknown",
abi_libc_version = "unknown", abi_libc_version = "unknown",
artifact_name_patterns = artifact_name_pattern_strings,
visibility = ["//visibility:private"], visibility = ["//visibility:private"],
) )

View File

@ -72,6 +72,13 @@ def _target_macos(gocpu, zigcpu):
"@platforms//cpu:{}".format(zigcpu), "@platforms//cpu:{}".format(zigcpu),
], ],
tool_paths = {"ld": "ld64.lld"}, tool_paths = {"ld": "ld64.lld"},
artifact_name_patterns = [
{
"category_name": "dynamic_library",
"prefix": "lib",
"extension": ".dylib",
},
],
) )
def _target_windows(gocpu, zigcpu): def _target_windows(gocpu, zigcpu):
@ -92,6 +99,23 @@ def _target_windows(gocpu, zigcpu):
"@platforms//cpu:{}".format(zigcpu), "@platforms//cpu:{}".format(zigcpu),
], ],
tool_paths = {"ld": "ld64.lld"}, tool_paths = {"ld": "ld64.lld"},
artifact_name_patterns = [
{
"category_name": "static_library",
"prefix": "",
"extension": ".lib",
},
{
"category_name": "dynamic_library",
"prefix": "",
"extension": ".dll",
},
{
"category_name": "executable",
"prefix": "",
"extension": ".exe",
},
],
) )
def _target_linux_gnu(gocpu, zigcpu, glibc_version): def _target_linux_gnu(gocpu, zigcpu, glibc_version):
@ -126,6 +150,7 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version):
], ],
libc_constraint = "@zig_sdk//libc:{}".format(glibc_suffix), libc_constraint = "@zig_sdk//libc:{}".format(glibc_suffix),
tool_paths = {"ld": "ld.lld"}, tool_paths = {"ld": "ld.lld"},
artifact_name_patterns = [],
) )
def _target_linux_musl(gocpu, zigcpu): def _target_linux_musl(gocpu, zigcpu):
@ -151,4 +176,5 @@ def _target_linux_musl(gocpu, zigcpu):
], ],
libc_constraint = "@zig_sdk//libc:musl", libc_constraint = "@zig_sdk//libc:musl",
tool_paths = {"ld": "ld.lld"}, tool_paths = {"ld": "ld.lld"},
artifact_name_patterns = [],
) )

View File

@ -1,6 +1,7 @@
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load( load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"artifact_name_pattern",
"feature", "feature",
"feature_set", "feature_set",
"flag_group", "flag_group",
@ -140,6 +141,11 @@ def _zig_cc_toolchain_config_impl(ctx):
supports_dynamic_linker, supports_dynamic_linker,
] + _compilation_mode_features(ctx) ] + _compilation_mode_features(ctx)
artifact_name_patterns = [
artifact_name_pattern(**json.decode(p))
for p in ctx.attr.artifact_name_patterns
]
return cc_common.create_cc_toolchain_config_info( return cc_common.create_cc_toolchain_config_info(
ctx = ctx, ctx = ctx,
features = features, features = features,
@ -156,6 +162,7 @@ def _zig_cc_toolchain_config_impl(ctx):
for name, path in ctx.attr.tool_paths.items() for name, path in ctx.attr.tool_paths.items()
], ],
cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories, cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
artifact_name_patterns = artifact_name_patterns,
) )
zig_cc_toolchain_config = rule( zig_cc_toolchain_config = rule(
@ -174,6 +181,7 @@ zig_cc_toolchain_config = rule(
"compiler": attr.string(), "compiler": attr.string(),
"abi_version": attr.string(), "abi_version": attr.string(),
"abi_libc_version": attr.string(), "abi_libc_version": attr.string(),
"artifact_name_patterns": attr.string_list(),
}, },
provides = [CcToolchainConfigInfo], provides = [CcToolchainConfigInfo],
) )