Skip to content

Commit c915f7c

Browse files
committed
[lldb][RPC] Upstream Python scripts
As part of upstreaming LLDB RPC, this commit adds python scripts that are used by LLDB RPC to modify the public lldb header files for use with RPC. https://discourse.llvm.org/t/rfc-upstreaming-lldb-rpc/85804
1 parent b85f37a commit c915f7c

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Usage: convert-lldb-header-to-rpc-header.py <path/to/input-header.h> <path/to/output-header.h>
3+
# This scripts takes common LLDB headers (such as lldb-defines.h) and replaces references to LLDB
4+
# with those for RPC. This happens for:
5+
# - namespace definitions
6+
# - namespace usage
7+
# - version string macros
8+
# - ifdef/ifndef lines
9+
10+
import argparse
11+
import os
12+
import re
13+
14+
15+
def main():
16+
parser = argparse.ArgumentParser()
17+
parser.add_argument("input")
18+
parser.add_argument("output")
19+
args = parser.parse_args()
20+
input_path = str(args.input)
21+
output_path = str(args.output)
22+
with open(input_path, "r") as input_file:
23+
lines = input_file.readlines()
24+
25+
with open(output_path, "w") as output_file:
26+
for line in lines:
27+
# NOTE: We do not use lldb-forward.h or lldb-versioning.h in RPC, so remove
28+
# all includes that are found for these files.
29+
if re.match(
30+
r'#include "lldb/lldb-forward|#include "lldb/lldb-versioning', line
31+
):
32+
continue
33+
# For lldb-rpc-defines.h, replace the ifndef LLDB_LLDB_ portion with LLDB_RPC_ as we're not
34+
# using LLDB private definitions in RPC.
35+
elif re.match(r".+LLDB_LLDB_", line):
36+
output_file.write(re.sub(r"LLDB_LLDB_", r"LLDB_RPC_", line))
37+
# Similarly to lldb-rpc-defines.h, replace the ifndef for LLDB_API in SBDefines.h to LLDB_RPC_API_ for the same reason.
38+
elif re.match(r".+LLDB_API_", line):
39+
output_file.write(re.sub(r"LLDB_API_", r"LLDB_RPC_API_", line))
40+
# Replace the references for the macros that define the versioning strings in
41+
# lldb-rpc-defines.h.
42+
elif re.match(r".+LLDB_VERSION", line):
43+
output_file.write(re.sub(r"LLDB_VERSION", r"LLDB_RPC_VERSION", line))
44+
elif re.match(r".+LLDB_REVISION", line):
45+
output_file.write(re.sub(r"LLDB_REVISION", r"LLDB_RPC_REVISION", line))
46+
elif re.match(r".+LLDB_VERSION_STRING", line):
47+
output_file.write(
48+
re.sub(r"LLDB_VERSION_STRING", r"LLDB_RPC_VERSION_STRING", line)
49+
)
50+
# For local #includes
51+
elif re.match(r'#include "lldb/lldb-', line):
52+
output_file.write(re.sub(r"lldb/lldb-", r"lldb-rpc-", line))
53+
# Rename the lldb namespace definition to lldb-rpc.
54+
elif re.match(r"namespace lldb", line):
55+
output_file.write(re.sub(r"lldb", r"lldb_rpc", line))
56+
# Rename namespace references
57+
elif re.match(r".+lldb::", line):
58+
output_file.write(re.sub(r"lldb::", r"lldb_rpc::", line))
59+
else:
60+
# Write any line that doesn't need to be converted
61+
output_file.write(line)
62+
63+
64+
if __name__ == "__main__":
65+
main()
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
# Usage: framework-header-include-fix.py <path/to/input-header.h> <path/to/output-header.h>
3+
# This script modifies all #include lines in all lldb-rpc headers
4+
# from either filesystem or local includes to liblldbrpc includes.
5+
6+
import argparse
7+
import os
8+
import re
9+
10+
11+
def main():
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument("input")
14+
parser.add_argument("output")
15+
args = parser.parse_args()
16+
input_path = str(args.input)
17+
output_path = str(args.output)
18+
with open(input_path, "r+") as input_file:
19+
lines = input_file.readlines()
20+
21+
with open(output_path, "w+") as output_file:
22+
for line in lines:
23+
# Replace includes from RPCCommon to liblldbrpc includes.
24+
# e.g. #include <lldb-rpc/common/RPCArgument.h> -> #include <LLDBRPC/RPCArgument.h>
25+
if re.match(r".+<lldb-rpc/common", line):
26+
output_file.write(re.sub(r"<lldb-rpc/common", r"<LLDBRPC", line))
27+
# Replace all local file includes to liblldbrpc includes.
28+
# e.g. #include "SBFoo.h" -> #include <LLDBRPC/SBFoo.h>
29+
elif re.match(r'#include "(.*)"', line):
30+
include_filename = re.search(r'#include "(.*)"', line).groups()[0]
31+
output_file.write(
32+
re.sub(
33+
r'#include "(.*)"',
34+
r"#include <LLDBRPC/" + include_filename + ">",
35+
line,
36+
)
37+
)
38+
else:
39+
# Write any line that doesn't need to be converted
40+
output_file.write(line)
41+
42+
43+
if __name__ == "__main__":
44+
main()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# Usage: framework-header-version-fix.py <path/to/input-header.h> <path/to/output-header.h> MAJOR MINOR PATCH
3+
# This script modifies lldb-rpc-defines.h to uncomment the macro defines used for the LLDB
4+
# major, minor and patch values as well as populating their definitions.
5+
6+
import argparse
7+
import os
8+
import re
9+
10+
11+
def main():
12+
parser = argparse.ArgumentParser()
13+
parser.add_argument("input")
14+
parser.add_argument("output")
15+
parser.add_argument("lldb_version_major")
16+
parser.add_argument("lldb_version_minor")
17+
parser.add_argument("lldb_version_patch")
18+
args = parser.parse_args()
19+
input_path = str(args.input)
20+
output_path = str(args.output)
21+
lldb_version_major = args.lldb_version_major
22+
lldb_version_minor = args.lldb_version_minor
23+
lldb_version_patch = args.lldb_version_patch
24+
25+
with open(input_path, "r") as input_file:
26+
lines = input_file.readlines()
27+
28+
with open(output_path, "w") as output_file:
29+
for line in lines:
30+
# Uncomment the line that defines the LLDB major version and populate its value.
31+
if re.match(r"//#define LLDB_RPC_VERSION$", line):
32+
output_file.write(
33+
re.sub(
34+
r"//#define LLDB_RPC_VERSION",
35+
r"#define LLDB_RPC_VERSION " + lldb_version_major,
36+
line,
37+
)
38+
)
39+
# Uncomment the line that defines the LLDB minor version and populate its value.
40+
elif re.match(r"//#define LLDB_RPC_REVISION$", line):
41+
output_file.write(
42+
re.sub(
43+
r"//#define LLDB_RPC_REVISION",
44+
r"#define LLDB_RPC_REVISION " + lldb_version_minor,
45+
line,
46+
)
47+
)
48+
# Uncomment the line that defines the complete LLDB version string and populate its value.
49+
elif re.match(r"//#define LLDB_RPC_VERSION_STRING$", line):
50+
output_file.write(
51+
re.sub(
52+
r"//#define LLDB_RPC_VERSION_STRING",
53+
r'#define LLDB_RPC_VERSION_STRING "{0}.{1}.{2}"'.format(
54+
lldb_version_major, lldb_version_minor, lldb_version_patch
55+
),
56+
line,
57+
)
58+
)
59+
else:
60+
# Write any line that doesn't need to be converted
61+
output_file.write(line)
62+
63+
64+
if __name__ == "__main__":
65+
main()

0 commit comments

Comments
 (0)