From 13d1c7359f46d80d88fe9836956e4d5889b978e5 Mon Sep 17 00:00:00 2001 From: Armando Martins Date: Thu, 13 Aug 2026 09:14:11 +0100 Subject: [PATCH 1/3] Created abstraction layer on top of qnx/linux config Now the "cc_toolchain_config" is not duplicated in both qnx and linux, and instead there is an abstraction layer that handles the general config, and the specific ones are still inside linux/qnx folder When adding a new OS, for example, we just need to create the specific folder with the "cc_toolchain_config.bzl.template", the needed flags and the BUILD file. Base "cc_toolchain_config" is now ready to not need changes in there when updating qnx/linux toolchain, or creating a new one. --- extensions/gcc.bzl | 4 +- rules/gcc.bzl | 61 +++- templates/cc_toolchain_config.bzl.template | 205 ++++++++++++ .../linux/cc_toolchain_config.bzl.template | 292 +++++------------- .../qnx/cc_toolchain_config.bzl.template | 270 ++++------------ templates/shared/BUILD | 0 .../cc_toolchain_shared_features.bzl.template | 0 tests/MODULE.bazel.lock | 42 +-- 8 files changed, 414 insertions(+), 460 deletions(-) create mode 100644 templates/cc_toolchain_config.bzl.template create mode 100644 templates/shared/BUILD rename templates/{ => shared}/cc_toolchain_shared_features.bzl.template (100%) diff --git a/extensions/gcc.bzl b/extensions/gcc.bzl index d86ee8a..15e086e 100644 --- a/extensions/gcc.bzl +++ b/extensions/gcc.bzl @@ -203,9 +203,9 @@ def _get_toolchains(tags): toolchains = [] for tag in tags: toolchain = { - "cc_toolchain_config": "@score_bazel_cpp_toolchains//templates/{}:cc_toolchain_config.bzl.template".format(tag.target_os), + "cc_toolchain_config": "@score_bazel_cpp_toolchains//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@score_bazel_cpp_toolchains//templates/{}:cc_toolchain_flags.bzl.template".format(tag.target_os), - "cc_toolchain_shared_features": "@score_bazel_cpp_toolchains//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@score_bazel_cpp_toolchains//templates/shared:cc_toolchain_shared_features.bzl.template", "gcc_version": tag.version, "name": tag.name, "use_base_constraints_only": tag.use_base_constraints_only, diff --git a/rules/gcc.bzl b/rules/gcc.bzl index f128e12..6ebb814 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -241,7 +241,19 @@ def _impl(rctx): extra_link_flags = get_flag_groups(replace_placeholder(rctx.attr.extra_link_flags)) compiler_library_search_paths = replace_placeholder(rctx.attr.tc_compiler_library_search_paths) - template_dict = { + # Compute once so it is available for both config and qnx feature template dicts. + mapped_sdp_version = _apply_sdp_version_mapping(rctx.attr.sdp_version) + mapped_sdp_version_for_config = mapped_sdp_version if rctx.attr.tc_os == _OS_QNX else "" + + config_template_dict = { + "%{sdp_version}": mapped_sdp_version_for_config, + "%{tc_abi_version}": "{}-linux-gnu".format(_normalize_cpu(rctx.attr.tc_cpu)) if rctx.attr.tc_os == _OS_LINUX else "", + "%{tc_cpu}": _normalize_cpu(rctx.attr.tc_cpu), + "%{tc_identifier}": rctx.attr.tc_identifier if rctx.attr.tc_identifier else "gcc", + "%{tc_os}": rctx.attr.tc_os, + } + + linux_features_template_dict = { "%{compiler_library_search_paths_switch}": "True" if len(rctx.attr.tc_compiler_library_search_paths) else "False", "%{compiler_library_search_paths}": ":".join(["/proc/self/cwd/" + entry for entry in compiler_library_search_paths]), "%{extra_c_compile_flags_switch}": "True" if len(rctx.attr.extra_c_compile_flags) else "False", @@ -252,28 +264,38 @@ def _impl(rctx): "%{extra_cxx_compile_flags}": extra_cxx_compile_flags, "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", "%{extra_link_flags}": extra_link_flags, + } + + qnx_features_template_dict = { + "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", + "%{extra_compile_flags}": extra_compile_flags, + "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", + "%{extra_link_flags}": extra_link_flags, + "%{license_info_value}": rctx.attr.license_info_value, + "%{license_info_variable}": rctx.attr.license_info_variable, + "%{license_path}": rctx.attr.license_path, + "%{sdp_version}": mapped_sdp_version_for_config, "%{tc_cpu}": _normalize_cpu(rctx.attr.tc_cpu), - "%{tc_identifier}": "gcc", - "%{tc_runtime_es}": rctx.attr.tc_runtime_ecosystem, "%{tc_version}": rctx.attr.gcc_version, + "%{use_license_info}": "False" if rctx.attr.license_info_value == "" else "True", } - if rctx.attr.tc_os == _OS_QNX: - mapped_sdp_version = _apply_sdp_version_mapping(rctx.attr.sdp_version) - extra_template_dict = { - "%{license_info_value}": rctx.attr.license_info_value, - "%{license_info_variable}": rctx.attr.license_info_variable, - "%{license_path}": rctx.attr.license_path, - "%{sdp_version}": mapped_sdp_version, - "%{tc_cpu_cxx}": _normalize_cpu(rctx.attr.tc_cpu), - "%{use_license_info}": "False" if rctx.attr.license_info_value == "" else "True", - } - template_dict = dict_union(template_dict, extra_template_dict) - rctx.template( "cc_toolchain_config.bzl", rctx.attr.cc_toolchain_config, - template_dict, + config_template_dict, + ) + + rctx.template( + "cc_toolchain_linux_config.bzl", + rctx.attr._cc_toolchain_linux_config, + linux_features_template_dict, + ) + + rctx.template( + "cc_toolchain_qnx_config.bzl", + rctx.attr._cc_toolchain_qnx_config, + qnx_features_template_dict, ) rctx.template( @@ -304,7 +326,6 @@ def _impl(rctx): elif rctx.attr.tc_os == _OS_QNX: # Generate gcov wrapper for QNX toolchains to enable `bazel coverage`. # See: https://github.com/bazelbuild/rules_cc/issues/351 - mapped_sdp_version = _apply_sdp_version_mapping(rctx.attr.sdp_version) if rctx.attr.tc_cpu == _CPU_AARCH64: gcov_triple = _TRIPLE_AARCH64_QNX_FMT.format(sdp = mapped_sdp_version) else: @@ -355,6 +376,12 @@ gcc_toolchain = repository_rule( "_cc_gcov_wrapper_script": attr.label( default = "@score_bazel_cpp_toolchains//templates/linux:cc_gcov_wrapper.template", ), + "_cc_toolchain_linux_config": attr.label( + default = "@score_bazel_cpp_toolchains//templates/linux:cc_toolchain_config.bzl.template", + ), + "_cc_toolchain_qnx_config": attr.label( + default = "@score_bazel_cpp_toolchains//templates/qnx:cc_toolchain_config.bzl.template", + ), "_cc_toolchain_build": attr.label( default = "@score_bazel_cpp_toolchains//templates:BUILD.template", doc = "Path to the Bazel BUILD file template for the toolchain.", diff --git a/templates/cc_toolchain_config.bzl.template b/templates/cc_toolchain_config.bzl.template new file mode 100644 index 0000000..5e4b243 --- /dev/null +++ b/templates/cc_toolchain_config.bzl.template @@ -0,0 +1,205 @@ +# ******************************************************************************* +# Copyright (c) 2025 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +"""Unified GCC toolchain configuration — dispatches to the OS-specific feature module.""" + +load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", + "action_config", + "flag_group", + "flag_set", + "tool", + "tool_path", +) +load("@rules_cc//cc:defs.bzl", "CcToolchainConfigInfo") +load("@rules_cc//cc/toolchains:feature_injection.bzl", "FeatureInfo", "convert_feature") +load(":cc_toolchain_%{tc_os}_config.bzl", "EXTRA_ATTRS", "make_features", "make_toolchain_config_info") +load( + ":flags.bzl", + "ALL_WALL_C_WARNINGS", + "ALL_WALL_CXX_WARNINGS", + "ALL_WALL_WARNINGS", + "DBG_COMPILE_FLAGS", + "DEFAULT_C_COMPILE_FLAGS", + "DEFAULT_COMPILE_FLAGS", + "DEFAULT_CXX_COMPILE_FLAGS", + "DEFAULT_LINK_FLAGS", + "MINIMAL_C_WARNINGS_FLAGS", + "MINIMAL_CXX_WARNINGS_FLAGS", + "MINIMAL_WARNINGS_FLAGS", + "OPT_COMPILE_FLAGS", + "STRICT_C_WARNINGS_FLAGS", + "STRICT_CXX_WARNINGS_FLAGS", + "STRICT_WARNINGS_FLAGS", + "UNFILTERED_COMPILE_FLAGS", + "WARNINGS_AS_ERRORS", +) +load( + ":shared_features.bzl", + "all_actions", + "all_assemble_actions", + "all_c_compile_actions", + "all_compile_actions", + "all_cpp_compile_actions", + "all_link_actions", +) + +def _impl(ctx): + flags = struct( + unfiltered_compile_flags = UNFILTERED_COMPILE_FLAGS, + default_compile_flags = DEFAULT_COMPILE_FLAGS, + default_c_compile_flags = DEFAULT_C_COMPILE_FLAGS, + default_cxx_compile_flags = DEFAULT_CXX_COMPILE_FLAGS, + default_link_flags = DEFAULT_LINK_FLAGS, + dbg_compile_flags = DBG_COMPILE_FLAGS, + opt_compile_flags = OPT_COMPILE_FLAGS, + minimal_warnings_flags = MINIMAL_WARNINGS_FLAGS, + minimal_c_warnings_flags = MINIMAL_C_WARNINGS_FLAGS, + minimal_cxx_warnings_flags = MINIMAL_CXX_WARNINGS_FLAGS, + strict_warnings_flags = STRICT_WARNINGS_FLAGS, + strict_c_warnings_flags = STRICT_C_WARNINGS_FLAGS, + strict_cxx_warnings_flags = STRICT_CXX_WARNINGS_FLAGS, + all_wall_warnings = ALL_WALL_WARNINGS, + all_wall_c_warnings = ALL_WALL_C_WARNINGS, + all_wall_cxx_warnings = ALL_WALL_CXX_WARNINGS, + warnings_as_errors = WARNINGS_AS_ERRORS, + ) + + actions = struct( + all_actions = all_actions, + all_assemble_actions = all_assemble_actions, + all_c_compile_actions = all_c_compile_actions, + all_compile_actions = all_compile_actions, + all_cpp_compile_actions = all_cpp_compile_actions, + all_link_actions = all_link_actions, + ) + + assemble_action = action_config( + action_name = ACTION_NAMES.assemble, + tools = [tool(tool = ctx.executable.cc_binary)], + ) + preprocess_assemble_action = action_config( + action_name = ACTION_NAMES.preprocess_assemble, + tools = [tool(tool = ctx.executable.cc_binary)], + ) + c_compile_action = action_config( + action_name = ACTION_NAMES.c_compile, + tools = [tool(tool = ctx.executable.cc_binary)], + ) + cpp_compile_action = action_config( + action_name = ACTION_NAMES.cpp_compile, + tools = [tool(tool = ctx.executable.cxx_binary)], + ) + cpp_link_executable_action = action_config( + action_name = ACTION_NAMES.cpp_link_executable, + tools = [tool(tool = ctx.executable.cxx_binary)], + ) + cpp_link_dynamic_library_action = action_config( + action_name = ACTION_NAMES.cpp_link_dynamic_library, + tools = [tool(tool = ctx.executable.cxx_binary)], + ) + cpp_link_nodeps_dynamic_library_action = action_config( + action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, + tools = [tool(tool = ctx.executable.cxx_binary)], + ) + cpp_link_static_library_action = action_config( + action_name = ACTION_NAMES.cpp_link_static_library, + tools = [tool(tool = ctx.executable.ar_binary)], + implies = ["archiver_flags"], + ) + strip_action = action_config( + action_name = ACTION_NAMES.strip, + tools = [tool(tool = ctx.executable.strip_binary)], + flag_sets = [ + flag_set( + flag_groups = [ + flag_group(flags = ["-g", "-p"]), + flag_group( + iterate_over = "stripopts", + flags = ["%{stripopts}"], + ), + flag_group(flags = ["%{input_file}", "%{output_file}"]), + ], + ), + ], + ) + action_configs = [ + assemble_action, + c_compile_action, + cpp_compile_action, + cpp_link_dynamic_library_action, + cpp_link_executable_action, + cpp_link_nodeps_dynamic_library_action, + cpp_link_static_library_action, + preprocess_assemble_action, + strip_action, + ] + + features = make_features(ctx, flags, actions) + + extra_rules_based_features = depset(ctx.attr.extra_enabled_features + ctx.attr.extra_known_features) + features.extend([ + convert_feature(extra_feature[FeatureInfo], enabled = extra_feature in ctx.attr.extra_enabled_features) + for extra_feature in extra_rules_based_features.to_list() + ]) + + tool_paths = [tool_path(name = "gcov", path = "gcov_wrapper")] + + return make_toolchain_config_info( + ctx, + features, + action_configs, + tool_paths, + abi_version = "%{tc_abi_version}", + target_cpu = "%{tc_cpu}", + target_os = "%{tc_os}", + toolchain_identifier = "%{tc_identifier}", + ) + +_BASE_ATTRS = { + "ar_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), + "cc_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), + "cxx_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), + "gcov_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), + "strip_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), + "target_cpu": attr.string(mandatory = True), + "target_os": attr.string(mandatory = True), + "extra_enabled_features": attr.label_list( + providers = [FeatureInfo], + default = [], + doc = """ +Extra `cc_feature` features to add to this toolchain in an initially enabled state. +This attribute has limited integration with `cc_feature`, and does not run additional correctness checks or handle things like `data` files. +This is only offered as a migration bridge for projects transitioning to rule-based toolchain configurations, or sharing of simple argument sets with older toolchains. +""", + ), + "extra_known_features": attr.label_list( + providers = [FeatureInfo], + default = [], + doc = """ +Extra `cc_feature` features to add to this toolchain in an initially disabled state. +This attribute has limited integration with `cc_feature`, and does not run additional correctness checks or handle things like `data` files. +This is only offered as a migration bridge for projects transitioning to rule-based toolchain configurations, or sharing of simple argument sets with older toolchains. +""", + ), +} + +_RULE_ATTRS = dict(_BASE_ATTRS) +_RULE_ATTRS.update(EXTRA_ATTRS) + +cc_toolchain_config = rule( + implementation = _impl, + provides = [CcToolchainConfigInfo], + attrs = _RULE_ATTRS, +) diff --git a/templates/linux/cc_toolchain_config.bzl.template b/templates/linux/cc_toolchain_config.bzl.template index 8d28e81..5fde0c0 100644 --- a/templates/linux/cc_toolchain_config.bzl.template +++ b/templates/linux/cc_toolchain_config.bzl.template @@ -11,137 +11,36 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -"""C/C++ toolchain definition""" +"""Linux-specific GCC toolchain feature factory.""" load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") -load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", - "action_config", +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "env_entry", "env_set", "feature", "feature_set", "flag_group", "flag_set", - "tool", - "tool_path", - "variable_with_value", "with_feature_set", ) -load("@rules_cc//cc:defs.bzl", "cc_common", "CcToolchainConfigInfo") -load("@rules_cc//cc/toolchains:feature_injection.bzl", "convert_feature", "FeatureInfo") -load(":flags.bzl", - "UNFILTERED_COMPILE_FLAGS", - "DEFAULT_COMPILE_FLAGS", - "DEFAULT_x86_64_COMPILE_FLAGS", - "DEFAULT_ARM64_COMPILE_FLAGS", - "DEFAULT_C_COMPILE_FLAGS", - "DEFAULT_CXX_COMPILE_FLAGS", - "DEFAULT_LINK_FLAGS", - "DBG_COMPILE_FLAGS", - "OPT_COMPILE_FLAGS", - "MINIMAL_WARNINGS_FLAGS", - "MINIMAL_C_WARNINGS_FLAGS", - "MINIMAL_CXX_WARNINGS_FLAGS", - "STRICT_WARNINGS_FLAGS", - "STRICT_C_WARNINGS_FLAGS", - "STRICT_CXX_WARNINGS_FLAGS", - "ALL_WALL_WARNINGS", - "ALL_WALL_C_WARNINGS", - "ALL_WALL_CXX_WARNINGS", - "WARNINGS_AS_ERRORS", +load("@rules_cc//cc:defs.bzl", "cc_common") +load( + ":shared_features.bzl", + "make_shared_features" ) -load(":shared_features.bzl", - "all_actions", - "all_assemble_actions", - "all_c_compile_actions", - "all_compile_actions", - "all_cpp_compile_actions", - "all_link_actions", - "make_shared_features", -) +_DEFAULT_x86_64_COMPILE_FLAGS = [flag_group(flags = ["-m64"])] +_DEFAULT_ARM64_COMPILE_FLAGS = [] + +def make_features(ctx, flags, actions): -def _impl(ctx): + shared = make_shared_features(flags.unfiltered_compile_flags) - # Temp solution until we do not add feature injection gnu11_feature = feature(name = "gnu11") supports_fission_feature = feature(name = "supports_fission", enabled = True) - assemble_action = action_config( - action_name = ACTION_NAMES.assemble, - tools = [tool(tool = ctx.executable.cc_binary)], - ) - preprocess_assemble_action = action_config( - action_name = ACTION_NAMES.preprocess_assemble, - tools = [tool(tool = ctx.executable.cc_binary)], - ) - c_compile_action = action_config( - action_name = ACTION_NAMES.c_compile, - tools = [tool(tool = ctx.executable.cc_binary)], - ) - cpp_compile_action = action_config( - action_name = ACTION_NAMES.cpp_compile, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_executable_action = action_config( - action_name = ACTION_NAMES.cpp_link_executable, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_dynamic_library_action = action_config( - action_name = ACTION_NAMES.cpp_link_dynamic_library, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_nodeps_dynamic_library_action = action_config( - action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_static_library_action = action_config( - action_name = ACTION_NAMES.cpp_link_static_library, - tools = [tool(tool = ctx.executable.ar_binary)], - implies = ["archiver_flags"], - ) - strip_action = action_config( - action_name = ACTION_NAMES.strip, - tools = [tool(tool = ctx.executable.strip_binary)], - flag_sets = [ - flag_set( - flag_groups = [ - flag_group( - flags = [ - "-g", - "-p", - ], - ), - flag_group( - iterate_over = "stripopts", - flags = ["%{stripopts}"], - ), - flag_group( - flags = [ - "%{input_file}", - "%{output_file}", - ], - ), - ], - ), - ], - ) - - action_configs = [ - assemble_action, - c_compile_action, - cpp_compile_action, - cpp_link_dynamic_library_action, - cpp_link_executable_action, - cpp_link_nodeps_dynamic_library_action, - cpp_link_static_library_action, - preprocess_assemble_action, - strip_action, - ] - - shared = make_shared_features(UNFILTERED_COMPILE_FLAGS) - # Set LD_LIBRARY_PATH environment variable for all actions # This is needed by some toolchains since these libraries are not in `rpath` of compiler binary # which are needed to run it. @@ -151,7 +50,7 @@ def _impl(ctx): enabled = %{compiler_library_search_paths_switch}, env_sets = [ env_set( - actions = all_actions, + actions = actions.all_actions, env_entries = [env_entry(key = "LD_LIBRARY_PATH", value = compiler_library_search_paths)] if compiler_library_search_paths != "" else [], ), ], @@ -159,44 +58,44 @@ def _impl(ctx): target_cpu_flags = [] if ctx.attr.target_cpu == "x86_64": - target_cpu_flags = DEFAULT_x86_64_COMPILE_FLAGS + target_cpu_flags = _DEFAULT_x86_64_COMPILE_FLAGS else: - target_cpu_flags = DEFAULT_ARM64_COMPILE_FLAGS + target_cpu_flags = _DEFAULT_ARM64_COMPILE_FLAGS default_compile_flags_feature = feature( name = "default_compile_flags", enabled = True, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = DEFAULT_COMPILE_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.default_compile_flags, ), flag_set( - actions = all_compile_actions, + actions = actions.all_compile_actions, flag_groups = target_cpu_flags, ), flag_set( - actions = all_c_compile_actions, + actions = actions.all_c_compile_actions, flag_groups = [flag_group(flags = ["-std=c11"])], with_features = [with_feature_set(not_features = ["gnu11"])], ), flag_set( - actions = all_c_compile_actions, + actions = actions.all_c_compile_actions, flag_groups = [flag_group(flags = ["-std=gnu11"])], with_features = [with_feature_set(features = ["gnu11"])], ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = DEFAULT_CXX_COMPILE_FLAGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.default_cxx_compile_flags, ), flag_set( - actions = all_compile_actions, - flag_groups = DBG_COMPILE_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.dbg_compile_flags, with_features = [with_feature_set(features = ["dbg"])], ), flag_set( - actions = all_compile_actions, - flag_groups = OPT_COMPILE_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.opt_compile_flags, with_features = [ with_feature_set(features = ["opt"]), with_feature_set(features = ["fastbuild"]), @@ -213,7 +112,7 @@ def _impl(ctx): sysroot_path = sysroot_files[0].path sysroot_link_flags_feature_flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [flag_group(flags = [ "--sysroot=" + sysroot_path, "-Wl,--sysroot=" + sysroot_path, @@ -234,16 +133,16 @@ def _impl(ctx): enabled = default_link_flags_enabled, flag_sets = [ flag_set( - actions = all_link_actions, - flag_groups = DEFAULT_LINK_FLAGS, + actions = actions.all_link_actions, + flag_groups = flags.default_link_flags, ), flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [flag_group(flags = ["-Wl,--gc-sections"])], with_features = [with_feature_set(features = ["opt"])], ), flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group( flags = [ @@ -261,16 +160,16 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = MINIMAL_WARNINGS_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.minimal_warnings_flags, ), flag_set( - actions = all_c_compile_actions, - flag_groups = MINIMAL_C_WARNINGS_FLAGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.minimal_c_warnings_flags, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = MINIMAL_CXX_WARNINGS_FLAGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.minimal_cxx_warnings_flags, ), ], ) @@ -281,16 +180,16 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = STRICT_WARNINGS_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.strict_warnings_flags, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = STRICT_CXX_WARNINGS_FLAGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.strict_cxx_warnings_flags, ), flag_set( - actions = all_c_compile_actions, - flag_groups = STRICT_C_WARNINGS_FLAGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.strict_c_warnings_flags, ), ], ) @@ -301,16 +200,16 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = ALL_WALL_WARNINGS, + actions = actions.all_compile_actions, + flag_groups = flags.all_wall_warnings, ), flag_set( - actions = all_c_compile_actions, - flag_groups = ALL_WALL_C_WARNINGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.all_wall_c_warnings, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = ALL_WALL_CXX_WARNINGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.all_wall_cxx_warnings, ), ], ) @@ -320,8 +219,8 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = WARNINGS_AS_ERRORS, + actions = actions.all_compile_actions, + flag_groups = flags.warnings_as_errors, ), ], ) @@ -333,7 +232,7 @@ def _impl(ctx): implies = ["default_compile_flags"], flag_sets = [ flag_set( - actions = all_compile_actions, + actions = actions.all_compile_actions, flag_groups = extra_compile_flags, ), ], @@ -346,7 +245,7 @@ def _impl(ctx): implies = ["default_compile_flags"], flag_sets = [ flag_set( - actions = all_c_compile_actions, + actions = actions.all_c_compile_actions, flag_groups = extra_c_compile_flags, ), ], @@ -359,7 +258,7 @@ def _impl(ctx): implies = ["default_compile_flags"], flag_sets = [ flag_set( - actions = all_cpp_compile_actions, + actions = actions.all_cpp_compile_actions, flag_groups = extra_cxx_compile_flags, ), ], @@ -371,7 +270,7 @@ def _impl(ctx): enabled = %{extra_link_flags_switch}, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = extra_link_flags, ), ], @@ -382,7 +281,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group(flags = ["-pthread"]), ], @@ -421,7 +320,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group( iterate_over = "runtime_library_search_directories", @@ -485,7 +384,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group( flags = ["-Wl,--gdb-index"], @@ -502,7 +401,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group( iterate_over = "linkstamp_paths", @@ -520,7 +419,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_compile_actions, + actions = actions.all_compile_actions, flag_groups = [ flag_group( iterate_over = "includes", @@ -555,7 +454,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group( flags = ["-Wl,-S"], @@ -624,7 +523,7 @@ def _impl(ctx): )], ), flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [flag_group(flags = ["--coverage"])], ), ], @@ -634,8 +533,7 @@ def _impl(ctx): # The order of the features is relevant, they are applied in this specific order. # A command line parameter from a feature at the end of the list will appear # after a command line parameter from a feature at the beginning of the list. - - features = [ + return [ shared.no_legacy_features_feature, compiler_library_search_paths_feature, shared.dbg_feature, @@ -686,70 +584,36 @@ def _impl(ctx): gcc_coverage_map_format_feature, ] - extra_rules_based_features = depset(ctx.attr.extra_enabled_features + ctx.attr.extra_known_features) - features.extend([convert_feature(extra_feature[FeatureInfo], enabled = extra_feature in ctx.attr.extra_enabled_features) for extra_feature in extra_rules_based_features.to_list()]) +def make_toolchain_config_info(ctx, features, action_configs, tool_paths, abi_version, target_cpu, target_os, toolchain_identifier): + sysroot = None + if ctx.attr.sysroot != None: + sysroot_files = ctx.attr.sysroot[DefaultInfo].files.to_list() + if sysroot_files: + sysroot = sysroot_files[0].path - # Get builtin include directories from attribute if provided cxx_builtin_include_directories = [] if hasattr(ctx.attr, "builtin_include_directories") and ctx.attr.builtin_include_directories: cxx_builtin_include_directories = ctx.attr.builtin_include_directories - # TODO: Once https://github.com/bazelbuild/rules_cc/issues/351 is fixed remove this. - tool_paths = [tool_path(name = "gcov", path = "gcov_wrapper")] - - sysroot = None - if ctx.attr.sysroot != None: - sysroot = ctx.attr.sysroot[DefaultInfo].files.to_list()[0].path return cc_common.create_cc_toolchain_config_info( ctx = ctx, - abi_version = "%{tc_abi_version}", + abi_version = abi_version, abi_libc_version = "unknown", builtin_sysroot = sysroot, compiler = "gcc", cxx_builtin_include_directories = cxx_builtin_include_directories, - features = features, + features = features, action_configs = action_configs, host_system_name = "local", - target_system_name = "%{tc_cpu}-%{tc_os}", - target_cpu = ctx.attr.target_cpu, + target_system_name = "{}-{}".format(target_cpu, target_os), + target_cpu = target_cpu, target_libc = "unknown", - toolchain_identifier = "%{tc_identifier}", + toolchain_identifier = toolchain_identifier, tool_paths = tool_paths, ) -cc_toolchain_config = rule( - implementation = _impl, - provides = [CcToolchainConfigInfo], - attrs = { - "ar_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "cc_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "cxx_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "gcov_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "target_cpu": attr.string(mandatory = True), - "target_os": attr.string(mandatory = True), - "strip_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "sysroot": attr.label(default = None), - "host_dir": attr.label(default = None), - "target_dir": attr.label(default = None), - "cxx_builtin_include_directories": attr.label(allow_files = True, default = None), - "extra_enabled_features": attr.label_list( - providers = [FeatureInfo], - default = [], - doc = """ -Extra `cc_feature` features to add to this toolchain in an initially enabled state. -This attribute has limited integration with `cc_feature`, and does not run additional correctness checks or handle things like `data` files. -This is only offered as a migration bridge for projects transitioning to rule-based toolchain configurations, or sharing of simple argument sets with older toolchains. -""", - ), - "extra_known_features": attr.label_list( - providers = [FeatureInfo], - default = [], - doc = """ -Extra `cc_feature` features to add to this toolchain in an initially disabled state. -This attribute has limited integration with `cc_feature`, and does not run additional correctness checks or handle things like `data` files. -This is only offered as a migration bridge for projects transitioning to rule-based toolchain configurations, or sharing of simple argument sets with older toolchains. -""", - ), - }, -) \ No newline at end of file +EXTRA_ATTRS = { + "sysroot": attr.label(default = None), + "cxx_builtin_include_directories": attr.label(allow_files = True, default = None), +} diff --git a/templates/qnx/cc_toolchain_config.bzl.template b/templates/qnx/cc_toolchain_config.bzl.template index c023bd2..a85ca76 100644 --- a/templates/qnx/cc_toolchain_config.bzl.template +++ b/templates/qnx/cc_toolchain_config.bzl.template @@ -11,162 +11,56 @@ # SPDX-License-Identifier: Apache-2.0 # ******************************************************************************* -"""C/C++ toolchain definition""" +"""QNX-specific GCC toolchain feature factory.""" load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES") -load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", - "action_config", +load( + "@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "env_entry", "env_set", "feature", "feature_set", "flag_group", "flag_set", - "tool", - "tool_path", - "variable_with_value", "with_feature_set", ) -load("@rules_cc//cc:defs.bzl", "cc_common", "CcToolchainConfigInfo") -load("@rules_cc//cc/toolchains:feature_injection.bzl", "convert_feature", "FeatureInfo") -load(":flags.bzl", - "UNFILTERED_COMPILE_FLAGS", - "DEFAULT_COMPILE_FLAGS", - "DEFAULT_C_COMPILE_FLAGS", - "DEFAULT_CXX_COMPILE_FLAGS", - "DEFAULT_LINK_FLAGS", - "DBG_COMPILE_FLAGS", - "OPT_COMPILE_FLAGS", - "MINIMAL_WARNINGS_FLAGS", - "MINIMAL_C_WARNINGS_FLAGS", - "MINIMAL_CXX_WARNINGS_FLAGS", - "STRICT_WARNINGS_FLAGS", - "STRICT_C_WARNINGS_FLAGS", - "STRICT_CXX_WARNINGS_FLAGS", - "ALL_WALL_WARNINGS", - "ALL_WALL_C_WARNINGS", - "ALL_WALL_CXX_WARNINGS", - "WARNINGS_AS_ERRORS", +load("@rules_cc//cc:defs.bzl", "cc_common") +load( + ":shared_features.bzl", + "make_shared_features" ) -load(":shared_features.bzl", - "all_actions", - "all_assemble_actions", - "all_c_compile_actions", - "all_compile_actions", - "all_cpp_compile_actions", - "all_link_actions", - "make_shared_features", -) - -def _impl(ctx): - """ Implementation function of GCC toolchains. - """ - - assemble_action = action_config( - action_name = ACTION_NAMES.assemble, - tools = [tool(tool = ctx.executable.cc_binary)], - ) - preprocess_assemble_action = action_config( - action_name = ACTION_NAMES.preprocess_assemble, - tools = [tool(tool = ctx.executable.cc_binary)], - ) - c_compile_action = action_config( - action_name = ACTION_NAMES.c_compile, - tools = [tool(tool = ctx.executable.cc_binary)], - ) - cpp_compile_action = action_config( - action_name = ACTION_NAMES.cpp_compile, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_executable_action = action_config( - action_name = ACTION_NAMES.cpp_link_executable, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_dynamic_library_action = action_config( - action_name = ACTION_NAMES.cpp_link_dynamic_library, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_nodeps_dynamic_library_action = action_config( - action_name = ACTION_NAMES.cpp_link_nodeps_dynamic_library, - tools = [tool(tool = ctx.executable.cxx_binary)], - ) - cpp_link_static_library_action = action_config( - action_name = ACTION_NAMES.cpp_link_static_library, - tools = [tool(tool = ctx.executable.ar_binary)], - implies = ["archiver_flags"], - ) - strip_action = action_config( - action_name = ACTION_NAMES.strip, - tools = [tool(tool = ctx.executable.strip_binary)], - flag_sets = [ - flag_set( - flag_groups = [ - flag_group( - flags = [ - "-g", - "-p", - ], - ), - flag_group( - iterate_over = "stripopts", - flags = ["%{stripopts}"], - ), - flag_group( - flags = [ - "%{input_file}", - "%{output_file}", - ], - ), - ], - ), - ], - ) - - action_configs = [ - assemble_action, - c_compile_action, - cpp_compile_action, - cpp_link_dynamic_library_action, - cpp_link_executable_action, - cpp_link_nodeps_dynamic_library_action, - cpp_link_static_library_action, - preprocess_assemble_action, - strip_action, - ] - - shared = make_shared_features(UNFILTERED_COMPILE_FLAGS) - - # Core linking features +def make_features(ctx, flags, actions): + shared = make_shared_features(flags.unfiltered_compile_flags) default_compile_flags_feature = feature( name = "default_compile_flags", enabled = True, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = DEFAULT_COMPILE_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.default_compile_flags, ), flag_set( - actions = all_compile_actions, + actions = actions.all_compile_actions, flag_groups = [flag_group(flags = ["-V%{tc_version},gcc_nto%{tc_cpu}"])], ), flag_set( - actions = all_c_compile_actions, - flag_groups = DEFAULT_C_COMPILE_FLAGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.default_c_compile_flags, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = DEFAULT_CXX_COMPILE_FLAGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.default_cxx_compile_flags, ), flag_set( - actions = all_compile_actions, - flag_groups = DBG_COMPILE_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.dbg_compile_flags, with_features = [with_feature_set(features = ["dbg"])], ), flag_set( - actions = all_compile_actions, - flag_groups = OPT_COMPILE_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.opt_compile_flags, with_features = [ with_feature_set(features = ["opt"]), with_feature_set(features = ["fastbuild"]), @@ -180,12 +74,12 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [flag_group(flags = ["-V%{tc_version},gcc_nto%{tc_cpu}_cxx"])], ), flag_set( - actions = all_link_actions, - flag_groups = DEFAULT_LINK_FLAGS, + actions = actions.all_link_actions, + flag_groups = flags.default_link_flags, ), ], ) @@ -195,16 +89,16 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = MINIMAL_WARNINGS_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.minimal_warnings_flags, ), flag_set( - actions = all_c_compile_actions, - flag_groups = MINIMAL_C_WARNINGS_FLAGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.minimal_c_warnings_flags, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = MINIMAL_CXX_WARNINGS_FLAGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.minimal_cxx_warnings_flags, ), ], ) @@ -215,16 +109,16 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = STRICT_WARNINGS_FLAGS, + actions = actions.all_compile_actions, + flag_groups = flags.strict_warnings_flags, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = STRICT_CXX_WARNINGS_FLAGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.strict_cxx_warnings_flags, ), flag_set( - actions = all_c_compile_actions, - flag_groups = STRICT_C_WARNINGS_FLAGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.strict_c_warnings_flags, ), ], ) @@ -235,16 +129,16 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = ALL_WALL_WARNINGS, + actions = actions.all_compile_actions, + flag_groups = flags.all_wall_warnings, ), flag_set( - actions = all_c_compile_actions, - flag_groups = ALL_WALL_C_WARNINGS, + actions = actions.all_c_compile_actions, + flag_groups = flags.all_wall_c_warnings, ), flag_set( - actions = all_cpp_compile_actions, - flag_groups = ALL_WALL_CXX_WARNINGS, + actions = actions.all_cpp_compile_actions, + flag_groups = flags.all_wall_cxx_warnings, ), ], ) @@ -254,8 +148,8 @@ def _impl(ctx): enabled = False, flag_sets = [ flag_set( - actions = all_compile_actions, - flag_groups = WARNINGS_AS_ERRORS, + actions = actions.all_compile_actions, + flag_groups = flags.warnings_as_errors, ), ], ) @@ -267,7 +161,7 @@ def _impl(ctx): implies = ["default_compile_flags"], flag_sets = [ flag_set( - actions = all_compile_actions, + actions = actions.all_compile_actions, flag_groups = extra_compile_flags, ), ], @@ -280,7 +174,7 @@ def _impl(ctx): implies = ["default_link_flags"], flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = extra_link_flags, ), ], @@ -291,7 +185,7 @@ def _impl(ctx): enabled = False, ) - use_license_env_info_feautre = feature( + use_license_env_info_feature = feature( name = "qnx_license_env_info", enabled = %{use_license_info}, ) @@ -304,7 +198,7 @@ def _impl(ctx): enabled = True, env_sets = [ env_set( - actions = all_compile_actions + all_link_actions, + actions = actions.all_compile_actions + actions.all_link_actions, env_entries = [ env_entry(key = "QNX_HOST", value = "/proc/self/cwd/" + ctx.file.host_dir.path), env_entry(key = "QNX_TARGET", value = "/proc/self/cwd/" + ctx.file.target_dir.path), @@ -313,10 +207,10 @@ def _impl(ctx): ], ), env_set( - actions = all_compile_actions + all_link_actions, + actions = actions.all_compile_actions + actions.all_link_actions, env_entries = [env_entry(key = license_variable, value = license_value)] if license_value != "" else [], with_features = [with_feature_set(features = ["qnx_license_env_info"])], - ) + ), ], ) @@ -376,7 +270,7 @@ def _impl(ctx): enabled = True, flag_sets = [ flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [ flag_group( iterate_over = "runtime_library_search_directories", @@ -410,7 +304,7 @@ def _impl(ctx): )], ), flag_set( - actions = all_link_actions, + actions = actions.all_link_actions, flag_groups = [flag_group(flags = ["-lgcov"])], ), ], @@ -420,8 +314,7 @@ def _impl(ctx): # The order of the features is relevant, they are applied in this specific order. # A command line parameter from a feature at the end of the list will appear # after a command line parameter from a feature at the beginning of the list. - - features = [ + return [ shared.dbg_feature, shared.no_legacy_features_feature, shared.unfiltered_compile_flags_feature, @@ -449,7 +342,7 @@ def _impl(ctx): extra_compile_flags_feature, extra_link_flags_feature, shared.opt_feature, - use_license_env_info_feautre, + use_license_env_info_feature, sdp_env_feature, shared.supports_dynamic_linker_feature, shared.supports_pic_feature, @@ -459,64 +352,29 @@ def _impl(ctx): gcc_coverage_map_format_feature, ] - extra_rules_based_features = depset(ctx.attr.extra_enabled_features + ctx.attr.extra_known_features) - features.extend([convert_feature(extra_feature[FeatureInfo], enabled = extra_feature in ctx.attr.extra_enabled_features) for extra_feature in extra_rules_based_features.to_list()]) - +def make_toolchain_config_info(ctx, features, action_configs, tool_paths, abi_version, target_cpu, target_os, toolchain_identifier): cxx_builtin_include_directories = [ "/proc/self/cwd/{}".format(include_directory.path) for include_directory in ctx.files.cxx_builtin_include_directories ] - - # TODO: Once https://github.com/bazelbuild/rules_cc/issues/351 is fixed remove this. - tool_paths = [tool_path(name = "gcov", path = "gcov_wrapper")] - return cc_common.create_cc_toolchain_config_info( ctx = ctx, - abi_version = "%{tc_cpu}-qnx%{sdp_version}", + abi_version = abi_version, abi_libc_version = "unknown", compiler = "qcc", cxx_builtin_include_directories = cxx_builtin_include_directories, - features = features, + features = features, action_configs = action_configs, host_system_name = "local", - target_system_name = "%{tc_cpu}-qnx", - target_cpu = "%{tc_cpu}", + target_system_name = "{}-{}".format(target_cpu, target_os), + target_cpu = target_cpu, target_libc = "unknown", - toolchain_identifier = "%{tc_cpu}-qnx%{sdp_version}", + toolchain_identifier = toolchain_identifier, tool_paths = tool_paths, ) -cc_toolchain_config = rule( - implementation = _impl, - provides = [CcToolchainConfigInfo], - attrs = { - "ar_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "cc_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "cxx_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "gcov_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "strip_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True), - "host_dir": attr.label(allow_single_file = True, mandatory = True), - "target_dir": attr.label(allow_single_file = True, mandatory = True), - "target_cpu": attr.string(mandatory = True), - "target_os": attr.string(mandatory = True), - "cxx_builtin_include_directories": attr.label(allow_files = True, mandatory = True), - "extra_enabled_features": attr.label_list( - providers = [FeatureInfo], - default = [], - doc = """ -Extra `cc_feature` features to add to this toolchain in an initially enabled state. -This attribute has limited integration with `cc_feature`, and does not run additional correctness checks or handle things like `data` files. -This is only offered as a migration bridge for projects transitioning to rule-based toolchain configurations, or sharing of simple argument sets with older toolchains. -""", - ), - "extra_known_features": attr.label_list( - providers = [FeatureInfo], - default = [], - doc = """ -Extra `cc_feature` features to add to this toolchain in an initially disabled state. -This attribute has limited integration with `cc_feature`, and does not run additional correctness checks or handle things like `data` files. -This is only offered as a migration bridge for projects transitioning to rule-based toolchain configurations, or sharing of simple argument sets with older toolchains. -""", - ), - }, -) +EXTRA_ATTRS = { + "cxx_builtin_include_directories": attr.label(allow_files = True, mandatory = True), + "host_dir": attr.label(allow_single_file = True, mandatory = True), + "target_dir": attr.label(allow_single_file = True, mandatory = True), +} diff --git a/templates/shared/BUILD b/templates/shared/BUILD new file mode 100644 index 0000000..e69de29 diff --git a/templates/cc_toolchain_shared_features.bzl.template b/templates/shared/cc_toolchain_shared_features.bzl.template similarity index 100% rename from templates/cc_toolchain_shared_features.bzl.template rename to templates/shared/cc_toolchain_shared_features.bzl.template diff --git a/tests/MODULE.bazel.lock b/tests/MODULE.bazel.lock index 6fe2457..72733d6 100644 --- a/tests/MODULE.bazel.lock +++ b/tests/MODULE.bazel.lock @@ -1369,7 +1369,7 @@ }, "@@score_bazel_cpp_toolchains+//extensions:gcc.bzl%gcc": { "general": { - "bzlTransitiveDigest": "k0Poh7g76vVDdUlgMYBtFBF1aGJ46pOj0iy05dby95c=", + "bzlTransitiveDigest": "Jy96LAtJW12XgAmjOLEbuqYPd/mkrEiU/xDUNP7VbxI=", "usagesDigest": "dWr2zFjJ8fBvweFk2KWoZzQqJ+aKnb9A8QACQhKlUfw=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -1507,9 +1507,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": true } }, @@ -1535,9 +1535,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1563,9 +1563,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "15.3.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1591,9 +1591,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1619,9 +1619,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "15.3.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1649,9 +1649,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1677,9 +1677,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/qnx:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/qnx:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1705,9 +1705,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/qnx:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/qnx:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1769,9 +1769,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "autosd10", "gcc_version": "", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } }, @@ -1849,9 +1849,9 @@ "tc_system_toolchain": false, "tc_runtime_ecosystem": "ebclfsa", "gcc_version": "", - "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_config.bzl.template", + "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", - "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_shared_features.bzl.template", + "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } } From 73b61c46e6e781e4b672f7dbf92215135c528243 Mon Sep 17 00:00:00 2001 From: Armando Martins Date: Thu, 13 Aug 2026 17:14:38 +0100 Subject: [PATCH 2/3] Common toolchain flags separated from specific ones Created a layer of abstraction with the flags that are common to all toolchains Inside linux and qnx folders we have only the specific ones. Common flags are sent to the specific toolchain via arguments of "make_features" and the specific ones are loaded directly inside specific toolchain --- extensions/gcc.bzl | 2 +- rules/gcc.bzl | 18 +++++++ templates/cc_toolchain_config.bzl.template | 32 +---------- templates/cc_toolchain_flags.bzl.template | 54 +++++++++++++++++++ .../linux/cc_toolchain_config.bzl.template | 42 ++++++++++----- .../linux/cc_toolchain_flags.bzl.template | 22 +------- .../qnx/cc_toolchain_config.bzl.template | 44 ++++++++++----- templates/qnx/cc_toolchain_flags.bzl.template | 20 ------- tests/MODULE.bazel.lock | 22 ++++---- 9 files changed, 146 insertions(+), 110 deletions(-) create mode 100644 templates/cc_toolchain_flags.bzl.template diff --git a/extensions/gcc.bzl b/extensions/gcc.bzl index 15e086e..7daf650 100644 --- a/extensions/gcc.bzl +++ b/extensions/gcc.bzl @@ -204,7 +204,7 @@ def _get_toolchains(tags): for tag in tags: toolchain = { "cc_toolchain_config": "@score_bazel_cpp_toolchains//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@score_bazel_cpp_toolchains//templates/{}:cc_toolchain_flags.bzl.template".format(tag.target_os), + "cc_toolchain_flags": "@score_bazel_cpp_toolchains//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@score_bazel_cpp_toolchains//templates/shared:cc_toolchain_shared_features.bzl.template", "gcc_version": tag.version, "name": tag.name, diff --git a/rules/gcc.bzl b/rules/gcc.bzl index 6ebb814..6462bef 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -298,6 +298,18 @@ def _impl(rctx): qnx_features_template_dict, ) + rctx.template( + "linux_flags.bzl", + rctx.attr._cc_toolchain_linux_flags, + {}, + ) + + rctx.template( + "qnx_flags.bzl", + rctx.attr._cc_toolchain_qnx_flags, + {}, + ) + rctx.template( "flags.bzl", rctx.attr.cc_toolchain_flags, @@ -379,9 +391,15 @@ gcc_toolchain = repository_rule( "_cc_toolchain_linux_config": attr.label( default = "@score_bazel_cpp_toolchains//templates/linux:cc_toolchain_config.bzl.template", ), + "_cc_toolchain_linux_flags": attr.label( + default = "@score_bazel_cpp_toolchains//templates/linux:cc_toolchain_flags.bzl.template", + ), "_cc_toolchain_qnx_config": attr.label( default = "@score_bazel_cpp_toolchains//templates/qnx:cc_toolchain_config.bzl.template", ), + "_cc_toolchain_qnx_flags": attr.label( + default = "@score_bazel_cpp_toolchains//templates/qnx:cc_toolchain_flags.bzl.template", + ), "_cc_toolchain_build": attr.label( default = "@score_bazel_cpp_toolchains//templates:BUILD.template", doc = "Path to the Bazel BUILD file template for the toolchain.", diff --git a/templates/cc_toolchain_config.bzl.template b/templates/cc_toolchain_config.bzl.template index 5e4b243..e24d78b 100644 --- a/templates/cc_toolchain_config.bzl.template +++ b/templates/cc_toolchain_config.bzl.template @@ -27,23 +27,9 @@ load("@rules_cc//cc/toolchains:feature_injection.bzl", "FeatureInfo", "convert_f load(":cc_toolchain_%{tc_os}_config.bzl", "EXTRA_ATTRS", "make_features", "make_toolchain_config_info") load( ":flags.bzl", - "ALL_WALL_C_WARNINGS", - "ALL_WALL_CXX_WARNINGS", - "ALL_WALL_WARNINGS", - "DBG_COMPILE_FLAGS", - "DEFAULT_C_COMPILE_FLAGS", - "DEFAULT_COMPILE_FLAGS", - "DEFAULT_CXX_COMPILE_FLAGS", - "DEFAULT_LINK_FLAGS", - "MINIMAL_C_WARNINGS_FLAGS", - "MINIMAL_CXX_WARNINGS_FLAGS", - "MINIMAL_WARNINGS_FLAGS", - "OPT_COMPILE_FLAGS", - "STRICT_C_WARNINGS_FLAGS", - "STRICT_CXX_WARNINGS_FLAGS", - "STRICT_WARNINGS_FLAGS", "UNFILTERED_COMPILE_FLAGS", - "WARNINGS_AS_ERRORS", + "DBG_COMPILE_FLAGS", + "OPT_COMPILE_FLAGS" ) load( ":shared_features.bzl", @@ -58,22 +44,8 @@ load( def _impl(ctx): flags = struct( unfiltered_compile_flags = UNFILTERED_COMPILE_FLAGS, - default_compile_flags = DEFAULT_COMPILE_FLAGS, - default_c_compile_flags = DEFAULT_C_COMPILE_FLAGS, - default_cxx_compile_flags = DEFAULT_CXX_COMPILE_FLAGS, - default_link_flags = DEFAULT_LINK_FLAGS, dbg_compile_flags = DBG_COMPILE_FLAGS, opt_compile_flags = OPT_COMPILE_FLAGS, - minimal_warnings_flags = MINIMAL_WARNINGS_FLAGS, - minimal_c_warnings_flags = MINIMAL_C_WARNINGS_FLAGS, - minimal_cxx_warnings_flags = MINIMAL_CXX_WARNINGS_FLAGS, - strict_warnings_flags = STRICT_WARNINGS_FLAGS, - strict_c_warnings_flags = STRICT_C_WARNINGS_FLAGS, - strict_cxx_warnings_flags = STRICT_CXX_WARNINGS_FLAGS, - all_wall_warnings = ALL_WALL_WARNINGS, - all_wall_c_warnings = ALL_WALL_C_WARNINGS, - all_wall_cxx_warnings = ALL_WALL_CXX_WARNINGS, - warnings_as_errors = WARNINGS_AS_ERRORS, ) actions = struct( diff --git a/templates/cc_toolchain_flags.bzl.template b/templates/cc_toolchain_flags.bzl.template new file mode 100644 index 0000000..26cf500 --- /dev/null +++ b/templates/cc_toolchain_flags.bzl.template @@ -0,0 +1,54 @@ +# ******************************************************************************* +# Copyright (c) 2025 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* + +""" Common compile and link flags for GCC toolchains. +""" + +load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl", "flag_group") + +def get_flag_group(flags): + """Helper function to create a flag_group. + + Args: + flags (list[str]): A list of compiler or linker flags. + + Returns: + flag_group: A Bazel flag_group object containing the provided flags. + """ + if len(flags): + return [ + flag_group( + flags = flags, + ) + ] + return [] + +# Flags that should always be applied, without filtering. +UNFILTERED_COMPILE_FLAGS = get_flag_group([ + "-D__DATE__=\"redacted\"", + "-D__TIMESTAMP__=\"redacted\"", + "-D__TIME__=\"redacted\"", + "-Wno-builtin-macro-redefined", +]) + +# Debug compile flags. +DBG_COMPILE_FLAGS = get_flag_group([ + "-Og", + "-g3", +]) + +# Optimization compile flags. +OPT_COMPILE_FLAGS = get_flag_group([ + "-O2", + "-DNDEBUG", +]) diff --git a/templates/linux/cc_toolchain_config.bzl.template b/templates/linux/cc_toolchain_config.bzl.template index 5fde0c0..a485ac0 100644 --- a/templates/linux/cc_toolchain_config.bzl.template +++ b/templates/linux/cc_toolchain_config.bzl.template @@ -25,6 +25,22 @@ load( "with_feature_set", ) load("@rules_cc//cc:defs.bzl", "cc_common") +load(":linux_flags.bzl", + "DEFAULT_COMPILE_FLAGS", + "DEFAULT_C_COMPILE_FLAGS", + "DEFAULT_CXX_COMPILE_FLAGS", + "DEFAULT_LINK_FLAGS", + "MINIMAL_WARNINGS_FLAGS", + "MINIMAL_C_WARNINGS_FLAGS", + "MINIMAL_CXX_WARNINGS_FLAGS", + "STRICT_WARNINGS_FLAGS", + "STRICT_C_WARNINGS_FLAGS", + "STRICT_CXX_WARNINGS_FLAGS", + "ALL_WALL_WARNINGS", + "ALL_WALL_C_WARNINGS", + "ALL_WALL_CXX_WARNINGS", + "WARNINGS_AS_ERRORS", +) load( ":shared_features.bzl", "make_shared_features" @@ -68,7 +84,7 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.default_compile_flags, + flag_groups = DEFAULT_COMPILE_FLAGS, ), flag_set( actions = actions.all_compile_actions, @@ -86,7 +102,7 @@ def make_features(ctx, flags, actions): ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.default_cxx_compile_flags, + flag_groups = DEFAULT_CXX_COMPILE_FLAGS, ), flag_set( actions = actions.all_compile_actions, @@ -134,7 +150,7 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_link_actions, - flag_groups = flags.default_link_flags, + flag_groups = DEFAULT_LINK_FLAGS, ), flag_set( actions = actions.all_link_actions, @@ -161,15 +177,15 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.minimal_warnings_flags, + flag_groups = MINIMAL_WARNINGS_FLAGS, ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.minimal_c_warnings_flags, + flag_groups = MINIMAL_C_WARNINGS_FLAGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.minimal_cxx_warnings_flags, + flag_groups = MINIMAL_CXX_WARNINGS_FLAGS, ), ], ) @@ -181,15 +197,15 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.strict_warnings_flags, + flag_groups = STRICT_WARNINGS_FLAGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.strict_cxx_warnings_flags, + flag_groups = STRICT_CXX_WARNINGS_FLAGS, ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.strict_c_warnings_flags, + flag_groups = STRICT_C_WARNINGS_FLAGS, ), ], ) @@ -201,15 +217,15 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.all_wall_warnings, + flag_groups = ALL_WALL_WARNINGS, ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.all_wall_c_warnings, + flag_groups = ALL_WALL_C_WARNINGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.all_wall_cxx_warnings, + flag_groups = ALL_WALL_CXX_WARNINGS, ), ], ) @@ -220,7 +236,7 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.warnings_as_errors, + flag_groups = WARNINGS_AS_ERRORS, ), ], ) diff --git a/templates/linux/cc_toolchain_flags.bzl.template b/templates/linux/cc_toolchain_flags.bzl.template index b42f862..eee1e3b 100644 --- a/templates/linux/cc_toolchain_flags.bzl.template +++ b/templates/linux/cc_toolchain_flags.bzl.template @@ -33,14 +33,6 @@ def get_flag_group(flags): ] return [] -# Flags that should always be applied, without filtering. -UNFILTERED_COMPILE_FLAGS = get_flag_group([ - "-D__DATE__=\"redacted\"", - "-D__TIMESTAMP__=\"redacted\"", - "-D__TIME__=\"redacted\"", - "-Wno-builtin-macro-redefined", -]) - # Default compile flags when no specific build type is selected. DEFAULT_COMPILE_FLAGS = get_flag_group([ "-U_FORTIFY_SOURCE", @@ -72,18 +64,6 @@ DEFAULT_CXX_COMPILE_FLAGS = get_flag_group([ "-std=c++17", ]) -# Debug compile flags. -DBG_COMPILE_FLAGS = get_flag_group([ - "-Og", - "-g3", -]) - -# Optimization compile flags. -OPT_COMPILE_FLAGS = get_flag_group([ - "-O2", - "-DNDEBUG", -]) - # Default link flags when no specific build type is selected. DEFAULT_LINK_FLAGS = get_flag_group([ "-lm", @@ -173,7 +153,7 @@ ALL_WALL_CXX_WARNINGS = get_flag_group([ # "-Wenum-int-mismatch", not supported in GCC12.2 # "-Wargument-mismatch", cc1plus: warning: command-line option '-Wduplicate-decl-specifier' is valid for C/ObjC but not for C++ "-Wmismatched-new-delete", - "-Woverloaded-virtual", + "-Woverloaded-virtual", # -Woverloaded-virtual=1, not supported in GCC12.2 "-Wpessimizing-move", "-Wrange-loop-construct", diff --git a/templates/qnx/cc_toolchain_config.bzl.template b/templates/qnx/cc_toolchain_config.bzl.template index a85ca76..b21775a 100644 --- a/templates/qnx/cc_toolchain_config.bzl.template +++ b/templates/qnx/cc_toolchain_config.bzl.template @@ -25,6 +25,22 @@ load( "with_feature_set", ) load("@rules_cc//cc:defs.bzl", "cc_common") +load(":qnx_flags.bzl", + "DEFAULT_COMPILE_FLAGS", + "DEFAULT_C_COMPILE_FLAGS", + "DEFAULT_CXX_COMPILE_FLAGS", + "DEFAULT_LINK_FLAGS", + "MINIMAL_WARNINGS_FLAGS", + "MINIMAL_C_WARNINGS_FLAGS", + "MINIMAL_CXX_WARNINGS_FLAGS", + "STRICT_WARNINGS_FLAGS", + "STRICT_C_WARNINGS_FLAGS", + "STRICT_CXX_WARNINGS_FLAGS", + "ALL_WALL_WARNINGS", + "ALL_WALL_C_WARNINGS", + "ALL_WALL_CXX_WARNINGS", + "WARNINGS_AS_ERRORS", +) load( ":shared_features.bzl", "make_shared_features" @@ -39,7 +55,7 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.default_compile_flags, + flag_groups = DEFAULT_COMPILE_FLAGS, ), flag_set( actions = actions.all_compile_actions, @@ -47,11 +63,11 @@ def make_features(ctx, flags, actions): ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.default_c_compile_flags, + flag_groups = DEFAULT_C_COMPILE_FLAGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.default_cxx_compile_flags, + flag_groups = DEFAULT_CXX_COMPILE_FLAGS, ), flag_set( actions = actions.all_compile_actions, @@ -79,7 +95,7 @@ def make_features(ctx, flags, actions): ), flag_set( actions = actions.all_link_actions, - flag_groups = flags.default_link_flags, + flag_groups = DEFAULT_LINK_FLAGS, ), ], ) @@ -90,15 +106,15 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.minimal_warnings_flags, + flag_groups = MINIMAL_WARNINGS_FLAGS, ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.minimal_c_warnings_flags, + flag_groups = MINIMAL_C_WARNINGS_FLAGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.minimal_cxx_warnings_flags, + flag_groups = MINIMAL_CXX_WARNINGS_FLAGS, ), ], ) @@ -110,15 +126,15 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.strict_warnings_flags, + flag_groups = STRICT_WARNINGS_FLAGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.strict_cxx_warnings_flags, + flag_groups = STRICT_CXX_WARNINGS_FLAGS, ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.strict_c_warnings_flags, + flag_groups = STRICT_C_WARNINGS_FLAGS, ), ], ) @@ -130,15 +146,15 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.all_wall_warnings, + flag_groups = ALL_WALL_WARNINGS, ), flag_set( actions = actions.all_c_compile_actions, - flag_groups = flags.all_wall_c_warnings, + flag_groups = ALL_WALL_C_WARNINGS, ), flag_set( actions = actions.all_cpp_compile_actions, - flag_groups = flags.all_wall_cxx_warnings, + flag_groups = ALL_WALL_CXX_WARNINGS, ), ], ) @@ -149,7 +165,7 @@ def make_features(ctx, flags, actions): flag_sets = [ flag_set( actions = actions.all_compile_actions, - flag_groups = flags.warnings_as_errors, + flag_groups = WARNINGS_AS_ERRORS, ), ], ) diff --git a/templates/qnx/cc_toolchain_flags.bzl.template b/templates/qnx/cc_toolchain_flags.bzl.template index ba18bc6..810fc47 100644 --- a/templates/qnx/cc_toolchain_flags.bzl.template +++ b/templates/qnx/cc_toolchain_flags.bzl.template @@ -33,14 +33,6 @@ def get_flag_group(flags): ] return [] -# Flags that should always be applied, without filtering. -UNFILTERED_COMPILE_FLAGS = get_flag_group([ - "-D__DATE__=\"redacted\"", - "-D__TIMESTAMP__=\"redacted\"", - "-D__TIME__=\"redacted\"", - "-Wno-builtin-macro-redefined", -]) - # Default compile flags when no specific build type is selected. DEFAULT_COMPILE_FLAGS = get_flag_group([ "-fno-canonical-system-headers", @@ -57,18 +49,6 @@ DEFAULT_CXX_COMPILE_FLAGS = get_flag_group([ "-std=c++17", ]) -# Debug compile flags. -DBG_COMPILE_FLAGS = get_flag_group([ - "-Og", - "-g3", -]) - -# Optimization compile flags. -OPT_COMPILE_FLAGS = get_flag_group([ - "-O2", - "-DNDEBUG", -]) - # Default link flags when no specific build type is selected. DEFAULT_LINK_FLAGS = get_flag_group([ "-Wl,-z,relro", diff --git a/tests/MODULE.bazel.lock b/tests/MODULE.bazel.lock index 72733d6..ee3f3f8 100644 --- a/tests/MODULE.bazel.lock +++ b/tests/MODULE.bazel.lock @@ -1369,7 +1369,7 @@ }, "@@score_bazel_cpp_toolchains+//extensions:gcc.bzl%gcc": { "general": { - "bzlTransitiveDigest": "Jy96LAtJW12XgAmjOLEbuqYPd/mkrEiU/xDUNP7VbxI=", + "bzlTransitiveDigest": "EpbNFwAvKdUJiqEDswJ3jdtGKx9mv7ALyE/EmQsQZQ8=", "usagesDigest": "dWr2zFjJ8fBvweFk2KWoZzQqJ+aKnb9A8QACQhKlUfw=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, @@ -1508,7 +1508,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": true } @@ -1536,7 +1536,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1564,7 +1564,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "15.3.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1592,7 +1592,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1620,7 +1620,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "15.3.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1650,7 +1650,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1678,7 +1678,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/qnx:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1706,7 +1706,7 @@ "tc_runtime_ecosystem": "", "gcc_version": "12.2.0", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/qnx:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1770,7 +1770,7 @@ "tc_runtime_ecosystem": "autosd10", "gcc_version": "", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } @@ -1850,7 +1850,7 @@ "tc_runtime_ecosystem": "ebclfsa", "gcc_version": "", "cc_toolchain_config": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_config.bzl.template", - "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates/linux:cc_toolchain_flags.bzl.template", + "cc_toolchain_flags": "@@score_bazel_cpp_toolchains+//templates:cc_toolchain_flags.bzl.template", "cc_toolchain_shared_features": "@@score_bazel_cpp_toolchains+//templates/shared:cc_toolchain_shared_features.bzl.template", "use_base_constraints_only": false } From fa52e9063f38f3ce11ba24f9b367bf6dd3b3994a Mon Sep 17 00:00:00 2001 From: Armando Martins Date: Fri, 14 Aug 2026 10:44:47 +0100 Subject: [PATCH 3/3] only generate OS-specific toolchain files previously we were generating the config/flags for all the toolchains available in the project. Now we isolate each one of those, and the toolchain config/flags generated are now only the ones that are needed for a certain build, so when we run qnx toolchain, we dont generate the linux artifacts and vice versa. --- rules/gcc.bzl | 97 ++++++++++++++++++++--------------------- tests/MODULE.bazel.lock | 2 +- 2 files changed, 48 insertions(+), 51 deletions(-) diff --git a/rules/gcc.bzl b/rules/gcc.bzl index 6462bef..e353b65 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -253,62 +253,59 @@ def _impl(rctx): "%{tc_os}": rctx.attr.tc_os, } - linux_features_template_dict = { - "%{compiler_library_search_paths_switch}": "True" if len(rctx.attr.tc_compiler_library_search_paths) else "False", - "%{compiler_library_search_paths}": ":".join(["/proc/self/cwd/" + entry for entry in compiler_library_search_paths]), - "%{extra_c_compile_flags_switch}": "True" if len(rctx.attr.extra_c_compile_flags) else "False", - "%{extra_c_compile_flags}": extra_c_compile_flags, - "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", - "%{extra_compile_flags}": extra_compile_flags, - "%{extra_cxx_compile_flags_switch}": "True" if len(rctx.attr.extra_cxx_compile_flags) else "False", - "%{extra_cxx_compile_flags}": extra_cxx_compile_flags, - "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", - "%{extra_link_flags}": extra_link_flags, - } - - qnx_features_template_dict = { - "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", - "%{extra_compile_flags}": extra_compile_flags, - "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", - "%{extra_link_flags}": extra_link_flags, - "%{license_info_value}": rctx.attr.license_info_value, - "%{license_info_variable}": rctx.attr.license_info_variable, - "%{license_path}": rctx.attr.license_path, - "%{sdp_version}": mapped_sdp_version_for_config, - "%{tc_cpu}": _normalize_cpu(rctx.attr.tc_cpu), - "%{tc_version}": rctx.attr.gcc_version, - "%{use_license_info}": "False" if rctx.attr.license_info_value == "" else "True", - } - rctx.template( "cc_toolchain_config.bzl", rctx.attr.cc_toolchain_config, config_template_dict, ) - rctx.template( - "cc_toolchain_linux_config.bzl", - rctx.attr._cc_toolchain_linux_config, - linux_features_template_dict, - ) - - rctx.template( - "cc_toolchain_qnx_config.bzl", - rctx.attr._cc_toolchain_qnx_config, - qnx_features_template_dict, - ) - - rctx.template( - "linux_flags.bzl", - rctx.attr._cc_toolchain_linux_flags, - {}, - ) - - rctx.template( - "qnx_flags.bzl", - rctx.attr._cc_toolchain_qnx_flags, - {}, - ) + if rctx.attr.tc_os == _OS_LINUX: + linux_features_template_dict = { + "%{compiler_library_search_paths_switch}": "True" if len(rctx.attr.tc_compiler_library_search_paths) else "False", + "%{compiler_library_search_paths}": ":".join(["/proc/self/cwd/" + entry for entry in compiler_library_search_paths]), + "%{extra_c_compile_flags_switch}": "True" if len(rctx.attr.extra_c_compile_flags) else "False", + "%{extra_c_compile_flags}": extra_c_compile_flags, + "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", + "%{extra_compile_flags}": extra_compile_flags, + "%{extra_cxx_compile_flags_switch}": "True" if len(rctx.attr.extra_cxx_compile_flags) else "False", + "%{extra_cxx_compile_flags}": extra_cxx_compile_flags, + "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", + "%{extra_link_flags}": extra_link_flags, + } + rctx.template( + "cc_toolchain_linux_config.bzl", + rctx.attr._cc_toolchain_linux_config, + linux_features_template_dict, + ) + rctx.template( + "linux_flags.bzl", + rctx.attr._cc_toolchain_linux_flags, + {}, + ) + elif rctx.attr.tc_os == _OS_QNX: + qnx_features_template_dict = { + "%{extra_compile_flags_switch}": "True" if len(rctx.attr.extra_compile_flags) else "False", + "%{extra_compile_flags}": extra_compile_flags, + "%{extra_link_flags_switch}": "True" if len(rctx.attr.extra_link_flags) else "False", + "%{extra_link_flags}": extra_link_flags, + "%{license_info_value}": rctx.attr.license_info_value, + "%{license_info_variable}": rctx.attr.license_info_variable, + "%{license_path}": rctx.attr.license_path, + "%{sdp_version}": mapped_sdp_version_for_config, + "%{tc_cpu}": _normalize_cpu(rctx.attr.tc_cpu), + "%{tc_version}": rctx.attr.gcc_version, + "%{use_license_info}": "False" if rctx.attr.license_info_value == "" else "True", + } + rctx.template( + "cc_toolchain_qnx_config.bzl", + rctx.attr._cc_toolchain_qnx_config, + qnx_features_template_dict, + ) + rctx.template( + "qnx_flags.bzl", + rctx.attr._cc_toolchain_qnx_flags, + {}, + ) rctx.template( "flags.bzl", diff --git a/tests/MODULE.bazel.lock b/tests/MODULE.bazel.lock index ee3f3f8..ed00968 100644 --- a/tests/MODULE.bazel.lock +++ b/tests/MODULE.bazel.lock @@ -1369,7 +1369,7 @@ }, "@@score_bazel_cpp_toolchains+//extensions:gcc.bzl%gcc": { "general": { - "bzlTransitiveDigest": "EpbNFwAvKdUJiqEDswJ3jdtGKx9mv7ALyE/EmQsQZQ8=", + "bzlTransitiveDigest": "dSSh8n3cAOdwaMVKE/KXyuS+8dOvtBntngyzfLzpMMk=", "usagesDigest": "dWr2zFjJ8fBvweFk2KWoZzQqJ+aKnb9A8QACQhKlUfw=", "recordedFileInputs": {}, "recordedDirentsInputs": {},