Ninja: generated source files lost when cleaning the build directory
Source files generated with configure_file()
or file(WRITE)
will be lost when ninja is told to clean the build directory. They won't be regenerated automatically, so the next build will fail.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(cmake-ninja-test CXX)
configure_file(test.cc.in test.cc)
set_property(SOURCE test.cc PROPERTY GENERATED TRUE)
add_executable(dummytest test.cc)
test.cc.in:
#include <iostream>
int main()
{
std::cout << "huhu" << std::endl;
}
Put both into a directory, then run the command shown in the log:
-*- mode: compilation; default-directory: "~/Projekte/cmake-ninja-test/" -*-
Compilation started at Fri May 25 20:25:15
export LC_ALL=C; set -ex; rm -rf build; mkdir build; cd build; cmake -GNinja ..; ls -l test.cc; cmake --build . --; cmake --build . -- clean; ls -l test.cc||true; cmake --build . --
+ rm -rf build
+ mkdir build
+ cd build
+ cmake -GNinja ..
-- The CXX compiler identification is GNU 6.3.0
-- Check for working CXX compiler: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/joe/Projekte/cmake-ninja-test/build
+ ls -l test.cc
-rw-r--r-- 1 joe joe 72 May 25 20:25 test.cc
+ cmake --build . --
[1/3]
[2/3] Building CXX object CMakeFiles/dummytest.dir/test.cc.o
[3/3] Linking CXX executable dummytest
+ cmake --build . -- clean
[1/1] Cleaning all built files...
Cleaning... 3 files.
+ ls -l test.cc
ls: cannot access 'test.cc': No such file or directory
+ true
+ cmake --build . --
[1/3]
[2/3] Building CXX object CMakeFiles/dummytest.dir/test.cc.o
FAILED: CMakeFiles/dummytest.dir/test.cc.o
/usr/lib/ccache/c++ -MD -MT CMakeFiles/dummytest.dir/test.cc.o -MF CMakeFiles/dummytest.dir/test.cc.o.d -o CMakeFiles/dummytest.dir/test.cc.o -c /home/joe/Projekte/cmake-ninja-test/build/test.cc
c++: error: /home/joe/Projekte/cmake-ninja-test/build/test.cc: No such file or directory
c++: fatal error: no input files
compilation terminated.
ninja: build stopped: subcommand failed.
Compilation exited abnormally with code 1 at Fri May 25 20:25:16
I was able to reproduce this with cmake 3.11.2 obtained from https://cmake.org/files/v3.11/cmake-3.11.2-Linux-x86_64.tar.gz and the following Dockerfile
This does not happen with the standard generator (Unix Makefiles).
This does not happen with .hh
files (they do not get deleted), presumably because they would not be given as a source file in add_executable()
.
There is an ancient CMake bug #13105 (closed) (Mantis) that looks somewhat similar, but I believe that is a different issue and in any case it was resolved long ago.
Convenience link to our issue: https://gitlab.dune-project.org/core/dune-common/issues/119