Skip to content

Commit

Permalink
support extra flags
Browse files Browse the repository at this point in the history
If you just want to add a single flag (that is per platform) and leave the rest of the default
flags, you have to copy out all the existing flags and add it to what
you have. Instead support just adding an extra flag that is appended to
the default flags.
  • Loading branch information
rockwotj committed Feb 5, 2025
1 parent 69d3996 commit e2a23ab
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
23 changes: 23 additions & 0 deletions toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,29 @@ def cc_toolchain_config(
if compiler_configuration["unfiltered_compile_flags"] != None:
unfiltered_compile_flags = _fmt_flags(compiler_configuration["unfiltered_compile_flags"], toolchain_path_prefix)

if compiler_configuration["extra_compile_flags"] != None:
compile_flags.extend(_fmt_flags(compiler_configuration["extra_compile_flags"], toolchain_path_prefix))
if compiler_configuration["extra_cxx_flags"] != None:
cxx_flags.extend(_fmt_flags(compiler_configuration["extra_cxx_flags"], toolchain_path_prefix))
if compiler_configuration["extra_link_flags"] != None:
link_flags.extend(_fmt_flags(compiler_configuration["extra_link_flags"], toolchain_path_prefix))
if compiler_configuration["extra_archive_flags"] != None:
archive_flags.extend(_fmt_flags(compiler_configuration["extra_archive_flags"], toolchain_path_prefix))
if compiler_configuration["extra_link_libs"] != None:
link_libs.extend(_fmt_flags(compiler_configuration["extra_link_libs"], toolchain_path_prefix))
if compiler_configuration["extra_opt_compile_flags"] != None:
opt_compile_flags.extend(_fmt_flags(compiler_configuration["extra_opt_compile_flags"], toolchain_path_prefix))
if compiler_configuration["extra_opt_link_flags"] != None:
opt_link_flags.extend(_fmt_flags(compiler_configuration["extra_opt_link_flags"], toolchain_path_prefix))
if compiler_configuration["extra_dbg_compile_flags"] != None:
dbg_compile_flags.extend(_fmt_flags(compiler_configuration["extra_dbg_compile_flags"], toolchain_path_prefix))
if compiler_configuration["extra_coverage_compile_flags"] != None:
coverage_compile_flags.extend(_fmt_flags(compiler_configuration["extra_coverage_compile_flags"], toolchain_path_prefix))
if compiler_configuration["extra_coverage_link_flags"] != None:
coverage_link_flags.extend(_fmt_flags(compiler_configuration["extra_coverage_link_flags"], toolchain_path_prefix))
if compiler_configuration["extra_unfiltered_compile_flags"] != None:
unfiltered_compile_flags.extend(_fmt_flags(compiler_configuration["extra_unfiltered_compile_flags"], toolchain_path_prefix))

# Source: https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/unix_cc_toolchain_config.bzl
unix_cc_toolchain_config(
name = name,
Expand Down
33 changes: 33 additions & 0 deletions toolchain/internal/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ def llvm_config_impl(rctx):
extra_compiler_files = rctx.attr.extra_compiler_files,
extra_exec_compatible_with = rctx.attr.extra_exec_compatible_with,
extra_target_compatible_with = rctx.attr.extra_target_compatible_with,
extra_compile_flags_dict = rctx.attr.extra_compile_flags,
extra_cxx_flags_dict = rctx.attr.extra_cxx_flags,
extra_link_flags_dict = rctx.attr.extra_link_flags,
extra_archive_flags_dict = rctx.attr.extra_archive_flags,
extra_link_libs_dict = rctx.attr.extra_link_libs,
extra_opt_compile_flags_dict = rctx.attr.extra_opt_compile_flags,
extra_opt_link_flags_dict = rctx.attr.extra_opt_link_flags,
extra_dbg_compile_flags_dict = rctx.attr.extra_dbg_compile_flags,
extra_coverage_compile_flags_dict = rctx.attr.extra_coverage_compile_flags,
extra_coverage_link_flags_dict = rctx.attr.extra_coverage_link_flags,
extra_unfiltered_compile_flags_dict = rctx.attr.extra_unfiltered_compile_flags,
)
exec_dl_ext = "dylib" if os == "darwin" else "so"
cc_toolchains_str, toolchain_labels_str = _cc_toolchains_str(
Expand Down Expand Up @@ -389,6 +400,17 @@ cc_toolchain_config(
"coverage_compile_flags": {coverage_compile_flags},
"coverage_link_flags": {coverage_link_flags},
"unfiltered_compile_flags": {unfiltered_compile_flags},
"extra_compile_flags": {extra_compile_flags},
"extra_cxx_flags": {extra_cxx_flags},
"extra_link_flags": {extra_link_flags},
"extra_archive_flags": {extra_archive_flags},
"extra_link_libs": {extra_link_libs},
"extra_opt_compile_flags": {extra_opt_compile_flags},
"extra_opt_link_flags": {extra_opt_link_flags},
"extra_dbg_compile_flags": {extra_dbg_compile_flags},
"extra_coverage_compile_flags": {extra_coverage_compile_flags},
"extra_coverage_link_flags": {extra_coverage_link_flags},
"extra_unfiltered_compile_flags": {extra_unfiltered_compile_flags},
}},
cxx_builtin_include_directories = {cxx_builtin_include_directories},
major_llvm_version = {major_llvm_version},
Expand Down Expand Up @@ -562,6 +584,17 @@ cc_toolchain(
coverage_compile_flags = _list_to_string(_dict_value(toolchain_info.coverage_compile_flags_dict, target_pair)),
coverage_link_flags = _list_to_string(_dict_value(toolchain_info.coverage_link_flags_dict, target_pair)),
unfiltered_compile_flags = _list_to_string(_dict_value(toolchain_info.unfiltered_compile_flags_dict, target_pair)),
extra_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_compile_flags_dict, target_pair)),
extra_cxx_flags = _list_to_string(_dict_value(toolchain_info.extra_cxx_flags_dict, target_pair)),
extra_link_flags = _list_to_string(_dict_value(toolchain_info.extra_link_flags_dict, target_pair)),
extra_archive_flags = _list_to_string(_dict_value(toolchain_info.extra_archive_flags_dict, target_pair)),
extra_link_libs = _list_to_string(_dict_value(toolchain_info.extra_link_libs_dict, target_pair)),
extra_opt_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_opt_compile_flags_dict, target_pair)),
extra_opt_link_flags = _list_to_string(_dict_value(toolchain_info.extra_opt_link_flags_dict, target_pair)),
extra_dbg_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_dbg_compile_flags_dict, target_pair)),
extra_coverage_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_coverage_compile_flags_dict, target_pair)),
extra_coverage_link_flags = _list_to_string(_dict_value(toolchain_info.extra_coverage_link_flags_dict, target_pair)),
extra_unfiltered_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_unfiltered_compile_flags_dict, target_pair)),
extra_files_str = extra_files_str,
cxx_builtin_include_directories = _list_to_string(filtered_cxx_builtin_include_directories),
extra_compiler_files = ("\"%s\"," % str(toolchain_info.extra_compiler_files)) if toolchain_info.extra_compiler_files else "",
Expand Down
89 changes: 89 additions & 0 deletions toolchain/internal/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,95 @@ _compiler_configuration_attrs = {
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
# Same as the above flags, but instead of overriding the defaults, it just adds extras
"extra_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra compile_flags, added after default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_cxx_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra cxx_flags, added after default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra link_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_archive_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra archive_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_link_libs": attr.string_list_dict(
mandatory = False,
doc = ("Extra for link_libs, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_opt_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra opt_compile_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_opt_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra opt_link_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_dbg_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra dbg_compile_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_coverage_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra coverage_compile_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_coverage_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra coverage_link_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"extra_unfiltered_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Extra unfiltered_compile_flags, added after the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"target_settings": attr.string_list_dict(
mandatory = False,
doc = ("Override the toolchain's `target_settings` attribute."),
Expand Down

0 comments on commit e2a23ab

Please sign in to comment.