blob: c99e427baa046ec64b3a464d85f7633748fb9fc3 [file] [log] [blame]
Matthew Hartfb6fd362020-03-04 21:03:59 +00001#!/usr/bin/env python3
2
3from __future__ import print_function
4
5__copyright__ = """
6/*
7 * Copyright (c) 2020, Arm Limited. All rights reserved.
8 *
9 * SPDX-License-Identifier: BSD-3-Clause
10 *
11 */
12 """
13
14"""
15Script for create LAVA definitions from a single tf-m-build-config
16jenkins Job.
17"""
18
19import os
20import sys
Matthew Hartfb6fd362020-03-04 21:03:59 +000021import argparse
Matthew Hartfb6fd362020-03-04 21:03:59 +000022from jinja2 import Environment, FileSystemLoader
23from lava_helper_configs import *
24
25try:
26 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
27except ImportError:
28 dir_path = os.path.dirname(os.path.realpath(__file__))
29 sys.path.append(os.path.join(dir_path, "../"))
30 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
31
32
33def load_config_overrides(user_args, config_key):
34 """ Load a configuration from multiple locations and override it with
35 user provided arguements """
36
37 print("Using built-in config: %s" % config_key)
38 try:
39 config = lava_gen_config_map[config_key]
40 except KeyError:
41 print("No template found for config: %s" % config_key)
42 sys.exit(1)
43
44 config["build_no"] = user_args.build_no
Dean Birch5d2dc572020-05-29 13:15:59 +010045 config["artifact_store_url"] = user_args.jenkins_build_url
Matthew Hartfb6fd362020-03-04 21:03:59 +000046
47 # Add the template folder
48 config["templ"] = os.path.join(user_args.template_dir, config["templ"])
49 return config
50
51
52def get_artifact_url(artifact_store_url, params, filename):
53 platform = params['platform']
54 if params['device_type'] == 'fvp':
55 platform = 'fvp'
56 return "{}/artifact/trusted-firmware-m/build/install/outputs/{}/{}".format(
57 artifact_store_url.rstrip('/'), platform, filename,
58 )
59
60
61def get_recovery_url(recovery_store_url, recovery):
Dean Birch5d2dc572020-05-29 13:15:59 +010062 return "{}/{}".format(recovery_store_url.rstrip('/'), recovery)
Matthew Hartfb6fd362020-03-04 21:03:59 +000063
64
65def get_job_name(name, params, job):
66 return "{}_{}_{}_{}_{}_{}_{}_{}".format(
67 name,
68 job,
69 params["platform"],
70 params["build_no"],
71 params["compiler"],
72 params["build_type"],
73 params["boot_type"],
74 params["name"],
75 )
76
77
78def get_build_name(params):
79 return "{}_{}_{}_{}_{}".format(
80 params["platform"],
81 params["compiler"],
82 params["name"],
83 params["build_type"],
84 params["boot_type"],
85 )
86
87
88def generate_test_definitions(config, work_dir, user_args):
89 """ Get a dictionary configuration, and an existing jinja2 template
90 and generate a LAVA compatbile yaml definition """
91
92 template_loader = FileSystemLoader(searchpath=work_dir)
93 template_env = Environment(loader=template_loader)
Dean Birch5d2dc572020-05-29 13:15:59 +010094 recovery_store_url = config.get('recovery_store_url', '')
Matthew Hartfb6fd362020-03-04 21:03:59 +000095 build_no = user_args.build_no
Dean Birch5d2dc572020-05-29 13:15:59 +010096 artifact_store_url = config["artifact_store_url"]
Matthew Hartfb6fd362020-03-04 21:03:59 +000097 template_file = config.pop("templ")
98
99 definitions = {}
100
101 for platform, recovery in config["platforms"].items():
102 if platform != user_args.platform:
103 continue
104 recovery_image_url = get_recovery_url(recovery_store_url, recovery)
105 for compiler in config["compilers"]:
106 if compiler != user_args.compiler:
107 continue
108 for build_type in config["build_types"]:
109 if build_type != user_args.build_type:
110 continue
111 for boot_type in config["boot_types"]:
112 bl2_string = "BL2" if user_args.bl2 else "NOBL2"
113 if boot_type != bl2_string:
114 continue
115 for test_name, test_dict in config["tests"].items():
116 if "Config{}".format(test_name) != user_args.proj_config:
117 continue
118 params = {
119 "device_type": config["device_type"],
120 "job_timeout": config["job_timeout"],
121 "action_timeout": config["action_timeout"],
122 "monitor_timeout": config["monitor_timeout"],
123 "poweroff_timeout": config["poweroff_timeout"],
124 "compiler": compiler,
125 "build_type": build_type,
126 "build_no": build_no,
127 "boot_type": boot_type,
128 "name": test_name,
129 "test": test_dict,
130 "platform": platform,
131 "recovery_image_url": recovery_image_url,
132 "data_bin_offset": config.get('data_bin_offset', ''),
133 "docker_prefix": vars(user_args).get('docker_prefix', ''),
134 "license_variable": vars(user_args).get('license_variable', ''),
135 "build_job_url": artifact_store_url,
Matthew Hart2c2688f2020-05-26 13:09:20 +0100136 "cpu0_baseline": config.get("cpu0_baseline", 0),
137 "cpu0_initvtor_s": config.get("cpu0_initvtor_s", "0x10000000")
Matthew Hartfb6fd362020-03-04 21:03:59 +0000138 }
139 params.update(
140 {
141 "firmware_url": get_artifact_url(
142 artifact_store_url,
143 params,
144 test_dict["binaries"]["firmware"],
145 ),
146 "bootloader_url": get_artifact_url(
147 artifact_store_url,
148 params,
149 test_dict["binaries"]["bootloader"],
150 ),
151 }
152 )
153 params.update(
154 {
155 "job_name": get_job_name(
156 config["job_name"], params, user_args.jenkins_job,
157 ),
158 "build_name": get_build_name(params)
159 }
160 )
161
162 definition = template_env.get_template(template_file).render(
163 params
164 )
165 definitions.update({params["job_name"]: definition})
166 return definitions
167
168
169def generate_lava_job_defs(user_args, config):
170 """ Create a LAVA test job definition file """
171
172 # Evaluate current directory
173 work_dir = os.path.abspath(os.path.dirname(__file__))
174
175 # If a single platform is requested and it exists in the platform
176 if user_args.platform and user_args.platform in config["platforms"]:
177 # Only test this platform
178 platform = user_args.platform
179 config["platforms"] = {platform: config["platforms"][platform]}
Matthew Hartfb6fd362020-03-04 21:03:59 +0000180 # Generate the ouptut definition
181 definitions = generate_test_definitions(config, work_dir, user_args)
182
183 # Write it into a file
184 out_dir = os.path.abspath(user_args.lava_def_output)
185 os.makedirs(out_dir, exist_ok=True)
186 for name, definition in definitions.items():
187 out_file = os.path.join(out_dir, "{}{}".format(name, ".yaml"))
188 with open(out_file, "w") as F:
189 F.write(definition)
190 print("Definition created at %s" % out_file)
191
192
193def main(user_args):
194 user_args.template_dir = "jinja2_templates"
195 config_keys = lava_gen_config_map.keys()
196 if user_args.config_key:
197 config_keys = [user_args.config_key]
198 for config_key in config_keys:
199 config = load_config_overrides(user_args, config_key)
200 generate_lava_job_defs(user_args, config)
201
202
203def get_cmd_args():
204 """ Parse command line arguments """
205
206 # Parse command line arguments to override config
207 parser = argparse.ArgumentParser(description="Lava Create Jobs")
208 cmdargs = parser.add_argument_group("Create LAVA Jobs")
209
210 # Configuration control
211 cmdargs.add_argument(
212 "--config-name",
213 dest="config_key",
214 action="store",
215 help="Select built-in configuration by name",
216 )
217 cmdargs.add_argument(
218 "--build-number",
219 dest="build_no",
220 action="store",
221 default="lastSuccessfulBuild",
222 help="JENKINS Build number selector. " "Default: lastSuccessfulBuild",
223 )
224 cmdargs.add_argument(
225 "--output-dir",
226 dest="lava_def_output",
227 action="store",
228 default="job_results",
229 help="Set LAVA compatible .yaml output file",
230 )
231 cmdargs.add_argument(
232 "--platform",
233 dest="platform",
234 action="store",
235 help="Override platform.Only the provided one " "will be tested",
236 )
237 cmdargs.add_argument(
238 "--compiler",
239 dest="compiler",
240 action="store",
241 help="Compiler to build definitions for",
242 )
243 cmdargs.add_argument(
244 "--jenkins-build-url",
245 dest="jenkins_build_url",
246 action="store",
247 help="Set the Jenkins URL",
248 )
249 cmdargs.add_argument(
250 "--jenkins-job",
251 dest="jenkins_job",
252 action="store",
253 default="tf-m-build-config",
254 help="Set the jenkins job name",
255 )
256 cmdargs.add_argument(
257 "--proj-config", dest="proj_config", action="store", help="Proj config"
258 )
259 cmdargs.add_argument(
260 "--build-type", dest="build_type", action="store", help="Build type"
261 )
262 cmdargs.add_argument(
263 "--docker-prefix", dest="docker_prefix", action="store", help="Prefix string for the FVP docker registry location"
264 )
265 cmdargs.add_argument(
266 "--license-variable", dest="license_variable", action="store", help="License string for Fastmodels"
267 )
268 cmdargs.add_argument("--bl2", dest="bl2", action="store_true", help="BL2")
Matthew Hart2c2688f2020-05-26 13:09:20 +0100269 cmdargs.add_argument(
270 "--psa-api-suite", dest="psa_suite", action="store", help="PSA API Suite name"
271 )
Matthew Hartfb6fd362020-03-04 21:03:59 +0000272 return parser.parse_args()
273
274
275if __name__ == "__main__":
276 main(get_cmd_args())