1
Fork 0
hermetic_cc_toolchain/toolchain/zig_toolchain.bzl

213 lines
5.8 KiB
Python
Raw Normal View History

2021-04-10 01:05:01 +03:00
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
2021-06-10 13:12:54 +03:00
load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"artifact_name_pattern",
2021-04-10 01:05:01 +03:00
"feature",
"feature_set",
2021-04-10 01:05:01 +03:00
"flag_group",
"flag_set",
"tool",
2021-06-10 13:12:54 +03:00
"tool_path",
2021-04-10 01:05:01 +03:00
)
all_link_actions = [
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
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 = [
2021-04-10 01:05:01 +03:00
ACTION_NAMES.c_compile,
ACTION_NAMES.cpp_compile,
]
rest_compile_actions = [
ACTION_NAMES.assemble,
2021-04-10 01:05:01 +03:00
ACTION_NAMES.cc_flags_make_variable,
ACTION_NAMES.clif_match,
ACTION_NAMES.cpp_header_parsing,
ACTION_NAMES.cpp_module_codegen,
ACTION_NAMES.cpp_module_compile,
ACTION_NAMES.linkstamp_compile,
ACTION_NAMES.lto_backend,
ACTION_NAMES.preprocess_assemble,
]
def _compilation_mode_features(ctx):
actions = all_link_actions + compile_and_link_actions + rest_compile_actions
dbg_feature = feature(
name = "dbg",
flag_sets = [
flag_set(
actions = actions,
flag_groups = [
flag_group(
flags = ["-g"],
),
],
),
],
)
opt_feature = feature(
name = "opt",
flag_sets = [
flag_set(
actions = actions,
flag_groups = [
flag_group(
flags = ["-O2", "-DNDEBUG"],
),
],
),
],
)
# fastbuild also gets the strip_debug_symbols flags by default.
fastbuild_feature = feature(
name = "fastbuild",
flag_sets = [
flag_set(
actions = actions,
flag_groups = [
flag_group(
flags = ["-fno-lto"],
),
],
),
],
)
return [
dbg_feature,
opt_feature,
fastbuild_feature,
]
2021-04-10 01:05:01 +03:00
def _zig_cc_toolchain_config_impl(ctx):
compiler_flags = [
"-I" + d
for d in ctx.attr.cxx_builtin_include_directories
] + [
"-no-canonical-prefixes",
"-Wno-builtin-macro-redefined",
"-D__DATE__=\"redacted\"",
"-D__TIMESTAMP__=\"redacted\"",
"-D__TIME__=\"redacted\"",
]
compile_and_link_flags = feature(
name = "compile_and_link_flags",
2021-04-10 01:05:01 +03:00
enabled = True,
flag_sets = [
flag_set(
actions = compile_and_link_actions,
flag_groups = [
flag_group(flags = compiler_flags + ctx.attr.copts),
2021-04-10 01:05:01 +03:00
],
),
],
)
2021-06-10 13:12:54 +03:00
link_flag_sets = []
if ctx.attr.linkopts:
link_flag_sets.append(
flag_set(
actions = all_link_actions,
flag_groups = [flag_group(flags = ctx.attr.linkopts)],
2023-04-26 06:28:31 +03:00
),
)
if ctx.attr.dynamic_library_linkopts:
link_flag_sets.append(
flag_set(
actions = dynamic_library_link_actions,
flag_groups = [flag_group(flags = ctx.attr.dynamic_library_linkopts)],
2023-04-26 06:28:31 +03:00
),
)
2021-04-10 01:05:01 +03:00
default_linker_flags = feature(
name = "default_linker_flags",
enabled = True,
flag_sets = link_flag_sets,
2021-04-10 01:05:01 +03:00
)
supports_dynamic_linker = feature(
name = "supports_dynamic_linker",
enabled = True,
)
strip_debug_symbols_feature = feature(
name = "strip_debug_symbols",
flag_sets = [
flag_set(
actions = all_link_actions,
flag_groups = [
flag_group(
flags = ["-Wl,-S"],
expand_if_available = "strip_debug_symbols",
),
],
),
],
)
2021-04-10 01:05:01 +03:00
features = [
compile_and_link_flags,
2021-04-10 01:05:01 +03:00
default_linker_flags,
supports_dynamic_linker,
strip_debug_symbols_feature,
] + _compilation_mode_features(ctx)
2021-04-10 01:05:01 +03:00
artifact_name_patterns = [
artifact_name_pattern(**json.decode(p))
for p in ctx.attr.artifact_name_patterns
]
2021-04-10 01:05:01 +03:00
return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
toolchain_identifier = "%s-toolchain" % ctx.attr.target,
host_system_name = "local",
target_system_name = ctx.attr.target_system_name,
target_cpu = ctx.attr.target_cpu,
target_libc = ctx.attr.target_libc,
compiler = ctx.attr.compiler,
abi_version = ctx.attr.abi_version,
abi_libc_version = ctx.attr.abi_libc_version,
tool_paths = [
2021-06-10 13:12:54 +03:00
tool_path(name = name, path = path)
2021-04-10 01:05:01 +03:00
for name, path in ctx.attr.tool_paths.items()
],
cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories,
artifact_name_patterns = artifact_name_patterns,
2021-04-10 01:05:01 +03:00
)
zig_cc_toolchain_config = rule(
implementation = _zig_cc_toolchain_config_impl,
attrs = {
"cxx_builtin_include_directories": attr.string_list(),
"linkopts": attr.string_list(),
"dynamic_library_linkopts": attr.string_list(),
2021-04-10 01:05:01 +03:00
"copts": attr.string_list(),
"tool_paths": attr.string_dict(),
"target": attr.string(),
"target_system_name": attr.string(),
"target_cpu": attr.string(),
"target_libc": attr.string(),
"target_suffix": attr.string(),
2021-04-10 01:05:01 +03:00
"compiler": attr.string(),
"abi_version": attr.string(),
"abi_libc_version": attr.string(),
"artifact_name_patterns": attr.string_list(),
2021-04-10 01:05:01 +03:00
},
provides = [CcToolchainConfigInfo],
)