firmware.scons 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. },
  41. )
  42. def ApplyLibFlags(env):
  43. flags_to_apply = env["FW_LIB_OPTS"].get(
  44. env.get("FW_LIB_NAME"),
  45. env["FW_LIB_OPTS"]["Default"],
  46. )
  47. # print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
  48. env.MergeFlags(flags_to_apply)
  49. env.AddMethod(ApplyLibFlags)
  50. Export("env")
  51. if not env["VERBOSE"]:
  52. env.SetDefault(
  53. HEXCOMSTR="\tHEX\t${TARGET}",
  54. BINCOMSTR="\tBIN\t${TARGET}",
  55. DFUCOMSTR="\tDFU\t${TARGET}",
  56. OOCDCOMSTR="\tFLASH\t${SOURCE}",
  57. )
  58. if fw_build_meta["type"] == "updater":
  59. env.Append(
  60. FIRMWARE_BUILD_CFG="updater",
  61. RAM_EXEC=True,
  62. CPPDEFINES=[
  63. "FURI_RAM_EXEC",
  64. ],
  65. )
  66. else:
  67. env.Append(
  68. FIRMWARE_BUILD_CFG="firmware",
  69. RAM_EXEC=False,
  70. )
  71. # print(env.Dump())
  72. # Invoke child SCopscripts to populate global `env` + build their own part of the code
  73. lib_targets = env.BuildModules(
  74. [
  75. "lib",
  76. "assets",
  77. "firmware",
  78. "core",
  79. ],
  80. )
  81. # Now, env is fully set up with everything to build apps
  82. fwenv = env.Clone()
  83. # Set up additional app-specific build flags
  84. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  85. # Set up app configuration
  86. if env["IS_BASE_FIRMWARE"]:
  87. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  88. else:
  89. fwenv.Append(APPS=["updater"])
  90. if extra_int_apps := GetOption("extra_int_apps"):
  91. for extra_int_app in extra_int_apps.split(","):
  92. fwenv.Append(APPS=[extra_int_app])
  93. fwenv.LoadApplicationManifests()
  94. fwenv.PrepareApplicationsBuild()
  95. # Build external apps
  96. extapps = SConscript("applications/extapps.scons", exports={"ENV": fwenv})
  97. # Add preprocessor definitions for current set of apps
  98. fwenv.AppendUnique(
  99. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  100. )
  101. # Build applications.c for selected services & apps
  102. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  103. apps_c = fwenv.ApplicationsC(
  104. "applications/applications.c",
  105. Value(fwenv["APPS"]),
  106. )
  107. sources = [apps_c]
  108. # Gather sources only from app folders from current configuration
  109. for app_folder in fwenv["APPBUILD"].get_builtin_app_folders():
  110. sources += fwenv.GlobRecursive("*.c*", os.path.join("applications", app_folder))
  111. fwenv.AppendUnique(
  112. LINKFLAGS=[
  113. "-specs=nano.specs",
  114. "-specs=nosys.specs",
  115. "-Wl,--start-group",
  116. "-lstdc++",
  117. "-lsupc++",
  118. "-Wl,--end-group",
  119. "-Wl,--gc-sections",
  120. "-Wl,--undefined=uxTopUsedPriority",
  121. "-Wl,--wrap,_malloc_r",
  122. "-Wl,--wrap,_free_r",
  123. "-Wl,--wrap,_calloc_r",
  124. "-Wl,--wrap,_realloc_r",
  125. "-u",
  126. "_printf_float",
  127. "-n",
  128. ],
  129. )
  130. # Debug
  131. # print(fwenv.Dump())
  132. # Full firmware definition
  133. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  134. "${FIRMWARE_BUILD_CFG}",
  135. sources,
  136. LIBS=[
  137. "flipper${TARGET_HW}",
  138. "core",
  139. "freertos",
  140. "stm32cubewb",
  141. "hwdrivers",
  142. "fatfs",
  143. "littlefs",
  144. "subghz",
  145. "flipperformat",
  146. "toolbox",
  147. "microtar",
  148. "usb_stm32",
  149. "st25rfal002",
  150. "infrared",
  151. "appframe",
  152. "assets",
  153. "misc",
  154. # 2nd round
  155. "flipperformat",
  156. "toolbox",
  157. ],
  158. )
  159. # Make it depend on everything child builders returned
  160. Depends(fwelf, lib_targets)
  161. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  162. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  163. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  164. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  165. # Default(dfu)
  166. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  167. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  168. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  169. # Additional FW-related pseudotargets
  170. flash = fwenv["FW_FLASH"] = fwenv.OOCDFlashCommand(
  171. "${FIRMWARE_BUILD_CFG}",
  172. OPENOCD_COMMAND='-c "program ${SOURCE.posix} reset exit ${IMAGE_BASE_ADDRESS}"',
  173. )
  174. if fwenv["FORCE"]:
  175. AlwaysBuild(flash)
  176. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_flash", flash)
  177. if fwenv["IS_BASE_FIRMWARE"]:
  178. Alias("flash", flash)
  179. # Compile DB generation
  180. fwcdb = fwenv["FW_CDB"] = fwenv.CompilationDatabase("compile_commands.json")
  181. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  182. artifacts = [fwhex, fwbin, fwdfu, env["FW_VERSION_JSON"]]
  183. fwenv["FW_ARTIFACTS"] = artifacts
  184. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", artifacts)
  185. Return("fwenv")