cxxmodules: changing FILE_SET visibility does not trigger a module dependencies update
With the following project:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.28)
project(moduletest LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
add_library(modulelib)
target_sources(modulelib PRIVATE FILE_SET CXX_MODULES FILES lib.cpp)
add_executable(moduleexe)
target_sources(moduleexe PRIVATE main.cpp)
target_link_libraries(moduleexe PRIVATE modulelib)
// lib.cpp
export module mylib;
// main.cpp
import mylib;
int main() { return 0; }
I get the following errors:
[ 50%][4/8] Generating CXX dyndep file CMakeFiles/moduleexe.dir/CXX.dd
CMake Error: Unable to use module 'mylib' as it is 'PRIVATE' and therefore not accessible outside of its owning target.
[ 87%][7/8] Building CXX object CMakeFiles/moduleexe.dir/main.cpp.o
FAILED: CMakeFiles/moduleexe.dir/main.cpp.o
/usr/lib/ccache/clang++-17 -g -std=c++20 -fcolor-diagnostics -MD -MT CMakeFiles/moduleexe.dir/main.cpp.o -MF CMakeFiles/moduleexe.dir/main.cpp.o.d @CMakeFiles/moduleexe.dir/main.cpp.o.modmap -o CMakeFiles/moduleexe.dir/main.cpp.o -c /home/sjoubert/test/cppmodule/main.cpp
/home/sjoubert/test/cppmodule/main.cpp:1:8: fatal error: module 'mylib' not found
1 | import mylib;
| ~~~~~~~^~~~~
1 error generated.
ninja: build stopped: subcommand failed.
I would expect the build to stop at the first "CMake Error" and not proceed to a compilation that we know will fail. I'm guessing the error is printed but that the actual command is considered "successful" from its exit code.
Fixing the FILE_SET visibility to PUBLIC I still get the following error:
[ 0%][0/1] Re-running CMake...
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/sjoubert/test/cppmodule/build
[ 50%][1/2] Building CXX object CMakeFiles/moduleexe.dir/main.cpp.o
FAILED: CMakeFiles/moduleexe.dir/main.cpp.o
/usr/lib/ccache/clang++-17 -g -std=c++20 -fcolor-diagnostics -MD -MT CMakeFiles/moduleexe.dir/main.cpp.o -MF CMakeFiles/moduleexe.dir/main.cpp.o.d @CMakeFiles/moduleexe.dir/main.cpp.o.modmap -o CMakeFiles/moduleexe.dir/main.cpp.o -c /home/sjoubert/test/cppmodule/main.cpp
/home/sjoubert/test/cppmodule/main.cpp:1:8: fatal error: module 'mylib' not found
1 | import mylib;
| ~~~~~~~^~~~~
1 error generated.
ninja: build stopped: subcommand failed.
CMake was successfully retriggered but the module information were not updated and the build still fails.
A ninja clean && ninja
will properly build successfully.
Also note that changing back from PUBLIC to PRIVATE after a successful build will not produce an error (ninja actually says no work to do
since nothing changed in the build graph)
Versions:
- cmake version 3.28.1
- Debian clang version 17.0.6 (3)
- ninja 1.11.1
Edited by Sylvain Joubert