blob: a7dbf6caf823a622e49e406788315dac964f697e [file] [log] [blame]
brettw@chromium.org67bb8612013-11-08 20:51:40 +00001#!/usr/bin/env python
2# 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.
5
6"""This script is a wrapper around the GN binary that is pulled from Google
7Cloud Storage when you sync Chrome. The binaries go into platform-specific
8subdirectories in the source tree.
9
10This script makes there be one place for forwarding to the correct platform's
11binary. It will also automatically try to find the gn binary when run inside
12the chrome source tree, so users can just type "gn" on the command line
13(normally depot_tools is on the path)."""
14
Nico Weber09e0b382019-03-11 16:54:07 +000015import gclient_paths
brettw@chromium.org67bb8612013-11-08 20:51:40 +000016import os
17import subprocess
18import sys
19
20
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000021def PruneVirtualEnv():
22 # Set by VirtualEnv, no need to keep it.
23 os.environ.pop('VIRTUAL_ENV', None)
24
25 # Set by VPython, if scripts want it back they have to set it explicitly.
26 os.environ.pop('PYTHONNOUSERSITE', None)
27
28 # Look for "activate_this.py" in this path, which is installed by VirtualEnv.
29 # This mechanism is used by vpython as well to sanitize VirtualEnvs from
30 # $PATH.
31 os.environ['PATH'] = os.pathsep.join([
32 p for p in os.environ.get('PATH', '').split(os.pathsep)
33 if not os.path.isfile(os.path.join(p, 'activate_this.py'))
34 ])
35
36
brettw@chromium.org67bb8612013-11-08 20:51:40 +000037def main(args):
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000038 # Prune all evidence of VPython/VirtualEnv out of the environment. This means
39 # that we 'unwrap' vpython VirtualEnv path/env manipulation. Invocations of
40 # `python` from GN should never inherit the gn.py's own VirtualEnv. This also
41 # helps to ensure that generated ninja files do not reference python.exe from
42 # the VirtualEnv generated from depot_tools' own .vpython file (or lack
43 # thereof), but instead reference the default python from the PATH.
44 PruneVirtualEnv()
45
Scott Grahama991ac62018-06-26 23:19:51 +000046 # Try in primary solution location first, with the gn binary having been
47 # downloaded by cipd in the projects DEPS.
Nico Weber09e0b382019-03-11 16:54:07 +000048 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
Lowell Manners4d2d5b42018-11-07 09:46:12 +000049 if primary_solution_path:
50 gn_path = os.path.join(primary_solution_path, 'third_party',
Nico Weber09e0b382019-03-11 16:54:07 +000051 'gn', 'gn' + gclient_paths.GetExeSuffix())
Lowell Manners4d2d5b42018-11-07 09:46:12 +000052 if os.path.exists(gn_path):
53 return subprocess.call([gn_path] + args[1:])
Scott Grahama991ac62018-06-26 23:19:51 +000054
55 # Otherwise try the old .sha1 and download_from_google_storage locations
56 # inside of buildtools.
Nico Weber09e0b382019-03-11 16:54:07 +000057 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
brettw@chromium.orgcc968fe2014-06-23 17:30:32 +000058 if not bin_path:
59 print >> sys.stderr, ('gn.py: Could not find checkout in any parent of '
60 'the current path.\nThis must be run inside a '
61 'checkout.')
kjellander@chromium.orgf7facfa2014-09-05 12:40:28 +000062 return 1
Nico Weber09e0b382019-03-11 16:54:07 +000063 gn_path = os.path.join(bin_path, 'gn' + gclient_paths.GetExeSuffix())
kjellander@chromium.orgf7facfa2014-09-05 12:40:28 +000064 if not os.path.exists(gn_path):
65 print >> sys.stderr, 'gn.py: Could not find gn executable at: %s' % gn_path
66 return 2
67 else:
scottmg@chromium.orgd05ab352014-12-05 17:24:26 +000068 return subprocess.call([gn_path] + args[1:])
brettw@chromium.org67bb8612013-11-08 20:51:40 +000069
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000070
brettw@chromium.org67bb8612013-11-08 20:51:40 +000071if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000072 try:
73 sys.exit(main(sys.argv))
74 except KeyboardInterrupt:
75 sys.stderr.write('interrupted\n')
76 sys.exit(1)