0

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)
Slaus
  • 2,086
  • 4
  • 26
  • 41
  • "avoid building tests in the default target" - What do you mean by building tests? Test executables, created with `add_executable`? Or do you want to not create `tests` target? – Tsyvarev Sep 12 '20 at 20:23
  • @Tsyvarev thank you for comment! Yes, **CMake** builds test executable by building `test` target. I want tests target to be created, but build it only when I explicitly type `make tests`, **not** when I type `make`. – Slaus Sep 12 '20 at 20:57
  • 1
    "CMake builds test executable by building test target." - No, this is wrong. CMake builds a test executable because it is defined with `add_executable`. By default, all such targets are build with `all`(default) target. You may redefine this behavior by adding `EXCLUDE_FROM_ALL` flag for `add_executable` call. This flag could also be passed to `add_subdirectory`, so it will affect on all libraries/executables defined in the subdirectory. From the other side, `test` target has no dependencies. So if you want to build something before testing, you need to create another target with dependency. – Tsyvarev Sep 12 '20 at 21:38
  • @Tsyvarev Replacing `add_subdirectory( test )` with `add_subdirectory( test EXCLUDE_FROM_ALL )` solved the issue! Thank you very much! Should I format it up as the solution? – Slaus Sep 12 '20 at 21:45
  • Actually, this topic is not a new one, and I have found the [question](https://stackoverflow.com/questions/20500118/how-to-configure-cmake-to-get-an-executable-not-by-default/50874088) with similar problem as yours. As for dependencies from `test` and test executables, there are also several questions on Stack Overflow. See e.g. [that one](https://stackoverflow.com/questions/733475/cmake-ctest-make-test-doesnt-build-tests). – Tsyvarev Sep 12 '20 at 22:06
  • @Tsyvarev yeah I've seen that one, but problem there is opposite to mine: I couldn't get rid of **tests** being built. – Slaus Sep 12 '20 at 22:15
  • Em, How your "Is there a way to avoid building tests in the default target" differs from "I don't want to build unit-test always!" in the [other question](https://stackoverflow.com/q/20500118/3440745)? And its [second answer](https://stackoverflow.com/a/50874088/3440745) suggests using exactly `EXCLUDE_FROM_ALL`. – Tsyvarev Sep 13 '20 at 09:06
  • @Tsyvarev it's title is **How to configure cmake to get an executable not by default** - maybe topic is dug somewhere deeper into the question which over-complicates googling. It's OK though it might be a duplicate indeed. – Slaus Sep 13 '20 at 11:00
  • While I could find some similarity between the title - **How to configure cmake to get an executable not by default** - and the problem "I don't want to build unit-test always!", - I agree, that the title is not so good. Unfortunately, I cannot suggest another title, since I understand the question only partially. Actually, I feel the same about your question, but similarity of code and solution makes me to think about "duplicate". – Tsyvarev Sep 13 '20 at 11:20

0 Answers0