This repository has been archived by the owner on Jul 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino-core-mbed.py
231 lines (185 loc) · 6.84 KB
/
arduino-core-mbed.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
# Copyright 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
#
# http://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://github.com/arduino/ArduinoCore-mbed
"""
import os
from SCons.Script import DefaultEnvironment
env = DefaultEnvironment()
platform = env.PioPlatform()
board = env.BoardConfig()
FRAMEWORK_DIR = platform.get_package_dir("framework-arduinorenesas")
assert os.path.isdir(FRAMEWORK_DIR)
def is_pio_build():
from SCons.Script import COMMAND_LINE_TARGETS
return "idedata" not in COMMAND_LINE_TARGETS and "_idedata" not in COMMAND_LINE_TARGETS and "__idedata" not in COMMAND_LINE_TARGETS
def load_flags(filename):
if not filename:
return []
file_path = os.path.join(FRAMEWORK_DIR, "variants", board.get(
"build.variant"), "%s.txt" % filename)
if not os.path.isfile(file_path):
print("Warning: Couldn't find file '%s'" % file_path)
return []
with open(file_path, "r") as fp:
return [f.strip() for f in fp.readlines() if f.strip()]
def configure_fpu_flags(board_config):
board_cpu = board_config.get("build.cpu", "")
if board_cpu not in ("cortex-m4", "cortex-m33"):
return
fpv_version = "4-sp" if board_cpu == "cortex-m4" else "5"
env.Append(
ASFLAGS=[
"-mfloat-abi=hard",
"-mfpu=fpv%s-d16" % fpv_version
],
# already contained in cxxflags.txt
#CCFLAGS=[
# "-mfloat-abi=hard",
# "-mfpu=fpv%s-d16" % fpv_version
#],
LINKFLAGS=[
"-mcpu=%s" % board_cpu,
"-mfloat-abi=hard",
"-mfpu=fpv%s-d16" % fpv_version
]
)
cflags = set(load_flags("cflags"))
cxxflags = set(load_flags("cxxflags"))
ccflags = cflags.intersection(cxxflags)
env.Append(
ASFLAGS=[f for f in ccflags if isinstance(f, str) and f.startswith("-m")],
ASPPFLAGS=["-x", "assembler-with-cpp"],
CFLAGS=sorted(list(cflags - ccflags)),
CCFLAGS=sorted(list(ccflags)),
CPPDEFINES=[d.replace("-D", "") for d in load_flags("defines")],
CXXFLAGS=sorted(list(cxxflags - ccflags)),
LIBPATH=[
os.path.join(FRAMEWORK_DIR, "variants", board.get("build.variant")),
os.path.join(FRAMEWORK_DIR, "variants", board.get("build.variant"), "libs")
],
LINKFLAGS=[],
LIBSOURCE_DIRS=[os.path.join(FRAMEWORK_DIR, "libraries")],
LIBS=[]
)
env.Append(
# Due to long path names "-iprefix" hook is required to avoid toolchain crashes
CCFLAGS=[
"-Os",
"-nostdlib",
"-fno-builtin"
],
CXXFLAGS=[
"-std=gnu++17",
"-fno-use-cxa-atexit",
"-fno-rtti",
"-fno-exceptions"
],
CFLAGS=[
"-std=gnu11"
],
CPPDEFINES=[
("ARDUINO", 10819),
"ARDUINO_ARCH_RENESAS",
("F_CPU", "$BOARD_F_CPU"),
"_XOPEN_SOURCE"
],
CPPPATH=[
os.path.join(FRAMEWORK_DIR, "cores", board.get("build.core")),
os.path.join(FRAMEWORK_DIR, "cores", board.get(
"build.core"), "api", "deprecated"),
os.path.join(FRAMEWORK_DIR, "cores", board.get(
"build.core"), "api", "deprecated-avr-comp"),
os.path.join(FRAMEWORK_DIR, "cores", board.get(
"build.core"), "tinyusb")
],
LINKFLAGS=[
"-Wl,--gc-sections",
"--specs=nano.specs",
"--specs=nosys.specs",
'-Wl,-Map="%s"' % os.path.join("${BUILD_DIR}", "${PROGNAME}.map")
]
)
# Weird: The Arduino core builds without any warnings flags. Filter them all out, too.
env.Replace(CCFLAGS = list(filter(lambda x: not str(x).startswith("-W"), env["CCFLAGS"])))
env.Replace(CFLAGS = list(filter(lambda x: not str(x).startswith("-W"), env["CFLAGS"])))
env.Replace(CXXFLAGS = list(filter(lambda x: not str(x).startswith("-W"), env["CXXFLAGS"])))
# Additionally suprress warning in cm_backtrace.c
env.Append(CFLAGS=["-Wno-discarded-qualifiers"])
# read includes from this file to add them into CPPPATH later for good IDE intellisense
# will use original -iprefix <prefix> @<file> for compilation though.
includes_file = os.path.join(FRAMEWORK_DIR, "variants", board.get("build.variant"), "includes.txt")
file_lines = []
includes = []
with open(includes_file, "r") as fp:
file_lines = fp.readlines()
for l in file_lines:
path = l.strip().replace("-iwithprefixbefore/", "").replace("/", os.sep)
# emulate -iprefix <framework path>.
path = os.path.join(FRAMEWORK_DIR, path)
# prevent non-existent paths from being added
if os.path.isdir(path):
includes.append(path)
if not is_pio_build():
env.Append(CPPPATH=includes)
else:
env.Append(CCFLAGS= [
"-iprefix" + FRAMEWORK_DIR,
"@%s" % os.path.join(FRAMEWORK_DIR, "variants", board.get(
"build.variant"), "includes.txt"),
])
#
# Configure FPU flags
#
configure_fpu_flags(board)
#
# Linker requires preprocessing with specific defines
#
if not board.get("build.ldscript", ""):
ldscript = os.path.join(
FRAMEWORK_DIR, "variants", board.get("build.variant"), "fsp.ld")
if board.get("build.mbed.ldscript", ""):
ldscript = env.subst(board.get("build.arduino.ldscript"))
# no preprocessing of linker script! It should be used as-is
if os.path.isfile(ldscript):
env.Replace(LDSCRIPT_PATH=ldscript)
else:
print("Warning! Couldn't find linker script file!")
# Framework requires all symbols from mbed libraries
#env.Prepend(_LIBFLAGS="-Wl,--whole-archive ")
# -Wl,--no-whole-archive
env.Append(_LIBFLAGS=" -lstdc++ -lsupc++ -lm -lc -lgcc -lnosys")
libs = []
if "build.variant" in board:
env.Append(CPPPATH=[
os.path.join(FRAMEWORK_DIR, "variants", board.get("build.variant"))
])
env.BuildSources(
os.path.join("$BUILD_DIR", "FrameworkArduinoVariant"),
os.path.join(FRAMEWORK_DIR, "variants", board.get("build.variant"))
)
libs.append(
File(
os.path.join(FRAMEWORK_DIR, "variants", board.get("build.variant"), "libs", "libfsp.a")
)
)
libs.append(
env.BuildLibrary(
os.path.join("$BUILD_DIR", "FrameworkArduino"),
os.path.join(FRAMEWORK_DIR, "cores", board.get("build.core"))))
env.Prepend(LIBS=libs)