diff options
Diffstat (limited to 'cc/private/rules_impl')
-rw-r--r-- | cc/private/rules_impl/BUILD | 18 | ||||
-rw-r--r-- | cc/private/rules_impl/cc_flags_supplier.bzl | 35 | ||||
-rw-r--r-- | cc/private/rules_impl/cc_flags_supplier_lib.bzl | 79 | ||||
-rw-r--r-- | cc/private/rules_impl/compiler_flag.bzl | 29 | ||||
-rw-r--r-- | cc/private/rules_impl/native.bzl | 34 |
5 files changed, 195 insertions, 0 deletions
diff --git a/cc/private/rules_impl/BUILD b/cc/private/rules_impl/BUILD new file mode 100644 index 0000000..dc74dfe --- /dev/null +++ b/cc/private/rules_impl/BUILD @@ -0,0 +1,18 @@ +package(default_visibility = ["//visibility:public"]) + +licenses(["notice"]) # Apache 2.0 + +filegroup( + name = "bzl_srcs", + srcs = glob([ + "**/*.bzl", + ]), +) + +filegroup( + name = "srcs", + srcs = glob([ + "**/*.bzl", + "**/BUILD", + ]), +) diff --git a/cc/private/rules_impl/cc_flags_supplier.bzl b/cc/private/rules_impl/cc_flags_supplier.bzl new file mode 100644 index 0000000..474c7ce --- /dev/null +++ b/cc/private/rules_impl/cc_flags_supplier.bzl @@ -0,0 +1,35 @@ +# Copyright 2018 The Bazel Authors. All rights reserved. +# +# 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 +# +# http://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. +"""Rule that provides the CC_FLAGS Make variable.""" + +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") +load("//cc:action_names.bzl", "CC_FLAGS_MAKE_VARIABLE_ACTION_NAME") +load("//cc/private/rules_impl:cc_flags_supplier_lib.bzl", "build_cc_flags") + +def _cc_flags_supplier_impl(ctx): + cc_toolchain = find_cpp_toolchain(ctx) + cc_flags = build_cc_flags(ctx, cc_toolchain, CC_FLAGS_MAKE_VARIABLE_ACTION_NAME) + variables = platform_common.TemplateVariableInfo({ + "CC_FLAGS": cc_flags, + }) + return [variables] + +cc_flags_supplier = rule( + implementation = _cc_flags_supplier_impl, + attrs = { + "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + }, + toolchains = use_cpp_toolchain(), + fragments = ["cpp"], +) diff --git a/cc/private/rules_impl/cc_flags_supplier_lib.bzl b/cc/private/rules_impl/cc_flags_supplier_lib.bzl new file mode 100644 index 0000000..4b0782a --- /dev/null +++ b/cc/private/rules_impl/cc_flags_supplier_lib.bzl @@ -0,0 +1,79 @@ +# Copyright 2018 The Bazel Authors. All rights reserved. +# +# 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 +# +# http://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. +"""Library of functions that provide the CC_FLAGS Make variable.""" + +# This should match the logic in CcCommon.computeCcFlags: +def build_cc_flags(ctx, cc_toolchain, action_name): + """Determine the value for CC_FLAGS based on the given toolchain. + + Args: + ctx: The rule context. + cc_toolchain: CcToolchainInfo instance. + action_name: Name of the action. + Returns: + string containing flags separated by a space. + """ + + # Get default cc flags from toolchain's make_variables. + legacy_cc_flags = cc_common.legacy_cc_flags_make_variable_do_not_use( + cc_toolchain = cc_toolchain, + ) + + # Determine the sysroot flag. + sysroot_cc_flags = _from_sysroot(cc_toolchain) + + # Flags from feature config. + feature_config_cc_flags = _from_features(ctx, cc_toolchain, action_name) + + # Combine the different sources, but only add the sysroot flag if nothing + # else adds sysroot. + # If added, it must appear before the feature config flags. + cc_flags = [] + if legacy_cc_flags: + cc_flags.append(legacy_cc_flags) + if sysroot_cc_flags and not _contains_sysroot(feature_config_cc_flags): + cc_flags.append(sysroot_cc_flags) + cc_flags.extend(feature_config_cc_flags) + + return " ".join(cc_flags) + +def _contains_sysroot(flags): + for flag in flags: + if "--sysroot=" in flag: + return True + return False + +def _from_sysroot(cc_toolchain): + sysroot = cc_toolchain.sysroot + if sysroot: + return "--sysroot=%s" % sysroot + else: + return None + +def _from_features(ctx, cc_toolchain, action_name): + feature_configuration = cc_common.configure_features( + ctx = ctx, + cc_toolchain = cc_toolchain, + requested_features = ctx.features, + unsupported_features = ctx.disabled_features, + ) + + variables = cc_common.empty_variables() + + cc_flags = cc_common.get_memory_inefficient_command_line( + feature_configuration = feature_configuration, + action_name = action_name, + variables = variables, + ) + return cc_flags diff --git a/cc/private/rules_impl/compiler_flag.bzl b/cc/private/rules_impl/compiler_flag.bzl new file mode 100644 index 0000000..ebbac94 --- /dev/null +++ b/cc/private/rules_impl/compiler_flag.bzl @@ -0,0 +1,29 @@ +# Copyright 2018 The Bazel Authors. All rights reserved. +# +# 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 +# +# http://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. + +"""Rule that allows select() to differentiate between compilers.""" + +load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain") + +def _compiler_flag_impl(ctx): + toolchain = find_cpp_toolchain(ctx) + return [config_common.FeatureFlagInfo(value = toolchain.compiler)] + +compiler_flag = rule( + implementation = _compiler_flag_impl, + attrs = { + "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")), + }, + toolchains = use_cpp_toolchain(), +) diff --git a/cc/private/rules_impl/native.bzl b/cc/private/rules_impl/native.bzl new file mode 100644 index 0000000..cce8c7f --- /dev/null +++ b/cc/private/rules_impl/native.bzl @@ -0,0 +1,34 @@ +# Copyright 2022 The Bazel Authors. All rights reserved. +# +# 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 +# +# http://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. + +# Redefine native symbols with a new name as a workaround for +# exporting them in `//third_party/bazel_rules/rules_proto/proto:defs.bzl` with their original name. +# +# While we cannot force users to load these symbol due to the lack of a +# allowlisting mechanism, we can still export them and tell users to +# load it to make a future migration to pure Starlark easier. + +"""Lovely workaround to be able to expose native constants pretending to be Starlark.""" + +# buildifier: disable=native-cc +NativeCcInfo = CcInfo + +# buildifier: disable=native-cc +NativeDebugPackageInfo = DebugPackageInfo + +# buildifier: disable=native-cc +NativeCcToolchainConfigInfo = CcToolchainConfigInfo + +# buildifier: disable=native-cc +native_cc_common = cc_common |