Skip to content

Commit

Permalink
Merge pull request #45 from edisongustavo/master
Browse files Browse the repository at this point in the history
Add support to find_package(Backward)
  • Loading branch information
bombela committed Nov 27, 2016
2 parents e371aa9 + b3380e2 commit d820636
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
29 changes: 23 additions & 6 deletions BackwardMacros.cmake → BackwardConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
20 changes: 14 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand All @@ -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
)
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,63 @@ 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)
# This will add backward.cpp to your target
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).
Expand Down

0 comments on commit d820636

Please sign in to comment.