Delete platform_mappings and switch to platform suffixes

As far as I can tell, we only stuck with the `--cpu` legacy mechanism
to get different output bases for each platform. This can be achieved
with `--platform_suffix` as well.

I want folks (especially students) to move away from thinking about
compiling for specific CPUs. Instead the students should think of
target platforms. Along the lines of "I want to build for the Pi"
instead of "I want to build for an armv7 machine". The renaming of
config options will happen in a future patch. This patch just changes
everything over to use equivalent config options.

Sample paths for Linux-based targets:

    $ for config in k8 roborio armv7 arm64 cortex-m4f rp2040; do bazel cquery //build_tests:tcmalloc_build_test_binary --output=starlark --starlark:expr='providers(target)["FilesToRunProvider"].executable.path' --config=${config} 2>/dev/null; done
    bazel-out/k8-fastbuild/bin/build_tests/tcmalloc_build_test_binary
    bazel-out/k8-fastbuild-roborio/bin/build_tests/tcmalloc_build_test_binary
    bazel-out/k8-fastbuild-armv7/bin/build_tests/tcmalloc_build_test_binary
    bazel-out/k8-fastbuild-arm64/bin/build_tests/tcmalloc_build_test_binary

Sample paths for embedded targets:

    $ for config in k8 roborio armv7 arm64 cortex-m4f rp2040; do bazel cquery //motors:simple_receiver.elf --output=starlark --starlark:expr='providers(target)["FilesToRunProvider"].executable.path' --config=${config} 2>/dev/null; done
    bazel-out/k8-fastbuild-cortex-m4f/bin/motors/simple_receiver.elf

It's unfortunate that all the names start with `k8-fastbuild`, but I
don't see a way around this.

In our own code the `--cpu` option appears to be used in a meaningful
way only in abseil. I patched one `config_setting` that was checking
for `--cpu=k8` and replaced it with a check for the
`@platforms//cpu:x86_64` constraint. Other than that, it doesn't look
like anything's making use of it.

    $ git grep '"cpu":' -- '*BUILD*' '*bzl'
    third_party/abseil/absl/BUILD.bazel:        "cpu": "ppc",
    third_party/abseil/absl/BUILD.bazel:        "cpu": "wasm32",
    third_party/abseil/absl/copts/configure_copts.bzl:            values = {"cpu": cpu},
    third_party/bazel-toolchain/bazel_tools_changes/tools/cpp/unix_cc_toolchain_config.bzl:        "cpu": attr.string(mandatory = True),
    third_party/gflags/BUILD:    values = {"cpu": "x64_windows"},
    third_party/google-benchmark/BUILD.bazel:        "cpu": "x64_windows",
    tools/cpp/toolchain_config.bzl:        "cpu": attr.string(mandatory = True, values = ["cortex-m4f", "cortex-m4f-k22", "roborio", "rp2040"]),

I looked through our external dependencies to see if any of them need
to be fixed up, but it doesn't look like it.

    $ bazel fetch //...
    $ grep -rnI --include='*BUILD*' --include='*.bzl' '"cpu":' -- '*BUILD*' '*bzl' external/
    grep: *BUILD*: No such file or directory
    grep: *bzl: No such file or directory
    external/rules_python/python/BUILD:77:#         values = {"cpu": "arm"},
    external/boringssl/src/util/BUILD.toplevel:39:    values = {"cpu": "k8"},
    external/boringssl/src/util/BUILD.toplevel:44:    values = {"cpu": "ppc"},
    external/boringssl/src/util/BUILD.toplevel:49:    values = {"cpu": "darwin"},
    external/boringssl/src/util/BUILD.toplevel:54:    values = {"cpu": "x64_windows"},
    external/boringssl/BUILD:56:    values = {"cpu": "ppc"},
    external/boringssl/BUILD:61:    values = {"cpu": "darwin"},
    external/boringssl/BUILD:66:    values = {"cpu": "x64_windows"},
    external/com_github_bazelbuild_buildtools/BUILD.bazel:11:    values = {"cpu": "x64_windows"},
    external/rules_cc/cc/private/toolchain/unix_cc_toolchain_config.bzl:1180:        "cpu": attr.string(mandatory = True),
    external/rules_cc/cc/private/toolchain/cc_toolchain_config.bzl:1486:        "cpu": attr.string(mandatory = True),
    external/rules_cc/cc/private/toolchain/windows_cc_toolchain_config.bzl:1321:        "cpu": attr.string(mandatory = True),
    external/rules_cc/cc/private/toolchain/freebsd_cc_toolchain_config.bzl:303:        "cpu": attr.string(mandatory = True),
    external/io_bazel_rules_go/tests/legacy/examples/cgo/cc_dependency/BUILD.bazel:3:    values = {"cpu": "darwin"},

I also added an OBJCOPY make variable that supports platforms. The
`@bazel_tools//tools/cpp:current_cc_toolchain` target that we were
using doesn't seem to work with platforms.

Change-Id: I7a3dff63a55e6bc5d637dd700473f4fd1351bdbd
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
diff --git a/.bazelrc b/.bazelrc
index be65a4b..b12d5b2 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -22,13 +22,15 @@
 # Shortcuts for selecting the target platform.
 build:k8 --platforms=//tools/platforms:linux_x86
 build:roborio --platforms=//tools/platforms:linux_roborio
+build:roborio --platform_suffix=-roborio
 build:armv7 --platforms=//tools/platforms:linux_armv7
+build:armv7 --platform_suffix=-armv7
 build:arm64 --platforms=//tools/platforms:linux_arm64
+build:arm64 --platform_suffix=-arm64
 build:cortex-m4f --platforms=//tools/platforms:cortex_m4f
+build:cortex-m4f --platform_suffix=-cortex-m4f
 build:rp2040 --platforms=//tools/platforms:rp2040
-
-# Without this, we end up rebuilding from scratch every time we change compilers.  This is needed to make --cpu work (even though it shouldn't be used).
-build --crosstool_top=@//tools/cpp:toolchain --host_crosstool_top=@//tools/cpp:toolchain
+build:rp2040 --platform_suffix=-rp2040
 
 build:asan --copt -fsanitize=address
 build:asan --linkopt -fsanitize=address --linkopt -ldl
diff --git a/frc971/raspi/rootfs/README.md b/frc971/raspi/rootfs/README.md
index f8d9971..b972038 100644
--- a/frc971/raspi/rootfs/README.md
+++ b/frc971/raspi/rootfs/README.md
@@ -69,6 +69,6 @@
   * Download the code:
     Once this is completed, you can boot the pi with the newly flashed SD
     card, and download the code to the pi using:
-      `bazel run -c opt --cpu=armv7 //y2022:pi_download_stripped -- PI_IP_ADDR
+      `bazel run -c opt --config=armv7 //y2022:pi_download_stripped -- PI_IP_ADDR
 
     where PI_IP_ADDR is the IP address of the target pi, e.g., 10.9.71.101
diff --git a/platform_mappings b/platform_mappings
deleted file mode 100644
index 1816850..0000000
--- a/platform_mappings
+++ /dev/null
@@ -1,38 +0,0 @@
-# https://docs.bazel.build/versions/master/platforms-intro.html#platform-mappings
-platforms:
-  //tools/platforms:linux_x86
-    --cpu=k8
-
-  //tools/platforms:linux_roborio
-    --cpu=roborio
-
-  //tools/platforms:linux_armv7
-    --cpu=armv7
-
-  //tools/platforms:cortex_m4f
-    --cpu=cortex-m4f
-
-  //tools/platforms:rp2040
-    --cpu=rp2040
-
-  //tools/platforms:linux_arm64
-    --cpu=arm64
-
-flags:
-  --cpu=k8
-    //tools/platforms:linux_x86
-
-  --cpu=roborio
-    //tools/platforms:linux_roborio
-
-  --cpu=armv7
-    //tools/platforms:linux_armv7
-
-  --cpu=cortex-m4f
-    //tools/platforms:cortex_m4f
-
-  --cpu=rp2040
-    //tools/platforms:rp2040
-
-  --cpu=arm64
-    //tools/platforms:linux_arm64
diff --git a/third_party/abseil/absl/copts/configure_copts.bzl b/third_party/abseil/absl/copts/configure_copts.bzl
index 4d34254..46dc156 100644
--- a/third_party/abseil/absl/copts/configure_copts.bzl
+++ b/third_party/abseil/absl/copts/configure_copts.bzl
@@ -63,7 +63,7 @@
     # These configs have consistent flags to enable HWAES intsructions.
     cpu_configs = [
         "ppc",
-        "k8",
+        #"k8",
         "darwin_x86_64",
         "darwin",
         "x64_windows_msvc",
@@ -74,3 +74,7 @@
             name = "cpu_%s" % cpu,
             values = {"cpu": cpu},
         )
+    native.config_setting(
+        name = "cpu_k8",
+        constraint_values = ["@platforms//cpu:x86_64"],
+    )
diff --git a/third_party/bazel-toolchain/bazel_tools_changes/tools/cpp/unix_cc_toolchain_config.bzl b/third_party/bazel-toolchain/bazel_tools_changes/tools/cpp/unix_cc_toolchain_config.bzl
index 8de828f..0044dce 100755
--- a/third_party/bazel-toolchain/bazel_tools_changes/tools/cpp/unix_cc_toolchain_config.bzl
+++ b/third_party/bazel-toolchain/bazel_tools_changes/tools/cpp/unix_cc_toolchain_config.bzl
@@ -160,6 +160,17 @@
 
     action_configs.append(llvm_cov_action)
 
+    objcopy_embed_data_action = action_config(
+        action_name = "objcopy_embed_data",
+        tools = [
+            tool(
+                path = ctx.attr.tool_paths["objcopy"],
+            ),
+        ],
+    )
+
+    action_configs.append(objcopy_embed_data_action)
+
     supports_pic_feature = feature(
         name = "supports_pic",
         enabled = True,
diff --git a/third_party/pico-sdk/hex.bzl b/third_party/pico-sdk/hex.bzl
index 5e9dfae..31c951c 100644
--- a/third_party/pico-sdk/hex.bzl
+++ b/third_party/pico-sdk/hex.bzl
@@ -7,7 +7,7 @@
         executable = True,
         output_to_bindir = True,
         target_compatible_with = target_compatible_with,
-        toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
+        toolchains = ["//tools/cpp:cc_toolchain_make_variables"],
     )
 
 def uf2_from_elf(name, target_compatible_with = None):
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index db5fa06..5c36559 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -1,19 +1,11 @@
 load(":toolchain_config.bzl", "cc_toolchain_config")
+load(":toolchain_make_variables.bzl", "cc_toolchain_make_variables")
 
 package(default_visibility = ["//visibility:public"])
 
-cc_toolchain_suite(
-    name = "toolchain",
-    toolchains = {
-        "k8": "@llvm_toolchain//:cc-clang-x86_64-linux",
-        "armv7": "@llvm_toolchain//:cc-clang-armv7-linux",
-        "arm64": "@llvm_toolchain//:cc-clang-aarch64-linux",
-        "roborio": ":cc-compiler-roborio",
-        "cortex-m4f": ":cc-compiler-cortex-m4f",
-        "rp2040": ":cc-compiler-rp2040",
-    },
-    visibility = ["//visibility:public"],
-)
+# Use this instead of @bazel_tools//tools/cpp:current_cc_toolchain.
+# This one supports platforms.
+cc_toolchain_make_variables(name = "cc_toolchain_make_variables")
 
 [
     cc_toolchain_config(
diff --git a/tools/cpp/toolchain_make_variables.bzl b/tools/cpp/toolchain_make_variables.bzl
new file mode 100644
index 0000000..7c06e1d
--- /dev/null
+++ b/tools/cpp/toolchain_make_variables.bzl
@@ -0,0 +1,39 @@
+load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
+
+def _cc_toolchain_make_variables_impl(ctx):
+    """Supports make variables for toolchains in a platforms setup.
+
+    The upstream @bazel_tools//tools/cpp:current_cc_toolchain target on its own
+    doesn't appear to work with platforms. It only works when using --cpu.
+    """
+    toolchain = find_cpp_toolchain(ctx)
+
+    feature_configuration = cc_common.configure_features(
+        ctx = ctx,
+        cc_toolchain = toolchain,
+        requested_features = ctx.features,
+        unsupported_features = ctx.disabled_features,
+    )
+    objcopy = cc_common.get_tool_for_action(
+        feature_configuration = feature_configuration,
+        action_name = "objcopy_embed_data",
+    )
+
+    return [
+        platform_common.TemplateVariableInfo({
+            "OBJCOPY": objcopy,
+        }),
+        DefaultInfo(files = toolchain.all_files),
+    ]
+
+cc_toolchain_make_variables = rule(
+    implementation = _cc_toolchain_make_variables_impl,
+    attrs = {
+        # This is a dependency of find_cpp_toolchain().
+        "_toolchain": attr.label(
+            default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
+        ),
+    },
+    fragments = ["cpp"],
+    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
+)