VS: Behaviour of CXX_SCAN_FOR_MODULES in Visual Studio generated projects
This is not so much a bug as issue with how CMake generates the MSBuild XML for a Visual Studio project file.
This is using CMake 3.29.
So I create a simple Hello World executable, set the C++ version to C++23 and generate a Visual Studio solution. Everything works fine.
I now want to use import std;
so I add
set_target_properties(hello_world PROPERTIES CXX_SCAN_FOR_MODULES true)
This works and I can import std;
. However when I look in the build properties in the Visual Studio GUI for the project. I see
Scan Sources for Module Dependencies : No
I was very confused by this.
So I looked in the vcxproj file and I see the MSBuild setting
<ScanSourceForModuleDependencies>false</ScanSourceForModuleDependencies>
note the "false" here.
After more confusion I then discovered that there were source file specific settings like
ScanSourceForModuleDependencies Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ScanSourceForModuleDependencies>
So at the moment CMake seems to always set the project wide setting to false and then always overrides is on the sources. So things work because the source file settings were overriding the project settings.
This seems wrong to me the behavior I expect would be
- Setting the property
CXX_SCAN_FOR_MODULES
on a target should control the MSBuild setting for the project - Setting the property
CXX_SCAN_FOR_MODULES
on a source should control the MSBuild setting on a source file
That way the <ScanSourceForModuleDependencies>
only appears in the output on a source if it has explicitly been set but the CMakeLists.txt.
In an ideal world you would only add <ScanSourceForModuleDependencies>
to a source file if the value differs from the project setting
but that would just be an optimization.
BuildStlModules
On a related topic MSBuild does have a BuildStlModules
setting. This seems to be a subset of ScanSourceForModuleDependencies
and it allows you to use import std;
but does not switch on full module support. I'm not sure if this a build time optimization thing. Maybe this is worth supporting if the other compilers are going to do something similar.