CMake doesn't re-run custom command if set of DEPENDS changes
Consider the following situation:
# Two input files
$ echo 1 > /tmp/input1
$ echo 2 > /tmp/input2
# A sample project for this query
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(query)
set(external_file /tmp/input1)
add_custom_command(
OUTPUT out
COMMAND echo hello > out
COMMENT "Running echo hello"
DEPENDS ${external_file}
VERBATIM)
add_custom_target(run DEPENDS out)
# Configure
$ cmake -S . -Bbuild -GNinja
...
-- Configuring done
-- Generating done
-- Build files have been written to: /spare/scratch/1592007425/build
# Build the target 'run'
$ cmake --build build/ -- run
[1/1] Running echo hello
# So far so good
$ cmake --build build/ -- run
ninja: no work to do.
# Now, let's modify the input for the `DEPENDS` in the argument from `/tmp/input1` to `/tmp/input2`:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(query)
set(external_file /tmp/input2)
add_custom_command(
OUTPUT out
COMMAND echo hello > out
COMMENT "Running echo hello"
DEPENDS ${external_file}
VERBATIM)
add_custom_target(run DEPENDS out)
# Now, try running the 'run' target. It doesn't say 'echo hello' :'(
$ cmake --build build/ -- run
[0/1] Re-running CMake...
-- Configuring done
-- Generating done
-- Build files have been written to: /spare/scratch/1592007425/build
ninja: no work to do.
I also tried: set_source_files_properties(${external_file} PROPERTIES GENERATED TRUE)
, but it didn't make any difference.
Of course, in the above scenario, if I update the timestamp (mtime) of the input to be greater than the out file, run re-triggers the 'echo hello'.
My actual use case is as follows: I am using add_custom_command()
for running a binary to generate some files (source-gen). I have multiple versions of this same binary already built (read only, can't modify them). I want to be able to switch to any of these to check how it impacts by project. Think of this as trying to run protoc
, but wanting to regenerate the .pb.h
and .pb.cc
files whenever I want to use a different build of protoc
.
Edited by Brad King