Switch everything to platforms
This patch switches the codebase over from using the "cpu"
mechanism to using bazel platforms. See
https://docs.bazel.build/versions/master/platforms.html for some more
information.
Most of the substantial changes are in //tools. Instead of using
`cc_toolchain_suite` rules, we now use regular `toolchain` rules that
are registered in the WORKSPACE. That also means that bazel now uses
the target platform to select the compiler.
All --cpu=* arguments should now be --config=* arguments. For example,
`--cpu=roborio` should now be `--config=roborio`. The CI script and
all documentation has been updated to reflect that.
The remainder of the changes revolve around tagging all targets with
`target_compatible_with`. The old mechanism allowed us to specify
repo-wide defaults. The new mechanism does not. That means every
target that didn't have any compatibility specified, now requires
compatibility with `@platforms//os:linux`.
I used buildozer for the vast majority of `target_compatible_with`
changes. buildozer automatically buildifies any BUILD files it
touches. That means this patch also contains a few non-functional
changes that I was too lazy to remove.
Change-Id: I66d6e6ad9161520ee397597cdb492585820a3acd
diff --git a/tools/BUILD b/tools/BUILD
index 50c9141..3acd443 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -7,44 +7,47 @@
name = "compiler_clang",
flag_values = {
"@bazel_tools//tools/cpp:compiler": "clang",
- }
+ },
)
config_setting(
name = "compiler_gcc",
flag_values = {
"@bazel_tools//tools/cpp:compiler": "gcc",
- }
+ },
)
+# TODO(phil): Get rid of these and just use the constraint values directly
+# everywhere.
config_setting(
name = "cpu_k8",
- values = {"cpu": "k8"},
+ constraint_values = ["@platforms//cpu:x86_64"],
)
config_setting(
name = "cpu_roborio",
- values = {"cpu": "roborio"},
+ constraint_values = ["@//tools/platforms/hardware:roborio"],
)
config_setting(
name = "cpu_cortex_m4f",
- values = {"cpu": "cortex-m4f"},
+ constraint_values = ["@//tools/platforms/hardware:cortex_m4f"],
)
-config_setting(
- name = "cpu_cortex_m4f_k22",
- values = {"cpu": "cortex-m4f-k22"},
-)
+# TODO(phil): Re-enable this with proper support.
+#config_setting(
+# name = "cpu_cortex_m4f_k22",
+# constraint_values = ["@platforms//cpu:?"],
+#)
config_setting(
name = "cpu_armhf",
- values = {"cpu": "armhf-debian"},
+ constraint_values = ["@//tools/platforms/hardware:raspberry_pi"],
)
config_setting(
name = "cpu_aarch64",
- values = {"cpu": "aarch64"},
+ constraint_values = ["@platforms//cpu:arm64"],
)
config_setting(
@@ -61,33 +64,3 @@
name = "has_ubsan",
values = {"define": "have_ubsan=true"},
)
-
-environment(name = "k8")
-
-environment(name = "armhf-debian")
-
-environment(name = "roborio")
-
-environment(name = "cortex-m4f")
-
-environment(name = "cortex-m4f-k22")
-
-environment(name = "aarch64")
-
-environment_group(
- name = "cpus",
- defaults = [
- ":k8",
- ":roborio",
- ":aarch64",
- ":armhf-debian",
- ],
- environments = [
- ":k8",
- ":roborio",
- ":armhf-debian",
- ":cortex-m4f",
- ":cortex-m4f-k22",
- ":aarch64",
- ],
-)
diff --git a/tools/actions/BUILD b/tools/actions/BUILD
index 46c9541..7c69bfb 100644
--- a/tools/actions/BUILD
+++ b/tools/actions/BUILD
@@ -3,6 +3,7 @@
srcs = [
"generate_compile_command.py",
],
+ target_compatible_with = ["@platforms//os:linux"],
deps = [
"//third_party/bazel:extra_actions_proto_py",
],
@@ -14,6 +15,7 @@
mnemonics = [
"CppCompile",
],
+ target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
)
@@ -24,6 +26,7 @@
out_templates = [
"$(ACTION_ID)_compile_command",
],
+ target_compatible_with = ["@platforms//os:linux"],
tools = [
":generate_compile_command",
],
diff --git a/tools/build_rules/BUILD b/tools/build_rules/BUILD
index 8ad7018..cf27ca0 100644
--- a/tools/build_rules/BUILD
+++ b/tools/build_rules/BUILD
@@ -1,12 +1,14 @@
sh_binary(
name = "quiet_success",
srcs = ["quiet_success.sh"],
+ target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
)
py_binary(
name = "jinja2_generator",
srcs = ["jinja2_generator.py"],
+ target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
deps = ["@python_jinja2"],
)
diff --git a/tools/build_rules/fortran.bzl b/tools/build_rules/fortran.bzl
index 9e6e789..d55f1c1 100644
--- a/tools/build_rules/fortran.bzl
+++ b/tools/build_rules/fortran.bzl
@@ -1,109 +1,5 @@
load("@//tools/build_rules:select.bzl", "compiler_select")
-def _single_fortran_object_impl(ctx):
- toolchain_cflags = (ctx.fragments.cpp.compiler_options([]) +
- ctx.fragments.cpp.c_options +
- ctx.fragments.cpp.unfiltered_compiler_options([]) +
- [
- "-fPIC",
- "-Wno-maybe-uninitialized",
- "-Wno-unused-dummy-argument",
- "-Wno-conversion",
- "-Wno-unused-variable",
- "-Wno-character-truncation",
- ])
-
- cmd = toolchain_cflags + ["-c", ctx.file.src.path, "-o", ctx.outputs.pic_o.path]
- filtered_cmd = []
-
- # Strip out the C/C++/Clang specific flags.
- exclude_flags = [
- "-fcolor-diagnostics",
- "-Wswitch-enum",
- "-Wpointer-arith",
- "-Wcast-qual",
- "-Wwrite-strings",
- "-Wsign-compare",
- "-Wformat=2",
- "-Werror",
- "-Wextra",
- "-Wno-builtin-macro-redefined",
- "-Wunused-local-typedefs",
- "-D__has_feature(x)=0",
- "-fmacro-backtrace-limit=0",
- ]
-
- for flag in cmd:
- if flag not in exclude_flags and not (flag.startswith("-fsanitize") or
- flag.startswith("-fno-sanitize")):
- filtered_cmd.append(flag)
-
- ctx.action(
- inputs = [ctx.file.src] + ctx.files._cc_toolchain,
- outputs = [ctx.outputs.pic_o],
- mnemonic = "Fortran",
- executable = ctx.fragments.cpp.compiler_executable,
- arguments = filtered_cmd,
- progress_message = "Building %s" % ctx.outputs.pic_o.short_path,
- )
-
-def _define_fortran_output(src):
- if not src.name.endswith(".f"):
- fail("Fortran files must end in '.f'", "src")
-
- fortran_file_base = src.name[:-2]
- return {
- "pic_o": fortran_file_base + ".pic.o",
- }
-
-_single_fortran_object = rule(
- attrs = {
- "src": attr.label(
- allow_single_file = [".f"],
- ),
- "cc_libs": attr.label_list(providers = ["cc"]),
- # TODO(Brian): Replace this with something more fine-grained from the
- # configuration fragment or something.
- "_cc_toolchain": attr.label(
- default = Label("@//tools/cpp:toolchain"),
- ),
- },
- fragments = [
- "cpp",
- ],
- outputs = _define_fortran_output,
- implementation = _single_fortran_object_impl,
-)
-
-def fortran_library(name, srcs, deps = [], visibility = None):
- """Builds a shared library from a set of fortran files.
-
- Args:
- srcs: list of fortran files ending in .f
- deps: cc_library or fortran_library dependencies.
- """
- pic_o_files = []
- for src in srcs:
- pic_o_file = src[:-2] + ".pic.o"
- _single_fortran_object(
- name = name + "_" + pic_o_file,
- src = src,
- visibility = ["//visibility:private"],
- restricted_to = ["@//tools:k8"],
- )
- pic_o_files.append(pic_o_file)
-
- native.cc_library(
- name = name,
- deps = deps,
- srcs = pic_o_files,
- linkopts = [
- "-lgfortran",
- ],
- visibility = visibility,
- restricted_to = ["@//tools:k8"],
- )
-
f2c_copts = compiler_select({
"clang": [
"-Wno-incompatible-pointer-types-discards-qualifiers",
diff --git a/tools/build_rules/pandoc.bzl b/tools/build_rules/pandoc.bzl
index bc63bac..ad47ccc 100644
--- a/tools/build_rules/pandoc.bzl
+++ b/tools/build_rules/pandoc.bzl
@@ -6,7 +6,7 @@
src: Markdown file to convert. Only one file can be specified.
"""
-def pandoc_html(name, src):
+def pandoc_html(name, src, target_compatible_with = None):
output = name + ".html"
native.genrule(
name = name,
@@ -15,4 +15,5 @@
cmd = "$(location @pandoc//:pandoc_wrapper) -s $< -o $@",
tools = ["@pandoc//:all_files", "@pandoc//:pandoc_wrapper"],
executable = True,
+ target_compatible_with = target_compatible_with,
)
diff --git a/tools/build_rules/select.bzl b/tools/build_rules/select.bzl
index 1c48eb7..23addf5 100644
--- a/tools/build_rules/select.bzl
+++ b/tools/build_rules/select.bzl
@@ -46,7 +46,8 @@
"@//tools:cpu_roborio": values["roborio"],
"@//tools:cpu_armhf": values["armhf"],
"@//tools:cpu_cortex_m4f": values["cortex-m"],
- "@//tools:cpu_cortex_m4f_k22": values["cortex-m"],
+ # TODO(phil): Support this properly.
+ #"@//tools:cpu_cortex_m4f_k22": values["cortex-m"],
})
"""A select wrapper for address space sizes.
@@ -66,7 +67,8 @@
"@//tools:cpu_roborio": values["32"],
"@//tools:cpu_armhf": values["32"],
"@//tools:cpu_cortex_m4f": values["32"],
- "@//tools:cpu_cortex_m4f_k22": values["32"],
+ # TODO(phil): Support this properly.
+ #"@//tools:cpu_cortex_m4f_k22": values["32"],
})
"""A select wrapper for compilers.
diff --git a/tools/ci/run-tests.sh b/tools/ci/run-tests.sh
index fd92ea3..ef4cff8 100755
--- a/tools/ci/run-tests.sh
+++ b/tools/ci/run-tests.sh
@@ -16,21 +16,21 @@
# bugs with Eigen.
tools/bazel --output_base=../k8_output_base test \
${COMMON} \
- --cpu=k8 \
+ --config=k8 \
--config=eigen \
${TARGETS}
tools/bazel --output_base=../roborio_output_base build \
${COMMON} \
- --cpu=roborio \
+ --config=roborio \
${TARGETS}
tools/bazel --output_base=../armhf-debian_output_base build \
${COMMON} \
- --cpu=armhf-debian \
+ --config=armhf-debian \
${TARGETS}
tools/bazel --output_base=../cortex-m4f_output_base build \
${COMMON} \
- --cpu=cortex-m4f \
+ --config=cortex-m4f \
${M4F_TARGETS}
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index d370750..4f0e6d9 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -2,16 +2,25 @@
package(default_visibility = ["//visibility:public"])
-[cc_toolchain_config(
- name = "{}_toolchain_config".format(cpu),
- cpu = cpu,
-) for cpu in
- ["armeabi-v7a", "armhf-debian", "cortex-m4f", "cortex-m4f-k22", "k8", "roborio"]
+[
+ cc_toolchain_config(
+ name = "{}_toolchain_config".format(cpu),
+ cpu = cpu,
+ )
+ for cpu in [
+ "armeabi-v7a",
+ "armhf-debian",
+ "cortex-m4f",
+ "cortex-m4f-k22",
+ "k8",
+ "roborio",
+ ]
]
cc_library(
name = "empty_main",
srcs = ["empty_main.c"],
+ target_compatible_with = ["@platforms//os:linux"],
)
cc_library(
@@ -20,13 +29,15 @@
"//tools:has_asan": [],
"//tools:has_tsan": [],
"//tools:cpu_cortex_m4f": [],
- "//tools:cpu_cortex_m4f_k22": [],
+ # TODO(phil): Support this properly.
+ #"//tools:cpu_cortex_m4f_k22": [],
"//conditions:default": ["//third_party/gperftools:tcmalloc"],
}),
)
cc_library(
name = "stl",
+ target_compatible_with = ["@platforms//os:linux"],
)
filegroup(
@@ -34,18 +45,6 @@
srcs = [],
)
-# This is the entry point for --crosstool_top.
-cc_toolchain_suite(
- name = "toolchain",
- toolchains = {
- "k8": ":cc-compiler-k8",
- "roborio": ":cc-compiler-roborio",
- "armhf-debian": "cc-compiler-armhf-debian",
- "cortex-m4f": "cc-compiler-cortex-m4f",
- "cortex-m4f-k22": "cc-compiler-cortex-m4f-k22",
- },
-)
-
# Compiler inputs given by --copt etc in //tools:bazel.rc.
filegroup(
name = "flags_compiler_inputs",
@@ -111,16 +110,30 @@
cc_toolchain(
name = "cc-compiler-k8",
all_files = ":clang_6p0_all_files",
+ ar_files = ":clang_6p0_ar_files",
+ as_files = ":clang_6p0_compiler_files",
compiler_files = ":clang_6p0_compiler_files",
dwp_files = ":empty",
linker_files = ":clang_6p0_linker_files",
- ar_files = ":clang_6p0_ar_files",
- as_files = ":clang_6p0_compiler_files",
objcopy_files = "//tools/cpp/clang_6p0:objcopy",
strip_files = ":clang_6p0_strip_files",
supports_param_files = 1,
- toolchain_identifier = "k8_linux",
toolchain_config = ":k8_toolchain_config",
+ toolchain_identifier = "k8_linux",
+)
+
+toolchain(
+ name = "cc-toolchain-k8",
+ exec_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+ target_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+ toolchain = ":cc-compiler-k8",
+ toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
filegroup(
@@ -173,16 +186,30 @@
cc_toolchain(
name = "cc-compiler-roborio",
all_files = ":roborio-compiler-files",
+ ar_files = ":roborio_ar_files",
+ as_files = ":roborio_compiler_files",
compiler_files = ":roborio_compiler_files",
dwp_files = ":empty",
linker_files = ":roborio_linker_files",
- ar_files = ":roborio_ar_files",
- as_files = ":roborio_compiler_files",
objcopy_files = "//tools/cpp/arm-frc-linux-gnueabi:objcopy",
strip_files = ":roborio_strip_files",
supports_param_files = 1,
- toolchain_identifier = "roborio_linux",
toolchain_config = ":roborio_toolchain_config",
+ toolchain_identifier = "roborio_linux",
+)
+
+toolchain(
+ name = "cc-toolchain-roborio",
+ exec_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+ target_compatible_with = [
+ "@platforms//os:linux",
+ "//tools/platforms/hardware:roborio",
+ ],
+ toolchain = ":cc-compiler-roborio",
+ toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
filegroup(
@@ -240,16 +267,30 @@
cc_toolchain(
name = "cc-compiler-armhf-debian",
all_files = ":linaro-gcc-files",
+ ar_files = "linaro_linux_ar_files",
+ as_files = "linaro_linux_compiler_files",
compiler_files = ":linaro_linux_compiler_files",
dwp_files = ":empty",
linker_files = ":linaro_linux_linker_files",
- ar_files = "linaro_linux_ar_files",
- as_files = "linaro_linux_compiler_files",
objcopy_files = "//tools/cpp/linaro_linux_gcc:objcopy",
strip_files = ":linaro_linux_strip_files",
supports_param_files = 1,
- toolchain_identifier = "clang_linux_armhf",
toolchain_config = ":armhf-debian_toolchain_config",
+ toolchain_identifier = "clang_linux_armhf",
+)
+
+toolchain(
+ name = "cc-toolchain-armhf-debian",
+ exec_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+ target_compatible_with = [
+ "@platforms//os:linux",
+ "//tools/platforms/hardware:raspberry_pi",
+ ],
+ toolchain = ":cc-compiler-armhf-debian",
+ toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
filegroup(
@@ -292,15 +333,29 @@
cc_toolchain(
name = "cc-compiler-cortex-m4f",
all_files = ":gcc_arm_none_eabi_none_files",
+ ar_files = ":gcc_arm_none_eabi_ar_files",
compiler_files = ":gcc_arm_none_eabi_compiler_files",
dwp_files = ":empty",
linker_files = ":gcc_arm_none_eabi_linker_files",
- ar_files = ":gcc_arm_none_eabi_ar_files",
objcopy_files = "//tools/cpp/gcc_arm_none_eabi:objcopy",
strip_files = "//tools/cpp/gcc_arm_none_eabi:strip",
supports_param_files = 1,
- toolchain_identifier = "cortex-m4f",
toolchain_config = ":cortex-m4f_toolchain_config",
+ toolchain_identifier = "cortex-m4f",
+)
+
+toolchain(
+ name = "cc-toolchain-cortex-m4f",
+ exec_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+ target_compatible_with = [
+ "@platforms//os:none",
+ "//tools/platforms/hardware:cortex_m4f",
+ ],
+ toolchain = ":cc-compiler-cortex-m4f",
+ toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
cc_toolchain(
@@ -312,6 +367,20 @@
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 1,
- toolchain_identifier = "cortex-m4f-k22",
toolchain_config = ":cortex-m4f-k22_toolchain_config",
+ toolchain_identifier = "cortex-m4f-k22",
+)
+
+toolchain(
+ name = "cc-toolchain-cortex-m4f-k22",
+ exec_compatible_with = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+ target_compatible_with = [
+ "@platforms//os:none",
+ "//tools/platforms/hardware:cortex_m4f",
+ ],
+ toolchain = ":cc-compiler-cortex-m4f-k22",
+ toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
diff --git a/tools/environments.bzl b/tools/environments.bzl
deleted file mode 100644
index c82a01b..0000000
--- a/tools/environments.bzl
+++ /dev/null
@@ -1,6 +0,0 @@
-# Contains some helpers for working with environments.
-
-mcu_cpus = [
- "@//tools:cortex-m4f",
- "@//tools:cortex-m4f-k22",
-]
diff --git a/tools/platforms.bzl b/tools/platforms.bzl
new file mode 100644
index 0000000..fb2f69d
--- /dev/null
+++ b/tools/platforms.bzl
@@ -0,0 +1,11 @@
+load("@bazel_skylib//lib:selects.bzl", "selects")
+
+def _any_of(constraint_values):
+ return selects.with_or({
+ tuple(constraint_values): [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ })
+
+platforms = struct(
+ any_of = _any_of,
+)
diff --git a/tools/platforms/BUILD b/tools/platforms/BUILD
new file mode 100644
index 0000000..65563bf
--- /dev/null
+++ b/tools/platforms/BUILD
@@ -0,0 +1,45 @@
+package(default_visibility = ["//visibility:public"])
+
+platform(
+ name = "linux_x86",
+ constraint_values = [
+ "@platforms//os:linux",
+ "@platforms//cpu:x86_64",
+ ],
+)
+
+platform(
+ name = "linux_armhf",
+ constraint_values = [
+ "@platforms//os:linux",
+ "@platforms//cpu:armv7",
+ "//tools/platforms/hardware:raspberry_pi",
+ ],
+)
+
+platform(
+ name = "linux_arm64",
+ constraint_values = [
+ "@platforms//os:linux",
+ "@platforms//cpu:arm64",
+ ],
+)
+
+platform(
+ name = "linux_roborio",
+ constraint_values = [
+ "@platforms//os:linux",
+ "@platforms//cpu:armv7",
+ "//tools/platforms/hardware:roborio",
+ ],
+)
+
+platform(
+ name = "cortex_m4f",
+ constraint_values = [
+ "@platforms//os:none",
+ "//tools/platforms/hardware:cortex_m4f",
+ ],
+)
+
+# TODO(phil): Create something for "cortex-m4f-k22" builds.
diff --git a/tools/platforms/hardware/BUILD b/tools/platforms/hardware/BUILD
new file mode 100644
index 0000000..521b243
--- /dev/null
+++ b/tools/platforms/hardware/BUILD
@@ -0,0 +1,21 @@
+# Additional constraint values not available in @platforms. Use these in things
+# like select() statements or target_compatible_with.
+
+package(default_visibility = ["//visibility:public"])
+
+constraint_setting(name = "hardware")
+
+constraint_value(
+ name = "raspberry_pi",
+ constraint_setting = ":hardware",
+)
+
+constraint_value(
+ name = "roborio",
+ constraint_setting = ":hardware",
+)
+
+constraint_value(
+ name = "cortex_m4f",
+ constraint_setting = ":hardware",
+)