Skip to content

Commit 1d0c58f

Browse files
zifeitongrockwotj
andauthored
Support extra toolchain flags (bazel-contrib#554)
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. This is a resubmit of bazel-contrib#452 by @rockwotj with minor fixes in documentation. --------- Co-authored-by: Tyler Rockwood <rockwood@redpanda.com>
1 parent fcf133b commit 1d0c58f

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

toolchain/cc_toolchain_config.bzl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,29 @@ def cc_toolchain_config(
412412
if compiler_configuration["unfiltered_compile_flags"] != None:
413413
unfiltered_compile_flags = _fmt_flags(compiler_configuration["unfiltered_compile_flags"], toolchain_path_prefix)
414414

415+
if compiler_configuration["extra_compile_flags"] != None:
416+
compile_flags.extend(_fmt_flags(compiler_configuration["extra_compile_flags"], toolchain_path_prefix))
417+
if compiler_configuration["extra_cxx_flags"] != None:
418+
cxx_flags.extend(_fmt_flags(compiler_configuration["extra_cxx_flags"], toolchain_path_prefix))
419+
if compiler_configuration["extra_link_flags"] != None:
420+
link_flags.extend(_fmt_flags(compiler_configuration["extra_link_flags"], toolchain_path_prefix))
421+
if compiler_configuration["extra_archive_flags"] != None:
422+
archive_flags.extend(_fmt_flags(compiler_configuration["extra_archive_flags"], toolchain_path_prefix))
423+
if compiler_configuration["extra_link_libs"] != None:
424+
link_libs.extend(_fmt_flags(compiler_configuration["extra_link_libs"], toolchain_path_prefix))
425+
if compiler_configuration["extra_opt_compile_flags"] != None:
426+
opt_compile_flags.extend(_fmt_flags(compiler_configuration["extra_opt_compile_flags"], toolchain_path_prefix))
427+
if compiler_configuration["extra_opt_link_flags"] != None:
428+
opt_link_flags.extend(_fmt_flags(compiler_configuration["extra_opt_link_flags"], toolchain_path_prefix))
429+
if compiler_configuration["extra_dbg_compile_flags"] != None:
430+
dbg_compile_flags.extend(_fmt_flags(compiler_configuration["extra_dbg_compile_flags"], toolchain_path_prefix))
431+
if compiler_configuration["extra_coverage_compile_flags"] != None:
432+
coverage_compile_flags.extend(_fmt_flags(compiler_configuration["extra_coverage_compile_flags"], toolchain_path_prefix))
433+
if compiler_configuration["extra_coverage_link_flags"] != None:
434+
coverage_link_flags.extend(_fmt_flags(compiler_configuration["extra_coverage_link_flags"], toolchain_path_prefix))
435+
if compiler_configuration["extra_unfiltered_compile_flags"] != None:
436+
unfiltered_compile_flags.extend(_fmt_flags(compiler_configuration["extra_unfiltered_compile_flags"], toolchain_path_prefix))
437+
415438
# Source: https://cs.opensource.google/bazel/bazel/+/master:tools/cpp/unix_cc_toolchain_config.bzl
416439
unix_cc_toolchain_config(
417440
name = name,

toolchain/internal/configure.bzl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ def llvm_config_impl(rctx):
176176
extra_compiler_files = rctx.attr.extra_compiler_files,
177177
extra_exec_compatible_with = rctx.attr.extra_exec_compatible_with,
178178
extra_target_compatible_with = rctx.attr.extra_target_compatible_with,
179+
extra_compile_flags_dict = rctx.attr.extra_compile_flags,
180+
extra_cxx_flags_dict = rctx.attr.extra_cxx_flags,
181+
extra_link_flags_dict = rctx.attr.extra_link_flags,
182+
extra_archive_flags_dict = rctx.attr.extra_archive_flags,
183+
extra_link_libs_dict = rctx.attr.extra_link_libs,
184+
extra_opt_compile_flags_dict = rctx.attr.extra_opt_compile_flags,
185+
extra_opt_link_flags_dict = rctx.attr.extra_opt_link_flags,
186+
extra_dbg_compile_flags_dict = rctx.attr.extra_dbg_compile_flags,
187+
extra_coverage_compile_flags_dict = rctx.attr.extra_coverage_compile_flags,
188+
extra_coverage_link_flags_dict = rctx.attr.extra_coverage_link_flags,
189+
extra_unfiltered_compile_flags_dict = rctx.attr.extra_unfiltered_compile_flags,
179190
)
180191
exec_dl_ext = "dylib" if os == "darwin" else "so"
181192
cc_toolchains_str, toolchain_labels_str = _cc_toolchains_str(
@@ -404,6 +415,17 @@ cc_toolchain_config(
404415
"coverage_compile_flags": {coverage_compile_flags},
405416
"coverage_link_flags": {coverage_link_flags},
406417
"unfiltered_compile_flags": {unfiltered_compile_flags},
418+
"extra_compile_flags": {extra_compile_flags},
419+
"extra_cxx_flags": {extra_cxx_flags},
420+
"extra_link_flags": {extra_link_flags},
421+
"extra_archive_flags": {extra_archive_flags},
422+
"extra_link_libs": {extra_link_libs},
423+
"extra_opt_compile_flags": {extra_opt_compile_flags},
424+
"extra_opt_link_flags": {extra_opt_link_flags},
425+
"extra_dbg_compile_flags": {extra_dbg_compile_flags},
426+
"extra_coverage_compile_flags": {extra_coverage_compile_flags},
427+
"extra_coverage_link_flags": {extra_coverage_link_flags},
428+
"extra_unfiltered_compile_flags": {extra_unfiltered_compile_flags},
407429
}},
408430
cxx_builtin_include_directories = {cxx_builtin_include_directories},
409431
major_llvm_version = {major_llvm_version},
@@ -579,6 +601,17 @@ cc_toolchain(
579601
coverage_compile_flags = _list_to_string(_dict_value(toolchain_info.coverage_compile_flags_dict, target_pair)),
580602
coverage_link_flags = _list_to_string(_dict_value(toolchain_info.coverage_link_flags_dict, target_pair)),
581603
unfiltered_compile_flags = _list_to_string(_dict_value(toolchain_info.unfiltered_compile_flags_dict, target_pair)),
604+
extra_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_compile_flags_dict, target_pair)),
605+
extra_cxx_flags = _list_to_string(_dict_value(toolchain_info.extra_cxx_flags_dict, target_pair)),
606+
extra_link_flags = _list_to_string(_dict_value(toolchain_info.extra_link_flags_dict, target_pair)),
607+
extra_archive_flags = _list_to_string(_dict_value(toolchain_info.extra_archive_flags_dict, target_pair)),
608+
extra_link_libs = _list_to_string(_dict_value(toolchain_info.extra_link_libs_dict, target_pair)),
609+
extra_opt_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_opt_compile_flags_dict, target_pair)),
610+
extra_opt_link_flags = _list_to_string(_dict_value(toolchain_info.extra_opt_link_flags_dict, target_pair)),
611+
extra_dbg_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_dbg_compile_flags_dict, target_pair)),
612+
extra_coverage_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_coverage_compile_flags_dict, target_pair)),
613+
extra_coverage_link_flags = _list_to_string(_dict_value(toolchain_info.extra_coverage_link_flags_dict, target_pair)),
614+
extra_unfiltered_compile_flags = _list_to_string(_dict_value(toolchain_info.extra_unfiltered_compile_flags_dict, target_pair)),
582615
extra_files_str = extra_files_str,
583616
cxx_builtin_include_directories = _list_to_string(filtered_cxx_builtin_include_directories),
584617
extra_compiler_files = ("\"%s\"," % str(toolchain_info.extra_compiler_files)) if toolchain_info.extra_compiler_files else "",

toolchain/internal/repo.bzl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,95 @@ _compiler_configuration_attrs = {
265265
"target OS and arch pair you want to override " +
266266
"({}); empty key overrides all.".format(_target_pairs)),
267267
),
268+
# Same as the above flags, but instead of overriding the defaults, it just adds extras
269+
"extra_compile_flags": attr.string_list_dict(
270+
mandatory = False,
271+
doc = ("Extra compile_flags, added after default values. " +
272+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
273+
"to the root LLVM distribution directory. Provide one list for each " +
274+
"target OS and arch pair you want to add " +
275+
"({}); an empty key adds all.".format(_target_pairs)),
276+
),
277+
"extra_cxx_flags": attr.string_list_dict(
278+
mandatory = False,
279+
doc = ("Extra cxx_flags, added after default values. " +
280+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
281+
"to the root LLVM distribution directory. Provide one list for each " +
282+
"target OS and arch pair you want to add " +
283+
"({}); an empty key adds all.".format(_target_pairs)),
284+
),
285+
"extra_link_flags": attr.string_list_dict(
286+
mandatory = False,
287+
doc = ("Extra link_flags, added after the default values. " +
288+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
289+
"to the root LLVM distribution directory. Provide one list for each " +
290+
"target OS and arch pair you want to add " +
291+
"({}); an empty key adds all.".format(_target_pairs)),
292+
),
293+
"extra_archive_flags": attr.string_list_dict(
294+
mandatory = False,
295+
doc = ("Extra archive_flags, added after the default values. " +
296+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
297+
"to the root LLVM distribution directory. Provide one list for each " +
298+
"target OS and arch pair you want to add " +
299+
"({}); an empty key adds all.".format(_target_pairs)),
300+
),
301+
"extra_link_libs": attr.string_list_dict(
302+
mandatory = False,
303+
doc = ("Extra for link_libs, added after the default values. " +
304+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
305+
"to the root LLVM distribution directory. Provide one list for each " +
306+
"target OS and arch pair you want to add " +
307+
"({}); an empty key adds all.".format(_target_pairs)),
308+
),
309+
"extra_opt_compile_flags": attr.string_list_dict(
310+
mandatory = False,
311+
doc = ("Extra opt_compile_flags, added after the default values. " +
312+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
313+
"to the root LLVM distribution directory. Provide one list for each " +
314+
"target OS and arch pair you want to add " +
315+
"({}); an empty key adds all.".format(_target_pairs)),
316+
),
317+
"extra_opt_link_flags": attr.string_list_dict(
318+
mandatory = False,
319+
doc = ("Extra opt_link_flags, added after the default values. " +
320+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
321+
"to the root LLVM distribution directory. Provide one list for each " +
322+
"target OS and arch pair you want to add " +
323+
"({}); an empty key adds all.".format(_target_pairs)),
324+
),
325+
"extra_dbg_compile_flags": attr.string_list_dict(
326+
mandatory = False,
327+
doc = ("Extra dbg_compile_flags, added after the default values. " +
328+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
329+
"to the root LLVM distribution directory. Provide one list for each " +
330+
"target OS and arch pair you want to add " +
331+
"({}); an empty key adds all.".format(_target_pairs)),
332+
),
333+
"extra_coverage_compile_flags": attr.string_list_dict(
334+
mandatory = False,
335+
doc = ("Extra coverage_compile_flags, added after the default values. " +
336+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
337+
"to the root LLVM distribution directory. Provide one list for each " +
338+
"target OS and arch pair you want to add " +
339+
"({}); an empty key adds all.".format(_target_pairs)),
340+
),
341+
"extra_coverage_link_flags": attr.string_list_dict(
342+
mandatory = False,
343+
doc = ("Extra coverage_link_flags, added after the default values. " +
344+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
345+
"to the root LLVM distribution directory. Provide one list for each " +
346+
"target OS and arch pair you want to add " +
347+
"({}); an empty key adds all.".format(_target_pairs)),
348+
),
349+
"extra_unfiltered_compile_flags": attr.string_list_dict(
350+
mandatory = False,
351+
doc = ("Extra unfiltered_compile_flags, added after the default values. " +
352+
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
353+
"to the root LLVM distribution directory. Provide one list for each " +
354+
"target OS and arch pair you want to add " +
355+
"({}); an empty key adds all.".format(_target_pairs)),
356+
),
268357
"target_settings": attr.string_list_dict(
269358
mandatory = False,
270359
doc = ("Override the toolchain's `target_settings` attribute."),

0 commit comments

Comments
 (0)