Skip to content

Commit

Permalink
cleanup and format script/generate_cmake.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lmnotran committed Feb 7, 2024
1 parent 674e937 commit d1fa0d0
Showing 1 changed file with 35 additions and 45 deletions.
80 changes: 35 additions & 45 deletions script/generate_cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
# POSSIBILITY OF SUCH DAMAGE.
#


from itertools import filterfalse
from jinja2 import Environment, FileSystemLoader
import jinja2
from pathlib import Path
import copy
import git
import yaml
import argparse
import os
Expand All @@ -59,31 +57,30 @@ def prepare_path(path: str) -> str:
path = path.replace('(SDK_PATH)', '{SILABS_GSDK_DIR}')

# Redirect OpenThread stack sources to the ot-efr32 openthread submodule #}
path = path.replace(
'${SILABS_GSDK_DIR}/util/third_party/openthread', '${PROJECT_SOURCE_DIR}/openthread')
path = path.replace('${SILABS_GSDK_DIR}/util/third_party/openthread', '${PROJECT_SOURCE_DIR}/openthread')

# Redirect PAL sources to the ot-efr32 PAL
path = path.replace(
'${SILABS_GSDK_DIR}/protocol/openthread/platform-abstraction/efr32', '${PROJECT_SOURCE_DIR}/src/src')
path = path.replace('${SILABS_GSDK_DIR}/protocol/openthread/platform-abstraction/efr32',
'${PROJECT_SOURCE_DIR}/src/src')

return path


def is_mbedtls_source(source: str) -> bool:
r = False
r: bool = False
r |= '${SILABS_GSDK_DIR}/util/third_party/mbedtls' in source
return r


def is_mbedtls_define(define: str) -> bool:
r = False
r: bool = False
r |= define.startswith("MBEDTLS_")
r |= define.startswith("PSA_")
return r


def is_openthread_device_type_define(define: str) -> bool:
device_types = [
device_types: list[str] = [
"OPENTHREAD_COPROCESSOR",
"OPENTHREAD_RADIO",
"OPENTHREAD_FTD",
Expand All @@ -93,7 +90,7 @@ def is_openthread_device_type_define(define: str) -> bool:


def is_openthread_define(define: str) -> bool:
r = True
r: bool = False
r &= not is_mbedtls_define(define)
r &= not is_openthread_device_type_define(define)
r &= define.startswith("OPENTHREAD_")
Expand All @@ -104,20 +101,20 @@ def filter_mbedtls_lib_vars(slc_vars: dict) -> dict:
'''Filter mbedtls lib vars'''

# Start with a copy of everything
mbedtls_lib_vars = copy.deepcopy(slc_vars)
mbedtls_lib_vars: dict = copy.deepcopy(slc_vars)

# Filter includes
def f(include: str) -> bool:
keep = False
keep: bool = False
keep |= (include == '"autogen"') or (include == '"config"')
keep |= '${SILABS_GSDK_DIR}/hardware' in include
keep |= '${SILABS_GSDK_DIR}/platform' in include
keep |= '${SILABS_GSDK_DIR}/util/third_party/crypto' in include
if not keep:
pass
return keep
mbedtls_lib_vars['MBEDTLS_INCLUDES'] = list(
filter(f, mbedtls_lib_vars['C_CXX_INCLUDES']))

mbedtls_lib_vars['MBEDTLS_INCLUDES'] = list(filter(f, mbedtls_lib_vars['C_CXX_INCLUDES']))

return mbedtls_lib_vars

Expand All @@ -131,25 +128,13 @@ def filter_platform_lib_vars(slc_vars: dict) -> dict:
return platform_lib_vars


parser = argparse.ArgumentParser()
parser.add_argument(
"slc_vars_yaml", help="the .yml file generated by slc. This file should contain all the standard SLC template variables")
parser: argparse.ArgumentParser = argparse.ArgumentParser()
parser.add_argument(
"output_dir", help="the output dir for any generated files")
"slc_vars_yaml",
help="the .yml file generated by slc. This file should contain all the standard SLC template variables")
parser.add_argument("output_dir", help="the output dir for any generated files")
args = parser.parse_args()

script_dir = Path(os.path.dirname(os.path.abspath(__file__)))
repo_root = script_dir.parent

# Define CMakeLists.txt template location
environment = Environment(loader=FileSystemLoader(
f"{repo_root}/slc/exporter_templates/platform_library"))
platform_lib_template = environment.get_template("CMakeLists.txt.jinja")
mbedtls_lib_template = environment.get_template("mbedtls.cmake.jinja")
slc_vars_yaml_dir = Path(args.slc_vars_yaml).parent
platform_lib_output_file = slc_vars_yaml_dir / "CMakeLists.txt"
mbedtls_lib_output_file = slc_vars_yaml_dir / "mbedtls.cmake"

# Import slc variables
with open(args.slc_vars_yaml, mode="r") as slc_vars_yaml:
global slc_vars
Expand All @@ -160,6 +145,7 @@ def filter_platform_lib_vars(slc_vars: dict) -> dict:
if not v:
slc_vars[k] = list()


# Remove mapfile flag from linker options
def f(flag: str) -> bool:
'''Function for use with filter() to determine if a flag should be kept'''
Expand All @@ -168,6 +154,8 @@ def f(flag: str) -> bool:
keep &= "--specs=" not in flag
keep &= "linkerfile.ld" not in flag
return keep


for key in ["EXT_LD_FLAGS", "EXT_DEBUG_LD_FLAGS"]:
flags = slc_vars[key]
slc_vars[key] = list(filter(f, flags))
Expand All @@ -178,9 +166,7 @@ def f(flag: str) -> bool:
"ALL_SOURCES",
"SYS_LIBS",
"USER_LIBS",

]
# for key,value in slc_vars.items():
for key in keys_to_prepare:
value = slc_vars[key]
if type(value) is list:
Expand All @@ -195,50 +181,54 @@ def f(flag: str) -> bool:
for key in slc_vars:
if type(slc_vars[key]) is list:
slc_vars[key] = list(filter(lambda item: item, slc_vars[key]))
# elif type(slc_vars[key]) is dict:
# for k,v in slc_vars[key].items():

# Separate mbedtls sources
slc_vars['MBEDTLS_SOURCES'] = list(
filter(is_mbedtls_source, slc_vars['ALL_SOURCES']))
slc_vars['NON_MBEDTLS_SOURCES'] = list(
filterfalse(is_mbedtls_source, slc_vars['ALL_SOURCES']))
slc_vars['MBEDTLS_SOURCES'] = list(filter(is_mbedtls_source, slc_vars['ALL_SOURCES']))
slc_vars['NON_MBEDTLS_SOURCES'] = list(filterfalse(is_mbedtls_source, slc_vars['ALL_SOURCES']))

# ==============================================================================
# Filter defines
# ==============================================================================
defines = slc_vars['C_CXX_DEFINES']

# Separate mbedtls defines
slc_vars['MBEDTLS_DEFINES'] = {d: defines[d]
for d in defines if is_mbedtls_define(d)}
slc_vars['MBEDTLS_DEFINES'] = {d: defines[d] for d in defines if is_mbedtls_define(d)}
for d in slc_vars['MBEDTLS_DEFINES']:
del defines[d]

# Filter OPENTHREAD device type and remove from C_CXX_DEFINES
slc_vars['OPENTHREAD_DEVICE_TYPE'] = {d: defines[d]
for d in defines if is_openthread_device_type_define(d)}
slc_vars['OPENTHREAD_DEVICE_TYPE'] = {d: defines[d] for d in defines if is_openthread_device_type_define(d)}
for d in slc_vars['OPENTHREAD_DEVICE_TYPE']:
del defines[d]

# Filter OPENTHREAD_* defines
slc_vars['OPENTHREAD_DEFINES'] = {d: defines[d]
for d in defines if is_openthread_define(d)}
slc_vars['OPENTHREAD_DEFINES'] = {d: defines[d] for d in defines if is_openthread_define(d)}
for d in slc_vars['OPENTHREAD_DEFINES']:
del defines[d]

# filter slc vars
platform_lib_vars = filter_platform_lib_vars(slc_vars)
mbedtls_lib_vars = filter_mbedtls_lib_vars(slc_vars)

# Define CMakeLists.txt template location
script_dir: Path = Path(os.path.dirname(os.path.abspath(__file__)))
repo_root: Path = script_dir.parent
environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(f"{repo_root}/slc/exporter_templates/platform_library"))
platform_lib_template: jinja2.Template = environment.get_template("CMakeLists.txt.jinja")
mbedtls_lib_template: jinja2.Template = environment.get_template("mbedtls.cmake.jinja")

# Render the template with the imported variables
platform_lib_content = platform_lib_template.render(platform_lib_vars)
mbedtls_lib_content = mbedtls_lib_template.render(mbedtls_lib_vars)

# Output rendered files
slc_vars_yaml_dir: Path = Path(args.slc_vars_yaml).parent
platform_lib_output_file: Path = slc_vars_yaml_dir / "CMakeLists.txt"
with open(platform_lib_output_file, mode="w", encoding="utf-8") as message:
message.write(platform_lib_content)
print(f"... wrote {platform_lib_output_file}")
mbedtls_lib_output_file: Path = slc_vars_yaml_dir / "mbedtls.cmake"
with open(mbedtls_lib_output_file, mode="w", encoding="utf-8") as message:
message.write(mbedtls_lib_content)
print(f"... wrote {mbedtls_lib_output_file}")

0 comments on commit d1fa0d0

Please sign in to comment.