FindBacktrace: Add imported library

This is to avoid (a future version of) Qt from having to wrap
FindBacktrace like [1].

[1] https://code.qt.io/cgit/qt/qtbase.git/tree/cmake/FindWrapBacktrace.cmake
This commit is contained in:
David Faure
2024-04-06 01:43:33 +02:00
committed by Brad King
parent aefd952085
commit 9433755e5d
8 changed files with 103 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ if (NOT "$ENV{CMAKE_CI_NIGHTLY}" STREQUAL "")
endif()
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_FindBoost "ON" CACHE BOOL "")
set(CMake_TEST_FindBoost_Python "ON" CACHE BOOL "")

View File

@@ -8,6 +8,7 @@ endif()
set(CMake_TEST_ASM_NASM "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_FindBoost "ON" CACHE BOOL "")
set(CMake_TEST_FindBoost_Python "ON" CACHE BOOL "")

View File

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

View File

@@ -36,6 +36,17 @@ with the contents like the following::
#endif
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)
@@ -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})
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()

View File

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

View File

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

View File

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

View File

@@ -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);
}