Skip to content

Commit d204aa9

Browse files
[lldb][headers] Create script to fix up versioning (#141116)
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.
1 parent 96336b2 commit d204aa9

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

lldb/scripts/framework-header-fix.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,5 @@ for file in `find $1 -name "*.h"`
77
do
88
/usr/bin/sed -i.bak 's/\(#include\)[ ]*"lldb\/\(API\/\)\{0,1\}\(.*\)"/\1 <LLDB\/\3>/1' "$file"
99
/usr/bin/sed -i.bak 's|<LLDB/Utility|<LLDB|' "$file"
10-
LLDB_VERSION=`echo $2 | /usr/bin/sed -E 's/^([0-9]+).([0-9]+).([0-9]+)(.[0-9]+)?$/\\1/g'`
11-
LLDB_REVISION=`echo $2 | /usr/bin/sed -E 's/^([0-9]+).([0-9]+).([0-9]+)(.[0-9]+)?$/\\3/g'`
12-
LLDB_VERSION_STRING=`echo $2`
13-
/usr/bin/sed -i.bak "s|//#define LLDB_VERSION$|#define LLDB_VERSION $LLDB_VERSION |" "$file"
14-
/usr/bin/sed -i.bak "s|//#define LLDB_REVISION|#define LLDB_REVISION $LLDB_REVISION |" "$file"
15-
/usr/bin/sed -i.bak "s|//#define LLDB_VERSION_STRING|#define LLDB_VERSION_STRING \"$LLDB_VERSION_STRING\" |" "$file"
1610
rm -f "$file.bak"
1711
done

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
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)