blob: 5fc5ef83ddbfab718d0cb1ad009c3ac2eb0aa764 [file] [log] [blame]
lowRISC Contributors802543a2019-08-31 12:12:56 +01001#!/usr/bin/env python3
2# Copyright lowRISC contributors.
3# Licensed under the Apache License, Version 2.0, see LICENSE for details.
4# SPDX-License-Identifier: Apache-2.0
5
6import argparse
7import json
8import os
9import subprocess
10import sys
11import tempfile
12from urllib.request import urlopen, urlretrieve
13
14TOOLCHAIN_VERSION = 'latest'
15RELEASES_URL_BASE = 'https://api.github.com/repos/lowRISC/lowrisc-toolchains/releases'
16ASSET_PREFIX = "lowrisc-toolchain-gcc-rv32imc-"
17TARGET_DIR = '/tools/riscv'
18
19
20def get_download_url(toolchain_version):
21 if toolchain_version == 'latest':
22 releases_url = '%s/%s' % (RELEASES_URL_BASE, toolchain_version)
23 else:
24 releases_url = '%s/tags/%s' % (RELEASES_URL_BASE, toolchain_version)
25 with urlopen(releases_url) as f:
26 info = json.loads(f.read().decode('utf-8'))
27 return [
28 a["browser_download_url"] for a in info["assets"]
29 if a["name"].startswith(ASSET_PREFIX)
30 ][0]
31
32
33def download(url):
34 print("Downloading toolchain from %s" % (url, ))
35 tmpfile = tempfile.mktemp()
36 urlretrieve(url, tmpfile)
37 return tmpfile
38
39
40def install(archive_file, target_dir):
41 os.makedirs(target_dir)
42
43 cmd = [
44 'tar', '-x', '-f', archive_file, '--strip-components=1', '-C',
45 target_dir
46 ]
47 subprocess.run(cmd, check=True)
48
49
50def main():
51 parser = argparse.ArgumentParser()
52 parser.add_argument(
53 '--target-dir',
54 '-t',
55 required=False,
56 default=TARGET_DIR,
57 help="Target directory (must not exist) (default: %(default)s)")
58 parser.add_argument(
59 '--release-version',
60 '-r',
61 required=False,
62 default=TOOLCHAIN_VERSION,
63 help="Toolchain version (default: %(default)s)")
64 args = parser.parse_args()
65
66 target_dir = args.target_dir
67 toolchain_version = args.release_version
68
69 if os.path.exists(args.target_dir):
70 sys.exit('Target directory %s already exists. Delete it first it you '
71 'want to re-download the toolchain.' % (target_dir, ))
72
73 download_url = get_download_url(toolchain_version)
74 try:
75 archive_file = download(download_url)
76 install(archive_file, target_dir)
77 finally:
78 os.remove(archive_file)
79
80 print('Toolchain downloaded and installed to %s' % (target_dir, ))
81
82
83if __name__ == "__main__":
84 main()