2019-03-19 10:38:18 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019, Nordic Semiconductor ASA
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
'''Tool for parsing a list of projects to determine if they are Zephyr
|
|
|
|
projects. If no projects are given then the output from `west list` will be
|
|
|
|
used as project list.
|
|
|
|
|
|
|
|
Include file is generated for Kconfig using --kconfig-out.
|
|
|
|
A <name>:<path> text file is generated for use with CMake using --cmake-out.
|
2019-12-11 16:13:23 +01:00
|
|
|
|
2020-12-07 20:52:10 +01:00
|
|
|
Using --twister-out <filename> an argument file for twister script will
|
2019-12-11 16:13:23 +01:00
|
|
|
be generated which would point to test and sample roots available in modules
|
2020-12-07 20:52:10 +01:00
|
|
|
that can be included during a twister run. This allows testing code
|
2019-12-11 16:13:23 +01:00
|
|
|
maintained in modules in addition to what is available in the main Zephyr tree.
|
2019-03-19 10:38:18 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
import pykwalify.core
|
2019-12-13 09:42:13 +01:00
|
|
|
from pathlib import Path, PurePath
|
2020-05-18 22:34:49 +02:00
|
|
|
from collections import namedtuple
|
2019-03-19 10:38:18 +01:00
|
|
|
|
|
|
|
METADATA_SCHEMA = '''
|
|
|
|
## A pykwalify schema for basic validation of the structure of a
|
|
|
|
## metadata YAML file.
|
|
|
|
##
|
|
|
|
# The zephyr/module.yml file is a simple list of key value pairs to be used by
|
|
|
|
# the build system.
|
|
|
|
type: map
|
|
|
|
mapping:
|
|
|
|
build:
|
2019-12-11 16:13:23 +01:00
|
|
|
required: false
|
2019-03-19 10:38:18 +01:00
|
|
|
type: map
|
|
|
|
mapping:
|
|
|
|
cmake:
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
kconfig:
|
|
|
|
required: false
|
|
|
|
type: str
|
2020-05-18 22:34:49 +02:00
|
|
|
depends:
|
|
|
|
required: false
|
|
|
|
type: seq
|
|
|
|
sequence:
|
|
|
|
- type: str
|
2020-07-07 17:29:56 +02:00
|
|
|
settings:
|
|
|
|
required: false
|
|
|
|
type: map
|
|
|
|
mapping:
|
|
|
|
board_root:
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
dts_root:
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
soc_root:
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
arch_root:
|
|
|
|
required: false
|
|
|
|
type: str
|
2019-12-11 16:13:23 +01:00
|
|
|
tests:
|
|
|
|
required: false
|
|
|
|
type: seq
|
|
|
|
sequence:
|
|
|
|
- type: str
|
|
|
|
samples:
|
|
|
|
required: false
|
|
|
|
type: seq
|
|
|
|
sequence:
|
|
|
|
- type: str
|
|
|
|
boards:
|
|
|
|
required: false
|
|
|
|
type: seq
|
|
|
|
sequence:
|
|
|
|
- type: str
|
2019-03-19 10:38:18 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
schema = yaml.safe_load(METADATA_SCHEMA)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_setting(setting, module_path, filename=None):
|
|
|
|
if setting is not None:
|
|
|
|
if filename is not None:
|
|
|
|
checkfile = os.path.join(module_path, setting, filename)
|
|
|
|
else:
|
|
|
|
checkfile = os.path.join(module_path, setting)
|
|
|
|
if not os.path.isfile(checkfile):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
def process_module(module):
|
2019-12-13 09:42:13 +01:00
|
|
|
module_path = PurePath(module)
|
|
|
|
module_yml = module_path.joinpath('zephyr/module.yml')
|
2019-12-11 16:13:23 +01:00
|
|
|
|
2019-12-19 15:49:22 +01:00
|
|
|
# The input is a module if zephyr/module.yml is a valid yaml file
|
|
|
|
# or if both zephyr/CMakeLists.txt and zephyr/Kconfig are present.
|
|
|
|
|
2019-12-13 09:42:13 +01:00
|
|
|
if Path(module_yml).is_file():
|
|
|
|
with Path(module_yml).open('r') as f:
|
2019-03-19 10:38:18 +01:00
|
|
|
meta = yaml.safe_load(f.read())
|
|
|
|
|
|
|
|
try:
|
|
|
|
pykwalify.core.Core(source_data=meta, schema_data=schema)\
|
|
|
|
.validate()
|
|
|
|
except pykwalify.errors.SchemaError as e:
|
2019-09-07 14:41:01 +02:00
|
|
|
sys.exit('ERROR: Malformed "build" section in file: {}\n{}'
|
2019-12-13 09:42:13 +01:00
|
|
|
.format(module_yml.as_posix(), e))
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
return meta
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2019-12-19 15:49:22 +01:00
|
|
|
if Path(module_path.joinpath('zephyr/CMakeLists.txt')).is_file() and \
|
|
|
|
Path(module_path.joinpath('zephyr/Kconfig')).is_file():
|
|
|
|
return {'build': {'cmake': 'zephyr', 'kconfig': 'zephyr/Kconfig'}}
|
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def process_cmake(module, meta):
|
|
|
|
section = meta.get('build', dict())
|
|
|
|
module_path = PurePath(module)
|
|
|
|
module_yml = module_path.joinpath('zephyr/module.yml')
|
|
|
|
cmake_setting = section.get('cmake', None)
|
|
|
|
if not validate_setting(cmake_setting, module, 'CMakeLists.txt'):
|
|
|
|
sys.exit('ERROR: "cmake" key in {} has folder value "{}" which '
|
2020-08-25 14:02:04 +02:00
|
|
|
'does not contain a CMakeLists.txt file.'
|
|
|
|
.format(module_yml.as_posix(), cmake_setting))
|
2019-12-11 16:13:23 +01:00
|
|
|
|
|
|
|
cmake_path = os.path.join(module, cmake_setting or 'zephyr')
|
|
|
|
cmake_file = os.path.join(cmake_path, 'CMakeLists.txt')
|
|
|
|
if os.path.isfile(cmake_file):
|
2020-08-25 13:32:33 +02:00
|
|
|
return('\"{}\":\"{}\":\"{}\"\n'
|
2020-08-25 14:02:04 +02:00
|
|
|
.format(module_path.name,
|
2020-08-25 13:32:33 +02:00
|
|
|
module_path.as_posix(),
|
2020-08-25 14:02:04 +02:00
|
|
|
Path(cmake_path).resolve().as_posix()))
|
2019-12-11 16:13:23 +01:00
|
|
|
else:
|
2020-08-25 13:32:33 +02:00
|
|
|
return('\"{}\":\"{}\":\"\"\n'
|
|
|
|
.format(module_path.name,
|
|
|
|
module_path.as_posix()))
|
2020-08-25 14:02:04 +02:00
|
|
|
|
2020-07-07 17:29:56 +02:00
|
|
|
def process_settings(module, meta):
|
|
|
|
section = meta.get('build', dict())
|
|
|
|
build_settings = section.get('settings', None)
|
|
|
|
out_text = ""
|
|
|
|
|
|
|
|
if build_settings is not None:
|
|
|
|
for root in ['board', 'dts', 'soc', 'arch']:
|
|
|
|
setting = build_settings.get(root+'_root', None)
|
|
|
|
if setting is not None:
|
|
|
|
root_path = PurePath(module) / setting
|
2020-10-15 22:16:41 +02:00
|
|
|
out_text += f'"{root.upper()}_ROOT":"{root_path.as_posix()}"\n'
|
2020-07-07 17:29:56 +02:00
|
|
|
|
|
|
|
return out_text
|
|
|
|
|
2020-08-25 14:02:04 +02:00
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
def process_kconfig(module, meta):
|
|
|
|
section = meta.get('build', dict())
|
|
|
|
module_path = PurePath(module)
|
|
|
|
module_yml = module_path.joinpath('zephyr/module.yml')
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
kconfig_setting = section.get('kconfig', None)
|
|
|
|
if not validate_setting(kconfig_setting, module):
|
|
|
|
sys.exit('ERROR: "kconfig" key in {} has value "{}" which does '
|
2020-08-25 14:02:04 +02:00
|
|
|
'not point to a valid Kconfig file.'
|
|
|
|
.format(module_yml, kconfig_setting))
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
kconfig_file = os.path.join(module, kconfig_setting or 'zephyr/Kconfig')
|
|
|
|
if os.path.isfile(kconfig_file):
|
2020-08-25 14:02:04 +02:00
|
|
|
return 'osource "{}"\n\n'.format(Path(kconfig_file)
|
|
|
|
.resolve().as_posix())
|
2019-12-11 16:13:23 +01:00
|
|
|
else:
|
|
|
|
return ""
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2020-12-07 20:52:10 +01:00
|
|
|
def process_twister(module, meta):
|
2019-12-11 16:13:23 +01:00
|
|
|
|
|
|
|
out = ""
|
|
|
|
tests = meta.get('tests', [])
|
|
|
|
samples = meta.get('samples', [])
|
|
|
|
boards = meta.get('boards', [])
|
|
|
|
|
|
|
|
for pth in tests + samples:
|
|
|
|
if pth:
|
|
|
|
dir = os.path.join(module, pth)
|
2020-08-25 14:02:04 +02:00
|
|
|
out += '-T\n{}\n'.format(PurePath(os.path.abspath(dir))
|
|
|
|
.as_posix())
|
2019-12-11 16:13:23 +01:00
|
|
|
|
|
|
|
for pth in boards:
|
|
|
|
if pth:
|
|
|
|
dir = os.path.join(module, pth)
|
2020-08-25 14:02:04 +02:00
|
|
|
out += '--board-root\n{}\n'.format(PurePath(os.path.abspath(dir))
|
|
|
|
.as_posix())
|
2019-12-11 16:13:23 +01:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2019-03-19 10:38:18 +01:00
|
|
|
parser = argparse.ArgumentParser(description='''
|
|
|
|
Process a list of projects and create Kconfig / CMake include files for
|
|
|
|
projects which are also a Zephyr module''')
|
|
|
|
|
|
|
|
parser.add_argument('--kconfig-out',
|
2019-12-11 16:13:23 +01:00
|
|
|
help="""File to write with resulting KConfig import
|
|
|
|
statements.""")
|
2020-12-07 20:52:10 +01:00
|
|
|
parser.add_argument('--twister-out',
|
|
|
|
help="""File to write with resulting twister
|
2020-08-25 14:02:04 +02:00
|
|
|
parameters.""")
|
2019-03-19 10:38:18 +01:00
|
|
|
parser.add_argument('--cmake-out',
|
2019-12-11 16:13:23 +01:00
|
|
|
help="""File to write with resulting <name>:<path>
|
|
|
|
values to use for including in CMake""")
|
2020-07-07 17:29:56 +02:00
|
|
|
parser.add_argument('--settings-out',
|
|
|
|
help="""File to write with resulting <name>:<value>
|
|
|
|
values to use for including in CMake""")
|
2019-03-19 10:38:18 +01:00
|
|
|
parser.add_argument('-m', '--modules', nargs='+',
|
2019-12-11 16:13:23 +01:00
|
|
|
help="""List of modules to parse instead of using `west
|
|
|
|
list`""")
|
2020-01-29 21:38:58 +01:00
|
|
|
parser.add_argument('-x', '--extra-modules', nargs='+', default=[],
|
2019-03-19 10:38:18 +01:00
|
|
|
help='List of extra modules to parse')
|
2020-02-19 12:01:39 +01:00
|
|
|
parser.add_argument('-z', '--zephyr-base',
|
|
|
|
help='Path to zephyr repository')
|
2019-03-19 10:38:18 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.modules is None:
|
2020-08-25 14:02:04 +02:00
|
|
|
# West is imported here, as it is optional
|
|
|
|
# (and thus maybe not installed)
|
cmake: west: invoke west using same python as rest of build system
When running CMake, then Python3 will be used.
This is detected through FindPython3, with a preference for using the
python or python3 in path, if any of those matches the required Python
minimal version in Zephyr.
It is also possible for users to specify a different Python, as example
by using:
`cmake -DPYTHON_PREFER=/usr/bin/python3.x`
However, when running `west` as native command, then west will be
invoked on linux based on the python defined in:
`west` launcher, which could be: `#!/usr/bin/python3.y`
Thus there could be mismatch in Pythons used for `west` and the python
used for other scripts.
This is even worse on windows, where a user might experience:
```
>.\opt\bin\Scripts\west.exe --version
Traceback (most recent call last):
File "C:\Python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
...
File "C:\Python37\lib\socket.py", line 49, in <module>
import _socket
ImportError: Module use of python38.dll conflicts with this version of
Python.
```
when testing out a newer Python, but the python in path is still a 3.7.
By importing `west` into zephyr_module.py and by using, as example
`python -c "from west.util import west_topdir; print(topdir())"`
we ensure the same python is used in all python scripts.
Also it allows the user to control the python to use for west.
It also ensures that the west version being tested, is also the version
being used, where old code would test the version imported by python,
but using the west in path (which could be a different version)
If the west version installed in the current Python, and west invocation
is using a different Python interpreter, then an additional help text
is printed, to easier assist users with debugging.
Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2020-06-08 21:09:15 +02:00
|
|
|
# if user is providing a specific modules list.
|
|
|
|
from west.manifest import Manifest
|
|
|
|
from west.util import WestNotFound
|
|
|
|
try:
|
|
|
|
manifest = Manifest.from_file()
|
|
|
|
projects = [p.posixpath for p in manifest.get_projects([])]
|
|
|
|
except WestNotFound:
|
|
|
|
# Only accept WestNotFound, meaning we are not in a west
|
|
|
|
# workspace. Such setup is allowed, as west may be installed
|
|
|
|
# but the project is not required to use west.
|
2019-03-19 10:38:18 +01:00
|
|
|
projects = []
|
2020-07-20 14:09:12 +02:00
|
|
|
else:
|
|
|
|
projects = args.modules.copy()
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2020-01-29 21:38:58 +01:00
|
|
|
projects += args.extra_modules
|
|
|
|
extra_modules = set(args.extra_modules)
|
2019-12-11 16:13:23 +01:00
|
|
|
|
|
|
|
kconfig = ""
|
|
|
|
cmake = ""
|
2020-07-07 17:29:56 +02:00
|
|
|
settings = ""
|
2020-12-07 20:52:10 +01:00
|
|
|
twister = ""
|
2019-12-11 16:13:23 +01:00
|
|
|
|
2020-05-18 22:34:49 +02:00
|
|
|
Module = namedtuple('Module', ['project', 'meta', 'depends'])
|
|
|
|
# dep_modules is a list of all modules that has an unresolved dependency
|
|
|
|
dep_modules = []
|
|
|
|
# start_modules is a list modules with no depends left (no incoming edge)
|
|
|
|
start_modules = []
|
|
|
|
# sorted_modules is a topological sorted list of the modules
|
|
|
|
sorted_modules = []
|
|
|
|
|
2019-12-11 16:13:23 +01:00
|
|
|
for project in projects:
|
|
|
|
# Avoid including Zephyr base project as module.
|
cmake: west: invoke west using same python as rest of build system
When running CMake, then Python3 will be used.
This is detected through FindPython3, with a preference for using the
python or python3 in path, if any of those matches the required Python
minimal version in Zephyr.
It is also possible for users to specify a different Python, as example
by using:
`cmake -DPYTHON_PREFER=/usr/bin/python3.x`
However, when running `west` as native command, then west will be
invoked on linux based on the python defined in:
`west` launcher, which could be: `#!/usr/bin/python3.y`
Thus there could be mismatch in Pythons used for `west` and the python
used for other scripts.
This is even worse on windows, where a user might experience:
```
>.\opt\bin\Scripts\west.exe --version
Traceback (most recent call last):
File "C:\Python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
...
File "C:\Python37\lib\socket.py", line 49, in <module>
import _socket
ImportError: Module use of python38.dll conflicts with this version of
Python.
```
when testing out a newer Python, but the python in path is still a 3.7.
By importing `west` into zephyr_module.py and by using, as example
`python -c "from west.util import west_topdir; print(topdir())"`
we ensure the same python is used in all python scripts.
Also it allows the user to control the python to use for west.
It also ensures that the west version being tested, is also the version
being used, where old code would test the version imported by python,
but using the west in path (which could be a different version)
If the west version installed in the current Python, and west invocation
is using a different Python interpreter, then an additional help text
is printed, to easier assist users with debugging.
Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2020-06-08 21:09:15 +02:00
|
|
|
if project == args.zephyr_base:
|
2019-12-11 16:13:23 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
meta = process_module(project)
|
|
|
|
if meta:
|
2020-05-18 22:34:49 +02:00
|
|
|
section = meta.get('build', dict())
|
|
|
|
deps = section.get('depends', [])
|
|
|
|
if not deps:
|
|
|
|
start_modules.append(Module(project, meta, []))
|
|
|
|
else:
|
|
|
|
dep_modules.append(Module(project, meta, deps))
|
2020-01-29 21:38:58 +01:00
|
|
|
elif project in extra_modules:
|
|
|
|
sys.exit(f'{project}, given in ZEPHYR_EXTRA_MODULES, '
|
|
|
|
'is not a valid zephyr module')
|
2019-12-11 16:13:23 +01:00
|
|
|
|
2020-05-18 22:34:49 +02:00
|
|
|
# This will do a topological sort to ensure the modules are ordered
|
|
|
|
# according to dependency settings.
|
|
|
|
while start_modules:
|
|
|
|
node = start_modules.pop(0)
|
|
|
|
sorted_modules.append(node)
|
|
|
|
node_name = PurePath(node.project).name
|
|
|
|
to_remove = []
|
|
|
|
for module in dep_modules:
|
|
|
|
if node_name in module.depends:
|
|
|
|
module.depends.remove(node_name)
|
|
|
|
if not module.depends:
|
|
|
|
start_modules.append(module)
|
|
|
|
to_remove.append(module)
|
|
|
|
for module in to_remove:
|
|
|
|
dep_modules.remove(module)
|
|
|
|
|
|
|
|
if dep_modules:
|
|
|
|
# If there are any modules with unresolved dependencies, then the
|
|
|
|
# modules contains unmet or cyclic dependencies. Error out.
|
|
|
|
error = 'Unmet or cyclic dependencies in modules:\n'
|
|
|
|
for module in dep_modules:
|
|
|
|
error += f'{module.project} depends on: {module.depends}\n'
|
|
|
|
sys.exit(error)
|
|
|
|
|
|
|
|
for module in sorted_modules:
|
|
|
|
kconfig += process_kconfig(module.project, module.meta)
|
|
|
|
cmake += process_cmake(module.project, module.meta)
|
2020-07-07 17:29:56 +02:00
|
|
|
settings += process_settings(module.project, module.meta)
|
2020-12-07 20:52:10 +01:00
|
|
|
twister += process_twister(module.project, module.meta)
|
2020-05-18 22:34:49 +02:00
|
|
|
|
2019-03-19 10:38:18 +01:00
|
|
|
if args.kconfig_out:
|
2019-12-11 16:13:23 +01:00
|
|
|
with open(args.kconfig_out, 'w', encoding="utf-8") as fp:
|
|
|
|
fp.write(kconfig)
|
2019-03-19 10:38:18 +01:00
|
|
|
|
|
|
|
if args.cmake_out:
|
2019-12-11 16:13:23 +01:00
|
|
|
with open(args.cmake_out, 'w', encoding="utf-8") as fp:
|
|
|
|
fp.write(cmake)
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2020-07-07 17:29:56 +02:00
|
|
|
if args.settings_out:
|
|
|
|
with open(args.settings_out, 'w', encoding="utf-8") as fp:
|
|
|
|
fp.write(settings)
|
|
|
|
|
2020-12-07 20:52:10 +01:00
|
|
|
if args.twister_out:
|
|
|
|
with open(args.twister_out, 'w', encoding="utf-8") as fp:
|
|
|
|
fp.write(twister)
|
2019-03-19 10:38:18 +01:00
|
|
|
|
2020-08-25 14:02:04 +02:00
|
|
|
|
2019-03-19 10:38:18 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|