Skip to content

Commit a8bf4ff

Browse files
authored
Merge branch 'master' into master
2 parents 8cb73c3 + 1ec9bc3 commit a8bf4ff

3 files changed

Lines changed: 102 additions & 67 deletions

File tree

BackwardConfig.cmake

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ set(STACK_DETAILS_BFD FALSE CACHE BOOL
4343
set(STACK_DETAILS_DWARF FALSE CACHE BOOL
4444
"Use libdwarf/libelf to read debug info")
4545

46-
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND NOT DEFINED BACKWARD_TESTS)
47-
# If this is a top level CMake project, we most lixely want the tests
48-
set(BACKWARD_TESTS ON CACHE BOOL "Enable tests")
49-
else()
50-
set(BACKWARD_TESTS OFF CACHE BOOL "Enable tests")
51-
endif()
5246
###############################################################################
5347
# CONFIGS
5448
###############################################################################
@@ -222,7 +216,10 @@ if(WIN32)
222216
endif()
223217
endif()
224218

225-
set(BACKWARD_INCLUDE_DIR "${CMAKE_CURRENT_LIST_DIR}")
219+
set(BACKWARD_INCLUDE_DIR
220+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
221+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
222+
)
226223

227224
set(BACKWARD_HAS_EXTERNAL_LIBRARIES FALSE)
228225
set(FIND_PACKAGE_REQUIRED_VARS BACKWARD_INCLUDE_DIR)
@@ -239,6 +236,9 @@ list(APPEND _BACKWARD_INCLUDE_DIRS ${BACKWARD_INCLUDE_DIR})
239236

240237
# add_backward, optional bool argument; if passed and true, backward will be included as a system header
241238
macro(add_backward target)
239+
message(DEPRECATION "The add_backward() macro is deprecated, use target_link_libraries() to link to "
240+
"one of the exported targets: Backward::Interface, Backward::Object, or Backward::Backward."
241+
)
242242
if ("${ARGN}")
243243
target_include_directories(${target} SYSTEM PRIVATE ${BACKWARD_INCLUDE_DIRS})
244244
else()
@@ -248,7 +248,7 @@ macro(add_backward target)
248248
set_property(TARGET ${target} APPEND PROPERTY LINK_LIBRARIES ${BACKWARD_LIBRARIES})
249249
endmacro()
250250

251-
set(BACKWARD_INCLUDE_DIRS ${_BACKWARD_INCLUDE_DIRS} CACHE INTERNAL "_BACKWARD_INCLUDE_DIRS")
251+
set(BACKWARD_INCLUDE_DIRS ${_BACKWARD_INCLUDE_DIRS} CACHE INTERNAL "BACKWARD_INCLUDE_DIRS")
252252
set(BACKWARD_DEFINITIONS ${_BACKWARD_DEFINITIONS} CACHE INTERNAL "BACKWARD_DEFINITIONS")
253253
set(BACKWARD_LIBRARIES ${_BACKWARD_LIBRARIES} CACHE INTERNAL "BACKWARD_LIBRARIES")
254254
mark_as_advanced(BACKWARD_INCLUDE_DIRS BACKWARD_DEFINITIONS BACKWARD_LIBRARIES)
@@ -263,15 +263,8 @@ foreach(var ${BACKWARD_DEFINITIONS})
263263
mark_as_advanced(${var_name})
264264
endforeach()
265265

266-
if (NOT TARGET Backward::Backward)
267-
add_library(Backward::Backward INTERFACE IMPORTED)
268-
set_target_properties(Backward::Backward PROPERTIES
269-
INTERFACE_INCLUDE_DIRECTORIES "${BACKWARD_INCLUDE_DIRS}"
270-
INTERFACE_COMPILE_DEFINITIONS "${BACKWARD_DEFINITIONS}"
271-
)
272-
if(BACKWARD_HAS_EXTERNAL_LIBRARIES)
273-
set_target_properties(Backward::Backward PROPERTIES
274-
INTERFACE_LINK_LIBRARIES "${BACKWARD_LIBRARIES}"
275-
)
276-
endif()
266+
# if this file is used from the install tree by find_package(), include the
267+
# file CMake-generated file where the targets are defined
268+
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/BackwardTargets.cmake)
269+
include(${CMAKE_CURRENT_LIST_DIR}/BackwardTargets.cmake)
277270
endif()

CMakeLists.txt

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ include(GNUInstallDirs)
3131

3232
include(BackwardConfig.cmake)
3333

34+
###############################################################################
35+
# OPTIONS
36+
###############################################################################
37+
38+
option(BACKWARD_SHARED "Build backward as a shared library" OFF)
39+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR AND NOT DEFINED BACKWARD_TESTS)
40+
# If this is a top level CMake project, we most lixely want the tests
41+
set(BACKWARD_TESTS ON CACHE BOOL "Enable tests")
42+
else()
43+
set(BACKWARD_TESTS OFF CACHE BOOL "Enable tests")
44+
endif()
45+
46+
###############################################################################
47+
# COMPILER FLAGS
48+
###############################################################################
49+
3450
# check if compiler is nvcc or nvcc_wrapper
3551
set(COMPILER_IS_NVCC false)
3652
get_filename_component(COMPILER_NAME ${CMAKE_CXX_COMPILER} NAME)
@@ -52,10 +68,6 @@ if (${COMPILER_IS_NVCC})
5268
set(CMAKE_CXX_EXTENSIONS OFF)
5369
endif()
5470

55-
###############################################################################
56-
# COMPILER FLAGS
57-
###############################################################################
58-
5971
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
6072
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra")
6173
if (NOT ${COMPILER_IS_NVCC})
@@ -65,31 +77,42 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_COMPILER_IS_GNUCXX)
6577
endif()
6678

6779
###############################################################################
68-
# BACKWARD OBJECT
80+
# BACKWARD INTERFACE
6981
###############################################################################
7082

71-
add_library(backward_object OBJECT backward.cpp)
72-
target_compile_definitions(backward_object PRIVATE ${BACKWARD_DEFINITIONS})
73-
target_include_directories(backward_object PRIVATE ${BACKWARD_INCLUDE_DIRS})
74-
set(BACKWARD_ENABLE $<TARGET_OBJECTS:backward_object> CACHE STRING
75-
"Link with this object to setup backward automatically")
83+
add_library(backward_interface INTERFACE)
84+
set_target_properties(backward_interface PROPERTIES EXPORT_NAME Interface)
85+
target_compile_definitions(backward_interface INTERFACE ${BACKWARD_DEFINITIONS})
86+
target_include_directories(backward_interface INTERFACE ${BACKWARD_INCLUDE_DIRS})
87+
if(BACKWARD_HAS_EXTERNAL_LIBRARIES)
88+
target_link_libraries(backward_interface INTERFACE ${BACKWARD_LIBRARIES})
89+
endif()
90+
add_library(Backward::Interface ALIAS backward_interface)
7691

92+
###############################################################################
93+
# BACKWARD OBJECT (Includes backward.cpp)
94+
# (Note that this target is not exported, since CMake currently does not allow
95+
# exporting an OBJECT library.)
96+
###############################################################################
97+
98+
add_library(backward_object OBJECT backward.cpp)
99+
set_target_properties(backward_object PROPERTIES EXPORT_NAME Object)
100+
target_link_libraries(backward_object PUBLIC Backward::Interface)
101+
add_library(Backward::Object ALIAS backward_object)
77102

78103
###############################################################################
79104
# BACKWARD LIBRARY (Includes backward.cpp)
105+
# (Note that the linker will not include unused objects from a static library,
106+
# unless the -Wl,--whole-archive option (or similar) is used.)
80107
###############################################################################
81-
option(BACKWARD_SHARED "Build dynamic backward-cpp shared lib" OFF)
82108

109+
set(libtype STATIC)
83110
if(BACKWARD_SHARED)
84111
set(libtype SHARED)
85112
endif()
86113
add_library(backward ${libtype} backward.cpp)
87-
target_compile_definitions(backward PUBLIC ${BACKWARD_DEFINITIONS})
88-
target_include_directories(backward PUBLIC ${BACKWARD_INCLUDE_DIRS})
89-
if(BACKWARD_HAS_EXTERNAL_LIBRARIES)
90-
set_target_properties(backward PROPERTIES
91-
INTERFACE_LINK_LIBRARIES "${BACKWARD_LIBRARIES}")
92-
endif()
114+
set_target_properties(backward PROPERTIES EXPORT_NAME Backward)
115+
target_link_libraries(backward PUBLIC Backward::Interface)
93116
add_library(Backward::Backward ALIAS backward)
94117

95118
###############################################################################
@@ -107,7 +130,7 @@ if(BACKWARD_TESTS)
107130

108131
add_executable(${test_name} ${src} ${ARGN} $<TARGET_OBJECTS:test_main>)
109132

110-
target_link_libraries(${test_name} PRIVATE Backward::Backward)
133+
target_link_libraries(${test_name} PRIVATE Backward::Interface)
111134

112135
add_test(NAME ${name} COMMAND ${test_name})
113136
endmacro()
@@ -130,7 +153,7 @@ if(BACKWARD_TESTS)
130153
)
131154

132155
foreach(test ${TESTS})
133-
backward_add_test(test/${test}.cpp ${BACKWARD_ENABLE})
156+
backward_add_test(test/${test}.cpp backward.cpp)
134157
endforeach()
135158
endif()
136159

@@ -140,5 +163,14 @@ install(
140163
)
141164
install(
142165
FILES "BackwardConfig.cmake"
143-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/backward
166+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
144167
)
168+
# check if Backward is being used as a top-level project or included as a subproject
169+
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
170+
# export the targets (note that exporting backward_object does not make sense)
171+
install(TARGETS backward_interface backward EXPORT BackwardTargets)
172+
# install a CMake file for the exported targets
173+
install(EXPORT BackwardTargets
174+
NAMESPACE Backward::
175+
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
176+
endif()

README.md

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Backward-cpp [![badge](https://img.shields.io/badge/conan.io-backward%2F1.3.0-green.svg?logo=data:image/png;base64%2CiVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAA1VBMVEUAAABhlctjlstkl8tlmMtlmMxlmcxmmcxnmsxpnMxpnM1qnc1sn85voM91oM11oc1xotB2oc56pNF6pNJ2ptJ8ptJ8ptN9ptN8p9N5qNJ9p9N9p9R8qtOBqdSAqtOAqtR%2BrNSCrNJ/rdWDrNWCsNWCsNaJs9eLs9iRvNuVvdyVv9yXwd2Zwt6axN6dxt%2Bfx%2BChyeGiyuGjyuCjyuGly%2BGlzOKmzOGozuKoz%2BKqz%2BOq0OOv1OWw1OWw1eWx1eWy1uay1%2Baz1%2Baz1%2Bez2Oe02Oe12ee22ujUGwH3AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACxMAAAsTAQCanBgAAAAHdElNRQfgBQkREyOxFIh/AAAAiklEQVQI12NgAAMbOwY4sLZ2NtQ1coVKWNvoc/Eq8XDr2wB5Ig62ekza9vaOqpK2TpoMzOxaFtwqZua2Bm4makIM7OzMAjoaCqYuxooSUqJALjs7o4yVpbowvzSUy87KqSwmxQfnsrPISyFzWeWAXCkpMaBVIC4bmCsOdgiUKwh3JojLgAQ4ZCE0AMm2D29tZwe6AAAAAElFTkSuQmCC)](http://www.conan.io/source/backward/1.3.0/Manu343726/testing)
1+
Backward-cpp [![Conan Center](https://img.shields.io/conan/v/backward-cpp)](https://conan.io/center/recipes/backward-cpp)
22
============
33

44
Backward is a beautiful stack trace pretty printer for C++.
@@ -45,10 +45,26 @@ Note for [folly](https://github.com/facebook/folly) library users: must define `
4545

4646
### Integration with CMake
4747

48-
If you are using CMake and want to use its configuration abilities to save
49-
you the trouble, you can easily integrate Backward, depending on how you obtained
48+
If you are using CMake and want to use its configuration abilities to save you
49+
the trouble, you can easily integrate Backward, depending on how you obtained
5050
the library.
5151

52+
Notice that all approaches are equivalent in the way Backward is added to a
53+
CMake target, the difference is in how CMake is pointed to the Backward
54+
sources. Backward defines three targets:
55+
56+
- `Backward::Interface` is an interface target that brings compiler definition
57+
flags, include directory, and external libraries. This is all you need to use
58+
the `backward.hpp` header library.
59+
- `Backward::Object` brings `Backward::Interface` and `backward.cpp` as an
60+
`OBJECT` CMake library. This target cannot be exported, so it is not
61+
available when Backward is used via `find_package`.
62+
- `Backward::Backward` brings `Backward::Interface` and `backward.cpp` as
63+
either `STATIC` or `SHARED` library (depending on the `BACKWARD_SHARED`
64+
option). This target is exported and always available, however note that the
65+
linker will not include unused objects from a static library, unless the
66+
`-Wl,--whole-archive` option (or similar) is used.
67+
5268
#### With `FetchContent()`:
5369

5470
If you are using a recent version of CMake, you can integrate `backward` via `FetchContent` like below:
@@ -58,47 +74,41 @@ include(FetchContent)
5874
5975
# Also requires one of: libbfd (gnu binutils), libdwarf, libdw (elfutils)
6076
FetchContent_Declare(backward
61-
GIT_REPOSITORY https://github.com/bombela/backward-cpp
62-
GIT_TAG v1.6)
77+
GIT_REPOSITORY https://github.com/bombela/backward-cpp
78+
GIT_TAG master # or a version tag, such as v1.6
79+
SYSTEM # optional, the Backward include directory will be treated as system directory
80+
)
6381
FetchContent_MakeAvailable(backward)
6482
65-
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.cpp)
66-
add_executable(example ${SOURCES} ${BACKWARD_ENABLE}) # Notice the "BACKWARD_ENABLE" here
67-
add_backward(example)
83+
# Add Backward to your target (either Backward::Interface, Backward::Object, or Backward::Backward)
84+
target_link_libraries(mytarget PUBLIC Backward::Interface)
6885
```
6986

7087
#### As a subdirectory:
7188

7289
In this case you have a subdirectory containing the whole repository of Backward
73-
(eg.: using git-submodules), in this case you can do:
90+
(e.g. using [git-submodule](https://git-scm.com/book/en/v2/Git-Tools-Submodules)),
91+
in this case you can do:
7492

75-
```
93+
```cmake
7694
add_subdirectory(/path/to/backward-cpp)
7795
78-
# This will add backward.cpp to your target
79-
add_executable(mytarget mysource.cpp ${BACKWARD_ENABLE})
80-
81-
# This will add libraries, definitions and include directories needed by backward
82-
# by setting each property on the target.
83-
add_backward(mytarget)
96+
# Add Backward to your target (either Backward::Interface, Backward::Object, or Backward::Backward)
97+
target_link_libraries(mytarget PUBLIC Backward::Interface)
8498
```
8599

86-
#### Modifying CMAKE_MODULE_PATH
100+
#### Modifying `CMAKE_MODULE_PATH`:
87101

88102
In this case you can have Backward installed as a subdirectory:
89103

90104
```
91105
list(APPEND CMAKE_MODULE_PATH /path/to/backward-cpp)
92106
find_package(Backward)
93107
94-
# This will add libraries, definitions and include directories needed by backward
95-
# through an IMPORTED target.
96-
target_link_libraries(mytarget PUBLIC Backward::Backward)
108+
# Add Backward to your target (either Backward::Interface or Backward::Backward)
109+
target_link_libraries(mytarget PUBLIC Backward::Interface)
97110
```
98111

99-
Notice that this is equivalent to using the the approach that uses `add_subdirectory()`,
100-
however it uses cmake's [imported target](https://cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets) mechanism.
101-
102112
#### Installation through a regular package manager
103113

104114
In this case you have obtained Backward through a package manager.
@@ -109,10 +119,10 @@ Packages currently available:
109119
```
110120
find_package(Backward)
111121
112-
# This will add libraries, definitions and include directories needed by backward
113-
# through an IMPORTED target.
114-
target_link_libraries(mytarget PUBLIC Backward::Backward)
122+
# Add Backward to your target (either Backward::Interface or Backward::Backward)
123+
target_link_libraries(mytarget PUBLIC Backward::Interface)
115124
```
125+
116126
### Libraries to unwind the stack
117127

118128
On Linux and macOS, backtrace can back-trace or "walk" the stack using the
@@ -422,8 +432,8 @@ struct ResolvedTrace: public Trace {
422432
struct SourceLoc {
423433
std::string function;
424434
std::string filename;
425-
size_t line;
426-
size_t col;
435+
unsigned line;
436+
unsigned col;
427437
};
428438
429439
// In which binary object this trace is located.

0 commit comments

Comments
 (0)