Hi,
In my project, I have a library called “Library” and a library called “LibraryTest”. They both compile from the same sources. The former is exported, the latter is compiled with address sanitation and linked against the test suite.
To get faster builds, I want to link the test suite against “Library” in the “Devel” configuration, with the intention/expectation that “LibraryTest” would not have to be compiled. In Release mode, both are compiled and the test suite is run. I’m using the Ninja Multi-Config generator. My list file looks like this:
cmake_minimum_required(VERSION 3.27)
project(Example LANGUAGES CXX)
add_library(Library lib.cc)
add_library(LibraryTest lib.cc)
target_compile_options(LibraryTest PUBLIC -fsanitize=address)
target_link_libraries(LibraryTest PUBLIC -fsanitize=address)
add_executable(test test.cc)
target_link_libraries(test Library$<$<CONFIG:Release>:Test>)
The list file works as expected, and the correct library is linked depending on the configuration. However, both libraries are always compiled.
Looking at the generated Ninja file for “Debug”, the “Devel/test” target has an order-only dependency on the test library:
build Devel/test: CXX_EXECUTABLE_LINKER__test_Devel CMakeFiles/test.dir/Devel/test.cc.o | Devel/libLibrary.a || Devel/libLibrary.a Devel/libLibraryTest.a
Removing this order-only dependency stops building the test library.
Is this behavior expected? Any thoughts on how I could make my approach work?