cxxmodules: CMP0155 NEW with C++20 breaks trivial try_compile
https://cmake.org/cmake/help/git-stage/policy/CMP0155.html was (or will be) introduced with CMake 3.28, and it enables files to be scanned for imports when C++20 is enabled. This breaks the try_compile()
of trivial code snippets
Minimal reproducible example working with CMake 3.27.7
but not with CMake 3.28.20231004-g2baa6f8
cmake_minimum_required(VERSION 3.27...${CMAKE_VERSION})
project(test_try_compile_cxx20 CXX)
set(CMAKE_CXX_STANDARD 20)
file(WRITE ${CMAKE_BINARY_DIR}/main.cpp
"int main() {\n"
"}\n"
)
# compile toolchain-definitions file
try_compile(
try_compile_result
"${CMAKE_BINARY_DIR}"
"${CMAKE_BINARY_DIR}/main.cpp"
OUTPUT_VARIABLE outresult
)
This issue can be mitigated by
- use a older minimum CMake-version like
cmake_minimum_required(VERSION 3.27)
- explicitly disable the new policy:
cmake_policy(SET CMP0155 OLD)
But with both cases I'm stuck with a deprecated usage of a feature (or old CMake policy version) and no way forward (at least I haven't found one yet)
Edit: removed the line set(CMAKE_CXX_STANDARD_REQUIRED ON)
from minimal example to make it even more minimal
Edit2: even setting the proposed variable set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
or -DCMAKE_CXX_SCAN_FOR_MODULES=OFF
breaks the minimal example
Edit3: add error message, running on Ubuntu 23.04 with CMake 3.28.20231004-g2baa6f8
and GCC 12.3.0
The target named "cmTC_9d50f" contains C++ sources that use modules which
is not supported by the generator