From 002bef8635c49893f06544dd8882c9f730dbd0f0 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Mon, 24 Jun 2024 17:19:10 -0600 Subject: [PATCH] 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 `` header file, such as those using libbacktrace directly. --- AK/Assertions.cpp | 12 ++++-------- AK/Backtrace.h.in | 17 +++++++++++++++++ Meta/Lagom/CMakeLists.txt | 14 +++++++++++--- 3 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 AK/Backtrace.h.in diff --git a/AK/Assertions.cpp b/AK/Assertions.cpp index 407634e18a3..9334a49ad57 100644 --- a/AK/Assertions.cpp +++ b/AK/Assertions.cpp @@ -5,15 +5,12 @@ */ #include +#include #include #include #include #include -#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) # include # define EXECINFO_BACKTRACE @@ -22,9 +19,8 @@ # define PRINT_ERROR(s) (void)fputs((s), stderr) #endif -#if defined(EXECINFO_BACKTRACE) +#if defined(AK_HAS_BACKTRACE_HEADER) # include -# include #endif #if defined(AK_OS_SERENITY) @@ -33,7 +29,7 @@ # define ERRORLN warnln #endif -#if defined(EXECINFO_BACKTRACE) +#if defined(AK_HAS_BACKTRACE_HEADER) namespace { ALWAYS_INLINE void dump_backtrace() { @@ -100,7 +96,7 @@ void ak_verification_failed(char const* message) else ERRORLN("VERIFICATION FAILED: {}", message); -#if defined(EXECINFO_BACKTRACE) +#if defined(AK_HAS_BACKTRACE_HEADER) dump_backtrace(); #endif __builtin_trap(); diff --git a/AK/Backtrace.h.in b/AK/Backtrace.h.in new file mode 100644 index 00000000000..b3ddd1576a0 --- /dev/null +++ b/AK/Backtrace.h.in @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2024, Andrew Kaster . + * + * 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 diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index bf9a6ca03b7..98bcc3256fe 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -329,9 +329,16 @@ install(TARGETS LibC LibCrypt NoCoverage EXPORT LagomTargets) add_serenity_subdirectory(AK) lagom_lib(AK ak SOURCES ${AK_SOURCES}) find_package(Backtrace) -if (Backtrace_FOUND AND NOT "${Backtrace_LIBRARIES}" STREQUAL "") - target_link_libraries(AK PRIVATE ${Backtrace_LIBRARIES}) - target_include_directories(AK PRIVATE ${Backtrace_INCLUDE_DIRS}) +configure_file(../../AK/Backtrace.h.in AK/Backtrace.h @ONLY) +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_link_libraries(AK PRIVATE ${Backtrace_LIBRARIES}) + endif() +else() + message(WARNING "Backtrace not found, stack traces will be unavailable") endif() # LibCoreMinimal @@ -361,6 +368,7 @@ install( ) install(FILES ${Lagom_BINARY_DIR}/AK/Debug.h + ${Lagom_BINARY_DIR}/AK/Backtrace.h COMPONENT Lagom_Development DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/AK" )