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_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.