Skip to content

Commit baad2eb

Browse files
committed
[lldb][headers] Create script to fix up versioning
This commit creates a Python script that fixes up the versioning information in lldb-defines.h. It also moves the build logic for fixing up the lldb headers from being in the framework only to being in the same location that we create the liblldb target. (cherry picked from commit 40793cd)
1 parent b47eaa6 commit baad2eb

File tree

7 files changed

+122
-3
lines changed

7 files changed

+122
-3
lines changed

lldb/scripts/framework-header-fix.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import argparse
1515
import os
1616
import re
17+
import shutil
1718
import subprocess
1819

1920
# Main header regexes
@@ -85,8 +86,8 @@ def remove_guards(output_directory_path, unifdef_path, unifdef_guards):
8586
if not unifdef_path:
8687
unifdef_path = shutil.which("unifdef")
8788
for current_file in os.listdir(output_directory_path):
89+
current_file = os.path.join(output_directory_path, current_file)
8890
if os.path.isfile(current_file):
89-
current_file = os.path.join(output_directory_path, current_file)
9091
subprocess_command = (
9192
[unifdef_path, "-o", current_file] + unifdef_guards + [current_file]
9293
)

lldb/scripts/version-header-fix.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Usage: <path/to/input-header.h> <path/to/output-header.h> LLDB_MAJOR_VERSION LLDB_MINOR_VERSION LLDB_PATCH_VERSION
4+
5+
This script uncomments and populates the versioning information in lldb-defines.h
6+
"""
7+
8+
import argparse
9+
import os
10+
import re
11+
12+
LLDB_VERSION_REGEX = re.compile(r"//\s*#define LLDB_VERSION\s*$", re.M)
13+
LLDB_REVISION_REGEX = re.compile(r"//\s*#define LLDB_REVISION\s*$", re.M)
14+
LLDB_VERSION_STRING_REGEX = re.compile(r"//\s*#define LLDB_VERSION_STRING\s*$", re.M)
15+
16+
17+
def main():
18+
parser = argparse.ArgumentParser()
19+
parser.add_argument("input_path")
20+
parser.add_argument("output_path")
21+
parser.add_argument("lldb_version_major")
22+
parser.add_argument("lldb_version_minor")
23+
parser.add_argument("lldb_version_patch")
24+
args = parser.parse_args()
25+
input_path = str(args.input_path)
26+
output_path = str(args.output_path)
27+
lldb_version_major = args.lldb_version_major
28+
lldb_version_minor = args.lldb_version_minor
29+
lldb_version_patch = args.lldb_version_patch
30+
31+
with open(input_path, "r") as input_file:
32+
lines = input_file.readlines()
33+
file_buffer = "".join(lines)
34+
35+
with open(output_path, "w") as output_file:
36+
# For the defines in lldb-defines.h that define the major, minor and version string
37+
# uncomment each define and populate its value using the arguments passed in.
38+
# e.g. //#define LLDB_VERSION -> #define LLDB_VERSION <LLDB_MAJOR_VERSION>
39+
file_buffer = re.sub(
40+
LLDB_VERSION_REGEX,
41+
r"#define LLDB_VERSION " + lldb_version_major,
42+
file_buffer,
43+
)
44+
45+
file_buffer = re.sub(
46+
LLDB_REVISION_REGEX,
47+
r"#define LLDB_REVISION " + lldb_version_patch,
48+
file_buffer,
49+
)
50+
file_buffer = re.sub(
51+
LLDB_VERSION_STRING_REGEX,
52+
r'#define LLDB_VERSION_STRING "{0}.{1}.{2}"'.format(
53+
lldb_version_major, lldb_version_minor, lldb_version_patch
54+
),
55+
file_buffer,
56+
)
57+
output_file.write(file_buffer)
58+
59+
60+
if __name__ == "__main__":
61+
main()

lldb/source/API/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,45 @@ else()
290290
endif()
291291
endif()
292292

293+
# Stage all headers in the include directory in the build dir.
294+
file(GLOB public_headers ${LLDB_SOURCE_DIR}/include/lldb/API/*.h)
295+
set(lldb_header_staging_dir ${CMAKE_BINARY_DIR}/include/lldb)
296+
file(GLOB root_public_headers ${LLDB_SOURCE_DIR}/include/lldb/lldb-*.h)
297+
file(GLOB root_private_headers ${LLDB_SOURCE_DIR}/include/lldb/lldb-private*.h)
298+
list(REMOVE_ITEM root_public_headers ${root_private_headers})
299+
300+
find_program(unifdef_EXECUTABLE unifdef)
301+
302+
foreach(header
303+
${public_headers}
304+
${generated_public_headers}
305+
${root_public_headers})
306+
get_filename_component(basename ${header} NAME)
307+
set(staged_header ${lldb_header_staging_dir}/${basename})
308+
309+
if(unifdef_EXECUTABLE)
310+
# unifdef returns 0 when the file is unchanged and 1 if something was changed.
311+
# That means if we successfully remove SWIG code, the build system believes
312+
# that the command has failed and stops. This is undesirable.
313+
set(copy_command ${unifdef_EXECUTABLE} -USWIG -o ${staged_header} ${header} || (exit 0))
314+
else()
315+
set(copy_command ${CMAKE_COMMAND} -E copy ${header} ${staged_header})
316+
endif()
317+
318+
add_custom_command(
319+
DEPENDS ${header} OUTPUT ${staged_header}
320+
COMMAND ${copy_command}
321+
COMMENT "LLDB headers: stage LLDB headers in include directory")
322+
323+
list(APPEND lldb_staged_headers ${staged_header})
324+
endforeach()
325+
326+
add_custom_command(TARGET liblldb POST_BUILD
327+
COMMAND ${LLDB_SOURCE_DIR}/scripts/version-header-fix.py ${LLDB_SOURCE_DIR}/include/lldb/lldb-defines.h ${lldb_header_staging_dir}/lldb-defines.h ${LLDB_VERSION_MAJOR} ${LLDB_VERSION_MINOR} ${LLDB_VERSION_PATCH}
328+
)
329+
add_custom_target(liblldb-header-staging DEPENDS ${lldb_staged_headers})
330+
add_dependencies(liblldb liblldb-header-staging)
331+
293332
if(LLDB_BUILD_FRAMEWORK)
294333
include(LLDBFramework)
295334

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This is a truncated version of lldb-defines.h used to test the script
2+
// that fixes up its versioning info.
3+
4+
// The script needs to uncomment these lines and populate the info for versioning.
5+
// #define LLDB_VERSION
6+
// #define LLDB_REVISION
7+
// #define LLDB_VERSION_STRING

lldb/test/Shell/Scripts/TestFrameworkFixScript.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Create a temp dir for output and run the framework fix script on the truncated version of SBAddress.h in the inputs dir.
22
RUN: mkdir -p %t/Outputs
3-
RUN: %python %p/../../../scripts/framework-header-fix.py lldb_main %p/Inputs/Main %t/Outputs/ --unifdef_guards=-USWIG
3+
RUN: %python %p/../../../scripts/framework-header-fix.py -f lldb_main -i %p/Inputs/Main -o %t/Outputs/ -p /usr/bin/unifdef USWIG
44

55
# Check the output
66
RUN: cat %t/Outputs/SBAddress.h | FileCheck %s

lldb/test/Shell/Scripts/TestRPCFrameworkFixScript.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Create a temp dir for output and run the framework fix script on the truncated version of SBAddress.h in the inputs dir.
22
RUN: mkdir -p %t/Outputs
3-
RUN: %python %p/../../../scripts/framework-header-fix.py lldb_rpc %p/Inputs/ %t/Outputs/
3+
RUN: %python %p/../../../scripts/framework-header-fix.py -f lldb_rpc -i %p/Inputs/Main -o %t/Outputs/ -p /usr/bin/unifdef USWIG
44

55
# Check the output
66
RUN: cat %t/Outputs/RPCSBAddress.h | FileCheck %s
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Create a temp dir for output and run the version fix script on the truncated version of lldb-defines.h in the inputs dir.
2+
RUN: mkdir -p %t/Outputs
3+
RUN: %python %p/../../../scripts/version-header-fix.py %p/Inputs/lldb-defines.h %t/Outputs/lldb-defines.h 21 0 12
4+
5+
# Check the output
6+
RUN: cat %t/Outputs/lldb-defines.h | FileCheck %s
7+
8+
# The LLDB version defines must be uncommented and filled in with the values passed into the script.
9+
CHECK: {{^}}#define LLDB_VERSION 21
10+
CHECK: {{^}}#define LLDB_REVISION 12
11+
CHECK: {{^}}#define LLDB_VERSION_STRING "21.0.12"

0 commit comments

Comments
 (0)