From 6adabae730603cf2248aef429c243ce12a0830d4 Mon Sep 17 00:00:00 2001 From: Tim 'mithro' Ansell Date: Mon, 6 Apr 2020 16:39:49 -0700 Subject: [PATCH] Adding SiFive RISC-V toolchain downloading to litex_setup.py --- litex_setup.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/litex_setup.py b/litex_setup.py index 59e8d1b2b..14fc0cb37 100755 --- a/litex_setup.py +++ b/litex_setup.py @@ -5,6 +5,7 @@ import sys import subprocess from collections import OrderedDict +import urllib.request current_path = os.path.dirname(os.path.realpath(__file__)) @@ -33,14 +34,68 @@ repos = [ ] repos = OrderedDict(repos) + +def sifive_riscv_download(): + base_url = "https://static.dev.sifive.com/dev-tools/" + base_file = "riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-" + + is_windows = ( + sys.platform.startswith('win') or sys.platform.startswith('cygwin')) + if is_windows: + end_file = 'w64-mingw32.zip' + elif sys.platform.startswith('linux'): + end_file = 'linux-ubuntu14.tar.gz' + elif sys.platform.startswith('darwin'): + end_file = 'apple-darwin.tar.gz' + else: + raise NotImplementedError(sys.platform) + fn = base_file + end_file + + if not os.path.exists(fn): + url = base_url+fn + print("Downloading", url, "to", fn) + urllib.request.urlretrieve(url, fn) + else: + print("Using existing file", fn) + + print("Extracting", fn) + if fn.endswith(".tar.gz"): + import tarfile + with tarfile.open(fn) as t: + t.extractall() + elif fn.endswith(".zip"): + import zipfile + with zipfile.open(fn) as z: + z.extractall() + + if "--user" in sys.argv[1:] and not is_windows: + print("Linking compiler into ~/.local/bin") + local_bin_dir = os.path.join( + base_file + end_file.split('.', 1)[0], "bin") + assert os.path.exists(local_bin_dir), local_bin_dir + user_bin_dir = os.path.abspath(os.path.expanduser("~/.local/bin")) + if not os.path.exists(user_bin_dir): + os.makedirs(user_bin_dir) + for f in os.listdir(local_bin_dir): + src = os.path.abspath(os.path.join(local_bin_dir, f)) + dst = os.path.join(user_bin_dir, f) + assert os.path.exists(src), src + if os.path.exists(dst): + os.unlink(dst) + assert not os.path.exists(dst), dst + os.symlink(src, dst) + + if len(sys.argv) < 2: print("Available commands:") print("- init") print("- install (add --user to install to user directory)") print("- update") + print("- gcc") exit() if "init" in sys.argv[1:]: + os.chdir(os.path.join(current_path)) for name in repos.keys(): url, need_recursive, need_develop = repos[name] # clone repo (recursive if needed) @@ -66,6 +121,10 @@ if "install" in sys.argv[1:]: subprocess.check_call( "python3 setup.py develop", shell=True) + os.chdir(os.path.join(current_path)) + +if "gcc" in sys.argv[1:]: + sifive_riscv_download() if "update" in sys.argv[1:]: for name in repos.keys(): @@ -75,3 +134,10 @@ if "update" in sys.argv[1:]: subprocess.check_call( "git pull", shell=True) + os.chdir(os.path.join(current_path)) + +if "--user" in sys.argv[1:]: + if ".local/bin" not in os.environ.get("PATH", ""): + print("Make sure that ~/.local/bin is in your PATH") +elif "gcc" in sys.argv[1:]: + print("Make sure that the download RISC-V compiler is in your $PATH.")