]> begriffs open source - cmsis/blob - CMSIS/CoreValidation/Project/build.py
Updates for CMSIS 6
[cmsis] / CMSIS / CoreValidation / Project / build.py
1 #!/usr/bin/python3
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     IAR = ('IAR')
75
76     @property
77     def image_ext(self):
78         ext = {
79             CompilerAxis.AC6: 'axf',
80             CompilerAxis.AC6LTM: 'axf',
81             CompilerAxis.GCC: 'elf',
82             CompilerAxis.IAR: 'elf'
83         }
84         return ext[self]
85
86     @property
87     def toolchain(self):
88         ext = {
89             CompilerAxis.AC6: 'AC6',
90             CompilerAxis.AC6LTM: 'AC6@6.16.2',
91             CompilerAxis.GCC: 'GCC',
92             CompilerAxis.IAR: 'IAR'
93         }
94         return ext[self]
95
96
97 @matrix_axis("optimize", "o", "Optimization level(s) to be considered.")
98 class OptimizationAxis(Enum):
99     NONE = ('none')
100     BALANCED = ('balanced')
101     SPEED = ('speed')
102     SIZE = ('size')
103
104
105 MODEL_EXECUTABLE = {
106     DeviceAxis.CM0: ("VHT_MPS2_Cortex-M0", []),
107     DeviceAxis.CM0plus: ("VHT_MPS2_Cortex-M0plus", []),
108     DeviceAxis.CM3: ("VHT_MPS2_Cortex-M3", []),
109     DeviceAxis.CM4: ("VHT_MPS2_Cortex-M4", []),
110     DeviceAxis.CM4FP: ("VHT_MPS2_Cortex-M4", []),
111     DeviceAxis.CM7: ("VHT_MPS2_Cortex-M7", []),
112     DeviceAxis.CM7DP: ("VHT_MPS2_Cortex-M7", []),
113     DeviceAxis.CM7SP: ("VHT_MPS2_Cortex-M7", []),
114     DeviceAxis.CM23: ("VHT_MPS2_Cortex-M23", []),
115     DeviceAxis.CM23S: ("VHT_MPS2_Cortex-M23", []),
116     DeviceAxis.CM23NS: ("VHT_MPS2_Cortex-M23", []),
117     DeviceAxis.CM33: ("VHT_MPS2_Cortex-M33", []),
118     DeviceAxis.CM33S: ("VHT_MPS2_Cortex-M33", []),
119     DeviceAxis.CM33NS: ("VHT_MPS2_Cortex-M33", []),
120     DeviceAxis.CM35P: ("VHT_MPS2_Cortex-M35P", []),
121     DeviceAxis.CM35PS: ("VHT_MPS2_Cortex-M35P", []),
122     DeviceAxis.CM35PNS: ("VHT_MPS2_Cortex-M35P", []),
123     DeviceAxis.CM55S: ("VHT_MPS2_Cortex-M55", []),
124     DeviceAxis.CM55NS: ("VHT_MPS2_Cortex-M55", []),
125     DeviceAxis.CM85S: ("VHT_MPS2_Cortex-M85", []),
126     DeviceAxis.CM85NS: ("VHT_MPS2_Cortex-M85", []),
127     DeviceAxis.CA5: ("FVP_VE_Cortex-A5x1", []),
128     DeviceAxis.CA7: ("FVP_VE_Cortex-A7x1", []),
129     DeviceAxis.CA9: ("FVP_VE_Cortex-A9x1", []),
130 #    DeviceAxis.CA5NEON: ("FVP_VE_Cortex-A5x1", []),
131 #    DeviceAxis.CA7NEON: ("FVP_VE_Cortex-A7x1", []),
132 #    DeviceAxis.CA9NEON: ("FVP_VE_Cortex-A9x1", [])
133 }
134
135 def config_suffix(config, timestamp=True):
136     suffix = f"{config.compiler[0]}-{config.optimize[0]}-{config.device[1]}"
137     if timestamp:
138         suffix += f"-{datetime.now().strftime('%Y%m%d%H%M%S')}"
139     return suffix
140
141
142 def project_name(config):
143     return f"Validation.{config.compiler}_{config.optimize}+{config.device[1]}"
144
145
146 def bl_project_name(config):
147     return f"Bootloader.{config.compiler}_{config.optimize}+{config.device.bl_device}"
148
149
150 def output_dir(config):
151     return f"Validation/outdir"
152
153
154 def bl_output_dir(config):
155     return f"Bootloader/outdir"
156
157
158 def model_config(config):
159     return f"../layer/target/{config.device[1]}/model_config.txt"
160
161
162 def build_dir(config):
163     return f"build/{config.device[1]}/{config.compiler}/{config.optimize}"
164
165
166 @matrix_action
167 def clean(config):
168     """Build the selected configurations using CMSIS-Build."""
169     yield cbuild_clean(f"{project_name(config)}/{project_name(config)}.cprj")
170
171
172 @matrix_action
173 def build(config, results):
174     """Build the selected configurations using CMSIS-Build."""
175
176     logging.info("Compiling Tests...")
177
178     if config.compiler == CompilerAxis.GCC and config.device.match("CA*"):
179         ldfile = Path(f"{project_name(config)}/RTE/Device/ARM{config.device[1]}/ARM{config.device[1]}.ld")
180         infile = ldfile.replace(ldfile.with_suffix('.ld.in'))
181         yield preprocess(infile, ldfile)
182
183     yield cbuild(config)
184
185     if not all(r.success for r in results):
186         return
187
188     file = f"build/CoreValidation-{config_suffix(config)}.zip"
189     logging.info("Archiving build output to %s...", file)
190     with ZipFile(file, "w") as archive:
191         for content in iglob(f"{build_dir(config)}/**/*", recursive=True):
192             if Path(content).is_file():
193                 archive.write(content)
194
195
196 @matrix_action
197 def extract(config):
198     """Extract the latest build archive."""
199     archives = sorted(glob(f"build/CoreValidation-{config_suffix(config, timestamp=False)}-*.zip"), reverse=True)
200     yield unzip(archives[0])
201
202
203 @matrix_action
204 def run(config, results):
205     """Run the selected configurations."""
206     logging.info("Running Core Validation on Arm model ...")
207     yield model_exec(config)
208
209     try:
210         results[0].test_report.write(f"build/CoreValidation-{config_suffix(config)}.junit")
211     except RuntimeError as ex:
212         if isinstance(ex.__cause__, XMLSyntaxError):
213             logging.error("No valid test report found in model output!")
214         else:
215             logging.exception(ex)
216
217
218 @matrix_command()
219 def cbuild_clean(project):
220     return ["cbuild", "-c", project]
221
222
223 @matrix_command()
224 def unzip(archive):
225     return ["bash", "-c", f"unzip {archive}"]
226
227
228 @matrix_command()
229 def preprocess(infile, outfile):
230     return ["arm-none-eabi-gcc", "-xc", "-E", infile, "-P", "-o", outfile]
231
232
233 @matrix_command()
234 def cbuild(config):
235     return ["cbuild", "--toolchain", config.compiler.toolchain, "--update-rte", \
236              "--configuration", f".{config.optimize}+{config.device[1]}", \
237              "Validation.csolution.yml" ]
238
239
240 @matrix_command(test_report=ConsoleReport() |
241                             CropReport('<\?xml version="1.0"\?>', '</report>') |
242                             TransformReport('validation.xsl') |
243                             JUnitReport(title=lambda title, result: f"{result.command.config.compiler}."
244                                                                     f"{result.command.config.optimize}."
245                                                                     f"{result.command.config.device}."
246                                                                     f"{title}"))
247 def model_exec(config):
248     cmdline = [MODEL_EXECUTABLE[config.device][0], "-q", "--simlimit", 100, "-f", model_config(config)]
249     cmdline += MODEL_EXECUTABLE[config.device][1]
250     cmdline += ["-a", f"{build_dir(config)}/{output_dir(config)}/Validation.{config.compiler.image_ext}"]
251     if config.device.has_bl():
252         cmdline += ["-a", f"{build_dir(config)}/{bl_output_dir(config)}/Bootloader.{config.compiler.image_ext}"]
253     return cmdline
254
255
256 if __name__ == "__main__":
257     main()