Browse Source

Merge topic 'FindBacktrace-imported-library'

9433755e5d FindBacktrace: Add imported library

Acked-by: Kitware Robot <[email protected]>
Merge-request: !9406
Brad King 1 năm trước cách đây
mục cha
commit
f494bbaf8f

+ 1 - 0
.gitlab/ci/configure_debian12_ninja_common.cmake

@@ -11,6 +11,7 @@ if (NOT "$ENV{CMAKE_CI_NIGHTLY}" STREQUAL "")
 endif()
 endif()
 
 
 set(CMake_TEST_FindALSA "ON" CACHE BOOL "")
 set(CMake_TEST_FindALSA "ON" CACHE BOOL "")
+set(CMake_TEST_FindBacktrace "ON" CACHE BOOL "")
 set(CMake_TEST_FindBLAS "All;static=1;Generic" CACHE STRING "")
 set(CMake_TEST_FindBLAS "All;static=1;Generic" CACHE STRING "")
 set(CMake_TEST_FindBoost "ON" CACHE BOOL "")
 set(CMake_TEST_FindBoost "ON" CACHE BOOL "")
 set(CMake_TEST_FindBoost_Python "ON" CACHE BOOL "")
 set(CMake_TEST_FindBoost_Python "ON" CACHE BOOL "")

+ 1 - 0
.gitlab/ci/configure_fedora39_makefiles.cmake

@@ -11,6 +11,7 @@ endif()
 
 
 set(CMake_TEST_ASM_NASM "ON" CACHE BOOL "")
 set(CMake_TEST_ASM_NASM "ON" CACHE BOOL "")
 set(CMake_TEST_FindALSA "ON" CACHE BOOL "")
 set(CMake_TEST_FindALSA "ON" CACHE BOOL "")
+set(CMake_TEST_FindBacktrace "ON" CACHE BOOL "")
 set(CMake_TEST_FindBLAS "All;static=1;Generic" CACHE STRING "")
 set(CMake_TEST_FindBLAS "All;static=1;Generic" CACHE STRING "")
 set(CMake_TEST_FindBoost "ON" CACHE BOOL "")
 set(CMake_TEST_FindBoost "ON" CACHE BOOL "")
 set(CMake_TEST_FindBoost_Python "ON" CACHE BOOL "")
 set(CMake_TEST_FindBoost_Python "ON" CACHE BOOL "")

+ 4 - 0
Help/release/dev/FindBacktrace-imported-library.rst

@@ -0,0 +1,4 @@
+FindBacktrace-imported-library
+------------------------------
+
+* The :module:`FindBacktrace` module now provides an imported target.

+ 22 - 0
Modules/FindBacktrace.cmake

@@ -36,6 +36,17 @@ with the contents like the following::
  #endif
  #endif
 
 
 And then reference that generated header file in actual source.
 And then reference that generated header file in actual source.
+
+Imported Targets
+^^^^^^^^^^^^^^^^
+
+.. versionadded:: 3.30
+
+This module defines the following :prop_tgt:`IMPORTED` targets:
+
+``Backtrace::Backtrace``
+  An interface library providing usage requirements for the found components.
+
 #]=======================================================================]
 #]=======================================================================]
 
 
 include(CMakePushCheckState)
 include(CMakePushCheckState)
@@ -89,3 +100,14 @@ set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing b
 
 
 find_package_handle_standard_args(Backtrace FOUND_VAR Backtrace_FOUND REQUIRED_VARS ${_Backtrace_STD_ARGS})
 find_package_handle_standard_args(Backtrace FOUND_VAR Backtrace_FOUND REQUIRED_VARS ${_Backtrace_STD_ARGS})
 mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)
 mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)
+
+if(Backtrace_FOUND AND NOT TARGET Backtrace::Backtrace)
+  if(Backtrace_LIBRARY)
+    add_library(Backtrace::Backtrace UNKNOWN IMPORTED)
+    set_property(TARGET Backtrace::Backtrace PROPERTY IMPORTED_LOCATION "${Backtrace_LIBRARY}")
+  else()
+    add_library(Backtrace::Backtrace INTERFACE IMPORTED)
+    target_link_libraries(Backtrace::Backtrace INTERFACE ${Backtrace_LIBRARIES})
+  endif()
+  target_include_directories(Backtrace::Backtrace INTERFACE ${Backtrace_INCLUDE_DIRS})
+endif()

+ 1 - 0
Tests/CMakeLists.txt

@@ -1455,6 +1455,7 @@ if(BUILD_TESTING)
   _mod
   _mod
   IN ITEMS
   IN ITEMS
       ALSA
       ALSA
+      Backtrace
       BLAS
       BLAS
       Boost
       Boost
       BZip2
       BZip2

+ 10 - 0
Tests/FindBacktrace/CMakeLists.txt

@@ -0,0 +1,10 @@
+add_test(NAME FindBacktrace.Test COMMAND
+  ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+  --build-and-test
+  "${CMake_SOURCE_DIR}/Tests/FindBacktrace/Test"
+  "${CMake_BINARY_DIR}/Tests/FindBacktrace/Test"
+  ${build_generator_args}
+  --build-project TestFindBacktrace
+  --build-options ${build_options}
+  --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+  )

+ 11 - 0
Tests/FindBacktrace/Test/CMakeLists.txt

@@ -0,0 +1,11 @@
+cmake_minimum_required(VERSION 3.29)
+project(TestFindBLAS C)
+include(CTest)
+
+find_package(Backtrace REQUIRED)
+
+add_executable(test_tgt backtrace.c)
+target_link_libraries(test_tgt Backtrace::Backtrace)
+target_compile_options(test_tgt PUBLIC -rdynamic -fno-omit-frame-pointer)
+target_link_options(test_tgt PUBLIC -rdynamic -fno-omit-frame-pointer)
+add_test(NAME test_tgt COMMAND test_tgt)

+ 53 - 0
Tests/FindBacktrace/Test/backtrace.c

@@ -0,0 +1,53 @@
+/* This is the code from `man backtrace_symbols`, reformatted, and without
+ * requiring a command-line argument */
+
+#include <execinfo.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define BT_BUF_SIZE 100
+
+void myfunc3(void)
+{
+  int nptrs;
+  void* buffer[BT_BUF_SIZE];
+  char** strings;
+
+  nptrs = backtrace(buffer, BT_BUF_SIZE);
+  printf("backtrace() returned %d addresses\n", nptrs);
+
+  /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
+     would produce similar output to the following: */
+
+  strings = backtrace_symbols(buffer, nptrs);
+  if (strings == NULL) {
+    perror("backtrace_symbols");
+    exit(EXIT_FAILURE);
+  }
+
+  for (size_t j = 0; j < nptrs; j++)
+    printf("%s\n", strings[j]);
+
+  free(strings);
+}
+
+static void /* "static" means don't export the symbol... */
+myfunc2(void)
+{
+  myfunc3();
+}
+
+void myfunc(int ncalls)
+{
+  if (ncalls > 1)
+    myfunc(ncalls - 1);
+  else
+    myfunc2();
+}
+
+int main(int argc, char* argv[])
+{
+  myfunc(5);
+  exit(EXIT_SUCCESS);
+}