Backport from upstream patch. Much of this is only for macos aarch64,
but it includes a piece that fixes build with newer setuptools as well.
Keeping the whole lot together because it's easier to handle the diff
that way.

(ports/games/0ad/Makefile.inc sets ONLY_FOR_ARCHS=amd64 i386 anyway
so this won't actually be built on aarch64 as-is).

Index: libraries/source/spidermonkey/FixMacBuild.diff
--- libraries/source/spidermonkey/FixMacBuild.diff.orig
+++ libraries/source/spidermonkey/FixMacBuild.diff
@@ -13,12 +13,84 @@
                  js_flags.append('-Xclang')
 --- a/build/moz.configure/toolchain.configure
 +++ b/build/moz.configure/toolchain.configure
-@@ -141,7 +141,7 @@
+@@ -141,7 +141,6 @@
      @imports(_from='biplist', _import='readPlist')
      def macos_sdk(sdk, host):
          sdk_min_version = Version('10.11')
 -        sdk_max_version = Version('10.15.4')
-+        sdk_max_version = Version('11.99')
  
          if sdk:
-             sdk = sdk[0]
\ No newline at end of file
+             sdk = sdk[0]
+@@ -170,11 +169,6 @@
+                 'You may need to point to it using --with-macos-sdk=<path> in your '
+                 'mozconfig. Various SDK versions are available from '
+                 'https://github.com/phracker/MacOSX-SDKs' % (version, sdk_min_version))
+-        if version > sdk_max_version:
+-            die('SDK version "%s" is unsupported. Please downgrade to version '
+-                '%s. You may need to point to it using --with-macos-sdk=<path> in '
+-                'your mozconfig. Various SDK versions are available from '
+-                'https://github.com/phracker/MacOSX-SDKs' % (version, sdk_max_version))
+         return sdk
+ 
+     set_config('MACOS_SDK_DIR', macos_sdk)
+--- a/build/moz.configure/init.configure
++++ b/build/moz.configure/init.configure
+@@ -375,9 +375,6 @@
+         sys.exit(subprocess.call([python] + sys.argv))
+ 
+     # We are now in the virtualenv
+-    if not distutils.sysconfig.get_python_lib():
+-        die('Could not determine python site packages directory')
+-
+     str_version = '.'.join(str(v) for v in version)
+ 
+     return namespace(
+--- a/js/src/jit/arm64/vixl/MozCpu-vixl.cpp
++++ b/js/src/jit/arm64/vixl/MozCpu-vixl.cpp
+@@ -69,7 +69,7 @@ void CPU::SetUp() {
+ 
+ 
+ uint32_t CPU::GetCacheType() {
+-#if defined(__aarch64__) && !defined(_MSC_VER)
++#if defined(__aarch64__) && (defined(__linux__) || defined(__android__))
+   uint64_t cache_type_register;
+   // Copy the content of the cache type register to a core register.
+   __asm__ __volatile__ ("mrs %[ctr], ctr_el0"  // NOLINT
+--- a/js/src/wasm/WasmSignalHandlers.cpp
++++ b/js/src/wasm/WasmSignalHandlers.cpp
+@@ -226,6 +226,10 @@ using mozilla::DebugOnly;
+ #  define R13_sig(p) ((p)->thread.__sp)
+ #  define R14_sig(p) ((p)->thread.__lr)
+ #  define R15_sig(p) ((p)->thread.__pc)
++#  define EPC_sig(p) ((p)->thread.__pc)
++#  define RFP_sig(p) ((p)->thread.__fp)
++#  define R31_sig(p) ((p)->thread.__sp)
++#  define RLR_sig(p) ((p)->thread.__lr)
+ #else
+ #  error "Don't know how to read/write to the thread state via the mcontext_t."
+ #endif
+@@ -351,6 +355,12 @@ struct macos_arm_context {
+   arm_neon_state_t float_;
+ };
+ #    define CONTEXT macos_arm_context
++#  elif defined(__aarch64__)
++struct macos_aarch64_context {
++  arm_thread_state64_t thread;
++  arm_neon_state64_t float_;
++};
++#    define CONTEXT macos_aarch64_context
+ #  else
+ #    error Unsupported architecture
+ #  endif
+@@ -816,6 +826,11 @@ static bool HandleMachException(const ExceptionRequest& request) {
+   unsigned int float_state_count = ARM_NEON_STATE_COUNT;
+   int thread_state = ARM_THREAD_STATE;
+   int float_state = ARM_NEON_STATE;
++#  elif defined(__aarch64__)
++  unsigned int thread_state_count = ARM_THREAD_STATE64_COUNT;
++  unsigned int float_state_count = ARM_NEON_STATE64_COUNT;
++  int thread_state = ARM_THREAD_STATE64;
++  int float_state = ARM_NEON_STATE64;
+ #  else
+ #    error Unsupported architecture
+ #  endif
