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