Fix update_reference.sh to work with a custom output directory.

Change-Id: I6af731713efdc7e3da3fda4aa23062db6a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/23923
Reviewed-by: Takuto Ikuta <tikuta@google.com>
diff --git a/infra/recipes/gn.expected/ci_linux.json b/infra/recipes/gn.expected/ci_linux.json
index d4c741e..3a98040 100644
--- a/infra/recipes/gn.expected/ci_linux.json
+++ b/infra/recipes/gn.expected/ci_linux.json
@@ -742,6 +742,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
@@ -945,6 +946,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
diff --git a/infra/recipes/gn.expected/cipd_exists.json b/infra/recipes/gn.expected/cipd_exists.json
index 47dceaf..9a363f0 100644
--- a/infra/recipes/gn.expected/cipd_exists.json
+++ b/infra/recipes/gn.expected/cipd_exists.json
@@ -742,6 +742,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
@@ -945,6 +946,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
diff --git a/infra/recipes/gn.expected/cipd_register.json b/infra/recipes/gn.expected/cipd_register.json
index 73674e8..76cdb0f 100644
--- a/infra/recipes/gn.expected/cipd_register.json
+++ b/infra/recipes/gn.expected/cipd_register.json
@@ -742,6 +742,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
@@ -945,6 +946,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
diff --git a/infra/recipes/gn.expected/cq_linux.json b/infra/recipes/gn.expected/cq_linux.json
index 1dc70c7..91282cc 100644
--- a/infra/recipes/gn.expected/cq_linux.json
+++ b/infra/recipes/gn.expected/cq_linux.json
@@ -793,6 +793,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
@@ -996,6 +997,7 @@
       "CFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot",
       "CXX": "[START_DIR]/cipd/bin/clang++",
       "LDFLAGS": "--target=x86_64-linux-gnu --sysroot=[START_DIR]/cipd/sysroot -static-libstdc++",
+      "NINJA_OUT_DIR": "[START_DIR]/gn/out/Default",
       "NOBUILD": "1"
     },
     "luci_context": {
diff --git a/infra/recipes/gn.py b/infra/recipes/gn.py
index c8ea376..28039e9 100644
--- a/infra/recipes/gn.py
+++ b/infra/recipes/gn.py
@@ -282,7 +282,7 @@
                   api.step('Check tools/run_formatter.sh',
                            [src_dir.join('tools', 'run_formatter.sh'), '--diff'])
                 # We've already built gn, so tell update_reference not to rebuild it.
-                with api.context(env={'NOBUILD': '1'}):
+                with api.context(env={'NOBUILD': '1', 'NINJA_OUT_DIR': out_dir}):
                   api.step('Check tools/update_reference.sh',
                           [src_dir.join('tools', 'update_reference.sh'), '--diff'])
 
diff --git a/tools/update_reference.sh b/tools/update_reference.sh
index ecf1899..2939358 100755
--- a/tools/update_reference.sh
+++ b/tools/update_reference.sh
@@ -1,5 +1,7 @@
 #!/bin/bash -eu
 
+NINJA_OUT_DIR="${NINJA_OUT_DIR:-out}"
+
 check=false
 if [[ "$#" -ge 1 && "$1" == "--diff" ]]; then
     check=true
@@ -12,10 +14,10 @@
 # CI, where we've just built it, and ninja is not present in the path.
 if [[ -z "${NOBUILD:-}" ]]; then
     echo Building gn...
-    ninja -C out gn
+    ninja -C "${NINJA_OUT_DIR}" gn
 fi
 echo Generating new docs/reference.md...
-content=$(out/gn help --markdown all)
+content=$("${NINJA_OUT_DIR}/gn" help --markdown all)
 
 if "${check}"; then
     diff -u docs/reference.md <(echo "$content")