From aa19a5c37a59b1f701638a43f532d7fd378fae03 Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Tue, 15 Jul 2025 23:15:10 -0700 Subject: [PATCH 1/3] add additional toolchain options `cxxopts`: flags used only for compiling C++ targets `conlyopts`: flags used only for compiling C targets `dbg_compile_flags`: flags used when compiling in `dbg` mode `opt_compile_flags`: flags used when compiling in `opt` mode `opt_link_flags`: flags used when linking in `opt` mode --- toolchain/config.bzl | 134 +++++++++++++++++++++++----- toolchain/templates/toolchain.bazel | 10 +++ 2 files changed, 124 insertions(+), 20 deletions(-) diff --git a/toolchain/config.bzl b/toolchain/config.bzl index cfd350e..81f746a 100644 --- a/toolchain/config.bzl +++ b/toolchain/config.bzl @@ -60,6 +60,28 @@ def _additional_link_library_flags(ctx): return flags +def _flag_groups_if_not_empty(flags, actual_flags = None): + if not flags: + return [] + return [ + flag_group( + flags = actual_flags or flags, + ), + ] + +_all_compile_actions =[ + ACTION_NAMES.assemble, + ACTION_NAMES.preprocess_assemble, + ACTION_NAMES.linkstamp_compile, + ACTION_NAMES.c_compile, + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.lto_backend, + ACTION_NAMES.clif_match, +] + def _impl(ctx): default_compiler_flags = _default_compiler_flags(ctx) default_linker_flags = _default_linker_flags(ctx) @@ -100,27 +122,82 @@ def _impl(ctx): "strip", ) + dbg_feature = feature( + name = "dbg", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = _flag_groups_if_not_empty(ctx.attr.dbg_compile_flags), + ), + ], + provides = ["compilation_mode"], + ) + + opt_feature = feature( + name = "opt", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = _flag_groups_if_not_empty(ctx.attr.opt_compile_flags), + ), + flag_set( + actions = [ACTION_NAMES.cpp_link_executable], + flag_groups = _flag_groups_if_not_empty(ctx.attr.opt_link_flags), + ), + ], + provides = ["compilation_mode"], + ) + + fastbuild_feature = feature( + name = "fastbuild", + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [], + ), + ], + provides = ["compilation_mode"], + ) + toolchain_compiler_flags = feature( name = "compiler_flags", enabled = True, + flag_sets = [ + flag_set( + actions = _all_compile_actions, + flag_groups = [ + flag_group(flags = ["-isystem " + include.path for include in ctx.files.include_path]), + flag_group(flags = ctx.attr.copts + default_compiler_flags), + ], + ), + ], + ) + + cxx_flags = feature( + name = "cxx_flags", + enabled = True, flag_sets = [ flag_set( actions = [ - ACTION_NAMES.assemble, - ACTION_NAMES.preprocess_assemble, - ACTION_NAMES.linkstamp_compile, - ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, - ACTION_NAMES.lto_backend, - ACTION_NAMES.clif_match, ], - flag_groups = [ - flag_group(flags = ["-isystem" + include.path for include in ctx.files.include_path]), - flag_group(flags = ctx.attr.copts + default_compiler_flags), + flag_groups = _flag_groups_if_not_empty(ctx.attr.cxxopts), + ), + ], + ) + + conly_flags = feature( + name = "conly_flags", + enabled = True, + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.c_compile, ], + flag_groups = _flag_groups_if_not_empty(ctx.attr.conlyopts), ), ], ) @@ -141,30 +218,29 @@ def _impl(ctx): ], ) - additional_link_libraries = feature( - name = "additional_link_libraries", + custom_linkopts = feature( + name = "custom_linkopts", enabled = True, flag_sets = [ flag_set( actions = [ACTION_NAMES.cpp_link_executable], flag_groups = [ - flag_group( - flags = _additional_link_library_flags(ctx), - ), - ] if ctx.attr.additional_link_libraries else [], + flag_group(flags = ctx.attr.linkopts + default_linker_flags), + ], ), ], ) - custom_linkopts = feature( - name = "custom_linkopts", + additional_link_libraries = feature( + name = "additional_link_libraries", enabled = True, flag_sets = [ flag_set( actions = [ACTION_NAMES.cpp_link_executable], - flag_groups = [ - flag_group(flags = ctx.attr.linkopts + default_linker_flags), - ], + flag_groups = _flag_groups_if_not_empty( + ctx.attr.additional_link_libraries, + _additional_link_library_flags(ctx), + ), ), ], ) @@ -204,8 +280,13 @@ def _impl(ctx): for include in ctx.files.include_path ], features = [ + dbg_feature, + opt_feature, + fastbuild_feature, generate_linkmap_feature, toolchain_compiler_flags, + cxx_flags, + conly_flags, toolchain_linker_flags, additional_link_libraries, custom_linkopts, @@ -224,10 +305,23 @@ cc_arm_gnu_toolchain_config = rule( "gcc_tool": attr.string(default = "gcc"), "abi_version": attr.string(default = ""), "copts": attr.string_list(default = []), + "cxxopts": attr.string_list(default = []), + "conlyopts": attr.string_list(default = []), "linkopts": attr.string_list(default = []), "include_path": attr.label_list(default = [], allow_files = True), "library_path": attr.label_list(default = [], allow_files = True), "include_std": attr.bool(default = False), + "dbg_compile_flags": attr.string_list(default = [ + "-ggdb", + "-Og", + ]), + "opt_compile_flags": attr.string_list(default = [ + "-DNDEBUG", + "-O2", + ]), + "opt_link_flags": attr.string_list(default = [ + "-Wl,-S", + ]), "additional_link_libraries": attr.label_list( providers = [CcInfo], default = [], diff --git a/toolchain/templates/toolchain.bazel b/toolchain/templates/toolchain.bazel index 1473f76..c205532 100644 --- a/toolchain/templates/toolchain.bazel +++ b/toolchain/templates/toolchain.bazel @@ -98,7 +98,12 @@ def %toolchain_name%_toolchain( gcc_tool = "gcc", abi_version = "", copts = [], + cxxopts = [], + conlyopts = [], linkopts = [], + dbg_compile_flags = [], + opt_compile_flags = [], + opt_link_flags = [], target_compatible_with = [], additional_link_libraries = [], include_std = False, @@ -162,8 +167,13 @@ def %toolchain_name%_toolchain( include_path = ["{}:include_path".format(alias_repo)], library_path = ["{}:library_path".format(alias_repo)], copts = copts + fix_copts, + cxxopts = cxxopts, + conlyopts = conlyopts, linkopts = linkopts + fix_linkopts, include_std = include_std, + dbg_compile_flags = dbg_compile_flags, + opt_compile_flags = opt_compile_flags, + opt_link_flags = opt_link_flags, additional_link_libraries = variant.additional_link_libraries, tags = ["manual"], ) From 6f240bf8c6f19531ba5de41b79d25542aebc3a40 Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Tue, 15 Jul 2025 23:26:55 -0700 Subject: [PATCH 2/3] include std by default --- MODULE.bazel.lock | 2 +- toolchain/config.bzl | 2 +- toolchain/templates/toolchain.bazel | 2 +- toolchain/toolchain.bzl | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 0385efc..17dc2ba 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -139,7 +139,7 @@ "moduleExtensions": { "//:extensions.bzl%arm_toolchain": { "general": { - "bzlTransitiveDigest": "cqGNMbHOpcWofrKzWJEvft+a7/gOVZb5B5O+Nc0sihQ=", + "bzlTransitiveDigest": "u/MHEDqrY6t5zvDArzdCUgXwMLBgC0k67ZL2eV4eOtY=", "usagesDigest": "P/3UnhHe8mBjAHhQOgY8IbRzO7gyGHPkTqDB/W/U9qY=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, diff --git a/toolchain/config.bzl b/toolchain/config.bzl index 81f746a..52e42a2 100644 --- a/toolchain/config.bzl +++ b/toolchain/config.bzl @@ -310,7 +310,7 @@ cc_arm_gnu_toolchain_config = rule( "linkopts": attr.string_list(default = []), "include_path": attr.label_list(default = [], allow_files = True), "library_path": attr.label_list(default = [], allow_files = True), - "include_std": attr.bool(default = False), + "include_std": attr.bool(default = True), "dbg_compile_flags": attr.string_list(default = [ "-ggdb", "-Og", diff --git a/toolchain/templates/toolchain.bazel b/toolchain/templates/toolchain.bazel index c205532..8f3000c 100644 --- a/toolchain/templates/toolchain.bazel +++ b/toolchain/templates/toolchain.bazel @@ -106,7 +106,7 @@ def %toolchain_name%_toolchain( opt_link_flags = [], target_compatible_with = [], additional_link_libraries = [], - include_std = False, + include_std = True, visibility = None, ): """ diff --git a/toolchain/toolchain.bzl b/toolchain/toolchain.bzl index 76e6ac1..7c8655b 100644 --- a/toolchain/toolchain.bzl +++ b/toolchain/toolchain.bzl @@ -80,7 +80,7 @@ def _arm_gnu_toolchain( copts = [], linkopts = [], version = "", - include_std = False): + include_std = True): for host, exec_compatible_with in hosts[toolchain_prefix].items(): fix_linkopts = [] From 5012bfb601600cfbae5322cbc2acfbed41c52e0f Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Wed, 16 Jul 2025 23:35:47 -0700 Subject: [PATCH 3/3] use `None` to propagate default values --- toolchain/templates/toolchain.bazel | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/toolchain/templates/toolchain.bazel b/toolchain/templates/toolchain.bazel index 8f3000c..ff50981 100644 --- a/toolchain/templates/toolchain.bazel +++ b/toolchain/templates/toolchain.bazel @@ -101,9 +101,9 @@ def %toolchain_name%_toolchain( cxxopts = [], conlyopts = [], linkopts = [], - dbg_compile_flags = [], - opt_compile_flags = [], - opt_link_flags = [], + dbg_compile_flags = None, + opt_compile_flags = None, + opt_link_flags = None, target_compatible_with = [], additional_link_libraries = [], include_std = True,