When re-running build/gen.py from ninja, preserve environment variables.

This ensures that when you run:
```
CXX=PATH_TO_CXX build/gen.py ...
ninja -C out
```

PATH_TO_CXX is actually used when building

Change-Id: Ie9b7fb149bd37181e7e1ba76024a9a806a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/26180
Reviewed-by: Takuto Ikuta <tikuta@google.com>
Commit-Queue: Matt Stark <msta@google.com>
diff --git a/build/gen.py b/build/gen.py
index e4e377e..d9c4b1f 100755
--- a/build/gen.py
+++ b/build/gen.py
@@ -292,6 +292,17 @@
       f.write(contents)
 
 
+USED_ENV_VARS = (
+    'AR',
+    'CFLAGS',
+    'CXX',
+    'CXXFLAGS',
+    'LD',
+    'LDFLAGS',
+    'LIBFLAGS',
+)
+
+
 def WriteGenericNinja(path, static_libraries, executables,
                       cxx, ar, ld, platform, host, options,
                       args_list, cflags=[], ldflags=[],
@@ -306,6 +317,20 @@
 
   rel_self = os.path.relpath(os.path.join(SCRIPT_DIR, 'gen.py'), build_dir)
 
+  sys_exec = f'"{sys.executable}"' if host.is_windows() else shlex.quote(sys.executable)
+  cmd = '%s %s%s' % (sys_exec, rel_self, args)
+  explicit_env = [k for k in USED_ENV_VARS if k in os.environ]
+  if explicit_env:
+    if host.is_windows():
+      set_cmds = ['set "%s=%s"' % (k, os.environ[k].replace('$', '$$')) for k in explicit_env]
+      cmd = 'cmd.exe /s /c "%s && %s"' % (' && '.join(set_cmds), cmd)
+    else:
+      env_prefix = ' '.join(
+          '%s=%s' % (k, shlex.quote(os.environ[k]).replace('$', '$$'))
+          for k in explicit_env
+      )
+      cmd = '%s %s' % (env_prefix, cmd)
+
   ninja_header_lines = [
     'cxx = ' + cxx,
     'ar = ' + ar,
@@ -314,7 +339,7 @@
     '  depth = 1',
     '',
     'rule regen',
-    '  command = %s %s%s' % (sys.executable, rel_self, args),
+    '  command = ' + cmd,
     '  description = Regenerating ninja files',
     '',
     'build build.ninja: regen',