I believe that I have encountered a bug in CMake. I am using C++ modules, and target_compile_options or compile_options are only being applied to the regular C++ files, not the module files (as observed through the compile_commands.json).
Interestingly enough, the options set through add_compile_definitions were applied to both file types.
I started debugging the issue and noticed divergence at target->GetCompileOptions(config, lang)
The config and lang were the same, but the target objects were different. This is not what I expected both types of sources were connected to the same “target” as per CMake.
I don’t think this problem is exclusive to Ninja, but I am using Ninja, which is why I have filed this as such.
I am happy to provide a minimal error case, but I don’t see a good way to do that with this text-based interface. Maybe I could provide a zip or tar.gz of the files I have?
If you will forgive the style errors, this is the most important part:
cmake_minimum_required(VERSION 3.28)
add_library(test)
# Add module interface files
target_sources(test
PUBLIC
FILE_SET CXX_MODULES FILES
ternary.cxx
)
# Add other source files
target_sources(test
PUBLIC
trie.cpp
)
string(TIMESTAMP TODAY "%Y-%m-%d")
message(STATUS "Current build date: ${TODAY}")
add_compile_definitions("UNIVERSAL")
add_compile_options("-DDATE_OPTION=${TODAY}")
add_executable(testo main.cpp)
set_target_properties(testo PROPERTIES OUTPUT_NAME "modules-test")
target_compile_options(testo PUBLIC "-DDATE_TARGET=${TODAY}")
target_compile_options(testo PUBLIC "$<$<CONFIG:DEBUG>:-Wall>")
target_link_libraries(testo test)
install(TARGETS testo)
“UNIVERSAL” is defined for all files as expected. However, -DDATE_OPTION
, -DDATE_TARGET
, and -Wall
are only specified for main.cpp
and trie.cpp
. They are not provided for ternary.cxx
as expected.