firmware.scons 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. from SCons.Errors import UserError
  2. from SCons.Node import FS
  3. import itertools
  4. from fbt_extra.util import (
  5. should_gen_cdb_and_link_dir,
  6. link_elf_dir_as_latest,
  7. )
  8. Import("ENV", "fw_build_meta")
  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. "pvsstudio",
  16. "fbt_hwtarget",
  17. ],
  18. COMPILATIONDB_USE_ABSPATH=False,
  19. BUILD_DIR=fw_build_meta["build_dir"],
  20. IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
  21. FW_FLAVOR=fw_build_meta["flavor"],
  22. LIB_DIST_DIR=fw_build_meta["build_dir"].Dir("lib"),
  23. LINT_SOURCES=[
  24. Dir("applications"),
  25. ],
  26. LIBPATH=[
  27. "${LIB_DIST_DIR}",
  28. ],
  29. CPPPATH=[
  30. "#/furi",
  31. *(f"#/{app_dir[0]}" for app_dir in ENV["APPDIRS"] if app_dir[1]),
  32. "#/firmware/targets/furi_hal_include",
  33. ],
  34. # Specific flags for building libraries - always do optimized builds
  35. FW_LIB_OPTS={
  36. "Default": {
  37. "CCFLAGS": [
  38. "-Og" if ENV["LIB_DEBUG"] else "-Os",
  39. ],
  40. "CPPDEFINES": [
  41. "NDEBUG",
  42. "FURI_DEBUG" if ENV["LIB_DEBUG"] else "FURI_NDEBUG",
  43. ],
  44. # You can add other entries named after libraries
  45. # If they are present, they have precedence over Default
  46. },
  47. # for furi_check to respect build type
  48. "furi": {
  49. "CCFLAGS": [
  50. "-Os",
  51. ],
  52. "CPPDEFINES": [
  53. "NDEBUG",
  54. "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
  55. ],
  56. },
  57. "flipper_application": {
  58. "CCFLAGS": [
  59. "-Og",
  60. ],
  61. "CPPDEFINES": [
  62. "NDEBUG",
  63. "FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
  64. ],
  65. },
  66. },
  67. FW_API_TABLE=None,
  68. _APP_ICONS=None,
  69. )
  70. if env["IS_BASE_FIRMWARE"]:
  71. env.Append(
  72. FIRMWARE_BUILD_CFG="firmware",
  73. RAM_EXEC=False,
  74. )
  75. else:
  76. env.Append(
  77. FIRMWARE_BUILD_CFG="updater",
  78. RAM_EXEC=True,
  79. CPPDEFINES=[
  80. "FURI_RAM_EXEC",
  81. ],
  82. )
  83. env.ConfigureForTarget(env.subst("${TARGET_HW}"))
  84. Export("env")
  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(FW_ARTIFACTS=[])
  96. fw_artifacts = fwenv["FW_ARTIFACTS"]
  97. # Set up additional app-specific build flags
  98. SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
  99. # Set up app configuration
  100. if env["IS_BASE_FIRMWARE"]:
  101. fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
  102. else:
  103. fwenv.Append(APPS=["updater"])
  104. if extra_int_apps := GetOption("extra_int_apps"):
  105. fwenv.Append(APPS=extra_int_apps.split(","))
  106. for app_dir, _ in fwenv["APPDIRS"]:
  107. app_dir_node = env.Dir("#").Dir(app_dir)
  108. for entry in app_dir_node.glob("*"):
  109. if isinstance(entry, FS.Dir) and not str(entry).startswith("."):
  110. fwenv.LoadAppManifest(entry)
  111. fwenv.PrepareApplicationsBuild()
  112. # Build external apps + configure SDK
  113. if env["IS_BASE_FIRMWARE"]:
  114. fwenv.SetDefault(FBT_FAP_DEBUG_ELF_ROOT="${BUILD_DIR}/.extapps")
  115. fwenv["FW_EXTAPPS"] = SConscript(
  116. "site_scons/extapps.scons",
  117. exports={"ENV": fwenv},
  118. )
  119. fw_artifacts.append(fwenv["FW_EXTAPPS"].sdk_tree)
  120. # Add preprocessor definitions for current set of apps
  121. fwenv.Append(
  122. CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
  123. )
  124. # Build applications.c for selected services & apps
  125. # Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
  126. apps_c = fwenv.ApplicationsC(
  127. "applications/applications.c",
  128. [Value(fwenv["APPS"]), Value(fwenv["LOADER_AUTOSTART"])],
  129. )
  130. # Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
  131. for app_dir, _ in env["APPDIRS"]:
  132. app_dir_node = env.Dir("#").Dir(app_dir)
  133. fwenv.Depends(apps_c, app_dir_node.glob("*/application.fam"))
  134. # Sanity check - certain external apps are using features that are not available in base firmware
  135. if advanced_faps := list(
  136. filter(
  137. lambda app: app.fap_extbuild or app.fap_private_libs or app.fap_icon_assets,
  138. fwenv["APPBUILD"].get_builtin_apps(),
  139. )
  140. ):
  141. raise UserError(
  142. "An Application that is using fap-specific features cannot be built into base firmware."
  143. f" Offending app(s): {', '.join(app.appid for app in advanced_faps)}"
  144. )
  145. sources = [apps_c]
  146. # Gather sources only from app folders in current configuration
  147. sources.extend(
  148. itertools.chain.from_iterable(
  149. fwenv.GlobRecursive(source_type, appdir.relpath, exclude=["lib"])
  150. for appdir, source_type in fwenv["APPBUILD"].get_builtin_app_folders()
  151. )
  152. )
  153. # Debug
  154. # print(fwenv.Dump())
  155. # Full firmware definition
  156. fwelf = fwenv["FW_ELF"] = fwenv.Program(
  157. "${FIRMWARE_BUILD_CFG}",
  158. sources,
  159. LIBS=fwenv["TARGET_CFG"].linker_dependencies,
  160. )
  161. # Firmware depends on everything child builders returned
  162. # Depends(fwelf, lib_targets)
  163. # Output extra details after building firmware
  164. AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
  165. AddPostAction(
  166. fwelf,
  167. Action(
  168. '${PYTHON3} "${BIN_SIZE_SCRIPT}" elf ${TARGET}',
  169. "Firmware size",
  170. ),
  171. )
  172. # Produce extra firmware files
  173. fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
  174. fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
  175. AddPostAction(
  176. fwbin,
  177. Action('@${PYTHON3} "${BIN_SIZE_SCRIPT}" bin ${TARGET}'),
  178. )
  179. fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
  180. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
  181. fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
  182. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
  183. fw_artifacts.extend(
  184. [
  185. fwhex,
  186. fwbin,
  187. fwdfu,
  188. fwenv["FW_VERSION_JSON"],
  189. ]
  190. )
  191. fwcdb = fwenv.CompilationDatabase()
  192. # without filtering, both updater & firmware commands would be generated in same file
  193. fwenv.Replace(
  194. COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"),
  195. COMPILATIONDB_SRCPATH_FILTER="*.c*",
  196. )
  197. AlwaysBuild(fwcdb)
  198. Precious(fwcdb)
  199. NoClean(fwcdb)
  200. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
  201. pvscheck = fwenv.PVSCheck("pvsreport.log", fwcdb)
  202. Depends(
  203. pvscheck,
  204. [
  205. fwenv["FW_VERSION_JSON"],
  206. fwenv["FW_ASSETS_HEADERS"],
  207. fwenv["FW_API_TABLE"],
  208. fwenv["_APP_ICONS"],
  209. ],
  210. )
  211. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_pvscheck", pvscheck)
  212. AlwaysBuild(pvscheck)
  213. Precious(pvscheck)
  214. pvsreport = fwenv.PVSReport(None, pvscheck, REPORT_DIR=Dir("pvsreport"))
  215. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_pvs", pvsreport)
  216. AlwaysBuild(pvsreport)
  217. # If current configuration was explicitly requested, generate compilation database
  218. # and link its directory as build/latest
  219. if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
  220. fw_artifacts.append(fwcdb)
  221. # Adding as a phony target, so folder link is updated even if elf didn't change
  222. link_dir_command = fwenv.PhonyTarget(
  223. fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
  224. Action(
  225. lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
  226. None,
  227. ),
  228. source=fwelf,
  229. )
  230. fw_artifacts.append(link_dir_command)
  231. Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
  232. Return("fwenv")