986dda424d53302e7538a95cd09e8837d290eb7a
[gem5.git] / util / gen_arm_fs_files.py
1 #!/usr/bin/env python2.7
2
3 # Copyright (c) 2017-2018 Metempsy Technology Consulting
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are
8 # met: redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer;
10 # redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution;
13 # neither the name of the copyright holders nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 from optparse import OptionParser
30 from subprocess import call
31 from platform import machine
32 from distutils import spawn
33 from glob import glob
34
35 import sys
36 import os
37
38 def run_cmd(explanation, working_dir, cmd, stdout = None):
39 print "Running phase '%s'" % explanation
40 sys.stdout.flush()
41
42 # some of the commands need $PWD to be properly set
43 env = os.environ.copy()
44 env['PWD'] = working_dir
45
46 return_code = call(cmd, cwd = working_dir, stdout = stdout,
47 env = env)
48
49 if return_code == 0:
50 return
51
52 print "Error running phase %s. Returncode: %d" % (explanation, return_code)
53 sys.exit(1)
54
55 def linux_clone():
56 kernel_vexpress_gem5_dir = os.path.join(
57 options.dest_dir, "linux-kernel-vexpress_gem5")
58
59 run_cmd("clone linux kernel for VExpress_GEM5_V1 platform",
60 options.dest_dir,
61 ["git", "clone", "https://gem5.googlesource.com/arm/linux",
62 kernel_vexpress_gem5_dir])
63
64 def linux64():
65 kernel_vexpress_gem5_dir = os.path.join(
66 options.dest_dir, "linux-kernel-vexpress_gem5")
67
68 linux_bin = os.path.join(
69 binaries_dir, "vmlinux.vexpress_gem5_v1_64")
70
71 with open(revisions_dir + "/linux", "w+") as rev_file:
72 run_cmd("write revision of linux-kernel-vexpress_gem5 repo",
73 kernel_vexpress_gem5_dir,
74 ["git", "rev-parse", "--short", "HEAD"],
75 rev_file)
76
77 run_cmd("configure kernel for arm64",
78 kernel_vexpress_gem5_dir,
79 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
80 "gem5_defconfig", make_jobs_str])
81 run_cmd("compile kernel for arm64",
82 kernel_vexpress_gem5_dir,
83 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
84 make_jobs_str])
85 run_cmd("copy arm64 vmlinux",
86 kernel_vexpress_gem5_dir,
87 ["cp", "vmlinux", linux_bin])
88 run_cmd("cleanup arm64 kernel compilation",
89 kernel_vexpress_gem5_dir,
90 ["make", "distclean"])
91
92 def linux32():
93 kernel_vexpress_gem5_dir = os.path.join(
94 options.dest_dir, "linux-kernel-vexpress_gem5")
95
96 linux_bin = os.path.join(
97 binaries_dir, "vmlinux.vexpress_gem5_v1")
98
99 run_cmd("configure kernel for arm",
100 kernel_vexpress_gem5_dir,
101 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
102 "gem5_defconfig"])
103 run_cmd("compile kernel for arm",
104 kernel_vexpress_gem5_dir,
105 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
106 make_jobs_str])
107 run_cmd("copy arm vmlinux",
108 kernel_vexpress_gem5_dir,
109 ["cp", "vmlinux", linux_bin])
110
111 def linux():
112 """
113 Checkout and build linux kernel for VExpress_GEM5_V1 (arm and arm64)
114 """
115 linux_clone()
116 linux64()
117 linux32()
118
119 def linux_legacy():
120 """
121 Checkout and build linux kernel and DTB for VExpress_EMM64/EMM
122 """
123 kernel_vexpress_emm64_dir = os.path.join(options.dest_dir,
124 "linux-kernel-vexpress_emm64")
125 run_cmd("clone linux kernel for VExpress_EMM64 platform",
126 options.dest_dir,
127 ["git", "clone", "https://gem5.googlesource.com/arm/linux-arm64-legacy",
128 kernel_vexpress_emm64_dir])
129 with open(revisions_dir + "/linux-arm64-legacy", "w+") as rev_file:
130 run_cmd("write revision of linux-kernel-vexpress_emm64 repo",
131 kernel_vexpress_emm64_dir,
132 ["git", "rev-parse", "--short", "HEAD"],
133 rev_file)
134 run_cmd("configure kernel",
135 kernel_vexpress_emm64_dir,
136 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
137 "CC=aarch64-linux-gnu-gcc-4.8", "gem5_defconfig"])
138 run_cmd("compile kernel",
139 kernel_vexpress_emm64_dir,
140 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
141 "CC=aarch64-linux-gnu-gcc-4.8", make_jobs_str])
142 run_cmd("copy vmlinux",
143 kernel_vexpress_emm64_dir,
144 ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm64"])
145 run_cmd("copy DTB",
146 kernel_vexpress_emm64_dir,
147 ["cp", "arch/arm64/boot/dts/aarch64_gem5_server.dtb", binaries_dir])
148
149 kernel_vexpress_emm_dir = options.dest_dir + "/linux-kernel-vexpress_emm"
150 run_cmd("clone linux kernel for VExpress_EMM platform",
151 options.dest_dir,
152 ["git", "clone", "https://gem5.googlesource.com/arm/linux-arm-legacy",
153 kernel_vexpress_emm_dir])
154 with open(revisions_dir + "/linux-arm-legacy", "w+") as rev_file:
155 run_cmd("write revision of linux-kernel-vexpress_emm64 repo",
156 kernel_vexpress_emm_dir,
157 ["git", "rev-parse", "--short", "HEAD"],
158 rev_file)
159 run_cmd("configure kernel",
160 kernel_vexpress_emm_dir,
161 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
162 "CC=arm-linux-gnueabihf-gcc-4.8", "vexpress_gem5_server_defconfig"])
163 run_cmd("compile kernel",
164 kernel_vexpress_emm_dir,
165 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
166 "CC=arm-linux-gnueabihf-gcc-4.8", make_jobs_str])
167 run_cmd("copy vmlinux",
168 kernel_vexpress_emm_dir,
169 ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm"])
170 run_cmd("rename DTB for 1 CPU",
171 kernel_vexpress_emm_dir,
172 ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5.dtb",
173 binaries_dir + "/vexpress-v2p-ca15-tc1-gem5_1cpus.dtb"])
174 run_cmd("copy DTBs",
175 kernel_vexpress_emm_dir,
176 ["cp"] + glob(kernel_vexpress_emm_dir + "/arch/arm/boot/dts/*gem5_*dtb") +
177 [binaries_dir])
178
179 def dtbs():
180 """
181 Build DTBs for VExpress_GEM5_V1
182 """
183 dt_dir = gem5_dir + "/system/arm/dt"
184 run_cmd("compile DTBs for VExpress_GEM5_V1 platform",
185 dt_dir,
186 ["make", make_jobs_str])
187 run_cmd("copy DTBs",
188 dt_dir,
189 ["cp"] + glob(dt_dir + "/*dtb") + [binaries_dir])
190
191 def bootloaders():
192 """
193 Build bootloaders arm64/arm
194 """
195
196 bootloader_arm64_dir = gem5_dir + "/system/arm/bootloader/arm64"
197 run_cmd("compile arm64 bootloader",
198 bootloader_arm64_dir,
199 ["make"])
200 run_cmd("copy arm64 bootloader",
201 bootloader_arm64_dir,
202 ["cp", "boot.arm64", "boot_emm.arm64", "boot_v2.arm64", binaries_dir])
203
204 bootloader_arm_dir = gem5_dir + "/system/arm/bootloader/arm"
205 run_cmd("compile arm bootloader",
206 bootloader_arm_dir,
207 ["make"])
208 run_cmd("copy arm bootloaders",
209 bootloader_arm_dir,
210 ["cp", "boot.arm", "boot_emm.arm", binaries_dir])
211
212 def m5():
213 """
214 Build m5 binaries
215 """
216 m5_dir = gem5_dir + "/util/m5"
217 run_cmd("compile arm64 m5",
218 m5_dir,
219 ["make", "-f", "Makefile.aarch64"])
220 run_cmd("copy arm64 m5",
221 m5_dir,
222 ["cp", "m5", binaries_dir + "/m5.aarch64"])
223 run_cmd("clean arm64 m5",
224 m5_dir,
225 ["make", "clean", "-f", "Makefile.aarch64"])
226 run_cmd("compile arm m5",
227 m5_dir,
228 ["make", "-f", "Makefile.arm"])
229 run_cmd("copy arm m5",
230 m5_dir,
231 ["cp", "m5", binaries_dir + "/m5.aarch32"])
232
233 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
234 gem5_dir = os.path.dirname(script_dir)
235
236 all_binaries = {
237 "linux" : linux,
238 "linux-legacy" : linux_legacy,
239 "dtbs" : dtbs,
240 "bootloaders" : bootloaders,
241 "m5" : m5,
242 }
243
244 parser = OptionParser()
245
246 parser.add_option("--gem5-dir", default = gem5_dir,
247 metavar = "GEM5_DIR",
248 help = "gem5 root directory to be used for bootloader and "
249 "VExpress_GEM5_V1 DTB sources. The default value is the gem5 root "
250 "directory of the executed script (%default)")
251 parser.add_option("--dest-dir", default = "/tmp",
252 metavar = "DEST_DIR",
253 help = "Directory to use for checking out the different kernel "
254 "repositories. Generated files will be copied to "
255 "DEST_DIR/binaries (which must not exist). The default "
256 "value is %default")
257 parser.add_option("-j", "--make-jobs", type = "int", default = 1,
258 metavar = "MAKE_JOBS",
259 help = "Number of jobs to use with the 'make' commands. Default value: "
260 "%default")
261 parser.add_option("-b", "--fs-binaries", action="append",
262 choices=all_binaries.keys(), default=[],
263 help = "List of FS files to be generated. Defaulting to all")
264
265 (options, args) = parser.parse_args()
266
267 if args:
268 print "Unrecognized argument(s) %s." % args
269 sys.exit(1)
270
271 if not os.path.isdir(options.dest_dir):
272 print "Error: %s is not a directory." % options.dest_dir
273 sys.exit(1)
274
275 if not os.path.isdir(options.gem5_dir):
276 print "Error: %s is not a directory." % options.gem5_dir
277 sys.exit(1)
278
279 if machine() != "x86_64":
280 print "Error: This script should run in a x86_64 machine"
281 sys.exit(1)
282
283 binaries_dir = options.dest_dir + "/binaries"
284
285 if os.path.exists(binaries_dir):
286 print "Error: %s already exists." % binaries_dir
287 sys.exit(1)
288
289 revisions_dir = options.dest_dir + "/revisions"
290
291 if os.path.exists(revisions_dir):
292 print "Error: %s already exists." %revisions_dir
293 sys.exit(1)
294
295 os.mkdir(binaries_dir);
296 os.mkdir(revisions_dir);
297
298 make_jobs_str = "-j" + str(options.make_jobs)
299
300 rev_file = open(revisions_dir + "/gem5", "w+")
301 run_cmd("write revision of gem5 repo",
302 gem5_dir,
303 ["git", "rev-parse", "--short", "HEAD"],
304 rev_file)
305 rev_file.close()
306
307 binaries = options.fs_binaries if options.fs_binaries else all_binaries.keys()
308 for fs_binary in binaries:
309 all_binaries[fs_binary]()
310
311 print "Done! All the generated files can be found in %s" % binaries_dir
312
313 sys.exit(0)
314