Upgrade arm-none-eabi compiler

It was 5 years out of date and didn't fully support C++20.

In doing so, flip off "freestanding" mode for the cortex-m4f compiler's
use of libstdc++.
This is because some libraries appear to have been using STL headers
that cannot be safely linked against in freestanding mode (e.g.,
<ostream>). I am unsure why the prior compiler was not catching this.
If this becomes a problem, we can disable it.

Change-Id: Ie08ce2c089f787b0433a761ad2242dcd2a516404
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/WORKSPACE b/WORKSPACE
index 35715d2..d76d359 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -623,13 +623,13 @@
 )
 
 # Downloaded from
-# https://developer.arm.com/-/media/Files/downloads/gnu-rm/7-2018q2/gcc-arm-none-eabi-7-2018-q2-update-linux.tar.bz2?revision=bc2c96c0-14b5-4bb4-9f18-bceb4050fee7?product=GNU%20Arm%20Embedded%20Toolchain,64-bit,,Linux,7-2018-q2-update
+# From https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads
 http_archive(
     name = "gcc_arm_none_eabi",
     build_file = "@//:compilers/gcc_arm_none_eabi.BUILD",
-    sha256 = "bb17109f0ee697254a5d4ae6e5e01440e3ea8f0277f2e8169bf95d07c7d5fe69",
-    strip_prefix = "gcc-arm-none-eabi-7-2018-q2-update/",
-    url = "https://software.frc971.org/Build-Dependencies/gcc-arm-none-eabi-7-2018-q2-update-linux.tar.bz2",
+    sha256 = "6cd1bbc1d9ae57312bcd169ae283153a9572bd6a8e4eeae2fedfbc33b115fdbb",
+    strip_prefix = "arm-gnu-toolchain-13.2.Rel1-x86_64-arm-none-eabi",
+    url = "https://developer.arm.com/-/media/Files/downloads/gnu/13.2.rel1/binrel/arm-gnu-toolchain-13.2.rel1-x86_64-arm-none-eabi.tar.xz",
 )
 
 # Java11 JDK.
diff --git a/build_tests/BUILD b/build_tests/BUILD
index c097726..f0ff94d 100644
--- a/build_tests/BUILD
+++ b/build_tests/BUILD
@@ -213,6 +213,7 @@
     srcs = [
         "upstream_python_test.py",
     ],
+    target_compatible_with = ["@platforms//os:linux"],
     deps = [
         "@pip//matplotlib",
         "@pip//numpy",
diff --git a/compilers/gcc_arm_none_eabi.BUILD b/compilers/gcc_arm_none_eabi.BUILD
index db2cc77..ba2462a 100644
--- a/compilers/gcc_arm_none_eabi.BUILD
+++ b/compilers/gcc_arm_none_eabi.BUILD
@@ -4,6 +4,7 @@
     name = "gcc",
     srcs = [
         "bin/arm-none-eabi-gcc",
+        ":compiler_pieces",
     ],
 )
 
@@ -61,6 +62,7 @@
     srcs = glob([
         "arm-none-eabi/**",
         "lib/gcc/arm-none-eabi/**",
+        "libexec/gcc/arm-none-eabi/**",
         "include/**",
     ]),
 )
diff --git a/motors/button_board.cc b/motors/button_board.cc
index fb17328..1d20fdb 100644
--- a/motors/button_board.cc
+++ b/motors/button_board.cc
@@ -289,7 +289,11 @@
   PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2);
 
   // .1ms filter time.
-  PORTA_DFWR = PORTB_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000;
+  PORTA_DFWR = 6000;
+  PORTB_DFWR = 6000;
+  PORTC_DFWR = 6000;
+  PORTD_DFWR = 6000;
+  PORTE_DFWR = 6000;
 
   // Set up the buttons. The LEDs pull them up to 5V, so the Teensy needs to not
   // be set to pull up.
diff --git a/motors/motor.cc b/motors/motor.cc
index a343301..ed1296c 100644
--- a/motors/motor.cc
+++ b/motors/motor.cc
@@ -40,8 +40,10 @@
               "Need to prescale");
 
 void Motor::Init() {
-  pwm_ftm_->CNTIN = encoder_ftm_->CNTIN = 0;
-  pwm_ftm_->CNT = encoder_ftm_->CNT = 0;
+  pwm_ftm_->CNTIN = 0;
+  encoder_ftm_->CNTIN = 0;
+  pwm_ftm_->CNT = 0;
+  encoder_ftm_->CNT = 0;
 
   pwm_ftm_->MOD = counts_per_cycle() - 1;
   encoder_ftm_->MOD = controls_->mechanical_counts_per_revolution() - 1;
diff --git a/motors/peripheral/adc_dma.cc b/motors/peripheral/adc_dma.cc
index d2dc6c9..15d50eb 100644
--- a/motors/peripheral/adc_dma.cc
+++ b/motors/peripheral/adc_dma.cc
@@ -147,17 +147,16 @@
   DMAMUX0.CHCFG[reconfigure_dma_channel(0)] = 0;
   DMAMUX0.CHCFG[reconfigure_dma_channel(1)] = 0;
 
-  static constexpr ssize_t kResultABOffset =
-      static_cast<ssize_t>(offsetof(KINETIS_ADC_t, RB)) -
-      static_cast<ssize_t>(offsetof(KINETIS_ADC_t, RA));
+  static constexpr long kResultABOffset =
+      static_cast<long>(offsetof(KINETIS_ADC_t, RB)) -
+      static_cast<long>(offsetof(KINETIS_ADC_t, RA));
   static_assert(kResultABOffset > 0, "Offset is backwards");
-  static constexpr ssize_t kResultOffsetBits =
-      ConstexprLog2(kResultABOffset * 2);
-  static constexpr ssize_t kSC1ABOffset =
-      static_cast<ssize_t>(offsetof(KINETIS_ADC_t, SC1B)) -
-      static_cast<ssize_t>(offsetof(KINETIS_ADC_t, SC1A));
+  static constexpr long kResultOffsetBits = ConstexprLog2(kResultABOffset * 2);
+  static constexpr long kSC1ABOffset =
+      static_cast<long>(offsetof(KINETIS_ADC_t, SC1B)) -
+      static_cast<long>(offsetof(KINETIS_ADC_t, SC1A));
   static_assert(kSC1ABOffset > 0, "Offset is backwards");
-  static constexpr ssize_t kSC1OffsetBits = ConstexprLog2(kSC1ABOffset * 2);
+  static constexpr long kSC1OffsetBits = ConstexprLog2(kSC1ABOffset * 2);
   for (int adc = 0; adc < 2; ++adc) {
     // Make sure we can actually write to all the fields in the DMA channels.
     DMA.CDNE = result_dma_channel(adc);
@@ -204,16 +203,16 @@
       if (next_result_channel != -1) {
         link = M_TCD_ELINK | V_TCD_LINKCH(next_result_channel);
       }
-      result_dma(adc)->CITER = result_dma(adc)->BITER =
-          link | 4 /* 4 minor iterations */;
+      result_dma(adc)->CITER = link | 4 /* 4 minor iterations */;
+      result_dma(adc)->BITER = link | 4 /* 4 minor iterations */;
     }
     {
       uint16_t link = 0;
       if (next_reconfigure_channel != -1) {
         link = M_TCD_ELINK | V_TCD_LINKCH(next_reconfigure_channel);
       }
-      reconfigure_dma(adc)->CITER = reconfigure_dma(adc)->BITER =
-          link | 4 /* 4 minor iterations */;
+      reconfigure_dma(adc)->CITER = link | 4 /* 4 minor iterations */;
+      reconfigure_dma(adc)->BITER = link | 4 /* 4 minor iterations */;
     }
     result_dma(adc)->DLASTSGA = -(kNumberAdcSamples * sizeof(uint16_t));
     reconfigure_dma(adc)->DLASTSGA = 0;
diff --git a/motors/pistol_grip/controller.cc b/motors/pistol_grip/controller.cc
index 5a0d5a2..a88b9a0 100644
--- a/motors/pistol_grip/controller.cc
+++ b/motors/pistol_grip/controller.cc
@@ -792,7 +792,8 @@
   uint32_t motor0_sum = 0, motor1_sum = 0, wheel_sum = 0;
 
   // First clear both encoders.
-  MOTOR0_ENCODER_FTM->CNT = MOTOR1_ENCODER_FTM->CNT = 0;
+  MOTOR0_ENCODER_FTM->CNT = 0;
+  MOTOR1_ENCODER_FTM->CNT = 0;
   for (int i = 0; i < kNumberSamples; ++i) {
     delay(1);
 
@@ -847,7 +848,10 @@
   PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2);
 
   // .1ms filter time.
-  PORTA_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000;
+  PORTA_DFWR = 6000;
+  PORTC_DFWR = 6000;
+  PORTD_DFWR = 6000;
+  PORTE_DFWR = 6000;
 
   // BTN0
   PORTC_PCR7 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);
@@ -946,8 +950,8 @@
   PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN;
 
   // Have them both wait for the GTB signal.
-  FTM0->CONF = FTM3->CONF =
-      FTM_CONF_GTBEEN | FTM_CONF_NUMTOF(kSwitchingDivisor - 1);
+  FTM0->CONF = FTM_CONF_GTBEEN | FTM_CONF_NUMTOF(kSwitchingDivisor - 1);
+  FTM3->CONF = FTM_CONF_GTBEEN | FTM_CONF_NUMTOF(kSwitchingDivisor - 1);
   // Make FTM3's period half of what it should be so we can get it a half-cycle
   // out of phase.
   const uint32_t original_mod = FTM3->MOD;
diff --git a/tools/cpp/cortex_m4f_crosstool.pb b/tools/cpp/cortex_m4f_crosstool.pb
deleted file mode 100644
index 68114e6..0000000
--- a/tools/cpp/cortex_m4f_crosstool.pb
+++ /dev/null
@@ -1,240 +0,0 @@
-toolchain {
-  abi_version: "%NAME%"
-  abi_libc_version: "%NAME%"
-  builtin_sysroot: ""
-  compiler: "gcc"
-  host_system_name: "local"
-  needsPic: false
-  supports_gold_linker: false
-  supports_incremental_linker: false
-  supports_fission: false
-  supports_interface_shared_objects: false
-  supports_normalizing_ar: false
-  supports_start_end_lib: false
-  supports_thin_archives: false
-  target_libc: "%NAME%"
-  target_cpu: "%NAME%"
-  target_system_name: "%NAME%"
-  toolchain_identifier: "%NAME%"
-
-  tool_path { name: "ar" path: "gcc_arm_none_eabi/arm-none-eabi-ar" }
-  tool_path { name: "compat-ld" path: "gcc_arm_none_eabi/arm-none-eabi-ld" }
-  tool_path { name: "cpp" path: "gcc_arm_none_eabi/arm-none-eabi-cpp" }
-  tool_path { name: "dwp" path: "gcc_arm_none_eabi/arm-none-eabi-dwp" }
-  tool_path { name: "gcc" path: "gcc_arm_none_eabi/arm-none-eabi-gcc" }
-  tool_path { name: "gcov" path: "gcc_arm_none_eabi/arm-none-eabi-gcov" }
-  # C(++) compiles invoke the compiler (as that is the one knowing where
-  # to find libraries), but we provide LD so other rules can invoke the linker.
-  tool_path { name: "ld" path: "gcc_arm_none_eabi/arm-none-eabi-ld" }
-  tool_path { name: "nm" path: "gcc_arm_none_eabi/arm-none-eabi-nm" }
-  tool_path { name: "objcopy" path: "gcc_arm_none_eabi/arm-none-eabi-objcopy" }
-  objcopy_embed_flag: "-I"
-  objcopy_embed_flag: "binary"
-  tool_path { name: "objdump" path: "gcc_arm_none_eabi/arm-none-eabi-objdump" }
-  tool_path { name: "strip" path: "gcc_arm_none_eabi/arm-none-eabi-strip" }
-  linking_mode_flags { mode: FULLY_STATIC }
-
-  # TODO(bazel-team): In theory, the path here ought to exactly match the path
-  # used by gcc. That works because bazel currently doesn't track files at
-  # absolute locations and has no remote execution, yet. However, this will need
-  # to be fixed, maybe with auto-detection?
-  cxx_builtin_include_directory: '/usr/lib/gcc/arm-none-eabi/4.8/include'
-  cxx_builtin_include_directory: '/usr/lib/gcc/arm-none-eabi/4.8/include-fixed'
-  cxx_builtin_include_directory: '/usr/lib/arm-none-eabi/include'
-  cxx_builtin_include_directory: '/usr/include/newlib',
-
-  feature {
-    name: "dbg"
-    implies: "all_modes"
-    flag_set {
-      action: "preprocess-assemble"
-      action: "c-compile"
-      action: "c++-compile"
-      action: "c++-header-parsing"
-      action: "c++-header-preprocessing"
-      action: "c++-module-compile"
-      flag_group {
-        flag: "-fno-omit-frame-pointer"
-      }
-    }
-  }
-
-  feature {
-    name: "opt"
-    implies: "all_modes"
-  }
-  feature {
-    name: "fastbuild"
-    implies: "all_modes"
-  }
-
-  feature {
-    name: "all_modes"
-    flag_set {
-      action: "preprocess-assemble"
-      action: "assemble"
-      action: "c-compile"
-      flag_group {
-        flag: "--std=gnu99"
-      }
-    }
-    flag_set {
-      action: "c++-compile"
-      action: "c++-header-parsing"
-      action: "c++-header-preprocessing"
-      action: "c++-module-compile"
-      flag_group {
-        flag: "--std=gnu++1z"
-        flag: "-fno-exceptions"
-        flag: "-fno-rtti"
-      }
-    }
-  }
-
-  # Anticipated future default.
-  # This makes GCC and Clang do what we want when called through symlinks.
-  unfiltered_cxx_flag: "-no-canonical-prefixes"
-  linker_flag: "-no-canonical-prefixes"
-
-  # Things that the code wants defined.
-  compiler_flag: "-D__STDC_FORMAT_MACROS"
-  compiler_flag: "-D__STDC_CONSTANT_MACROS"
-  compiler_flag: "-D__STDC_LIMIT_MACROS"
-
-  # Some identifiers for what MCU we're using.
-  compiler_flag: "-D%CPU%"
-  compiler_flag: "-DF_CPU=%F_CPU%"
-
-  compiler_flag: "-Wl,--gc-sections"
-
-  # Newlib's stdint.h does this, but GCC's freestanding stdint.h doesn't use
-  # newlib's so we have to do it manually...
-  compiler_flag: "-D__have_long32"
-
-  # Make C++ compilation deterministic. Use linkstamping instead of these
-  # compiler symbols.
-  unfiltered_cxx_flag: "-Wno-builtin-macro-redefined"
-  unfiltered_cxx_flag: "-D__DATE__=\"redacted\""
-  unfiltered_cxx_flag: "-D__TIMESTAMP__=\"redacted\""
-  unfiltered_cxx_flag: "-D__TIME__=\"redacted\""
-
-  # Security hardening on by default.
-  # Conservative choice; -D_FORTIFY_SOURCE=2 may be unsafe in some cases.
-  # We need to undef it before redefining it as some distributions now have
-  # it enabled by default.
-  compiler_flag: "-fstack-protector"
-  compiler_flag: "-mcpu=cortex-m4"
-  compiler_flag: "-mfpu=fpv4-sp-d16"
-  compiler_flag: "-mthumb"
-  compiler_flag: "-mfloat-abi=hard"
-  compiler_flag: "-fno-strict-aliasing"
-  linker_flag: "-mcpu=cortex-m4"
-  linker_flag: "-mfpu=fpv4-sp-d16"
-  linker_flag: "-mthumb"
-  linker_flag: "-mfloat-abi=hard"
-  linker_flag: "-fno-strict-aliasing"
-  linker_flag: "--specs=nano.specs"
-
-  # Pretty much everything needs this, including parts of the glibc STL...
-  linker_flag: "-lgcc"
-  linker_flag: "-lstdc++_nano"
-  linker_flag: "-lm"
-  linker_flag: "-lc_nano"
-  linker_flag: "-T%LINKER_SCRIPT%"
-  linker_flag: "-Tmotors/core/kinetis_sections.ld"
-
-  compiler_flag: "-fmessage-length=80"
-  compiler_flag: "-fmax-errors=20"
-
-  compiler_flag: "-Wall"
-  compiler_flag: "-Wextra"
-  compiler_flag: "-Wpointer-arith"
-  compiler_flag: "-Wcast-qual"
-  compiler_flag: "-Wwrite-strings"
-  compiler_flag: "-Wtype-limits"
-  compiler_flag: "-Wsign-compare"
-  compiler_flag: "-Wformat=2"
-  compiler_flag: "-Werror"
-  compiler_flag: "-Wstrict-aliasing=2"
-
-  # TODO(Brian): Drop these once we upgrade Eigen.
-  compiler_flag: "-Wno-misleading-indentation"
-  compiler_flag: "-Wno-int-in-bool-context"
-
-  # Be annoying about using doubles places we probably didn't mean to, because
-  # the FPU only does single-precision.
-  compiler_flag: "-Wdouble-promotion"
-
-  # Don't use temp files while compiling.
-  compiler_flag: "-pipe"
-
-  # Stamp the binary with a unique identifier.
-  # TODO(austin): Put these back in.
-  #linker_flag: "-Wl,--build-id=md5"
-  #linker_flag: "-Wl,--hash-style=gnu"
-
-  # Enable debug symbols.
-  compiler_flag: "-g"
-
-  # Common symbols are weird and not what we want, so just give multiple
-  # declaration errors instead.
-  compiler_flag: "-fno-common"
-
-  # We're not a hosted environment (no file IO, main is called from our code,
-  # etc).
-  compiler_flag: "-ffreestanding"
-  # However, we still want to optimize things like memcpy.
-  compiler_flag: "-fbuiltin"
-
-  compilation_mode_flags {
-    mode: OPT
-
-    # Freescale recommends this combination for reducing cycle count.
-    # http://www.nxp.com/assets/documents/data/en/application-notes/AN4808.pdf
-    compiler_flag: "-O2"
-    compiler_flag: "-finline-functions"
-
-    # This is definitely worth it for us. It makes the FPU a lot more useful,
-    # especially with complex arithmetic, which matters a lot.
-    compiler_flag: "-ffast-math"
-
-    # It seems like this is a good idea, at least for the number crunching code.
-    # Might want to look into moving it to copts on specific rules if the code
-    # size increase becomes a problem.
-    compiler_flag: "-funroll-loops"
-
-    # Disable assertions
-    compiler_flag: "-DNDEBUG"
-
-    # Removal of unused code and data at link time (can this increase binary size in some cases?).
-    compiler_flag: "-ffunction-sections"
-    #compiler_flag: "-fdata-sections"
-    linker_flag: "-Wl,--gc-sections"
-  }
-
-  feature {
-    name: 'include_paths'
-    flag_set {
-      action: 'preprocess-assemble'
-      action: 'c-compile'
-      action: 'c++-compile'
-      action: 'c++-header-parsing'
-      action: 'c++-header-preprocessing'
-      action: 'c++-module-compile'
-      flag_group {
-        iterate_over: 'quote_include_paths'
-        flag: '-iquote'
-        flag: '%{quote_include_paths}'
-      }
-      flag_group {
-        iterate_over: 'include_paths'
-        flag: '-I%{include_paths}'
-      }
-      flag_group {
-        iterate_over: 'system_include_paths'
-        flag: '-I'
-        flag: '%{system_include_paths}'
-      }
-    }
-  }
-}
diff --git a/tools/cpp/gcc_arm_none_eabi/arm-none-eabi-gcc b/tools/cpp/gcc_arm_none_eabi/arm-none-eabi-gcc
index 6ff0448..b7146d0 100755
--- a/tools/cpp/gcc_arm_none_eabi/arm-none-eabi-gcc
+++ b/tools/cpp/gcc_arm_none_eabi/arm-none-eabi-gcc
@@ -1,6 +1,6 @@
 #!/bin/bash --norc
 
-PATH="${BAZEL_OUTPUT_ROOT}external/gcc_arm_none_eabi/lib/gcc/arm-none-eabi/7.3.1:$PATH" \
+PATH="${BAZEL_OUTPUT_ROOT}external/gcc_arm_none_eabi/lib/gcc/arm-none-eabi/13.2.1:${BAZEL_OUTPUT_ROOT}external/gcc_arm_none_eabi/libexec/gcc/arm-none-eabi/13.2.1:${PATH}" \
 	exec \
 	${BAZEL_OUTPUT_ROOT}external/gcc_arm_none_eabi/bin/arm-none-eabi-gcc \
 	"$@"
diff --git a/tools/cpp/toolchain_config.bzl b/tools/cpp/toolchain_config.bzl
index 8bab5c7..5e320cd 100644
--- a/tools/cpp/toolchain_config.bzl
+++ b/tools/cpp/toolchain_config.bzl
@@ -296,7 +296,6 @@
                                 "-pipe",
                                 "-g",
                                 "-fno-common",
-                                "-ffreestanding",
                                 "-fbuiltin",
                             ],
                         ),
@@ -797,7 +796,7 @@
                     actions = all_cpp_compile_actions,
                     flag_groups = [
                         flag_group(
-                            flags = ["--std=gnu++1z", "-fno-exceptions", "-fno-rtti"],
+                            flags = ["--std=gnu++20", "-fno-exceptions", "-fno-rtti"],
                         ),
                     ],
                 ),
diff --git a/tools/dependency_rewrite b/tools/dependency_rewrite
index 2cc90cf..2d2e610 100644
--- a/tools/dependency_rewrite
+++ b/tools/dependency_rewrite
@@ -3,6 +3,7 @@
 block *
 rewrite maven.ctr-electronics.com/(.*) software.frc971.org/Build-Dependencies/maven.ctr-electronics.com/$1
 rewrite github.com/(.*) software.frc971.org/Build-Dependencies/github.com/$1
+rewrite developer.arm.com/(.*) software.frc971.org/Build-Dependencies/developer.arm.com/$1
 rewrite dl.google.com/(.*) software.frc971.org/Build-Dependencies/dl.google.com/$1
 rewrite mirror.bazel.build/(.*) software.frc971.org/Build-Dependencies/mirror.bazel.build/$1
 rewrite nodejs.org/(.*) software.frc971.org/Build-Dependencies/nodejs.org/$1
diff --git a/y2019/vision/constants.h b/y2019/vision/constants.h
index 8510f4c..959f5b7 100644
--- a/y2019/vision/constants.h
+++ b/y2019/vision/constants.h
@@ -3,6 +3,7 @@
 
 #include <array>
 #include <cmath>
+#include <cstdint>
 #include <string>
 
 namespace y2019 {