Build GN for mac-arm64

This enables cross-compiling GN for mac-arm64 and uploading the
package to CIPD. Unfortunately, Fuchsia's Clang toolchain that GN
uses doesn't yet contain universal libc++ that would support both
x86_64 and arm64 macOS, so we need to temporarily revert back to
using the system libc++ until the universal one is availabile in
the toolchain.

Change-Id: I958fd2ea3b3ae1304b706bd977a7e3875dfaab6a
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/10740
Reviewed-by: Nico Weber <thakis@chromium.org>
Reviewed-by: Scott Graham <scottmg@chromium.org>
Commit-Queue: Petr Hosek <phosek@google.com>
diff --git a/infra/README.recipes.md b/infra/README.recipes.md
index 869f775..c99a1bd 100644
--- a/infra/README.recipes.md
+++ b/infra/README.recipes.md
@@ -27,7 +27,7 @@
 
 API for using OS X SDK distributed via CIPD.
 
-&emsp; **@contextmanager**<br>&mdash; **def [\_\_call\_\_](/infra/recipe_modules/macos_sdk/api.py#24)(self):**
+&emsp; **@contextmanager**<br>&mdash; **def [\_\_call\_\_](/infra/recipe_modules/macos_sdk/api.py#30)(self):**
 
 Sets up the XCode SDK environment.
 
@@ -60,6 +60,8 @@
 
 Raises:
     StepFailure or InfraFailure.
+
+&emsp; **@property**<br>&mdash; **def [sdk\_dir](/infra/recipe_modules/macos_sdk/api.py#25)(self):**
 ### *recipe_modules* / [target](/infra/recipe_modules/target)
 
 [DEPS](/infra/recipe_modules/target/__init__.py#5): [recipe\_engine/platform][recipe_engine/recipe_modules/platform]
diff --git a/infra/recipe_modules/macos_sdk/api.py b/infra/recipe_modules/macos_sdk/api.py
index 23d0066..cde4a8b 100644
--- a/infra/recipe_modules/macos_sdk/api.py
+++ b/infra/recipe_modules/macos_sdk/api.py
@@ -17,10 +17,16 @@
   def __init__(self, sdk_properties, *args, **kwargs):
     super(MacOSSDKApi, self).__init__(*args, **kwargs)
 
+    self._sdk_dir = None
     self._sdk_version = sdk_properties['sdk_version'].lower()
     self._tool_package = sdk_properties['tool_package']
     self._tool_version = sdk_properties['tool_version']
 
+  @property
+  def sdk_dir(self):
+    assert self._sdk_dir
+    return self._sdk_dir
+
   @contextmanager
   def __call__(self):
     """Sets up the XCode SDK environment.
@@ -61,9 +67,9 @@
 
     try:
       with self.m.context(infra_steps=True):
-        sdk_dir = self._ensure_sdk()
+        self._sdk_dir = self._ensure_sdk()
         self.m.step('select XCode',
-                    ['sudo', 'xcode-select', '--switch', sdk_dir])
+                    ['sudo', 'xcode-select', '--switch', self._sdk_dir])
       yield
     finally:
       with self.m.context(infra_steps=True):
diff --git a/infra/recipe_modules/macos_sdk/examples/full.py b/infra/recipe_modules/macos_sdk/examples/full.py
index 5546ad5..a3348ab 100644
--- a/infra/recipe_modules/macos_sdk/examples/full.py
+++ b/infra/recipe_modules/macos_sdk/examples/full.py
@@ -12,6 +12,7 @@
 
 def RunSteps(api):
   with api.macos_sdk():
+    sdk_dir = api.macos_sdk.sdk_dir if api.platform.is_mac else None
     api.step('gn', ['gn', 'gen', 'out/Release'])
     api.step('ninja', ['ninja', '-C', 'out/Release'])
 
diff --git a/infra/recipe_modules/target/api.py b/infra/recipe_modules/target/api.py
index f128707..850c104 100644
--- a/infra/recipe_modules/target/api.py
+++ b/infra/recipe_modules/target/api.py
@@ -11,7 +11,7 @@
   'linux-amd64': 'x86_64-linux-gnu',
   'linux-arm64': 'aarch64-linux-gnu',
   'mac-amd64': 'x86_64-apple-darwin',
-  'mac-arm64': 'aarch64-apple-darwin',
+  'mac-arm64': 'arm64-apple-darwin',
 }
 PLATFORMS = PLATFORM_TO_TRIPLE.keys()
 
diff --git a/infra/recipes/gn.expected/ci_mac.json b/infra/recipes/gn.expected/ci_mac.json
index 03d5da6..fa8164d 100644
--- a/infra/recipes/gn.expected/ci_mac.json
+++ b/infra/recipes/gn.expected/ci_mac.json
@@ -155,7 +155,7 @@
       "xcrun",
       "--show-sdk-path"
     ],
-    "name": "debug.mac-amd64.xcrun",
+    "name": "debug.mac-amd64.xcrun sdk-path",
     "stdout": "/path/to/tmp/",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
@@ -165,6 +165,25 @@
   },
   {
     "cmd": [
+      "xcrun",
+      "--toolchain",
+      "clang",
+      "clang++",
+      "-xc++",
+      "-fsyntax-only",
+      "-Wp,-v",
+      "-"
+    ],
+    "name": "debug.mac-amd64.xcrun toolchain",
+    "stderr": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[toolchain]@[CACHE]/macos_sdk/XCode.app/include/c++/v1@@@",
+      "@@@STEP_LOG_END@raw_io.output[toolchain]@@@"
+    ]
+  },
+  {
+    "cmd": [
       "python",
       "-u",
       "[START_DIR]/gn/build/gen.py",
@@ -174,9 +193,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "debug.mac-amd64.generate",
     "~followup_annotations": [
@@ -193,9 +212,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "debug.mac-amd64.build",
     "~followup_annotations": [
@@ -227,7 +246,7 @@
       "xcrun",
       "--show-sdk-path"
     ],
-    "name": "release.mac-amd64.xcrun",
+    "name": "release.mac-amd64.xcrun sdk-path",
     "stdout": "/path/to/tmp/",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
@@ -237,6 +256,25 @@
   },
   {
     "cmd": [
+      "xcrun",
+      "--toolchain",
+      "clang",
+      "clang++",
+      "-xc++",
+      "-fsyntax-only",
+      "-Wp,-v",
+      "-"
+    ],
+    "name": "release.mac-amd64.xcrun toolchain",
+    "stderr": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[toolchain]@[CACHE]/macos_sdk/XCode.app/include/c++/v1@@@",
+      "@@@STEP_LOG_END@raw_io.output[toolchain]@@@"
+    ]
+  },
+  {
+    "cmd": [
       "python",
       "-u",
       "[START_DIR]/gn/build/gen.py",
@@ -247,9 +285,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "release.mac-amd64.generate",
     "~followup_annotations": [
@@ -266,9 +304,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "release.mac-amd64.build",
     "~followup_annotations": [
@@ -317,6 +355,117 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "release.mac-arm64",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "xcrun",
+      "--show-sdk-path"
+    ],
+    "name": "release.mac-arm64.xcrun sdk-path",
+    "stdout": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[sdk-path]@/some/xcode/path@@@",
+      "@@@STEP_LOG_END@raw_io.output[sdk-path]@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "xcrun",
+      "--toolchain",
+      "clang",
+      "clang++",
+      "-xc++",
+      "-fsyntax-only",
+      "-Wp,-v",
+      "-"
+    ],
+    "name": "release.mac-arm64.xcrun toolchain",
+    "stderr": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[toolchain]@[CACHE]/macos_sdk/XCode.app/include/c++/v1@@@",
+      "@@@STEP_LOG_END@raw_io.output[toolchain]@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "python",
+      "-u",
+      "[START_DIR]/gn/build/gen.py",
+      "--use-lto",
+      "--use-icf"
+    ],
+    "cwd": "[START_DIR]/gn",
+    "env": {
+      "AR": "[START_DIR]/cipd/bin/llvm-ar",
+      "CC": "[START_DIR]/cipd/bin/clang",
+      "CFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
+      "CXX": "[START_DIR]/cipd/bin/clang++",
+      "LDFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path"
+    },
+    "name": "release.mac-arm64.generate",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/ninja",
+      "-C",
+      "[START_DIR]/gn/out"
+    ],
+    "cwd": "[START_DIR]/gn",
+    "env": {
+      "AR": "[START_DIR]/cipd/bin/llvm-ar",
+      "CC": "[START_DIR]/cipd/bin/clang",
+      "CFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
+      "CXX": "[START_DIR]/cipd/bin/clang++",
+      "LDFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path"
+    },
+    "name": "release.mac-arm64.build",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [],
+    "name": "release.mac-arm64.upload",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "cipd",
+      "pkg-build",
+      "-pkg-def",
+      "{\"data\": [{\"file\": \"gn\"}, {\"version_file\": \".versions/gn.cipd_version\"}], \"install_mode\": \"copy\", \"package\": \"gn/gn/mac-arm64\", \"root\": \"[START_DIR]/gn/out\"}",
+      "-out",
+      "[CLEANUP]/gn.cipd",
+      "-hash-algo",
+      "sha256",
+      "-json-output",
+      "/path/to/tmp/json"
+    ],
+    "name": "release.mac-arm64.upload.build gn/gn/mac-arm64",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@3@@@",
+      "@@@STEP_LOG_LINE@json.output@{@@@",
+      "@@@STEP_LOG_LINE@json.output@  \"result\": {@@@",
+      "@@@STEP_LOG_LINE@json.output@    \"instance_id\": \"40-chars-fake-of-the-package-instance_id\", @@@",
+      "@@@STEP_LOG_LINE@json.output@    \"package\": \"gn/gn/mac-arm64\"@@@",
+      "@@@STEP_LOG_LINE@json.output@  }@@@",
+      "@@@STEP_LOG_LINE@json.output@}@@@",
+      "@@@STEP_LOG_END@json.output@@@"
+    ]
+  },
+  {
     "cmd": [
       "sudo",
       "xcode-select",
diff --git a/infra/recipes/gn.expected/cq_mac.json b/infra/recipes/gn.expected/cq_mac.json
index 0244003..c1d51f5 100644
--- a/infra/recipes/gn.expected/cq_mac.json
+++ b/infra/recipes/gn.expected/cq_mac.json
@@ -182,7 +182,7 @@
       "xcrun",
       "--show-sdk-path"
     ],
-    "name": "debug.mac-amd64.xcrun",
+    "name": "debug.mac-amd64.xcrun sdk-path",
     "stdout": "/path/to/tmp/",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
@@ -192,6 +192,25 @@
   },
   {
     "cmd": [
+      "xcrun",
+      "--toolchain",
+      "clang",
+      "clang++",
+      "-xc++",
+      "-fsyntax-only",
+      "-Wp,-v",
+      "-"
+    ],
+    "name": "debug.mac-amd64.xcrun toolchain",
+    "stderr": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[toolchain]@[CACHE]/macos_sdk/XCode.app/include/c++/v1@@@",
+      "@@@STEP_LOG_END@raw_io.output[toolchain]@@@"
+    ]
+  },
+  {
+    "cmd": [
       "python",
       "-u",
       "[START_DIR]/gn/build/gen.py",
@@ -201,9 +220,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "debug.mac-amd64.generate",
     "~followup_annotations": [
@@ -220,9 +239,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "debug.mac-amd64.build",
     "~followup_annotations": [
@@ -254,7 +273,7 @@
       "xcrun",
       "--show-sdk-path"
     ],
-    "name": "release.mac-amd64.xcrun",
+    "name": "release.mac-amd64.xcrun sdk-path",
     "stdout": "/path/to/tmp/",
     "~followup_annotations": [
       "@@@STEP_NEST_LEVEL@2@@@",
@@ -264,6 +283,25 @@
   },
   {
     "cmd": [
+      "xcrun",
+      "--toolchain",
+      "clang",
+      "clang++",
+      "-xc++",
+      "-fsyntax-only",
+      "-Wp,-v",
+      "-"
+    ],
+    "name": "release.mac-amd64.xcrun toolchain",
+    "stderr": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[toolchain]@[CACHE]/macos_sdk/XCode.app/include/c++/v1@@@",
+      "@@@STEP_LOG_END@raw_io.output[toolchain]@@@"
+    ]
+  },
+  {
+    "cmd": [
       "python",
       "-u",
       "[START_DIR]/gn/build/gen.py",
@@ -274,9 +312,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "release.mac-amd64.generate",
     "~followup_annotations": [
@@ -293,9 +331,9 @@
     "env": {
       "AR": "[START_DIR]/cipd/bin/llvm-ar",
       "CC": "[START_DIR]/cipd/bin/clang",
-      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path",
+      "CFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
       "CXX": "[START_DIR]/cipd/bin/clang++",
-      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path -nostdlib++ [START_DIR]/cipd/lib/libc++.a"
+      "LDFLAGS": "--target=x86_64-apple-darwin --sysroot=/some/xcode/path"
     },
     "name": "release.mac-amd64.build",
     "~followup_annotations": [
@@ -312,6 +350,85 @@
     ]
   },
   {
+    "cmd": [],
+    "name": "release.mac-arm64",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@1@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "xcrun",
+      "--show-sdk-path"
+    ],
+    "name": "release.mac-arm64.xcrun sdk-path",
+    "stdout": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[sdk-path]@/some/xcode/path@@@",
+      "@@@STEP_LOG_END@raw_io.output[sdk-path]@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "xcrun",
+      "--toolchain",
+      "clang",
+      "clang++",
+      "-xc++",
+      "-fsyntax-only",
+      "-Wp,-v",
+      "-"
+    ],
+    "name": "release.mac-arm64.xcrun toolchain",
+    "stderr": "/path/to/tmp/",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@",
+      "@@@STEP_LOG_LINE@raw_io.output[toolchain]@[CACHE]/macos_sdk/XCode.app/include/c++/v1@@@",
+      "@@@STEP_LOG_END@raw_io.output[toolchain]@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "python",
+      "-u",
+      "[START_DIR]/gn/build/gen.py",
+      "--use-lto",
+      "--use-icf"
+    ],
+    "cwd": "[START_DIR]/gn",
+    "env": {
+      "AR": "[START_DIR]/cipd/bin/llvm-ar",
+      "CC": "[START_DIR]/cipd/bin/clang",
+      "CFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
+      "CXX": "[START_DIR]/cipd/bin/clang++",
+      "LDFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path"
+    },
+    "name": "release.mac-arm64.generate",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
+    "cmd": [
+      "[START_DIR]/cipd/ninja",
+      "-C",
+      "[START_DIR]/gn/out"
+    ],
+    "cwd": "[START_DIR]/gn",
+    "env": {
+      "AR": "[START_DIR]/cipd/bin/llvm-ar",
+      "CC": "[START_DIR]/cipd/bin/clang",
+      "CFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path -nostdinc++ -cxx-isystem [CACHE]/macos_sdk/XCode.app/include/c++/v1",
+      "CXX": "[START_DIR]/cipd/bin/clang++",
+      "LDFLAGS": "--target=arm64-apple-darwin --sysroot=/some/xcode/path"
+    },
+    "name": "release.mac-arm64.build",
+    "~followup_annotations": [
+      "@@@STEP_NEST_LEVEL@2@@@"
+    ]
+  },
+  {
     "cmd": [
       "sudo",
       "xcode-select",
diff --git a/infra/recipes/gn.py b/infra/recipes/gn.py
index 68e721c..5c767b1 100644
--- a/infra/recipes/gn.py
+++ b/infra/recipes/gn.py
@@ -65,6 +65,14 @@
                        'sysroot')
     api.cipd.ensure(cipd_dir, pkgs)
 
+  def release_targets():
+    if api.platform.is_linux:
+      return [api.target('linux-amd64'), api.target('linux-arm64')]
+    elif api.platform.is_mac:
+      return [api.target('mac-amd64'), api.target('mac-arm64')]
+    else:
+      return [api.target.host]
+
   # The order is important since release build will get uploaded to CIPD.
   configs = [
       {
@@ -75,12 +83,29 @@
       {
           'name': 'release',
           'args': ['--use-lto', '--use-icf'],
-          'targets': [api.target('linux-amd64'),
-                      api.target('linux-arm64')]
-                     if api.platform.is_linux else [api.target.host],
+          'targets': release_targets(),
       },
   ]
 
+  def get_libcxx_include_path():
+    # Run the preprocessor with an empty input and print all include paths.
+    lines = api.step(
+        'xcrun toolchain', [
+            'xcrun', '--toolchain', 'clang', 'clang++', '-xc++',
+            '-fsyntax-only', '-Wp,-v', '-'
+        ],
+        stderr=api.raw_io.output(name='toolchain', add_output_log=True),
+        step_test_data=lambda: api.raw_io.test_api.stream_output(
+            str(api.macos_sdk.sdk_dir.join('include', 'c++', 'v1')),
+            stream='stderr')).stderr.splitlines()
+    # Iterate over all include paths and look for the SDK libc++ one.
+    sdk_dir = str(api.macos_sdk.sdk_dir)
+    for line in lines:
+      line = line.strip()
+      if line.startswith(sdk_dir) and 'include/c++/v1' in line:
+        return line
+    return None  # pragma: no cover
+
   with api.macos_sdk(), api.windows_sdk():
     for config in configs:
       with api.step.nest(config['name']):
@@ -99,18 +124,31 @@
             elif target.is_mac:
               triple = '--target=%s' % target.triple
               sysroot = '--sysroot=%s' % api.step(
-                  'xcrun', ['xcrun', '--show-sdk-path'],
+                  'xcrun sdk-path', ['xcrun', '--show-sdk-path'],
                   stdout=api.raw_io.output(
                       name='sdk-path', add_output_log=True),
                   step_test_data=lambda: api.raw_io.test_api.stream_output(
                       '/some/xcode/path')).stdout.strip()
               stdlib = cipd_dir.join('lib', 'libc++.a')
+              cxx_include = get_libcxx_include_path()
+              assert cxx_include, 'cannot find C++ header directory'
               env = {
-                  'CC': cipd_dir.join('bin', 'clang'),
-                  'CXX': cipd_dir.join('bin', 'clang++'),
-                  'AR': cipd_dir.join('bin', 'llvm-ar'),
-                  'CFLAGS': '%s %s' % (triple, sysroot),
-                  'LDFLAGS': '%s %s -nostdlib++ %s' % (triple, sysroot, stdlib),
+                  'CC':
+                      cipd_dir.join('bin', 'clang'),
+                  'CXX':
+                      cipd_dir.join('bin', 'clang++'),
+                  'AR':
+                      cipd_dir.join('bin', 'llvm-ar'),
+                  # TODO(phosek): temporarily use XCode C++ headers to make
+                  # sure these are compatible with the system libc++.
+                  'CFLAGS':
+                      '%s %s -nostdinc++ -cxx-isystem %s' %
+                      (triple, sysroot, cxx_include),
+                  # TODO(phosek): Use the system libc++ temporarily until we
+                  # have universal libc++.a for macOS that supports both x86_64
+                  # and arm64.
+                  'LDFLAGS':
+                      '%s %s' % (triple, sysroot),
               }
             else:
               env = {}