firmware.scons 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. Import("ENV", "fw_build_meta")
  2. from SCons.Errors import UserError
  3. import itertools
  4. from fbt_extra.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 env["IS_BASE_FIRMWARE"]:
  81. env.Append(
  82. FIRMWARE_BUILD_CFG="firmware",
  83. RAM_EXEC=False,
  84. )
  85. else:
  86. env.Append(
  87. FIRMWARE_BUILD_CFG="updater",
  88. RAM_EXEC=True,
  89. CPPDEFINES=[
  90. "FURI_RAM_EXEC",
  91. ],
  92. )
  93. # Invoke child SConscripts to populate global `env` + build their own part of the code
  94. lib_targets = env.BuildModules(
  95. [
  96. "lib",
  97. "assets",
  98. "firmware",
  99. "furi",
  100. ],
  101. )
  102. # Now, env is fully set up with everything to build apps
  103. fwenv = env.Clone()
  104. # Set up additional app-specific build flags
  105. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  106. # Set up app configuration
  107. if env["IS_BASE_FIRMWARE"]:
  108. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  109. else:
  110. fwenv.Append(APPS=["updater"])
  111. if extra_int_apps := GetOption("extra_int_apps"):
  112. fwenv.Append(APPS=extra_int_apps.split(","))
  113. if fwenv["FAP_EXAMPLES"]:
  114. fwenv.Append(APPDIRS=[("applications/examples", False)])
  115. fwenv.LoadApplicationManifests()
  116. fwenv.PrepareApplicationsBuild()
  117. # Build external apps
  118. if env["IS_BASE_FIRMWARE"]:
  119. extapps = fwenv["FW_EXTAPPS"] = SConscript(
  120. "site_scons/extapps.scons", exports={"ENV": fwenv}
  121. )
  122. # Add preprocessor definitions for current set of apps
  123. fwenv.Append(
  124. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  125. )
  126. # Build applications.c for selected services & apps
  127. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  128. apps_c = fwenv.ApplicationsC(
  129. "applications/applications.c",
  130. [Value(fwenv["APPS"]), Value(fwenv["LOADER_AUTOSTART"])],
  131. )
  132. # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
  133. for app_dir, _ in env["APPDIRS"]:
  134. app_dir_node = env.Dir("#").Dir(app_dir)
  135. fwenv.Depends(apps_c, app_dir_node.glob("*/application.fam"))
  136. # Sanity check - certain external apps are using features that are not available in base firmware
  137. if advanced_faps := list(
  138. filter(
  139. lambda app: app.fap_extbuild or app.fap_private_libs or app.fap_icon_assets,
  140. fwenv["APPBUILD"].get_builtin_apps(),
  141. )
  142. ):
  143. raise UserError(
  144. "An Application that is using fap-specific features cannot be built into base firmware."
  145. f" Offending app(s): {', '.join(app.appid for app in advanced_faps)}"
  146. )
  147. sources = [apps_c]
  148. # Gather sources only from app folders in current configuration
  149. sources.extend(
  150. itertools.chain.from_iterable(
  151. fwenv.GlobRecursive(source_type, appdir.relpath, exclude="lib")
  152. for appdir, source_type in fwenv["APPBUILD"].get_builtin_app_folders()
  153. )
  154. )
  155. fwenv.AppendUnique(
  156. LINKFLAGS=[
  157. "-specs=nano.specs",
  158. "-specs=nosys.specs",
  159. "-Wl,--gc-sections",
  160. "-Wl,--undefined=uxTopUsedPriority",
  161. "-Wl,--wrap,_malloc_r",
  162. "-Wl,--wrap,_free_r",
  163. "-Wl,--wrap,_calloc_r",
  164. "-Wl,--wrap,_realloc_r",
  165. "-n",
  166. "-Xlinker",
  167. "-Map=${TARGET}.map",
  168. ],
  169. )
  170. # Debug
  171. # print(fwenv.Dump())
  172. # Full firmware definition
  173. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  174. "${FIRMWARE_BUILD_CFG}",
  175. sources,
  176. LIBS=[
  177. "print",
  178. "flipper${TARGET_HW}",
  179. "furi",
  180. "freertos",
  181. "stm32cubewb",
  182. "hwdrivers",
  183. "fatfs",
  184. "littlefs",
  185. "subghz",
  186. "flipperformat",
  187. "toolbox",
  188. "nfc",
  189. "microtar",
  190. "usb_stm32",
  191. "st25rfal002",
  192. "infrared",
  193. "appframe",
  194. "assets",
  195. "misc",
  196. "mbedtls",
  197. "lfrfid",
  198. "flipper_application",
  199. # 2nd round
  200. "flipperformat",
  201. "toolbox",
  202. ],
  203. )
  204. # Firmware depends on everything child builders returned
  205. Depends(fwelf, lib_targets)
  206. # Output extra details after building firmware
  207. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  208. AddPostAction(
  209. fwelf,
  210. Action('${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" elf ${TARGET}', "Firmware size"),
  211. )
  212. # Produce extra firmware files
  213. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  214. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  215. AddPostAction(
  216. fwbin,
  217. Action('@${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" bin ${TARGET}'),
  218. )
  219. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  220. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  221. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  222. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  223. fw_artifacts = fwenv["FW_ARTIFACTS"] = [
  224. fwhex,
  225. fwbin,
  226. fwdfu,
  227. fwenv["FW_VERSION_JSON"],
  228. ]
  229. fwcdb = fwenv.CompilationDatabase()
  230. # without filtering, both updater & firmware commands would be generated in same file
  231. fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
  232. AlwaysBuild(fwcdb)
  233. Precious(fwcdb)
  234. NoClean(fwcdb)
  235. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  236. # If current configuration was explicitly requested, generate compilation database
  237. # and link its directory as build/latest
  238. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  239. fw_artifacts.append(fwcdb)
  240. # Adding as a phony target, so folder link is updated even if elf didn't change
  241. link_dir_command = fwenv.PhonyTarget(
  242. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  243. Action(
  244. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  245. None,
  246. ),
  247. source=fwelf,
  248. )
  249. fw_artifacts.append(link_dir_command)
  250. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  251. if fwenv["IS_BASE_FIRMWARE"]:
  252. sdk_source = fwenv.SDKPrebuilder(
  253. "sdk_origin",
  254. # Deps on root SDK headers and generated files
  255. (fwenv["SDK_HEADERS"], fwenv["FW_ASSETS_HEADERS"]),
  256. )
  257. # Extra deps on headers included in deeper levels
  258. Depends(sdk_source, fwenv.ProcessSdkDepends("sdk_origin.d"))
  259. fwenv["SDK_DIR"] = fwenv.Dir("sdk")
  260. sdk_tree = fwenv.SDKTree(fwenv["SDK_DIR"], "sdk_origin")
  261. fw_artifacts.append(sdk_tree)
  262. # AlwaysBuild(sdk_tree)
  263. Alias("sdk_tree", sdk_tree)
  264. sdk_apicheck = fwenv.SDKSymUpdater(fwenv["SDK_DEFINITION"], "sdk_origin")
  265. Precious(sdk_apicheck)
  266. NoClean(sdk_apicheck)
  267. AlwaysBuild(sdk_apicheck)
  268. Alias("sdk_check", sdk_apicheck)
  269. sdk_apisyms = fwenv.SDKSymGenerator(
  270. "assets/compiled/symbols.h", fwenv["SDK_DEFINITION"]
  271. )
  272. Alias("api_syms", sdk_apisyms)
  273. if fwenv["FORCE"]:
  274. fwenv.AlwaysBuild(sdk_source, sdk_tree, sdk_apicheck, sdk_apisyms)
  275. Return("fwenv")