]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Project/build.py
Migrate CMSIS-CoreValidation to CMSIS-Toolbox build system
[cmsis] / CMSIS / CoreValidation / Project / build.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import logging
5
6 from datetime import datetime
7 from enum import Enum
8 from glob import glob, iglob
9 from pathlib import Path
10
11 from lxml.etree import XMLSyntaxError
12 from zipfile import ZipFile
13
14 from matrix_runner import main, matrix_axis, matrix_action, matrix_command, matrix_filter, \
15     ConsoleReport, CropReport, TransformReport, JUnitReport
16
17
18 @matrix_axis("device", "d", "Device(s) to be considered.")
19 class DeviceAxis(Enum):
20     CM0 = ('Cortex-M0', 'CM0')
21     CM0plus = ('Cortex-M0plus', 'CM0plus')
22     CM3 = ('Cortex-M3', 'CM3')
23     CM4 = ('Cortex-M4', 'CM4')
24     CM4FP = ('Cortex-M4FP', 'CM4FP')
25     CM7 = ('Cortex-M7', 'CM7')
26     CM7SP = ('Cortex-M7SP', 'CM7SP')
27     CM7DP = ('Cortex-M7DP', 'CM7DP')
28     CM23 = ('Cortex-M23', 'CM23')
29     CM23S = ('Cortex-M23S', 'CM23S')
30     CM23NS = ('Cortex-M23NS', 'CM23NS')
31     CM33 = ('Cortex-M33', 'CM33')
32     CM33S = ('Cortex-M33S', 'CM33S')
33     CM33NS = ('Cortex-M33NS', 'CM33NS')
34     CM35P = ('Cortex-M35P', 'CM35P')
35     CM35PS = ('Cortex-M35PS', 'CM35PS')
36     CM35PNS = ('Cortex-M35PNS', 'CM35PNS')
37     CM55S = ('Cortex-M55S', 'CM55S')
38     CM55NS = ('Cortex-M55NS', 'CM55NS')
39     CM85S = ('Cortex-M85S', 'CM85S')
40     CM85NS = ('Cortex-M85NS', 'CM85NS')
41     CA5 = ('Cortex-A5', 'CA5')
42     CA7 = ('Cortex-A7', 'CA7')
43     CA9 = ('Cortex-A9', 'CA9')
44 #    CA5NEON = ('Cortex-A5neon', 'CA5neon')
45 #    CA7NEON = ('Cortex-A7neon', 'CA7neon')
46 #    CA9NEON = ('Cortex-A9neon', 'CA9neon')
47
48     def has_bl(self):
49         return self in [
50             DeviceAxis.CM23NS,
51             DeviceAxis.CM33NS,
52             DeviceAxis.CM35PNS,
53             DeviceAxis.CM55NS,
54             DeviceAxis.CM85NS
55         ]
56
57     @property
58     def bl_device(self):
59         bld = {
60             DeviceAxis.CM23NS: 'CM23S',
61             DeviceAxis.CM33NS: 'CM33S',
62             DeviceAxis.CM35PNS: 'CM35PS',
63             DeviceAxis.CM55NS: 'CM55S',
64             DeviceAxis.CM85NS: 'CM85S'
65         }
66         return bld[self]
67
68
69 @matrix_axis("compiler", "c", "Compiler(s) to be considered.")
70 class CompilerAxis(Enum):
71     AC6 = ('AC6')
72     AC6LTM = ('AC6LTM')
73     GCC = ('GCC')
74
75     @property
76     def image_ext(self):
77         ext = {
78             CompilerAxis.AC6: 'axf',
79             CompilerAxis.AC6LTM: 'axf',
80             CompilerAxis.GCC: 'elf'
81         }
82         return ext[self]
83
84
85 @matrix_axis("optimize", "o", "Optimization level(s) to be considered.")
86 class OptimizationAxis(Enum):
87     LOW = ('low', 'O1')
88     MID = ('mid', 'O2')
89     HIGH = ('high', 'Ofast')
90     SIZE = ('size', 'Os')
91     TINY = ('tiny', 'Oz')
92
93
94 MODEL_EXECUTABLE = {
95     DeviceAxis.CM0: ("VHT_MPS2_Cortex-M0", []),
96     DeviceAxis.CM0plus: ("VHT_MPS2_Cortex-M0plus", []),
97     DeviceAxis.CM3: ("VHT_MPS2_Cortex-M3", []),
98     DeviceAxis.CM4: ("VHT_MPS2_Cortex-M4", []),
99     DeviceAxis.CM4FP: ("VHT_MPS2_Cortex-M4", []),
100     DeviceAxis.CM7: ("VHT_MPS2_Cortex-M7", []),
101     DeviceAxis.CM7DP: ("VHT_MPS2_Cortex-M7", []),
102     DeviceAxis.CM7SP: ("VHT_MPS2_Cortex-M7", []),
103     DeviceAxis.CM23: ("VHT_MPS2_Cortex-M23", []),
104     DeviceAxis.CM23S: ("VHT_MPS2_Cortex-M23", []),
105     DeviceAxis.CM23NS: ("VHT_MPS2_Cortex-M23", []),
106     DeviceAxis.CM33: ("VHT_MPS2_Cortex-M33", []),
107     DeviceAxis.CM33S: ("VHT_MPS2_Cortex-M33", []),
108     DeviceAxis.CM33NS: ("VHT_MPS2_Cortex-M33", []),
109     DeviceAxis.CM35P: ("VHT_MPS2_Cortex-M35P", []),
110     DeviceAxis.CM35PS: ("VHT_MPS2_Cortex-M35P", []),
111     DeviceAxis.CM35PNS: ("VHT_MPS2_Cortex-M35P", []),
112     DeviceAxis.CM55S: ("VHT_MPS2_Cortex-M55", []),
113     DeviceAxis.CM55NS: ("VHT_MPS2_Cortex-M55", []),
114     DeviceAxis.CM85S: ("VHT_MPS2_Cortex-M85", []),
115     DeviceAxis.CM85NS: ("VHT_MPS2_Cortex-M85", []),
116     DeviceAxis.CA5: ("FVP_VE_Cortex-A5x1", []),
117     DeviceAxis.CA7: ("FVP_VE_Cortex-A7x1", []),
118     DeviceAxis.CA9: ("FVP_VE_Cortex-A9x1", []),
119 #    DeviceAxis.CA5NEON: ("FVP_VE_Cortex-A5x1", []),
120 #    DeviceAxis.CA7NEON: ("FVP_VE_Cortex-A7x1", []),
121 #    DeviceAxis.CA9NEON: ("FVP_VE_Cortex-A9x1", [])
122 }
123
124 def config_suffix(config, timestamp=True):
125     suffix = f"{config.compiler[0]}-{config.optimize[0]}-{config.device[1]}"
126     if timestamp:
127         suffix += f"-{datetime.now().strftime('%Y%m%d%H%M%S')}"
128     return suffix
129
130
131 def project_name(config):
132     return f"Validation.{config.compiler}_{config.optimize}+{config.device[1]}"
133
134
135 def bl_project_name(config):
136     return f"Bootloader.{config.compiler}_{config.optimize}+{config.device.bl_device}"
137
138
139 def output_dir(config):
140     return f"{project_name(config)}_outdir"
141
142
143 def bl_output_dir(config):
144     return f"{bl_project_name(config)}_outdir"
145
146
147 def model_config(config):
148     return f"../Layer/Target/{config.device[1]}/model_config.txt"
149
150
151 @matrix_action
152 def clean(config):
153     """Build the selected configurations using CMSIS-Build."""
154     yield cbuild_clean(f"{project_name(config)}/{project_name(config)}.cprj")
155
156
157 @matrix_action
158 def build(config, results):
159     """Build the selected configurations using CMSIS-Build."""
160
161     if config.device.has_bl():
162         logging.info("Compiling Bootloader...")
163         yield csolution(f"{bl_project_name(config)}")
164         yield cbuild(f"{bl_project_name(config)}/{bl_project_name(config)}.cprj")
165
166     logging.info("Compiling Tests...")
167
168     if config.compiler == CompilerAxis.GCC and  config.device.match("CA*"):
169         ldfile = Path(f"{project_name(config)}/RTE/Device/ARM{config.device[1]}/ARM{config.device[1]}.ld")
170         infile = ldfile.replace(ldfile.with_suffix('.ld.in'))
171         yield preprocess(infile, ldfile)
172
173     yield csolution(f"{project_name(config)}")
174     yield cbuild(f"{project_name(config)}/{project_name(config)}.cprj")
175
176     if not all(r.success for r in results):
177         return
178
179     file = f"Core_Validation-{config_suffix(config)}.zip"
180     logging.info(f"Archiving build output to {file}...")
181     with ZipFile(file, "w") as archive:
182         for content in iglob(f"{project_name(config)}/**/*", recursive=True):
183             if Path(content).is_file():
184                 archive.write(content)
185
186
187 @matrix_action
188 def extract(config):
189     """Extract the latest build archive."""
190     archives = sorted(glob(f"RTOS2_Validation-{config_suffix(config, timestamp=False)}-*.zip"), reverse=True)
191     yield unzip(archives[0])
192
193
194 @matrix_action
195 def run(config, results):
196     """Run the selected configurations."""
197     logging.info("Running Core Validation on Arm model ...")
198     yield model_exec(config)
199
200     try:
201         results[0].test_report.write(f"Core_Validation-{config_suffix(config)}.junit")
202     except RuntimeError as e:
203         if isinstance(e.__cause__, XMLSyntaxError):
204             logging.error("No valid test report found in model output!")
205         else:
206             logging.exception(e)
207
208
209 @matrix_command()
210 def cbuild_clean(project):
211     return ["cbuild", "-c", project]
212
213
214 @matrix_command()
215 def unzip(archive):
216     return ["bash", "-c", f"unzip {archive}"]
217
218
219 @matrix_command()
220 def preprocess(infile, outfile):
221     return ["arm-none-eabi-gcc", "-xc", "-E", infile, "-P", "-o", outfile]
222
223 @matrix_command()
224 def csolution(project):
225     return ["csolution", "convert", "-s", "Validation.csolution.yml", "-c", project]
226
227 @matrix_command()
228 def cbuild(project):
229     return ["cbuild", project]
230
231
232 @matrix_command(test_report=ConsoleReport() |
233                             CropReport('<\?xml version="1.0"\?>', '</report>') |
234                             TransformReport('validation.xsl') |
235                             JUnitReport(title=lambda title, result: f"{result.command.config.compiler}."
236                                                                     f"{result.command.config.optimize}."
237                                                                     f"{result.command.config.device}."
238                                                                     f"{title}"))
239 def model_exec(config):
240     cmdline = [MODEL_EXECUTABLE[config.device][0], "-q", "--simlimit", 100, "-f", model_config(config)]
241     cmdline += MODEL_EXECUTABLE[config.device][1]
242     cmdline += ["-a", f"{project_name(config)}/{output_dir(config)}/{project_name(config)}.{config.compiler.image_ext}"]
243     if config.device.has_bl():
244         cmdline += ["-a", f"{bl_project_name(config)}/{bl_output_dir(config)}/{bl_project_name(config)}.{config.compiler.image_ext}"]
245     return cmdline
246
247
248 if __name__ == "__main__":
249     main()