[CMake] custom target like add_executable

Alan W. Irwin irwin at beluga.phys.uvic.ca
Thu Feb 5 14:37:45 EST 2015


On 2015-02-05 10:14-0800 Bill Newcomb wrote:

> A contrived example:
>
> $ ls ..
> b  CMakeLists.txt  foo
>
> $ cat ../CMakeLists.txt
> cmake_minimum_required(VERSION 3.1)
>
> add_custom_target(foo.sha1 ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/foo.sha1)
>
> add_custom_command(
>    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.sha1
>    COMMAND openssl sha1 -out ${CMAKE_CURRENT_BINARY_DIR}/foo.sha1 ${CMAKE_CURRENT_SOURCE_DIR}/foo
>    )
>

Hi Bill:

I am virtually positive that target name = filename as in the above
example will cause circular dependencies (as you have observed) since
for at least the default "Unix Makefiles" generator target names are usually identified with the
corresponding filename which name clashes in this case.  Also, your
add_custom_command has no DEPENDS so by definition it is always out of
date and will keep being repeated.  I am not familiar with
command-line openssl, but I assume ${CMAKE_CURRENT_SOURCE_DIR}/foo is
the dependency there.

Therefore, I suggest the following modifications:

add_custom_command(
     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/foo.sha1
     COMMAND openssl sha1 -out ${CMAKE_CURRENT_BINARY_DIR}/foo.sha1 ${CMAKE_CURRENT_SOURCE_DIR}/foo
     DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/foo
     )

add_custom_target(target_foo.sha1 ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/foo.sha1)

(Note, the target name "target_foo.sha1" which no longer corresponds
to a real file name (i.e., it has the .PHONY attribute in the Makefile similar to the
"all", "install", "help", etc., targets), and the DEPENDS line on the
custom command.)

After running cmake, then

make VERBOSE=1  (or make VERBOSE=1 target_foo.sha1) should run the openssl command

Subsequent make commands should not run that openssl command since the
result foo.sha1 will have a later date than foo.  Subsequently, if you
touch foo, then any of the above make commands will run that openssl
command (once) again.

Alan
__________________________
Alan W. Irwin

Astronomical research affiliation with Department of Physics and Astronomy,
University of Victoria (astrowww.phys.uvic.ca).

Programming affiliations with the FreeEOS equation-of-state
implementation for stellar interiors (freeeos.sf.net); the Time
Ephemerides project (timeephem.sf.net); PLplot scientific plotting
software package (plplot.sf.net); the libLASi project
(unifont.org/lasi); the Loads of Linux Links project (loll.sf.net);
and the Linux Brochure Project (lbproject.sf.net).
__________________________

Linux-powered Science
__________________________


More information about the CMake mailing list