I have a custom target for my CMake file that is configurable in the number of output files it produces. If the caller passes a particular argument to my script, I want to generate more output files. The issue that I am struggling to resolve is that if the user doesn’t pass in the argument to generate more files, and the inputs haven’t changed, I want the target to be skipped.
file_generator.sh
#!/bin/bash
touch foo.txt
if [ -n "$1" ]; then
touch bar.txt
fi
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(example VERSION 1.0.0 LANGUAGES )
list(APPEND outputs ${CMAKE_CURRENT_SOURCE_DIR}/foo.txt)
if (DEFINED MORE_OUTPUT)
list(APPEND outputs ${CMAKE_CURRENT_SOURCE_DIR}/bar.txt)
endif()
add_custom_command(
DEPENDS something.txt
OUTPUT ${outputs}
COMMAND ./file_generator.sh ${MORE_OUTPUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/
)
add_custom_target(EXAMPLE ALL DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/foo.txt)
The behavior that I am observing is that the target is run if…
- Any inputs have a last modified date after any outputs, or
- The output list has changed
Is there any way to update this example to prevent running the target if only statement #2 is true? This is called with either cmake -DMORE_OUTPUT=
or cmake -DMORE_OUTPUT=TRUE
.