Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 40 additions & 27 deletions builder/frameworks/component_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ def _map_library_to_include_path(self, lib_name: str, dir_name: str) -> str:
# Arduino Core specific mappings (safe mappings that don't conflict with critical components)
'esp32blearduino': 'bt',
'esp32_ble_arduino': 'bt',
'simpleble': 'bt',
'esp-nimble-cpp': 'bt',
'esp32': 'esp32',
'wire': 'driver',
'spi': 'driver',
Expand Down Expand Up @@ -824,7 +826,11 @@ def _map_library_to_include_path(self, lib_name: str, dir_name: str) -> str:
'tcpip': 'lwip',
'usb': 'arduino_tinyusb',
'tinyusb': 'arduino_tinyusb',
'arduino_tinyusb': 'arduino_tinyusb'
'dsp': 'espressif__esp-dsp',
'esp_dsp': 'espressif__esp-dsp',
'dsps': 'espressif__esp-dsp',
'fft2r': 'espressif__esp-dsp',
'dsps_fft2r': 'espressif__esp-dsp'
}

# Check extended mapping first
Expand Down Expand Up @@ -885,7 +891,14 @@ def _convert_lib_name_to_include(self, lib_name: str) -> str:
direct_mapping = {
'ble': 'bt',
'bluetooth': 'bt',
'bluetoothserial': 'bt'
'bluetoothserial': 'bt',
'simpleble': 'bt',
'esp-nimble-cpp': 'bt',
'dsp': 'espressif__esp-dsp',
'esp_dsp': 'espressif__esp-dsp',
'dsps': 'espressif__esp-dsp',
'fft2r': 'espressif__esp-dsp',
'dsps_fft2r': 'espressif__esp-dsp'
}

if cleaned_name in direct_mapping:
Expand All @@ -896,42 +909,42 @@ def _convert_lib_name_to_include(self, lib_name: str) -> str:
def _remove_ignored_lib_includes(self) -> None:
"""
Remove include entries for ignored libraries from pioarduino-build.py.

Processes the Arduino build script to remove CPPPATH entries for
all ignored libraries. Implements protection for BT/BLE and DSP
components when dependencies are detected. Uses multiple regex
patterns to catch different include path formats.
"""
build_py_path = str(Path(self.config.arduino_libs_mcu) / "pioarduino-build.py")

if not os.path.exists(build_py_path):
self.logger.log_change("Build file not found")
return

# Check if BT/BLE dependencies exist in lib_deps
bt_ble_protected = self._has_bt_ble_dependencies()
if bt_ble_protected:
self.logger.log_change("BT/BLE protection enabled")

try:
with open(build_py_path, 'r', encoding='utf-8') as f:
content = f.read()

original_content = content
total_removed = 0

# Remove CPPPATH entries for each ignored library
for lib_name in self.ignored_libs:
# Skip BT-related libraries if BT/BLE dependencies are present
if bt_ble_protected and self._is_bt_related_library(lib_name):
self.logger.log_change(f"Protected BT library: {lib_name}")
continue
# Hard protection for DSP components
if lib_name.lower() in ['dsp', 'esp_dsp', 'dsps', 'fft2r', 'dsps_fft2r']:
self.logger.log_change(f"Protected DSP component: {lib_name}")
continue

# # Hard protection for DSP components
# if lib_name.lower() in ['dsp', 'esp_dsp', 'dsps', 'fft2r', 'dsps_fft2r']:
# self.logger.log_change(f"Protected DSP component: {lib_name}")
# continue

# Multiple patterns to catch different include formats
patterns = [
rf'.*join\([^,]*,\s*"include",\s*"{re.escape(lib_name)}"[^)]*\),?\n',
Expand All @@ -946,55 +959,55 @@ def _remove_ignored_lib_includes(self) -> None:
rf'.*Path\([^)]*\)\s*/\s*"include"\s*/\s*"{re.escape(lib_name)}"[^,\n]*,?\n',
rf'.*Path\([^)]*{re.escape(lib_name)}[^)]*\)\s*/\s*"include"[^,\n]*,?\n'
]

removed_count = 0
for pattern in patterns:
matches = re.findall(pattern, content)
if matches:
content = re.sub(pattern, '', content)
removed_count += len(matches)

if removed_count > 0:
self.logger.log_change(f"Ignored library: {lib_name} ({removed_count} entries)")
total_removed += removed_count

# Clean up empty lines and trailing commas
content = re.sub(r'\n\s*\n', '\n', content)
content = re.sub(r',\s*\n\s*\]', '\n]', content)

# Validate and write changes
if self._validate_changes(original_content, content) and content != original_content:
with open(build_py_path, 'w', encoding='utf-8') as f:
f.write(content)
self.logger.log_change(f"Updated build file ({total_removed} total removals)")

except (IOError, OSError) as e:
self.logger.log_change(f"Error processing libraries: {str(e)}")
except Exception as e:
self.logger.log_change(f"Unexpected error processing libraries: {str(e)}")

def _validate_changes(self, original_content: str, new_content: str) -> bool:
"""
Validate that the changes are reasonable and safe.

Performs sanity checks on the modified content to ensure that
the changes don't remove too much content or create invalid
modifications that could break the build process.

Args:
original_content: Original file content before modifications
new_content: Modified file content after processing

Returns:
True if changes are within acceptable limits and safe to apply
"""
original_lines = len(original_content.splitlines())
new_lines = len(new_content.splitlines())
removed_lines = original_lines - new_lines

# Don't allow removing more than 50% of the file or negative changes
return not (removed_lines > original_lines * 0.5 or removed_lines < 0)

def _backup_pioarduino_build_py(self) -> None:
"""
Create backup of the original pioarduino-build.py file.
Expand All @@ -1005,12 +1018,12 @@ def _backup_pioarduino_build_py(self) -> None:
"""
if "arduino" not in self.config.env.subst("$PIOFRAMEWORK"):
return

if not self.config.arduino_libs_mcu:
return
build_py_path = str(Path(self.config.arduino_libs_mcu) / "pioarduino-build.py")
backup_path = str(Path(self.config.arduino_libs_mcu) / f"pioarduino-build.py.{self.config.mcu}")

if os.path.exists(build_py_path) and not os.path.exists(backup_path):
shutil.copy2(build_py_path, backup_path)

Expand Down
5 changes: 5 additions & 0 deletions examples/arduino-rmt-blink/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ lib_ignore = ble
Zigbee
Matter
OpenThread
dsp
build_flags = -DBUILTIN_RGBLED_PIN=18
-DNR_OF_LEDS=1

Expand All @@ -27,6 +28,7 @@ lib_ignore = ble
Zigbee
Matter
OpenThread
dsp
build_flags = -DBUILTIN_RGBLED_PIN=48
-DNR_OF_LEDS=1

Expand All @@ -43,6 +45,7 @@ lib_ignore = ble
Zigbee
Matter
OpenThread
dsp
build_flags = -DBUILTIN_RGBLED_PIN=8
-DNR_OF_LEDS=1

Expand All @@ -59,6 +62,7 @@ lib_ignore = ble
Zigbee
Matter
OpenThread
dsp
build_flags = -DBUILTIN_RGBLED_PIN=27
-DNR_OF_LEDS=1

Expand All @@ -75,5 +79,6 @@ lib_ignore = ble
Zigbee
Matter
OpenThread
dsp
build_flags = -DBUILTIN_RGBLED_PIN=8
-DNR_OF_LEDS=1