firmware.scons 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. Import("ENV", "fw_build_meta")
  2. from SCons.Errors import UserError
  3. from SCons.Node import FS
  4. import itertools
  5. from fbt_extra.util import (
  6. should_gen_cdb_and_link_dir,
  7. link_elf_dir_as_latest,
  8. )
  9. # Building initial C environment for libs
  10. env = ENV.Clone(
  11. tools=[
  12. ("compilation_db", {"COMPILATIONDB_COMSTR": "\tCDB\t${TARGET}"}),
  13. "fwbin",
  14. "fbt_apps",
  15. "pvsstudio",
  16. ],
  17. COMPILATIONDB_USE_ABSPATH=False,
  18. BUILD_DIR=fw_build_meta["build_dir"],
  19. IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
  20. FW_FLAVOR=fw_build_meta["flavor"],
  21. LIB_DIST_DIR=fw_build_meta["build_dir"].Dir("lib"),
  22. LINT_SOURCES=[
  23. Dir("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. "-Og" if ENV["LIB_DEBUG"] else "-Os",
  42. ],
  43. "CPPDEFINES": [
  44. "NDEBUG",
  45. "FURI_DEBUG" if ENV["LIB_DEBUG"] else "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. SDK_APISYMS=None,
  71. _APP_ICONS=None,
  72. )
  73. def ApplyLibFlags(env):
  74. flags_to_apply = env["FW_LIB_OPTS"].get(
  75. env.get("FW_LIB_NAME"),
  76. env["FW_LIB_OPTS"]["Default"],
  77. )
  78. # print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
  79. env.MergeFlags(flags_to_apply)
  80. env.AddMethod(ApplyLibFlags)
  81. Export("env")
  82. if env["IS_BASE_FIRMWARE"]:
  83. env.Append(
  84. FIRMWARE_BUILD_CFG="firmware",
  85. RAM_EXEC=False,
  86. )
  87. else:
  88. env.Append(
  89. FIRMWARE_BUILD_CFG="updater",
  90. RAM_EXEC=True,
  91. CPPDEFINES=[
  92. "FURI_RAM_EXEC",
  93. ],
  94. )
  95. # Invoke child SConscripts to populate global `env` + build their own part of the code
  96. lib_targets = env.BuildModules(
  97. [
  98. "lib",
  99. "assets",
  100. "firmware",
  101. "furi",
  102. ],
  103. )
  104. # Now, env is fully set up with everything to build apps
  105. fwenv = env.Clone(FW_ARTIFACTS=[])
  106. fw_artifacts = fwenv["FW_ARTIFACTS"]
  107. # Set up additional app-specific build flags
  108. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  109. # Set up app configuration
  110. if env["IS_BASE_FIRMWARE"]:
  111. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  112. else:
  113. fwenv.Append(APPS=["updater"])
  114. if extra_int_apps := GetOption("extra_int_apps"):
  115. fwenv.Append(APPS=extra_int_apps.split(","))
  116. for app_dir, _ in env["APPDIRS"]:
  117. app_dir_node = env.Dir("#").Dir(app_dir)
  118. for entry in app_dir_node.glob("*"):
  119. if isinstance(entry, FS.Dir) and not str(entry).startswith("."):
  120. fwenv.LoadAppManifest(entry)
  121. fwenv.PrepareApplicationsBuild()
  122. # Build external apps + configure SDK
  123. if env["IS_BASE_FIRMWARE"]:
  124. fwenv.SetDefault(FBT_FAP_DEBUG_ELF_ROOT="${BUILD_DIR}/.extapps")
  125. fwenv["FW_EXTAPPS"] = SConscript(
  126. "site_scons/extapps.scons",
  127. exports={"ENV": fwenv},
  128. )
  129. fw_artifacts.append(fwenv["FW_EXTAPPS"].sdk_tree)
  130. # Add preprocessor definitions for current set of apps
  131. fwenv.Append(
  132. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  133. )
  134. # Build applications.c for selected services & apps
  135. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  136. apps_c = fwenv.ApplicationsC(
  137. "applications/applications.c",
  138. [Value(fwenv["APPS"]), Value(fwenv["LOADER_AUTOSTART"])],
  139. )
  140. # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
  141. for app_dir, _ in env["APPDIRS"]:
  142. app_dir_node = env.Dir("#").Dir(app_dir)
  143. fwenv.Depends(apps_c, app_dir_node.glob("*/application.fam"))
  144. # Sanity check - certain external apps are using features that are not available in base firmware
  145. if advanced_faps := list(
  146. filter(
  147. lambda app: app.fap_extbuild or app.fap_private_libs or app.fap_icon_assets,
  148. fwenv["APPBUILD"].get_builtin_apps(),
  149. )
  150. ):
  151. raise UserError(
  152. "An Application that is using fap-specific features cannot be built into base firmware."
  153. f" Offending app(s): {', '.join(app.appid for app in advanced_faps)}"
  154. )
  155. sources = [apps_c]
  156. # Gather sources only from app folders in current configuration
  157. sources.extend(
  158. itertools.chain.from_iterable(
  159. fwenv.GlobRecursive(source_type, appdir.relpath, exclude="lib")
  160. for appdir, source_type in fwenv["APPBUILD"].get_builtin_app_folders()
  161. )
  162. )
  163. # Debug
  164. # print(fwenv.Dump())
  165. # Full firmware definition
  166. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  167. "${FIRMWARE_BUILD_CFG}",
  168. sources,
  169. LIBS=[
  170. "print",
  171. "flipper${TARGET_HW}",
  172. "furi",
  173. "freertos",
  174. "stm32cubewb",
  175. "hwdrivers",
  176. "fatfs",
  177. "littlefs",
  178. "subghz",
  179. "flipperformat",
  180. "toolbox",
  181. "nfc",
  182. "microtar",
  183. "usb_stm32",
  184. "st25rfal002",
  185. "infrared",
  186. "appframe",
  187. "assets",
  188. "misc",
  189. "mbedtls",
  190. "lfrfid",
  191. "flipper_application",
  192. # 2nd round
  193. "flipperformat",
  194. "toolbox",
  195. ],
  196. )
  197. # Firmware depends on everything child builders returned
  198. Depends(fwelf, lib_targets)
  199. # Output extra details after building firmware
  200. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  201. AddPostAction(
  202. fwelf,
  203. Action(
  204. '${PYTHON3} "${BIN_SIZE_SCRIPT}" elf ${TARGET}',
  205. "Firmware size",
  206. ),
  207. )
  208. # Produce extra firmware files
  209. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  210. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  211. AddPostAction(
  212. fwbin,
  213. Action('@${PYTHON3} "${BIN_SIZE_SCRIPT}" bin ${TARGET}'),
  214. )
  215. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  216. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  217. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  218. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  219. fw_artifacts.extend(
  220. [
  221. fwhex,
  222. fwbin,
  223. fwdfu,
  224. fwenv["FW_VERSION_JSON"],
  225. ]
  226. )
  227. fwcdb = fwenv.CompilationDatabase()
  228. # without filtering, both updater & firmware commands would be generated in same file
  229. fwenv.Replace(
  230. COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"),
  231. COMPILATIONDB_SRCPATH_FILTER="*.c*",
  232. )
  233. AlwaysBuild(fwcdb)
  234. Precious(fwcdb)
  235. NoClean(fwcdb)
  236. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  237. pvscheck = fwenv.PVSCheck("pvsreport.log", fwcdb)
  238. Depends(
  239. pvscheck,
  240. [
  241. fwenv["FW_VERSION_JSON"],
  242. fwenv["FW_ASSETS_HEADERS"],
  243. fwenv["SDK_APISYMS"],
  244. fwenv["_APP_ICONS"],
  245. ],
  246. )
  247. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_pvscheck", pvscheck)
  248. AlwaysBuild(pvscheck)
  249. Precious(pvscheck)
  250. pvsreport = fwenv.PVSReport(None, pvscheck, REPORT_DIR=Dir("pvsreport"))
  251. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_pvs", pvsreport)
  252. AlwaysBuild(pvsreport)
  253. # If current configuration was explicitly requested, generate compilation database
  254. # and link its directory as build/latest
  255. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  256. fw_artifacts.append(fwcdb)
  257. # Adding as a phony target, so folder link is updated even if elf didn't change
  258. link_dir_command = fwenv.PhonyTarget(
  259. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  260. Action(
  261. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  262. None,
  263. ),
  264. source=fwelf,
  265. )
  266. fw_artifacts.append(link_dir_command)
  267. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  268. Return("fwenv")