[CMake] custom target like add_executable

Bill Newcomb bnewcomb at nvidia.com
Thu Feb 5 13:14:18 EST 2015


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
    )

$ cmake ..
-- The C compiler identification is GNU 4.1.2
-- The CXX compiler identification is GNU 4.1.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bnewcomb/tmp/cmake-tests/test-fail/b

$ make
Scanning dependencies of target foo.sha1
make[2]: Circular CMakeFiles/foo.sha1 <- foo.sha1 dependency dropped.
make[2]: Circular foo.sha1 <- foo.sha1 dependency dropped.
[100%] Generating foo.sha1
[100%] Built target foo.sha1

$ ls --full-time foo.sha1 
-rw-rw-r-- 1 bnewcomb hardware 93 2015-02-05 10:10:48.984606000 -0800 foo.sha1

$ make
make[2]: Circular CMakeFiles/foo.sha1 <- foo.sha1 dependency dropped.
make[2]: Circular foo.sha1 <- foo.sha1 dependency dropped.
[100%] Generating foo.sha1
[100%] Built target foo.sha1

$ ls --full-time foo.sha1 
-rw-rw-r-- 1 bnewcomb hardware 93 2015-02-05 10:11:05.432925000 -0800 foo.sha1

$ 

I read in a years-old bug that I should always use absolute paths
with add_custom_command, but I don't know if that's still necessary.
Using relative paths doesn't seem to change the outcome.

Thanks,
B.

On 02/04/2015 06:59 PM, Alan W. Irwin wrote:
> On 2015-02-04 13:45-0800 Bill Newcomb wrote:
> 
>> Oops, so I guess it's not that it doesn't work at all, but that the 
>> add_custom_command gets run every time I 'make all', and that gnu make 
>> emits some warnings about circular dependencies.  The first is more of 
>> a problem, but both are kind of annoying.
> 
> Neither of those "issues" occurs if you have set up add_custom_command
> and the corresponding add_custom_target with the appropriate
> dependencies between them.
> 
> If re-reading the CMake documentation for add_custom_command and
> add_custom_target doesn't help, then I suggest you give a simple example
> that is not working for you.  I assume you have some dependency
> issue, but until you give an example, it is hard to anticipate what
> that might be.
> 
> 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
> __________________________

-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------


More information about the CMake mailing list