#!/usr/bin/env python3 """ Script to wrap ddscore linkage in BUILD_RTI conditionals across all CMakeLists.txt files. This is needed because ddscore (RTI DDS) is proprietary and only available when BUILD_RTI=ON. """ import os import re from pathlib import Path def fix_ddscore_linkage(content, filepath): """ Fix ddscore linkage by wrapping it in BUILD_RTI conditionals. Handles patterns like: NGL_TARGET_LINK_LIBRARIES(target PUBLIC ddscore OtherLib ) Becomes: NGL_TARGET_LINK_LIBRARIES(target PUBLIC OtherLib ) IF(BUILD_RTI) NGL_TARGET_LINK_LIBRARIES(target PUBLIC ddscore ) ENDIF(BUILD_RTI) """ original_content = content changes_made = False # Pattern to match NGL_TARGET_LINK_LIBRARIES blocks containing ddscore # This is complex because we need to handle multi-line blocks pattern = r'(NGL_TARGET_LINK_LIBRARIES\(\s*\w+\s*)((?:[^)]*?)(ddscore)(?:[^)]*?))(\))' def replace_ddscore(match): nonlocal changes_made changes_made = True prefix = match.group(1) libs_block = match.group(2) suffix = match.group(4) # Split libs_block into lines and separate ddscore from other libs lines = libs_block.split('\n') ddscore_lines = [] other_lines = [] current_section = None for line in lines: stripped = line.strip() if stripped in ['PUBLIC', 'PRIVATE', 'INTERFACE']: current_section = stripped if 'ddscore' in line: ddscore_lines.append(line) else: other_lines.append(line) # Build new content result = prefix # Add non-ddscore libs if other_lines: result += '\n'.join(other_lines) else: # If ddscore was the only lib, we still need the section keyword # Find the section keyword from ddscore_lines for line in ddscore_lines: if 'PUBLIC' in line or 'PRIVATE' in line or 'INTERFACE' in line: # Extract section and add empty if 'PUBLIC' in line: result += '\n PUBLIC' elif 'PRIVATE' in line: result += '\n PRIVATE' elif 'INTERFACE' in line: result += '\n INTERFACE' break result += '\n' + suffix # Add BUILD_RTI conditional for ddscore if ddscore_lines: result += '\n\nIF(BUILD_RTI)\nNGL_TARGET_LINK_LIBRARIES(' # Extract target name from prefix target_match = re.search(r'NGL_TARGET_LINK_LIBRARIES\(\s*(\w+)', prefix) if target_match: target = target_match.group(1) result += target # Add the ddscore lines with proper section for line in ddscore_lines: result += '\n' + line result += '\n)\nENDIF(BUILD_RTI)' return result # Apply the replacement new_content = re.sub(pattern, replace_ddscore, content, flags=re.DOTALL) # Also handle simpler cases where ddscore is on its own line in TARGET_LINK_LIBRARIES simple_pattern = r'(TARGET_LINK_LIBRARIES\([^)]*)(ddscore)([^)]*\))' new_content = re.sub(simple_pattern, r'\1\3\nIF(BUILD_RTI)\nTARGET_LINK_LIBRARIES(\1\2\3\nENDIF(BUILD_RTI)', new_content) return new_content, changes_made def process_file(filepath): """Process a single CMakeLists.txt file.""" try: with open(filepath, 'r') as f: content = f.read() new_content, changes_made = fix_ddscore_linkage(content, filepath) if changes_made: with open(filepath, 'w') as f: f.write(new_content) print(f"✓ Fixed: {filepath}") return True return False except Exception as e: print(f"✗ Error processing {filepath}: {e}") return False def main(): base_dir = Path('/home/torsten/swy/sandbox/qt-migration-legatus/qt6-after/qt6-migrated-legatus') # Find all CMakeLists.txt files cmake_files = list(base_dir.rglob('CMakeLists.txt')) print(f"Found {len(cmake_files)} CMakeLists.txt files") fixed_count = 0 checked_count = 0 for filepath in cmake_files: try: with open(filepath, 'r') as f: content = f.read() if 'ddscore' in content: checked_count += 1 if process_file(filepath): fixed_count += 1 except Exception as e: print(f"Error checking {filepath}: {e}") print(f"\nSummary:") print(f" Files checked: {checked_count}") print(f" Files fixed: {fixed_count}") if __name__ == '__main__': main()