From 380472463b4826133ab4040f241c4050bac46220 Mon Sep 17 00:00:00 2001 From: Nikola Radakovic Date: Mon, 9 Feb 2026 11:27:18 +0000 Subject: [PATCH 1/3] Add option for base platform tc definition --- examples/.bazelrc | 4 ++-- examples/MODULE.bazel | 1 + extensions/gcc.bzl | 10 ++++++++-- rules/gcc.bzl | 18 +++++++++++++++--- templates/BUILD.template | 9 +++++---- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/examples/.bazelrc b/examples/.bazelrc index ee241d5..0a9e4d5 100644 --- a/examples/.bazelrc +++ b/examples/.bazelrc @@ -24,19 +24,19 @@ build:shared --host_platform=@score_bazel_platforms//:x86_64-linux # Config dedicated to host platform CPU:x86_64 and OS:Linux # ------------------------------------------------------------------------------- build:x86_64-linux --config=shared -build:x86_64-linux --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix # ------------------------------------------------------------------------------- # Different toolchain configuration for x86_64-linux # ------------------------------------------------------------------------------- build:host_config_1 --config=x86_64-linux -build:host_config_1 --extra_toolchains=@score_gcc_toolchain//:x86_64-linux-gcc_12.2.0-posix +build:host_config_1 --extra_toolchains=@score_gcc_toolchain//:x86_64-linux build:host_config_1 --features=use_pthread # ------------------------------------------------------------------------------- # Different toolchain configuration for x86_64-linux # ------------------------------------------------------------------------------- build:host_config_2 --config=x86_64-linux +build:host_config_2 --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix build:host_config_2 --extra_toolchains=@my_toolchain//:x86_64-linux-gcc_12.2.0-posix # ------------------------------------------------------------------------------- diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 3aadc2e..0961408 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -65,6 +65,7 @@ gcc.toolchain( target_os = "linux", version = "12.2.0", use_default_package = True, + use_base_constraints_only = True, ) # ******************************************************************************* diff --git a/extensions/gcc.bzl b/extensions/gcc.bzl index 2373a32..94ee080 100644 --- a/extensions/gcc.bzl +++ b/extensions/gcc.bzl @@ -62,12 +62,16 @@ _attrs_tc = { ), "use_system_toolchain": attr.bool( default = False, - doc = "TBD", + doc = "Flag for toolchain creation to use host installed binaries. Not yet supported!", + ), + "use_base_constraints_only": attr.bool( + default = False, + doc = "Experimental. Attribute for flag toolchain creation to use only base platform constraints. Limits toolchain registration to 1 per base platform definition." ), "runtime_ecosystem": attr.string( default = "posix", mandatory = False, - doc = "TBD", + doc = "Attribute for identifing the system-level runtime environment a binary or target is built to run in.", ), "target_cpu": attr.string( mandatory = True, @@ -172,6 +176,7 @@ def _get_toolchains(tags): "sdp_to_link": tag.sdp_to_link, "use_default_package": tag.use_default_package, "use_system_toolchain": tag.use_system_toolchain, + "use_base_constraints_only": tag.use_base_constraints_only, "tc_extra_compile_flags": tag.extra_compile_flags, "tc_extra_c_compile_flags": tag.extra_c_compile_flags, "tc_extra_cxx_compile_flags": tag.extra_cxx_compile_flags, @@ -285,6 +290,7 @@ def _impl(mctx): gcc_version = toolchain_info["tc_version"], cc_toolchain_config = toolchain_info["cc_toolchain_config"], cc_toolchain_flags = toolchain_info["cc_toolchain_flags"], + use_base_constraints_only = toolchain_info["use_base_constraints_only"] ) gcc = module_extension( diff --git a/rules/gcc.bzl b/rules/gcc.bzl index 69263f8..daf8368 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -101,6 +101,16 @@ def _impl(rctx): else: fail("Unsupported OS detected!") + tc_identifier_short_1 = "" + tc_identifier_long_1 = "[]" + tc_identifier_short_2 = "" + tc_identifier_long_2 = "[]" + if not rctx.attr.use_base_constraints_only: + tc_identifier_short_1 = "-{}".format(tc_identifier) + tc_identifier_long_1 = "[\"@score_bazel_platforms//version:{}\"]".format(tc_identifier) + tc_identifier_short_2 = "-{}".format(rctx.attr.tc_runtime_ecosystem) + tc_identifier_long_2 = "[\"@score_bazel_platforms//runtime_es:{}\"]".format(rctx.attr.tc_runtime_ecosystem) + rctx.template( "BUILD", rctx.attr._cc_toolchain_build, @@ -110,8 +120,10 @@ def _impl(rctx): "%{tc_cpu}": rctx.attr.tc_cpu, "%{tc_os}": rctx.attr.tc_os, "%{tc_version}": rctx.attr.gcc_version, - "%{tc_identifier}": tc_identifier, - "%{tc_runtime_es}": rctx.attr.tc_runtime_ecosystem, + "%{tc_identifier_short_1}": tc_identifier_short_1, + "%{tc_identifier_short_2}": tc_identifier_short_2, + "%{tc_identifier_long_1}": tc_identifier_long_1, + "%{tc_identifier_long_2}": tc_identifier_long_2, }, ) @@ -136,7 +148,6 @@ def _impl(rctx): extra_cxx_compile_flags = get_flag_groups(replace_placeholder(rctx.attr.extra_cxx_compile_flags)) extra_link_flags = get_flag_groups(replace_placeholder(rctx.attr.extra_link_flags)) - template_dict = { "%{tc_version}": rctx.attr.gcc_version, "%{tc_identifier}": "gcc", @@ -212,6 +223,7 @@ gcc_toolchain = repository_rule( "cc_toolchain_flags": attr.label( doc = "Path to the Bazel BUILD file template for the toolchain.", ), + "use_base_constraints_only": attr.bool(doc="Boolean flag to state only base constraints should be used for toolchain compatibility definition"), "_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/BUILD.template b/templates/BUILD.template index fbfad1b..e2c4558 100644 --- a/templates/BUILD.template +++ b/templates/BUILD.template @@ -37,8 +37,11 @@ cc_toolchain( toolchain_config = ":cc_toolchain_config", ) +# "@score_bazel_platforms//version:%{tc_runtime_es}", +# "@score_bazel_platforms//version:%{tc_identifier}", + toolchain( - name = "%{tc_cpu}-%{tc_os}-%{tc_identifier}-%{tc_runtime_es}", + name = "%{tc_cpu}-%{tc_os}%{tc_identifier_short_1}%{tc_identifier_short_2}", exec_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:linux", @@ -46,9 +49,7 @@ toolchain( target_compatible_with = [ "@platforms//cpu:%{tc_cpu}", "@platforms//os:%{tc_os}", - "@score_bazel_platforms//runtime_es:%{tc_runtime_es}", - "@score_bazel_platforms//version:%{tc_identifier}", - ], + ] + %{tc_identifier_long_1} + %{tc_identifier_long_2}, toolchain = ":cc_toolchain", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", visibility = [ From 9da8f4fde3dd7e482d6e953241d834eea756d10a Mon Sep 17 00:00:00 2001 From: Nikola Radakovic Date: Tue, 10 Feb 2026 14:44:32 +0100 Subject: [PATCH 2/3] Add SDK as constraint definition for toolchain --- extensions/gcc.bzl | 11 +++++++++-- rules/gcc.bzl | 14 ++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/extensions/gcc.bzl b/extensions/gcc.bzl index 94ee080..1dee121 100644 --- a/extensions/gcc.bzl +++ b/extensions/gcc.bzl @@ -104,10 +104,15 @@ _attrs_tc = { mandatory = False, doc = "QNX License info variable.", ), + "sdk_version": attr.string( + default = "", + mandatory = False, + doc = "SDK version info variable.", + ), "sdp_version": attr.string( default = "8.0.0", mandatory = False, - doc = "Version of the SDP package.", + doc = "Version of the QNX SDP package.", ), "license_path": attr.string( default = "/opt/score_qnx/license/licenses", @@ -182,6 +187,7 @@ def _get_toolchains(tags): "tc_extra_cxx_compile_flags": tag.extra_cxx_compile_flags, "tc_extra_link_flags": tag.extra_link_flags, "sdp_version": tag.sdp_version, + "sdk_version": tag.sdk_version, "tc_license_info_variable": tag.license_info_variable, "tc_license_info_url": tag.license_info_url, "tc_license_path": tag.license_path, @@ -282,12 +288,13 @@ def _impl(mctx): license_info_value = toolchain_info["tc_license_info_url"], license_path = toolchain_info["tc_license_path"], sdp_version = toolchain_info["sdp_version"], + tc_sdk_version = toolchain_info["sdk_version"], tc_cpu = toolchain_info["tc_cpu"], tc_os = toolchain_info["tc_os"], tc_pkg_repo = toolchain_info["sdp_to_link"], tc_system_toolchain = toolchain_info["use_system_toolchain"], tc_runtime_ecosystem = toolchain_info["tc_runtime_ecosystem"], - gcc_version = toolchain_info["tc_version"], + tc_gcc_version = toolchain_info["tc_version"], cc_toolchain_config = toolchain_info["cc_toolchain_config"], cc_toolchain_flags = toolchain_info["cc_toolchain_flags"], use_base_constraints_only = toolchain_info["use_base_constraints_only"] diff --git a/rules/gcc.bzl b/rules/gcc.bzl index daf8368..769cc3b 100644 --- a/rules/gcc.bzl +++ b/rules/gcc.bzl @@ -90,7 +90,9 @@ def _impl(rctx): Args: rctx: The repository context. """ - tc_identifier = "gcc_{}".format(rctx.attr.gcc_version) + tc_identifier = "gcc_{}".format(rctx.attr.tc_gcc_version) + if rctx.attr.tc_sdk_version != "": + tc_identifier = "sdk_{}".format(rctx.attr.tc_sdk_version) if rctx.attr.tc_os == "qnx": tc_identifier = "sdp_{}".format(rctx.attr.sdp_version) @@ -101,6 +103,9 @@ def _impl(rctx): else: fail("Unsupported OS detected!") + # The reason for this implementation is to provide support for toolchains defined only by based constraints. + # By default `cpu-os-version-runtime_os` is constraint set used for toolchain target compatible fileds but + # in case of base platform constraints, only cpu and os are involved in toolchain definition. tc_identifier_short_1 = "" tc_identifier_long_1 = "[]" tc_identifier_short_2 = "" @@ -119,7 +124,7 @@ def _impl(rctx): "%{tc_pkg_repo}": rctx.attr.tc_pkg_repo, "%{tc_cpu}": rctx.attr.tc_cpu, "%{tc_os}": rctx.attr.tc_os, - "%{tc_version}": rctx.attr.gcc_version, + "%{tc_version}": rctx.attr.tc_gcc_version, "%{tc_identifier_short_1}": tc_identifier_short_1, "%{tc_identifier_short_2}": tc_identifier_short_2, "%{tc_identifier_long_1}": tc_identifier_long_1, @@ -149,7 +154,7 @@ def _impl(rctx): extra_link_flags = get_flag_groups(replace_placeholder(rctx.attr.extra_link_flags)) template_dict = { - "%{tc_version}": rctx.attr.gcc_version, + "%{tc_version}": rctx.attr.tc_gcc_version, "%{tc_identifier}": "gcc", "%{tc_cpu}": "aarch64le" if rctx.attr.tc_cpu == "aarch64" else rctx.attr.tc_cpu, "%{tc_runtime_es}": rctx.attr.tc_runtime_ecosystem, @@ -206,7 +211,8 @@ gcc_toolchain = repository_rule( "tc_pkg_repo": attr.string(doc="The label name of toolchain tarbal."), "tc_cpu": attr.string(doc="Target platform CPU."), "tc_os": attr.string(doc="Target platform OS."), - "gcc_version": attr.string(doc="GCC version number"), + "tc_gcc_version": attr.string(doc="GCC version number"), + "tc_sdk_version": attr.string(doc="SDK version number"), "extra_compile_flags": attr.string_list(doc="Extra/Additional compile flags."), "extra_c_compile_flags": attr.string_list(doc="Extra/Additional C-specific compile flags."), "extra_cxx_compile_flags": attr.string_list(doc="Extra/Additional C++-specific compile flags."), From de734b0cbcb7b0765d4555242294c068432ba66d Mon Sep 17 00:00:00 2001 From: Nikola Radakovic Date: Tue, 10 Feb 2026 15:06:33 +0100 Subject: [PATCH 3/3] Add more use cases in example --- examples/.bazelrc | 18 ++++++++++++------ examples/MODULE.bazel | 14 +++++++++++++- examples/test.sh | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 7 deletions(-) create mode 100755 examples/test.sh diff --git a/examples/.bazelrc b/examples/.bazelrc index 0a9e4d5..0dce012 100644 --- a/examples/.bazelrc +++ b/examples/.bazelrc @@ -29,15 +29,22 @@ build:x86_64-linux --config=shared # Different toolchain configuration for x86_64-linux # ------------------------------------------------------------------------------- build:host_config_1 --config=x86_64-linux -build:host_config_1 --extra_toolchains=@score_gcc_toolchain//:x86_64-linux -build:host_config_1 --features=use_pthread +build:host_config_1 --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix +build:host_config_1 --extra_toolchains=@score_gcc_toolchain//:x86_64-linux-gcc_12.2.0-posix # ------------------------------------------------------------------------------- -# Different toolchain configuration for x86_64-linux +# Different toolchain configuration for x86_64-linux (base platforms only) # ------------------------------------------------------------------------------- build:host_config_2 --config=x86_64-linux -build:host_config_2 --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix -build:host_config_2 --extra_toolchains=@my_toolchain//:x86_64-linux-gcc_12.2.0-posix +build:host_config_2 --extra_toolchains=@score_gcc_toolchain_bp_only//:x86_64-linux +build:host_config_2 --features=use_pthread + +# ------------------------------------------------------------------------------- +# Different toolchain configuration for x86_64-linux +# ------------------------------------------------------------------------------- +build:host_config_3 --config=x86_64-linux +build:host_config_3 --platforms=@score_bazel_platforms//:x86_64-linux-gcc_12.2.0-posix +build:host_config_3 --extra_toolchains=@my_toolchain//:x86_64-linux-gcc_12.2.0-posix # ------------------------------------------------------------------------------- # Config dedicated to target platform CPU:aarch64 and OS:linux @@ -51,7 +58,6 @@ build:aarch64-linux --platforms=@score_bazel_platforms//:aarch64-linux-gcc_12.2. build:target_config_3 --config=aarch64-linux build:target_config_3 --extra_toolchains=@score_aarch64_gcc_toolchain//:aarch64-linux-gcc_12.2.0-posix - # ------------------------------------------------------------------------------- # Config dedicated to target platform CPU:x86_64 and OS:QNX # ------------------------------------------------------------------------------- diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 0961408..a3936f7 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -65,11 +65,22 @@ gcc.toolchain( target_os = "linux", version = "12.2.0", use_default_package = True, +) + +# ******************************************************************************* +# Setting GCC (CPU:x86_64|OS:Linux) +# ******************************************************************************* +gcc.toolchain( + name = "score_gcc_toolchain_bp_only", + target_cpu = "x86_64", + target_os = "linux", + version = "12.2.0", + use_default_package = True, use_base_constraints_only = True, ) # ******************************************************************************* -# Setting default GCC (CPU:aarch64|OS:Linux|V:12.2.0|ES:posix) +# Setting GCC (CPU:aarch64|OS:Linux|V:12.2.0|ES:posix) # ******************************************************************************* gcc.toolchain( name = "score_aarch64_gcc_toolchain", @@ -150,6 +161,7 @@ gcc.toolchain( use_repo( gcc, "score_gcc_toolchain", + "score_gcc_toolchain_bp_only", "score_aarch64_gcc_toolchain", "my_toolchain", "score_qcc_toolchain", diff --git a/examples/test.sh b/examples/test.sh new file mode 100755 index 0000000..9b71d6c --- /dev/null +++ b/examples/test.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +echo -e "Testing 'host_config_1' ..." +bazel build --config host_config_1 //:main_cpp +bazel clean +echo -e "Testing 'host_config_2' ..." +bazel build --config host_config_2 //:main_cpp +bazel clean +echo -e "Testing 'host_config_3' ..." +bazel build --config host_config_3 //:main_cpp +bazel clean +echo -e "Testing 'target_config_1' ..." +bazel build --config target_config_1 //:main_cpp +bazel clean +echo -e "Testing 'target_config_2' ..." +bazel build --config target_config_2 //:main_cpp +bazel clean +echo -e "Testing 'target_config_3' ..." +bazel build --config target_config_3 //:main_cpp +bazel clean