firmware.scons 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. ],
  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(FW_ARTIFACTS=[])
  104. fw_artifacts = fwenv["FW_ARTIFACTS"]
  105. # Set up additional app-specific build flags
  106. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  107. # Set up app configuration
  108. if env["IS_BASE_FIRMWARE"]:
  109. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  110. else:
  111. fwenv.Append(APPS=["updater"])
  112. if extra_int_apps := GetOption("extra_int_apps"):
  113. fwenv.Append(APPS=extra_int_apps.split(","))
  114. if fwenv["FAP_EXAMPLES"]:
  115. fwenv.Append(APPDIRS=[("applications/examples", False)])
  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
  123. if env["IS_BASE_FIRMWARE"]:
  124. extapps = fwenv["FW_EXTAPPS"] = SConscript(
  125. "site_scons/extapps.scons", exports={"ENV": fwenv}
  126. )
  127. fw_artifacts.append(extapps["sdk_tree"])
  128. # Add preprocessor definitions for current set of apps
  129. fwenv.Append(
  130. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  131. )
  132. # Build applications.c for selected services & apps
  133. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  134. apps_c = fwenv.ApplicationsC(
  135. "applications/applications.c",
  136. [Value(fwenv["APPS"]), Value(fwenv["LOADER_AUTOSTART"])],
  137. )
  138. # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
  139. for app_dir, _ in env["APPDIRS"]:
  140. app_dir_node = env.Dir("#").Dir(app_dir)
  141. fwenv.Depends(apps_c, app_dir_node.glob("*/application.fam"))
  142. # Sanity check - certain external apps are using features that are not available in base firmware
  143. if advanced_faps := list(
  144. filter(
  145. lambda app: app.fap_extbuild or app.fap_private_libs or app.fap_icon_assets,
  146. fwenv["APPBUILD"].get_builtin_apps(),
  147. )
  148. ):
  149. raise UserError(
  150. "An Application that is using fap-specific features cannot be built into base firmware."
  151. f" Offending app(s): {', '.join(app.appid for app in advanced_faps)}"
  152. )
  153. sources = [apps_c]
  154. # Gather sources only from app folders in current configuration
  155. sources.extend(
  156. itertools.chain.from_iterable(
  157. fwenv.GlobRecursive(source_type, appdir.relpath, exclude="lib")
  158. for appdir, source_type in fwenv["APPBUILD"].get_builtin_app_folders()
  159. )
  160. )
  161. # Debug
  162. # print(fwenv.Dump())
  163. # Full firmware definition
  164. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  165. "${FIRMWARE_BUILD_CFG}",
  166. sources,
  167. LIBS=[
  168. "print",
  169. "flipper${TARGET_HW}",
  170. "furi",
  171. "freertos",
  172. "stm32cubewb",
  173. "hwdrivers",
  174. "fatfs",
  175. "littlefs",
  176. "subghz",
  177. "flipperformat",
  178. "toolbox",
  179. "nfc",
  180. "microtar",
  181. "usb_stm32",
  182. "st25rfal002",
  183. "infrared",
  184. "appframe",
  185. "assets",
  186. "misc",
  187. "mbedtls",
  188. "lfrfid",
  189. "flipper_application",
  190. # 2nd round
  191. "flipperformat",
  192. "toolbox",
  193. ],
  194. )
  195. # Firmware depends on everything child builders returned
  196. Depends(fwelf, lib_targets)
  197. # Output extra details after building firmware
  198. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  199. AddPostAction(
  200. fwelf,
  201. Action(
  202. '${PYTHON3} "${BIN_SIZE_SCRIPT}" elf ${TARGET}',
  203. "Firmware size",
  204. ),
  205. )
  206. # Produce extra firmware files
  207. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  208. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  209. AddPostAction(
  210. fwbin,
  211. Action('@${PYTHON3} "${BIN_SIZE_SCRIPT}" bin ${TARGET}'),
  212. )
  213. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  214. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  215. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  216. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  217. fw_artifacts.extend(
  218. [
  219. fwhex,
  220. fwbin,
  221. fwdfu,
  222. fwenv["FW_VERSION_JSON"],
  223. ]
  224. )
  225. fwcdb = fwenv.CompilationDatabase()
  226. # without filtering, both updater & firmware commands would be generated in same file
  227. fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
  228. AlwaysBuild(fwcdb)
  229. Precious(fwcdb)
  230. NoClean(fwcdb)
  231. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  232. # If current configuration was explicitly requested, generate compilation database
  233. # and link its directory as build/latest
  234. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  235. fw_artifacts.append(fwcdb)
  236. # Adding as a phony target, so folder link is updated even if elf didn't change
  237. link_dir_command = fwenv.PhonyTarget(
  238. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  239. Action(
  240. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  241. None,
  242. ),
  243. source=fwelf,
  244. )
  245. fw_artifacts.append(link_dir_command)
  246. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  247. Return("fwenv")