2023-05-03 05:48:49 +00:00
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
import pathlib
|
|
|
|
import posixpath
|
2022-10-25 22:15:02 +00:00
|
|
|
import shutil
|
2023-05-03 05:48:49 +00:00
|
|
|
|
|
|
|
from fbt.sdk.cache import SdkCache
|
|
|
|
from fbt.sdk.collector import SdkCollector
|
2024-03-20 22:36:38 +00:00
|
|
|
from fbt.util import PosixPathWrapper
|
2022-09-14 16:11:38 +00:00
|
|
|
from SCons.Action import Action
|
2023-05-03 05:48:49 +00:00
|
|
|
from SCons.Builder import Builder
|
2022-09-14 16:11:38 +00:00
|
|
|
from SCons.Errors import UserError
|
|
|
|
|
|
|
|
# from SCons.Scanner import C
|
2022-10-28 15:32:06 +00:00
|
|
|
from SCons.Script import Entry
|
2022-09-14 16:11:38 +00:00
|
|
|
from SCons.Util import LogicalLines
|
|
|
|
|
|
|
|
|
2022-10-12 16:12:05 +00:00
|
|
|
def ProcessSdkDepends(env, filename):
|
|
|
|
try:
|
|
|
|
with open(filename, "r") as fin:
|
|
|
|
lines = LogicalLines(fin).readlines()
|
|
|
|
except IOError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
_, depends = lines[0].split(":", 1)
|
|
|
|
depends = depends.split()
|
|
|
|
depends.pop(0) # remove the .c file
|
|
|
|
depends = list(
|
|
|
|
# Don't create dependency on non-existing files
|
|
|
|
# (e.g. when they were renamed since last build)
|
|
|
|
filter(
|
|
|
|
lambda file: file.exists(),
|
|
|
|
(env.File(f"#{path}") for path in depends),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return depends
|
|
|
|
|
|
|
|
|
2023-10-30 15:17:30 +00:00
|
|
|
def _api_amalgam_emitter(target, source, env):
|
2022-09-14 16:11:38 +00:00
|
|
|
target.append(env.ChangeFileExtension(target[0], ".d"))
|
2022-09-19 12:39:00 +00:00
|
|
|
target.append(env.ChangeFileExtension(target[0], ".i.c"))
|
2022-09-14 16:11:38 +00:00
|
|
|
return target, source
|
|
|
|
|
|
|
|
|
2023-10-30 15:17:30 +00:00
|
|
|
def _api_amalgam_gen_origin_header(target, source, env):
|
2022-09-14 16:11:38 +00:00
|
|
|
mega_file = env.subst("${TARGET}.c", target=target[0])
|
|
|
|
with open(mega_file, "wt") as sdk_c:
|
2022-11-02 15:15:40 +00:00
|
|
|
sdk_c.write(
|
|
|
|
"\n".join(f"#include <{h.srcnode().path}>" for h in env["SDK_HEADERS"])
|
|
|
|
)
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
|
2022-10-12 16:12:05 +00:00
|
|
|
class SdkMeta:
|
2022-11-10 11:55:11 +00:00
|
|
|
MAP_FILE_SUBST = "SDK_MAP_FILE_SUBST"
|
|
|
|
|
2022-10-28 15:32:06 +00:00
|
|
|
def __init__(self, env, tree_builder: "SdkTreeBuilder"):
|
2022-10-12 16:12:05 +00:00
|
|
|
self.env = env
|
2022-10-28 15:32:06 +00:00
|
|
|
self.treebuilder = tree_builder
|
2022-10-12 16:12:05 +00:00
|
|
|
|
|
|
|
def save_to(self, json_manifest_path: str):
|
|
|
|
meta_contents = {
|
2022-10-28 15:32:06 +00:00
|
|
|
"sdk_symbols": self.treebuilder.build_sdk_file_path(
|
|
|
|
self.env["SDK_DEFINITION"].path
|
|
|
|
),
|
2022-10-12 16:12:05 +00:00
|
|
|
"cc_args": self._wrap_scons_vars("$CCFLAGS $_CCCOMCOM"),
|
|
|
|
"cpp_args": self._wrap_scons_vars("$CXXFLAGS $CCFLAGS $_CCCOMCOM"),
|
|
|
|
"linker_args": self._wrap_scons_vars("$LINKFLAGS"),
|
2022-11-02 15:15:40 +00:00
|
|
|
"linker_libs": self.env.subst("${LIBS}"),
|
|
|
|
"app_ep_subst": self.env.subst("${APP_ENTRY}"),
|
|
|
|
"sdk_path_subst": self.env.subst("${SDK_DIR_SUBST}"),
|
2022-11-10 11:55:11 +00:00
|
|
|
"map_file_subst": self.MAP_FILE_SUBST,
|
2022-11-02 15:15:40 +00:00
|
|
|
"hardware": self.env.subst("${TARGET_HW}"),
|
2022-10-12 16:12:05 +00:00
|
|
|
}
|
|
|
|
with open(json_manifest_path, "wt") as f:
|
|
|
|
json.dump(meta_contents, f, indent=4)
|
|
|
|
|
|
|
|
def _wrap_scons_vars(self, vars: str):
|
2022-11-02 15:15:40 +00:00
|
|
|
expanded_vars = self.env.subst(
|
|
|
|
vars,
|
2022-11-10 11:55:11 +00:00
|
|
|
target=Entry(self.MAP_FILE_SUBST),
|
2022-11-02 15:15:40 +00:00
|
|
|
)
|
2024-03-20 22:36:38 +00:00
|
|
|
return PosixPathWrapper.fixup_separators(expanded_vars)
|
2022-10-12 16:12:05 +00:00
|
|
|
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
class SdkTreeBuilder:
|
2022-10-28 15:32:06 +00:00
|
|
|
SDK_DIR_SUBST = "SDK_ROOT_DIR"
|
2022-11-02 15:15:40 +00:00
|
|
|
SDK_APP_EP_SUBST = "SDK_APP_EP_SUBST"
|
2023-04-06 02:44:37 +00:00
|
|
|
HEADER_EXTENSIONS = [".h", ".hpp"]
|
2022-10-28 15:32:06 +00:00
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
def __init__(self, env, target, source) -> None:
|
|
|
|
self.env = env
|
|
|
|
self.target = target
|
|
|
|
self.source = source
|
|
|
|
|
|
|
|
self.header_depends = []
|
|
|
|
self.header_dirs = []
|
|
|
|
|
2022-10-12 16:12:05 +00:00
|
|
|
self.target_sdk_dir_name = env.subst("f${TARGET_HW}_sdk")
|
|
|
|
self.sdk_root_dir = target[0].Dir(".")
|
|
|
|
self.sdk_deploy_dir = self.sdk_root_dir.Dir(self.target_sdk_dir_name)
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-11-02 15:15:40 +00:00
|
|
|
self.sdk_env = self.env.Clone(
|
|
|
|
APP_ENTRY=self.SDK_APP_EP_SUBST,
|
|
|
|
SDK_DIR_SUBST=self.SDK_DIR_SUBST,
|
|
|
|
)
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
def _parse_sdk_depends(self):
|
|
|
|
deps_file = self.source[0]
|
|
|
|
with open(deps_file.path, "rt") as deps_f:
|
|
|
|
lines = LogicalLines(deps_f).readlines()
|
|
|
|
_, depends = lines[0].split(":", 1)
|
|
|
|
self.header_depends = list(
|
2023-04-06 02:44:37 +00:00
|
|
|
filter(
|
|
|
|
lambda fname: any(map(fname.endswith, self.HEADER_EXTENSIONS)),
|
|
|
|
depends.split(),
|
|
|
|
),
|
2022-09-14 16:11:38 +00:00
|
|
|
)
|
2022-11-02 15:15:40 +00:00
|
|
|
self.header_depends.append(self.sdk_env.subst("${LINKER_SCRIPT_PATH}"))
|
|
|
|
self.header_depends.append(self.sdk_env.subst("${SDK_DEFINITION}"))
|
2022-09-14 16:11:38 +00:00
|
|
|
self.header_dirs = sorted(
|
|
|
|
set(map(os.path.normpath, map(os.path.dirname, self.header_depends)))
|
|
|
|
)
|
|
|
|
|
|
|
|
def _generate_sdk_meta(self):
|
2022-11-02 15:15:40 +00:00
|
|
|
filtered_paths = ["."]
|
2022-09-14 16:11:38 +00:00
|
|
|
full_fw_paths = list(
|
|
|
|
map(
|
|
|
|
os.path.normpath,
|
2022-11-02 15:15:40 +00:00
|
|
|
(
|
|
|
|
self.sdk_env.Dir(inc_dir).relpath
|
|
|
|
for inc_dir in self.sdk_env["CPPPATH"]
|
|
|
|
),
|
2022-09-14 16:11:38 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
sdk_dirs = ", ".join(f"'{dir}'" for dir in self.header_dirs)
|
2022-10-28 15:32:06 +00:00
|
|
|
filtered_paths.extend(
|
2022-11-02 15:15:40 +00:00
|
|
|
filter(lambda path: path in sdk_dirs, full_fw_paths),
|
2022-10-28 15:32:06 +00:00
|
|
|
)
|
2022-11-02 15:15:40 +00:00
|
|
|
filtered_paths = list(map(self.build_sdk_file_path, filtered_paths))
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-11-02 15:15:40 +00:00
|
|
|
self.sdk_env.Replace(
|
2022-10-28 15:32:06 +00:00
|
|
|
CPPPATH=filtered_paths,
|
|
|
|
ORIG_LINKER_SCRIPT_PATH=self.env["LINKER_SCRIPT_PATH"],
|
|
|
|
LINKER_SCRIPT_PATH=self.build_sdk_file_path("${ORIG_LINKER_SCRIPT_PATH}"),
|
|
|
|
)
|
2022-11-02 15:15:40 +00:00
|
|
|
meta = SdkMeta(self.sdk_env, self)
|
2022-10-12 16:12:05 +00:00
|
|
|
meta.save_to(self.target[0].path)
|
|
|
|
|
2022-10-28 15:32:06 +00:00
|
|
|
def build_sdk_file_path(self, orig_path: str) -> str:
|
2024-03-20 22:36:38 +00:00
|
|
|
return PosixPathWrapper.fix_path(
|
2022-11-10 11:55:11 +00:00
|
|
|
posixpath.normpath(
|
|
|
|
posixpath.join(
|
|
|
|
self.SDK_DIR_SUBST,
|
|
|
|
self.target_sdk_dir_name,
|
|
|
|
orig_path,
|
|
|
|
)
|
2022-10-28 15:32:06 +00:00
|
|
|
)
|
2022-11-10 11:55:11 +00:00
|
|
|
)
|
2022-10-28 15:32:06 +00:00
|
|
|
|
2022-10-12 16:12:05 +00:00
|
|
|
def emitter(self, target, source, env):
|
|
|
|
target_folder = target[0]
|
|
|
|
target = [target_folder.File("sdk.opts")]
|
|
|
|
return target, source
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
def _run_deploy_commands(self):
|
2022-09-14 16:11:38 +00:00
|
|
|
dirs_to_create = set(
|
2022-10-25 22:15:02 +00:00
|
|
|
self.sdk_deploy_dir.Dir(dirpath).path for dirpath in self.header_dirs
|
2022-09-14 16:11:38 +00:00
|
|
|
)
|
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
shutil.rmtree(self.sdk_root_dir.path, ignore_errors=False)
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
for sdkdir in dirs_to_create:
|
|
|
|
os.makedirs(sdkdir, exist_ok=True)
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
for header in self.header_depends:
|
|
|
|
shutil.copy2(header, self.sdk_deploy_dir.File(header).path)
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
def deploy_action(self):
|
|
|
|
self._parse_sdk_depends()
|
|
|
|
self._run_deploy_commands()
|
|
|
|
self._generate_sdk_meta()
|
2022-09-14 16:11:38 +00:00
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
|
2023-10-30 15:17:30 +00:00
|
|
|
def _deploy_sdk_header_tree_action(target, source, env):
|
2022-09-14 16:11:38 +00:00
|
|
|
sdk_tree = SdkTreeBuilder(env, target, source)
|
2022-10-25 22:15:02 +00:00
|
|
|
return sdk_tree.deploy_action()
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
|
2023-10-30 15:17:30 +00:00
|
|
|
def _deploy_sdk_header_tree_emitter(target, source, env):
|
2022-10-12 16:12:05 +00:00
|
|
|
sdk_tree = SdkTreeBuilder(env, target, source)
|
|
|
|
return sdk_tree.emitter(target, source, env)
|
|
|
|
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
def gen_sdk_data(sdk_cache: SdkCache):
|
|
|
|
api_def = []
|
|
|
|
api_def.extend(
|
|
|
|
(f"#include <{h.name}>" for h in sdk_cache.get_headers()),
|
|
|
|
)
|
2024-05-20 17:23:47 +00:00
|
|
|
api_def.append('#pragma GCC diagnostic ignored "-Wdeprecated-declarations"')
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
api_def.append(f"const int elf_api_version = {sdk_cache.version.as_int()};")
|
|
|
|
|
|
|
|
api_def.append(
|
|
|
|
"static constexpr auto elf_api_table = sort(create_array_t<sym_entry>("
|
|
|
|
)
|
|
|
|
|
|
|
|
api_lines = []
|
|
|
|
for fun_def in sdk_cache.get_functions():
|
|
|
|
api_lines.append(
|
|
|
|
f"API_METHOD({fun_def.name}, {fun_def.returns}, ({fun_def.params}))"
|
|
|
|
)
|
|
|
|
|
|
|
|
for var_def in sdk_cache.get_variables():
|
|
|
|
api_lines.append(f"API_VARIABLE({var_def.name}, {var_def.var_type })")
|
|
|
|
|
|
|
|
api_def.append(",\n".join(api_lines))
|
|
|
|
|
|
|
|
api_def.append("));")
|
|
|
|
return api_def
|
|
|
|
|
|
|
|
|
|
|
|
def _check_sdk_is_up2date(sdk_cache: SdkCache):
|
|
|
|
if not sdk_cache.is_buildable():
|
|
|
|
raise UserError(
|
2023-03-14 14:29:28 +00:00
|
|
|
"SDK version is not finalized, please review changes and re-run operation. See AppsOnSDCard.md for more details"
|
2022-09-14 16:11:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-10-30 15:17:30 +00:00
|
|
|
def _validate_api_cache(source, target, env):
|
2022-09-14 16:11:38 +00:00
|
|
|
# print(f"Generating SDK for {source[0]} to {target[0]}")
|
|
|
|
current_sdk = SdkCollector()
|
|
|
|
current_sdk.process_source_file_for_sdk(source[0].path)
|
|
|
|
for h in env["SDK_HEADERS"]:
|
2022-11-02 15:15:40 +00:00
|
|
|
current_sdk.add_header_to_sdk(pathlib.Path(h.srcnode().path).as_posix())
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
sdk_cache = SdkCache(target[0].path)
|
|
|
|
sdk_cache.validate_api(current_sdk.get_api())
|
|
|
|
sdk_cache.save()
|
|
|
|
_check_sdk_is_up2date(sdk_cache)
|
|
|
|
|
|
|
|
|
2023-10-30 15:17:30 +00:00
|
|
|
def _generate_api_table(source, target, env):
|
2022-09-14 16:11:38 +00:00
|
|
|
sdk_cache = SdkCache(source[0].path)
|
|
|
|
_check_sdk_is_up2date(sdk_cache)
|
|
|
|
|
|
|
|
api_def = gen_sdk_data(sdk_cache)
|
|
|
|
with open(target[0].path, "wt") as f:
|
|
|
|
f.write("\n".join(api_def))
|
|
|
|
|
|
|
|
|
|
|
|
def generate(env, **kw):
|
2022-10-25 22:15:02 +00:00
|
|
|
if not env["VERBOSE"]:
|
|
|
|
env.SetDefault(
|
2023-04-06 02:44:37 +00:00
|
|
|
SDK_AMALGAMATE_HEADER_COMSTR="\tAPIPREP\t${TARGET}",
|
|
|
|
SDK_AMALGAMATE_PP_COMSTR="\tAPIPP\t${TARGET}",
|
2022-10-25 22:15:02 +00:00
|
|
|
SDKSYM_UPDATER_COMSTR="\tSDKCHK\t${TARGET}",
|
2023-04-06 02:44:37 +00:00
|
|
|
APITABLE_GENERATOR_COMSTR="\tAPITBL\t${TARGET}",
|
|
|
|
SDKTREE_COMSTR="\tSDKTREE\t${TARGET}",
|
2022-10-25 22:15:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Filtering out things cxxheaderparser cannot handle
|
|
|
|
env.SetDefault(
|
|
|
|
SDK_PP_FLAGS=[
|
|
|
|
'-D"_Static_assert(x,y)="',
|
|
|
|
'-D"__asm__(x)="',
|
|
|
|
'-D"__attribute__(x)="',
|
|
|
|
"-Drestrict=",
|
|
|
|
"-D_Noreturn=",
|
|
|
|
"-D__restrict=",
|
|
|
|
"-D__extension__=",
|
|
|
|
"-D__inline=inline",
|
|
|
|
"-D__inline__=inline",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2022-10-12 16:12:05 +00:00
|
|
|
env.AddMethod(ProcessSdkDepends)
|
2022-09-14 16:11:38 +00:00
|
|
|
env.Append(
|
|
|
|
BUILDERS={
|
2023-04-06 02:44:37 +00:00
|
|
|
"ApiAmalgamator": Builder(
|
2023-10-30 15:17:30 +00:00
|
|
|
emitter=_api_amalgam_emitter,
|
2022-09-14 16:11:38 +00:00
|
|
|
action=[
|
|
|
|
Action(
|
2023-10-30 15:17:30 +00:00
|
|
|
_api_amalgam_gen_origin_header,
|
2023-04-06 02:44:37 +00:00
|
|
|
"$SDK_AMALGAMATE_HEADER_COMSTR",
|
2022-09-14 16:11:38 +00:00
|
|
|
),
|
|
|
|
Action(
|
2023-11-15 16:27:35 +00:00
|
|
|
[
|
|
|
|
[
|
|
|
|
"$CC",
|
|
|
|
"-o",
|
|
|
|
"$TARGET",
|
|
|
|
"-E",
|
|
|
|
"-P",
|
|
|
|
"$CCFLAGS",
|
|
|
|
"$_CCCOMCOM",
|
|
|
|
"$SDK_PP_FLAGS",
|
|
|
|
"-MMD",
|
|
|
|
"${TARGET}.c",
|
|
|
|
]
|
|
|
|
],
|
2023-04-06 02:44:37 +00:00
|
|
|
"$SDK_AMALGAMATE_PP_COMSTR",
|
2022-09-14 16:11:38 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
suffix=".i",
|
|
|
|
),
|
2023-04-06 02:44:37 +00:00
|
|
|
"SDKHeaderTreeExtractor": Builder(
|
2022-10-25 22:15:02 +00:00
|
|
|
action=Action(
|
2023-10-30 15:17:30 +00:00
|
|
|
_deploy_sdk_header_tree_action,
|
2023-04-06 02:44:37 +00:00
|
|
|
"$SDKTREE_COMSTR",
|
2022-10-25 22:15:02 +00:00
|
|
|
),
|
2023-10-30 15:17:30 +00:00
|
|
|
emitter=_deploy_sdk_header_tree_emitter,
|
2022-09-14 16:11:38 +00:00
|
|
|
src_suffix=".d",
|
|
|
|
),
|
2023-04-06 02:44:37 +00:00
|
|
|
"ApiTableValidator": Builder(
|
2022-09-14 16:11:38 +00:00
|
|
|
action=Action(
|
2023-10-30 15:17:30 +00:00
|
|
|
_validate_api_cache,
|
2022-09-14 16:11:38 +00:00
|
|
|
"$SDKSYM_UPDATER_COMSTR",
|
|
|
|
),
|
|
|
|
suffix=".csv",
|
|
|
|
src_suffix=".i",
|
|
|
|
),
|
2023-04-06 02:44:37 +00:00
|
|
|
"ApiSymbolTable": Builder(
|
2022-09-14 16:11:38 +00:00
|
|
|
action=Action(
|
2023-10-30 15:17:30 +00:00
|
|
|
_generate_api_table,
|
2023-04-06 02:44:37 +00:00
|
|
|
"$APITABLE_GENERATOR_COMSTR",
|
2022-09-14 16:11:38 +00:00
|
|
|
),
|
|
|
|
suffix=".h",
|
|
|
|
src_suffix=".csv",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def exists(env):
|
|
|
|
return True
|