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 | |
Nico Weber | 09e0b38 | 2019-03-11 16:54:07 +0000 | [diff] [blame] | 14 | import gclient_paths |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | |
| 19 | |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 20 | def PruneVirtualEnv(): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 21 | # Set by VirtualEnv, no need to keep it. |
| 22 | os.environ.pop('VIRTUAL_ENV', None) |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 23 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 24 | # Set by VPython, if scripts want it back they have to set it explicitly. |
| 25 | os.environ.pop('PYTHONNOUSERSITE', None) |
Sergiy Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 26 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 27 | # 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 Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 34 | |
| 35 | |
Junji Watanabe | c550588 | 2023-09-25 01:32:45 +0000 | [diff] [blame] | 36 | def 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.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 52 | def main(args): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 53 | # 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 Byelozyorov | 0148955 | 2018-06-29 18:13:15 +0000 | [diff] [blame] | 61 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 62 | # 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 Graham | a991ac6 | 2018-06-26 23:19:51 +0000 | [diff] [blame] | 70 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 71 | # 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 Watanabe | c550588 | 2023-09-25 01:32:45 +0000 | [diff] [blame] | 75 | gn_path = findGnInPath() |
| 76 | if gn_path: |
| 77 | return subprocess.call([gn_path] + args[1:]) |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 78 | 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.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 89 | |
nick@chromium.org | 3ac1c4e | 2014-01-16 02:44:42 +0000 | [diff] [blame] | 90 | |
brettw@chromium.org | 67bb861 | 2013-11-08 20:51:40 +0000 | [diff] [blame] | 91 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 92 | try: |
| 93 | sys.exit(main(sys.argv)) |
| 94 | except KeyboardInterrupt: |
| 95 | sys.stderr.write('interrupted\n') |
| 96 | sys.exit(1) |