From 9d52dc24e25eda8fc6238578544858473a70c976 Mon Sep 17 00:00:00 2001 From: Jeremy Rifkin <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sun, 23 Jul 2023 23:59:34 -0400 Subject: [PATCH] Auto config improvements (#20) --- CMakeLists.txt | 44 +++++++++++++++++---------- cmake/has_dl.cpp | 6 ++++ test/expected/macos.addr2line.txt | 25 ---------------- test/expected/macos.libdl.txt | 25 ++++++++++++++++ test/expected/macos.txt | 50 +++++++++++++++---------------- 5 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 cmake/has_dl.cpp delete mode 100644 test/expected/macos.addr2line.txt create mode 100644 test/expected/macos.libdl.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c2b212..0225ebc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,10 +104,11 @@ function(check_support var source includes libraries definitions) set(${var} ${${var}} PARENT_SCOPE) endfunction() -if(NOT MSVC) # No need to bother checking in msvc, but do check in minngw +if(MINGW OR NOT WIN32) # No need to bother checking in msvc, but do check in minngw check_support(HAS_UNWIND has_unwind.cpp "" "" "") check_support(HAS_EXECINFO has_execinfo.cpp "" "" "") check_support(HAS_BACKTRACE has_backtrace.cpp "" "backtrace" "${CPPTRACE_BACKTRACE_PATH_DEFINITION}") + check_support(HAS_DL has_dl.cpp "" "dl" "") check_support(HAS_CXXABI has_cxxabi.cpp "" "" "") if(NOT MSVC) set(STACKTRACE_LINK_LIB "stdc++_libbacktrace") @@ -115,6 +116,16 @@ if(NOT MSVC) # No need to bother checking in msvc, but do check in minngw set(STACKTRACE_LINK_LIB "") endif() check_support(HAS_STACKTRACE has_stacktrace.cpp "" "${STACKTRACE_LINK_LIB}" "") + if(APPLE) + find_program(ADDR2LINE_PATH atos PATHS ENV PATH) + else() + find_program(ADDR2LINE_PATH addr2line PATHS ENV PATH) + endif() + if("${ADDR2LINE_PATH}" STREQUAL "ADDR2LINE_PATH-NOTFOUND") + set(HAS_ADDR2LINE FALSE) + else() + set(HAS_ADDR2LINE TRUE) + endif() endif() # =============================================== Autoconfig full dump =============================================== @@ -135,12 +146,14 @@ if( ) ) # Attempt to auto-config - if(HAS_STACKTRACE AND NOT WIN32) # Our trace is better than msvc's - set(CPPTRACE_FULL_TRACE_WITH_LIBBACKTRACE On) - message(STATUS "Cpptrace auto config: Using C++23 for the full trace") - elseif(HAS_BACKTRACE AND NOT WIN32) # Mingw libbacktrace doesn't seem to be working - set(CPPTRACE_FULL_TRACE_WITH_LIBBACKTRACE On) - message(STATUS "Cpptrace auto config: Using libbacktrace for the full trace") + if(MINGW OR NOT WIN32) # Our trace is better than msvc's + if(HAS_STACKTRACE) + set(CPPTRACE_FULL_TRACE_WITH_LIBBACKTRACE On) + message(STATUS "Cpptrace auto config: Using C++23 for the full trace") + elseif(HAS_BACKTRACE AND NOT WIN32) # Mingw libbacktrace doesn't seem to be working + set(CPPTRACE_FULL_TRACE_WITH_LIBBACKTRACE On) + message(STATUS "Cpptrace auto config: Using libbacktrace for the full trace") + endif() endif() endif() @@ -168,7 +181,7 @@ if( set(CPPTRACE_UNWIND_WITH_NOTHING On) message(FATAL_ERROR "Cpptrace auto config: No unwinding back-end seems to be supported, stack tracing will not work. To compile anyway set CPPTRACE_UNWIND_WITH_NOTHING.") endif() - elseif(WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # mingw + elseif(MINGW) if(HAS_UNWIND) set(CPPTRACE_UNWIND_WITH_UNWIND On) message(STATUS "Cpptrace auto config: Using libgcc unwind for unwinding") @@ -201,18 +214,19 @@ if( ) ) # Attempt to auto-config - if(APPLE) - set(CPPTRACE_GET_SYMBOLS_WITH_LIBDL ON) - elseif(UNIX) - if(HAS_BACKTRACE) + if(UNIX OR MINGW) + if(HAS_BACKTRACE AND NOT MINGW) # not working on mingw at the moment set(CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE On) message(STATUS "Cpptrace auto config: Using libbacktrace for symbols") + elseif(HAS_ADDR2LINE) + set(CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE On) + message(STATUS "Cpptrace auto config: Using addr2line for symbols") + elseif(HAS_DL) + set(CPPTRACE_GET_SYMBOLS_WITH_LIBDL On) + message(STATUS "Cpptrace auto config: Using libdl for symbols") else() message(FATAL_ERROR "Cpptrace auto config: No symbol back-end could be automatically configured. To compile anyway set CPPTRACE_GET_SYMBOLS_WITH_NOTHING.") endif() - elseif(WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # mingw - set(CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE On) - message(STATUS "Cpptrace auto config: Using addr2line for symbols") elseif(WIN32) set(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP On) message(STATUS "Cpptrace auto config: Using dbghelp for symbols") diff --git a/cmake/has_dl.cpp b/cmake/has_dl.cpp new file mode 100644 index 0000000..efa25e5 --- /dev/null +++ b/cmake/has_dl.cpp @@ -0,0 +1,6 @@ +#include + +int main() { + Dl_info info; + dladdr(nullptr, &info); +} diff --git a/test/expected/macos.addr2line.txt b/test/expected/macos.addr2line.txt deleted file mode 100644 index 046b968..0000000 --- a/test/expected/macos.addr2line.txt +++ /dev/null @@ -1,25 +0,0 @@ -test.cpp||19||trace() -test.cpp||35||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||39||foo(int) -test.cpp||47||void foo(int, int) -test.cpp||47||void foo(int, int, int) -test.cpp||47||void foo(int, int, int, int) -test.cpp||47||void foo(int, int, int, int, int) -test.cpp||47||void foo(int, int, int, int, int, int) -test.cpp||47||void foo(int, int, int, int, int, int, int) -test.cpp||47||void foo(int, int, int, int, int, int, int, int) -test.cpp||47||void foo(int, int, int, int, int, int, int, int, int) -test.cpp||47||void foo(int, int, int, int, int, int, int, int, int, int) -test.cpp||53||function_two(int, float) -test.cpp||59||function_one(int) -test.cpp||65||main -||0|| \ No newline at end of file diff --git a/test/expected/macos.libdl.txt b/test/expected/macos.libdl.txt new file mode 100644 index 0000000..4882533 --- /dev/null +++ b/test/expected/macos.libdl.txt @@ -0,0 +1,25 @@ +build/test||0||trace() +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||foo(int) +build/test||0||void foo(int, int) +build/test||0||void foo(int, int, int) +build/test||0||void foo(int, int, int, int) +build/test||0||void foo(int, int, int, int, int) +build/test||0||void foo(int, int, int, int, int, int) +build/test||0||void foo(int, int, int, int, int, int, int) +build/test||0||void foo(int, int, int, int, int, int, int, int) +build/test||0||void foo(int, int, int, int, int, int, int, int, int) +build/test||0||void foo(int, int, int, int, int, int, int, int, int, int) +build/test||0||function_two(int, float) +build/test||0||function_one(int) +build/test||0||main +/usr/lib/dyld||0||start \ No newline at end of file diff --git a/test/expected/macos.txt b/test/expected/macos.txt index 4882533..046b968 100644 --- a/test/expected/macos.txt +++ b/test/expected/macos.txt @@ -1,25 +1,25 @@ -build/test||0||trace() -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||foo(int) -build/test||0||void foo(int, int) -build/test||0||void foo(int, int, int) -build/test||0||void foo(int, int, int, int) -build/test||0||void foo(int, int, int, int, int) -build/test||0||void foo(int, int, int, int, int, int) -build/test||0||void foo(int, int, int, int, int, int, int) -build/test||0||void foo(int, int, int, int, int, int, int, int) -build/test||0||void foo(int, int, int, int, int, int, int, int, int) -build/test||0||void foo(int, int, int, int, int, int, int, int, int, int) -build/test||0||function_two(int, float) -build/test||0||function_one(int) -build/test||0||main -/usr/lib/dyld||0||start \ No newline at end of file +test.cpp||19||trace() +test.cpp||35||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||39||foo(int) +test.cpp||47||void foo(int, int) +test.cpp||47||void foo(int, int, int) +test.cpp||47||void foo(int, int, int, int) +test.cpp||47||void foo(int, int, int, int, int) +test.cpp||47||void foo(int, int, int, int, int, int) +test.cpp||47||void foo(int, int, int, int, int, int, int) +test.cpp||47||void foo(int, int, int, int, int, int, int, int) +test.cpp||47||void foo(int, int, int, int, int, int, int, int, int) +test.cpp||47||void foo(int, int, int, int, int, int, int, int, int, int) +test.cpp||53||function_two(int, float) +test.cpp||59||function_one(int) +test.cpp||65||main +||0|| \ No newline at end of file