util: Make the googletest library available to the m5 utility.
[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 script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
56 gem5_dir = os.path.dirname(script_dir)
57
58 parser = OptionParser()
59
60 parser.add_option("--gem5-dir", default = gem5_dir,
61 metavar = "GEM5_DIR",
62 help = "gem5 root directory to be used for bootloader and "
63 "VExpress_GEM5_V1 DTB sources. The default value is the gem5 root "
64 "directory of the executed script (%default)")
65 parser.add_option("--dest-dir", default = "/tmp",
66 metavar = "DEST_DIR",
67 help = "Directory to use for checking out the different kernel "
68 "repositories. Generated files will be copied to "
69 "DEST_DIR/binaries (which must not exist). The default "
70 "value is %default")
71 parser.add_option("--make-jobs", type = "int", default = 1,
72 metavar = "MAKE_JOBS",
73 help = "Number of jobs to use with the 'make' commands. Default value: "
74 "%default")
75
76 (options, args) = parser.parse_args()
77
78 if args:
79 print "Unrecognized argument(s) %s." % args
80 sys.exit(1)
81
82 if not os.path.isdir(options.dest_dir):
83 print "Error: %s is not a directory." % options.dest_dir
84 sys.exit(1)
85
86 if not os.path.isdir(options.gem5_dir):
87 print "Error: %s is not a directory." % options.gem5_dir
88 sys.exit(1)
89
90 if machine() != "x86_64":
91 print "Error: This script should run in a x86_64 machine"
92 sys.exit(1)
93
94 binaries_dir = options.dest_dir + "/binaries"
95
96 if os.path.exists(binaries_dir):
97 print "Error: %s already exists." % binaries_dir
98 sys.exit(1)
99
100 revisions_dir = options.dest_dir + "/revisions"
101
102 if os.path.exists(revisions_dir):
103 print "Error: %s already exists." %revisions_dir
104 sys.exit(1)
105
106 # Some basic dependency checking
107 needed_programs = [
108 "make",
109 "aarch64-linux-gnu-gcc",
110 "arm-linux-gnueabihf-gcc",
111 "aarch64-linux-gnu-gcc-4.8",
112 "arm-linux-gnueabihf-gcc-4.8",
113 "gcc",
114 "bc",
115 "dtc",
116 "arm-linux-gnueabi-gcc"
117 ]
118
119 for program in needed_programs:
120 if not spawn.find_executable(program):
121 print "Error: command %s not found in $PATH" % program
122 print ("If running on an Debian-based linux, please try the following "
123 "cmd to get all the necessary packages: ")
124 print ("sudo apt-get install -y make gcc bc gcc-aarch64-linux-gnu "
125 "gcc-4.8-aarch64-linux-gnu gcc-4.8-arm-linux-gnueabihf "
126 "gcc-arm-linux-gnueabihf device-tree-compiler "
127 "gcc-arm-linux-gnueabi")
128 sys.exit(1)
129
130 os.mkdir(binaries_dir);
131 os.mkdir(revisions_dir);
132
133 make_jobs_str = "-j" + str(options.make_jobs)
134
135 rev_file = open(revisions_dir + "/gem5", "w+")
136 run_cmd("write revision of gem5 repo",
137 gem5_dir,
138 ["git", "rev-parse", "--short", "HEAD"],
139 rev_file)
140 rev_file.close()
141
142 # Checkout and build linux kernel for VExpress_GEM5_V1 (arm and arm64)
143 kernel_vexpress_gem5_dir = options.dest_dir + "/linux-kernel-vexpress_gem5"
144 run_cmd("clone linux kernel for VExpress_GEM5_V1 platform",
145 options.dest_dir,
146 ["git", "clone", "https://gem5.googlesource.com/arm/linux",
147 kernel_vexpress_gem5_dir])
148 rev_file = open(revisions_dir + "/linux", "w+")
149 run_cmd("write revision of linux-kernel-vexpress_gem5 repo",
150 kernel_vexpress_gem5_dir,
151 ["git", "rev-parse", "--short", "HEAD"],
152 rev_file)
153 rev_file.close()
154 run_cmd("configure kernel for arm64",
155 kernel_vexpress_gem5_dir,
156 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
157 "gem5_defconfig", make_jobs_str])
158 run_cmd("compile kernel for arm64",
159 kernel_vexpress_gem5_dir,
160 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-", make_jobs_str])
161 run_cmd("copy arm64 vmlinux",
162 kernel_vexpress_gem5_dir,
163 ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_gem5_v1_64"])
164 run_cmd("cleanup arm64 kernel compilation",
165 kernel_vexpress_gem5_dir,
166 ["make", "distclean"])
167 run_cmd("configure kernel for arm",
168 kernel_vexpress_gem5_dir,
169 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
170 "gem5_defconfig"])
171 run_cmd("compile kernel for arm",
172 kernel_vexpress_gem5_dir,
173 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-", make_jobs_str])
174 run_cmd("copy arm vmlinux",
175 kernel_vexpress_gem5_dir,
176 ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_gem5_v1"])
177
178 # Checkout and build linux kernel and DTB for VExpress_EMM64
179 kernel_vexpress_emm64_dir = options.dest_dir + "/linux-kernel-vexpress_emm64"
180 run_cmd("clone linux kernel for VExpress_EMM64 platform",
181 options.dest_dir,
182 ["git", "clone", "https://gem5.googlesource.com/arm/linux-arm64-legacy",
183 kernel_vexpress_emm64_dir])
184 rev_file = open(revisions_dir + "/linux-arm64-legacy", "w+")
185 run_cmd("write revision of linux-kernel-vexpress_emm64 repo",
186 kernel_vexpress_emm64_dir,
187 ["git", "rev-parse", "--short", "HEAD"],
188 rev_file)
189 rev_file.close()
190 run_cmd("configure kernel",
191 kernel_vexpress_emm64_dir,
192 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
193 "CC=aarch64-linux-gnu-gcc-4.8", "gem5_defconfig"])
194 run_cmd("compile kernel",
195 kernel_vexpress_emm64_dir,
196 ["make", "ARCH=arm64", "CROSS_COMPILE=aarch64-linux-gnu-",
197 "CC=aarch64-linux-gnu-gcc-4.8", make_jobs_str])
198 run_cmd("copy vmlinux",
199 kernel_vexpress_emm64_dir,
200 ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm64"])
201 run_cmd("copy DTB",
202 kernel_vexpress_emm64_dir,
203 ["cp", "arch/arm64/boot/dts/aarch64_gem5_server.dtb", binaries_dir])
204
205 # Checkout and build linux kernel and DTBs for VExpress_EMM
206 kernel_vexpress_emm_dir = options.dest_dir + "/linux-kernel-vexpress_emm"
207 run_cmd("clone linux kernel for VExpress_EMM platform",
208 options.dest_dir,
209 ["git", "clone", "https://gem5.googlesource.com/arm/linux-arm-legacy",
210 kernel_vexpress_emm_dir])
211 rev_file = open(revisions_dir + "/linux-arm-legacy", "w+")
212 run_cmd("write revision of linux-kernel-vexpress_emm64 repo",
213 kernel_vexpress_emm_dir,
214 ["git", "rev-parse", "--short", "HEAD"],
215 rev_file)
216 rev_file.close()
217 run_cmd("configure kernel",
218 kernel_vexpress_emm_dir,
219 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
220 "CC=arm-linux-gnueabihf-gcc-4.8", "vexpress_gem5_server_defconfig"])
221 run_cmd("compile kernel",
222 kernel_vexpress_emm_dir,
223 ["make", "ARCH=arm", "CROSS_COMPILE=arm-linux-gnueabihf-",
224 "CC=arm-linux-gnueabihf-gcc-4.8", make_jobs_str])
225 run_cmd("copy vmlinux",
226 kernel_vexpress_emm_dir,
227 ["cp", "vmlinux", binaries_dir + "/vmlinux.vexpress_emm"])
228 run_cmd("rename DTB for 1 CPU",
229 kernel_vexpress_emm_dir,
230 ["cp", "arch/arm/boot/dts/vexpress-v2p-ca15-tc1-gem5.dtb",
231 binaries_dir + "/vexpress-v2p-ca15-tc1-gem5_1cpus.dtb"])
232 run_cmd("copy DTBs",
233 kernel_vexpress_emm_dir,
234 ["cp"] + glob(kernel_vexpress_emm_dir + "/arch/arm/boot/dts/*gem5_*dtb") +
235 [binaries_dir])
236
237 # Build DTBs for VExpress_GEM5_V1
238 dt_dir = gem5_dir + "/system/arm/dt"
239 run_cmd("compile DTBs for VExpress_GEM5_V1 platform",
240 dt_dir,
241 ["make", make_jobs_str])
242 run_cmd("copy DTBs",
243 dt_dir,
244 ["cp"] + glob(dt_dir + "/*dtb") + [binaries_dir])
245
246 # Build bootloaders arm64
247 bootloader_arm64_dir = gem5_dir + "/system/arm/bootloader/arm64"
248 run_cmd("compile arm64 bootloader",
249 bootloader_arm64_dir,
250 ["make"])
251 run_cmd("copy arm64 bootloader",
252 bootloader_arm64_dir,
253 ["cp", "boot.arm64", "boot_emm.arm64", "boot_v2.arm64", binaries_dir])
254
255 # Build bootloaders arm
256 bootloader_arm_dir = gem5_dir + "/system/arm/bootloader/arm"
257 run_cmd("compile arm bootloader",
258 bootloader_arm_dir,
259 ["make"])
260 run_cmd("copy arm bootloaders",
261 bootloader_arm_dir,
262 ["cp", "boot.arm", "boot_emm.arm", binaries_dir])
263
264 # Build m5 binaries
265 m5_dir = gem5_dir + "/util/m5"
266 run_cmd("compile arm64 m5",
267 m5_dir,
268 ["make", "-f", "Makefile.aarch64"])
269 run_cmd("copy arm64 m5",
270 m5_dir,
271 ["cp", "m5", binaries_dir + "/m5.aarch64"])
272 run_cmd("clean arm64 m5",
273 m5_dir,
274 ["make", "clean", "-f", "Makefile.aarch64"])
275 run_cmd("compile arm m5",
276 m5_dir,
277 ["make", "-f", "Makefile.arm"])
278 run_cmd("copy arm m5",
279 m5_dir,
280 ["cp", "m5", binaries_dir + "/m5.aarch32"])
281
282 print "Done! All the generated files can be found in %s" % binaries_dir
283
284 sys.exit(0)
285