[FL-3097] fbt, faploader: minimal app module implementation (#2420)

* fbt, faploader: minimal app module implementation
* faploader, libs: moved API hashtable core to flipper_application
* example: compound api
* lib: flipper_application: naming fixes, doxygen comments
* fbt: changed `requires` manifest field behavior for app extensions
* examples: refactored plugin apps; faploader: changed new API naming; fbt: changed PLUGIN app type meaning
* loader: dropped support for debug apps & plugin menus
* moved applications/plugins -> applications/external
* Restored x bit on chiplist_convert.py
* git: fixed free-dap submodule path
* pvs: updated submodule paths
* examples: example_advanced_plugins.c: removed potential memory leak on errors
* examples: example_plugins: refined requires
* fbt: not deploying app modules for debug/sample apps; extra validation for .PLUGIN-type apps
* apps: removed cdefines for external apps
* fbt: moved ext app path definition
* fbt: reworked fap_dist handling; f18: synced api_symbols.csv
* fbt: removed resources_paths for extapps
* scripts: reworked storage
* scripts: reworked runfap.py & selfupdate.py to use new api
* wip: fal runner
* fbt: moved file packaging into separate module
* scripts: storage: fixes
* scripts: storage: minor fixes for new api
* fbt: changed internal artifact storage details for external apps
* scripts: storage: additional fixes and better error reporting; examples: using APP_DATA_PATH()
* fbt, scripts: reworked launch_app to deploy plugins; moved old runfap.py to distfap.py
* fbt: extra check for plugins descriptors
* fbt: additional checks in emitter
* fbt: better info message on SDK rebuild
* scripts: removed requirements.txt
* loader: removed remnants of plugins & debug menus
* post-review fixes
This commit is contained in:
hedger
2023-03-14 18:29:28 +04:00
committed by GitHub
parent 4bd3dca16f
commit 53435579b3
376 changed files with 2041 additions and 1036 deletions

View File

@@ -12,13 +12,13 @@ class FlipperAppType(Enum):
SERVICE = "Service"
SYSTEM = "System"
APP = "App"
PLUGIN = "Plugin"
DEBUG = "Debug"
ARCHIVE = "Archive"
SETTINGS = "Settings"
STARTUP = "StartupHook"
EXTERNAL = "External"
METAPACKAGE = "Package"
PLUGIN = "Plugin"
@dataclass
@@ -69,12 +69,22 @@ class FlipperApplication:
fap_private_libs: List[Library] = field(default_factory=list)
fap_file_assets: Optional[str] = None
# Internally used by fbt
_appmanager: Optional["AppManager"] = None
_appdir: Optional[object] = None
_apppath: Optional[str] = None
_plugins: List["FlipperApplication"] = field(default_factory=list)
def supports_hardware_target(self, target: str):
return target in self.targets or "all" in self.targets
@property
def is_default_deployable(self):
return self.apptype != FlipperAppType.DEBUG and self.fap_category != "Examples"
def __post_init__(self):
if self.apptype == FlipperAppType.PLUGIN:
self.stack_size = 0
class AppManager:
def __init__(self):
@@ -94,6 +104,23 @@ class AppManager:
return app
return None
def _validate_app_params(self, *args, **kw):
apptype = kw.get("apptype")
if apptype == FlipperAppType.PLUGIN:
if kw.get("stack_size"):
raise FlipperManifestException(
f"Plugin {kw.get('appid')} cannot have stack (did you mean FlipperAppType.EXTERNAL?)"
)
if not kw.get("requires"):
raise FlipperManifestException(
f"Plugin {kw.get('appid')} must have 'requires' in manifest"
)
# Harmless - cdefines for external apps are meaningless
# if apptype == FlipperAppType.EXTERNAL and kw.get("cdefines"):
# raise FlipperManifestException(
# f"External app {kw.get('appid')} must not have 'cdefines' in manifest"
# )
def load_manifest(self, app_manifest_path: str, app_dir_node: object):
if not os.path.exists(app_manifest_path):
raise FlipperManifestException(
@@ -105,12 +132,14 @@ class AppManager:
def App(*args, **kw):
nonlocal app_manifests
self._validate_app_params(*args, **kw)
app_manifests.append(
FlipperApplication(
*args,
**kw,
_appdir=app_dir_node,
_apppath=os.path.dirname(app_manifest_path),
_appmanager=self,
),
)
@@ -155,7 +184,6 @@ class AppBuildset:
FlipperAppType.SERVICE,
FlipperAppType.SYSTEM,
FlipperAppType.APP,
FlipperAppType.PLUGIN,
FlipperAppType.DEBUG,
FlipperAppType.ARCHIVE,
FlipperAppType.SETTINGS,
@@ -182,6 +210,7 @@ class AppBuildset:
self._check_conflicts()
self._check_unsatisfied() # unneeded?
self._check_target_match()
self._group_plugins()
self.apps = sorted(
list(map(self.appmgr.get, self.appnames)),
key=lambda app: app.appid,
@@ -260,6 +289,18 @@ class AppBuildset:
f"Apps incompatible with target {self.hw_target}: {', '.join(incompatible)}"
)
def _group_plugins(self):
known_extensions = self.get_apps_of_type(FlipperAppType.PLUGIN, all_known=True)
for extension_app in known_extensions:
for parent_app_id in extension_app.requires:
try:
parent_app = self.appmgr.get(parent_app_id)
parent_app._plugins.append(extension_app)
except FlipperManifestException as e:
self._writer(
f"Module {extension_app.appid} has unknown parent {parent_app_id}"
)
def get_apps_cdefs(self):
cdefs = set()
for app in self.apps:
@@ -301,7 +342,6 @@ class ApplicationsCGenerator:
FlipperAppType.SERVICE: ("FlipperApplication", "FLIPPER_SERVICES"),
FlipperAppType.SYSTEM: ("FlipperApplication", "FLIPPER_SYSTEM_APPS"),
FlipperAppType.APP: ("FlipperApplication", "FLIPPER_APPS"),
FlipperAppType.PLUGIN: ("FlipperApplication", "FLIPPER_PLUGINS"),
FlipperAppType.DEBUG: ("FlipperApplication", "FLIPPER_DEBUG_APPS"),
FlipperAppType.SETTINGS: ("FlipperApplication", "FLIPPER_SETTINGS_APPS"),
FlipperAppType.STARTUP: ("FlipperOnStartHook", "FLIPPER_ON_SYSTEM_START"),

108
scripts/fbt/fapassets.py Normal file
View File

@@ -0,0 +1,108 @@
import os
import hashlib
import struct
from typing import TypedDict
class File(TypedDict):
path: str
size: int
content_path: str
class Dir(TypedDict):
path: str
class FileBundler:
"""
u32 magic
u32 version
u32 dirs_count
u32 files_count
u32 signature_size
u8[] signature
Dirs:
u32 dir_name length
u8[] dir_name
Files:
u32 file_name length
u8[] file_name
u32 file_content_size
u8[] file_content
"""
def __init__(self, directory_path: str):
self.directory_path = directory_path
self.file_list: list[File] = []
self.directory_list: list[Dir] = []
self._gather()
def _gather(self):
for root, dirs, files in os.walk(self.directory_path):
for file_info in files:
file_path = os.path.join(root, file_info)
file_size = os.path.getsize(file_path)
self.file_list.append(
{
"path": os.path.relpath(file_path, self.directory_path),
"size": file_size,
"content_path": file_path,
}
)
for dir_info in dirs:
dir_path = os.path.join(root, dir_info)
# dir_size = sum(
# os.path.getsize(os.path.join(dir_path, f)) for f in os.listdir(dir_path)
# )
self.directory_list.append(
{
"path": os.path.relpath(dir_path, self.directory_path),
}
)
self.file_list.sort(key=lambda f: f["path"])
self.directory_list.sort(key=lambda d: d["path"])
def export(self, target_path: str):
self._md5_hash = hashlib.md5()
with open(target_path, "wb") as f:
# Write header magic and version
f.write(struct.pack("<II", 0x4F4C5A44, 0x01))
# Write dirs count
f.write(struct.pack("<I", len(self.directory_list)))
# Write files count
f.write(struct.pack("<I", len(self.file_list)))
md5_hash_size = len(self._md5_hash.digest())
# write signature size and null signature, we'll fill it in later
f.write(struct.pack("<I", md5_hash_size))
signature_offset = f.tell()
f.write(b"\x00" * md5_hash_size)
self._write_contents(f)
f.seek(signature_offset)
f.write(self._md5_hash.digest())
def _write_contents(self, f):
for dir_info in self.directory_list:
f.write(struct.pack("<I", len(dir_info["path"]) + 1))
f.write(dir_info["path"].encode("ascii") + b"\x00")
self._md5_hash.update(dir_info["path"].encode("ascii") + b"\x00")
# Write files
for file_info in self.file_list:
f.write(struct.pack("<I", len(file_info["path"]) + 1))
f.write(file_info["path"].encode("ascii") + b"\x00")
f.write(struct.pack("<I", file_info["size"]))
self._md5_hash.update(file_info["path"].encode("ascii") + b"\x00")
with open(file_info["content_path"], "rb") as content_file:
content = content_file.read()
f.write(content)
self._md5_hash.update(content)