llvm.org GIT mirror llvm / ccc713e
gn build: Add get.py script to download prebuilt gn, make gn.py run downloaded gn if gn is not on PATH Prebuilts are available for x86_64 Linux, macOS, Windows. The script always pulls the latest GN version. Differential Revision: https://reviews.llvm.org/D57256 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352420 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Weber 8 months ago
4 changed file(s) with 99 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
4141
4242 GN only works in the monorepo layout.
4343
44 #. Obtain a `gn binary `_.
44 #. Obtain a gn binary. If gn is not already on your PATH, run
45 `llvm/utils/gn/get.py` to download a prebuilt gn binary if you're on a 64-bit
46 X86 system running Linux, macOS, or Windows, or `build gn yourself
47 `_ if you're on a different
48 platform or don't want to trust prebuilt binaries.
4549
4650 #. In the root of the monorepo, run `llvm/utils/gn/gn.py gen out/gn`.
4751 `out/gn` is the build directory, it can have any name, and you can have as
0 #!/usr/bin/env python
1 """Downloads a prebuilt gn binary to a place where gn.py can find it."""
2
3 from __future__ import print_function
4
5 import os
6 import urllib2
7 import sys
8 import tempfile
9 import zipfile
10
11
12 def download_url(url, output_file):
13 """Download url into output_file."""
14 print('downloading %s ...' % url, end='')
15 sys.stdout.flush()
16 output_file.write(urllib2.urlopen(url).read())
17 print(' done')
18
19
20 def download_and_unpack(url, output_dir, gn):
21 """Download an archive from url and extract gn from it into output_dir."""
22 with tempfile.TemporaryFile() as f:
23 download_url(url, f)
24 f.seek(0)
25 zipfile.ZipFile(f).extract(gn, path=output_dir)
26
27
28 def set_executable_bit(path):
29 mode = os.stat(path).st_mode
30 mode |= (mode & 0o444) >> 2 # Copy R bits to X.
31 os.chmod(path, mode) # No-op on Windows.
32
33
34 def get_platform():
35 if os.uname()[4] != 'x86_64':
36 return None
37 if sys.platform.startswith('linux'):
38 return 'linux-amd64'
39 if sys.platform == 'darwin':
40 return 'mac-amd64'
41 if sys.platform == 'win32':
42 return 'windows-amd64'
43
44
45 def main():
46 platform = get_platform()
47 if not platform:
48 print('no prebuilt binary for', sys.platform)
49 return 1
50
51 dirname = os.path.join(os.path.dirname(__file__), 'bin', platform)
52 if not os.path.exists(dirname):
53 os.makedirs(dirname)
54
55 url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/%s/+/latest'
56 gn = 'gn' + ('.exe' if sys.platform == 'win32' else '')
57 download_and_unpack(url % platform, dirname, gn)
58 set_executable_bit(os.path.join(dirname, gn))
59
60
61 if __name__ == '__main__':
62 sys.exit(main())
1414 ROOT_DIR = os.path.join(THIS_DIR, '..', '..', '..')
1515
1616
17 def get_platform():
18 if os.uname()[4] != 'x86_64':
19 return None
20 if sys.platform.startswith('linux'):
21 return 'linux-amd64'
22 if sys.platform == 'darwin':
23 return 'mac-amd64'
24 if sys.platform == 'win32':
25 return 'windows-amd64'
26
27
28 def print_no_gn(mention_get):
29 print('gn binary not found in PATH')
30 if mention_get:
31 print('run llvm/utils/gn/get.py to download a binary and try again, or')
32 print('follow https://gn.googlesource.com/gn/#getting-started')
33 return 1
34
35
1736 def main():
18 # Find real gn executable. For now, just assume it's on PATH.
19 # FIXME: Probably need to append '.exe' on Windows.
37 # Find real gn executable.
2038 gn = 'gn'
39 if subprocess.call([gn, '--version'], stdout=open(os.devnull, 'w'),
40 stderr=subprocess.STDOUT) != 0:
41 # Not on path. See if get.py downloaded a prebuilt binary and run that
42 # if it's there, or suggest to run get.py if it isn't.
43 platform = get_platform()
44 if not platform:
45 return print_no_gn(mention_get=False)
46 gn = os.path.join(os.path.dirname(__file__), 'bin', platform, 'gn')
47 if not os.path.exists(gn + ('.exe' if platform == 'windows' else '')):
48 return print_no_gn(mention_get=True)
2149
2250 # Compute --dotfile= and --root= args to add.
2351 extra_args = []