2019-01-28 20:54:41 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""Downloads a prebuilt gn binary to a place where gn.py can find it."""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2019-03-25 12:32:27 +01:00
|
|
|
import io
|
2019-01-28 20:54:41 +01:00
|
|
|
import os
|
2020-01-22 23:31:12 +01:00
|
|
|
try:
|
|
|
|
# In Python 3, we need the module urllib.reqest. In Python 2, this
|
|
|
|
# functionality was in the urllib2 module.
|
|
|
|
from urllib import request as urllib_request
|
|
|
|
except ImportError:
|
|
|
|
import urllib2 as urllib_request
|
2019-01-28 20:54:41 +01:00
|
|
|
import sys
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
|
2019-03-25 12:32:27 +01:00
|
|
|
def download_and_unpack(url, output_dir, gn):
|
|
|
|
"""Download an archive from url and extract gn from it into output_dir."""
|
2019-01-28 20:54:41 +01:00
|
|
|
print('downloading %s ...' % url, end='')
|
|
|
|
sys.stdout.flush()
|
2020-01-22 23:31:12 +01:00
|
|
|
data = urllib_request.urlopen(url).read()
|
2019-01-28 20:54:41 +01:00
|
|
|
print(' done')
|
2019-03-25 12:32:27 +01:00
|
|
|
zipfile.ZipFile(io.BytesIO(data)).extract(gn, path=output_dir)
|
2019-01-28 20:54:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
def set_executable_bit(path):
|
|
|
|
mode = os.stat(path).st_mode
|
|
|
|
mode |= (mode & 0o444) >> 2 # Copy R bits to X.
|
|
|
|
os.chmod(path, mode) # No-op on Windows.
|
|
|
|
|
|
|
|
|
|
|
|
def get_platform():
|
2019-03-08 13:45:50 +01:00
|
|
|
import platform
|
2020-07-02 20:22:31 +02:00
|
|
|
if sys.platform == 'darwin':
|
|
|
|
return 'mac-amd64' if platform.machine() != 'arm64' else 'mac-arm64'
|
2019-03-08 13:45:50 +01:00
|
|
|
if platform.machine() not in ('AMD64', 'x86_64'):
|
2019-01-28 20:54:41 +01:00
|
|
|
return None
|
|
|
|
if sys.platform.startswith('linux'):
|
|
|
|
return 'linux-amd64'
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
return 'windows-amd64'
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
platform = get_platform()
|
|
|
|
if not platform:
|
|
|
|
print('no prebuilt binary for', sys.platform)
|
2021-01-27 01:19:19 +01:00
|
|
|
print('build it yourself with:')
|
|
|
|
print(' rm -rf /tmp/gn &&')
|
|
|
|
print(' pushd /tmp && git clone https://gn.googlesource.com/gn &&')
|
|
|
|
print(' cd gn && build/gen.py && ninja -C out gn && popd &&')
|
|
|
|
print(' cp /tmp/gn/out/gn somewhere/on/PATH')
|
2019-01-28 20:54:41 +01:00
|
|
|
return 1
|
|
|
|
dirname = os.path.join(os.path.dirname(__file__), 'bin', platform)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
|
|
|
|
url = 'https://chrome-infra-packages.appspot.com/dl/gn/gn/%s/+/latest'
|
|
|
|
gn = 'gn' + ('.exe' if sys.platform == 'win32' else '')
|
2020-11-25 18:08:10 +01:00
|
|
|
if platform == 'mac-arm64': # For https://openradar.appspot.com/FB8914243
|
|
|
|
try: os.remove(os.path.join(dirname, gn))
|
|
|
|
except OSError: pass
|
2019-01-28 20:54:41 +01:00
|
|
|
download_and_unpack(url % platform, dirname, gn)
|
|
|
|
set_executable_bit(os.path.join(dirname, gn))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|