Replace sysroot with a cc_sysroot macro.

BEGIN_PUBLIC
Replace sysroot with a cc_sysroot macro.

This is part of what amontanez@ and I discussed about making C++ toolchains less "magic".
I'd like to do the same for cxx_builtin_include_directories, but that will require significantly more effort.

END_PUBLIC

PiperOrigin-RevId: 666607531
Change-Id: Ic9cfb157e892c05a9c875f240c0ed9a1048dea19
This commit is contained in:
Googler 2024-08-22 20:35:27 -07:00 committed by Copybara-Service
parent f5eb3c0c4a
commit 2480c90525
7 changed files with 84 additions and 25 deletions

0
cc/toolchains/args/BUILD Normal file
View File

View File

@ -0,0 +1,39 @@
# Copyright 2024 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.
"""Implementation of the cc_sysroot macro."""
load("//cc/toolchains:args.bzl", "cc_args")
visibility("public")
def cc_sysroot(name, sysroot, **kwargs):
"""Creates args for a sysroot.
Args:
name: (str) The name of the target
sysroot: (bazel_skylib's directory rule) The directory that should be the
sysroot.
**kwargs: kwargs to pass to cc_args.
"""
cc_args(
name = name,
actions = [
"//cc/toolchains/actions:cpp_compile_actions",
"//cc/toolchains/actions:c_compile",
"//cc/toolchains/actions:link_actions",
],
args = ["--sysroot={sysroot}"],
format = {"sysroot": sysroot},
**kwargs
)

View File

@ -203,11 +203,8 @@ cc_external_feature(
overridable = True, overridable = True,
) )
cc_external_feature( # Instead of the "sysroot" legacy flag, use the cc_sysroot macro in
name = "sysroot", # //cc/toolchains/args:sysroot.bzl
feature_name = "sysroot",
overridable = True,
)
cc_external_feature( cc_external_feature(
name = "unfiltered_compile_flags", name = "unfiltered_compile_flags",
@ -269,7 +266,6 @@ cc_feature_set(
":gcc_coverage_map_format", ":gcc_coverage_map_format",
":fully_static_link", ":fully_static_link",
":user_compile_flags", ":user_compile_flags",
":sysroot",
":unfiltered_compile_flags", ":unfiltered_compile_flags",
":linker_param_file", ":linker_param_file",
":compiler_input_flags", ":compiler_input_flags",

View File

@ -66,10 +66,6 @@ def _cc_toolchain_config_impl(ctx):
legacy = convert_toolchain(toolchain_config) legacy = convert_toolchain(toolchain_config)
sysroot = None
if ctx.attr.sysroot:
sysroot = ctx.attr.sysroot[DirectoryInfo].path
cxx_builtin_include_directories = [ cxx_builtin_include_directories = [
d[DirectoryInfo].path d[DirectoryInfo].path
for d in ctx.attr.cxx_builtin_include_directories for d in ctx.attr.cxx_builtin_include_directories
@ -93,7 +89,6 @@ def _cc_toolchain_config_impl(ctx):
compiler = "", compiler = "",
abi_version = "", abi_version = "",
abi_libc_version = "", abi_libc_version = "",
builtin_sysroot = sysroot,
), ),
# This allows us to support all_files. # This allows us to support all_files.
# If all_files was simply an alias to # If all_files was simply an alias to
@ -117,7 +112,6 @@ cc_toolchain_config = rule(
"_enabled": attr.label(default = "//cc/toolchains:experimental_enable_rule_based_toolchains"), "_enabled": attr.label(default = "//cc/toolchains:experimental_enable_rule_based_toolchains"),
# Attributes translated from legacy cc toolchains. # Attributes translated from legacy cc toolchains.
"sysroot": attr.label(providers = [DirectoryInfo]),
"cxx_builtin_include_directories": attr.label_list(providers = [DirectoryInfo]), "cxx_builtin_include_directories": attr.label_list(providers = [DirectoryInfo]),
}, },
provides = [ToolchainConfigInfo], provides = [ToolchainConfigInfo],

View File

@ -400,10 +400,8 @@ cc_variable(
type = types.list(types.string), type = types.list(types.string),
) )
cc_variable( # Instead of the "sysroot" variable, use the cc_sysroot macro in
name = "sysroot", # //cc/toolchains/args:sysroot.bzl
type = types.directory,
)
cc_variable( cc_variable(
name = "system_include_paths", name = "system_include_paths",
@ -526,7 +524,6 @@ cc_builtin_variables(
":source_file", ":source_file",
":strip_debug_symbols", ":strip_debug_symbols",
":stripopts", ":stripopts",
":sysroot",
":system_include_paths", ":system_include_paths",
":thinlto_index", ":thinlto_index",
":thinlto_indexing_param_file", ":thinlto_indexing_param_file",

View File

@ -3,6 +3,7 @@ load("//cc/toolchains:args.bzl", "cc_args")
load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature.bzl", "cc_feature")
load("//cc/toolchains:feature_set.bzl", "cc_feature_set") load("//cc/toolchains:feature_set.bzl", "cc_feature_set")
load("//cc/toolchains:tool_map.bzl", "cc_tool_map") load("//cc/toolchains:tool_map.bzl", "cc_tool_map")
load("//cc/toolchains/args:sysroot.bzl", "cc_sysroot")
load("//cc/toolchains/impl:external_feature.bzl", "cc_external_feature") load("//cc/toolchains/impl:external_feature.bzl", "cc_external_feature")
load("//cc/toolchains/impl:toolchain_config.bzl", "cc_legacy_file_group", "cc_toolchain_config") load("//cc/toolchains/impl:toolchain_config.bzl", "cc_legacy_file_group", "cc_toolchain_config")
load("//tests/rule_based_toolchain:analysis_test_suite.bzl", "analysis_test_suite") load("//tests/rule_based_toolchain:analysis_test_suite.bzl", "analysis_test_suite")
@ -40,17 +41,24 @@ util.helper_target(
env = {"CPP_COMPILE": "1"}, env = {"CPP_COMPILE": "1"},
) )
cc_sysroot(
name = "sysroot",
sysroot = "//tests/rule_based_toolchain/testdata:directory",
)
util.helper_target( util.helper_target(
cc_toolchain_config, cc_toolchain_config,
name = "collects_files_toolchain_config", name = "collects_files_toolchain_config",
args = [":c_compile_args"], args = [
":sysroot",
":c_compile_args",
],
cxx_builtin_include_directories = [ cxx_builtin_include_directories = [
"//tests/rule_based_toolchain/testdata:directory", "//tests/rule_based_toolchain/testdata:directory",
], ],
enabled_features = [":simple_feature"], enabled_features = [":simple_feature"],
known_features = [":compile_feature"], known_features = [":compile_feature"],
skip_experimental_flag_validation_for_test = True, skip_experimental_flag_validation_for_test = True,
sysroot = "//tests/rule_based_toolchain/testdata:directory",
tool_map = ":compile_tool_map", tool_map = ":compile_tool_map",
) )

View File

@ -204,12 +204,37 @@ def _toolchain_collects_files_test(env, targets):
legacy_feature( legacy_feature(
name = "implied_by_always_enabled", name = "implied_by_always_enabled",
enabled = True, enabled = True,
flag_sets = [legacy_flag_set( flag_sets = [
legacy_flag_set(
actions = [
"c++-compile",
"c++-header-parsing",
"c++-link-dynamic-library",
"c++-link-executable",
"c++-link-nodeps-dynamic-library",
"c++-module-codegen",
"c++-module-compile",
"c-compile",
"clif-match",
"linkstamp-compile",
"lto-backend",
"lto-index-for-dynamic-library",
"lto-index-for-executable",
"lto-index-for-nodeps-dynamic-library",
],
flag_groups = [
legacy_flag_group(flags = [
"--sysroot=tests/rule_based_toolchain/testdata",
]),
],
),
legacy_flag_set(
actions = ["c_compile"], actions = ["c_compile"],
flag_groups = [ flag_groups = [
legacy_flag_group(flags = ["c_compile_args"]), legacy_flag_group(flags = ["c_compile_args"]),
], ],
)], ),
],
), ),
]).in_order() ]).in_order()