Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions extensions/gcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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_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_config": "@score_bazel_cpp_toolchains//templates:cc_toolchain_config.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",
"gcc_version": tag.version,
"name": tag.name,
"use_base_constraints_only": tag.use_base_constraints_only,
Expand Down
96 changes: 69 additions & 27 deletions rules/gcc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -241,40 +241,71 @@ 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 = {
"%{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,
# 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}": "gcc",
"%{tc_runtime_es}": rctx.attr.tc_runtime_ecosystem,
"%{tc_version}": rctx.attr.gcc_version,
"%{tc_identifier}": rctx.attr.tc_identifier if rctx.attr.tc_identifier else "gcc",
"%{tc_os}": rctx.attr.tc_os,
}

if rctx.attr.tc_os == _OS_QNX:
mapped_sdp_version = _apply_sdp_version_mapping(rctx.attr.sdp_version)
extra_template_dict = {
rctx.template(
"cc_toolchain_config.bzl",
rctx.attr.cc_toolchain_config,
config_template_dict,
)

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,
"%{tc_cpu_cxx}": _normalize_cpu(rctx.attr.tc_cpu),
"%{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",
}
template_dict = dict_union(template_dict, extra_template_dict)

rctx.template(
"cc_toolchain_config.bzl",
rctx.attr.cc_toolchain_config,
template_dict,
)
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",
Expand Down Expand Up @@ -304,7 +335,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:
Expand Down Expand Up @@ -355,6 +385,18 @@ 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_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.",
Expand Down
177 changes: 177 additions & 0 deletions templates/cc_toolchain_config.bzl.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# *******************************************************************************
# 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",
"UNFILTERED_COMPILE_FLAGS",
"DBG_COMPILE_FLAGS",
"OPT_COMPILE_FLAGS"
)
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,
dbg_compile_flags = DBG_COMPILE_FLAGS,
opt_compile_flags = OPT_COMPILE_FLAGS,
)

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,
)
54 changes: 54 additions & 0 deletions templates/cc_toolchain_flags.bzl.template
Original file line number Diff line number Diff line change
@@ -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",
])
Loading
Loading