1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 19:23:23 +01:00
llvm-mirror/utils/gn/gn.py
Nico Weber 7f776ffee2 gn build: Unbreak finding a working gn on $PATH on Unix after r355645
From the Python subprocess docs:

   If shell is True, it is recommended to pass args as a string rather than as
   a sequence.

   [...]

   If args is a sequence, the first item specifies the command string, and any
   additional items will be treated as additional arguments to the shell itself.

Prior to this change, the `--version` would be passed to the shell, not to
a potential gn binary on $PATH, and running `gn` without any arguments makes
it exit with an exit code != 0, so the script would think that there wasn't
a working gn binary on $PATH.

Fix this by following the documentation's recommendation of using a string
now that we pass shell=True. I tested this on macOS and Windows, each with
the three cases of

- no gn on PATH (should run gn downloaded by get.py if present,
  else suggest running get.py)
- broken gn wrapper on PATH (should behave like the previous item)
- working gn on PATH (should use gn on PATH)

llvm-svn: 355694
2019-03-08 13:01:58 +00:00

69 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python
"""Calls `gn` with the right --dotfile= and --root= arguments for LLVM."""
# GN normally expects a file called '.gn' at the root of the repository.
# Since LLVM's GN build isn't supported, putting that file at the root
# is deemed inappropriate, which requires passing --dotfile= and -root= to GN.
# Since that gets old fast, this script automatically passes these arguments.
import os
import subprocess
import sys
THIS_DIR = os.path.dirname(__file__)
ROOT_DIR = os.path.join(THIS_DIR, '..', '..', '..')
def get_platform():
import platform
if platform.machine() not in ('AMD64', 'x86_64'):
return None
if sys.platform.startswith('linux'):
return 'linux-amd64'
if sys.platform == 'darwin':
return 'mac-amd64'
if sys.platform == 'win32':
return 'windows-amd64'
def print_no_gn(mention_get):
print('gn binary not found in PATH')
if mention_get:
print('run llvm/utils/gn/get.py to download a binary and try again, or')
print('follow https://gn.googlesource.com/gn/#getting-started')
return 1
def main():
# Find real gn executable.
gn = 'gn'
if subprocess.call('gn --version', stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT,
shell=True) != 0:
# Not on path. See if get.py downloaded a prebuilt binary and run that
# if it's there, or suggest to run get.py if it isn't.
platform = get_platform()
if not platform:
return print_no_gn(mention_get=False)
gn = os.path.join(os.path.dirname(__file__), 'bin', platform, 'gn')
if not os.path.exists(gn + ('.exe' if sys.platform == 'win32' else '')):
return print_no_gn(mention_get=True)
# Compute --dotfile= and --root= args to add.
extra_args = []
gn_main_arg = next((x for x in sys.argv[1:] if not x.startswith('-')), None)
if gn_main_arg != 'help': # `gn help` gets confused by the switches.
cwd = os.getcwd()
dotfile = os.path.relpath(os.path.join(THIS_DIR, '.gn'), cwd)
root = os.path.relpath(ROOT_DIR, cwd)
extra_args = [ '--dotfile=' + dotfile, '--root=' + root ]
# Run GN command with --dotfile= and --root= added.
cmd = [gn] + extra_args + sys.argv[1:]
sys.exit(subprocess.call(cmd))
if __name__ == '__main__':
main()