Use shutil rather then rm -rf command.
[litex.git] / make.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import argparse
6 import subprocess
7 import struct
8 import shutil
9
10 from mibuild.tools import write_to_file
11 from migen.util.misc import autotype
12 from migen.fhdl import simplify
13
14 from misoclib.soc import cpuif
15 from misoclib.mem.sdram.phy import initsequence
16
17 from misoc_import import misoc_import
18
19
20 def _get_args():
21 parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
22 description="""\
23 MiSoC - a high performance and small footprint SoC based on Migen.
24
25 This program builds and/or loads MiSoC components.
26 One or several actions can be specified:
27
28 clean delete previous build(s).
29 build-bitstream build FPGA bitstream. Implies build-bios on targets with
30 integrated BIOS.
31 build-headers build software header files with CPU/CSR/IRQ/SDRAM_PHY definitions.
32 build-csr-csv save CSR map into CSV file.
33 build-bios build BIOS. Implies build-header.
34
35 load-bitstream load bitstream into volatile storage.
36 flash-bitstream load bitstream into non-volatile storage.
37 flash-bios load BIOS into non-volatile storage.
38
39 all clean, build-bitstream, build-bios, flash-bitstream, flash-bios.
40
41 Load/flash actions use the existing outputs, and do not trigger new builds.
42 """)
43
44 parser.add_argument("-t", "--target", default="mlabs_video", help="SoC type to build")
45 parser.add_argument("-s", "--sub-target", default="", help="variant of the SoC type to build")
46 parser.add_argument("-p", "--platform", default=None, help="platform to build for")
47 parser.add_argument("-Ot", "--target-option", default=[], nargs=2, action="append", help="set target-specific option")
48 parser.add_argument("-Op", "--platform-option", default=[], nargs=2, action="append", help="set platform-specific option")
49 parser.add_argument("-X", "--external", default="", help="use external directory for targets, platforms and imports")
50 parser.add_argument("--csr_csv", default="csr.csv", help="CSV file to save the CSR map into")
51
52 parser.add_argument("-d", "--decorate", default=[], action="append", help="apply simplification decorator to top-level")
53 parser.add_argument("-Ob", "--build-option", default=[], nargs=2, action="append", help="set build option")
54 parser.add_argument("-f", "--flash-proxy-dir", default=None, help="set search directory for flash proxy bitstreams")
55
56 parser.add_argument("action", nargs="+", help="specify an action")
57
58 return parser.parse_args()
59
60 if __name__ == "__main__":
61 args = _get_args()
62
63 external_target = ""
64 external_platform = ""
65 if args.external:
66 external_target = os.path.join(args.external, "targets")
67 external_platform = os.path.join(args.external, "platforms")
68 sys.path.insert(0, os.path.abspath(args.external))
69
70 # create top-level SoC object
71 target_module = misoc_import("targets", external_target, args.target)
72 if args.sub_target:
73 top_class = getattr(target_module, args.sub_target)
74 else:
75 top_class = target_module.default_subtarget
76
77 if args.platform is None:
78 if hasattr(top_class, "default_platform"):
79 platform_name = top_class.default_platform
80 else:
81 raise ValueError("Target has no default platform, specify a platform with -p your_platform")
82 else:
83 platform_name = args.platform
84 platform_module = misoc_import("mibuild.platforms", external_platform, platform_name)
85 platform_kwargs = dict((k, autotype(v)) for k, v in args.platform_option)
86 platform = platform_module.Platform(**platform_kwargs)
87 if args.external:
88 platform.soc_ext_path = os.path.abspath(args.external)
89
90 build_name = args.target + "-" + top_class.__name__.lower() + "-" + platform_name
91 top_kwargs = dict((k, autotype(v)) for k, v in args.target_option)
92 soc = top_class(platform, **top_kwargs)
93 soc.finalize()
94 memory_regions = soc.get_memory_regions()
95 csr_regions = soc.get_csr_regions()
96
97 # decode actions
98 action_list = ["clean", "build-bitstream", "build-headers", "build-csr-csv", "build-bios",
99 "load-bitstream", "flash-bitstream", "flash-bios", "all"]
100 actions = {k: False for k in action_list}
101 for action in args.action:
102 if action in actions:
103 actions[action] = True
104 else:
105 print("Unknown action: {}. Valid actions are:".format(action))
106 for a in action_list:
107 print(" "+a)
108 sys.exit(1)
109
110 print("""\
111 __ ___ _ ____ _____
112 / |/ / (_) / __/__ / ___/
113 / /|_/ / / / _\ \/ _ \/ /__
114 /_/ /_/ /_/ /___/\___/\___/
115
116 a high performance and small footprint SoC based on Migen
117
118 ====== Building for: ======
119 Platform: {}
120 Target: {}
121 Subtarget: {}
122 CPU type: {}
123 ===========================""".format(platform_name, args.target, top_class.__name__, soc.cpu_type))
124
125 # dependencies
126 if actions["all"]:
127 actions["clean"] = True
128 actions["build-bitstream"] = True
129 actions["build-bios"] = True
130 if not actions["load-bitstream"]:
131 actions["flash-bitstream"] = True
132 if not soc.integrated_rom_size:
133 actions["flash-bios"] = True
134 if actions["build-bitstream"] and soc.integrated_rom_size:
135 actions["build-bios"] = True
136 if actions["build-bios"]:
137 actions["build-headers"] = True
138
139 if actions["clean"]:
140 shutil.rmtree("build") # Need shell for the build/* globbing
141 os.mkdir("build")
142 subprocess.check_call(["make", "-C", os.path.join("software", "libcompiler-rt"), "clean"])
143 subprocess.check_call(["make", "-C", os.path.join("software", "libbase"), "clean"])
144 subprocess.check_call(["make", "-C", os.path.join("software", "libnet"), "clean"])
145 subprocess.check_call(["make", "-C", os.path.join("software", "bios"), "clean"])
146
147 if actions["build-headers"]:
148 boilerplate = """/*
149 * Platform: {}
150 * Target: {}
151 * Subtarget: {}
152 * CPU type: {}
153 */
154
155 """.format(platform_name, args.target, top_class.__name__, soc.cpu_type)
156 genhdir = os.path.join("software", "include", "generated")
157 if soc.cpu_type != "none":
158 cpu_mak = cpuif.get_cpu_mak(soc.cpu_type)
159 write_to_file(os.path.join(genhdir, "cpu.mak"), cpu_mak)
160 linker_output_format = cpuif.get_linker_output_format(soc.cpu_type)
161 write_to_file(os.path.join(genhdir, "output_format.ld"), linker_output_format)
162
163 linker_regions = cpuif.get_linker_regions(memory_regions)
164 write_to_file(os.path.join(genhdir, "regions.ld"), boilerplate + linker_regions)
165
166 for sdram_phy in ["sdrphy", "ddrphy"]:
167 if hasattr(soc, sdram_phy):
168 sdram_phy_header = initsequence.get_sdram_phy_header(getattr(soc, sdram_phy).settings)
169 write_to_file(os.path.join(genhdir, "sdram_phy.h"), boilerplate + sdram_phy_header)
170 mem_header = cpuif.get_mem_header(memory_regions, getattr(soc, "flash_boot_address", None))
171 write_to_file(os.path.join(genhdir, "mem.h"), boilerplate + mem_header)
172 csr_header = cpuif.get_csr_header(csr_regions, soc.get_constants())
173 write_to_file(os.path.join(genhdir, "csr.h"), boilerplate + csr_header)
174
175 if actions["build-csr-csv"]:
176 csr_csv = cpuif.get_csr_csv(csr_regions)
177 write_to_file(args.csr_csv, csr_csv)
178
179 if actions["build-bios"]:
180 ret = subprocess.call(["make", "-C", os.path.join("software", "bios")])
181 if ret:
182 raise OSError("BIOS build failed")
183
184 bios_file = os.path.join("software", "bios", "bios.bin")
185
186 if actions["build-bitstream"]:
187 if soc.integrated_rom_size:
188 with open(bios_file, "rb") as boot_file:
189 boot_data = []
190 while True:
191 w = boot_file.read(4)
192 if not w:
193 break
194 boot_data.append(struct.unpack(">I", w)[0])
195 soc.init_rom(boot_data)
196
197 for decorator in args.decorate:
198 soc = getattr(simplify, decorator)(soc)
199 build_kwargs = dict((k, autotype(v)) for k, v in args.build_option)
200 vns = platform.build(soc, build_name=build_name, **build_kwargs)
201 soc.do_exit(vns)
202
203 if actions["load-bitstream"]:
204 prog = platform.create_programmer()
205 prog.load_bitstream(os.path.join("build", build_name + platform.bitstream_ext))
206
207 if actions["flash-bitstream"]:
208 prog = platform.create_programmer()
209 prog.set_flash_proxy_dir(args.flash_proxy_dir)
210 if prog.needs_bitreverse:
211 flashbit = os.path.join("build", build_name + ".fpg")
212 subprocess.check_call([os.path.join("tools", "byteswap"),
213 os.path.join("build", build_name + ".bin"),
214 flashbit])
215 else:
216 flashbit = os.path.join("build", build_name + ".bin")
217 prog.flash(0, flashbit)
218
219 if actions["flash-bios"]:
220 prog = platform.create_programmer()
221 prog.set_flash_proxy_dir(args.flash_proxy_dir)
222 prog.flash(soc.cpu_reset_address, bios_file)