blob: 296042ed4f6de884e081d3a8805ebe1dd94af16a [file] [log] [blame]
Mirko Bonadeiea4e3992021-04-14 18:05:29 +00001#!/usr/bin/env python3
brettw@chromium.org67bb8612013-11-08 20:51:40 +00002# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
brettw@chromium.org67bb8612013-11-08 20:51:40 +00005"""This script is a wrapper around the GN binary that is pulled from Google
6Cloud Storage when you sync Chrome. The binaries go into platform-specific
7subdirectories in the source tree.
8
9This script makes there be one place for forwarding to the correct platform's
10binary. It will also automatically try to find the gn binary when run inside
11the chrome source tree, so users can just type "gn" on the command line
12(normally depot_tools is on the path)."""
13
Nico Weber09e0b382019-03-11 16:54:07 +000014import gclient_paths
brettw@chromium.org67bb8612013-11-08 20:51:40 +000015import os
16import subprocess
17import sys
18
19
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000020def PruneVirtualEnv():
Mike Frysinger124bb8e2023-09-06 05:48:55 +000021 # Set by VirtualEnv, no need to keep it.
22 os.environ.pop('VIRTUAL_ENV', None)
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000023
Mike Frysinger124bb8e2023-09-06 05:48:55 +000024 # Set by VPython, if scripts want it back they have to set it explicitly.
25 os.environ.pop('PYTHONNOUSERSITE', None)
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000026
Mike Frysinger124bb8e2023-09-06 05:48:55 +000027 # Look for "activate_this.py" in this path, which is installed by
28 # VirtualEnv. This mechanism is used by vpython as well to sanitize
29 # VirtualEnvs from $PATH.
30 os.environ['PATH'] = os.pathsep.join([
31 p for p in os.environ.get('PATH', '').split(os.pathsep)
32 if not os.path.isfile(os.path.join(p, 'activate_this.py'))
33 ])
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000034
35
Junji Watanabec5505882023-09-25 01:32:45 +000036def findGnInPath():
37 env_path = os.getenv('PATH')
38 if not env_path:
39 return
40 exe = 'gn'
41 if sys.platform in ('win32', 'cygwin'):
42 exe += '.exe'
43 for bin_dir in env_path.split(os.pathsep):
44 if bin_dir.rstrip(os.sep).endswith('depot_tools'):
45 # skip depot_tools to avoid calling gn.py infinitely.
46 continue
47 gn_path = os.path.join(bin_dir, exe)
48 if os.path.isfile(gn_path):
49 return gn_path
50
51
brettw@chromium.org67bb8612013-11-08 20:51:40 +000052def main(args):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000053 # Prune all evidence of VPython/VirtualEnv out of the environment. This
54 # means that we 'unwrap' vpython VirtualEnv path/env manipulation.
55 # Invocations of `python` from GN should never inherit the gn.py's own
56 # VirtualEnv. This also helps to ensure that generated ninja files do not
57 # reference python.exe from the VirtualEnv generated from depot_tools' own
58 # .vpython file (or lack thereof), but instead reference the default python
59 # from the PATH.
60 PruneVirtualEnv()
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000061
Mike Frysinger124bb8e2023-09-06 05:48:55 +000062 # Try in primary solution location first, with the gn binary having been
63 # downloaded by cipd in the projects DEPS.
64 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
65 if primary_solution_path:
66 gn_path = os.path.join(primary_solution_path, 'third_party', 'gn',
67 'gn' + gclient_paths.GetExeSuffix())
68 if os.path.exists(gn_path):
69 return subprocess.call([gn_path] + args[1:])
Scott Grahama991ac62018-06-26 23:19:51 +000070
Mike Frysinger124bb8e2023-09-06 05:48:55 +000071 # Otherwise try the old .sha1 and download_from_google_storage locations
72 # inside of buildtools.
73 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
74 if not bin_path:
Junji Watanabec5505882023-09-25 01:32:45 +000075 gn_path = findGnInPath()
76 if gn_path:
77 return subprocess.call([gn_path] + args[1:])
Mike Frysinger124bb8e2023-09-06 05:48:55 +000078 print(
79 'gn.py: Could not find checkout in any parent of the current '
80 'path.\nThis must be run inside a checkout.',
81 file=sys.stderr)
82 return 1
83 gn_path = os.path.join(bin_path, 'gn' + gclient_paths.GetExeSuffix())
84 if not os.path.exists(gn_path):
85 print('gn.py: Could not find gn executable at: %s' % gn_path,
86 file=sys.stderr)
87 return 2
88 return subprocess.call([gn_path] + args[1:])
brettw@chromium.org67bb8612013-11-08 20:51:40 +000089
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000090
brettw@chromium.org67bb8612013-11-08 20:51:40 +000091if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000092 try:
93 sys.exit(main(sys.argv))
94 except KeyboardInterrupt:
95 sys.stderr.write('interrupted\n')
96 sys.exit(1)