Skip to content

Commit

Permalink
Refactor auto-config logic out of the main CMakeLists.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Nov 4, 2024
1 parent f152788 commit 04b85a0
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 135 deletions.
141 changes: 6 additions & 135 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,139 +65,10 @@ else()
set(CPPTRACE_BACKTRACE_PATH_DEFINITION "")
endif()

# =============================================== Platform Support ===============================================
function(check_support var source includes libraries definitions)
set(CMAKE_REQUIRED_INCLUDES "${includes}")
list(APPEND CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_REQUIRED_LIBRARIES "${libraries}")
set(CMAKE_REQUIRED_DEFINITIONS "${definitions}")
string(CONCAT full_source "#include \"${source}\"" ${nonce})
check_cxx_source_compiles(${full_source} ${var})
set(${var} ${${var}} PARENT_SCOPE)
endfunction()

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_support(HAS_CXXABI has_cxxabi.cpp "" "" "")
endif()

if(NOT WIN32)
check_support(HAS_UNWIND has_unwind.cpp "" "" "")
check_support(HAS_EXECINFO has_execinfo.cpp "" "" "")
else()
check_support(HAS_STACKWALK has_stackwalk.cpp "" "dbghelp" "")
endif()

if(NOT WIN32 OR MINGW)
check_support(HAS_BACKTRACE has_backtrace.cpp "" "backtrace" "${CPPTRACE_BACKTRACE_PATH_DEFINITION}")
set(STACKTRACE_LINK_LIB "stdc++_libbacktrace")
check_support(HAS_CXX_EXCEPTION_TYPE has_cxx_exception_type.cpp "" "" "")
endif()

if(UNIX AND NOT APPLE)
check_support(HAS_DL_FIND_OBJECT has_dl_find_object.cpp "" "dl" "")
if(NOT HAS_DL_FIND_OBJECT)
check_support(HAS_DLADDR1 has_dladdr1.cpp "" "dl" "")
endif()
endif()

if(APPLE)
check_support(HAS_MACH_VM has_mach_vm.cpp "" "" "")
endif()

# =============================================== Autoconfig unwinding ===============================================
# Unwind back-ends
if(
NOT (
CPPTRACE_UNWIND_WITH_UNWIND OR
CPPTRACE_UNWIND_WITH_LIBUNWIND OR
CPPTRACE_UNWIND_WITH_EXECINFO OR
CPPTRACE_UNWIND_WITH_WINAPI OR
CPPTRACE_UNWIND_WITH_DBGHELP OR
CPPTRACE_UNWIND_WITH_NOTHING
)
)
# Attempt to auto-config
if(APPLE AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
if(HAS_EXECINFO)
set(CPPTRACE_UNWIND_WITH_EXECINFO On)
message(STATUS "Cpptrace auto config: Using execinfo.h for unwinding")
else()
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(UNIX)
if(HAS_UNWIND)
set(CPPTRACE_UNWIND_WITH_UNWIND On)
message(STATUS "Cpptrace auto config: Using libgcc unwind for unwinding")
elseif(HAS_EXECINFO)
set(CPPTRACE_UNWIND_WITH_EXECINFO On)
message(STATUS "Cpptrace auto config: Using execinfo.h for unwinding")
else()
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(MINGW OR WIN32)
if(HAS_STACKWALK)
set(CPPTRACE_UNWIND_WITH_DBGHELP On)
message(STATUS "Cpptrace auto config: Using dbghelp for unwinding")
else()
set(CPPTRACE_UNWIND_WITH_WINAPI On)
message(STATUS "Cpptrace auto config: Using winapi for unwinding")
endif()
endif()
else()
#message(STATUS "MANUAL CONFIG SPECIFIED")
endif()

# =============================================== Autoconfig symbols ===============================================
if(
NOT (
CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE OR
CPPTRACE_GET_SYMBOLS_WITH_LIBDL OR
CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE OR
CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF OR
CPPTRACE_GET_SYMBOLS_WITH_DBGHELP OR
CPPTRACE_GET_SYMBOLS_WITH_NOTHING
)
)
if(UNIX)
message(STATUS "Cpptrace auto config: Using libdwarf for symbols")
set(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF On)
elseif(MINGW)
message(STATUS "Cpptrace auto config: Using libdwarf + dbghelp for symbols")
# Use both dbghelp and libdwarf under mingw: Some files may use pdb symbols, e.g. system dlls like KERNEL32.dll and
# ntdll.dll at the very least, but also other libraries linked with may have pdb symbols.
set(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF On)
set(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP On)
else()
message(STATUS "Cpptrace auto config: Using dbghelp for symbols")
set(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP On)
endif()
endif()

# =============================================== Autoconfig demangling ===============================================
# Handle demangle configuration
if(
NOT (
CPPTRACE_DEMANGLE_WITH_CXXABI OR
CPPTRACE_DEMANGLE_WITH_WINAPI OR
CPPTRACE_DEMANGLE_WITH_NOTHING
)
)
if(HAS_CXXABI)
message(STATUS "Cpptrace auto config: Using cxxabi for demangling")
set(CPPTRACE_DEMANGLE_WITH_CXXABI On)
elseif(WIN32 AND NOT MINGW)
message(STATUS "Cpptrace auto config: Using dbghelp for demangling")
set(CPPTRACE_DEMANGLE_WITH_WINAPI On)
else()
set(CPPTRACE_DEMANGLE_WITH_NOTHING On)
endif()
else()
#message(STATUS "Manual demangling back-end specified")
endif()
# ========================================== Platform Support and Auto-config ==========================================
include(cmake/Autoconfig.cmake)

# =============================================== Now define the library ===============================================
# =================================================== Library Setup ====================================================

# Target that we can modify (can't modify ALIAS targets)
# Target name should not be the same as ${PROJECT_NAME}, causes add_subdirectory issues
Expand Down Expand Up @@ -345,7 +216,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
SET(CMAKE_CXX_ARCHIVE_FINISH "<CMAKE_RANLIB> -no_warning_for_no_symbols -c <TARGET>")
endif()

# =============================================== Apply options to build ===============================================
# =================================================== Back-end setup ===================================================

if(HAS_CXX_EXCEPTION_TYPE)
target_compile_definitions(${target_name} PUBLIC CPPTRACE_HAS_CXX_EXCEPTION_TYPE)
Expand Down Expand Up @@ -606,13 +477,13 @@ if(NOT "${CPPTRACE_HARD_MAX_FRAMES}" STREQUAL "")
target_compile_definitions(${target_name} PUBLIC CPPTRACE_HARD_MAX_FRAMES=${CPPTRACE_HARD_MAX_FRAMES})
endif()

# =============================================== Install ===============================================
# ====================================================== Install =======================================================

if(NOT CMAKE_SKIP_INSTALL_RULES)
include(cmake/InstallRules.cmake)
endif()

# =============================================== Demo/test ===============================================
# ===================================================== Demo/test ======================================================

if(CPPTRACE_BUILD_TESTING)
if(PROJECT_IS_TOP_LEVEL)
Expand Down
131 changes: 131 additions & 0 deletions cmake/Autoconfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# ================================================== Platform Support ==================================================
function(check_support var source includes libraries definitions)
set(CMAKE_REQUIRED_INCLUDES "${includes}")
list(APPEND CMAKE_REQUIRED_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_REQUIRED_LIBRARIES "${libraries}")
set(CMAKE_REQUIRED_DEFINITIONS "${definitions}")
string(CONCAT full_source "#include \"${source}\"" ${nonce})
check_cxx_source_compiles(${full_source} ${var})
set(${var} ${${var}} PARENT_SCOPE)
endfunction()

if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
check_support(HAS_CXXABI has_cxxabi.cpp "" "" "")
endif()

if(NOT WIN32)
check_support(HAS_UNWIND has_unwind.cpp "" "" "")
check_support(HAS_EXECINFO has_execinfo.cpp "" "" "")
else()
check_support(HAS_STACKWALK has_stackwalk.cpp "" "dbghelp" "")
endif()

if(NOT WIN32 OR MINGW)
check_support(HAS_BACKTRACE has_backtrace.cpp "" "backtrace" "${CPPTRACE_BACKTRACE_PATH_DEFINITION}")
set(STACKTRACE_LINK_LIB "stdc++_libbacktrace")
check_support(HAS_CXX_EXCEPTION_TYPE has_cxx_exception_type.cpp "" "" "")
endif()

if(UNIX AND NOT APPLE)
check_support(HAS_DL_FIND_OBJECT has_dl_find_object.cpp "" "dl" "")
if(NOT HAS_DL_FIND_OBJECT)
check_support(HAS_DLADDR1 has_dladdr1.cpp "" "dl" "")
endif()
endif()

if(APPLE)
check_support(HAS_MACH_VM has_mach_vm.cpp "" "" "")
endif()

# ================================================ Autoconfig unwinding ================================================
# Unwind back-ends
if(
NOT (
CPPTRACE_UNWIND_WITH_UNWIND OR
CPPTRACE_UNWIND_WITH_LIBUNWIND OR
CPPTRACE_UNWIND_WITH_EXECINFO OR
CPPTRACE_UNWIND_WITH_WINAPI OR
CPPTRACE_UNWIND_WITH_DBGHELP OR
CPPTRACE_UNWIND_WITH_NOTHING
)
)
# Attempt to auto-config
if(APPLE AND ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
if(HAS_EXECINFO)
set(CPPTRACE_UNWIND_WITH_EXECINFO On)
message(STATUS "Cpptrace auto config: Using execinfo.h for unwinding")
else()
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(UNIX)
if(HAS_UNWIND)
set(CPPTRACE_UNWIND_WITH_UNWIND On)
message(STATUS "Cpptrace auto config: Using libgcc unwind for unwinding")
elseif(HAS_EXECINFO)
set(CPPTRACE_UNWIND_WITH_EXECINFO On)
message(STATUS "Cpptrace auto config: Using execinfo.h for unwinding")
else()
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(MINGW OR WIN32)
if(HAS_STACKWALK)
set(CPPTRACE_UNWIND_WITH_DBGHELP On)
message(STATUS "Cpptrace auto config: Using dbghelp for unwinding")
else()
set(CPPTRACE_UNWIND_WITH_WINAPI On)
message(STATUS "Cpptrace auto config: Using winapi for unwinding")
endif()
endif()
else()
#message(STATUS "MANUAL CONFIG SPECIFIED")
endif()

# ================================================= Autoconfig symbols =================================================
if(
NOT (
CPPTRACE_GET_SYMBOLS_WITH_LIBBACKTRACE OR
CPPTRACE_GET_SYMBOLS_WITH_LIBDL OR
CPPTRACE_GET_SYMBOLS_WITH_ADDR2LINE OR
CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF OR
CPPTRACE_GET_SYMBOLS_WITH_DBGHELP OR
CPPTRACE_GET_SYMBOLS_WITH_NOTHING
)
)
if(UNIX)
message(STATUS "Cpptrace auto config: Using libdwarf for symbols")
set(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF On)
elseif(MINGW)
message(STATUS "Cpptrace auto config: Using libdwarf + dbghelp for symbols")
# Use both dbghelp and libdwarf under mingw: Some files may use pdb symbols, e.g. system dlls like KERNEL32.dll and
# ntdll.dll at the very least, but also other libraries linked with may have pdb symbols.
set(CPPTRACE_GET_SYMBOLS_WITH_LIBDWARF On)
set(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP On)
else()
message(STATUS "Cpptrace auto config: Using dbghelp for symbols")
set(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP On)
endif()
endif()

# =============================================== Autoconfig demangling ================================================
# Handle demangle configuration
if(
NOT (
CPPTRACE_DEMANGLE_WITH_CXXABI OR
CPPTRACE_DEMANGLE_WITH_WINAPI OR
CPPTRACE_DEMANGLE_WITH_NOTHING
)
)
if(HAS_CXXABI)
message(STATUS "Cpptrace auto config: Using cxxabi for demangling")
set(CPPTRACE_DEMANGLE_WITH_CXXABI On)
elseif(WIN32 AND NOT MINGW)
message(STATUS "Cpptrace auto config: Using dbghelp for demangling")
set(CPPTRACE_DEMANGLE_WITH_WINAPI On)
else()
set(CPPTRACE_DEMANGLE_WITH_NOTHING On)
endif()
else()
#message(STATUS "Manual demangling back-end specified")
endif()

0 comments on commit 04b85a0

Please sign in to comment.