-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pw_toolchain: Add Bazel mechansim for clang/gcc-specific flags
Adds sentinel features for compiler flavor and a rule that can extract properties from the current toolchain for use in `select()` statements across build files. Bug: b/361229275 Change-Id: Ibc4645a082d75ddda6650dbe75123ed2ed11ce0e Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/238429 Presubmit-Verified: CQ Bot Account <[email protected]> Lint: Lint 🤖 <[email protected]> Commit-Queue: Auto-Submit <[email protected]> Pigweed-Auto-Submit: Armando Montanez <[email protected]> Reviewed-by: Ted Pudlik <[email protected]>
- Loading branch information
1 parent
9fe59f4
commit 0125f4a
Showing
10 changed files
with
341 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2024 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
|
||
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
# All of the toolchain features enumerated in this build file are sentinel | ||
# features. By nature, they do not provide any flags. Instead, these features | ||
# are used to guide other behaviors throughout a toolchain. | ||
|
||
cc_feature( | ||
name = "compiler_is_clang", | ||
feature_name = "compiler_is_clang", | ||
) | ||
|
||
cc_feature( | ||
name = "compiler_is_gcc", | ||
feature_name = "compiler_is_gcc", | ||
) | ||
|
||
cc_feature( | ||
name = "linker_is_clang", | ||
feature_name = "linker_is_clang", | ||
) | ||
|
||
cc_feature( | ||
name = "linker_is_gcc", | ||
feature_name = "linker_is_gcc", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright 2024 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
|
||
load(":pw_cc_toolchain_feature_is_enabled.bzl", "pw_cc_toolchain_feature_is_enabled") | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "compiler_is_clang", | ||
feature_name = "compiler_is_clang", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "compiler_is_clang-cl", | ||
feature_name = "compiler_is_clang-cl", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "compiler_is_gcc", | ||
feature_name = "compiler_is_gcc", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "compiler_is_mingw-gcc", | ||
feature_name = "compiler_is_mingw-gcc", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "compiler_is_msvc-cl", | ||
feature_name = "compiler_is_msvc-cl", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "linker_is_clang", | ||
feature_name = "linker_is_clang", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "linker_is_clang-cl", | ||
feature_name = "linker_is_clang-cl", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "linker_is_gcc", | ||
feature_name = "linker_is_gcc", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "linker_is_mingw-gcc", | ||
feature_name = "linker_is_mingw-gcc", | ||
) | ||
|
||
pw_cc_toolchain_feature_is_enabled( | ||
name = "linker_is_msvc-cl", | ||
feature_name = "linker_is_msvc-cl", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2024 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""Helpers for expressing compiler-specific build file logic.""" | ||
|
||
def if_compiler_is_clang(then, *, otherwise): | ||
return select({ | ||
"//pw_toolchain/cc/current_toolchain:compiler_is_clang": then, | ||
"@rules_cc//cc/compiler:clang": then, | ||
"//conditions:default": otherwise, | ||
}) | ||
|
||
def if_compiler_is_gcc(then, *, otherwise): | ||
return select({ | ||
"//pw_toolchain/cc/current_toolchain:compiler_is_gcc": then, | ||
"@rules_cc//cc/compiler:gcc": then, | ||
"//conditions:default": otherwise, | ||
}) | ||
|
||
def if_linker_is_clang(then, *, otherwise): | ||
return select({ | ||
"//pw_toolchain/cc/current_toolchain:linker_is_clang": then, | ||
"@rules_cc//cc/compiler:clang": then, | ||
"//conditions:default": otherwise, | ||
}) | ||
|
||
def if_linker_is_gcc(then, *, otherwise): | ||
return select({ | ||
"//pw_toolchain/cc/current_toolchain:linker_is_gcc": then, | ||
"@rules_cc//cc/compiler:gcc": then, | ||
"//conditions:default": otherwise, | ||
}) |
86 changes: 86 additions & 0 deletions
86
pw_toolchain/cc/current_toolchain/pw_cc_toolchain_feature_is_enabled.bzl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright 2024 The Pigweed Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
# use this file except in compliance with the License. You may obtain a copy of | ||
# the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations under | ||
# the License. | ||
"""A rule for letting enabled toolchain features drive build configuration.""" | ||
|
||
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") | ||
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") | ||
|
||
def _cc_toolchain_feature_is_enabled_impl(ctx): | ||
toolchain = find_cpp_toolchain(ctx) | ||
feature_configuration = cc_common.configure_features( | ||
ctx = ctx, | ||
cc_toolchain = toolchain, | ||
) | ||
val = cc_common.is_enabled( | ||
feature_configuration = feature_configuration, | ||
feature_name = ctx.attr.feature_name, | ||
) | ||
return [ | ||
config_common.FeatureFlagInfo(value = str(val)), | ||
BuildSettingInfo(value = val), | ||
] | ||
|
||
_cc_toolchain_feature_is_enabled = rule( | ||
implementation = _cc_toolchain_feature_is_enabled_impl, | ||
attrs = { | ||
"feature_name": attr.string( | ||
mandatory = True, | ||
doc = "The feature name to match against", | ||
), | ||
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), | ||
}, | ||
doc = """Extracts a matching, enabled feature from the current C/C++ toolchain. | ||
This rule is a bridge that allows feature presence/absence to be communicated | ||
from a toolchain configuration as if it was just a `bool_flag`. | ||
This allows toolchain features to become branches in `select()` statements: | ||
```py | ||
_cc_toolchain_feature_is_enabled( | ||
name = "toolchain_flavor_chocolate", | ||
feature_name = "toolchain_flavor_chocolate", | ||
) | ||
config_setting( | ||
name = "chocolate", | ||
flag_values = {":toolchain_flavor_chocolate": "True"}, | ||
) | ||
cc_library( | ||
name = "libfoo", | ||
copts = select({ | ||
":chocolate": ["-fno-dog-food"], | ||
"//conditions:default": [], | ||
}), | ||
srcs = ["foo.cc"], | ||
) | ||
``` | ||
""", | ||
toolchains = use_cpp_toolchain(), | ||
fragments = ["cpp"], | ||
) | ||
|
||
def pw_cc_toolchain_feature_is_enabled(*, name, feature_name, **kwargs): | ||
_cc_toolchain_query_name = name + ".value" | ||
_cc_toolchain_feature_is_enabled( | ||
name = _cc_toolchain_query_name, | ||
feature_name = feature_name, | ||
**kwargs | ||
) | ||
|
||
native.config_setting( | ||
name = name, | ||
flag_values = {":{}".format(_cc_toolchain_query_name): "True"}, | ||
) |
Oops, something went wrong.