firmware.scons 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. Import("ENV", "fw_build_meta")
  2. from SCons.Errors import UserError
  3. import itertools
  4. from fbt.util import (
  5. should_gen_cdb_and_link_dir,
  6. link_elf_dir_as_latest,
  7. )
  8. # Building initial C environment for libs
  9. env = ENV.Clone(
  10. tools=[
  11. ("compilation_db", {"COMPILATIONDB_COMSTR": "\tCDB\t${TARGET}"}),
  12. "fwbin",
  13. "fbt_apps",
  14. "fbt_sdk",
  15. ],
  16. COMPILATIONDB_USE_ABSPATH=False,
  17. BUILD_DIR=fw_build_meta["build_dir"],
  18. IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
  19. FW_FLAVOR=fw_build_meta["flavor"],
  20. PLUGIN_ELF_DIR="${BUILD_DIR}",
  21. LIB_DIST_DIR="${BUILD_DIR}/lib",
  22. LINT_SOURCES=[
  23. "applications",
  24. ],
  25. LIBPATH=[
  26. "${LIB_DIST_DIR}",
  27. ],
  28. CPPPATH=[
  29. "#/furi",
  30. *(f"#/{app_dir[0]}" for app_dir in ENV["APPDIRS"] if app_dir[1]),
  31. "#/firmware/targets/f${TARGET_HW}/ble_glue",
  32. "#/firmware/targets/f${TARGET_HW}/fatfs",
  33. "#/firmware/targets/f${TARGET_HW}/furi_hal",
  34. "#/firmware/targets/f${TARGET_HW}/Inc",
  35. "#/firmware/targets/furi_hal_include",
  36. ],
  37. # Specific flags for building libraries - always do optimized builds
  38. FW_LIB_OPTS={
  39. "Default": {
  40. "CCFLAGS": [
  41. "-Os",
  42. ],
  43. "CPPDEFINES": [
  44. "NDEBUG",
  45. "FURI_NDEBUG",
  46. ],
  47. # You can add other entries named after libraries
  48. # If they are present, they have precedence over Default
  49. },
  50. # for furi_check to respect build type
  51. "furi": {
  52. "CCFLAGS": [
  53. "-Os",
  54. ],
  55. "CPPDEFINES": [
  56. "NDEBUG",
  57. "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
  58. ],
  59. },
  60. "flipper_application": {
  61. "CCFLAGS": [
  62. "-Og",
  63. ],
  64. "CPPDEFINES": [
  65. "NDEBUG",
  66. "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
  67. ],
  68. },
  69. },
  70. )
  71. def ApplyLibFlags(env):
  72. flags_to_apply = env["FW_LIB_OPTS"].get(
  73. env.get("FW_LIB_NAME"),
  74. env["FW_LIB_OPTS"]["Default"],
  75. )
  76. # print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
  77. env.MergeFlags(flags_to_apply)
  78. env.AddMethod(ApplyLibFlags)
  79. Export("env")
  80. if not env["VERBOSE"]:
  81. env.SetDefault(
  82. HEXCOMSTR="\tHEX\t${TARGET}",
  83. BINCOMSTR="\tBIN\t${TARGET}",
  84. DFUCOMSTR="\tDFU\t${TARGET}",
  85. SDK_PREGEN_COMSTR="\tPREGEN\t${TARGET}",
  86. SDK_COMSTR="\tSDKSRC\t${TARGET}",
  87. SDKSYM_UPDATER_COMSTR="\tSDKCHK\t${TARGET}",
  88. SDKSYM_GENERATOR_COMSTR="\tSDKSYM\t${TARGET}",
  89. APPMETA_COMSTR="\tAPPMETA\t${TARGET}",
  90. APPMETAEMBED_COMSTR="\tFAP\t${TARGET}",
  91. APPCHECK_COMSTR="\tAPPCHK\t${SOURCE}",
  92. )
  93. if env["IS_BASE_FIRMWARE"]:
  94. env.Append(
  95. FIRMWARE_BUILD_CFG="firmware",
  96. RAM_EXEC=False,
  97. )
  98. else:
  99. env.Append(
  100. FIRMWARE_BUILD_CFG="updater",
  101. RAM_EXEC=True,
  102. CPPDEFINES=[
  103. "FURI_RAM_EXEC",
  104. ],
  105. )
  106. # Invoke child SConscripts to populate global `env` + build their own part of the code
  107. lib_targets = env.BuildModules(
  108. [
  109. "lib",
  110. "assets",
  111. "firmware",
  112. "furi",
  113. ],
  114. )
  115. # Now, env is fully set up with everything to build apps
  116. fwenv = env.Clone()
  117. # Set up additional app-specific build flags
  118. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  119. # Set up app configuration
  120. if env["IS_BASE_FIRMWARE"]:
  121. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  122. else:
  123. fwenv.Append(APPS=["updater"])
  124. if extra_int_apps := GetOption("extra_int_apps"):
  125. fwenv.Append(APPS=extra_int_apps.split(","))
  126. fwenv.LoadApplicationManifests()
  127. fwenv.PrepareApplicationsBuild()
  128. # Build external apps
  129. extapps = fwenv["FW_EXTAPPS"] = SConscript(
  130. "site_scons/extapps.scons", exports={"ENV": fwenv}
  131. )
  132. # Add preprocessor definitions for current set of apps
  133. fwenv.Append(
  134. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  135. )
  136. # Build applications.c for selected services & apps
  137. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  138. apps_c = fwenv.ApplicationsC(
  139. "applications/applications.c",
  140. [Value(fwenv["APPS"]), Value(fwenv["LOADER_AUTOSTART"])],
  141. )
  142. # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
  143. for app_dir, _ in env["APPDIRS"]:
  144. app_dir_node = env.Dir("#").Dir(app_dir)
  145. fwenv.Depends(apps_c, app_dir_node.glob("*/application.fam"))
  146. # Sanity check - certain external apps are using features that are not available in base firmware
  147. if advanced_faps := list(
  148. filter(
  149. lambda app: app.fap_extbuild or app.fap_private_libs or app.fap_icon_assets,
  150. fwenv["APPBUILD"].get_builtin_apps(),
  151. )
  152. ):
  153. raise UserError(
  154. "An Application that is using fap-specific features cannot be built into base firmware."
  155. f" Offending app(s): {', '.join(app.appid for app in advanced_faps)}"
  156. )
  157. sources = [apps_c]
  158. # Gather sources only from app folders in current configuration
  159. sources.extend(
  160. itertools.chain.from_iterable(
  161. fwenv.GlobRecursive(source_type, appdir.relpath, exclude="lib")
  162. for appdir, source_type in fwenv["APPBUILD"].get_builtin_app_folders()
  163. )
  164. )
  165. fwenv.AppendUnique(
  166. LINKFLAGS=[
  167. "-specs=nano.specs",
  168. "-specs=nosys.specs",
  169. "-Wl,--gc-sections",
  170. "-Wl,--undefined=uxTopUsedPriority",
  171. "-Wl,--wrap,_malloc_r",
  172. "-Wl,--wrap,_free_r",
  173. "-Wl,--wrap,_calloc_r",
  174. "-Wl,--wrap,_realloc_r",
  175. "-n",
  176. "-Xlinker",
  177. "-Map=${TARGET}.map",
  178. ],
  179. )
  180. # Debug
  181. # print(fwenv.Dump())
  182. # Full firmware definition
  183. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  184. "${FIRMWARE_BUILD_CFG}",
  185. sources,
  186. LIBS=[
  187. "print",
  188. "flipper${TARGET_HW}",
  189. "furi",
  190. "freertos",
  191. "stm32cubewb",
  192. "hwdrivers",
  193. "fatfs",
  194. "littlefs",
  195. "subghz",
  196. "flipperformat",
  197. "toolbox",
  198. "nfc",
  199. "microtar",
  200. "usb_stm32",
  201. "st25rfal002",
  202. "infrared",
  203. "appframe",
  204. "assets",
  205. "misc",
  206. "mbedtls",
  207. "lfrfid",
  208. "flipper_application",
  209. # 2nd round
  210. "flipperformat",
  211. "toolbox",
  212. ],
  213. )
  214. # Firmware depends on everything child builders returned
  215. Depends(fwelf, lib_targets)
  216. # Output extra details after building firmware
  217. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  218. AddPostAction(
  219. fwelf,
  220. Action('${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" elf ${TARGET}', "Firmware size"),
  221. )
  222. # Produce extra firmware files
  223. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  224. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  225. AddPostAction(
  226. fwbin,
  227. Action('@${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" bin ${TARGET}'),
  228. )
  229. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  230. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  231. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  232. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  233. fw_artifacts = fwenv["FW_ARTIFACTS"] = [
  234. fwhex,
  235. fwbin,
  236. fwdfu,
  237. fwenv["FW_VERSION_JSON"],
  238. ]
  239. fwcdb = fwenv.CompilationDatabase()
  240. # without filtering, both updater & firmware commands would be generated in same file
  241. fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
  242. AlwaysBuild(fwcdb)
  243. Precious(fwcdb)
  244. NoClean(fwcdb)
  245. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  246. # If current configuration was explicitly requested, generate compilation database
  247. # and link its directory as build/latest
  248. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  249. fw_artifacts.append(fwcdb)
  250. # Adding as a phony target, so folder link is updated even if elf didn't change
  251. link_dir_command = fwenv.PhonyTarget(
  252. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  253. Action(
  254. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  255. None,
  256. ),
  257. source=fwelf,
  258. )
  259. fw_artifacts.append(link_dir_command)
  260. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  261. if fwenv["IS_BASE_FIRMWARE"]:
  262. sdk_source = fwenv.SDKPrebuilder(
  263. "sdk_origin",
  264. [],
  265. # Filtering out things cxxheaderparser cannot handle
  266. SDK_PP_FLAGS=[
  267. '-D"_Static_assert(x,y)="',
  268. '-D"__asm__(x)="',
  269. '-D"__attribute__(x)="',
  270. "-Drestrict=",
  271. "-D_Noreturn=",
  272. "-D__restrict=",
  273. "-D__extension__=",
  274. "-D__inline=inline",
  275. "-D__inline__=inline",
  276. ],
  277. )
  278. Depends(sdk_source, (fwenv["SDK_HEADERS"], fwenv["FW_ASSETS_HEADERS"]))
  279. sdk_tree = fwenv.SDKTree("sdk/sdk.opts", "sdk_origin")
  280. AlwaysBuild(sdk_tree)
  281. Alias("sdk_tree", sdk_tree)
  282. sdk_apicheck = fwenv.SDKSymUpdater(fwenv.subst("$SDK_DEFINITION"), "sdk_origin")
  283. Precious(sdk_apicheck)
  284. NoClean(sdk_apicheck)
  285. AlwaysBuild(sdk_apicheck)
  286. Alias("sdk_check", sdk_apicheck)
  287. sdk_apisyms = fwenv.SDKSymGenerator(
  288. "assets/compiled/symbols.h", fwenv.subst("$SDK_DEFINITION")
  289. )
  290. Alias("api_syms", sdk_apisyms)
  291. if fwenv["FORCE"]:
  292. fwenv.AlwaysBuild(sdk_source, sdk_tree, sdk_apicheck, sdk_apisyms)
  293. Return("fwenv")