[Lldb-commits] [lldb] [lldb][headers] Create Python script to fix up framework headers (PR #142051)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri May 30 13:07:07 PDT 2025


================
@@ -0,0 +1,117 @@
+#!/usr/bin/env python3
+
+"""
+Usage: <path/to/input-directory> <path/to/output-directory>
+
+This script is used when building LLDB.framework or LLDBRPC.framework. For each framework, local includes are converted to their respective framework includes.
+
+This script is used in 2 ways:
+1. It is used on header files that are copied into LLDB.framework. For these files, local LLDB includes are converted into framework includes, e.g. #include "lldb/API/SBDefines.h" -> #include <LLDB/SBDefines.h>.
+
+2. It is used on header files for LLDBRPC.framework. For these files, includes of RPC common files will be converted to framework includes, e.g. #include <lldb-rpc/common/RPCCommon.h> -> #include <LLDBRPC/RPCCommon.h>. It will also change local includes to framework includes, e.g. #include "SBAddress.h" -> #include <LLDBRPC/SBAddress.h>
+"""
+
+import argparse
+import os
+import re
+
+# Main header regexes
+INCLUDE_FILENAME_REGEX = re.compile(
+    r'#include "lldb/API/(?P<include_filename>.*){0,1}"'
+)
+
+# RPC header regexes
+RPC_COMMON_REGEX = re.compile(r"#include <lldb-rpc/common/(?P<include_filename>.*)>")
+RPC_INCLUDE_FILENAME_REGEX = re.compile(r'#include "(?P<include_filename>.*)"')
+
+
+def modify_rpc_includes(input_directory_path, output_directory_path):
+    for input_filepath in os.listdir(input_directory_path):
+        current_input_file = os.path.join(input_directory_path, input_filepath)
+        output_dest = os.path.join(output_directory_path, input_filepath)
+        if os.path.isfile(current_input_file):
+            with open(current_input_file, "r") as input_file:
+                lines = input_file.readlines()
+                file_buffer = "".join(lines)
+            with open(output_dest, "w") as output_file:
+                # Local includes must be changed to RPC framework level includes.
+                # e.g. #include "SBDefines.h" -> #include <LLDBRPC/SBDefines.h>
+                # Also, RPC common code includes must change to RPC framework level includes.
+                # e.g. #include "lldb-rpc/common/RPCPublic.h" -> #include <LLDBRPC/RPCPublic.h>
+                rpc_common_matches = RPC_COMMON_REGEX.finditer(file_buffer)
+                rpc_include_filename_matches = RPC_INCLUDE_FILENAME_REGEX.finditer(
+                    file_buffer
+                )
+                for match in rpc_common_matches:
+                    file_buffer = re.sub(
+                        match.group(),
+                        r"#include <LLDBRPC/" + match.group("include_filename") + ">",
+                        file_buffer,
+                    )
+                for match in rpc_include_filename_matches:
+                    file_buffer = re.sub(
+                        match.group(),
+                        r"#include <LLDBRPC/" + match.group("include_filename") + ">",
+                        file_buffer,
+                    )
+                output_file.write(file_buffer)
+
+
+def modify_main_includes(input_directory_path, output_directory_path):
+    for input_filepath in os.listdir(input_directory_path):
+        current_input_file = os.path.join(input_directory_path, input_filepath)
+        output_dest = os.path.join(output_directory_path, input_filepath)
+        if os.path.isfile(current_input_file):
+            with open(current_input_file, "r") as input_file:
+                lines = input_file.readlines()
+                file_buffer = "".join(lines)
+            with open(output_dest, "w") as output_file:
+                # Local includes must be changed to framework level includes.
+                # e.g. #include "lldb/API/SBDefines.h" -> #include <LLDB/SBDefines.h>
+                regex_matches = INCLUDE_FILENAME_REGEX.finditer(file_buffer)
+                for match in regex_matches:
+                    file_buffer = re.sub(
+                        match.group(),
+                        r"#include <LLDB/" + match.group("include_filename") + ">",
+                        file_buffer,
+                    )
+                output_file.write(file_buffer)
+
+
+def remove_guards(output_directory_path, unifdef_guards):
+    unifdef_path = shutil.which("unifdef")
----------------
JDevlieghere wrote:

Since CMake is computing the `unifdef` binary, let's pass that in to the script. If it's not specified, we can fallback to this (e.g. for the tests). 

https://github.com/llvm/llvm-project/pull/142051


More information about the lldb-commits mailing list