firmware.scons 6.6 KB

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