Skip to content

Commit adc0a17

Browse files
authored
Update arduino.py
1 parent 7feba13 commit adc0a17

File tree

1 file changed

+168
-6
lines changed

1 file changed

+168
-6
lines changed

builder/frameworks/arduino.py

Lines changed: 168 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,179 @@
2222
http://arduino.cc/en/Reference/HomePage
2323
"""
2424

25+
import subprocess
26+
import json
27+
import semantic_version
28+
import os
29+
import shutil
2530
from os.path import join
2631

27-
from SCons.Script import DefaultEnvironment, SConscript
32+
from SCons.Script import COMMAND_LINE_TARGETS, DefaultEnvironment, SConscript
33+
from platformio.package.version import pepver_to_semver
34+
from platformio.project.config import ProjectConfig
35+
from platformio.package.manager.tool import ToolPackageManager
2836

2937
env = DefaultEnvironment()
38+
pm = ToolPackageManager()
39+
platform = env.PioPlatform()
40+
config = env.GetProjectConfig()
3041
board = env.BoardConfig()
31-
build_core = board.get("build.core", "").lower()
42+
mcu = board.get("build.mcu", "esp32")
43+
board_sdkconfig = board.get("espidf.custom_sdkconfig", "")
44+
entry_custom_sdkconfig = "\n"
45+
flag_custom_sdkconfig = False
46+
47+
if config.has_option("env:"+env["PIOENV"], "custom_sdkconfig"):
48+
entry_custom_sdkconfig = env.GetProjectOption("custom_sdkconfig")
49+
flag_custom_sdkconfig = True
50+
51+
if len(str(board_sdkconfig)) > 2:
52+
flag_custom_sdkconfig = True
53+
54+
extra_flags = (''.join([element for element in board.get("build.extra_flags", "")])).replace("-D", " ")
55+
framework_reinstall = False
56+
flag_any_custom_sdkconfig = False
3257

3358
SConscript("_embed_files.py", exports="env")
3459

35-
if "espidf" not in env.subst("$PIOFRAMEWORK"):
36-
SConscript(
37-
join(DefaultEnvironment().PioPlatform().get_package_dir(
38-
"framework-arduinoespressif32"), "tools", "platformio-build.py"))
60+
flag_any_custom_sdkconfig = os.path.exists(join(platform.get_package_dir("framework-arduinoespressif32-libs"),"sdkconfig"))
61+
62+
# Esp32-solo1 libs needs adopted settings
63+
if flag_custom_sdkconfig == True and ("CORE32SOLO1" in extra_flags or "CONFIG_FREERTOS_UNICORE=y" in entry_custom_sdkconfig or "CONFIG_FREERTOS_UNICORE=y" in board_sdkconfig):
64+
if len(str(env.GetProjectOption("build_unflags"))) == 2: # No valid env, needs init
65+
env['BUILD_UNFLAGS'] = {}
66+
build_unflags = " ".join(env['BUILD_UNFLAGS'])
67+
build_unflags = build_unflags + " -mdisable-hardware-atomics -ustart_app_other_cores"
68+
new_build_unflags = build_unflags.split()
69+
env.Replace(
70+
BUILD_UNFLAGS=new_build_unflags
71+
)
72+
73+
def install_python_deps():
74+
def _get_installed_pip_packages():
75+
result = {}
76+
packages = {}
77+
pip_output = subprocess.check_output(
78+
[
79+
env.subst("$PYTHONEXE"),
80+
"-m",
81+
"pip",
82+
"list",
83+
"--format=json",
84+
"--disable-pip-version-check",
85+
]
86+
)
87+
try:
88+
packages = json.loads(pip_output)
89+
except:
90+
print("Warning! Couldn't extract the list of installed Python packages.")
91+
return {}
92+
for p in packages:
93+
result[p["name"]] = pepver_to_semver(p["version"])
94+
95+
return result
96+
97+
deps = {
98+
"wheel": ">=0.35.1",
99+
"PyYAML": ">=6.0.2",
100+
"intelhex": ">=2.3.0"
101+
}
102+
103+
installed_packages = _get_installed_pip_packages()
104+
packages_to_install = []
105+
for package, spec in deps.items():
106+
if package not in installed_packages:
107+
packages_to_install.append(package)
108+
else:
109+
version_spec = semantic_version.Spec(spec)
110+
if not version_spec.match(installed_packages[package]):
111+
packages_to_install.append(package)
112+
113+
if packages_to_install:
114+
env.Execute(
115+
env.VerboseAction(
116+
(
117+
'"$PYTHONEXE" -m pip install -U '
118+
+ " ".join(
119+
[
120+
'"%s%s"' % (p, deps[p])
121+
for p in packages_to_install
122+
]
123+
)
124+
),
125+
"Installing Arduino Python dependencies",
126+
)
127+
)
128+
return
129+
130+
install_python_deps()
131+
132+
def get_MD5_hash(phrase):
133+
import hashlib
134+
return hashlib.md5((phrase).encode('utf-8')).hexdigest()[:16]
135+
136+
137+
def matching_custom_sdkconfig():
138+
# check if current env is matching to existing sdkconfig
139+
cust_sdk_is_present = False
140+
matching_sdkconfig = False
141+
last_sdkconfig_path = join(env.subst("$PROJECT_DIR"),"sdkconfig.defaults")
142+
if flag_any_custom_sdkconfig == False:
143+
matching_sdkconfig = True
144+
return matching_sdkconfig, cust_sdk_is_present
145+
if os.path.exists(last_sdkconfig_path) == False:
146+
return matching_sdkconfig, cust_sdk_is_present
147+
if flag_custom_sdkconfig == False:
148+
matching_sdkconfig = False
149+
return matching_sdkconfig, cust_sdk_is_present
150+
with open(last_sdkconfig_path) as src:
151+
line = src.readline()
152+
if line.startswith("# TASMOTA__"):
153+
cust_sdk_is_present = True;
154+
costum_options = entry_custom_sdkconfig
155+
if (line.split("__")[1]).strip() == get_MD5_hash((costum_options).strip() + mcu):
156+
matching_sdkconfig = True
157+
158+
return matching_sdkconfig, cust_sdk_is_present
159+
160+
def check_reinstall_frwrk():
161+
framework_reinstall = False
162+
cust_sdk_is_present = False
163+
matching_sdkconfig = False
164+
if flag_custom_sdkconfig == True:
165+
matching_sdkconfig, cust_sdk_is_present = matching_custom_sdkconfig()
166+
if flag_custom_sdkconfig == False and flag_any_custom_sdkconfig == True:
167+
# case custom sdkconfig exists and a env without "custom_sdkconfig"
168+
framework_reinstall = True
169+
if flag_custom_sdkconfig == True and matching_sdkconfig == False:
170+
# check if current custom sdkconfig is different from existing
171+
framework_reinstall = True
172+
return framework_reinstall
173+
174+
def call_compile_libs():
175+
if mcu == "esp32c2":
176+
ARDUINO_FRMWRK_C2_LIB_DIR = join(platform.get_package_dir("framework-arduinoespressif32-libs"),mcu)
177+
ARDUINO_C2_DIR = join(platform.get_package_dir("framework-arduino-c2-skeleton-lib"),mcu)
178+
shutil.copytree(ARDUINO_C2_DIR, ARDUINO_FRMWRK_C2_LIB_DIR, dirs_exist_ok=True)
179+
print("*** Compile Arduino IDF libs for %s ***" % env["PIOENV"])
180+
SConscript("espidf.py")
181+
182+
if check_reinstall_frwrk() == True:
183+
print("*** Reinstall Arduino framework libs ***")
184+
shutil.rmtree(platform.get_package_dir("framework-arduinoespressif32-libs"))
185+
ARDUINO_FRMWRK_LIB_URL = str(platform.get_package_spec("framework-arduinoespressif32-libs")).split("uri=",1)[1][:-1]
186+
pm.install(ARDUINO_FRMWRK_LIB_URL)
187+
if flag_custom_sdkconfig == True:
188+
call_compile_libs()
189+
flag_custom_sdkconfig = False
190+
191+
if flag_custom_sdkconfig == True and flag_any_custom_sdkconfig == False:
192+
call_compile_libs()
193+
194+
if "arduino" in env.subst("$PIOFRAMEWORK") and "espidf" not in env.subst("$PIOFRAMEWORK") and env.subst("$ARDUINO_LIB_COMPILE_FLAG") in ("Inactive", "True"):
195+
if os.path.exists(join(platform.get_package_dir(
196+
"framework-arduinoespressif32"), "tools", "platformio-build.py")):
197+
PIO_BUILD = "platformio-build.py"
198+
else:
199+
PIO_BUILD = "pioarduino-build.py"
200+
SConscript(join(platform.get_package_dir("framework-arduinoespressif32"), "tools", PIO_BUILD))

0 commit comments

Comments
 (0)