#!/usr/bin/env python3 """Wrap ddscore linkage in BUILD_RTI conditionals.""" import re from pathlib import Path def process_file(filepath): with open(filepath, 'r') as f: lines = f.readlines() new_lines = [] i = 0 in_link_block = False link_target = None link_section = None ddscore_lines = [] other_lines = [] block_start_idx = -1 while i < len(lines): line = lines[i] # Detect NGL_TARGET_LINK_LIBRARIES start match = re.match(r'NGL_TARGET_LINK_LIBRARIES\(\s*(\w+)', line) if match and not in_link_block: in_link_block = True link_target = match.group(1) block_start_idx = len(new_lines) new_lines.append(line) i += 1 continue if in_link_block: # Track section (PUBLIC/PRIVATE/INTERFACE) if re.match(r'\s*(PUBLIC|PRIVATE|INTERFACE)\s*$', line.strip()): link_section = line.strip() # Check for ddscore if 'ddscore' in line and 'ddscore' not in ''.join(ddscore_lines): ddscore_lines.append(line) elif ')' in line and not ddscore_lines: # End of block without ddscore in_link_block = False link_target = None link_section = None new_lines.append(line) elif ')' in line and ddscore_lines: # End of block with ddscore - need to restructure # First, write the block without ddscore new_lines.append(line) # closing paren # Find where to insert the BUILD_RTI block # Insert after the current closing paren ddscore_block = [ f'\nIF(BUILD_RTI)\n', f'NGL_TARGET_LINK_LIBRARIES({link_target}\n', ] for dl in ddscore_lines: ddscore_block.append(dl) ddscore_block.append(')\n') ddscore_block.append('ENDIF(BUILD_RTI)\n') # Insert after current position for j, dl in enumerate(ddscore_block): new_lines.insert(len(new_lines) - len(ddscore_lines) + j, dl) in_link_block = False link_target = None link_section = None ddscore_lines = [] else: other_lines.append(line) else: new_lines.append(line) i += 1 return ''.join(new_lines) base = Path('/home/torsten/swy/sandbox/qt-migration-legatus/qt6-after/qt6-migrated-legatus') for f in base.rglob('CMakeLists.txt'): content = f.read_text() if 'ddscore' in content: # Simple approach: wrap entire NGL_TARGET_LINK_LIBRARIES blocks containing ddscore pattern = r'(NGL_TARGET_LINK_LIBRARIES\([^)]*ddscore[^)]*\))' def repl(m): return f'IF(BUILD_RTI)\n{m.group(1)}\nENDIF(BUILD_RTI)' new_content = re.sub(pattern, repl, content, flags=re.DOTALL) if new_content != content: f.write_text(new_content) print(f"Fixed: {f}") print("Done!")