From 259b4480c877e748b5f206d20f96117e6dfb2075 Mon Sep 17 00:00:00 2001 From: Srikanth Arunachalam Date: Sat, 10 Jan 2026 20:34:37 +0000 Subject: [PATCH] Expose compiler constraint values in public API This change addresses issue #330 by making the CC compiler constraint setting and constraint values publicly accessible in //cc/compiler. Previously, users who needed to define platforms constrained by compiler type had to reference @rules_cc//cc/private/toolchain:clang-cl (and similar), which: 1. Exposes private implementation details 2. Behaves inconsistently between Bazel 7 and 8 with bzlmod 3. Is not documented as the canonical location Changes: - Add constraint_setting "cc_compiler" to //cc/compiler - Add constraint_value targets (*_constraint) for each compiler type - Convert cc/private/toolchain constraints to aliases (backward compat) - Update BUILD.windows.tpl to use the new public paths Users can now use the public API: platform( name = "windows_clang_cl", constraint_values = [ "@platforms//os:windows", "@rules_cc//cc/compiler:clang-cl_constraint", ], ) Fixes #330 --- cc/compiler/BUILD | 48 ++++++++++++++++++++++++++ cc/private/toolchain/BUILD | 42 ++++++++++++++-------- cc/private/toolchain/BUILD.windows.tpl | 4 +-- 3 files changed, 78 insertions(+), 16 deletions(-) diff --git a/cc/compiler/BUILD b/cc/compiler/BUILD index 6db7145f8..46dfbe3f3 100644 --- a/cc/compiler/BUILD +++ b/cc/compiler/BUILD @@ -45,6 +45,54 @@ load("//cc/toolchains:compiler_flag.bzl", "compiler_flag") package(default_visibility = ["//visibility:public"]) +# Constraint setting and values for platform-based toolchain selection. +# Use these in your platform() definitions to constrain by compiler type. +# +# Example: +# platform( +# name = "windows_clang_cl", +# constraint_values = [ +# "@platforms//os:windows", +# "@rules_cc//cc/compiler:clang-cl", +# ], +# ) + +constraint_setting( + name = "cc_compiler", + # This is the canonical location for compiler constraint values. + # The values in cc/private/toolchain are aliases to these for backward compatibility. +) + +constraint_value( + name = "clang_constraint", + constraint_setting = ":cc_compiler", +) + +constraint_value( + name = "gcc_constraint", + constraint_setting = ":cc_compiler", +) + +constraint_value( + name = "msvc_constraint", + constraint_setting = ":cc_compiler", +) + +constraint_value( + name = "clang-cl_constraint", + constraint_setting = ":cc_compiler", +) + +constraint_value( + name = "mingw_constraint", + constraint_setting = ":cc_compiler", +) + +constraint_value( + name = "msys_constraint", + constraint_setting = ":cc_compiler", +) + licenses(["notice"]) compiler_flag(name = "compiler") diff --git a/cc/private/toolchain/BUILD b/cc/private/toolchain/BUILD index 966da04f6..ad181b8a9 100644 --- a/cc/private/toolchain/BUILD +++ b/cc/private/toolchain/BUILD @@ -21,37 +21,51 @@ package(default_visibility = ["//visibility:public"]) licenses(["notice"]) # Apache 2.0 -# It is frequently necessary to constrain platforms based on the cc compiler type. -constraint_setting(name = "cc_compiler") +# DEPRECATED: Use //cc/compiler:cc_compiler and //cc/compiler:_constraint instead. +# These aliases are kept for backward compatibility. +# +# Prefer using the public API in //cc/compiler: +# @rules_cc//cc/compiler:cc_compiler (constraint_setting) +# @rules_cc//cc/compiler:clang_constraint +# @rules_cc//cc/compiler:gcc_constraint +# @rules_cc//cc/compiler:msvc_constraint +# @rules_cc//cc/compiler:clang-cl_constraint +# @rules_cc//cc/compiler:mingw_constraint +# @rules_cc//cc/compiler:msys_constraint -constraint_value( +alias( + name = "cc_compiler", + actual = "//cc/compiler:cc_compiler", +) + +alias( name = "clang", - constraint_setting = ":cc_compiler", + actual = "//cc/compiler:clang_constraint", ) -constraint_value( +alias( name = "gcc", - constraint_setting = ":cc_compiler", + actual = "//cc/compiler:gcc_constraint", ) -constraint_value( +alias( name = "msvc", - constraint_setting = ":cc_compiler", + actual = "//cc/compiler:msvc_constraint", ) -constraint_value( +alias( name = "clang-cl", - constraint_setting = ":cc_compiler", + actual = "//cc/compiler:clang-cl_constraint", ) -constraint_value( +alias( name = "mingw", - constraint_setting = ":cc_compiler", + actual = "//cc/compiler:mingw_constraint", ) -constraint_value( +alias( name = "msys", - constraint_setting = ":cc_compiler", + actual = "//cc/compiler:msys_constraint", ) cc_library( diff --git a/cc/private/toolchain/BUILD.windows.tpl b/cc/private/toolchain/BUILD.windows.tpl index afbc00d98..3813cac14 100644 --- a/cc/private/toolchain/BUILD.windows.tpl +++ b/cc/private/toolchain/BUILD.windows.tpl @@ -660,7 +660,7 @@ toolchain( exec_compatible_with = [ "@platforms//cpu:x86_64", "@platforms//os:windows", - "@rules_cc//cc/private/toolchain:clang-cl", + "@rules_cc//cc/compiler:clang-cl_constraint", ], target_compatible_with = [ "@platforms//cpu:x86_64", @@ -732,7 +732,7 @@ toolchain( name = "cc-toolchain-arm64_windows-clang-cl", exec_compatible_with = [ "@platforms//os:windows", - "@rules_cc//cc/private/toolchain:clang-cl", + "@rules_cc//cc/compiler:clang-cl_constraint", ], target_compatible_with = [ "@platforms//cpu:arm64",