AK+CMake: Use the find module to find the correct backtrace(3) header

As recommended by the CMake docs, let's tolerate systems or setups that
don't have backtrace(3) in the `<execinfo.h>` header file, such as those
using libbacktrace directly.
This commit is contained in:
Andrew Kaster 2024-06-24 17:19:10 -06:00
parent 29b4f21c7b
commit 002bef8635
Notes: sideshowbarker 2024-07-16 21:51:02 +09:00
3 changed files with 32 additions and 11 deletions

View file

@ -5,15 +5,12 @@
*/ */
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Backtrace.h>
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/Platform.h> #include <AK/Platform.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#if (defined(AK_OS_LINUX) && !defined(AK_OS_ANDROID)) || defined(AK_LIBC_GLIBC) || defined(AK_OS_BSD_GENERIC) || defined(AK_OS_SOLARIS) || defined(AK_OS_HAIKU)
# define EXECINFO_BACKTRACE
#endif
#if defined(AK_OS_ANDROID) && (__ANDROID_API__ >= 33) #if defined(AK_OS_ANDROID) && (__ANDROID_API__ >= 33)
# include <android/log.h> # include <android/log.h>
# define EXECINFO_BACKTRACE # define EXECINFO_BACKTRACE
@ -22,9 +19,8 @@
# define PRINT_ERROR(s) (void)fputs((s), stderr) # define PRINT_ERROR(s) (void)fputs((s), stderr)
#endif #endif
#if defined(EXECINFO_BACKTRACE) #if defined(AK_HAS_BACKTRACE_HEADER)
# include <cxxabi.h> # include <cxxabi.h>
# include <execinfo.h>
#endif #endif
#if defined(AK_OS_SERENITY) #if defined(AK_OS_SERENITY)
@ -33,7 +29,7 @@
# define ERRORLN warnln # define ERRORLN warnln
#endif #endif
#if defined(EXECINFO_BACKTRACE) #if defined(AK_HAS_BACKTRACE_HEADER)
namespace { namespace {
ALWAYS_INLINE void dump_backtrace() ALWAYS_INLINE void dump_backtrace()
{ {
@ -100,7 +96,7 @@ void ak_verification_failed(char const* message)
else else
ERRORLN("VERIFICATION FAILED: {}", message); ERRORLN("VERIFICATION FAILED: {}", message);
#if defined(EXECINFO_BACKTRACE) #if defined(AK_HAS_BACKTRACE_HEADER)
dump_backtrace(); dump_backtrace();
#endif #endif
__builtin_trap(); __builtin_trap();

17
AK/Backtrace.h.in Normal file
View file

@ -0,0 +1,17 @@
/*
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#cmakedefine Backtrace_FOUND
#if defined(Backtrace_FOUND)
# define AK_HAS_BACKTRACE_HEADER
# undef Backtrace_FOUND
#endif
#if defined(AK_HAS_BACKTRACE_HEADER)
# include <@Backtrace_HEADER@>
#endif

View file

@ -329,9 +329,16 @@ install(TARGETS LibC LibCrypt NoCoverage EXPORT LagomTargets)
add_serenity_subdirectory(AK) add_serenity_subdirectory(AK)
lagom_lib(AK ak SOURCES ${AK_SOURCES}) lagom_lib(AK ak SOURCES ${AK_SOURCES})
find_package(Backtrace) find_package(Backtrace)
if (Backtrace_FOUND AND NOT "${Backtrace_LIBRARIES}" STREQUAL "") configure_file(../../AK/Backtrace.h.in AK/Backtrace.h @ONLY)
target_link_libraries(AK PRIVATE ${Backtrace_LIBRARIES}) if (Backtrace_FOUND)
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.30)
target_link_libraries(AK PRIVATE Backtrace::Backtrace)
else()
target_include_directories(AK PRIVATE ${Backtrace_INCLUDE_DIRS}) target_include_directories(AK PRIVATE ${Backtrace_INCLUDE_DIRS})
target_link_libraries(AK PRIVATE ${Backtrace_LIBRARIES})
endif()
else()
message(WARNING "Backtrace not found, stack traces will be unavailable")
endif() endif()
# LibCoreMinimal # LibCoreMinimal
@ -361,6 +368,7 @@ install(
) )
install(FILES install(FILES
${Lagom_BINARY_DIR}/AK/Debug.h ${Lagom_BINARY_DIR}/AK/Debug.h
${Lagom_BINARY_DIR}/AK/Backtrace.h
COMPONENT Lagom_Development COMPONENT Lagom_Development
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/AK" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/AK"
) )