|
| 1 | +#!/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 |
| 8 | +projects. If no projects are given then the output from `west list` will be |
| 9 | +used as project list. |
| 10 | +
|
| 11 | +Include file is generated for Kconfig using --kconfig-out. |
| 12 | +A <name>:<path> text file is generated for use with CMake using --cmake-out. |
| 13 | +''' |
| 14 | + |
| 15 | +import argparse |
| 16 | +import os |
| 17 | +import sys |
| 18 | +import yaml |
| 19 | +import pykwalify.core |
| 20 | +import subprocess |
| 21 | +import re |
| 22 | + |
| 23 | + |
| 24 | +METADATA_SCHEMA = ''' |
| 25 | +## A pykwalify schema for basic validation of the structure of a |
| 26 | +## metadata YAML file. |
| 27 | +## |
| 28 | +# The zephyr/module.yml file is a simple list of key value pairs to be used by |
| 29 | +# the build system. |
| 30 | +type: map |
| 31 | +mapping: |
| 32 | + build: |
| 33 | + required: true |
| 34 | + type: map |
| 35 | + mapping: |
| 36 | + cmake: |
| 37 | + required: false |
| 38 | + type: str |
| 39 | + kconfig: |
| 40 | + required: false |
| 41 | + type: str |
| 42 | +''' |
| 43 | + |
| 44 | +schema = yaml.safe_load(METADATA_SCHEMA) |
| 45 | + |
| 46 | + |
| 47 | +def validate_setting(setting, module_path, filename=None): |
| 48 | + if setting is not None: |
| 49 | + if filename is not None: |
| 50 | + checkfile = os.path.join(module_path, setting, filename) |
| 51 | + else: |
| 52 | + checkfile = os.path.join(module_path, setting) |
| 53 | + if not os.path.isfile(checkfile): |
| 54 | + return False |
| 55 | + return True |
| 56 | + |
| 57 | + |
| 58 | +def process_module(module, cmake_out=None, kconfig_out=None): |
| 59 | + cmake_setting = None |
| 60 | + kconfig_setting = None |
| 61 | + |
| 62 | + module_yml = os.path.join(module, 'zephyr/module.yml') |
| 63 | + if os.path.isfile(module_yml): |
| 64 | + with open(module_yml, 'r') as f: |
| 65 | + meta = yaml.safe_load(f.read()) |
| 66 | + |
| 67 | + try: |
| 68 | + pykwalify.core.Core(source_data=meta, schema_data=schema)\ |
| 69 | + .validate() |
| 70 | + except pykwalify.errors.SchemaError as e: |
| 71 | + print('ERROR: Malformed "build" section in file: {}\n{}' |
| 72 | + .format(module_yml, e), file=sys.stderr) |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | + section = meta.get('build', dict()) |
| 76 | + cmake_setting = section.get('cmake', None) |
| 77 | + if not validate_setting(cmake_setting, module, 'CMakeLists.txt'): |
| 78 | + print('ERROR: "cmake" key in {} has folder value "{}" which ' |
| 79 | + 'does not contain a CMakeLists.txt file.' |
| 80 | + .format(module_yml, cmake_setting), file=sys.stderr) |
| 81 | + sys.exit(1) |
| 82 | + |
| 83 | + kconfig_setting = section.get('kconfig', None) |
| 84 | + if not validate_setting(kconfig_setting, module): |
| 85 | + print('ERROR: "kconfig" key in {} has value "{}" which does not ' |
| 86 | + 'point to a valid Kconfig file.' |
| 87 | + .format(module_yml, kconfig_setting), file=sys.stderr) |
| 88 | + sys.exit(1) |
| 89 | + |
| 90 | + cmake_path = os.path.join(module, cmake_setting or 'zephyr') |
| 91 | + cmake_file = os.path.join(cmake_path, 'CMakeLists.txt') |
| 92 | + if os.path.isfile(cmake_file) and cmake_out is not None: |
| 93 | + cmake_out.write('{}:{}\n'.format(os.path.basename(module), |
| 94 | + os.path.abspath(cmake_path))) |
| 95 | + |
| 96 | + kconfig_file = os.path.join(module, kconfig_setting or 'zephyr/Kconfig') |
| 97 | + if os.path.isfile(kconfig_file) and kconfig_out is not None: |
| 98 | + kconfig_out.write('osource "{}"\n\n' |
| 99 | + .format(os.path.abspath(kconfig_file))) |
| 100 | + |
| 101 | + |
| 102 | +def main(): |
| 103 | + kconfig_out_file = None |
| 104 | + cmake_out_file = None |
| 105 | + |
| 106 | + parser = argparse.ArgumentParser(description=''' |
| 107 | + Process a list of projects and create Kconfig / CMake include files for |
| 108 | + projects which are also a Zephyr module''') |
| 109 | + |
| 110 | + parser.add_argument('--kconfig-out', |
| 111 | + help='File to write with resulting KConfig import' |
| 112 | + 'statements.') |
| 113 | + parser.add_argument('--cmake-out', |
| 114 | + help='File to write with resulting <name>:<path>' |
| 115 | + 'values to use for including in CMake') |
| 116 | + parser.add_argument('-m', '--modules', nargs='+', |
| 117 | + help='List of modules to parse instead of using `west' |
| 118 | + 'list`') |
| 119 | + parser.add_argument('-x', '--extra-modules', nargs='+', |
| 120 | + help='List of extra modules to parse') |
| 121 | + args = parser.parse_args() |
| 122 | + |
| 123 | + if args.modules is None: |
| 124 | + p = subprocess.Popen(['west', 'list', '--format={posixpath}'], |
| 125 | + stdout=subprocess.PIPE, |
| 126 | + stderr=subprocess.PIPE) |
| 127 | + out, err = p.communicate() |
| 128 | + if p.returncode == 0: |
| 129 | + projects = out.decode(sys.getdefaultencoding()).splitlines() |
| 130 | + elif re.match(r'Error: .* is not in a west installation\..*', |
| 131 | + err.decode(sys.getdefaultencoding())): |
| 132 | + # Only accept the error from bootstrapper in the event we are |
| 133 | + # outside a west managed project. |
| 134 | + projects = [] |
| 135 | + else: |
| 136 | + # A real error occurred, raise an exception |
| 137 | + raise subprocess.CalledProcessError(cmd=p.args, |
| 138 | + returncode=p.returncode) |
| 139 | + else: |
| 140 | + projects = args.modules |
| 141 | + |
| 142 | + if args.extra_modules is not None: |
| 143 | + projects += args.extra_modules |
| 144 | + |
| 145 | + if args.kconfig_out: |
| 146 | + kconfig_out_file = open(args.kconfig_out, 'w') |
| 147 | + |
| 148 | + if args.cmake_out: |
| 149 | + cmake_out_file = open(args.cmake_out, 'w') |
| 150 | + |
| 151 | + try: |
| 152 | + for project in projects: |
| 153 | + # Avoid including Zephyr base project as module. |
| 154 | + if project != os.environ.get('ZEPHYR_BASE'): |
| 155 | + process_module(project, cmake_out_file, kconfig_out_file) |
| 156 | + finally: |
| 157 | + if args.kconfig_out: |
| 158 | + kconfig_out_file.close() |
| 159 | + if args.cmake_out: |
| 160 | + cmake_out_file.close() |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + main() |
0 commit comments