Mirko Bonadei | ea4e399 | 2021-04-14 18:05:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 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. |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 5 | """This script is a wrapper around the GN binary that is pulled from Google |
| 6 | Cloud Storage when you sync Chrome. The binaries go into platform-specific |
| 7 | subdirectories in the source tree. |
| 8 | |
| 9 | This script makes there be one place for forwarding to the correct platform's |
| 10 | binary. It will also automatically try to find the gn binary when run inside |
| 11 | the chrome source tree, so users can just type "gn" on the command line |
| 12 | (normally depot_tools is on the path).""" |
| 13 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 14 | from __future__ import print_function |
| 15 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 16 | import gclient_paths |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 17 | import os |
| 18 | import subprocess |
| 19 | import sys |
| 20 | |
| 21 | |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 22 | def PruneVirtualEnv(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 23 | # Set by VirtualEnv, no need to keep it. |
| 24 | os.environ.pop('VIRTUAL_ENV', None) |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 25 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 26 | # Set by VPython, if scripts want it back they have to set it explicitly. |
| 27 | os.environ.pop('PYTHONNOUSERSITE', None) |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 28 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 29 | # Look for "activate_this.py" in this path, which is installed by |
| 30 | # VirtualEnv. This mechanism is used by vpython as well to sanitize |
| 31 | # VirtualEnvs from $PATH. |
| 32 | os.environ['PATH'] = os.pathsep.join([ |
| 33 | p for p in os.environ.get('PATH', '').split(os.pathsep) |
| 34 | if not os.path.isfile(os.path.join(p, 'activate_this.py')) |
| 35 | ]) |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 36 | |
| 37 | |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 38 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 39 | # Prune all evidence of VPython/VirtualEnv out of the environment. This |
| 40 | # means that we 'unwrap' vpython VirtualEnv path/env manipulation. |
| 41 | # Invocations of `python` from GN should never inherit the gn.py's own |
| 42 | # VirtualEnv. This also helps to ensure that generated ninja files do not |
| 43 | # reference python.exe from the VirtualEnv generated from depot_tools' own |
| 44 | # .vpython file (or lack thereof), but instead reference the default python |
| 45 | # from the PATH. |
| 46 | PruneVirtualEnv() |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 47 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 48 | # Try in primary solution location first, with the gn binary having been |
| 49 | # downloaded by cipd in the projects DEPS. |
| 50 | primary_solution_path = gclient_paths.GetPrimarySolutionPath() |
| 51 | if primary_solution_path: |
| 52 | gn_path = os.path.join(primary_solution_path, 'third_party', 'gn', |
| 53 | 'gn' + gclient_paths.GetExeSuffix()) |
| 54 | if os.path.exists(gn_path): |
| 55 | return subprocess.call([gn_path] + args[1:]) |
Scott Graham | a991ac6 | 2018-06-26 23:19:51 +0000 | [diff] [blame] | 56 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 57 | # Otherwise try the old .sha1 and download_from_google_storage locations |
| 58 | # inside of buildtools. |
| 59 | bin_path = gclient_paths.GetBuildtoolsPlatformBinaryPath() |
| 60 | if not bin_path: |
| 61 | print( |
| 62 | 'gn.py: Could not find checkout in any parent of the current ' |
| 63 | 'path.\nThis must be run inside a checkout.', |
| 64 | file=sys.stderr) |
| 65 | return 1 |
| 66 | gn_path = os.path.join(bin_path, 'gn' + gclient_paths.GetExeSuffix()) |
| 67 | if not os.path.exists(gn_path): |
| 68 | print('gn.py: Could not find gn executable at: %s' % gn_path, |
| 69 | file=sys.stderr) |
| 70 | return 2 |
| 71 | return subprocess.call([gn_path] + args[1:]) |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 72 | |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 73 | |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 74 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 75 | try: |
| 76 | sys.exit(main(sys.argv)) |
| 77 | except KeyboardInterrupt: |
| 78 | sys.stderr.write('interrupted\n') |
| 79 | sys.exit(1) |