firmware.scons 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. Import("ENV", "fw_build_meta")
  2. import os
  3. from fbt.util import (
  4. should_gen_cdb_and_link_dir,
  5. link_elf_dir_as_latest,
  6. )
  7. # Building initial C environment for libs
  8. env = ENV.Clone(
  9. tools=[
  10. ("compilation_db", {"COMPILATIONDB_COMSTR": "\tCDB\t${TARGET}"}),
  11. "fwbin",
  12. "fbt_apps",
  13. ],
  14. COMPILATIONDB_USE_ABSPATH=False,
  15. BUILD_DIR=fw_build_meta["build_dir"],
  16. IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
  17. FW_FLAVOR=fw_build_meta["flavor"],
  18. PLUGIN_ELF_DIR="${BUILD_DIR}",
  19. LIB_DIST_DIR="${BUILD_DIR}/lib",
  20. LINT_SOURCES=[
  21. "applications",
  22. ],
  23. LIBPATH=[
  24. "${LIB_DIST_DIR}",
  25. ],
  26. CPPPATH=[
  27. "#/furi",
  28. "#/applications",
  29. "#/firmware/targets/f${TARGET_HW}/ble_glue",
  30. "#/firmware/targets/f${TARGET_HW}/fatfs",
  31. "#/firmware/targets/f${TARGET_HW}/furi_hal",
  32. "#/firmware/targets/f${TARGET_HW}/Inc",
  33. "#/firmware/targets/furi_hal_include",
  34. ],
  35. # Specific flags for building libraries - always do optimized builds
  36. FW_LIB_OPTS={
  37. "Default": {
  38. "CCFLAGS": [
  39. "-Os",
  40. ],
  41. "CPPDEFINES": [
  42. "NDEBUG",
  43. "FURI_NDEBUG",
  44. ],
  45. # You can add other entries named after libraries
  46. # If they are present, they have precedence over Default
  47. },
  48. # for furi_check to respect build type
  49. "furi": {
  50. "CCFLAGS": [
  51. "-Os",
  52. ],
  53. "CPPDEFINES": [
  54. "NDEBUG",
  55. "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
  56. ],
  57. },
  58. },
  59. )
  60. def ApplyLibFlags(env):
  61. flags_to_apply = env["FW_LIB_OPTS"].get(
  62. env.get("FW_LIB_NAME"),
  63. env["FW_LIB_OPTS"]["Default"],
  64. )
  65. # print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
  66. env.MergeFlags(flags_to_apply)
  67. env.AddMethod(ApplyLibFlags)
  68. Export("env")
  69. if not env["VERBOSE"]:
  70. env.SetDefault(
  71. HEXCOMSTR="\tHEX\t${TARGET}",
  72. BINCOMSTR="\tBIN\t${TARGET}",
  73. DFUCOMSTR="\tDFU\t${TARGET}",
  74. )
  75. if env["IS_BASE_FIRMWARE"]:
  76. env.Append(
  77. FIRMWARE_BUILD_CFG="firmware",
  78. RAM_EXEC=False,
  79. )
  80. else:
  81. env.Append(
  82. FIRMWARE_BUILD_CFG="updater",
  83. RAM_EXEC=True,
  84. CPPDEFINES=[
  85. "FURI_RAM_EXEC",
  86. ],
  87. )
  88. # Invoke child SCopscripts to populate global `env` + build their own part of the code
  89. lib_targets = env.BuildModules(
  90. [
  91. "lib",
  92. "assets",
  93. "firmware",
  94. "furi",
  95. ],
  96. )
  97. # Now, env is fully set up with everything to build apps
  98. fwenv = env.Clone()
  99. # Set up additional app-specific build flags
  100. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  101. # Set up app configuration
  102. if env["IS_BASE_FIRMWARE"]:
  103. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  104. else:
  105. fwenv.Append(APPS=["updater"])
  106. if extra_int_apps := GetOption("extra_int_apps"):
  107. fwenv.Append(APPS=extra_int_apps.split(","))
  108. fwenv.LoadApplicationManifests()
  109. fwenv.PrepareApplicationsBuild()
  110. # Build external apps
  111. extapps = SConscript("applications/extapps.scons", exports={"ENV": fwenv})
  112. # Add preprocessor definitions for current set of apps
  113. fwenv.AppendUnique(
  114. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  115. )
  116. # Build applications.c for selected services & apps
  117. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  118. apps_c = fwenv.ApplicationsC(
  119. "applications/applications.c",
  120. Value(fwenv["APPS"]),
  121. )
  122. # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
  123. fwenv.Depends(apps_c, fwenv.GlobRecursive("*.fam", "#/applications"))
  124. sources = [apps_c]
  125. # Gather sources only from app folders in current configuration
  126. for app_folder in fwenv["APPBUILD"].get_builtin_app_folders():
  127. sources += fwenv.GlobRecursive("*.c*", os.path.join("applications", app_folder))
  128. fwenv.AppendUnique(
  129. LINKFLAGS=[
  130. "-specs=nano.specs",
  131. "-specs=nosys.specs",
  132. "-Wl,--start-group",
  133. "-lstdc++",
  134. "-lsupc++",
  135. "-Wl,--end-group",
  136. "-Wl,--gc-sections",
  137. "-Wl,--undefined=uxTopUsedPriority",
  138. "-Wl,--wrap,_malloc_r",
  139. "-Wl,--wrap,_free_r",
  140. "-Wl,--wrap,_calloc_r",
  141. "-Wl,--wrap,_realloc_r",
  142. "-u",
  143. "_printf_float",
  144. "-n",
  145. "-Xlinker",
  146. "-Map=${TARGET}.map",
  147. ],
  148. )
  149. # Debug
  150. # print(fwenv.Dump())
  151. # Full firmware definition
  152. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  153. "${FIRMWARE_BUILD_CFG}",
  154. sources,
  155. LIBS=[
  156. "flipper${TARGET_HW}",
  157. "furi",
  158. "freertos",
  159. "stm32cubewb",
  160. "hwdrivers",
  161. "fatfs",
  162. "littlefs",
  163. "subghz",
  164. "flipperformat",
  165. "toolbox",
  166. "nfc",
  167. "microtar",
  168. "usb_stm32",
  169. "st25rfal002",
  170. "infrared",
  171. "appframe",
  172. "assets",
  173. "misc",
  174. "mbedtls",
  175. "loclass",
  176. # 2nd round
  177. "flipperformat",
  178. "toolbox",
  179. ],
  180. )
  181. # Firmware depends on everything child builders returned
  182. Depends(fwelf, lib_targets)
  183. # Output extra details after building firmware
  184. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  185. AddPostAction(fwelf, Action("@$SIZECOM"))
  186. # Produce extra firmware files
  187. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  188. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  189. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  190. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  191. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  192. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  193. fw_artifacts = fwenv["FW_ARTIFACTS"] = [
  194. fwhex,
  195. fwbin,
  196. fwdfu,
  197. fwenv["FW_VERSION_JSON"],
  198. ]
  199. # If current configuration was explicitly requested, generate compilation database
  200. # and link its directory as build/latest
  201. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  202. fwcdb = fwenv.CompilationDatabase()
  203. # without filtering, both updater & firmware commands would be generated
  204. fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
  205. AlwaysBuild(fwcdb)
  206. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  207. fw_artifacts.append(fwcdb)
  208. # Adding as a phony target, so folder link is updated even if elf didn't change
  209. link_dir_command = fwenv.PhonyTarget(
  210. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  211. Action(
  212. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  213. None,
  214. ),
  215. source=fwelf,
  216. )
  217. fw_artifacts.append(link_dir_command)
  218. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  219. Return("fwenv")