bombela / backward-cpp

A beautiful stack trace pretty printer for C++
MIT License
3.68k stars 467 forks source link

cmake: allow for inclusion with CPM #285

Closed damageboy closed 1 year ago

damageboy commented 1 year ago

Allow using backwards-cpp through CPM (Cmake Package Manager)/FetchContent without using the ${BACKWARD_ENABLE} target

CPM (https://github.com/cpm-cmake/CPM.cmake) can now be used like so:

In a main/top-level CMakeLists.txt:

include(cmake/CPM.cmake)

CPMAddPackage("gh:bombela/backward-cpp#1.7")

And in a project (or the same CMakeLists.txt):

cmake_minimum_required(VERSION 3.9)

project (example VERSION 0.1)

add_executable(example
    main.cpp
)

target_link_libraries(example
    Backward::Backward
)
GavinRay97 commented 1 year ago

Thanks for this!

Note that this also fixes the requirement to use some of the custom functionality like ADD_BACKWARD (I think) with just a plain FetchContent

For example, the below CMake project builds fine:

project(foo)
cmake_minimum_required(VERSION 3.20)

Include(FetchContent)
FetchContent_Declare(
    backward
    GIT_REPOSITORY https://github.com/damageboy/backward-cpp.git
    GIT_TAG master # Fork which has CMake ALIAS target   
)
FetchContent_MakeAvailable(backward)

# Add source files
file(GLOB_RECURSE SOURCES "src/*.cpp")
add_executable(foo ${SOURCES})
target_link_libraries(foo 
    PRIVATE
        Backward::Backward bfd dl
)
damageboy commented 1 year ago

@GavinRay97 Yeap, that's pretty much the idea, I can testify that it works for me with CPM (just a fancy wrapper around FetchContent) trying to get backward incorporated with minimal hassle.