lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | #!/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 | |
| 6 | import argparse |
| 7 | import json |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 8 | import logging as log |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 9 | import re |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 10 | import shutil |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 11 | import subprocess |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 12 | import sys |
| 13 | import tempfile |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 14 | from pathlib import Path |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 15 | from urllib.request import urlopen, urlretrieve |
| 16 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 17 | log.basicConfig(level=log.INFO, format="%(levelname)s: %(message)s") |
| 18 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 19 | ASSET_PREFIX = "lowrisc-toolchain-gcc-rv32imc-" |
Miguel Osorio | c705000 | 2019-09-20 00:16:04 -0700 | [diff] [blame] | 20 | ASSET_SUFFIX = ".tar.xz" |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 21 | RELEASES_URL_BASE = 'https://api.github.com/repos/lowRISC/lowrisc-toolchains/releases' |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 22 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 23 | TARGET_DIR = '/tools/riscv' |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 24 | TOOLCHAIN_VERSION = 'latest' |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 25 | |
| 26 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 27 | def get_available_toolchain_info(version): |
| 28 | if version == 'latest': |
| 29 | releases_url = '%s/%s' % (RELEASES_URL_BASE, version) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 30 | else: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 31 | releases_url = '%s/tags/%s' % (RELEASES_URL_BASE, version) |
| 32 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 33 | with urlopen(releases_url) as f: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 34 | release_info = json.loads(f.read().decode('utf-8')) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 35 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 36 | download_url = [ |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 37 | a["browser_download_url"] for a in release_info["assets"] |
| 38 | if (a["name"].startswith(ASSET_PREFIX) and |
| 39 | a["name"].endswith(ASSET_SUFFIX)) |
| 40 | ][0] |
| 41 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 42 | return {'download_url': download_url, 'version': release_info['tag_name']} |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 43 | |
| 44 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 45 | def get_installed_toolchain_info(install_path): |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 46 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 47 | # Try new-style buildinfo.json first |
| 48 | try: |
| 49 | buildinfo = {} |
| 50 | with open(str(install_path / 'buildinfo.json'), 'r') as f: |
| 51 | buildinfo = json.loads(f.read()) |
| 52 | return buildinfo |
| 53 | except Exception as e: |
| 54 | # buildinfo.json might not exist in older builds |
| 55 | log.info("Unable to parse buildinfo.json: %s", str(e)) |
| 56 | pass |
| 57 | |
| 58 | # If that wasn't successful, try old-style plaintext buildinfo |
| 59 | version_re = r"(lowRISC toolchain version|Version):\s*\n?(?P<version>[^\n\s]+)" |
| 60 | buildinfo_txt_path = install_path / 'buildinfo' |
| 61 | try: |
| 62 | with open(str(buildinfo_txt_path), 'r') as f: |
| 63 | match = re.match(version_re, f.read(), re.M) |
| 64 | if not match: |
| 65 | log.warning("Unable extract version from %s", |
| 66 | str(buildinfo_txt_path)) |
| 67 | return None |
| 68 | return {'version': match.group("version")} |
| 69 | except Exception as e: |
| 70 | log.error("Unable to read %s: %s", str(buildinfo_txt_path), str(e)) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 71 | return None |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def download(url): |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 75 | log.info("Downloading toolchain from %s", url) |
| 76 | tmpfile = tempfile.mkstemp()[1] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 77 | urlretrieve(url, tmpfile) |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 78 | return Path(tmpfile) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 79 | |
| 80 | |
Miguel Osorio | 8e28280 | 2019-09-24 10:37:27 -0700 | [diff] [blame] | 81 | def install(archive_file, target_dir): |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 82 | target_dir.mkdir(parents=True, exist_ok=True) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 83 | |
| 84 | cmd = [ |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 85 | 'tar', '-x', '-f', str(archive_file), '--strip-components=1', '-C', |
| 86 | str(target_dir) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 87 | ] |
| 88 | subprocess.run(cmd, check=True) |
| 89 | |
| 90 | |
| 91 | def main(): |
| 92 | parser = argparse.ArgumentParser() |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 93 | parser.add_argument('--target-dir', |
| 94 | '-t', |
| 95 | required=False, |
| 96 | default=TARGET_DIR, |
| 97 | help="Target directory (default: %(default)s)") |
| 98 | parser.add_argument('--release-version', |
| 99 | '-r', |
| 100 | required=False, |
| 101 | default=TOOLCHAIN_VERSION, |
| 102 | help="Toolchain version (default: %(default)s)") |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 103 | parser.add_argument( |
| 104 | '--update', |
| 105 | '-u', |
| 106 | required=False, |
| 107 | default=False, |
| 108 | action='store_true', |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 109 | help="Update to target version if needed (default: %(default)s)") |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 110 | args = parser.parse_args() |
| 111 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 112 | target_dir = Path(args.target_dir) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 113 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 114 | available_toolchain = get_available_toolchain_info(args.release_version) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 115 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 116 | if args.update and target_dir.is_dir(): |
| 117 | installed_toolchain = get_installed_toolchain_info(target_dir) |
| 118 | if installed_toolchain is None: |
| 119 | sys.exit('Unable to extract current toolchain version. ' |
| 120 | 'Delete target directory and try again.') |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 121 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 122 | if available_toolchain['version'] == installed_toolchain['version']: |
| 123 | log.info( |
| 124 | 'Toolchain version %s already installed at %s. Skipping ' |
| 125 | 'install.', installed_toolchain['version'], str(target_dir)) |
| 126 | sys.exit(0) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 127 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 128 | log.info( |
| 129 | "Found installed toolchain version %s, updating to version %s.", |
| 130 | installed_toolchain['version'], available_toolchain['version']) |
| 131 | else: |
| 132 | if target_dir.exists(): |
| 133 | sys.exit( |
| 134 | 'Target directory %s already exists. Delete it first, or use --update.' |
| 135 | % str(target_dir)) |
| 136 | |
| 137 | archive_file = None |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 138 | try: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 139 | archive_file = download(available_toolchain['download_url']) |
| 140 | |
| 141 | if args.update and target_dir.exists(): |
| 142 | # We only reach this point if |target_dir| contained a toolchain |
| 143 | # before, so removing it is reasonably safe. |
Miguel Osorio | 8e28280 | 2019-09-24 10:37:27 -0700 | [diff] [blame] | 144 | shutil.rmtree(target_dir) |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 145 | |
Miguel Osorio | 8e28280 | 2019-09-24 10:37:27 -0700 | [diff] [blame] | 146 | install(archive_file, target_dir) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 147 | finally: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 148 | if archive_file: |
| 149 | archive_file.unlink() |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 150 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame^] | 151 | log.info('Toolchain version %s downloaded and installed to %s.', |
| 152 | available_toolchain['version'], str(target_dir)) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 153 | |
| 154 | |
| 155 | if __name__ == "__main__": |
| 156 | main() |