Skip to content

Commit edd490a

Browse files
authored
shorten include path by using prefix
1 parent 6fd3dd5 commit edd490a

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

builder/frameworks/arduino.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from os.path import join
3131

3232
from SCons.Script import COMMAND_LINE_TARGETS, DefaultEnvironment, SConscript
33+
from platformio import fs
3334
from platformio.package.version import pepver_to_semver
3435
from platformio.project.config import ProjectConfig
3536
from platformio.package.manager.tool import ToolPackageManager
@@ -55,6 +56,8 @@
5556
framework_reinstall = False
5657
flag_any_custom_sdkconfig = False
5758

59+
FRAMEWORK_LIB_DIR = platform.get_package_dir("framework-arduinoespressif32-libs")
60+
5861
SConscript("_embed_files.py", exports="env")
5962

6063
flag_any_custom_sdkconfig = os.path.exists(join(platform.get_package_dir("framework-arduinoespressif32-libs"),"sdkconfig"))
@@ -171,6 +174,59 @@ def check_reinstall_frwrk():
171174
framework_reinstall = True
172175
return framework_reinstall
173176

177+
178+
FRAMEWORK_SDK_DIR = fs.to_unix_path(
179+
os.path.join(
180+
FRAMEWORK_LIB_DIR,
181+
mcu,
182+
"include",
183+
)
184+
)
185+
186+
IS_INTEGRATION_DUMP = env.IsIntegrationDump()
187+
188+
189+
def is_framework_subfolder(potential_subfolder):
190+
if not os.path.isabs(potential_subfolder):
191+
return False
192+
if (
193+
os.path.splitdrive(FRAMEWORK_SDK_DIR)[0]
194+
!= os.path.splitdrive(potential_subfolder)[0]
195+
):
196+
return False
197+
return os.path.commonpath([FRAMEWORK_SDK_DIR]) == os.path.commonpath(
198+
[FRAMEWORK_SDK_DIR, potential_subfolder]
199+
)
200+
201+
202+
def shorthen_includes(env, node):
203+
if IS_INTEGRATION_DUMP:
204+
# Don't shorten include paths for IDE integrations
205+
return node
206+
207+
includes = [fs.to_unix_path(inc) for inc in env.get("CPPPATH", [])]
208+
shortened_includes = []
209+
generic_includes = []
210+
for inc in includes:
211+
if is_framework_subfolder(inc):
212+
shortened_includes.append(
213+
"-iwithprefix/"
214+
+ fs.to_unix_path(os.path.relpath(inc, FRAMEWORK_SDK_DIR))
215+
)
216+
else:
217+
generic_includes.append(inc)
218+
219+
return env.Object(
220+
node,
221+
CPPPATH=generic_includes,
222+
CCFLAGS=env["CCFLAGS"]
223+
+ ["-iprefix", FRAMEWORK_SDK_DIR]
224+
+ shortened_includes,
225+
ASFLAGS=env["ASFLAGS"]
226+
+ ["-iprefix", FRAMEWORK_SDK_DIR]
227+
+ shortened_includes,
228+
)
229+
174230
def call_compile_libs():
175231
if mcu == "esp32c2":
176232
ARDUINO_FRMWRK_C2_LIB_DIR = join(platform.get_package_dir("framework-arduinoespressif32-libs"),mcu)
@@ -192,11 +248,10 @@ def call_compile_libs():
192248
call_compile_libs()
193249

194250
if "arduino" in env.subst("$PIOFRAMEWORK") and "espidf" not in env.subst("$PIOFRAMEWORK") and env.subst("$ARDUINO_LIB_COMPILE_FLAG") in ("Inactive", "True"):
251+
env.AddBuildMiddleware(shorthen_includes)
195252
if os.path.exists(join(platform.get_package_dir(
196253
"framework-arduinoespressif32"), "tools", "platformio-build.py")):
197254
PIO_BUILD = "platformio-build.py"
198255
else:
199256
PIO_BUILD = "pioarduino-build.py"
200-
SConscript(
201-
join(DefaultEnvironment().PioPlatform().get_package_dir(
202-
"framework-arduinoespressif32"), "tools", PIO_BUILD))
257+
SConscript(join(platform.get_package_dir("framework-arduinoespressif32"), "tools", PIO_BUILD))

0 commit comments

Comments
 (0)