blob: 0cf2538d142c1a0d29b38903ceae3d66c7bdcd73 [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.
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
Raul Tambre80ee78e2019-05-06 22:41:05 +000015from __future__ import print_function
16
Nico Weber09e0b382019-03-11 16:54:07 +000017import gclient_paths
brettw@chromium.org67bb8612013-11-08 20:51:40 +000018import os
19import subprocess
20import sys
21
22
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000023def PruneVirtualEnv():
24 # Set by VirtualEnv, no need to keep it.
25 os.environ.pop('VIRTUAL_ENV', None)
26
27 # Set by VPython, if scripts want it back they have to set it explicitly.
28 os.environ.pop('PYTHONNOUSERSITE', None)
29
30 # Look for "activate_this.py" in this path, which is installed by VirtualEnv.
31 # This mechanism is used by vpython as well to sanitize VirtualEnvs from
32 # $PATH.
33 os.environ['PATH'] = os.pathsep.join([
34 p for p in os.environ.get('PATH', '').split(os.pathsep)
35 if not os.path.isfile(os.path.join(p, 'activate_this.py'))
36 ])
37
38
brettw@chromium.org67bb8612013-11-08 20:51:40 +000039def main(args):
Sergiy Byelozyorov01489552018-06-29 18:13:15 +000040 # Prune all evidence of VPython/VirtualEnv out of the environment. This means
41 # that we 'unwrap' vpython VirtualEnv path/env manipulation. Invocations of
42 # `python` from GN should never inherit the gn.py's own VirtualEnv. This also
43 # helps to ensure that generated ninja files do not reference python.exe from
44 # the VirtualEnv generated from depot_tools' own .vpython file (or lack
45 # thereof), but instead reference the default python from the PATH.
46 PruneVirtualEnv()
47
Scott Grahama991ac62018-06-26 23:19:51 +000048 # Try in primary solution location first, with the gn binary having been
49 # downloaded by cipd in the projects DEPS.
Nico Weber09e0b382019-03-11 16:54:07 +000050 primary_solution_path = gclient_paths.GetPrimarySolutionPath()
Lowell Manners4d2d5b42018-11-07 09:46:12 +000051 if primary_solution_path:
52 gn_path = os.path.join(primary_solution_path, 'third_party',
Nico Weber09e0b382019-03-11 16:54:07 +000053 'gn', 'gn' + gclient_paths.GetExeSuffix())
Lowell Manners4d2d5b42018-11-07 09:46:12 +000054 if os.path.exists(gn_path):
55 return subprocess.call([gn_path] + args[1:])
Scott Grahama991ac62018-06-26 23:19:51 +000056
57 # Otherwise try the old .sha1 and download_from_google_storage locations
58 # inside of buildtools.
Nico Weber09e0b382019-03-11 16:54:07 +000059 bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath()
brettw@chromium.orgcc968fe2014-06-23 17:30:32 +000060 if not bin_path:
Raul Tambre80ee78e2019-05-06 22:41:05 +000061 print('gn.py: Could not find checkout in any parent of the current path.\n'
62 'This must be run inside a checkout.', file=sys.stderr)
kjellander@chromium.orgf7facfa2014-09-05 12:40:28 +000063 return 1
Nico Weber09e0b382019-03-11 16:54:07 +000064 gn_path = os.path.join(bin_path, 'gn' + gclient_paths.GetExeSuffix())
kjellander@chromium.orgf7facfa2014-09-05 12:40:28 +000065 if not os.path.exists(gn_path):
Raul Tambre80ee78e2019-05-06 22:41:05 +000066 print(
67 'gn.py: Could not find gn executable at: %s' % gn_path, file=sys.stderr)
kjellander@chromium.orgf7facfa2014-09-05 12:40:28 +000068 return 2
Aravind Vasudevan22bf6052022-01-24 21:11:19 +000069 return subprocess.call([gn_path] + args[1:])
brettw@chromium.org67bb8612013-11-08 20:51:40 +000070
nick@chromium.org3ac1c4e2014-01-16 02:44:42 +000071
brettw@chromium.org67bb8612013-11-08 20:51:40 +000072if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000073 try:
74 sys.exit(main(sys.argv))
75 except KeyboardInterrupt:
76 sys.stderr.write('interrupted\n')
77 sys.exit(1)