firmware.scons 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. Import("ENV", "fw_build_meta")
  2. import os
  3. # Building initial C environment for libs
  4. env = ENV.Clone(
  5. tools=["compilation_db", "fwbin", "openocd", "fbt_apps"],
  6. COMPILATIONDB_USE_ABSPATH=True,
  7. BUILD_DIR=fw_build_meta["build_dir"],
  8. IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
  9. FW_FLAVOR=fw_build_meta["flavor"],
  10. PLUGIN_ELF_DIR="${BUILD_DIR}",
  11. LIB_DIST_DIR="${BUILD_DIR}/lib",
  12. LINT_SOURCES=[
  13. "applications",
  14. ],
  15. LIBPATH=[
  16. "${LIB_DIST_DIR}",
  17. ],
  18. CPPPATH=[
  19. "#/core",
  20. "#/applications",
  21. "#/firmware/targets/f${TARGET_HW}/ble_glue",
  22. "#/firmware/targets/f${TARGET_HW}/fatfs",
  23. "#/firmware/targets/f${TARGET_HW}/furi_hal",
  24. "#/firmware/targets/f${TARGET_HW}/Inc",
  25. "#/firmware/targets/furi_hal_include",
  26. ],
  27. # Specific flags for building libraries - always do optimized builds
  28. FW_LIB_OPTS={
  29. "Default": {
  30. "CCFLAGS": [
  31. "-Os",
  32. ],
  33. "CPPDEFINES": [
  34. "NDEBUG",
  35. "FURI_NDEBUG",
  36. ],
  37. # You can add other entries named after libraries
  38. # If they are present, they have precedence over Default
  39. },
  40. # for furi_check to respect build type
  41. "core": {
  42. "CCFLAGS": [
  43. "-Os",
  44. ],
  45. "CPPDEFINES": [
  46. "NDEBUG",
  47. "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
  48. ],
  49. },
  50. },
  51. )
  52. def ApplyLibFlags(env):
  53. flags_to_apply = env["FW_LIB_OPTS"].get(
  54. env.get("FW_LIB_NAME"),
  55. env["FW_LIB_OPTS"]["Default"],
  56. )
  57. # print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
  58. env.MergeFlags(flags_to_apply)
  59. env.AddMethod(ApplyLibFlags)
  60. Export("env")
  61. if not env["VERBOSE"]:
  62. env.SetDefault(
  63. HEXCOMSTR="\tHEX\t${TARGET}",
  64. BINCOMSTR="\tBIN\t${TARGET}",
  65. DFUCOMSTR="\tDFU\t${TARGET}",
  66. OOCDCOMSTR="\tFLASH\t${SOURCE}",
  67. )
  68. if fw_build_meta["type"] == "updater":
  69. env.Append(
  70. FIRMWARE_BUILD_CFG="updater",
  71. RAM_EXEC=True,
  72. CPPDEFINES=[
  73. "FURI_RAM_EXEC",
  74. ],
  75. )
  76. else:
  77. env.Append(
  78. FIRMWARE_BUILD_CFG="firmware",
  79. RAM_EXEC=False,
  80. )
  81. # print(env.Dump())
  82. # Invoke child SCopscripts to populate global `env` + build their own part of the code
  83. lib_targets = env.BuildModules(
  84. [
  85. "lib",
  86. "assets",
  87. "firmware",
  88. "core",
  89. ],
  90. )
  91. # Now, env is fully set up with everything to build apps
  92. fwenv = env.Clone()
  93. # Set up additional app-specific build flags
  94. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  95. # Set up app configuration
  96. if env["IS_BASE_FIRMWARE"]:
  97. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  98. else:
  99. fwenv.Append(APPS=["updater"])
  100. if extra_int_apps := GetOption("extra_int_apps"):
  101. for extra_int_app in extra_int_apps.split(","):
  102. fwenv.Append(APPS=[extra_int_app])
  103. fwenv.LoadApplicationManifests()
  104. fwenv.PrepareApplicationsBuild()
  105. # Build external apps
  106. extapps = SConscript("applications/extapps.scons", exports={"ENV": fwenv})
  107. # Add preprocessor definitions for current set of apps
  108. fwenv.AppendUnique(
  109. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  110. )
  111. # Build applications.c for selected services & apps
  112. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  113. apps_c = fwenv.ApplicationsC(
  114. "applications/applications.c",
  115. Value(fwenv["APPS"]),
  116. )
  117. sources = [apps_c]
  118. # Gather sources only from app folders from current configuration
  119. for app_folder in fwenv["APPBUILD"].get_builtin_app_folders():
  120. sources += fwenv.GlobRecursive("*.c*", os.path.join("applications", app_folder))
  121. fwenv.AppendUnique(
  122. LINKFLAGS=[
  123. "-specs=nano.specs",
  124. "-specs=nosys.specs",
  125. "-Wl,--start-group",
  126. "-lstdc++",
  127. "-lsupc++",
  128. "-Wl,--end-group",
  129. "-Wl,--gc-sections",
  130. "-Wl,--undefined=uxTopUsedPriority",
  131. "-Wl,--wrap,_malloc_r",
  132. "-Wl,--wrap,_free_r",
  133. "-Wl,--wrap,_calloc_r",
  134. "-Wl,--wrap,_realloc_r",
  135. "-u",
  136. "_printf_float",
  137. "-n",
  138. ],
  139. )
  140. # Debug
  141. # print(fwenv.Dump())
  142. # Full firmware definition
  143. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  144. "${FIRMWARE_BUILD_CFG}",
  145. sources,
  146. LIBS=[
  147. "flipper${TARGET_HW}",
  148. "core",
  149. "freertos",
  150. "stm32cubewb",
  151. "hwdrivers",
  152. "fatfs",
  153. "littlefs",
  154. "subghz",
  155. "flipperformat",
  156. "toolbox",
  157. "microtar",
  158. "usb_stm32",
  159. "st25rfal002",
  160. "infrared",
  161. "appframe",
  162. "assets",
  163. "misc",
  164. # 2nd round
  165. "flipperformat",
  166. "toolbox",
  167. ],
  168. )
  169. # Make it depend on everything child builders returned
  170. Depends(fwelf, lib_targets)
  171. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  172. AddPostAction(fwelf, Action("@$SIZECOM"))
  173. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  174. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  175. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  176. # Default(dfu)
  177. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  178. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  179. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  180. # Additional FW-related pseudotargets
  181. flash = fwenv["FW_FLASH"] = fwenv.OOCDFlashCommand(
  182. "${FIRMWARE_BUILD_CFG}",
  183. OPENOCD_COMMAND='-c "program ${SOURCE.posix} reset exit ${IMAGE_BASE_ADDRESS}"',
  184. )
  185. if fwenv["FORCE"]:
  186. AlwaysBuild(flash)
  187. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_flash", flash)
  188. if fwenv["IS_BASE_FIRMWARE"]:
  189. Alias("flash", flash)
  190. # Compile DB generation
  191. fwcdb = fwenv["FW_CDB"] = fwenv.CompilationDatabase("compile_commands.json")
  192. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  193. artifacts = [fwhex, fwbin, fwdfu, env["FW_VERSION_JSON"]]
  194. fwenv["FW_ARTIFACTS"] = artifacts
  195. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", artifacts)
  196. Return("fwenv")