From d908da01a280feaada9b91065b99c876810fd204 Mon Sep 17 00:00:00 2001 From: Gleb Popov <6yearold@gmail.com> Date: Fri, 8 Aug 2025 22:28:15 +0300 Subject: [PATCH] Enable building Poco::Trace on FreeBSD --- Trace/CMakeLists.txt | 10 ++++++++++ Trace/src/binary/elf.hpp | 2 +- Trace/src/elf.cpp | 2 +- Trace/src/module_base.cpp | 4 ++-- Trace/src/object.cpp | 4 ++-- Trace/src/platform/platform.hpp | 3 +++ Trace/src/platform/program_name.hpp | 12 ++++++++++++ 7 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Trace/CMakeLists.txt b/Trace/CMakeLists.txt index 85a19e5c9..ac8ac4e10 100644 --- a/Trace/CMakeLists.txt +++ b/Trace/CMakeLists.txt @@ -47,6 +47,16 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") target_compile_definitions (Trace PRIVATE CPPTRACE_DEMANGLE_WITH_CXXABI CPPTRACE_UNWIND_WITH_UNWIND) target_compile_definitions (Trace PRIVATE CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE) target_link_libraries(Trace PRIVATE backtrace) +elseif (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD") + find_library(BACKTRACE_LIBRARY backtrace REQUIRED) + cmake_path(GET BACKTRACE_LIBRARY PARENT_PATH BACKTRACE_LIBDIR) + set(BACKTRACE_INCLUDE_DIR "${BACKTRACE_LIBDIR}/../include") + cmake_path(ABSOLUTE_PATH BACKTRACE_INCLUDE_DIR NORMALIZE) + find_file(BACKTRACE_H backtrace.h PATHS ${BACKTRACE_INCLUDE_DIR} REQUIRED) + target_compile_definitions (Trace PRIVATE CPPTRACE_DEMANGLE_WITH_CXXABI CPPTRACE_UNWIND_WITH_UNWIND) + target_compile_definitions (Trace PRIVATE CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE) + target_include_directories(Trace PRIVATE ${BACKTRACE_INCLUDE_DIR}) + target_link_libraries(Trace PRIVATE ${BACKTRACE_LIBRARY}) elseif (APPLE) target_compile_definitions (Trace PRIVATE CPPTRACE_DEMANGLE_WITH_CXXABI CPPTRACE_UNWIND_WITH_UNWIND) target_compile_definitions (Trace PRIVATE CPPTRACE_GET_SYMBOLS_WITH_LIBDL) diff --git a/Trace/src/binary/elf.hpp b/Trace/src/binary/elf.hpp index f63875696..0769d816c 100644 --- a/Trace/src/binary/elf.hpp +++ b/Trace/src/binary/elf.hpp @@ -4,7 +4,7 @@ #include "utils/common.hpp" #include "utils/utils.hpp" -#if IS_LINUX +#if IS_LINUX || IS_FREEBSD #include #include diff --git a/Trace/src/elf.cpp b/Trace/src/elf.cpp index 27c2a2096..bb3721a2b 100644 --- a/Trace/src/elf.cpp +++ b/Trace/src/elf.cpp @@ -1,6 +1,6 @@ #include "binary/elf.hpp" -#if IS_LINUX +#if IS_LINUX || IS_FREEBSD #include #include diff --git a/Trace/src/module_base.cpp b/Trace/src/module_base.cpp index 12e2f7c2a..95a41692b 100644 --- a/Trace/src/module_base.cpp +++ b/Trace/src/module_base.cpp @@ -8,7 +8,7 @@ #include #include -#if IS_LINUX || IS_APPLE +#if IS_LINUX || IS_APPLE || IS_FREEBSD #include #include #if IS_APPLE @@ -23,7 +23,7 @@ namespace cpptrace { namespace detail { - #if IS_LINUX + #if IS_LINUX || IS_FREEBSD Result get_module_image_base(const std::string& object_path) { static std::mutex mutex; std::lock_guard lock(mutex); diff --git a/Trace/src/object.cpp b/Trace/src/object.cpp index caf65898f..6f0695383 100644 --- a/Trace/src/object.cpp +++ b/Trace/src/object.cpp @@ -9,7 +9,7 @@ #include #include -#if IS_LINUX || IS_APPLE +#if IS_LINUX || IS_APPLE || IS_FREEBSD #include #include #if IS_LINUX @@ -21,7 +21,7 @@ namespace cpptrace { namespace detail { - #if IS_LINUX || IS_APPLE + #if IS_LINUX || IS_APPLE || IS_FREEBSD #if defined(CPPTRACE_HAS_DL_FIND_OBJECT) || defined(CPPTRACE_HAS_DLADDR1) std::string resolve_l_name(const char* l_name) { if(l_name != nullptr && l_name[0] != 0) { diff --git a/Trace/src/platform/platform.hpp b/Trace/src/platform/platform.hpp index 89206e826..4d9aa7ea9 100644 --- a/Trace/src/platform/platform.hpp +++ b/Trace/src/platform/platform.hpp @@ -14,6 +14,9 @@ #elif defined(__APPLE__) #undef IS_APPLE #define IS_APPLE 1 +#elif defined(__FreeBSD__) + #undef IS_FREEBSD + #define IS_FREEBSD 1 #else #error "Unexpected platform" #endif diff --git a/Trace/src/platform/program_name.hpp b/Trace/src/platform/program_name.hpp index 3257244d2..da9e524fe 100644 --- a/Trace/src/platform/program_name.hpp +++ b/Trace/src/platform/program_name.hpp @@ -95,6 +95,18 @@ namespace detail { } } +#elif IS_FREEBSD + +#include + +namespace cpptrace { +namespace detail { + inline const char* program_name() { + return getprogname(); + } +} +} + #endif #endif