blob: fd46580c9ee09402c030376bba3f0f7a8edc7cb6 [file] [log] [blame]
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +01001#!/usr/bin/env python3
2#
3# Copyright (c) 2019, Nordic Semiconductor ASA
4#
5# SPDX-License-Identifier: Apache-2.0
6
7'''Tool for parsing a list of projects to determine if they are Zephyr
8projects. If no projects are given then the output from `west list` will be
9used as project list.
10
11Include file is generated for Kconfig using --kconfig-out.
12A <name>:<path> text file is generated for use with CMake using --cmake-out.
Anas Nashif286a9ed2019-12-11 10:13:23 -050013
Anas Nashif54713982020-12-07 14:52:10 -050014Using --twister-out <filename> an argument file for twister script will
Anas Nashif286a9ed2019-12-11 10:13:23 -050015be generated which would point to test and sample roots available in modules
Anas Nashif54713982020-12-07 14:52:10 -050016that can be included during a twister run. This allows testing code
Anas Nashif286a9ed2019-12-11 10:13:23 -050017maintained in modules in addition to what is available in the main Zephyr tree.
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +010018'''
19
20import argparse
21import os
Torsten Rasmussen3d880832021-01-19 12:01:38 +010022import re
Torsten Rasmussenfffaf052021-10-12 23:08:36 +020023import subprocess
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +010024import sys
25import yaml
26import pykwalify.core
Torsten Rasmussenb3da9ef2019-12-13 09:42:13 +010027from pathlib import Path, PurePath
Torsten Rasmussen3917ee52020-05-18 22:34:49 +020028from collections import namedtuple
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +010029
30METADATA_SCHEMA = '''
31## A pykwalify schema for basic validation of the structure of a
32## metadata YAML file.
33##
34# The zephyr/module.yml file is a simple list of key value pairs to be used by
35# the build system.
36type: map
37mapping:
Torsten Rasmussenf24f8832021-01-07 15:27:53 +010038 name:
39 required: false
40 type: str
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +010041 build:
Anas Nashif286a9ed2019-12-11 10:13:23 -050042 required: false
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +010043 type: map
44 mapping:
45 cmake:
46 required: false
47 type: str
48 kconfig:
49 required: false
50 type: str
Torsten Rasmussen3673e282020-12-17 11:27:42 +010051 cmake-ext:
52 required: false
53 type: bool
54 default: false
55 kconfig-ext:
56 required: false
57 type: bool
58 default: false
Torsten Rasmussen3917ee52020-05-18 22:34:49 +020059 depends:
60 required: false
61 type: seq
62 sequence:
63 - type: str
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +020064 settings:
65 required: false
66 type: map
67 mapping:
68 board_root:
69 required: false
70 type: str
71 dts_root:
72 required: false
73 type: str
74 soc_root:
75 required: false
76 type: str
77 arch_root:
78 required: false
79 type: str
Torsten Rasmussen3673e282020-12-17 11:27:42 +010080 module_ext_root:
81 required: false
82 type: str
Anas Nashif286a9ed2019-12-11 10:13:23 -050083 tests:
84 required: false
85 type: seq
86 sequence:
87 - type: str
88 samples:
89 required: false
90 type: seq
91 sequence:
92 - type: str
93 boards:
94 required: false
95 type: seq
96 sequence:
97 - type: str
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +010098'''
99
100schema = yaml.safe_load(METADATA_SCHEMA)
101
102
103def validate_setting(setting, module_path, filename=None):
104 if setting is not None:
105 if filename is not None:
106 checkfile = os.path.join(module_path, setting, filename)
107 else:
108 checkfile = os.path.join(module_path, setting)
109 if not os.path.isfile(checkfile):
110 return False
111 return True
112
113
Anas Nashif286a9ed2019-12-11 10:13:23 -0500114def process_module(module):
Torsten Rasmussenb3da9ef2019-12-13 09:42:13 +0100115 module_path = PurePath(module)
116 module_yml = module_path.joinpath('zephyr/module.yml')
Anas Nashif286a9ed2019-12-11 10:13:23 -0500117
Sebastian Bøebb95dce2019-12-19 15:49:22 +0100118 # The input is a module if zephyr/module.yml is a valid yaml file
119 # or if both zephyr/CMakeLists.txt and zephyr/Kconfig are present.
120
Torsten Rasmussenb3da9ef2019-12-13 09:42:13 +0100121 if Path(module_yml).is_file():
122 with Path(module_yml).open('r') as f:
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100123 meta = yaml.safe_load(f.read())
124
125 try:
126 pykwalify.core.Core(source_data=meta, schema_data=schema)\
127 .validate()
128 except pykwalify.errors.SchemaError as e:
Ulf Magnusson50b9b122019-09-07 14:41:01 +0200129 sys.exit('ERROR: Malformed "build" section in file: {}\n{}'
Torsten Rasmussenb3da9ef2019-12-13 09:42:13 +0100130 .format(module_yml.as_posix(), e))
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100131
Torsten Rasmussenf24f8832021-01-07 15:27:53 +0100132 meta['name'] = meta.get('name', module_path.name)
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100133 meta['name-sanitized'] = re.sub('[^a-zA-Z0-9]', '_', meta['name'])
Anas Nashif286a9ed2019-12-11 10:13:23 -0500134 return meta
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100135
Sebastian Bøebb95dce2019-12-19 15:49:22 +0100136 if Path(module_path.joinpath('zephyr/CMakeLists.txt')).is_file() and \
137 Path(module_path.joinpath('zephyr/Kconfig')).is_file():
Torsten Rasmussenf24f8832021-01-07 15:27:53 +0100138 return {'name': module_path.name,
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100139 'name-sanitized': re.sub('[^a-zA-Z0-9]', '_', module_path.name),
Torsten Rasmussenf24f8832021-01-07 15:27:53 +0100140 'build': {'cmake': 'zephyr', 'kconfig': 'zephyr/Kconfig'}}
Sebastian Bøebb95dce2019-12-19 15:49:22 +0100141
Anas Nashif286a9ed2019-12-11 10:13:23 -0500142 return None
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100143
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100144
Anas Nashif286a9ed2019-12-11 10:13:23 -0500145def process_cmake(module, meta):
146 section = meta.get('build', dict())
147 module_path = PurePath(module)
148 module_yml = module_path.joinpath('zephyr/module.yml')
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100149
150 cmake_extern = section.get('cmake-ext', False)
151 if cmake_extern:
152 return('\"{}\":\"{}\":\"{}\"\n'
Andrzej Głąbek02819482021-01-19 07:39:47 +0100153 .format(meta['name'],
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100154 module_path.as_posix(),
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100155 "${ZEPHYR_" + meta['name-sanitized'].upper() + "_CMAKE_DIR}"))
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100156
Anas Nashif286a9ed2019-12-11 10:13:23 -0500157 cmake_setting = section.get('cmake', None)
158 if not validate_setting(cmake_setting, module, 'CMakeLists.txt'):
159 sys.exit('ERROR: "cmake" key in {} has folder value "{}" which '
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200160 'does not contain a CMakeLists.txt file.'
161 .format(module_yml.as_posix(), cmake_setting))
Anas Nashif286a9ed2019-12-11 10:13:23 -0500162
163 cmake_path = os.path.join(module, cmake_setting or 'zephyr')
164 cmake_file = os.path.join(cmake_path, 'CMakeLists.txt')
165 if os.path.isfile(cmake_file):
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200166 return('\"{}\":\"{}\":\"{}\"\n'
Torsten Rasmussenf24f8832021-01-07 15:27:53 +0100167 .format(meta['name'],
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200168 module_path.as_posix(),
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200169 Path(cmake_path).resolve().as_posix()))
Anas Nashif286a9ed2019-12-11 10:13:23 -0500170 else:
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200171 return('\"{}\":\"{}\":\"\"\n'
Torsten Rasmussenf24f8832021-01-07 15:27:53 +0100172 .format(meta['name'],
Torsten Rasmussenab7ec172020-08-25 13:32:33 +0200173 module_path.as_posix()))
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200174
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100175
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +0200176def process_settings(module, meta):
177 section = meta.get('build', dict())
178 build_settings = section.get('settings', None)
179 out_text = ""
180
181 if build_settings is not None:
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100182 for root in ['board', 'dts', 'soc', 'arch', 'module_ext']:
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +0200183 setting = build_settings.get(root+'_root', None)
184 if setting is not None:
185 root_path = PurePath(module) / setting
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100186 out_text += f'"{root.upper()}_ROOT":'
187 out_text += f'"{root_path.as_posix()}"\n'
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +0200188
189 return out_text
190
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200191
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100192def kconfig_snippet(meta, path, kconfig_file=None):
193 name = meta['name']
194 name_sanitized = meta['name-sanitized']
195
Trond Einar Snekvik6a8326f2021-12-02 15:44:36 +0100196 snippet = (f'menu "{name} ({path.as_posix()})"',
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100197 f'osource "{kconfig_file.resolve().as_posix()}"' if kconfig_file
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100198 else f'osource "$(ZEPHYR_{name_sanitized.upper()}_KCONFIG)"',
199 f'config ZEPHYR_{name_sanitized.upper()}_MODULE',
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100200 ' bool',
201 ' default y',
202 'endmenu\n')
203 return '\n'.join(snippet)
204
205
Anas Nashif286a9ed2019-12-11 10:13:23 -0500206def process_kconfig(module, meta):
207 section = meta.get('build', dict())
208 module_path = PurePath(module)
209 module_yml = module_path.joinpath('zephyr/module.yml')
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100210 kconfig_extern = section.get('kconfig-ext', False)
211 if kconfig_extern:
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100212 return kconfig_snippet(meta, module_path)
Anas Nashif286a9ed2019-12-11 10:13:23 -0500213
214 kconfig_setting = section.get('kconfig', None)
215 if not validate_setting(kconfig_setting, module):
216 sys.exit('ERROR: "kconfig" key in {} has value "{}" which does '
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200217 'not point to a valid Kconfig file.'
218 .format(module_yml, kconfig_setting))
Anas Nashif286a9ed2019-12-11 10:13:23 -0500219
220 kconfig_file = os.path.join(module, kconfig_setting or 'zephyr/Kconfig')
221 if os.path.isfile(kconfig_file):
Torsten Rasmussen3d880832021-01-19 12:01:38 +0100222 return kconfig_snippet(meta, module_path, Path(kconfig_file))
Anas Nashif286a9ed2019-12-11 10:13:23 -0500223 else:
224 return ""
225
Torsten Rasmussen3673e282020-12-17 11:27:42 +0100226
Anas Nashif54713982020-12-07 14:52:10 -0500227def process_twister(module, meta):
Anas Nashif286a9ed2019-12-11 10:13:23 -0500228
229 out = ""
230 tests = meta.get('tests', [])
231 samples = meta.get('samples', [])
232 boards = meta.get('boards', [])
233
234 for pth in tests + samples:
235 if pth:
236 dir = os.path.join(module, pth)
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200237 out += '-T\n{}\n'.format(PurePath(os.path.abspath(dir))
238 .as_posix())
Anas Nashif286a9ed2019-12-11 10:13:23 -0500239
240 for pth in boards:
241 if pth:
242 dir = os.path.join(module, pth)
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200243 out += '--board-root\n{}\n'.format(PurePath(os.path.abspath(dir))
244 .as_posix())
Anas Nashif286a9ed2019-12-11 10:13:23 -0500245
246 return out
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100247
248
Torsten Rasmussenfffaf052021-10-12 23:08:36 +0200249def process_meta(zephyr_base, west_projects, modules):
250 # Process zephyr_base, projects, and modules and create a dictionary
251 # with meta information for each input.
252 #
253 # The dictionary will contain meta info in the following lists:
254 # - zephyr: path and revision
255 # - modules: name, path, and revision
256 # - west-projects: path and revision
257 #
258 # returns the dictionary with said lists
259
260 meta = {'zephyr': None, 'modules': None, 'west': None}
261
262 def git_revision(path):
263 rc = subprocess.Popen(['git', 'rev-parse', '--is-inside-work-tree'],
264 stdout=subprocess.PIPE,
265 stderr=subprocess.PIPE,
266 cwd=path).wait()
267 if rc == 0:
268 # A git repo.
269 popen = subprocess.Popen(['git', 'rev-parse', 'HEAD'],
270 stdout=subprocess.PIPE,
271 stderr=subprocess.PIPE,
272 cwd=path)
273 stdout, stderr = popen.communicate()
274 stdout = stdout.decode('utf-8')
275
276 if not (popen.returncode or stderr):
277 revision = stdout.rstrip()
278
279 rc = subprocess.Popen(['git', 'diff-index', '--quiet', 'HEAD',
280 '--'],
281 stdout=None,
282 stderr=None,
283 cwd=path).wait()
284 if rc:
285 return revision + '-dirty'
286 return revision
287 return None
288
289 meta_project = {'path': zephyr_base,
290 'revision': git_revision(zephyr_base)}
291 meta['zephyr'] = meta_project
292
293 if west_projects is not None:
294 meta_projects = []
295 for project in west_projects['projects']:
296 project_path = PurePath(project).as_posix()
297 meta_project = {'path': project_path,
298 'revision': git_revision(project_path)}
299 meta_projects.append(meta_project)
300 meta['west'] = {'manifest': west_projects['manifest'],
301 'projects': meta_projects}
302
303 meta_projects = []
304 for module in modules:
305 module_path = PurePath(module.project).as_posix()
306 meta_project = {'name': module.meta['name'],
307 'path': module_path,
308 'revision': git_revision(module_path)}
309 meta_projects.append(meta_project)
310 meta['modules'] = meta_projects
311
312 return meta
313
314
315def west_projects():
316 manifest_file = None
317 projects = []
318 # West is imported here, as it is optional
319 # (and thus maybe not installed)
320 # if user is providing a specific modules list.
321 from west.manifest import Manifest
322 from west.util import WestNotFound
323 from west.version import __version__ as WestVersion
324 from packaging import version
325 try:
326 manifest = Manifest.from_file()
327 if version.parse(WestVersion) >= version.parse('0.9.0'):
328 projects = [p.posixpath for p in manifest.get_projects([])
329 if manifest.is_active(p)]
330 else:
331 projects = [p.posixpath for p in manifest.get_projects([])]
332 manifest_file = manifest.path
333 return {'manifest': manifest_file, 'projects': projects}
334 except WestNotFound:
335 # Only accept WestNotFound, meaning we are not in a west
336 # workspace. Such setup is allowed, as west may be installed
337 # but the project is not required to use west.
338 pass
339 return None
340
341
Marcin Niestroj248d25d2021-05-08 16:56:34 +0200342def parse_modules(zephyr_base, modules=None, extra_modules=None):
343 if modules is None:
Torsten Rasmussenfffaf052021-10-12 23:08:36 +0200344 modules = []
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100345
Marcin Niestroj248d25d2021-05-08 16:56:34 +0200346 if extra_modules is None:
347 extra_modules = []
Anas Nashif286a9ed2019-12-11 10:13:23 -0500348
Torsten Rasmussen3917ee52020-05-18 22:34:49 +0200349 Module = namedtuple('Module', ['project', 'meta', 'depends'])
350 # dep_modules is a list of all modules that has an unresolved dependency
351 dep_modules = []
352 # start_modules is a list modules with no depends left (no incoming edge)
353 start_modules = []
354 # sorted_modules is a topological sorted list of the modules
355 sorted_modules = []
356
Torsten Rasmussenfffaf052021-10-12 23:08:36 +0200357 for project in modules + extra_modules:
Anas Nashif286a9ed2019-12-11 10:13:23 -0500358 # Avoid including Zephyr base project as module.
Marcin Niestroj248d25d2021-05-08 16:56:34 +0200359 if project == zephyr_base:
Anas Nashif286a9ed2019-12-11 10:13:23 -0500360 continue
361
362 meta = process_module(project)
363 if meta:
Torsten Rasmussen3917ee52020-05-18 22:34:49 +0200364 section = meta.get('build', dict())
365 deps = section.get('depends', [])
366 if not deps:
367 start_modules.append(Module(project, meta, []))
368 else:
369 dep_modules.append(Module(project, meta, deps))
Martí Bolívar34346c42020-01-29 12:38:58 -0800370 elif project in extra_modules:
371 sys.exit(f'{project}, given in ZEPHYR_EXTRA_MODULES, '
372 'is not a valid zephyr module')
Anas Nashif286a9ed2019-12-11 10:13:23 -0500373
Torsten Rasmussen3917ee52020-05-18 22:34:49 +0200374 # This will do a topological sort to ensure the modules are ordered
375 # according to dependency settings.
376 while start_modules:
377 node = start_modules.pop(0)
378 sorted_modules.append(node)
Torsten Rasmussenf24f8832021-01-07 15:27:53 +0100379 node_name = node.meta['name']
Torsten Rasmussen3917ee52020-05-18 22:34:49 +0200380 to_remove = []
381 for module in dep_modules:
382 if node_name in module.depends:
383 module.depends.remove(node_name)
384 if not module.depends:
385 start_modules.append(module)
386 to_remove.append(module)
387 for module in to_remove:
388 dep_modules.remove(module)
389
390 if dep_modules:
391 # If there are any modules with unresolved dependencies, then the
392 # modules contains unmet or cyclic dependencies. Error out.
393 error = 'Unmet or cyclic dependencies in modules:\n'
394 for module in dep_modules:
395 error += f'{module.project} depends on: {module.depends}\n'
396 sys.exit(error)
397
Marcin Niestroj248d25d2021-05-08 16:56:34 +0200398 return sorted_modules
399
400
401def main():
402 parser = argparse.ArgumentParser(description='''
403 Process a list of projects and create Kconfig / CMake include files for
404 projects which are also a Zephyr module''')
405
406 parser.add_argument('--kconfig-out',
407 help="""File to write with resulting KConfig import
408 statements.""")
409 parser.add_argument('--twister-out',
410 help="""File to write with resulting twister
411 parameters.""")
412 parser.add_argument('--cmake-out',
413 help="""File to write with resulting <name>:<path>
414 values to use for including in CMake""")
Torsten Rasmussenfffaf052021-10-12 23:08:36 +0200415 parser.add_argument('--meta-out',
416 help="""Write a build meta YaML file containing a list
417 of Zephyr modules and west projects.
418 If a module or project is also a git repository
419 the current SHA revision will also be written.""")
Marcin Niestroj248d25d2021-05-08 16:56:34 +0200420 parser.add_argument('--settings-out',
421 help="""File to write with resulting <name>:<value>
422 values to use for including in CMake""")
423 parser.add_argument('-m', '--modules', nargs='+',
424 help="""List of modules to parse instead of using `west
425 list`""")
426 parser.add_argument('-x', '--extra-modules', nargs='+',
427 help='List of extra modules to parse')
428 parser.add_argument('-z', '--zephyr-base',
429 help='Path to zephyr repository')
430 args = parser.parse_args()
431
432 kconfig = ""
433 cmake = ""
434 settings = ""
435 twister = ""
436
Torsten Rasmussenfffaf052021-10-12 23:08:36 +0200437 west_proj = None
438 if args.modules is None:
439 west_proj = west_projects()
440 modules = parse_modules(args.zephyr_base,
441 west_proj['projects'] if west_proj else None,
442 args.extra_modules)
443 else:
444 modules = parse_modules(args.zephyr_base, args.modules,
445 args.extra_modules)
Marcin Niestroj248d25d2021-05-08 16:56:34 +0200446
447 for module in modules:
Torsten Rasmussen3917ee52020-05-18 22:34:49 +0200448 kconfig += process_kconfig(module.project, module.meta)
449 cmake += process_cmake(module.project, module.meta)
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +0200450 settings += process_settings(module.project, module.meta)
Anas Nashif54713982020-12-07 14:52:10 -0500451 twister += process_twister(module.project, module.meta)
Torsten Rasmussen3917ee52020-05-18 22:34:49 +0200452
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100453 if args.kconfig_out:
Anas Nashif286a9ed2019-12-11 10:13:23 -0500454 with open(args.kconfig_out, 'w', encoding="utf-8") as fp:
455 fp.write(kconfig)
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100456
457 if args.cmake_out:
Anas Nashif286a9ed2019-12-11 10:13:23 -0500458 with open(args.cmake_out, 'w', encoding="utf-8") as fp:
459 fp.write(cmake)
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100460
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +0200461 if args.settings_out:
462 with open(args.settings_out, 'w', encoding="utf-8") as fp:
Martí Bolívar361956b2021-08-09 10:46:17 -0700463 fp.write('''\
464# WARNING. THIS FILE IS AUTO-GENERATED. DO NOT MODIFY!
465#
466# This file contains build system settings derived from your modules.
467#
468# Modules may be set via ZEPHYR_MODULES, ZEPHYR_EXTRA_MODULES,
469# and/or the west manifest file.
470#
471# See the Modules guide for more information.
472''')
Torsten Rasmussen25d57ba2020-07-07 17:29:56 +0200473 fp.write(settings)
474
Anas Nashif54713982020-12-07 14:52:10 -0500475 if args.twister_out:
476 with open(args.twister_out, 'w', encoding="utf-8") as fp:
477 fp.write(twister)
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100478
Torsten Rasmussenfffaf052021-10-12 23:08:36 +0200479 if args.meta_out:
480 meta = process_meta(args.zephyr_base, west_proj, modules)
481 with open(args.meta_out, 'w', encoding="utf-8") as fp:
482 fp.write(yaml.dump(meta))
483
Torsten Rasmussen25c3f8e2020-08-25 14:02:04 +0200484
Torsten Rasmussenbd7569f2019-03-19 10:38:18 +0100485if __name__ == "__main__":
486 main()