#!/usr/bin/env python3 """ Automatisches Skript um BUILD_RTI conditional blocks zu CMakeLists.txt Dateien hinzuzufügen. Findet alle Dateien die RTI::nddscpp2 referenzieren und wrappt den relevanten Code in IF(BUILD_RTI). """ import os import re import sys def process_cmake_file(filepath): """Process a single CMakeLists.txt file and add BUILD_RTI conditionals if needed.""" with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() # Check if file uses RTI::nddscpp2 or other RTI targets rti_pattern = r'RTI::(nddscpp2|nddsc|ndds|rtiroutingservice)' if not re.search(rti_pattern, content): return False # No RTI usage, skip # Check if already wrapped in BUILD_RTI if re.search(r'if\s*\(\s*BUILD_RTI\s*\)', content, re.IGNORECASE): print(f" SKIP: {filepath} - already has BUILD_RTI conditional") return False print(f" PROCESS: {filepath}") # Strategy: Wrap the entire file content after initial comments/options in IF(BUILD_RTI) # But preserve cmake_minimum_required, project(), and option() statements at the top lines = content.split('\n') output_lines = [] # Track state in_header = True header_end_index = 0 indent_level = 0 for i, line in enumerate(lines): stripped = line.strip() # Skip empty lines and comments at the very beginning if in_header and (not stripped or stripped.startswith('#')): output_lines.append(line) continue # Keep cmake_minimum_required, project(), option(), set() at top level if in_header: if (stripped.startswith('cmake_minimum_required') or stripped.startswith('project(') or stripped.startswith('option(') or (stripped.startswith('set(') and 'CACHE' in stripped)): output_lines.append(line) continue else: in_header = False header_end_index = len(output_lines) output_lines.append(line) # Now wrap the non-header part in IF(BUILD_RTI) if header_end_index < len(output_lines): # Insert IF(BUILD_RTI) after header final_lines = output_lines[:header_end_index] final_lines.append('') final_lines.append('if(BUILD_RTI)') final_lines.append('') # Add remaining content with proper indentation for line in output_lines[header_end_index:]: final_lines.append(' ' + line if line.strip() else line) final_lines.append('') final_lines.append('endif()') final_lines.append('') new_content = '\n'.join(final_lines) # Write back with open(filepath, 'w', encoding='utf-8') as f: f.write(new_content) return True return False def main(): base_dir = sys.argv[1] if len(sys.argv) > 1 else '.' print(f"Scanning for CMakeLists.txt files with RTI dependencies in {base_dir}...") processed_count = 0 skipped_count = 0 for root, dirs, files in os.walk(base_dir): # Skip .git and other hidden directories dirs[:] = [d for d in dirs if not d.startswith('.')] if 'CMakeLists.txt' in files: filepath = os.path.join(root, 'CMakeLists.txt') if process_cmake_file(filepath): processed_count += 1 else: # Check if it has RTI but was skipped with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: content = f.read() if re.search(r'RTI::', content): skipped_count += 1 print(f"\n=== Summary ===") print(f"Processed (added BUILD_RTI): {processed_count} files") print(f"Skipped (already conditional or no RTI): {skipped_count} files") return 0 if __name__ == '__main__': sys.exit(main())