firmware.scons 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. # Debug
  156. # print(fwenv.Dump())
  157. # Full firmware definition
  158. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  159. "${FIRMWARE_BUILD_CFG}",
  160. sources,
  161. LIBS=[
  162. "print",
  163. "flipper${TARGET_HW}",
  164. "furi",
  165. "freertos",
  166. "stm32cubewb",
  167. "hwdrivers",
  168. "fatfs",
  169. "littlefs",
  170. "subghz",
  171. "flipperformat",
  172. "toolbox",
  173. "nfc",
  174. "microtar",
  175. "usb_stm32",
  176. "st25rfal002",
  177. "infrared",
  178. "appframe",
  179. "assets",
  180. "misc",
  181. "mbedtls",
  182. "lfrfid",
  183. "flipper_application",
  184. # 2nd round
  185. "flipperformat",
  186. "toolbox",
  187. ],
  188. )
  189. # Firmware depends on everything child builders returned
  190. Depends(fwelf, lib_targets)
  191. # Output extra details after building firmware
  192. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  193. AddPostAction(
  194. fwelf,
  195. Action('${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" elf ${TARGET}', "Firmware size"),
  196. )
  197. # Produce extra firmware files
  198. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  199. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  200. AddPostAction(
  201. fwbin,
  202. Action('@${PYTHON3} "${ROOT_DIR}/scripts/fwsize.py" bin ${TARGET}'),
  203. )
  204. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  205. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  206. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  207. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  208. fw_artifacts = fwenv["FW_ARTIFACTS"] = [
  209. fwhex,
  210. fwbin,
  211. fwdfu,
  212. fwenv["FW_VERSION_JSON"],
  213. ]
  214. fwcdb = fwenv.CompilationDatabase()
  215. # without filtering, both updater & firmware commands would be generated in same file
  216. fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
  217. AlwaysBuild(fwcdb)
  218. Precious(fwcdb)
  219. NoClean(fwcdb)
  220. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  221. # If current configuration was explicitly requested, generate compilation database
  222. # and link its directory as build/latest
  223. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  224. fw_artifacts.append(fwcdb)
  225. # Adding as a phony target, so folder link is updated even if elf didn't change
  226. link_dir_command = fwenv.PhonyTarget(
  227. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  228. Action(
  229. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  230. None,
  231. ),
  232. source=fwelf,
  233. )
  234. fw_artifacts.append(link_dir_command)
  235. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  236. if fwenv["IS_BASE_FIRMWARE"]:
  237. sdk_source = fwenv.SDKPrebuilder(
  238. "sdk_origin",
  239. # Deps on root SDK headers and generated files
  240. (fwenv["SDK_HEADERS"], fwenv["FW_ASSETS_HEADERS"]),
  241. )
  242. # Extra deps on headers included in deeper levels
  243. Depends(sdk_source, fwenv.ProcessSdkDepends("sdk_origin.d"))
  244. fwenv["SDK_DIR"] = fwenv.Dir("sdk")
  245. sdk_tree = fwenv.SDKTree(fwenv["SDK_DIR"], "sdk_origin")
  246. fw_artifacts.append(sdk_tree)
  247. # AlwaysBuild(sdk_tree)
  248. Alias("sdk_tree", sdk_tree)
  249. sdk_apicheck = fwenv.SDKSymUpdater(fwenv["SDK_DEFINITION"], "sdk_origin")
  250. Precious(sdk_apicheck)
  251. NoClean(sdk_apicheck)
  252. AlwaysBuild(sdk_apicheck)
  253. Alias("sdk_check", sdk_apicheck)
  254. sdk_apisyms = fwenv.SDKSymGenerator(
  255. "assets/compiled/symbols.h", fwenv["SDK_DEFINITION"]
  256. )
  257. Alias("api_syms", sdk_apisyms)
  258. if fwenv["FORCE"]:
  259. fwenv.AlwaysBuild(sdk_source, sdk_tree, sdk_apicheck, sdk_apisyms)
  260. Return("fwenv")