From 921fda92603cd59c876792fc99b912c04779a800 Mon Sep 17 00:00:00 2001 From: Jeremy Volkman Date: Tue, 24 May 2022 17:14:35 -0700 Subject: [PATCH] Adds dynamic_library_linkopts to zig_cc_toolchain_config If specified, the flags in dynamic_library_linkopts are included in cpp_link_dynamic_library and cpp_link_nodeps_dynamic_library actions. --- toolchain/private/cc_toolchains.bzl | 2 ++ toolchain/private/defs.bzl | 3 +++ toolchain/zig_toolchain.bzl | 22 +++++++++++++++++++--- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/toolchain/private/cc_toolchains.bzl b/toolchain/private/cc_toolchains.bzl index 64e99f1..3a1e1cb 100644 --- a/toolchain/private/cc_toolchains.bzl +++ b/toolchain/private/cc_toolchains.bzl @@ -32,6 +32,7 @@ def declare_cc_toolchains(absolute_path, zig_include_root): absolute_tool_paths[name] = "%s/%s" % (absolute_path, tool_path) linkopts = target_config.linkopts + dynamic_library_linkopts = target_config.dynamic_library_linkopts copts = target_config.copts for s in getattr(target_config, "linker_version_scripts", []): linkopts = linkopts + ["-Wl,--version-script,%s/%s" % (absolute_path, s)] @@ -45,6 +46,7 @@ def declare_cc_toolchains(absolute_path, zig_include_root): cxx_builtin_include_directories = cxx_builtin_include_directories, copts = copts, linkopts = linkopts, + dynamic_library_linkopts = dynamic_library_linkopts, target_cpu = target_config.bazel_target_cpu, target_system_name = "unknown", target_libc = "unknown", diff --git a/toolchain/private/defs.bzl b/toolchain/private/defs.bzl index 6059065..053641c 100644 --- a/toolchain/private/defs.bzl +++ b/toolchain/private/defs.bzl @@ -54,6 +54,7 @@ def _target_darwin(gocpu, zigcpu): "libc/include/any-macos-any", ], linkopts = [], + dynamic_library_linkopts = [], copts = [], bazel_target_cpu = "darwin", constraint_values = [ @@ -84,6 +85,7 @@ def _target_linux_gnu(gocpu, zigcpu, glibc_version): compiler_extra_includes = ["glibc-hacks/glibchack-fcntl.h"] if fcntl_hack else [], linker_version_scripts = ["glibc-hacks/fcntl.map"] if fcntl_hack else [], linkopts = ["-lc++", "-lc++abi"], + dynamic_library_linkopts = [], copts = [], bazel_target_cpu = "k8", constraint_values = [ @@ -105,6 +107,7 @@ def _target_linux_musl(gocpu, zigcpu): "libc/include/{}-linux-any".format(zigcpu), ] + (["libc/include/x86-linux-any"] if zigcpu == "x86_64" else []), linkopts = ["-s", "-w"], + dynamic_library_linkopts = [], copts = ["-D_LIBCPP_HAS_MUSL_LIBC", "-D_LIBCPP_HAS_THREAD_API_PTHREAD"], bazel_target_cpu = "k8", constraint_values = [ diff --git a/toolchain/zig_toolchain.bzl b/toolchain/zig_toolchain.bzl index aa14ec1..f22bc4c 100644 --- a/toolchain/zig_toolchain.bzl +++ b/toolchain/zig_toolchain.bzl @@ -14,6 +14,11 @@ all_link_actions = [ ACTION_NAMES.cpp_link_nodeps_dynamic_library, ] +dynamic_library_link_actions = [ + ACTION_NAMES.cpp_link_dynamic_library, + ACTION_NAMES.cpp_link_nodeps_dynamic_library, +] + compile_and_link_actions = [ ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile, @@ -73,21 +78,31 @@ def _zig_cc_toolchain_config_impl(ctx): ], ) + if ctx.attr.dynamic_library_linkopts: + dynamic_library_flag_sets = [ + flag_set( + actions = dynamic_library_link_actions, + flag_groups = [flag_group(flags = ctx.attr.dynamic_library_linkopts)], + ), + ] + else: + dynamic_library_flag_sets = [] + default_linker_flags = feature( name = "default_linker_flags", enabled = True, flag_sets = [ flag_set( actions = all_link_actions, - flag_groups = ([ + flag_groups = [ flag_group( flags = ["-target", ctx.attr.target] + no_gc_sections + ctx.attr.linkopts, ), - ]), + ], ), - ], + ] + dynamic_library_flag_sets, ) features = [ @@ -119,6 +134,7 @@ zig_cc_toolchain_config = rule( attrs = { "cxx_builtin_include_directories": attr.string_list(), "linkopts": attr.string_list(), + "dynamic_library_linkopts": attr.string_list(), "copts": attr.string_list(), "tool_paths": attr.string_dict(), "target": attr.string(),