Reorganize to address review feedback

This commit is contained in:
Armando Montanez 2024-10-03 10:04:23 -07:00
parent dd9e124c92
commit be2b2e94af
8 changed files with 363 additions and 251 deletions

View File

@ -22,6 +22,14 @@ http_archive(
url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/linux-amd64/+/git_revision:0cfd03ac0d3f9713090a581bda07584754c73a49",
)
http_archive(
name = "clang-linux-aarch64",
build_file = "//toolchain:clang.BUILD",
sha256 = "61abb915821190baddafa973c69a9db9acda5a16ed3a89489ea2b3b030a2330b",
type = "zip",
url = "https://chrome-infra-packages.appspot.com/dl/fuchsia/third_party/clang/linux-arm64/+/git_revision:0cfd03ac0d3f9713090a581bda07584754c73a49",
)
http_archive(
name = "linux_sysroot",
build_file = "//toolchain:linux_sysroot.BUILD",
@ -31,6 +39,6 @@ http_archive(
)
register_toolchains(
"//toolchain:host-linux-x86_64",
"//toolchain:host_cc_toolchain",
dev_dependency = True,
)

View File

@ -11,3 +11,5 @@ with the following command:
```
$ bazel test //...
```
This example currently only supports Linux.

View File

@ -0,0 +1,47 @@
# 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.
load("@bazel_skylib//lib:selects.bzl", "selects")
licenses(["notice"])
selects.config_setting_group(
name = "linux_x86_64",
match_all = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
)
selects.config_setting_group(
name = "linux_aarch64",
match_all = ["@platforms//os:linux", "@platforms//cpu:aarch64"],
)
selects.config_setting_group(
name = "macos_x86_64",
match_all = ["@platforms//os:macos", "@platforms//cpu:x86_64"],
)
selects.config_setting_group(
name = "macos_aarch64",
match_all = ["@platforms//os:macos", "@platforms//cpu:aarch64"],
)
selects.config_setting_group(
name = "windows_x86_64",
match_all = ["@platforms//os:windows", "@platforms//cpu:x86_64"],
)
selects.config_setting_group(
name = "windows_aarch64",
match_all = ["@platforms//os:windows", "@platforms//cpu:aarch64"],
)

View File

@ -12,72 +12,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:toolchain.bzl", "cc_toolchain")
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
# For now, each toolchain is only declared to be compatible
# when exec_platform == target_platform.
PLATFORMS = {
"linux-x86_64": [
"@platforms//os:linux",
"@platforms//cpu:x86_64",
],
}
cc_args(
name = "warnings",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = [
"-Werror",
"-Wall",
"-Wextra",
"-Wpedantic",
],
)
cc_args(
name = "no_canonical_prefixes",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = ["-no-canonical-prefixes"],
)
[
cc_toolchain(
name = "host-{}-config".format(platform_name),
args = select({
"@platforms//os:linux": [
"@linux_sysroot//:sysroot",
],
"//conditions:default": [],
}) + [
":no_canonical_prefixes",
":warnings",
cc_toolchain(
name = "host_clang",
args = select({
"@platforms//os:linux": [
"//toolchain/args:linux_sysroot",
],
enabled_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
known_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
target_compatible_with = constraints,
tool_map = "@clang-{}//:all_tools".format(platform_name),
)
for platform_name, constraints in PLATFORMS.items()
]
"//conditions:default": [],
}) + [
"//toolchain/args:no_canonical_prefixes",
"//toolchain/args:warnings",
],
enabled_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
known_features = ["@rules_cc//cc/toolchains/args:experimental_replace_legacy_action_config_features"],
tool_map = "//toolchain/tools:all_tools",
)
[
toolchain(
name = "host-{}".format(platform_name),
exec_compatible_with = constraints,
target_compatible_with = constraints,
toolchain = ":host-{}-config".format(platform_name),
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
for platform_name, constraints in PLATFORMS.items()
]
toolchain(
name = "host_cc_toolchain",
toolchain = ":host_clang",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

View File

@ -0,0 +1,51 @@
# 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.
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains/args:sysroot.bzl", "cc_sysroot")
package(default_visibility = ["//visibility:public"])
cc_sysroot(
name = "linux_sysroot",
data = [
"@linux_sysroot//:root",
"@linux_sysroot//:usr-include",
"@linux_sysroot//:usr-include-x86_64-linux-gnu",
],
sysroot = "@linux_sysroot//:root",
)
cc_args(
name = "warnings",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = [
"-Werror",
"-Wall",
"-Wextra",
"-Wpedantic",
],
)
cc_args(
name = "no_canonical_prefixes",
actions = [
"@rules_cc//cc/toolchains/actions:c_compile",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions",
],
args = ["-no-canonical-prefixes"],
)

View File

@ -14,13 +14,13 @@
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory")
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
exports_files(glob(["bin/**"]))
# Directory-based rules in this toolchain only referece things in
# lib/ or include/ subdirectories.
directory(
@ -31,180 +31,6 @@ directory(
]),
)
#####################
# Tools #
#####################
alias(
name = "all_tools",
actual = select({
"@platforms//os:macos": ":macos_tools",
"//conditions:default": ":default_tools",
}),
)
COMMON_TOOLS = {
"@rules_cc//cc/toolchains/actions:assembly_actions": ":asm",
"@rules_cc//cc/toolchains/actions:c_compile": ":clang",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions": ":clang++",
"@rules_cc//cc/toolchains/actions:link_actions": ":lld",
"@rules_cc//cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy",
"@rules_cc//cc/toolchains/actions:strip": ":llvm-strip",
}
cc_tool_map(
name = "default_tools",
tools = COMMON_TOOLS | {
"@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-ar",
},
visibility = ["//visibility:private"],
)
cc_tool_map(
name = "macos_tools",
tools = COMMON_TOOLS | {
"@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-libtool-darwin",
},
visibility = ["//visibility:private"],
)
# TODO: https://github.com/bazelbuild/rules_cc/issues/235 - Workaround until
# Bazel has a more robust way to implement `cc_tool_map`.
alias(
name = "asm",
actual = ":clang",
)
cc_tool(
name = "clang",
src = select({
"@platforms//os:windows": "//:bin/clang.exe",
"//conditions:default": "//:bin/clang",
}),
data = glob([
"bin/llvm",
]) + [
":lib-clang-include",
],
)
cc_tool(
name = "clang++",
src = select({
"@platforms//os:windows": "//:bin/clang++.exe",
"//conditions:default": "//:bin/clang++",
}),
data = glob([
"bin/llvm",
]) + [
":include-x86_64-unknown-linux-gnu-c++-v1",
":include-c++-v1",
":lib-clang-include",
],
)
cc_tool(
name = "lld",
src = select({
"@platforms//os:windows": "//:bin/clang++.exe",
"//conditions:default": "//:bin/clang++",
}),
data = glob([
"bin/llvm",
"bin/lld*",
"bin/ld*",
"lib/**/*.a",
"lib/**/*.so*",
"lib/**/*.o",
]),
)
cc_tool(
name = "llvm-ar",
src = select({
"@platforms//os:windows": "//:bin/llvm-ar.exe",
"//conditions:default": "//:bin/llvm-ar",
}),
data = glob(["bin/llvm"]),
)
cc_tool(
name = "llvm-libtool-darwin",
src = select({
"@platforms//os:windows": "//:bin/llvm-libtool-darwin.exe",
"//conditions:default": "//:bin/llvm-libtool-darwin",
}),
data = glob(["bin/llvm"]),
)
cc_tool(
name = "llvm-objcopy",
src = select({
"@platforms//os:windows": "//:bin/llvm-objcopy.exe",
"//conditions:default": "//:bin/llvm-objcopy",
}),
data = glob(["bin/llvm"]),
)
cc_tool(
name = "llvm-objdump",
src = select({
"@platforms//os:windows": "//:bin/llvm-objdump.exe",
"//conditions:default": "//:bin/llvm-objdump",
}),
data = glob(["bin/llvm"]),
)
cc_tool(
name = "llvm-cov",
src = select({
"@platforms//os:windows": "//:bin/llvm-cov.exe",
"//conditions:default": "//:bin/llvm-cov",
}),
data = glob(["bin/llvm"]),
)
cc_tool(
name = "llvm-strip",
src = select({
"@platforms//os:windows": "//:bin/llvm-strip.exe",
"//conditions:default": "//:bin/llvm-strip",
}),
data = glob(["bin/llvm"]),
)
cc_tool(
name = "clang-tidy",
src = select({
"@platforms//os:windows": "//:bin/clang-tidy.exe",
"//conditions:default": "//:bin/clang-tidy",
}),
data = glob([
"bin/llvm",
"include/**",
"lib/clang/**/include/**",
]) + select({
"@platforms//os:linux": ["@linux_sysroot//:root"],
"//conditions:default": [],
}),
)
#####################
# Arguments #
#####################
# None...
#####################
# Features #
#####################
# None...
#####################
# Builtins #
#####################
subdirectory(
name = "include-c++-v1",
parent = ":toolchain_root",
@ -222,3 +48,35 @@ subdirectory(
parent = ":toolchain_root",
path = "include/x86_64-unknown-linux-gnu/c++/v1",
)
filegroup(
name = "builtin_headers",
srcs = [
":include-c++-v1",
":include-x86_64-unknown-linux-gnu-c++-v1",
":lib-clang-include",
],
)
# Various supporting files needed to run the linker.
filegroup(
name = "linker_builtins",
data = glob([
"bin/lld*",
"bin/ld*",
"lib/**/*.a",
"lib/**/*.so*",
"lib/**/*.o",
]) + [
":multicall_support_files",
],
)
# Some toolchain distributions use busybox-style handling of tools, so things
# like `clang++` just redirect to a `llvm` binary. This glob catches this
# binary if it's included in the distribution, and is a no-op if the multicall
# binary doesn't exist.
filegroup(
name = "multicall_support_files",
srcs = glob(["bin/llvm"]),
)

View File

@ -14,22 +14,11 @@
load("@bazel_skylib//rules/directory:directory.bzl", "directory")
load("@bazel_skylib//rules/directory:subdirectory.bzl", "subdirectory")
load("@rules_cc//cc/toolchains/args:sysroot.bzl", "cc_sysroot")
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
cc_sysroot(
name = "sysroot",
data = [
":root",
":usr-include-x86_64-linux-gnu",
":usr-include",
],
sysroot = ":root",
)
directory(
name = "root",
srcs = glob(["**/*"]),

View File

@ -0,0 +1,201 @@
# 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.
load("@rules_cc//cc/toolchains:tool.bzl", "cc_tool")
load("@rules_cc//cc/toolchains:tool_map.bzl", "cc_tool_map")
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
# This `select` happens under the target configuration. For macOS,
# llvm-libtool-darwin should be used when creating static libraries even if the
# exec platform is linux.
alias(
name = "all_tools",
actual = select({
"@platforms//os:macos": ":macos_tools",
"//conditions:default": ":default_tools",
}),
)
COMMON_TOOLS = {
"@rules_cc//cc/toolchains/actions:assembly_actions": ":asm",
"@rules_cc//cc/toolchains/actions:c_compile": ":clang",
"@rules_cc//cc/toolchains/actions:cpp_compile_actions": ":clang++",
"@rules_cc//cc/toolchains/actions:link_actions": ":lld",
"@rules_cc//cc/toolchains/actions:objcopy_embed_data": ":llvm-objcopy",
"@rules_cc//cc/toolchains/actions:strip": ":llvm-strip",
}
cc_tool_map(
name = "default_tools",
tools = COMMON_TOOLS | {
"@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-ar",
},
visibility = ["//visibility:private"],
)
cc_tool_map(
name = "macos_tools",
tools = COMMON_TOOLS | {
"@rules_cc//cc/toolchains/actions:ar_actions": ":llvm-libtool-darwin",
},
visibility = ["//visibility:private"],
)
# TODO: https://github.com/bazelbuild/rules_cc/issues/235 - Workaround until
# Bazel has a more robust way to implement `cc_tool_map`.
alias(
name = "asm",
actual = ":clang",
)
cc_tool(
name = "clang",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/clang",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/clang",
}),
data = [
":exec_platform_builtin_headers",
":exec_platform_multicall_support_files",
],
)
cc_tool(
name = "clang++",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/clang++",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/clang++",
}),
data = [
":exec_platform_builtin_headers",
":exec_platform_multicall_support_files",
],
)
cc_tool(
name = "lld",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/clang++",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/clang++",
}),
data = [
":exec_platform_builtin_headers",
":exec_platform_linker_builtins",
":exec_platform_multicall_support_files",
],
)
cc_tool(
name = "llvm-ar",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/llvm-ar",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/llvm-ar",
}),
data = [":exec_platform_multicall_support_files"],
)
cc_tool(
name = "llvm-libtool-darwin",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/llvm-libtool-darwin",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/llvm-libtool-darwin",
}),
data = [":exec_platform_multicall_support_files"],
)
cc_tool(
name = "llvm-objcopy",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/llvm-objcopy",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/llvm-objcopy",
}),
data = [":exec_platform_multicall_support_files"],
)
cc_tool(
name = "llvm-objdump",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/llvm-objdump",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/llvm-objdump",
}),
data = [":exec_platform_multicall_support_files"],
)
cc_tool(
name = "llvm-cov",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/llvm-cov",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/llvm-cov",
}),
data = [":exec_platform_multicall_support_files"],
)
cc_tool(
name = "llvm-strip",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/llvm-strip",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/llvm-strip",
}),
data = [":exec_platform_multicall_support_files"],
)
cc_tool(
name = "clang-tidy",
src = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:bin/clang-tidy",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:bin/clang-tidy",
}),
data = [
":exec_platform_builtin_headers",
":exec_platform_multicall_support_files",
],
)
#################################
# Platform-specific aliases #
#################################
# These aliases are used to reduce duplication of `select` statements throughout
# this build file. The select statements in these aliases are evaluated under
# the exec configuration.
alias(
name = "exec_platform_builtin_headers",
actual = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:builtin_headers",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:builtin_headers",
}),
visibility = ["//visibility:private"],
)
alias(
name = "exec_platform_multicall_support_files",
actual = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:multicall_support_files",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:multicall_support_files",
}),
visibility = ["//visibility:private"],
)
alias(
name = "exec_platform_linker_builtins",
actual = select({
"//constraint:linux_aarch64": "@clang-linux-aarch64//:linker_builtins",
"//constraint:linux_x86_64": "@clang-linux-x86_64//:linker_builtins",
}),
visibility = ["//visibility:private"],
)