From 94d5864cb73cbe33d19fefd10daff8c9c794009e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Thu, 5 May 2022 14:36:36 +0300 Subject: [PATCH] move `--no-gc-sections` hack to the toolchain definition Thanks laurynasl for the tip. --- toolchain/defs.bzl | 28 +-------------- toolchain/zig_toolchain.bzl | 68 +++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 52 deletions(-) diff --git a/toolchain/defs.bzl b/toolchain/defs.bzl index 3a65eeb..696931a 100644 --- a/toolchain/defs.bzl +++ b/toolchain/defs.bzl @@ -78,33 +78,7 @@ fi export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc" export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR -lookup_args=() -add_arg= -case "{zig_tool}" in - cc|c++) - lookup_args=("-Wl,--gc-sections" "-Wl,--no-gc-sections") - add_arg="-Wl,--no-gc-sections" - ;; - ld.lld) - lookup_args=("--gc-sections" "--no-gc-sections") - add_arg="--no-gc-sections" - ;; -esac - -has_special_arg=0 -for arg in "${{lookup_args[@]}}"; do - if [[ " ${{args[*]}} " == *" $arg "* ]]; then - has_special_arg=1 - break - fi -done - -args=( "$@" ) -if [[ "$has_special_arg" == 0 ]]; then - args+=( "$add_arg" ) -fi - -exec "{zig}" "{zig_tool}" "${{args[@]}}" +exec "{zig}" "{zig_tool}" "$@" """ _ZIG_TOOLS = [ diff --git a/toolchain/zig_toolchain.bzl b/toolchain/zig_toolchain.bzl index f0793b2..aa14ec1 100644 --- a/toolchain/zig_toolchain.bzl +++ b/toolchain/zig_toolchain.bzl @@ -14,12 +14,15 @@ all_link_actions = [ ACTION_NAMES.cpp_link_nodeps_dynamic_library, ] -all_compile_actions = [ - ACTION_NAMES.assemble, +compile_and_link_actions = [ ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, +] + +rest_compile_actions = [ + ACTION_NAMES.assemble, ACTION_NAMES.cc_flags_make_variable, ACTION_NAMES.clif_match, - ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_module_compile, @@ -29,27 +32,42 @@ all_compile_actions = [ ] def _zig_cc_toolchain_config_impl(ctx): - default_compiler_flags = feature( - name = "default_compiler_flags", + compiler_flags = [ + "-I" + d + for d in ctx.attr.cxx_builtin_include_directories + ] + [ + "-target", + ctx.attr.target + ctx.attr.target_suffix, + "-no-canonical-prefixes", + "-Wno-builtin-macro-redefined", + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + ] + no_gc_sections = ["-Wl,--no-gc-sections"] + + compile_and_link_flags = feature( + name = "compile_and_link_flags", enabled = True, flag_sets = [ flag_set( - actions = all_compile_actions, + actions = compile_and_link_actions, flag_groups = [ - flag_group( - flags = [ - "-I" + d - for d in ctx.attr.cxx_builtin_include_directories - ] + [ - "-target", - ctx.attr.target + ctx.attr.target_suffix, - "-no-canonical-prefixes", - "-Wno-builtin-macro-redefined", - "-D__DATE__=\"redacted\"", - "-D__TIMESTAMP__=\"redacted\"", - "-D__TIME__=\"redacted\"", - ] + ctx.attr.copts, - ), + flag_group(flags = no_gc_sections), + flag_group(flags = compiler_flags + ctx.attr.copts), + ], + ), + ], + ) + + rest_compile_flags = feature( + name = "rest_compile_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = rest_compile_actions, + flag_groups = [ + flag_group(flags = compiler_flags + ctx.attr.copts), ], ), ], @@ -63,10 +81,9 @@ def _zig_cc_toolchain_config_impl(ctx): actions = all_link_actions, flag_groups = ([ flag_group( - flags = [ - "-target", - ctx.attr.target, - ] + ctx.attr.linkopts, + flags = ["-target", ctx.attr.target] + + no_gc_sections + + ctx.attr.linkopts, ), ]), ), @@ -74,7 +91,8 @@ def _zig_cc_toolchain_config_impl(ctx): ) features = [ - default_compiler_flags, + compile_and_link_flags, + rest_compile_flags, default_linker_flags, ]