@@ -780,6 +780,23 @@ def process_devices(self, dev_yaml_path: str, peripherals_dict, periph_name_map,
780780
781781 return device_types
782782
783+ def clear_build_directory (self , project_root : str ) -> bool :
784+ """Clear the build directory to ensure clean build when switching boards"""
785+ try :
786+ build_dir = os .path .join (project_root , 'build' )
787+
788+ if os .path .exists (build_dir ):
789+ self .logger .debug (f' Clearing build directory: { build_dir } ' )
790+ import shutil
791+ shutil .rmtree (build_dir )
792+ self .logger .info (' build directory cleared successfully' )
793+ else :
794+ self .logger .debug (f'build directory does not exist: { build_dir } ' )
795+ return True
796+ except Exception as e :
797+ self .logger .error (f'❌ Error clearing build directory: { e } ' )
798+ return False
799+
783800 def clear_gen_bmgr_codes_directory (self , project_root : str ) -> bool :
784801 """Clear all files and directories in the gen_bmgr_codes directory before generating new ones"""
785802 try :
@@ -808,6 +825,28 @@ def clear_gen_bmgr_codes_directory(self, project_root: str) -> bool:
808825 self .logger .error (f'❌ Error clearing gen_bmgr_codes directory: { e } ' )
809826 return False
810827
828+ def get_chip_name_from_board_path (self , board_path : str ) -> Optional [str ]:
829+ """Extract chip name from board_info.yaml file"""
830+ board_info_path = os .path .join (board_path , 'board_info.yaml' )
831+
832+ if not os .path .exists (board_info_path ):
833+ self .logger .warning (f'⚠️ board_info.yaml not found at { board_info_path } ' )
834+ return None
835+
836+ try :
837+ with open (board_info_path , 'r' , encoding = 'utf-8' ) as f :
838+ board_yml = yaml .safe_load (f )
839+ chip = board_yml .get ('chip' )
840+ if chip :
841+ self .logger .debug (f' Found chip name: { chip } ' )
842+ return chip
843+ else :
844+ self .logger .warning (f'⚠️ No chip field found in { board_info_path } ' )
845+ return None
846+ except Exception as e :
847+ self .logger .error (f'❌ Error reading board_info.yaml: { e } ' )
848+ return None
849+
811850 def get_version_info (self ):
812851 """Get version information including component version, git commit ID and date, and generation time"""
813852 import subprocess
@@ -900,6 +939,11 @@ def run(self, args):
900939 components_dir = os .path .join (project_root , 'components' )
901940 self .logger .debug (f' • Components boards: { components_dir } ' )
902941
942+ # Clear build directory to ensure clean build when switching boards
943+ if not self .clear_build_directory (project_root ):
944+ self .logger .error ('❌ Error: Failed to clear build directory!' )
945+ return False
946+
903947 # Clear gen_bmgr_codes directory before generating new files
904948 if not self .clear_gen_bmgr_codes_directory (project_root ):
905949 self .logger .error ('❌ Error: Failed to clear gen_bmgr_codes directory!' )
@@ -1076,12 +1120,25 @@ def run(self, args):
10761120 else :
10771121 # Default behavior: update sdkconfig based on board types and board selection
10781122 self .logger .debug (' Updating sdkconfig based on board types and board selection...' )
1123+
1124+ # Get chip name from board_info.yaml
1125+ board_path = all_boards .get (selected_board )
1126+ if not board_path :
1127+ self .logger .error (f'❌ Board path not found for { selected_board } ' )
1128+ return False
1129+
1130+ chip_name = self .get_chip_name_from_board_path (board_path )
1131+ if not chip_name :
1132+ self .logger .error (f'❌ Chip name not found in board_info.yaml for { selected_board } ' )
1133+ return False
1134+
10791135 result = self .sdkconfig_manager .update_sdkconfig_from_board_types (
10801136 device_types = device_types ,
10811137 peripheral_types = peripheral_types ,
10821138 sdkconfig_path = str (Path .cwd ()/ 'sdkconfig' ),
10831139 enable = True ,
1084- board_name = selected_board
1140+ board_name = selected_board ,
1141+ chip_name = chip_name
10851142 )
10861143 if result ['enabled' ]:
10871144 self .logger .info (f"✅ Updated { len (result ['enabled' ])} sdkconfig features" )
@@ -1102,21 +1159,22 @@ def run(self, args):
11021159 self .logger .warning (f'⚠️ Cannot write board info: board "{ selected_board } " not found in all_boards' )
11031160
11041161 # Setup components/gen_bmgr_codes directory and build system
1105- if not self .setup_gen_bmgr_codes_component (project_root , board_path , device_dependencies ):
1162+ if not self .setup_gen_bmgr_codes_component (project_root , board_path , device_dependencies , selected_board ):
11061163 self .logger .error ('❌ Error: Failed to setup components/gen_bmgr_codes!' )
11071164 return False
11081165
11091166 self .logger .info (f'✅ === Board configuration generation completed successfully for board: { selected_board } ===' )
11101167 return True
11111168
1112- def setup_gen_bmgr_codes_component (self , project_root : str , board_path : str , device_dependencies : dict ) -> bool :
1169+ def setup_gen_bmgr_codes_component (self , project_root : str , board_path : str , device_dependencies : dict , selected_board : str = None ) -> bool :
11131170 """
11141171 Setup components/gen_bmgr_codes directory and build system.
11151172
11161173 Args:
11171174 project_root: Path to the project root directory
11181175 board_path: Path to the selected board directory
11191176 device_dependencies: Dictionary of device dependencies
1177+ selected_board: Name of the selected board
11201178
11211179 Returns:
11221180 bool: True if setup was successful
@@ -1154,7 +1212,16 @@ def setup_gen_bmgr_codes_component(self, project_root: str, board_path: str, dev
11541212 src_dirs_str = ' ' .join (['"."' ] + board_src_dirs ) if board_src_dirs else '"."'
11551213 include_dirs_str = ' ' .join (['"."' ] + board_src_dirs ) if board_src_dirs else '"."'
11561214
1157- cmakelists_content = f"""idf_component_register(
1215+ # Add board information output to CMakeLists.txt
1216+ board_info_output = ''
1217+ if selected_board :
1218+ board_info_output = f"""# Board information output
1219+ message(STATUS "Selected Board: { selected_board } ")
1220+ message(STATUS "Board Path: { board_path if board_path else 'Not specified' } ")
1221+
1222+ """
1223+
1224+ cmakelists_content = f"""{ board_info_output } idf_component_register(
11581225 SRC_DIRS { src_dirs_str }
11591226 INCLUDE_DIRS { include_dirs_str }
11601227 REQUIRES esp_board_manager
@@ -1238,8 +1305,8 @@ def main():
12381305 formatter_class = argparse .RawDescriptionHelpFormatter ,
12391306 epilog = """
12401307Examples:
1241- python gen_bmgr_config_codes.py # Use sdkconfig and default boards
1242- python gen_bmgr_config_codes.py -b echoear_core_board_v1_0 # Specify board directly
1308+ python gen_bmgr_config_codes.py # Use sdkconfig and default boards (auto-sets CONFIG_IDF_TARGET)
1309+ python gen_bmgr_config_codes.py -b echoear_core_board_v1_0 # Specify board directly (auto-sets CONFIG_IDF_TARGET)
12431310 python gen_bmgr_config_codes.py -c /custom/boards # Add customer boards directory
12441311 python gen_bmgr_config_codes.py -c /path/to/single/board # Add single board directory
12451312 python gen_bmgr_config_codes.py -b my_board -c /custom/boards # Both options
0 commit comments