-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
platformio-build.py
431 lines (378 loc) · 13.2 KB
/
platformio-build.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Arduino
Arduino Wiring-based Framework allows writing cross-platform software to
control devices attached to a wide range of Arduino boards to create all
kinds of creative coding, interactive objects, spaces or physical experiences.
https://arduino.cc/en/Reference/HomePage
"""
# Extends: https://github.com/platformio/platform-espressif8266/blob/develop/builder/main.py
from os.path import isdir, join
from SCons import Util
from SCons.Script import Builder, DefaultEnvironment
def scons_patched_match_splitext(path, suffixes=None):
"""
Patch SCons Builder, append $OBJSUFFIX to the end of each target
"""
tokens = Util.splitext(path)
if suffixes and tokens[1] and tokens[1] in suffixes:
return (path, tokens[1])
return tokens
Builder.match_splitext = scons_patched_match_splitext
env = DefaultEnvironment()
platform = env.PioPlatform()
board = env.BoardConfig()
gzip_fw = board.get("build.gzip_fw", False)
gzip_switch = []
FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoespressif8266")
assert isdir(FRAMEWORK_DIR)
if gzip_fw:
gzip_switch = ["--gzip", "PIO"]
env.Append(
ASFLAGS=["-x", "assembler-with-cpp"],
CFLAGS=[
"-std=gnu17",
"-Wpointer-arith",
"-Wno-implicit-function-declaration",
"-Wl,-EL",
"-fno-inline-functions",
"-nostdlib"
],
CCFLAGS=[
"-Os", # optimize for size
"-mlongcalls",
"-mtext-section-literals",
"-falign-functions=4",
"-U__STRICT_ANSI__",
"-D_GNU_SOURCE",
"-ffunction-sections",
"-fdata-sections",
"-Wall",
"-free",
"-fipa-pta"
],
CXXFLAGS=[
"-fno-rtti",
"-std=gnu++17"
],
LINKFLAGS=[
"-Os",
"-nostdlib",
"-Wl,--no-check-sections",
"-Wl,-static",
"-Wl,--gc-sections",
"-Wl,-wrap,system_restart_local",
"-Wl,-wrap,spi_flash_read",
"-u", "app_entry",
"-u", "_printf_float",
"-u", "_scanf_float",
"-u", "_DebugExceptionVector",
"-u", "_DoubleExceptionVector",
"-u", "_KernelExceptionVector",
"-u", "_NMIExceptionVector",
"-u", "_UserExceptionVector"
],
CPPDEFINES=[
("F_CPU", "$BOARD_F_CPU"),
"__ets__",
"ICACHE_FLASH",
("ARDUINO", 10805),
("ARDUINO_BOARD", '\\"PLATFORMIO_%s\\"' % env.BoardConfig().id.upper()),
"FLASHMODE_${BOARD_FLASH_MODE.upper()}",
"LWIP_OPEN_SRC"
],
CPPPATH=[
join(FRAMEWORK_DIR, "tools", "sdk", "include"),
join(FRAMEWORK_DIR, "cores", env.BoardConfig().get("build.core")),
join(platform.get_package_dir("toolchain-xtensa"), "include")
],
LIBPATH=[
join("$BUILD_DIR", "ld"), # eagle.app.v6.common.ld
join(FRAMEWORK_DIR, "tools", "sdk", "lib"),
join(FRAMEWORK_DIR, "tools", "sdk", "ld")
],
LIBS=[
"hal", "phy", "pp", "net80211", "wpa", "crypto", "main",
"wps", "bearssl", "espnow", "smartconfig", "airkiss", "wpa2",
"m", "c", "gcc"
],
LIBSOURCE_DIRS=[
join(FRAMEWORK_DIR, "libraries")
],
BUILDERS=dict(
ElfToBin=Builder(
action=env.VerboseAction(" ".join([
'"$PYTHONEXE"',
'"%s"' % join(FRAMEWORK_DIR, "tools", "elf2bin.py"),
"--eboot", '"%s"' % join(
FRAMEWORK_DIR, "bootloaders", "eboot", "eboot.elf"),
"--app", "$SOURCE",
"--flash_mode", "$BOARD_FLASH_MODE",
"--flash_freq", "${__get_board_f_flash(__env__)}",
"--flash_size", "${__get_flash_size(__env__)}",
"--path", '"%s"' % join(
platform.get_package_dir("toolchain-xtensa"), "bin"),
"--out", "$TARGET"
] + gzip_switch), "Building $TARGET"),
suffix=".bin"
)
)
)
# copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode)
env.Append(ASFLAGS=env.get("CCFLAGS", [])[:])
flatten_cppdefines = env.Flatten(env['CPPDEFINES'])
#
# SDK
#
if "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK3" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("NONOSDK3V0", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK3V0")]
)
elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK221" in flatten_cppdefines:
#(previous default)
env.Append(
CPPDEFINES=[("NONOSDK221", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK221")]
)
elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_190313" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("NONOSDK22x_190313", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_190313")]
)
elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_191024" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("NONOSDK22x_191024", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_191024")]
)
elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_191105" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("NONOSDK22x_191105", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_191105")]
)
elif "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_191122" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("NONOSDK22x_191122", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_191122")]
)
else: #(default) if "PIO_FRAMEWORK_ARDUINO_ESPRESSIF_SDK22x_190703" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("NONOSDK22x_190703", 1)],
LIBPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lib", "NONOSDK22x_190703")]
)
#
# lwIP
#
if "PIO_FRAMEWORK_ARDUINO_LWIP2_IPV6_LOW_MEMORY" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 1)],
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
LIBS=["lwip6-536-feat"]
)
elif "PIO_FRAMEWORK_ARDUINO_LWIP2_IPV6_HIGHER_BANDWIDTH" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 1)],
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
LIBS=["lwip6-1460-feat"]
)
elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 0)],
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
LIBS=["lwip2-1460-feat"]
)
elif "PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY_LOW_FLASH" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)],
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
LIBS=["lwip2-536"]
)
elif "PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH" in flatten_cppdefines:
env.Append(
CPPDEFINES=[("TCP_MSS", 1460), ("LWIP_FEATURES", 0), ("LWIP_IPV6", 0)],
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
LIBS=["lwip2-1460"]
)
# PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY (default)
else:
env.Append(
CPPDEFINES=[("TCP_MSS", 536), ("LWIP_FEATURES", 1), ("LWIP_IPV6", 0)],
CPPPATH=[join(FRAMEWORK_DIR, "tools", "sdk", "lwip2", "include")],
LIBS=["lwip2-536-feat"]
)
#
# Waveform
#
if "PIO_FRAMEWORK_ARDUINO_WAVEFORM_LOCKED_PHASE" in flatten_cppdefines:
env.Append(CPPDEFINES=[("WAVEFORM_LOCKED_PHASE", 1)])
# PIO_FRAMEWORK_ARDUINO_WAVEFORM_LOCKED_PWM will be used by default
#
# Exceptions
#
if "PIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS" in flatten_cppdefines:
env.Append(
CXXFLAGS=["-fexceptions"],
LIBS=["stdc++-exc"]
)
else:
env.Append(
CXXFLAGS=["-fno-exceptions"],
LIBS=["stdc++"]
)
#
# VTables
#
current_vtables = None
fp_in_irom = ""
for d in flatten_cppdefines:
if str(d).startswith("VTABLES_IN_"):
current_vtables = d
if str(d) == "FP_IN_IROM":
fp_in_irom = "-DFP_IN_IROM"
if not current_vtables:
current_vtables = "VTABLES_IN_FLASH"
env.Append(CPPDEFINES=[current_vtables])
assert current_vtables
#
# MMU
#
mmu_flags = []
required_flags = ("MMU_IRAM_SIZE", "MMU_ICACHE_SIZE")
if "PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48" in flatten_cppdefines:
mmu_flags = [("MMU_IRAM_SIZE", "0xC000"), ("MMU_ICACHE_SIZE", "0x4000")]
elif "PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48_SECHEAP_SHARED" in flatten_cppdefines:
mmu_flags = [
("MMU_IRAM_SIZE", "0xC000"),
("MMU_ICACHE_SIZE", "0x4000"),
"MMU_IRAM_HEAP",
]
elif "PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM32_SECHEAP_NOTSHARED" in flatten_cppdefines:
mmu_flags = [
("MMU_IRAM_SIZE", "0x8000"),
("MMU_ICACHE_SIZE", "0x4000"),
("MMU_SEC_HEAP_SIZE", "0x4000"),
("MMU_SEC_HEAP", "0x40108000"),
]
elif "PIO_FRAMEWORK_ARDUINO_MMU_EXTERNAL_128K" in flatten_cppdefines:
mmu_flags = [
("MMU_IRAM_SIZE", "0x8000"),
("MMU_ICACHE_SIZE", "0x8000"),
("MMU_EXTERNAL_HEAP", "128"),
]
elif "PIO_FRAMEWORK_ARDUINO_MMU_EXTERNAL_1024K" in flatten_cppdefines:
mmu_flags = [
("MMU_IRAM_SIZE", "0x8000"),
("MMU_ICACHE_SIZE", "0x8000"),
("MMU_EXTERNAL_HEAP", "256"),
]
elif "PIO_FRAMEWORK_ARDUINO_MMU_CUSTOM" in flatten_cppdefines:
if not all(d in flatten_cppdefines for d in required_flags):
print(
"Error: Missing custom MMU configuration flags (%s)!"
% ", ".join(required_flags)
)
env.Exit(1)
for flag in env["CPPDEFINES"]:
define = flag
if isinstance(flag, (tuple, list)):
define, _ = flag
if define.startswith("MMU_"):
mmu_flags.append(flag)
# PIO_FRAMEWORK_ARDUINO_MMU_CACHE32_IRAM32 (default)
else:
mmu_flags = [
("MMU_IRAM_SIZE", board.get("build.mmu_iram_size", "0x8000")),
("MMU_ICACHE_SIZE", board.get("build.mmu_icache_size", "0x8000"))]
if any(f in flatten_cppdefines for f in required_flags):
print(
"Warning! Detected custom MMU flags. Please use the "
"`-D PIO_FRAMEWORK_ARDUINO_MMU_CUSTOM` option to disable "
"the default configuration."
)
assert mmu_flags
env.Append(CPPDEFINES=mmu_flags)
# Build the eagle.app.v6.common.ld linker file
app_ld = env.Command(
join("$BUILD_DIR", "ld", "local.eagle.app.v6.common.ld"),
join(FRAMEWORK_DIR, "tools", "sdk", "ld", "eagle.app.v6.common.ld.h"),
env.VerboseAction(
"$CC -CC -E -P -D%s %s %s $SOURCE -o $TARGET"
% (
current_vtables,
# String representation of MMU flags
" ".join(
[
"-D%s=%s" % f if isinstance(f, (tuple, list)) else "-D" + f
for f in mmu_flags
]
),
fp_in_irom,
),
"Generating LD script $TARGET",
),
)
env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", app_ld)
if not env.BoardConfig().get("build.ldscript", ""):
env.Replace(LDSCRIPT_PATH=env.BoardConfig().get("build.arduino.ldscript", ""))
#
# Dynamic core_version.h for staging builds
#
def platform_txt_version(default):
with open(join(FRAMEWORK_DIR, "platform.txt"), "r") as platform_txt:
for line in platform_txt:
if not line:
continue
k, delim, v = line.partition("=")
if not delim:
continue
if k == "version":
return v.strip()
return default
if isdir(join(FRAMEWORK_DIR, ".git")):
cmd = '"$PYTHONEXE" "{script}" -b "$BUILD_DIR" -p "{framework_dir}" -v {version}'
fmt = {
"script": join(FRAMEWORK_DIR, "tools", "makecorever.py"),
"framework_dir": FRAMEWORK_DIR,
"version": platform_txt_version("unspecified")
}
env.Prepend(CPPPATH=[
join("$BUILD_DIR", "core")
])
core_version = env.Command(
join("$BUILD_DIR", "core", "core_version.h"),
join(FRAMEWORK_DIR, ".git"),
env.VerboseAction(cmd.format(**fmt), "Generating $TARGET")
)
env.Depends("$BUILD_DIR/$PROGNAME$PROGSUFFIX", core_version)
#
# Target: Build Core Library
#
libs = []
if "build.variant" in env.BoardConfig():
env.Append(
CPPPATH=[
join(FRAMEWORK_DIR, "variants",
env.BoardConfig().get("build.variant"))
]
)
libs.append(env.BuildLibrary(
join("$BUILD_DIR", "FrameworkArduinoVariant"),
join(FRAMEWORK_DIR, "variants", env.BoardConfig().get("build.variant"))
))
libs.append(env.BuildLibrary(
join("$BUILD_DIR", "FrameworkArduino"),
join(FRAMEWORK_DIR, "cores", env.BoardConfig().get("build.core"))
))
env.Prepend(LIBS=libs)