|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Usage: <path/to/input-directory> <path/to/output-directory> |
| 5 | +
|
| 6 | +This script is used when building LLDB.framework or LLDBRPC.framework. For each framework, local includes are converted to their respective framework includes. |
| 7 | +
|
| 8 | +This script is used in 2 ways: |
| 9 | +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>. |
| 10 | +
|
| 11 | +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> |
| 12 | +""" |
| 13 | + |
| 14 | +import argparse |
| 15 | +import os |
| 16 | +import re |
| 17 | + |
| 18 | +# Main header regexes |
| 19 | +INCLUDE_FILENAME_REGEX = re.compile( |
| 20 | + r'#include "lldb/API/(?P<include_filename>.*){0,1}"' |
| 21 | +) |
| 22 | + |
| 23 | +# RPC header regexes |
| 24 | +RPC_COMMON_REGEX = re.compile(r"#include <lldb-rpc/common/(?P<include_filename>.*)>") |
| 25 | +RPC_INCLUDE_FILENAME_REGEX = re.compile(r'#include "(?P<include_filename>.*)"') |
| 26 | + |
| 27 | + |
| 28 | +def modify_rpc_includes(input_directory_path, output_directory_path): |
| 29 | + for input_filepath in os.listdir(input_directory_path): |
| 30 | + current_input_file = os.path.join(input_directory_path, input_filepath) |
| 31 | + output_dest = os.path.join(output_directory_path, input_filepath) |
| 32 | + if os.path.isfile(current_input_file): |
| 33 | + with open(current_input_file, "r") as input_file: |
| 34 | + lines = input_file.readlines() |
| 35 | + file_buffer = "".join(lines) |
| 36 | + with open(output_dest, "w") as output_file: |
| 37 | + # Local includes must be changed to RPC framework level includes. |
| 38 | + # e.g. #include "SBDefines.h" -> #include <LLDBRPC/SBDefines.h> |
| 39 | + # Also, RPC common code includes must change to RPC framework level includes. |
| 40 | + # e.g. #include "lldb-rpc/common/RPCPublic.h" -> #include <LLDBRPC/RPCPublic.h> |
| 41 | + rpc_common_matches = RPC_COMMON_REGEX.finditer(file_buffer) |
| 42 | + rpc_include_filename_matches = RPC_INCLUDE_FILENAME_REGEX.finditer( |
| 43 | + file_buffer |
| 44 | + ) |
| 45 | + for match in rpc_common_matches: |
| 46 | + file_buffer = re.sub( |
| 47 | + match.group(), |
| 48 | + r"#include <LLDBRPC/" + match.group("include_filename") + ">", |
| 49 | + file_buffer, |
| 50 | + ) |
| 51 | + for match in rpc_include_filename_matches: |
| 52 | + file_buffer = re.sub( |
| 53 | + match.group(), |
| 54 | + r"#include <LLDBRPC/" + match.group("include_filename") + ">", |
| 55 | + file_buffer, |
| 56 | + ) |
| 57 | + output_file.write(file_buffer) |
| 58 | + |
| 59 | + |
| 60 | +def modify_main_includes(input_directory_path, output_directory_path): |
| 61 | + for input_filepath in os.listdir(input_directory_path): |
| 62 | + current_input_file = os.path.join(input_directory_path, input_filepath) |
| 63 | + output_dest = os.path.join(output_directory_path, input_filepath) |
| 64 | + if os.path.isfile(current_input_file): |
| 65 | + with open(current_input_file, "r") as input_file: |
| 66 | + lines = input_file.readlines() |
| 67 | + file_buffer = "".join(lines) |
| 68 | + with open(output_dest, "w") as output_file: |
| 69 | + # Local includes must be changed to framework level includes. |
| 70 | + # e.g. #include "lldb/API/SBDefines.h" -> #include <LLDB/SBDefines.h> |
| 71 | + regex_matches = INCLUDE_FILENAME_REGEX.finditer(file_buffer) |
| 72 | + for match in regex_matches: |
| 73 | + file_buffer = re.sub( |
| 74 | + match.group(), |
| 75 | + r"#include <LLDB/" + match.group("include_filename") + ">", |
| 76 | + file_buffer, |
| 77 | + ) |
| 78 | + output_file.write(file_buffer) |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + parser = argparse.ArgumentParser() |
| 83 | + parser.add_argument("framework", choices=["lldb_main", "lldb_rpc"]) |
| 84 | + parser.add_argument("input_directory") |
| 85 | + parser.add_argument("output_directory") |
| 86 | + args = parser.parse_args() |
| 87 | + input_directory_path = str(args.input_directory) |
| 88 | + output_directory_path = str(args.output_directory) |
| 89 | + framework_version = args.framework |
| 90 | + |
| 91 | + if framework_version == "lldb_main": |
| 92 | + modify_main_includes(input_directory_path, output_directory_path) |
| 93 | + if framework_version == "lldb_rpc": |
| 94 | + modify_rpc_includes(input_directory_path, output_directory_path) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments