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