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/y2020/vision/BUILD b/y2020/vision/BUILD
index aaad085..a58ed8c 100644
--- a/y2020/vision/BUILD
+++ b/y2020/vision/BUILD
@@ -5,6 +5,7 @@
     name = "vision_fbs",
     srcs = ["vision.fbs"],
     gen_reflections = 1,
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
 )
 
@@ -16,6 +17,7 @@
     hdrs = [
         "v4l2_reader.h",
     ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
     deps = [
         ":vision_fbs",
@@ -34,10 +36,7 @@
     data = [
         "//y2020:config",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
     deps = [
         ":v4l2_reader",
@@ -57,6 +56,7 @@
 flatbuffer_ts_library(
     name = "vision_ts_fbs",
     srcs = ["vision.fbs"],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
 )
 
@@ -68,10 +68,7 @@
     data = [
         "//y2020:config",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
     deps = [
         ":vision_fbs",
@@ -90,10 +87,7 @@
     data = [
         "//y2020:config",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
     deps = [
         ":vision_fbs",
@@ -117,10 +111,7 @@
     data = [
         "//y2020:config",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
     deps = [
         ":vision_fbs",
diff --git a/y2020/vision/sift/BUILD b/y2020/vision/sift/BUILD
index 158aa94..4a0dcfb 100644
--- a/y2020/vision/sift/BUILD
+++ b/y2020/vision/sift/BUILD
@@ -6,10 +6,7 @@
     srcs = [
         "fast_gaussian_generator.cc",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         "//third_party:halide",
         "//third_party:halide_gengen",
@@ -25,18 +22,15 @@
     ],
     data = [
         ":fast_gaussian_generator",
-        # TODO(Brian): Replace this with something more fine-grained from the
-        # configuration fragment or something.
-        "//tools/cpp:toolchain",
         "@amd64_debian_sysroot//:sysroot_files",
     ],
-    python_version = "PY3",
-    main = "fast_gaussian_runner.py",
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
+    toolchains = [
+        "@bazel_tools//tools/cpp:current_cc_toolchain",
     ],
+    main = "fast_gaussian_runner.py",
+    python_version = "PY3",
     srcs_version = "PY2AND3",
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         "@bazel_tools//tools/python/runfiles",
     ],
@@ -121,10 +115,7 @@
     hdrs = [
         "sift971.h",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
     deps = [
         ":fast_gaussian",
@@ -141,10 +132,7 @@
     hdrs = [
         "fast_gaussian.h",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":fast_gaussian_all",
         "//third_party:halide_runtime",
@@ -158,10 +146,7 @@
     srcs = [
         "fast_gaussian_test.cc",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":fast_gaussian",
         "//aos/testing:googletest",
@@ -174,10 +159,7 @@
     srcs = [
         "testing_sift.cc",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":fast_gaussian",
         "//aos:init",
@@ -207,6 +189,7 @@
         "TrainingImage",
         "TrainingData",
     ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
 )
 
@@ -214,12 +197,14 @@
     name = "sift_fbs",
     srcs = ["sift.fbs"],
     gen_reflections = True,
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
 )
 
 flatbuffer_ts_library(
     name = "sift_ts_fbs",
     srcs = ["sift.fbs"],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//y2020:__subpackages__"],
 )
 
@@ -228,6 +213,7 @@
     srcs = ["sift_training.fbs"],
     gen_reflections = True,
     includes = [":sift_fbs_includes"],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
 )
 
@@ -236,6 +222,7 @@
     srcs = ["demo_sift_training.py"],
     python_version = "PY3",
     srcs_version = "PY2AND3",
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":sift_fbs_python",
         "@opencv_contrib_nonfree_amd64//:python_opencv",
@@ -255,6 +242,7 @@
         "$(location images/demo/FRC-Image4-cleaned.png)",
         "$(location demo_sift.h)",
     ]),
+    target_compatible_with = ["@platforms//os:linux"],
     tools = [
         ":demo_sift_training",
     ],
@@ -265,5 +253,6 @@
     hdrs = [
         "demo_sift.h",
     ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
 )
diff --git a/y2020/vision/sift/fast_gaussian.bzl b/y2020/vision/sift/fast_gaussian.bzl
index 0905423..5eaa4bf 100644
--- a/y2020/vision/sift/fast_gaussian.bzl
+++ b/y2020/vision/sift/fast_gaussian.bzl
@@ -1,3 +1,6 @@
+load("//tools:platforms.bzl", "platforms")
+load("//tools/build_rules:select.bzl", "cpu_select")
+
 def fast_gaussian(sigmas, sizes):
     files = []
     for _, sigma_name, _ in sigmas:
@@ -28,17 +31,22 @@
             ":fast_gaussian_runner",
         ],
         cmd = " ".join([
-            "$(location fast_gaussian_runner)",
+            "$(location :fast_gaussian_runner)",
             "'" + params.to_json() + "'",
-            # TODO(Brian): This should be RULEDIR once we have support for that.
-            "$(@D)",
-            "$(TARGET_CPU)",
-        ]),
+            "$(RULEDIR)",
+        ]) + " " + cpu_select({
+            "amd64": "k8",
+            "roborio": "roborio",
+            "armhf": "armhf-debian",
+            "cortex-m": "cortex-m",
+        }),
         outs = headers + objects + htmls,
-        restricted_to = [
-            "//tools:k8",
-            "//tools:armhf-debian",
-        ],
+        # The tool doesn't support anything other than k8 and armhf-debian
+        # right now.
+        target_compatible_with = platforms.any_of([
+            "@platforms//cpu:x86_64",
+            "//tools/platforms/hardware:raspberry_pi",
+        ]),
     )
 
     native.cc_library(
@@ -48,8 +56,4 @@
         deps = [
             "//third_party:halide_runtime",
         ],
-        restricted_to = [
-            "//tools:k8",
-            "//tools:armhf-debian",
-        ],
     )
diff --git a/y2020/vision/tools/python_code/BUILD b/y2020/vision/tools/python_code/BUILD
index 9b4eb08..4ff3397 100644
--- a/y2020/vision/tools/python_code/BUILD
+++ b/y2020/vision/tools/python_code/BUILD
@@ -1,4 +1,5 @@
 load("@com_github_google_flatbuffers//:build_defs.bzl", "flatbuffer_cc_library", "flatbuffer_py_library")
+load("//tools:platforms.bzl", "platforms")
 
 py_binary(
     name = "load_sift_training",
@@ -17,6 +18,7 @@
     ]),
     python_version = "PY3",
     srcs_version = "PY2AND3",
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         "//external:python-glog",
         "//y2020/vision/sift:sift_fbs_python",
@@ -34,6 +36,7 @@
         "$(location :load_sift_training)",
         "$(location sift_training_data.h)",
     ]),
+    target_compatible_with = ["@platforms//os:linux"],
     tools = [
         ":load_sift_training",
     ],
@@ -44,6 +47,7 @@
     hdrs = [
         "sift_training_data.h",
     ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
 )
 
@@ -63,9 +67,10 @@
     data = glob(["calib_files/*.json"]) + glob([
         "test_images/*.png",
     ]),
-    python_version = "PY3",
     main = "load_sift_training.py",
+    python_version = "PY3",
     srcs_version = "PY2AND3",
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":load_sift_training",
         "//external:python-glog",
@@ -84,6 +89,7 @@
         "$(location :load_sift_training_test)",
         "$(location sift_training_data_test.h) test",
     ]),
+    target_compatible_with = ["@platforms//os:linux"],
     tools = [
         ":load_sift_training_test",
     ],
@@ -94,6 +100,7 @@
     hdrs = [
         "sift_training_data_test.h",
     ],
+    target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
 )
 
@@ -102,10 +109,7 @@
     srcs = [
         "camera_param_test.cc",
     ],
-    restricted_to = [
-        "//tools:k8",
-        "//tools:armhf-debian",
-    ],
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         ":sift_training_data_test",
         "//aos/testing:googletest",