flipperzero-firmware/firmware.scons
hedger 6b6ea44802
fbt: initial blackmagic support (#1362)
* fbt: added separate script for Windows env setup; moved flash targets from firmware.scons to SConstruct; added Blackmagic support with automatic probe port resolution; added apps.c rebuild on any manifest.fam changes; fixed simultaneous flash & debug ops
* fbt: added networked BlackmagicResolver mode; added `get_blackmagic` target for IDE integration
* fbt: cleanup
* fbt: docs update; fixed blackmagic lookup on certain usb hubs
* fbt: removed explicit python serial port import
* fbt: cleanup
* fbt: raising exception on multiple serial blackmagic probes
2022-07-05 02:53:04 +10:00

273 lines
7.0 KiB
Plaintext

Import("ENV", "fw_build_meta")
import os
from fbt.util import link_dir
# Building initial C environment for libs
env = ENV.Clone(
tools=["compilation_db", "fwbin", "fbt_apps"],
COMPILATIONDB_USE_ABSPATH=True,
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=[
"#/core",
"#/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
"core": {
"CCFLAGS": [
"-Os",
],
"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}",
)
if fw_build_meta["type"] == "updater":
env.Append(
FIRMWARE_BUILD_CFG="updater",
RAM_EXEC=True,
CPPDEFINES=[
"FURI_RAM_EXEC",
],
)
else:
env.Append(
FIRMWARE_BUILD_CFG="firmware",
RAM_EXEC=False,
)
# print(env.Dump())
# Invoke child SCopscripts to populate global `env` + build their own part of the code
lib_targets = env.BuildModules(
[
"lib",
"assets",
"firmware",
"core",
],
)
# 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 = SConscript("applications/extapps.scons", exports={"ENV": fwenv})
# Add preprocessor definitions for current set of apps
fwenv.AppendUnique(
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 from 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}",
"core",
"freertos",
"stm32cubewb",
"hwdrivers",
"fatfs",
"littlefs",
"subghz",
"flipperformat",
"toolbox",
"microtar",
"usb_stm32",
"st25rfal002",
"infrared",
"appframe",
"assets",
"misc",
# 2nd round
"flipperformat",
"toolbox",
"mbedtls",
"loclass",
],
)
def link_elf_dir_as_latest(env, elf_target):
# Ugly way to check if updater-related targets were requested
elf_dir = elf_target.Dir(".")
explicitly_building_updater = False
# print("BUILD_TARGETS:", ','.join(BUILD_TARGETS))
for build_target in BUILD_TARGETS:
# print(">>> ", str(build_target))
if "updater" in str(build_target):
explicitly_building_updater = True
latest_dir = env.Dir("#build/latest")
link_this_dir = True
if explicitly_building_updater:
# If updater is explicitly requested, link to the latest updater
# Otherwise, link to the latest firmware
link_this_dir = not env["IS_BASE_FIRMWARE"]
if link_this_dir:
print(f"Setting {elf_dir} as latest built dir")
return link_dir(latest_dir.abspath, elf_dir.abspath, env["PLATFORM"] == "win32")
def link_latest_dir(env, target, source):
return link_elf_dir_as_latest(env, target[0])
# Make it depend on everything child builders returned
Depends(fwelf, lib_targets)
AddPostAction(fwelf, fwenv["APPBUILD_DUMP"])
AddPostAction(fwelf, Action("@$SIZECOM"))
AddPostAction(fwelf, Action(link_latest_dir, None))
link_dir_command = fwenv["LINK_DIR_CMD"] = fwenv.PhonyTarget(
fwenv.subst("${FIRMWARE_BUILD_CFG}_latest"),
Action(lambda target, source, env: link_elf_dir_as_latest(env, source[0]), None),
source=fwelf,
)
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)
# Compile DB generation
fwcdb = fwenv["FW_CDB"] = fwenv.CompilationDatabase("compile_commands.json")
fwenv.Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_cdb", fwcdb)
artifacts = [
fwhex,
fwbin,
fwdfu,
env["FW_VERSION_JSON"],
fwcdb,
]
fwenv["FW_ARTIFACTS"] = artifacts
Alias(fwenv["FIRMWARE_BUILD_CFG"] + "_all", artifacts)
Return("fwenv")