Topic | Detail |
---|---|
Directory | cmake_invoking_conan |
Purpose | Demo how to build your tests by getting CMake to invoke Conan, to download single headers for specific releases of ApprovalTests.cpp and Catch2. The released headers of those dependencies will be downloaded inside your CMake build space, and will not be shown inside your IDE. |
Dependencies | ApprovalTests.cpp - downloaded automatically by CMake invoking Conan Catch2 - downloaded automatically by CMake invoking Conan |
Mechanism | Uses the cmake-conan CMake module to invoke Conan automatically from within CMake. |
More Detail | See Example 3. Making CMake invoke Conan |
The conanfile.txt file is:
# See CMake/Conan.cmake for how 'conan install' is launched from cmake
[requires]
catch2/2.13.4
approvaltests.cpp/10.7.0
# Note that we don't say what generator we want.
# CMake code will take care of that for us.
There is a CMake file called CMake/Conan.cmake
which contains instructions for downloading a specific version of the cmake-conan CMake module:
macro(run_conan)
# Download automatically, you can also just copy the conan.cmake file
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(
STATUS
"Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.15/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
conan_add_remote(NAME bincrafters URL
https://api.bintray.com/conan/bincrafters/public-conan)
conan_cmake_run(
CONANFILE conanfile.txt
BASIC_SETUP
CMAKE_TARGETS # individual targets to link to
BUILD
missing
)
endmacro()
The top-level CMakeLists.txt file includes the above CMake/Conan.cmake
file, and runs the macro that it contained:
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(conan_cmake)
# Load CMake/Conan.cmake, which sets up a 'run_conan()' macro to download dependencies.
include(CMake/Conan.cmake)
run_conan()
enable_testing()
add_subdirectory(tests)
And the CMakeLists.txt that builds the tests is:
add_executable(tests
main.cpp
tests.cpp
)
# Note the Conan-specific library namees, beginning with CONAN_PKG.
# Conan sets up these names when its cmake generator is used.
# This ties your project to using Conan.
target_link_libraries(
tests
CONAN_PKG::approvaltests.cpp
CONAN_PKG::catch2)
target_compile_features(tests PUBLIC cxx_std_11)
set_target_properties(tests PROPERTIES CXX_EXTENSIONS OFF)
add_test(
NAME tests
COMMAND tests)
The build script is:
#!/bin/bash
# Force execution to halt if there are any errors in this script:
set -e
set -o pipefail
mkdir -p build
cd build
# Note that we do not need to invoke conan.
# However, we do need to say what build configuration we want.
cmake -DCMAKE_BUILD_TYPE=Debug ..
cmake --build .
ctest --output-on-failure . -C Debug