From aa2c019c5034d7e8ff35d7231a59f0f6730a884c Mon Sep 17 00:00:00 2001 From: Nikola Radakovic Date: Tue, 29 Sep 2026 14:36:46 +0200 Subject: [PATCH] Derive GCC version flag from resolved toolchain version The `-V,gcc_nto` flag passed to qcc for QNX toolchains was hardcoded to `12.2.0` in a static, repo-wide `cc_feature`, so it never reflected a toolchain instance's actual `version`/`gcc_version` attribute or version-matrix entry. - Convert `gcc_version_flags` into a per-instance generated feature (features/custom/qnx/gcc_version_flags/feature.bzl), mirroring the existing `sdp_env` pattern, driven by the resolved `gcc_version` and normalized target CPU. - Wire it into rules/gcc.bzl's get_custom_cc_features_qnx() and reference the per-instance label in make_cc_features.bzl instead of the shared static target. - Add QNX_SDP_TO_GCC_VERSION in rules/common.bzl to default gcc_version from sdp_version's major release, and fail the build if an explicit version conflicts with what the SDP ships. --- extensions/gcc.bzl | 40 +++++++++++++++- features/custom/qnx/gcc_version_flags/BUILD | 43 ----------------- .../custom/qnx/gcc_version_flags/feature.bzl | 47 +++++++++++++++++++ features/custom/qnx/make_cc_features.bzl | 2 +- rules/common.bzl | 7 +++ rules/gcc.bzl | 10 +++- tests/MODULE.bazel.lock | 2 +- 7 files changed, 104 insertions(+), 47 deletions(-) create mode 100644 features/custom/qnx/gcc_version_flags/feature.bzl diff --git a/extensions/gcc.bzl b/extensions/gcc.bzl index c16364f..e0911aa 100644 --- a/extensions/gcc.bzl +++ b/extensions/gcc.bzl @@ -16,7 +16,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") load("@score_bazel_cpp_toolchains//packages:version_matrix.bzl", "VERSION_MATRIX") -load("@score_bazel_cpp_toolchains//rules:common.bzl", "SDP_VERSION_MAPPING") +load("@score_bazel_cpp_toolchains//rules:common.bzl", "QNX_SDP_TO_GCC_VERSION", "SDP_VERSION_MAPPING") load("@score_bazel_cpp_toolchains//rules:gcc.bzl", "gcc_toolchain") # Constants @@ -24,9 +24,11 @@ _PACKAGE_SUFFIX = "_pkg" _IDENTIFIER_GCC = "gcc" _IDENTIFIER_SDK = "sdk" _IDENTIFIER_SDP = "sdp" +_OS_QNX = "qnx" _SUPPORTED_CPUS = ["x86_64", "aarch64"] _SUPPORTED_OSS = ["linux", "qnx"] _SDP_VERSION_MAPPING = SDP_VERSION_MAPPING +_QNX_SDP_TO_GCC_VERSION = QNX_SDP_TO_GCC_VERSION # GCC interface API for archive tag class _attrs_sdp = { @@ -381,6 +383,39 @@ def _resolve_identifier(toolchain_info): return "{}_{}".format(identifier, version) +def _resolve_qnx_gcc_version(toolchain_info): + """Defaults or validates gcc_version against the GCC version bundled with sdp_version. + + Args: + toolchain_info: dict holding toolchain configuration (modified in-place). + + Fails: + If an explicit `version` doesn't match the GCC version bundled with the SDP's + major release, per QNX_SDP_TO_GCC_VERSION. + """ + if toolchain_info["tc_os"] != _OS_QNX or toolchain_info["sdp_version"] == "": + return + + sdp_major = toolchain_info["sdp_version"].split(".")[0] + expected_gcc_version = _QNX_SDP_TO_GCC_VERSION.get(sdp_major) + if expected_gcc_version == None: + return + + if toolchain_info["gcc_version"] == "": + toolchain_info["gcc_version"] = expected_gcc_version + elif toolchain_info["gcc_version"] != expected_gcc_version: + fail(( + "Toolchain '{name}': version = '{actual}' does not match GCC {expected}, " + + "which is what QNX SDP {sdp_major}.x (sdp_version = '{sdp_version}') ships. " + + "Either drop the explicit version or set it to '{expected}'." + ).format( + name = toolchain_info["name"], + actual = toolchain_info["gcc_version"], + expected = expected_gcc_version, + sdp_major = sdp_major, + sdp_version = toolchain_info["sdp_version"], + )) + def _get_info(mctx): """Extracts and validates toolchain and package information from module configuration. @@ -415,11 +450,14 @@ def _get_info(mctx): # need to be sure not to link package in case of system toolchain. if tc["use_system_toolchain"]: + _resolve_qnx_gcc_version(tc) continue if tc["use_default_package"]: packages.append(_create_and_link_sdp(tc)) + _resolve_qnx_gcc_version(tc) + return toolchains, packages def _impl(mctx): diff --git a/features/custom/qnx/gcc_version_flags/BUILD b/features/custom/qnx/gcc_version_flags/BUILD index 6e049e2..e69de29 100644 --- a/features/custom/qnx/gcc_version_flags/BUILD +++ b/features/custom/qnx/gcc_version_flags/BUILD @@ -1,43 +0,0 @@ -# ******************************************************************************* -# Copyright (c) 2026 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 -# ******************************************************************************* - -load("@rules_cc//cc/toolchains:args.bzl", "cc_args") -load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature") - -cc_args( - name = "qnx_gcc_version_compile_args", - actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], - args = select({ - "@score_bazel_platforms//settings:aarch64-qnx8": ["-V12.2.0,gcc_ntoaarch64le"], - "@score_bazel_platforms//settings:x86_64-qnx8": ["-V12.2.0,gcc_ntox86_64"], - }), -) - -cc_args( - name = "qnx_gcc_version_link_args", - actions = ["@rules_cc//cc/toolchains/actions:link_actions"], - args = select({ - "@score_bazel_platforms//settings:aarch64-qnx8": ["-V12.2.0,gcc_ntoaarch64le_cxx"], - "@score_bazel_platforms//settings:x86_64-qnx8": ["-V12.2.0,gcc_ntox86_64_cxx"], - }), -) - -cc_feature( - name = "gcc_version_flags", - args = [ - ":qnx_gcc_version_compile_args", - ":qnx_gcc_version_link_args", - ], - feature_name = "gcc_version_flags", - visibility = ["//visibility:public"], -) diff --git a/features/custom/qnx/gcc_version_flags/feature.bzl b/features/custom/qnx/gcc_version_flags/feature.bzl new file mode 100644 index 0000000..ded7782 --- /dev/null +++ b/features/custom/qnx/gcc_version_flags/feature.bzl @@ -0,0 +1,47 @@ +# ******************************************************************************* +# Copyright (c) 2026 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 +# ******************************************************************************* + +load("@rules_cc//cc/toolchains:args.bzl", "cc_args") +load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature") + +def make_gcc_version_flags_feature(cpu, version): + """Creates a feature pinning qcc to the toolchain instance's resolved GCC version. + + Args: + cpu: str, normalized target CPU as used in qcc's `gcc_nto*` identifiers + (e.g. "aarch64le", "x86_64"). + version: str, GCC version resolved for this toolchain instance. + + Returns: + A feature definition for the gcc_version_flags toolchain. + """ + cc_args( + name = "gcc_version_compile_args", + actions = ["@rules_cc//cc/toolchains/actions:compile_actions"], + args = ["-V{version},gcc_nto{cpu}".format(version = version, cpu = cpu)], + ) + + cc_args( + name = "gcc_version_link_args", + actions = ["@rules_cc//cc/toolchains/actions:link_actions"], + args = ["-V{version},gcc_nto{cpu}_cxx".format(version = version, cpu = cpu)], + ) + + cc_feature( + name = "gcc_version_flags", + feature_name = "gcc_version_flags", + args = [ + ":gcc_version_compile_args", + ":gcc_version_link_args", + ], + ) diff --git a/features/custom/qnx/make_cc_features.bzl b/features/custom/qnx/make_cc_features.bzl index 3f5196f..0f08359 100644 --- a/features/custom/qnx/make_cc_features.bzl +++ b/features/custom/qnx/make_cc_features.bzl @@ -37,7 +37,7 @@ _QNX_FEATURES = [ ("@score_bazel_cpp_toolchains//features/native/markers:dbg", False), # Bazel auto-toggles via -c dbg ("@score_bazel_cpp_toolchains//features/native/markers:no_legacy_features", True), ("@score_bazel_cpp_toolchains//features/native/unfiltered_compile_flags", True), - ("@score_bazel_cpp_toolchains//features/custom/qnx/gcc_version_flags", True), + (":gcc_version_flags", True), # per-instance, see templates/BUILD.template ("@score_bazel_cpp_toolchains//features/native/default_compile_flags", True), ("@score_bazel_cpp_toolchains//features/native/random_seed", True), ("@score_bazel_cpp_toolchains//features/native/include_paths", True), diff --git a/rules/common.bzl b/rules/common.bzl index 6c5b3c2..cb5f610 100644 --- a/rules/common.bzl +++ b/rules/common.bzl @@ -24,6 +24,13 @@ SDP_VERSION_MAPPING = { "8.0.4": "8.0.0", } +# GCC version bundled with each QNX SDP major release, keyed by SDP major version. +# Used to default/validate a toolchain's `version` attribute from its `sdp_version`. +QNX_SDP_TO_GCC_VERSION = { + "7": "8.3.0", + "8": "12.2.0", +} + def get_flag_strings(flags): """Converts a list of warning flags into a Bazel flag group representation. diff --git a/rules/gcc.bzl b/rules/gcc.bzl index 21321d8..61c6c58 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -97,7 +97,8 @@ def get_custom_cc_features_qnx(rctx, canonical_pkg_name): # TODO: Once Bazel enables label resolution in cc_args' env, we can use labels instead of resolved paths. - custom_load = """load("@score_bazel_cpp_toolchains//features/custom/qnx/sdp_env:feature.bzl", "make_sdp_env_feature")""" + custom_load = """load("@score_bazel_cpp_toolchains//features/custom/qnx/sdp_env:feature.bzl", "make_sdp_env_feature") +load("@score_bazel_cpp_toolchains//features/custom/qnx/gcc_version_flags:feature.bzl", "make_gcc_version_flags_feature")""" custom_features = """ make_sdp_env_feature( host_dir = "{host_dir}", @@ -106,12 +107,19 @@ make_sdp_env_feature( license_info_variable = "{license_info_variable}", license_info_value = "{license_info_value}", ) + +make_gcc_version_flags_feature( + cpu = "{gcc_version_cpu}", + version = "{gcc_version}", +) """.format( host_dir = "/proc/self/cwd/external/{canonical_pkg}/host/linux/x86_64".format(canonical_pkg = canonical_pkg_name), target_dir = "/proc/self/cwd/external/{canonical_pkg}/target/qnx".format(canonical_pkg = canonical_pkg_name), license_path = rctx.attr.license_path, license_info_variable = rctx.attr.license_info_variable, license_info_value = rctx.attr.license_info_value, + gcc_version_cpu = _normalize_cpu(rctx.attr.tc_cpu), + gcc_version = rctx.attr.gcc_version, ) return custom_load, custom_features diff --git a/tests/MODULE.bazel.lock b/tests/MODULE.bazel.lock index db2f720..49e5692 100644 --- a/tests/MODULE.bazel.lock +++ b/tests/MODULE.bazel.lock @@ -1370,7 +1370,7 @@ }, "@@score_bazel_cpp_toolchains+//extensions:gcc.bzl%gcc": { "general": { - "bzlTransitiveDigest": "Nj8U7NPFVcEx27yeDyoy2LT/XDM9z/IzPO+KImFDYXI=", + "bzlTransitiveDigest": "kpNtv258enHNIv8H4ZsBTsAhtXDDdI3DWP1nLqoIxYY=", "usagesDigest": "3BxGiccNNlFja9y/vd4+7TlTcgtjWIybmed7P06K9QM=", "recordedFileInputs": {}, "recordedDirentsInputs": {},