Adding SiFive RISC-V toolchain downloading to litex_setup.py
authorTim 'mithro' Ansell <me@mith.ro>
Mon, 6 Apr 2020 23:39:49 +0000 (16:39 -0700)
committerTim 'mithro' Ansell <me@mith.ro>
Mon, 6 Apr 2020 23:51:14 +0000 (16:51 -0700)
litex_setup.py

index 59e8d1b2b0294d589c73f958fc3a16ef7921e792..14fc0cb3743b4ff240072ae863ffc3692232e92f 100755 (executable)
@@ -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.")