Files
libretiny/builder/frameworks/base.py
2023-03-13 22:18:02 +01:00

146 lines
4.8 KiB
Python

# Copyright (c) Kuba Szczodrzyński 2023-02-26.
from os.path import join
import click
from ltchiptool import Family
from platformio.platform.base import PlatformBase
from platformio.platform.board import PlatformBoardConfig
from SCons.Errors import UserError
from SCons.Script import DefaultEnvironment, Environment
env: Environment = DefaultEnvironment()
board: PlatformBoardConfig = env.BoardConfig()
platform: PlatformBase = env.PioPlatform()
family: Family = env["FAMILY_OBJ"]
# TODO remove include path prepending ("!<...>")
# Move common core sources (env.AddCoreSources()) and Arduino libs
# below per-family sources (to maintain child families taking precedence)
# Global public flags
# Refer to https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html
env.Append(
CCFLAGS=[
# C Language Options
"-fsigned-char", # Let the type char be signed
# Debugging Options
"-g2", # produce debugging information; the default level is 2
# Optimization Options
"-Os", # optimize for size; enables all -O2 optimizations except those that often increase code size
"-fdata-sections", # place each function or data item into its own section
"-ffunction-sections", # place each function or data item into its own section
"-fno-strict-aliasing", # (don't) assume the strictest aliasing rules applicable
"-fno-inline-functions", # (don't) consider all functions for inlining
# Preprocessor Options
"-MMD", # output a rule suitable for make describing the dependencies of the main source file
# Code Generation Options
"-fno-common", # place uninitialized global variables in the BSS section of the object file
"-fno-exceptions", # disable exception handling
# Developer Options
"-fstack-usage", # output stack usage information for the program, on a per-function basis
],
CFLAGS=[
"-std=gnu99",
],
CXXFLAGS=[
"-std=gnu++11",
"-fno-rtti", # disable generation of information about every class with virtual functions
],
)
# Include SDK builder scripts
# No environment options that follow later will be considered
found = False
for f in family.inheritance:
try:
env.SConscript(f"../family/{f.name}.py", must_exist=True)
found = True
except UserError:
pass
# Fail if no SDK builder was found
if not found:
click.secho(
f"Platform '{family.name}' is currently not supported - "
"no SDK builder script could be found.",
fg="red",
)
exit(1)
# Build a safe environment for this script
queue = env.AddLibraryQueue("base", prepend_includes=True)
# Add sources & include paths for each core
env.AddCoreSources(queue, name="common", path=join("$COMMON_DIR", "base"))
for f in family.inheritance:
env.AddCoreSources(queue, name=f.code, path=join("$CORES_DIR", f.name, "base"))
if f.short_name:
env.Prepend(CPPDEFINES=[(f"LT_{f.short_name}", "1")])
if f.code:
env.Prepend(CPPDEFINES=[(f"LT_{f.code.upper()}", "1")])
env.Prepend(LIBPATH=[join("$CORES_DIR", f.name, "misc")])
# Sources - external libraries
queue.AddExternalLibrary("ltchiptool") # uf2ota source code
queue.AddExternalLibrary("flashdb")
queue.AddExternalLibrary("printf")
# Non-SDK defines & linker options
queue.AppendPublic(
CCFLAGS=[
"-Wreturn-type",
"-Wno-undef",
],
CFLAGS=[
"-Werror=implicit-function-declaration",
],
CXXFLAGS=[
"-Wno-literal-suffix",
"-Wno-write-strings",
"-Wno-psabi", # parameter passing for argument of type ... changed in GCC 7.1
],
CPPDEFINES=[
("LIBRETUYA", 1),
("LT_VERSION", env.ReadLTVersion(platform.get_dir(), platform.version)),
("LT_BOARD", "${VARIANT}"),
("LT_VARIANT_H", r"\"${VARIANT}.h\""),
("F_CPU", board.get("build.f_cpu")),
("MCU", "${MCU}"),
("MCULC", "${MCULC}"),
("FAMILY", "F_${FAMILY}"),
# Add flash layout defines created in env.AddFlashLayout()
*env["FLASH_DEFINES"].items(),
],
CPPPATH=[
"$VARIANTS_DIR",
],
LINKFLAGS=[
"-g2",
"-Os",
"-Wl,--build-id=none",
"-Wl,--cref",
"-Wl,--gc-sections",
"-Wl,--no-enum-size-warning",
"-Wl,--no-wchar-size-warning",
# malloc.c wrappers
"-Wl,-wrap,malloc",
"-Wl,-wrap,calloc",
"-Wl,-wrap,zalloc",
"-Wl,-wrap,realloc",
"-Wl,-wrap,free",
"-Wl,-wrap,_malloc_r",
"-Wl,-wrap,_calloc_r",
"-Wl,-wrap,_realloc_r",
"-Wl,-wrap,_free_r",
# linker map path
'"-Wl,-Map=' + join("$BUILD_DIR", "${PROGNAME}.map") + '"',
],
LIBS=[
"stdc++",
"supc++",
],
)
# Build everything from the base core
queue.BuildLibraries()