This is a simplification of another problem. But it boils down to the last add_custom_command (one which appends), which I want to only execute when the target T1 is build.
However currently the problem is that every time I run make, the Appending file.. command is always running.
# Fitst time running make
$ make
[100%] Creating file..
Appending file..
[100%] Built target T1
# Second time on
$ make
Appending file..
[100%] Built target T1
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(proj12 NONE)
# Phony target T1, which depends on t1.txt.
add_custom_target(T1 ALL DEPENDS t1.txt T1)
# Rule to create t1.txt dependency
add_custom_command(
OUTPUT t1.txt
COMMAND echo "Created at `date`" > t1.txt
COMMENT "Creating file.."
)
# Should only execute if T1 target needs to be built.
add_custom_command(
POST_BUILD
TARGET T1
COMMAND echo "Appended at `date`" >> t1.txt
COMMENT "Appending file.."
)
The cmake documentation states the following
add_custom_command: This is useful for performing an operation before or after building the target. The command becomes part of the target and will only execute when the target itself is built. If the target is already built, the command will not execute.
So I was in the impression that the 'Appending' will only occur only when T1 target is build (which only builds when t1.txt is absent).
I am just learning CMake and is confused as to how this can be done.
Do I need to create a dummy file for the appending to depend on?
The original problem
With older CMake the UseJava function add_jar places resource files in wrong namespace in the jar file. So my solution is to provide add_jar with only *.java files and later will add the extra resource files using jar command in a CMake custom command.
The solution works and the resource files are placed properly in the jar file, but this custom command is executed every time. I want this to execute only when the jar file actually changes.
A couple of things to change:
Remove "ALL" from target definition T1 to prevent it from building with default all targets.
Add one more target "default" with "ALL" parameter that caters to the default target option, this target is built by default and executes the first command to create t1.txt, the second command is executed additionally only if target T1 is explicitly specified as "make T1".