Adding SiFive RISC-V toolchain downloading to litex_setup.py
[litex.git] / litex_setup.py
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5 import subprocess
6 from collections import OrderedDict
7
8 import urllib.request
9
10 current_path = os.path.dirname(os.path.realpath(__file__))
11
12 # name, (url, recursive clone, develop)
13 repos = [
14 # HDL
15 ("migen", ("https://github.com/m-labs/", True, True)),
16
17 # LiteX SoC builder
18 ("litex", ("https://github.com/enjoy-digital/", True, True)),
19
20 # LiteX cores ecosystem
21 ("liteeth", ("https://github.com/enjoy-digital/", False, True)),
22 ("litedram", ("https://github.com/enjoy-digital/", False, True)),
23 ("litepcie", ("https://github.com/enjoy-digital/", False, True)),
24 ("litesata", ("https://github.com/enjoy-digital/", False, True)),
25 ("litesdcard", ("https://github.com/enjoy-digital/", False, True)),
26 ("liteiclink", ("https://github.com/enjoy-digital/", False, True)),
27 ("litevideo", ("https://github.com/enjoy-digital/", False, True)),
28 ("litescope", ("https://github.com/enjoy-digital/", False, True)),
29 ("litejesd204b", ("https://github.com/enjoy-digital/", False, True)),
30 ("litespi", ("https://github.com/litex-hub/", False, True)),
31
32 # LiteX boards support
33 ("litex-boards", ("https://github.com/litex-hub/", False, True)),
34 ]
35 repos = OrderedDict(repos)
36
37
38 def sifive_riscv_download():
39 base_url = "https://static.dev.sifive.com/dev-tools/"
40 base_file = "riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-"
41
42 is_windows = (
43 sys.platform.startswith('win') or sys.platform.startswith('cygwin'))
44 if is_windows:
45 end_file = 'w64-mingw32.zip'
46 elif sys.platform.startswith('linux'):
47 end_file = 'linux-ubuntu14.tar.gz'
48 elif sys.platform.startswith('darwin'):
49 end_file = 'apple-darwin.tar.gz'
50 else:
51 raise NotImplementedError(sys.platform)
52 fn = base_file + end_file
53
54 if not os.path.exists(fn):
55 url = base_url+fn
56 print("Downloading", url, "to", fn)
57 urllib.request.urlretrieve(url, fn)
58 else:
59 print("Using existing file", fn)
60
61 print("Extracting", fn)
62 if fn.endswith(".tar.gz"):
63 import tarfile
64 with tarfile.open(fn) as t:
65 t.extractall()
66 elif fn.endswith(".zip"):
67 import zipfile
68 with zipfile.open(fn) as z:
69 z.extractall()
70
71 if "--user" in sys.argv[1:] and not is_windows:
72 print("Linking compiler into ~/.local/bin")
73 local_bin_dir = os.path.join(
74 base_file + end_file.split('.', 1)[0], "bin")
75 assert os.path.exists(local_bin_dir), local_bin_dir
76 user_bin_dir = os.path.abspath(os.path.expanduser("~/.local/bin"))
77 if not os.path.exists(user_bin_dir):
78 os.makedirs(user_bin_dir)
79 for f in os.listdir(local_bin_dir):
80 src = os.path.abspath(os.path.join(local_bin_dir, f))
81 dst = os.path.join(user_bin_dir, f)
82 assert os.path.exists(src), src
83 if os.path.exists(dst):
84 os.unlink(dst)
85 assert not os.path.exists(dst), dst
86 os.symlink(src, dst)
87
88
89 if len(sys.argv) < 2:
90 print("Available commands:")
91 print("- init")
92 print("- install (add --user to install to user directory)")
93 print("- update")
94 print("- gcc")
95 exit()
96
97 if "init" in sys.argv[1:]:
98 os.chdir(os.path.join(current_path))
99 for name in repos.keys():
100 url, need_recursive, need_develop = repos[name]
101 # clone repo (recursive if needed)
102 print("[cloning " + name + "]...")
103 full_url = url + name
104 opts = "--recursive" if need_recursive else ""
105 subprocess.check_call(
106 "git clone " + full_url + " " + opts,
107 shell=True)
108
109 if "install" in sys.argv[1:]:
110 for name in repos.keys():
111 url, need_recursive, need_develop = repos[name]
112 # develop if needed
113 print("[installing " + name + "]...")
114 if need_develop:
115 os.chdir(os.path.join(current_path, name))
116 if "--user" in sys.argv[1:]:
117 subprocess.check_call(
118 "python3 setup.py develop --user",
119 shell=True)
120 else:
121 subprocess.check_call(
122 "python3 setup.py develop",
123 shell=True)
124 os.chdir(os.path.join(current_path))
125
126 if "gcc" in sys.argv[1:]:
127 sifive_riscv_download()
128
129 if "update" in sys.argv[1:]:
130 for name in repos.keys():
131 # update
132 print("[updating " + name + "]...")
133 os.chdir(os.path.join(current_path, name))
134 subprocess.check_call(
135 "git pull",
136 shell=True)
137 os.chdir(os.path.join(current_path))
138
139 if "--user" in sys.argv[1:]:
140 if ".local/bin" not in os.environ.get("PATH", ""):
141 print("Make sure that ~/.local/bin is in your PATH")
142 elif "gcc" in sys.argv[1:]:
143 print("Make sure that the download RISC-V compiler is in your $PATH.")