| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- Import("ENV", "fw_build_meta")
- from SCons.Errors import UserError
- import itertools
- from fbt_extra.util import (
- should_gen_cdb_and_link_dir,
- link_elf_dir_as_latest,
- )
- # Building initial C environment for libs
- env = ENV.Clone(
- tools=[
- ("compilation_db", {"COMPILATIONDB_COMSTR": "\tCDB\t${TARGET}"}),
- "fwbin",
- "fbt_apps",
- "fbt_sdk",
- ],
- COMPILATIONDB_USE_ABSPATH=False,
- BUILD_DIR=fw_build_meta["build_dir"],
- IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
- FW_FLAVOR=fw_build_meta["flavor"],
- PLUGIN_ELF_DIR="${BUILD_DIR}",
- LIB_DIST_DIR="${BUILD_DIR}/lib",
- LINT_SOURCES=[
- "applications",
- ],
- LIBPATH=[
- "${LIB_DIST_DIR}",
- ],
- CPPPATH=[
- "#/furi",
- *(f"#/{app_dir[0]}" for app_dir in ENV["APPDIRS"] if app_dir[1]),
- "#/firmware/targets/f${TARGET_HW}/ble_glue",
- "#/firmware/targets/f${TARGET_HW}/fatfs",
- "#/firmware/targets/f${TARGET_HW}/furi_hal",
- "#/firmware/targets/f${TARGET_HW}/Inc",
- "#/firmware/targets/furi_hal_include",
- ],
- # Specific flags for building libraries - always do optimized builds
- FW_LIB_OPTS={
- "Default": {
- "CCFLAGS": [
- "-Os",
- ],
- "CPPDEFINES": [
- "NDEBUG",
- "FURI_NDEBUG",
- ],
- # You can add other entries named after libraries
- # If they are present, they have precedence over Default
- },
- # for furi_check to respect build type
- "furi": {
- "CCFLAGS": [
- "-Os",
- ],
- "CPPDEFINES": [
- "NDEBUG",
- "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
- ],
- },
- "flipper_application": {
- "CCFLAGS": [
- "-Og",
- ],
- "CPPDEFINES": [
- "NDEBUG",
- "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
- ],
- },
- },
- )
- def ApplyLibFlags(env):
- flags_to_apply = env["FW_LIB_OPTS"].get(
- env.get("FW_LIB_NAME"),
- env["FW_LIB_OPTS"]["Default"],
- )
- # print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
- env.MergeFlags(flags_to_apply)
- env.AddMethod(ApplyLibFlags)
- Export("env")
- if not env["VERBOSE"]:
- env.SetDefault(
- HEXCOMSTR="\tHEX\t${TARGET}",
- BINCOMSTR="\tBIN\t${TARGET}",
- DFUCOMSTR="\tDFU\t${TARGET}",
- SDK_PREGEN_COMSTR="\tPREGEN\t${TARGET}",
- SDK_COMSTR="\tSDKSRC\t${TARGET}",
- SDKSYM_UPDATER_COMSTR="\tSDKCHK\t${TARGET}",
- SDKSYM_GENERATOR_COMSTR="\tSDKSYM\t${TARGET}",
- APPMETA_COMSTR="\tAPPMETA\t${TARGET}",
- APPMETAEMBED_COMSTR="\tFAP\t${TARGET}",
- APPCHECK_COMSTR="\tAPPCHK\t${SOURCE}",
- )
- if env["IS_BASE_FIRMWARE"]:
- env.Append(
- FIRMWARE_BUILD_CFG="firmware",
- RAM_EXEC=False,
- )
- else:
- env.Append(
- FIRMWARE_BUILD_CFG="updater",
- RAM_EXEC=True,
- CPPDEFINES=[
- "FURI_RAM_EXEC",
- ],
- )
- # Invoke child SConscripts to populate global `env` + build their own part of the code
- lib_targets = env.BuildModules(
- [
- "lib",
- "assets",
- "firmware",
- "furi",
- ],
- )
- # Now, env is fully set up with everything to build apps
- fwenv = env.Clone()
- # Set up additional app-specific build flags
- SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
- # Set up app configuration
- if env["IS_BASE_FIRMWARE"]:
- fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
- else:
- fwenv.Append(APPS=["updater"])
- if extra_int_apps := GetOption("extra_int_apps"):
- fwenv.Append(APPS=extra_int_apps.split(","))
- if fwenv["FAP_EXAMPLES"]:
- fwenv.Append(APPDIRS=[("applications/examples", False)])
- fwenv.LoadApplicationManifests()
- fwenv.PrepareApplicationsBuild()
- # Build external apps
- extapps = fwenv["FW_EXTAPPS"] = SConscript(
- "site_scons/extapps.scons", exports={"ENV": fwenv}
- )
- # Add preprocessor definitions for current set of apps
- fwenv.Append(
- CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
- )
- # Build applications.c for selected services & apps
- # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
- apps_c = fwenv.ApplicationsC(
- "applications/applications.c",
- [Value(fwenv["APPS"]), Value(fwenv["LOADER_AUTOSTART"])],
- )
- # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
- for app_dir, _ in env["APPDIRS"]:
- app_dir_node = env.Dir("#").Dir(app_dir)
- fwenv.Depends(apps_c, app_dir_node.glob("*/application.fam"))
- # Sanity check - certain external apps are using features that are not available in base firmware
- if advanced_faps := list(
- filter(
- lambda app: app.fap_extbuild or app.fap_private_libs or app.fap_icon_assets,
- fwenv["APPBUILD"].get_builtin_apps(),
- )
- ):
- raise UserError(
- "An Application that is using fap-specific features cannot be built into base firmware."
- f" Offending app(s): {', '.join(app.appid for app in advanced_faps)}"
- )
- sources = [apps_c]
- # Gather sources only from app folders in current configuration
- sources.extend(
- itertools.chain.from_iterable(
- fwenv.GlobRecursive(source_type, appdir.relpath, exclude="lib")
- for appdir, source_type in fwenv["APPBUILD"].get_builtin_app_folders()
- )
- )
- fwenv.AppendUnique(
- LINKFLAGS=[
- "-specs=nano.specs",
- "-specs=nosys.specs",
- "-Wl,--gc-sections",
- "-Wl,--undefined=uxTopUsedPriority",
- "-Wl,--wrap,_malloc_r",
- "-Wl,--wrap,_free_r",
- "-Wl,--wrap,_calloc_r",
- "-Wl,--wrap,_realloc_r",
- "-n",
- "-Xlinker",
- "-Map=${TARGET}.map",
- ],
- )
- # Debug
- # print(fwenv.Dump())
- # Full firmware definition
- fwelf = fwenv["FW_ELF"] = fwenv.Program(
- "${FIRMWARE_BUILD_CFG}",
- sources,
- LIBS=[
- "print",
- "flipper${TARGET_HW}",
- "furi",
- "freertos",
- "stm32cubewb",
- "hwdrivers",
- "fatfs",
- "littlefs",
- "subghz",
- "flipperformat",
- "toolbox",
- "nfc",
- "microtar",
- "usb_stm32",
- "st25rfal002",
- "infrared",
- "appframe",
- "assets",
- "misc",
- "mbedtls",
- "lfrfid",
- "flipper_application",
- # 2nd round
- "flipperformat",
- "toolbox",
- ],
- )
- # Firmware depends on everything child builders returned
- Depends(fwelf, lib_targets)
- # Output extra details after building firmware
- AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
- AddPostAction(
- fwelf,
- Action('${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" elf ${TARGET}', "Firmware size"),
- )
- # Produce extra firmware files
- fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
- fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
- AddPostAction(
- fwbin,
- Action('@${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" bin ${TARGET}'),
- )
- fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
- Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
- fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
- Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
- fw_artifacts = fwenv["FW_ARTIFACTS"] = [
- fwhex,
- fwbin,
- fwdfu,
- fwenv["FW_VERSION_JSON"],
- ]
- fwcdb = fwenv.CompilationDatabase()
- # without filtering, both updater & firmware commands would be generated in same file
- fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
- AlwaysBuild(fwcdb)
- Precious(fwcdb)
- NoClean(fwcdb)
- Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
- # If current configuration was explicitly requested, generate compilation database
- # and link its directory as build/latest
- if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
- fw_artifacts.append(fwcdb)
- # Adding as a phony target, so folder link is updated even if elf didn't change
- link_dir_command = fwenv.PhonyTarget(
- fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
- Action(
- lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
- None,
- ),
- source=fwelf,
- )
- fw_artifacts.append(link_dir_command)
- Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
- if fwenv["IS_BASE_FIRMWARE"]:
- sdk_source = fwenv.SDKPrebuilder(
- "sdk_origin",
- [],
- # Filtering out things cxxheaderparser cannot handle
- 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",
- ],
- )
- # Depends(sdk_source, (fwenv["SDK_HEADERS"], fwenv["FW_ASSETS_HEADERS"]))
- Depends(sdk_source, fwenv.ProcessSdkDepends("sdk_origin.d"))
- fwenv["SDK_DIR"] = fwenv.Dir("sdk")
- sdk_tree = fwenv.SDKTree(fwenv["SDK_DIR"], "sdk_origin")
- fw_artifacts.append(sdk_tree)
- # AlwaysBuild(sdk_tree)
- Alias("sdk_tree", sdk_tree)
- sdk_apicheck = fwenv.SDKSymUpdater(fwenv["SDK_DEFINITION"], "sdk_origin")
- Precious(sdk_apicheck)
- NoClean(sdk_apicheck)
- AlwaysBuild(sdk_apicheck)
- Alias("sdk_check", sdk_apicheck)
- sdk_apisyms = fwenv.SDKSymGenerator(
- "assets/compiled/symbols.h", fwenv["SDK_DEFINITION"]
- )
- Alias("api_syms", sdk_apisyms)
- if fwenv["FORCE"]:
- fwenv.AlwaysBuild(sdk_source, sdk_tree, sdk_apicheck, sdk_apisyms)
- Return("fwenv")
|