1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 04:02:41 +01:00
llvm-mirror/utils/gn/get.py
Nico Weber 6e56add3ce gn build: Unbreak get.py and gn.py on Windows
`os.uname()` doesn't exist on Windows, so use `platform.machine()` which
returns `os.uname()[4]` on non-Win and (on 64-bit systems) "AMD64" on Windows.
Also use `sys.platform` instead of `platform` to check for Windows-ness for the
file extension in gn.py (get.py got this right).

Differential Revision: https://reviews.llvm.org/D59115

llvm-svn: 355693
2019-03-08 12:45:50 +00:00

65 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python
"""Downloads a prebuilt gn binary to a place where gn.py can find it."""
from __future__ import print_function
import os
import urllib2
import sys
import tempfile
import zipfile
def download_url(url, output_file):
"""Download url into output_file."""
print('downloading %s ...' % url, end='')
sys.stdout.flush()
output_file.write(urllib2.urlopen(url).read())
print(' done')
def download_and_unpack(url, output_dir, gn):
"""Download an archive from url and extract gn from it into output_dir."""
with tempfile.TemporaryFile() as f:
download_url(url, f)
f.seek(0)
zipfile.ZipFile(f).extract(gn, path=output_dir)
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():
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 main():
platform = get_platform()
if not platform:
print('no prebuilt binary for', sys.platform)
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 '')
download_and_unpack(url % platform, dirname, gn)
set_executable_bit(os.path.join(dirname, gn))
if __name__ == '__main__':
sys.exit(main())