On 27. Aug, 2009, at 0:58, Swaroop Ramachandra wrote:

Hi,

I'm trying to do the following in my CMake file:

1. Generate a "xyz.txt" file
2. Compile a "generate.c" file to give out a "generate" binary in my bin
directory.
3. Execute the "generate" binary (The binary just reads contents of
"xyz.txt" and creates a copy of "xyz.txt"using read() and write() functions
in C)

The problem:
When I do a fresh build, 1 and 2 succeed. 3 fails with the following error
*"bin/generate: Command not found"*

However, if I *re-run the build immediately* after, since the "generate" binary is already created, all 3 successfully execute. Here's a snippet of
what I have written.

------------
------------
/*---- Code to generate xyz.txt -- Successfully generated each time------*/
----------
---------
ADD_EXECUTABLE(generate ${CMAKE_CURRENT_SOURCE_DIR}/server/generate.c)

set(GEN ${CMAKE_BINARY_DIR}/bin/generate)

ADD_CUSTOM_COMMAND(
  TARGET generate POST_BUILD
  COMMAND ${GEN}
  DEPENDS ${CMAKE_BINARY_DIR}/server/xyz.txt}
  )
In my ADD_CUSTOM_COMMAND, I have specified POST_BUILD, which I understood to be that the command will be executed only after creation of the "generate"
binary.

That's the wrong way to go about it. the TARGET form of the add_custom_command is intended to "finish" the actual target. What you want is something like this:

# no need for absolute paths...
add_executable(generate server/generate.c)

add_custom_command(
  # tell cmake you want to generate xyz.c
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xyz.c
# using the generate program. cmake knows that "generate" refers to the above target
  COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/xyz.txt
  # only run if xyz.txt changed
  DEPENDS xyz.txt
  # tell to run in current binary dir
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  # some nice comment in the output
  COMMENT "Generating ${CMAKE_CURRENT_BINARY_DIR}/xyz.c"
  VERBATIM
  )


And then you simply use ${CMAKE_CURRENT_BINARY_DIR}/xyz.c in one of your other targets. CMake will then know that it first needs to create target "generate", then run the program on xyz.txt to create $ {CMAKE_CURRENT_BINARY_DIR}/xyz.c, and finally use that to build the actual target.


HTH

Michael

_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake

Reply via email to