If/how --fresh can be applied recursively to subdirectories

Hi,

I was surprised to see that configuring with --fresh deletes object files in my root directory, but not in directories added with add_subdirectory. My questions are:

  1. Is this intentional?
  2. If so, is there a separate, “recursive” variant of --fresh ?

To show what I mean, here’s a console transcript. Note how cmake --fresh .. deletes in_root_dir.cpp.o but not in_subdir.cpp.o.

$ ls -R
.:
CMakeLists.txt  in_root_dir.cpp  subdir

./subdir:
CMakeLists.txt  in_subdir.cpp
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.29)

project(ReproCase VERSION 1.0)

add_library(in_root_dir STATIC in_root_dir.cpp)

add_subdirectory(subdir)
$ cat subdir/CMakeLists.txt

add_library(in_subdir STATIC in_subdir.cpp)
$ mkdir build && cd build && cmake .. && cmake --build .
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: <path>/build
[4/4] Linking CXX static library subdir/libin_subdir.a
$ find . -name '*.cpp.o'
./subdir/CMakeFiles/in_subdir.dir/in_subdir.cpp.o
./CMakeFiles/in_root_dir.dir/in_root_dir.cpp.o
$ cmake --fresh ..
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: <path>/build
$ find . -name '*.cpp.o'
./subdir/CMakeFiles/in_subdir.dir/in_subdir.cpp.o

I actually was initially wondering more generally what interaction to expect between --fresh and incremental building, then was surprised to see the empirical answer depend on whether sources are in a subdirectory or not, so came here to ask.

Thanks,
-Joseph

Configuring with --fresh deletes the CMakeCache.txt and CMakeFiles/ directories from the top of the build tree. Those are where CMake’s builtin system and toolchain inspection results are stored. Some generators (like Ninja) happen to also store object files for the top-level directory under CMakeFiles/ too, so for those it is simply a side effect that the object files are removed.