[CMake] add_custom_command taints main dependency file?

Anders Lindgren andlind at gmail.com
Wed Jun 3 05:46:10 EDT 2015


Hi!

I just noticed that add_custom_command() "taints" the main dependency file,
a behavior that in counter-intuitive and does not correspond to the
description in the CMake manual.

The following files demonstrates the problem:

---- CMakeLists.txt
cmake_minimum_required(VERSION 3.1)

add_custom_command(
  MAIN_DEPENDENCY input.c
  COMMENT "Generating ${CMAKE_CURRENT_BINARY_DIR}/output.c"
  COMMAND
  ${CMAKE_COMMAND} -E copy
  ${CMAKE_CURRENT_SOURCE_DIR}/input.c
  ${CMAKE_CURRENT_BINARY_DIR}/output.c
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/output.c
  VERBATIM)

add_executable(foo ${CMAKE_CURRENT_BINARY_DIR}/output.c)

add_executable(bar input.c)
---- input.c:
int main() { return 0; }
----

In this case, I expect "bar" to simply include a plain "input.c" Instead,
it gets an "input.c" with a custom rule. This doesn't work since 1: "bar"
generates "output.c" (which is shouldn't) and 2: "input.c" isn't compiled
and included in the project.

I've tested this using CMake 3.1 using Visual Studio 2008 and using CMake
3.2.1 on UNIX using Makefile:s.


My specific use case is that I create two targets and after each target is
created I create a custom command and add the output files to the target.
(The path of the output file contains the ARCHIVE_OUTPUT_DIRECTORY target
property.)

In my case, what happens is that second target includes rules for building
the output file of the first target, as well as rules for building it's own.

    Sincerely,
         Anders Lindgren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20150603/2e010cf8/attachment.html>


More information about the CMake mailing list