This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
forked from RogueMaster/flipperzero-firmware-wPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firmware.scons
308 lines (266 loc) · 8.34 KB
/
firmware.scons
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
Import("ENV", "fw_build_meta")
import os
from fbt.util import (
should_gen_cdb_and_link_dir,
link_elf_dir_as_latest,
)
# Building initial C environment for libs
env = ENV.Clone(
tools=["compilation_db", "fwbin", "fbt_apps", "fbt_sdk"],
COMPILATIONDB_USE_ABSPATH=False,
BUILD_DIR=fw_build_meta["build_dir"],
IS_BASE_FIRMWARE=fw_build_meta["type"] == "firmware",
FW_FLAVOR=fw_build_meta["flavor"],
PLUGIN_ELF_DIR="${BUILD_DIR}",
LIB_DIST_DIR="${BUILD_DIR}/lib",
LINT_SOURCES=[
"applications",
],
LIBPATH=[
"${LIB_DIST_DIR}",
],
CPPPATH=[
"#/furi",
"#/applications",
"#/firmware/targets/f${TARGET_HW}/ble_glue",
"#/firmware/targets/f${TARGET_HW}/fatfs",
"#/firmware/targets/f${TARGET_HW}/furi_hal",
"#/firmware/targets/f${TARGET_HW}/Inc",
"#/firmware/targets/furi_hal_include",
],
# Specific flags for building libraries - always do optimized builds
FW_LIB_OPTS={
"Default": {
"CCFLAGS": [
"-Os",
],
"CPPDEFINES": [
"NDEBUG",
"FURI_NDEBUG",
],
# You can add other entries named after libraries
# If they are present, they have precedence over Default
},
# for furi_check to respect build type
"furi": {
"CCFLAGS": [
"-Os",
],
"CPPDEFINES": [
"NDEBUG",
"FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
],
},
"flipper_application": {
"CCFLAGS": [
"-Og",
],
"CPPDEFINES": [
"NDEBUG",
"FURI_DEBUG" if ENV["DEBUG"] else "FURI_NDEBUG",
],
},
},
)
def ApplyLibFlags(env):
flags_to_apply = env["FW_LIB_OPTS"].get(
env.get("FW_LIB_NAME"),
env["FW_LIB_OPTS"]["Default"],
)
# print("Flags for ", env.get("FW_LIB_NAME", "Default"), flags_to_apply)
env.MergeFlags(flags_to_apply)
env.AddMethod(ApplyLibFlags)
Export("env")
if not env["VERBOSE"]:
env.SetDefault(
HEXCOMSTR="\tHEX\t${TARGET}",
BINCOMSTR="\tBIN\t${TARGET}",
DFUCOMSTR="\tDFU\t${TARGET}",
OPENOCDCOMSTR="\tFLASH\t${SOURCE}",
SDK_PREGEN_COMSTR="\tPREGEN\t${TARGET}",
SDK_COMSTR="\tSDKSRC\t${TARGET}",
SDKSYM_UPDATER_COMSTR="\tSDKCHK\t${TARGET}",
SDKSYM_GENERATOR_COMSTR="\tSDKSYM\t${TARGET}",
APPMETA_COMSTR="\tAPPMETA\t${TARGET}",
APPMETAEMBED_COMSTR="\tFAP\t${TARGET}",
APPDUMP_COMSTR="\tFAPDUMP\t${TARGET}",
APPCHECK_COMSTR="\tFAPCHK\t${TARGET}",
)
if env["IS_BASE_FIRMWARE"]:
env.Append(
FIRMWARE_BUILD_CFG="firmware",
RAM_EXEC=False,
)
else:
env.Append(
FIRMWARE_BUILD_CFG="updater",
RAM_EXEC=True,
CPPDEFINES=[
"FURI_RAM_EXEC",
],
)
# Invoke child SCopscripts to populate global `env` + build their own part of the code
lib_targets = env.BuildModules(
[
"lib",
"assets",
"firmware",
"furi",
],
)
# Now, env is fully set up with everything to build apps
fwenv = env.Clone()
# Set up additional app-specific build flags
SConscript("site_scons/firmwareopts.scons", exports={"ENV": fwenv})
# Set up app configuration
if env["IS_BASE_FIRMWARE"]:
fwenv.Append(APPS=fwenv["FIRMWARE_APPS"].get(fwenv.subst("$FIRMWARE_APP_SET")))
else:
fwenv.Append(APPS=["updater"])
if extra_int_apps := GetOption("extra_int_apps"):
fwenv.Append(APPS=extra_int_apps.split(","))
fwenv.LoadApplicationManifests()
fwenv.PrepareApplicationsBuild()
# Build external apps
extapps = fwenv["FW_EXTAPPS"] = SConscript(
"applications/extapps.scons", exports={"ENV": fwenv}
)
# Add preprocessor definitions for current set of apps
fwenv.Append(
CPPDEFINES=fwenv["APPBUILD"].get_apps_cdefs(),
)
# Build applications.c for selected services & apps
# Depends on virtual value-only node, so it only gets rebuilt when set of apps changes
apps_c = fwenv.ApplicationsC(
"applications/applications.c",
Value(fwenv["APPS"]),
)
# Adding dependency on manifest files so apps.c is rebuilt when any manifest is changed
fwenv.Depends(apps_c, fwenv.GlobRecursive("*.fam", "applications"))
sources = [apps_c]
# Gather sources only from app folders in current configuration
for app_folder in fwenv["APPBUILD"].get_builtin_app_folders():
sources += fwenv.GlobRecursive("*.c*", os.path.join("applications", app_folder))
fwenv.AppendUnique(
LINKFLAGS=[
"-specs=nano.specs",
"-specs=nosys.specs",
"-Wl,--start-group",
"-lstdc++",
"-lsupc++",
"-Wl,--end-group",
"-Wl,--gc-sections",
"-Wl,--undefined=uxTopUsedPriority",
"-Wl,--wrap,_malloc_r",
"-Wl,--wrap,_free_r",
"-Wl,--wrap,_calloc_r",
"-Wl,--wrap,_realloc_r",
"-u",
"_printf_float",
"-n",
],
)
# Debug
# print(fwenv.Dump())
# Full firmware definition
fwelf = fwenv["FW_ELF"] = fwenv.Program(
"${FIRMWARE_BUILD_CFG}",
sources,
LIBS=[
"flipper${TARGET_HW}",
"furi",
"freertos",
"stm32cubewb",
"hwdrivers",
"fatfs",
"littlefs",
"subghz",
"flipperformat",
"toolbox",
"nfc",
"microtar",
"usb_stm32",
"st25rfal002",
"infrared",
"appframe",
"assets",
"misc",
"mbedtls",
"loclass",
"flipper_application",
# 2nd round
"flipperformat",
"toolbox",
],
)
# Make it depend on everything child builders returned
# Firmware depends on everything child builders returned
Depends(fwelf, lib_targets)
# Output extra details after building firmware
AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
AddPostAction(fwelf, Action("@$SIZECOM"))
# Produce extra firmware files
fwhex = fwenv["FW_HEX"] = fwenv.HEXBuilder("${FIRMWARE_BUILD_CFG}")
fwbin = fwenv["FW_BIN"] = fwenv.BINBuilder("${FIRMWARE_BUILD_CFG}")
fwdfu = fwenv["FW_DFU"] = fwenv.DFUBuilder("${FIRMWARE_BUILD_CFG}")
Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_dfu", fwdfu)
fwdump = fwenv.ObjDump("${FIRMWARE_BUILD_CFG}")
Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_list", fwdump)
fw_artifacts = fwenv["FW_ARTIFACTS"] = [
fwhex,
fwbin,
fwdfu,
fwenv["FW_VERSION_JSON"],
]
# If current configuration was explicitly requested, generate compilation database
# and link its directory as build/latest
if should_gen_cdb_and_link_dir(fwenv, BUILD_TARGETS):
fwcdb = fwenv.CompilationDatabase()
# without filtering, both updater & firmware commands would be generated
fwenv.Replace(COMPILATIONDB_PATH_FILTER=fwenv.subst("*${FW_FLAVOR}*"))
Depends(fwcdb, fwelf)
AlwaysBuild(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
Alias(fwcdb, "")
fw_artifacts.append(fwcdb)
# Adding as a phony target, so folder link is updated even if elf didn't change
link_dir_command = fwenv.PhonyTarget(
fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
Action(
lambda source, target, env: link_elf_dir_as_latest(env, source[0]),
None,
),
source=fwelf,
)
fw_artifacts.append(link_dir_command)
Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", fw_artifacts)
if fwenv["IS_BASE_FIRMWARE"]:
sdk_source = fwenv.SDKPrebuilder(
"sdk_origin",
[],
# Filtering out things cxxheaderparser cannot handle
SDK_PP_FLAGS=[
'-D"_Static_assert(x,y)="',
'-D"__asm__(x)="',
'-D"__attribute__(x)="',
"-Drestrict=",
"-D_Noreturn=",
"-D__restrict=",
"-D__extension__=",
"-D__inline=inline",
"-D__inline__=inline",
],
)
Depends(sdk_source, fwenv["SDK_HEADERS"])
sdk_tree = fwenv.SDKTree("sdk/sdk.opts", "sdk_origin")
AlwaysBuild(sdk_tree)
Alias("sdk_tree", sdk_tree)
sdk_apicheck = fwenv.SDKSymUpdater(fwenv.subst("$SDK_DEFINITION"), "sdk_origin")
Precious(sdk_apicheck)
Alias("sdk_check", sdk_apicheck)
sdk_apisyms = fwenv.SDKSymGenerator(
"assets/compiled/symbols.h", fwenv.subst("$SDK_DEFINITION")
)
Alias("api_syms", sdk_apisyms)
if fwenv["FORCE"]:
fwenv.AlwaysBuild(sdk_source, sdk_tree, sdk_apicheck, sdk_apisyms)
Return("fwenv")