diff --git a/BackwardMacros.cmake b/BackwardConfig.cmake similarity index 81% rename from BackwardMacros.cmake rename to BackwardConfig.cmake index 1d138cf..1effcd8 100644 --- a/BackwardMacros.cmake +++ b/BackwardConfig.cmake @@ -68,13 +68,13 @@ if (${STACK_DETAILS_AUTO_DETECT}) LIBDL_INCLUDE_DIR LIBDL_LIBRARY) if (LIBDW_FOUND) - LIST(APPEND BACKWARD_INCLUDE_DIRS ${LIBDW_INCLUDE_DIRS}) + LIST(APPEND _BACKWARD_INCLUDE_DIRS ${LIBDW_INCLUDE_DIRS}) LIST(APPEND BACKWARD_LIBRARIES ${LIBDW_LIBRARIES}) set(STACK_DETAILS_DW TRUE) set(STACK_DETAILS_BFD FALSE) set(STACK_DETAILS_BACKTRACE_SYMBOL FALSE) elseif(LIBBFD_FOUND) - LIST(APPEND BACKWARD_INCLUDE_DIRS ${LIBBFD_INCLUDE_DIRS}) + LIST(APPEND _BACKWARD_INCLUDE_DIRS ${LIBBFD_INCLUDE_DIRS}) LIST(APPEND BACKWARD_LIBRARIES ${LIBBFD_LIBRARIES}) set(STACK_DETAILS_DW FALSE) set(STACK_DETAILS_BFD TRUE) @@ -113,14 +113,31 @@ foreach(def ${BACKWARD_DEFINITIONS}) message(STATUS "${def}") endforeach() -LIST(APPEND BACKWARD_INCLUDE_DIRS ${CMAKE_CURRENT_LIST_DIR}) +find_path(BACKWARD_INCLUDE_DIR backward.hpp PATHS ${CMAKE_CURRENT_LIST_DIR}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Backward + REQUIRED_VARS + BACKWARD_INCLUDE_DIR + BACKWARD_LIBRARIES +) +list(APPEND _BACKWARD_INCLUDE_DIRS ${BACKWARD_INCLUDE_DIR}) macro(add_backward target) - target_include_directories(${target} PRIVATE ${BACKWARD_INCLUDE_DIRS}) + target_include_directories(${target} PRIVATE ${_BACKWARD_INCLUDE_DIRS}) set_property(TARGET ${target} APPEND PROPERTY COMPILE_DEFINITIONS ${BACKWARD_DEFINITIONS}) set_property(TARGET ${target} APPEND PROPERTY LINK_LIBRARIES ${BACKWARD_LIBRARIES}) endmacro() -set(BACKWARD_INCLUDE_DIRS ${BACKWARD_INCLUDE_DIRS} CACHE INTERNAL "BACKWARD_INCLUDE_DIRS") +set(BACKWARD_INCLUDE_DIRS ${_BACKWARD_INCLUDE_DIRS} CACHE INTERNAL "_BACKWARD_INCLUDE_DIRS") set(BACKWARD_DEFINITIONS ${BACKWARD_DEFINITIONS} CACHE INTERNAL "BACKWARD_DEFINITIONS") set(BACKWARD_LIBRARIES ${BACKWARD_LIBRARIES} CACHE INTERNAL "BACKWARD_LIBRARIES") -mark_as_advanced(BACKWARD_INCLUDE_DIRS BACKWARD_DEFINITIONS BACKWARD_LIBRARIES) +mark_as_advanced(_BACKWARD_INCLUDE_DIRS BACKWARD_DEFINITIONS BACKWARD_LIBRARIES) + +if (NOT TARGET Backward::Backward) + add_library(Backward::Backward INTERFACE IMPORTED) + set_target_properties(Backward::Backward PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${_BACKWARD_INCLUDE_DIRS}" + INTERFACE_LINK_LIBRARIES "${BACKWARD_LIBRARIES}" + INTERFACE_COMPILE_DEFINITIONS "${BACKWARD_DEFINITIONS}" + ) +endif() \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 4497cd4..a5d672a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,7 +23,10 @@ cmake_minimum_required(VERSION 2.8.12) project(backward CXX) -include(BackwardMacros.cmake) +include(BackwardConfig.cmake) + +set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_CXX_STANDARD 11) ############################################################################### # COMPILER FLAGS @@ -60,12 +63,8 @@ if(BACKWARD_TESTS) add_executable(${test_name} ${src} ${ARGN}) - set_target_properties(${test_name} PROPERTIES - COMPILE_DEFINITIONS "${BACKWARD_DEFINITIONS}") - - target_include_directories(${test_name} PRIVATE ${BACKWARD_INCLUDE_DIRS}) + target_link_libraries(${test_name} PRIVATE Backward::Backward test_main) - target_link_libraries(${test_name} ${BACKWARD_LIBRARIES} test_main) add_test(NAME ${name} COMMAND ${test_name}) endmacro() @@ -90,3 +89,12 @@ if(BACKWARD_TESTS) backward_add_test(test/${test}.cpp ${BACKWARD_ENABLE}) endforeach() endif() + +install( + FILES "backward.hpp" + DESTINATION ${CMAKE_INSTALL_PREFIX}/include +) +install( + FILES "BackwardConfig.cmake" + DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/backward +) \ No newline at end of file diff --git a/README.md b/README.md index be4993f..a217566 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,14 @@ doing at your convenience. ### Integration with CMake If you are using CMake and want to use its configuration abilities to save -you the trouble, you can easily integrate Backward: +you the trouble, you can easily integrate Backward, depending on how you obtained +the library. + +#### As a subdirectory: + +In this case you have a subdirectory containing the whole repository of Backward +(eg.: using git-submodules), in this case you can do: + ``` add_subdirectory(/path/to/backward-cpp) @@ -52,16 +59,48 @@ add_subdirectory(/path/to/backward-cpp) add_executable(mytarget mysource.cpp ${BACKWARD_ENABLE}) # This will add libraries, definitions and include directories needed by backward +# by setting each property on the target. add_backward(mytarget) ``` +#### Modifying CMAKE_MODULE_PATH + +In this case you can have Backward installed as a subdirectory: + +``` +list(APPEND CMAKE_MODULE_PATH /path/to/backward-cpp) +find_package(Backward) + +# This will add libraries, definitions and include directories needed by backward +# through an IMPORTED target. +target_link_libraries(mytarget PUBLIC Backward::Backward) +``` + +Notice that this is equivalent to using the the approach that uses `add_subdirectory()`, +however it uses cmake's [imported target](https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets) mechanism. + +#### Installation through a regular package manager + +In this case you have obtained Backward through a package manager. + +Packages currently available: +- [conda-forge](https://anaconda.org/conda-forge/backward-cpp) + +``` +find_package(Backward) + +# This will add libraries, definitions and include directories needed by backward +# through an IMPORTED target. +target_link_libraries(mytarget PUBLIC Backward::Backward) +``` + ### Compile with debug info You need to compile your project with generation of debug symbols enabled, usually `-g` with clang++ and g++. Note that you can use `-g` with any level of optimization, with modern debug -information encoding like DWARF, it only takes space in the binary (it'ss not +information encoding like DWARF, it only takes space in the binary (it's not loaded in memory until your debugger or Backward makes use of it, don't worry), and it doesn't impact the code generation (at least on GNU/Linux x86\_64 for what I know).