https://github.com/chelcassanova created 
https://github.com/llvm/llvm-project/pull/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.

>From 8e33443fecea8592af1379ad0745ac39f4562f1c Mon Sep 17 00:00:00 2001
From: Chelsea Cassanova <chelsea_cassan...@apple.com>
Date: Thu, 22 May 2025 11:00:06 -0700
Subject: [PATCH] [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.
---
 lldb/scripts/version-header-fix.py            | 53 +++++++++++++++++++
 lldb/source/API/CMakeLists.txt                | 34 ++++++++++++
 lldb/test/Shell/Scripts/Inputs/lldb-defines.h | 16 ++++++
 .../Shell/Scripts/TestVersionFixScript.test   | 13 +++++
 4 files changed, 116 insertions(+)
 create mode 100755 lldb/scripts/version-header-fix.py
 create mode 100644 lldb/test/Shell/Scripts/Inputs/lldb-defines.h
 create mode 100644 lldb/test/Shell/Scripts/TestVersionFixScript.test

diff --git a/lldb/scripts/version-header-fix.py 
b/lldb/scripts/version-header-fix.py
new file mode 100755
index 0000000000000..ddf2c9057fdc3
--- /dev/null
+++ b/lldb/scripts/version-header-fix.py
@@ -0,0 +1,53 @@
+#!/usr/bin/env python3
+"""
+Usage: <path/to/input-header.h> <path/to/output-header.h> LLDB_MAJOR_VERSION 
LLDB_MINOR_VERSION LLDB_PATCH_VERSION
+
+This script uncomments and populates the versioning information in 
lldb-defines.h
+"""
+
+import argparse
+import os
+import re
+
+LLDB_VERSION_REGEX = re.compile(r'^//#define LLDB_VERSION$')
+LLDB_REVISION_REGEX = re.compile(r'^//#define LLDB_REVISION$')
+LLDB_VERSION_STRING_REGEX = re.compile(r'^//#define LLDB_VERSION_STRING$')
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("input_path")
+    parser.add_argument("output_path")
+    parser.add_argument("lldb_version_major")
+    parser.add_argument("lldb_version_minor")
+    parser.add_argument("lldb_version_patch")
+    args = parser.parse_args()
+    input_path = str(args.input_path)
+    output_path = str(args.output_path)
+    lldb_version_major = args.lldb_version_major
+    lldb_version_minor = args.lldb_version_minor
+    lldb_version_patch = args.lldb_version_patch
+
+    with open(input_path, "r") as input_file:
+        lines = input_file.readlines()
+
+    with open(output_path, "w") as output_file:
+        for line in lines:
+            version_match = LLDB_VERSION_REGEX.match(line)
+            revision_match = LLDB_REVISION_REGEX.match(line)
+            version_string_match = LLDB_VERSION_STRING_REGEX.match(line)
+
+            # For the defines in lldb-defines.h that define the major, minor 
and version string
+            # uncomment each define and populate its value using the arguments 
passed in.
+            # e.g. //#define LLDB_VERSION -> #define LLDB_VERSION 
<LLDB_MAJOR_VERSION>
+            if version_match:
+                output_file.write(re.sub(LLDB_VERSION_REGEX, r'#define 
LLDB_VERSION ' + lldb_version_major, line))
+            elif revision_match:
+                output_file.write(re.sub(LLDB_REVISION_REGEX, r'#define 
LLDB_REVISION ' + lldb_version_minor, line))
+            elif version_string_match:
+                output_file.write(re.sub(LLDB_VERSION_STRING_REGEX, r'#define 
LLDB_VERSION_STRING "{0}.{1}.{2}"'.format(lldb_version_major, 
lldb_version_minor, lldb_version_patch), line))
+            else:
+                output_file.write(line)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/lldb/source/API/CMakeLists.txt b/lldb/source/API/CMakeLists.txt
index 3bc569608e458..a1f2d4fee82c8 100644
--- a/lldb/source/API/CMakeLists.txt
+++ b/lldb/source/API/CMakeLists.txt
@@ -290,6 +290,40 @@ else()
   endif()
 endif()
 
+# Stage all headers in the include directory in the build dir.
+file(GLOB public_headers ${LLDB_SOURCE_DIR}/include/lldb/API/*.h)
+set(lldb_header_staging ${CMAKE_BINARY_DIR}/include/lldb)
+file(GLOB root_public_headers ${LLDB_SOURCE_DIR}/include/lldb/lldb-*.h)
+file(GLOB root_private_headers ${LLDB_SOURCE_DIR}/include/lldb/lldb-private*.h)
+list(REMOVE_ITEM root_public_headers ${root_private_headers})
+foreach(header
+    ${public_headers}
+    ${generated_public_headers}
+    ${root_public_headers})
+  get_filename_component(basename ${header} NAME)
+  set(staged_header ${lldb_header_staging}/${basename})
+
+  if(unifdef_EXECUTABLE)
+    # unifdef returns 0 when the file is unchanged and 1 if something was 
changed.
+    # That means if we successfully remove SWIG code, the build system believes
+    # that the command has failed and stops. This is undesirable.
+    set(copy_command ${unifdef_EXECUTABLE} -USWIG -o ${staged_header} 
${header} || (exit 0))
+  else()
+    set(copy_command ${CMAKE_COMMAND} -E copy ${header} ${staged_header})
+  endif()
+
+  add_custom_command(
+    DEPENDS ${header} OUTPUT ${staged_header}
+    COMMAND ${copy_command}
+    COMMENT "LLDB headers: stage LLDB headers in include directory")
+
+  list(APPEND lldb_staged_headers ${staged_header})
+endforeach()
+
+add_custom_command(TARGET liblldb POST_BUILD
+  COMMAND ${LLDB_SOURCE_DIR}/scripts/version-header-fix.py 
${LLDB_SOURCE_DIR}/include/lldb/lldb-defines.h 
${lldb_header_staging}/lldb-defines.h ${LLDB_VERSION_MAJOR} 
${LLDB_VERSION_MINOR} ${LLDB_VERSION_PATCH}
+)
+
 if(LLDB_BUILD_FRAMEWORK)
   include(LLDBFramework)
 
diff --git a/lldb/test/Shell/Scripts/Inputs/lldb-defines.h 
b/lldb/test/Shell/Scripts/Inputs/lldb-defines.h
new file mode 100644
index 0000000000000..eed1b57d35ad3
--- /dev/null
+++ b/lldb/test/Shell/Scripts/Inputs/lldb-defines.h
@@ -0,0 +1,16 @@
+//===-- lldb-defines.h ------------------------------------------*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// LLDB version
+//
+// This is a truncated version of lldb-defines.h used to test the script
+// that fixes up its versioning info.
+
+// The script needs to uncomment these lines and populate the info for 
versioning.
+//#define LLDB_VERSION
+//#define LLDB_REVISION
+//#define LLDB_VERSION_STRING
diff --git a/lldb/test/Shell/Scripts/TestVersionFixScript.test 
b/lldb/test/Shell/Scripts/TestVersionFixScript.test
new file mode 100644
index 0000000000000..6acecd172f052
--- /dev/null
+++ b/lldb/test/Shell/Scripts/TestVersionFixScript.test
@@ -0,0 +1,13 @@
+// Run the convert script on it, then run the framework include fix on it. The 
framework version fix script
+// expects that all lldb references have been renamed to lldb-rpc in order for 
it to modify the includes
+// to go into the framework.
+# RUN: mkdir -p %t/Outputs
+# RUN: %python %p/../../../scripts/version-header-fix.py 
%p/Inputs/lldb-defines.h %t/Outputs/lldb-defines.h 21 0 0
+
+// Check the output
+# RUN: cat %t/Outputs/lldb-defines.h | FileCheck %s
+
+// The LLDB version defines must be uncommented and filled in with the values 
passed into the script.
+# CHECK: {{^}}#define LLDB_VERSION 21
+# CHECK: {{^}}#define LLDB_REVISION 0
+# CHECK: {{^}}#define LLDB_VERSION_STRING "21.0.0"

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to