-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
base.py
167 lines (154 loc) · 5.47 KB
/
base.py
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
# 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"]
# Parse custom options
env.ParseCustomOptions(platform)
# Parse custom flash layout
env.ParseCustomFlashLayout(platform, board)
# Add flash layout C defines
env.AddFlashLayout(board)
# Export board manifest for ltchiptool
env.ExportBoardData(board)
# Print information about versions and custom options
env.PrintInfo(platform, board)
# 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
"-fno-delete-null-pointer-checks", # assume that programs can safely dereference null pointers
# 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.Prepend(LIBPATH=[join("$CORES_DIR", f.name, "misc")])
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")])
# Sources - external libraries
queue.AddExternalLibrary("uf2ota")
queue.AddExternalLibrary("flashdb")
queue.AddExternalLibrary("printf")
# Find optimization level and add __OPTIMIZE_LEVEL__ macro
for flag in env["CCFLAGS"]:
if not flag.startswith("-O"):
continue
env.Append(CPPDEFINES=[("__OPTIMIZE_LEVEL__", flag[2])])
# 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),
("LIBRETINY", 1),
("LT_VERSION", env["LT_VERSION"]),
("LT_BOARD", "${VARIANT}"),
("LT_VARIANT_H", r"\"${VARIANT}.h\""),
("F_CPU", board.get("build.f_cpu")),
("MCU", "${MCU}"),
("MCULC", "${MCULC}"),
("FAMILY", "F_${FAMILY_SHORT_NAME}"),
# Add flash layout defines created in env.AddFlashLayout()
*env["FLASH_DEFINES"].items(),
],
CPPPATH=[
"$VARIANTS_DIR",
],
LINKFLAGS=[
"-g2",
"-Os",
"-Wl,--as-needed",
"-Wl,--build-id=none",
"-Wl,--cref",
"-Wl,--gc-sections",
"-Wl,--no-enum-size-warning",
"-Wl,--no-wchar-size-warning",
"-Wl,--no-undefined",
"-Wl,--warn-common",
# 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()