I have ./CMakeLists.txt
and ./test/CMakeLists.txt
is added with add_subdirectory(test)
command in ./CMakeLists.txt
. Unfortunately, CMake builds tests everytime I want to build just main by make
. Is there a way to avoid building tests in the default target and build tests only when I type make tests
?
Condition
if( TARGET tests )
enable_testing()
endif()
in ./test/CMakeLists.txt
doesn't help.
./CMakeLists.txt
:
cmake_minimum_required(VERSION 3.0.0)
project( my_project VERSION 0.1.0)
add_executable( my_project cpp/main.cpp )
target_link_libraries( my_project common )
add_library( common cpp/my_source.hpp )
set_target_properties( common PROPERTIES LINKER_LANGUAGE CXX )
project(TEST)
add_subdirectory(test)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
./test/CMakeLists.txt
:
# Add catch as an interface library
set( CATCH_INCLUDE_DIR ../lib/Catch2 )
add_library( Catch INTERFACE )
target_include_directories( Catch INTERFACE ${CATCH_INCLUDE_DIR} )
add_executable( tests test.cpp )
target_link_libraries( tests PUBLIC Catch )
if( TARGET tests )
enable_testing()
endif()
add_test( NAME tests COMMAND tests )
include(CTest)
# catch_discover_tests(tests)