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