1

move --no-gc-sections hack to the toolchain definition

Thanks laurynasl for the tip.
This commit is contained in:
Motiejus Jakštys 2022-05-05 14:36:36 +03:00
parent 9ce21b5276
commit 94d5864cb7
2 changed files with 44 additions and 52 deletions

View File

@ -78,33 +78,7 @@ fi
export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc" export ZIG_LOCAL_CACHE_DIR="$_cache_prefix/bazel-zig-cc"
export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR export ZIG_GLOBAL_CACHE_DIR=$ZIG_LOCAL_CACHE_DIR
lookup_args=() exec "{zig}" "{zig_tool}" "$@"
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[@]}}"
""" """
_ZIG_TOOLS = [ _ZIG_TOOLS = [

View File

@ -14,12 +14,15 @@ all_link_actions = [
ACTION_NAMES.cpp_link_nodeps_dynamic_library, ACTION_NAMES.cpp_link_nodeps_dynamic_library,
] ]
all_compile_actions = [ compile_and_link_actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.c_compile, ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
]
rest_compile_actions = [
ACTION_NAMES.assemble,
ACTION_NAMES.cc_flags_make_variable, ACTION_NAMES.cc_flags_make_variable,
ACTION_NAMES.clif_match, ACTION_NAMES.clif_match,
ACTION_NAMES.cpp_compile,
ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_compile,
@ -29,27 +32,42 @@ all_compile_actions = [
] ]
def _zig_cc_toolchain_config_impl(ctx): def _zig_cc_toolchain_config_impl(ctx):
default_compiler_flags = feature( compiler_flags = [
name = "default_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, enabled = True,
flag_sets = [ flag_sets = [
flag_set( flag_set(
actions = all_compile_actions, actions = compile_and_link_actions,
flag_groups = [ flag_groups = [
flag_group( flag_group(flags = no_gc_sections),
flags = [ flag_group(flags = compiler_flags + ctx.attr.copts),
"-I" + d ],
for d in ctx.attr.cxx_builtin_include_directories ),
] + [ ],
"-target", )
ctx.attr.target + ctx.attr.target_suffix,
"-no-canonical-prefixes", rest_compile_flags = feature(
"-Wno-builtin-macro-redefined", name = "rest_compile_flags",
"-D__DATE__=\"redacted\"", enabled = True,
"-D__TIMESTAMP__=\"redacted\"", flag_sets = [
"-D__TIME__=\"redacted\"", flag_set(
] + ctx.attr.copts, 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, actions = all_link_actions,
flag_groups = ([ flag_groups = ([
flag_group( flag_group(
flags = [ flags = ["-target", ctx.attr.target] +
"-target", no_gc_sections +
ctx.attr.target, ctx.attr.linkopts,
] + ctx.attr.linkopts,
), ),
]), ]),
), ),
@ -74,7 +91,8 @@ def _zig_cc_toolchain_config_impl(ctx):
) )
features = [ features = [
default_compiler_flags, compile_and_link_flags,
rest_compile_flags,
default_linker_flags, default_linker_flags,
] ]