Skip to content

Commit 0883ef9

Browse files
authored
Enhance library mapping and ignore handling
1 parent c75c5f1 commit 0883ef9

File tree

1 file changed

+43
-56
lines changed

1 file changed

+43
-56
lines changed

builder/frameworks/component_manager.py

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,8 @@ def _map_library_to_include_path(self, lib_name: str, dir_name: str) -> str:
770770
# Arduino Core specific mappings (safe mappings that don't conflict with critical components)
771771
'esp32blearduino': 'bt',
772772
'esp32_ble_arduino': 'bt',
773+
'simpleble': 'bt',
774+
'esp-nimble-cpp': 'bt',
773775
'esp32': 'esp32',
774776
'wire': 'driver',
775777
'spi': 'driver',
@@ -841,17 +843,7 @@ def _map_library_to_include_path(self, lib_name: str, dir_name: str) -> str:
841843

842844
# Fallback: Use directory name as include path
843845
return dir_name_lower
844-
845-
def _get_original_lib_ignore_entries(self) -> List[str]:
846-
"""Get original lib_ignore entries without conversion."""
847-
try:
848-
lib_ignore = self.config.env.GetProjectOption("lib_ignore", [])
849-
if isinstance(lib_ignore, str):
850-
lib_ignore = [lib_ignore]
851-
return [str(entry).strip().lower() for entry in lib_ignore if str(entry).strip()]
852-
except Exception:
853-
return []
854-
846+
855847
def _convert_lib_name_to_include(self, lib_name: str) -> str:
856848
"""
857849
Convert library name to potential include directory name.
@@ -900,8 +892,13 @@ def _convert_lib_name_to_include(self, lib_name: str) -> str:
900892
'ble': 'bt',
901893
'bluetooth': 'bt',
902894
'bluetoothserial': 'bt',
903-
'dsp': 'esp_dsp',
904-
'esp-dsp': 'esp_dsp'
895+
'simpleble': 'bt',
896+
'esp-nimble-cpp': 'bt',
897+
'dsp': 'espressif__esp-dsp',
898+
'esp_dsp': 'espressif__esp-dsp',
899+
'dsps': 'espressif__esp-dsp',
900+
'fft2r': 'espressif__esp-dsp',
901+
'dsps_fft2r': 'espressif__esp-dsp'
905902
}
906903

907904
if cleaned_name in direct_mapping:
@@ -912,46 +909,42 @@ def _convert_lib_name_to_include(self, lib_name: str) -> str:
912909
def _remove_ignored_lib_includes(self) -> None:
913910
"""
914911
Remove include entries for ignored libraries from pioarduino-build.py.
915-
912+
916913
Processes the Arduino build script to remove CPPPATH entries for
917914
all ignored libraries. Implements protection for BT/BLE and DSP
918915
components when dependencies are detected. Uses multiple regex
919916
patterns to catch different include path formats.
920917
"""
921918
build_py_path = str(Path(self.config.arduino_libs_mcu) / "pioarduino-build.py")
922-
919+
923920
if not os.path.exists(build_py_path):
924921
self.logger.log_change("Build file not found")
925922
return
926-
923+
927924
# Check if BT/BLE dependencies exist in lib_deps
928925
bt_ble_protected = self._has_bt_ble_dependencies()
929926
if bt_ble_protected:
930927
self.logger.log_change("BT/BLE protection enabled")
931-
932-
# Get original lib_ignore entries for DSP protection check
933-
original_lib_ignore = self._get_original_lib_ignore_entries()
934-
928+
935929
try:
936930
with open(build_py_path, 'r', encoding='utf-8') as f:
937931
content = f.read()
938-
932+
939933
original_content = content
940934
total_removed = 0
941-
935+
942936
# Remove CPPPATH entries for each ignored library
943937
for lib_name in self.ignored_libs:
944938
# Skip BT-related libraries if BT/BLE dependencies are present
945939
if bt_ble_protected and self._is_bt_related_library(lib_name):
946940
self.logger.log_change(f"Protected BT library: {lib_name}")
947941
continue
948-
949-
# # Protection for DSP components - only remove if 'dsp' is explicitly in lib_ignore
950-
# if lib_name.lower() in ['dsp', 'esp_dsp', 'dsps', 'fft2r', 'dsps_fft2r', 'espressif__esp-dsp']:
951-
# if 'dsp' not in original_lib_ignore:
952-
# self.logger.log_change(f"Protected DSP component: {lib_name}")
953-
# continue
954-
942+
943+
# # Hard protection for DSP components
944+
# if lib_name.lower() in ['dsp', 'esp_dsp', 'dsps', 'fft2r', 'dsps_fft2r']:
945+
# self.logger.log_change(f"Protected DSP component: {lib_name}")
946+
# continue
947+
955948
# Multiple patterns to catch different include formats
956949
patterns = [
957950
rf'.*join\([^,]*,\s*"include",\s*"{re.escape(lib_name)}"[^)]*\),?\n',
@@ -966,28 +959,28 @@ def _remove_ignored_lib_includes(self) -> None:
966959
rf'.*Path\([^)]*\)\s*/\s*"include"\s*/\s*"{re.escape(lib_name)}"[^,\n]*,?\n',
967960
rf'.*Path\([^)]*{re.escape(lib_name)}[^)]*\)\s*/\s*"include"[^,\n]*,?\n'
968961
]
969-
962+
970963
removed_count = 0
971964
for pattern in patterns:
972965
matches = re.findall(pattern, content)
973966
if matches:
974967
content = re.sub(pattern, '', content)
975968
removed_count += len(matches)
976-
969+
977970
if removed_count > 0:
978971
self.logger.log_change(f"Ignored library: {lib_name} ({removed_count} entries)")
979972
total_removed += removed_count
980-
973+
981974
# Clean up empty lines and trailing commas
982975
content = re.sub(r'\n\s*\n', '\n', content)
983976
content = re.sub(r',\s*\n\s*\]', '\n]', content)
984-
977+
985978
# Validate and write changes
986979
if self._validate_changes(original_content, content) and content != original_content:
987980
with open(build_py_path, 'w', encoding='utf-8') as f:
988981
f.write(content)
989982
self.logger.log_change(f"Updated build file ({total_removed} total removals)")
990-
983+
991984
except (IOError, OSError) as e:
992985
self.logger.log_change(f"Error processing libraries: {str(e)}")
993986
except Exception as e:
@@ -996,47 +989,41 @@ def _remove_ignored_lib_includes(self) -> None:
996989
def _validate_changes(self, original_content: str, new_content: str) -> bool:
997990
"""
998991
Validate that the changes are reasonable and safe.
999-
992+
1000993
Performs sanity checks on the modified content to ensure that
1001994
the changes don't remove too much content or create invalid
1002995
modifications that could break the build process.
1003-
996+
1004997
Args:
1005998
original_content: Original file content before modifications
1006-
new_content: Modified file content after changes
1007-
999+
new_content: Modified file content after processing
1000+
10081001
Returns:
1009-
True if changes are valid and safe to apply
1002+
True if changes are within acceptable limits and safe to apply
10101003
"""
1011-
# Check if too much content was removed (more than 50% indicates potential error)
1012-
if len(new_content) < len(original_content) * 0.5:
1013-
self.logger.log_change("Warning: Too much content removed, skipping changes")
1014-
return False
1015-
1016-
# Check for basic Python syntax structure preservation
1017-
if 'CPPPATH' not in new_content or 'env.Append' not in new_content:
1018-
self.logger.log_change("Warning: Critical build structure missing, skipping changes")
1019-
return False
1020-
1021-
return True
1004+
original_lines = len(original_content.splitlines())
1005+
new_lines = len(new_content.splitlines())
1006+
removed_lines = original_lines - new_lines
1007+
1008+
# Don't allow removing more than 50% of the file or negative changes
1009+
return not (removed_lines > original_lines * 0.5 or removed_lines < 0)
10221010

10231011
def _backup_pioarduino_build_py(self) -> None:
10241012
"""
10251013
Create backup of the original pioarduino-build.py file.
1026-
1027-
Creates a backup of the Arduino framework's build script before
1028-
making modifications. Only operates when Arduino framework is active
1029-
and creates MCU-specific backup names to avoid conflicts.
1014+
1015+
Creates a backup copy of the Arduino build script before making
1016+
modifications. Only operates when Arduino framework is active
1017+
and uses MCU-specific backup naming to avoid conflicts.
10301018
"""
10311019
if "arduino" not in self.config.env.subst("$PIOFRAMEWORK"):
10321020
return
10331021

10341022
if not self.config.arduino_libs_mcu:
10351023
return
1036-
10371024
build_py_path = str(Path(self.config.arduino_libs_mcu) / "pioarduino-build.py")
10381025
backup_path = str(Path(self.config.arduino_libs_mcu) / f"pioarduino-build.py.{self.config.mcu}")
1039-
1026+
10401027
if os.path.exists(build_py_path) and not os.path.exists(backup_path):
10411028
shutil.copy2(build_py_path, backup_path)
10421029

0 commit comments

Comments
 (0)