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 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 19 | # the keys in this dictionary specify valid toolchain kinds |
| 20 | ASSET_PREFIXES = { |
| 21 | # kind : prefix, |
| 22 | "combined": "lowrisc-toolchain-rv32imc-", |
| 23 | "gcc-only": "lowrisc-toolchain-gcc-rv32imc-", |
| 24 | } |
Miguel Osorio | c705000 | 2019-09-20 00:16:04 -0700 | [diff] [blame] | 25 | ASSET_SUFFIX = ".tar.xz" |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 26 | RELEASES_URL_BASE = 'https://api.github.com/repos/lowRISC/lowrisc-toolchains/releases' |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 27 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 28 | INSTALL_DIR = '/tools/riscv' |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 29 | TOOLCHAIN_VERSION = 'latest' |
Sam Elliott | 108b7f7 | 2020-06-03 11:52:24 +0100 | [diff] [blame] | 30 | TOOLCHAIN_KIND = 'combined' |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 31 | |
| 32 | FILE_PATTERNS_TO_REWRITE = [ |
| 33 | "riscv32-unknown-elf-*.cmake", |
| 34 | "meson-riscv32-unknown-elf-*.txt", |
| 35 | ] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 36 | |
| 37 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 38 | def get_available_toolchain_info(version, kind): |
| 39 | assert kind in ASSET_PREFIXES |
| 40 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 41 | if version == 'latest': |
| 42 | releases_url = '%s/%s' % (RELEASES_URL_BASE, version) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 43 | else: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 44 | releases_url = '%s/tags/%s' % (RELEASES_URL_BASE, version) |
| 45 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 46 | with urlopen(releases_url) as f: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 47 | release_info = json.loads(f.read().decode('utf-8')) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 48 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 49 | for asset in release_info["assets"]: |
| 50 | if (asset["name"].startswith(ASSET_PREFIXES[kind]) and |
| 51 | asset["name"].endswith(ASSET_SUFFIX)): |
| 52 | return { |
| 53 | 'download_url': asset['browser_download_url'], |
| 54 | 'name': asset['name'], |
| 55 | 'version': release_info['tag_name'], |
| 56 | 'kind': kind |
| 57 | } |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 58 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 59 | # No matching asset found for the toolchain kind requested |
| 60 | log.error("No available downloads found for %s toolchain version: %s", |
| 61 | kind, release_info['tag_name']) |
| 62 | raise SystemExit(1) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 63 | |
| 64 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 65 | def get_installed_toolchain_info(unpack_dir): |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 66 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 67 | # Try new-style buildinfo.json first |
| 68 | try: |
| 69 | buildinfo = {} |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 70 | with open(str(unpack_dir / 'buildinfo.json'), 'r') as f: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 71 | buildinfo = json.loads(f.read()) |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 72 | |
| 73 | # Toolchains before 20200602-4 contained a `buildinfo.json` without a |
| 74 | # 'kind' field. Setting it to 'unknown' will ensure we never skip |
| 75 | # updating because we think it's the same as the existing toolchain. |
| 76 | if 'kind' not in buildinfo: |
| 77 | buildinfo['kind'] = 'unknown' |
| 78 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 79 | return buildinfo |
| 80 | except Exception as e: |
| 81 | # buildinfo.json might not exist in older builds |
| 82 | log.info("Unable to parse buildinfo.json: %s", str(e)) |
| 83 | pass |
| 84 | |
| 85 | # If that wasn't successful, try old-style plaintext buildinfo |
| 86 | version_re = r"(lowRISC toolchain version|Version):\s*\n?(?P<version>[^\n\s]+)" |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 87 | buildinfo_txt_path = unpack_dir / 'buildinfo' |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 88 | try: |
| 89 | with open(str(buildinfo_txt_path), 'r') as f: |
| 90 | match = re.match(version_re, f.read(), re.M) |
| 91 | if not match: |
| 92 | log.warning("Unable extract version from %s", |
| 93 | str(buildinfo_txt_path)) |
| 94 | return None |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 95 | return {'version': match.group("version"), 'kind': 'unknown'} |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 96 | except Exception as e: |
| 97 | 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] | 98 | return None |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 99 | |
| 100 | |
| 101 | def download(url): |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 102 | log.info("Downloading toolchain from %s", url) |
| 103 | tmpfile = tempfile.mkstemp()[1] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 104 | urlretrieve(url, tmpfile) |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 105 | log.info("Download complete") |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 106 | return Path(tmpfile) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 107 | |
| 108 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 109 | def install(archive_file, unpack_dir): |
| 110 | unpack_dir.mkdir(parents=True, exist_ok=True) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 111 | |
| 112 | cmd = [ |
Srikrishna Iyer | ec601e1 | 2020-11-17 22:57:40 -0800 | [diff] [blame] | 113 | 'tar', |
| 114 | '-x', |
| 115 | '-f', |
| 116 | str(archive_file), |
| 117 | '--strip-components=1', |
| 118 | '-C', |
| 119 | str(unpack_dir), |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 120 | ] |
| 121 | subprocess.run(cmd, check=True) |
| 122 | |
| 123 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 124 | def postinstall_rewrite_configs(unpack_dir, install_dir): |
| 125 | """Rewrites the toolchain configuration files to point to install_dir. |
| 126 | |
| 127 | 'unpack_dir' is where the toolchain is unpacked by this script. |
| 128 | 'install_dir' is where the toolchain is eventually invoked from. Typically, |
| 129 | these are the same, unless a staged installation is being performed by |
| 130 | supplying both, --install-dir and --dest-dir switches. Regardless, if the |
| 131 | 'install_dir' is different from the default, the config files need to be |
| 132 | updated to reflect the correct paths. |
| 133 | """ |
| 134 | if str(install_dir) == INSTALL_DIR: |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 135 | return |
| 136 | |
| 137 | for file_pattern in FILE_PATTERNS_TO_REWRITE: |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 138 | for config_file_path in unpack_dir.glob(file_pattern): |
| 139 | # Rewrite INSTALL_DIR to the requested target dir. |
Srikrishna Iyer | ec601e1 | 2020-11-17 22:57:40 -0800 | [diff] [blame] | 140 | log.info("Updating toolchain paths in %s", str(config_file_path)) |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 141 | with open(str(config_file_path)) as f: |
| 142 | original = f.read() |
| 143 | with open(str(config_file_path), "w") as f: |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 144 | f.write(original.replace(INSTALL_DIR, str(install_dir))) |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 145 | |
| 146 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 147 | def main(): |
| 148 | parser = argparse.ArgumentParser() |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 149 | parser.add_argument('--install-dir', |
| 150 | '-i', |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 151 | required=False, |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 152 | default=INSTALL_DIR, |
| 153 | help="Installation directory (default: %(default)s)") |
| 154 | parser.add_argument('--dest-dir', |
| 155 | '-d', |
| 156 | required=False, |
| 157 | help="""Destination directory if performing a staged |
| 158 | installation. This is the staging directory where the |
| 159 | toolchain is unpacked.""") |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 160 | parser.add_argument('--release-version', |
| 161 | '-r', |
| 162 | required=False, |
| 163 | default=TOOLCHAIN_VERSION, |
| 164 | help="Toolchain version (default: %(default)s)") |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 165 | parser.add_argument('--latest-available-version', |
| 166 | '-l', |
| 167 | required=False, |
| 168 | default=False, |
| 169 | action='store_true', |
| 170 | help="Return the latest available toolchain version.") |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 171 | parser.add_argument('--kind', |
| 172 | required=False, |
| 173 | default=TOOLCHAIN_KIND, |
| 174 | choices=ASSET_PREFIXES.keys(), |
| 175 | help="Toolchain kind (default: %(default)s)") |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 176 | parser.add_argument( |
| 177 | '--update', |
| 178 | '-u', |
| 179 | required=False, |
| 180 | default=False, |
| 181 | action='store_true', |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 182 | help="Update to target version if needed (default: %(default)s)") |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 183 | args = parser.parse_args() |
| 184 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 185 | available_toolchain = get_available_toolchain_info(args.release_version, |
| 186 | args.kind) |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 187 | |
| 188 | if args.latest_available_version: |
| 189 | print(available_toolchain['version']) |
| 190 | sys.exit(0) |
| 191 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 192 | log.info("Found available %s toolchain version %s, %s", |
| 193 | available_toolchain['kind'], available_toolchain['version'], |
| 194 | available_toolchain['name']) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 195 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 196 | install_dir = Path(args.install_dir) |
| 197 | if args.dest_dir is None: |
| 198 | unpack_dir = install_dir |
| 199 | else: |
| 200 | unpack_dir = Path(args.dest_dir) |
| 201 | |
| 202 | if args.update and unpack_dir.is_dir(): |
| 203 | installed_toolchain = get_installed_toolchain_info(unpack_dir) |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 204 | if installed_toolchain is None: |
| 205 | sys.exit('Unable to extract current toolchain version. ' |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 206 | 'Delete target directory %s and try again.' % |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 207 | str(unpack_dir)) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 208 | |
Srikrishna Iyer | ec601e1 | 2020-11-17 22:57:40 -0800 | [diff] [blame] | 209 | version_matches = available_toolchain[ |
| 210 | 'version'] == installed_toolchain['version'] |
| 211 | kind_matches = available_toolchain['kind'] == installed_toolchain[ |
| 212 | 'kind'] |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 213 | |
Srikrishna Iyer | ec601e1 | 2020-11-17 22:57:40 -0800 | [diff] [blame] | 214 | if installed_toolchain[ |
| 215 | 'kind'] != 'unknown' and version_matches and kind_matches: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 216 | log.info( |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 217 | 'Downloaded %s toolchain is version %s, ' |
| 218 | 'same as the %s toolchain installed at %s (version %s).', |
| 219 | available_toolchain['kind'], available_toolchain['version'], |
| 220 | installed_toolchain['kind'], installed_toolchain['version'], |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 221 | str(unpack_dir)) |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 222 | log.warning("Skipping install.") |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 223 | sys.exit(0) |
Miguel Osorio | 1e5fc8e | 2019-09-23 17:33:54 -0700 | [diff] [blame] | 224 | |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 225 | log.info( |
Srikrishna Iyer | ec601e1 | 2020-11-17 22:57:40 -0800 | [diff] [blame] | 226 | "Found installed %s toolchain version %s, updating to %s toolchain " |
| 227 | "version %s.", |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 228 | installed_toolchain['kind'], installed_toolchain['version'], |
| 229 | available_toolchain['kind'], available_toolchain['version']) |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 230 | else: |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 231 | if unpack_dir.exists(): |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 232 | sys.exit('Target directory %s already exists. ' |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 233 | 'Delete it first, or use --update.' % str(unpack_dir)) |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 234 | |
| 235 | archive_file = None |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 236 | try: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 237 | archive_file = download(available_toolchain['download_url']) |
| 238 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 239 | if args.update and unpack_dir.exists(): |
| 240 | # We only reach this point if |unpack_dir| contained a toolchain |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 241 | # before, so removing it is reasonably safe. |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 242 | shutil.rmtree(str(unpack_dir)) |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 243 | |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 244 | install(archive_file, unpack_dir) |
Srikrishna Iyer | ec601e1 | 2020-11-17 22:57:40 -0800 | [diff] [blame] | 245 | postinstall_rewrite_configs(unpack_dir.resolve(), |
| 246 | install_dir.resolve()) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 247 | finally: |
Philipp Wagner | 1c51441 | 2019-11-27 14:48:47 +0000 | [diff] [blame] | 248 | if archive_file: |
| 249 | archive_file.unlink() |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 250 | |
Sam Elliott | c09da26 | 2020-06-03 11:10:36 +0100 | [diff] [blame] | 251 | log.info('Installed %s toolchain version %s to %s.', |
| 252 | available_toolchain['kind'], available_toolchain['version'], |
Srikrishna Iyer | 98333ba | 2020-11-16 23:12:29 -0800 | [diff] [blame] | 253 | str(unpack_dir)) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 254 | |
| 255 | |
| 256 | if __name__ == "__main__": |
| 257 | main() |