Skip to content

Commit

Permalink
lmdk: python tools for building library
Browse files Browse the repository at this point in the history
Libraries are build now:
	python lmdk/scripts/libraries_build.py -l <modules_names>

we can build multiple modules by adding them one after another.

Signed-off-by: Dobrowolski, PawelX <[email protected]>
  • Loading branch information
pjdobrowolski committed Apr 19, 2024
1 parent 11dae63 commit 8123ab9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
76 changes: 76 additions & 0 deletions lmdk/scripts/libraries_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause


import pathlib
import dataclasses
from tools.utils import rmtree_if_exists, execute_command
import argparse
import platform as py_platform
import sys
import os
import warnings

SOF_TOP = pathlib.Path(__file__).parents[3].resolve()
MIN_PYTHON_VERSION = 3, 8
assert sys.version_info >= MIN_PYTHON_VERSION, \
f"Python {MIN_PYTHON_VERSION} or above is required."

"""Constant value resolves SOF_TOP directory as: "this script directory/.." """
SOF_TOP = pathlib.Path(__file__).parents[1].resolve()
LMDK_BUILD_DIR = SOF_TOP / "../.." / "sof" / "lmdk"
RIMAGE_BUILD_DIR = SOF_TOP / "../.." /"build-rimage"

if py_platform.system() == "Windows":
xtensa_tools_version_postfix = "-win32"
elif py_platform.system() == "Linux":
xtensa_tools_version_postfix = "-linux"
else:
xtensa_tools_version_postfix = "-unsupportedOS"
warnings.warn(f"Your operating system: {py_platform.system()} is not supported")


def build_libraries(LMDK_BUILD_DIR, RIMAGE_BUILD_DIR, args):
library_dir = LMDK_BUILD_DIR / "libraries"
"""Cmake build"""
for lib in args.libraries:
library_cmake = library_dir / lib / "CMakeLists.txt"
if library_cmake.is_file():
print(f"\nBuilding loadable module: " + lib)
lib_path = pathlib.Path(library_dir, lib, "build")
print(f"\nRemoving existing artifacts")
rmtree_if_exists(lib_path)
lib_path.mkdir(parents=True, exist_ok=True)
rimage_bin = RIMAGE_BUILD_DIR / "rimage.exe"
if not rimage_bin.is_file():
rimage_bin = RIMAGE_BUILD_DIR / "rimage"
if args.key:
key = "-DSIGNING_KEY=" + args.key.__str__()
else:
key = "-DSIGNING_KEY=" + PlatformConfig.RIMAGE_KEY.__str__()
execute_command(["cmake", "-B", "build", "-G", "Ninja",
"-DRIMAGE_COMMAND="+str(rimage_bin), key],
cwd=library_dir/lib)
execute_command(["cmake", "--build", "build", "-v"], cwd=library_dir/lib)


def parse_args():
parser = argparse.ArgumentParser(description='Building loadable modules using python scripts')
parser.add_argument("-k", "--key", type=pathlib.Path, required=False,
help="Path to a non-default rimage signing key.")

parser.add_argument("-l", "--libraries", nargs="*", default=[],
help=""" Function for CMake building modules. We can build more then one module
just by adding more module names.""")
return parser.parse_args()


def main():
args = parse_args()

if args.libraries:
build_libraries(LMDK_BUILD_DIR, RIMAGE_BUILD_DIR, args)


if __name__ == "__main__":
main()
Empty file added lmdk/scripts/tools/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions lmdk/scripts/tools/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: BSD-3-Clause


import shlex
import subprocess
import shutil
import os


def rmtree_if_exists(directory):
"This is different from ignore_errors=False because it deletes everything or nothing"
if os.path.exists(directory):
shutil.rmtree(directory)


def execute_command(*run_args, **run_kwargs):
"""[summary] Provides wrapper for subprocess.run that prints
command executed when 'more verbose' verbosity level is set."""
command_args = run_args[0]

# If you really need the shell in some non-portable section then
# invoke subprocess.run() directly.
if run_kwargs.get('shell') or not isinstance(command_args, list):
raise RuntimeError("Do not rely on non-portable shell parsing")

run_kwargs = {k: run_kwargs[k] for k in run_kwargs if not k.startswith("sof_")}

if not 'check' in run_kwargs:
run_kwargs['check'] = True
#pylint:disable=subprocess-run-check

return subprocess.run(*run_args, **run_kwargs)

0 comments on commit 8123ab9

Please sign in to comment.