Skip to content

Commit 3b57d65

Browse files
chelcassanovarorth
authored andcommitted
Reland "[lldb][headers] Create script to fix up versioning" (llvm#142864)" (llvm#142871)
This relands the original commit for the versioning script in LLDB. This commit uses '>' for output from `unifdef` for platforms that have that executable but do not have the `-o` option. It also fixes the Xcode build by adding a dependency between the liblldb-header-staging target in the source/API/CMakeLists.txt the `liblldb-resource-headers` target in LLDBFramework.cmake. Original patch: llvm#141116
1 parent 91e827d commit 3b57d65

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

lldb/cmake/modules/LLDBFramework.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ endforeach()
107107
# Wrap output in a target, so lldb-framework can depend on it.
108108
add_custom_target(liblldb-resource-headers DEPENDS lldb-sbapi-dwarf-enums ${lldb_staged_headers})
109109
set_target_properties(liblldb-resource-headers PROPERTIES FOLDER "LLDB/Resources")
110+
# We're taking the header files from where they've been staged in the build directory's include folder,
111+
# so create a dependency on the build step that creates that directory.
112+
add_dependencies(liblldb-resource-headers liblldb-header-staging)
110113
add_dependencies(liblldb liblldb-resource-headers)
111114

112115
# At build time, copy the staged headers into the framework bundle (and do

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
@@ -292,6 +292,45 @@ else()
292292
endif()
293293
endif()
294294

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

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)