You must be doing something wrong. Have a look at this example:
generate.cpp:
------>8------
#include <iostream>
#include <fstream>
int main(int argc, char* argv[]) {
if( argc < 2 ) {
std::cerr << "Usage: " << argv[0] << " <outputFile>\n";
}
std::ofstream str(argv[1]);
str << "#include <iostream>\nint main() {\n";
<< " std::cout << \"Hello World!\\n\";\n";
<< " return 0;\n}\n";
str.close();
return 0;
}
------<8------
CMakeLists.txt:
------>8------
cmake_minimum_required(VERSION 2.6)
project(test CXX)
add_executable(generate generate.cpp)
set(HELLO_SRCS ${CMAKE_CURRENT_BINARY_DIR}/hello.cpp)
add_custom_command(
OUTPUT ${HELLO_SRCS}
COMMAND generate ${HELLO_SRCS}
DEPENDS generate
COMMENT "Generating ${HELLO_SRCS}"
VERBATIM
)
add_executable(hello ${HELLO_SRCS})
------<8------
The executable generate takes the name of an output file as its
argument and then generates the code for a simple "hello world"
program. This output is then used to generate the program hello.
I hope this helps
Michael
On 28. Aug, 2009, at 6:05, Swaroop Ramachandra wrote:
I did. The same problem persists. Is there a work-around - maybe
some other
command that I can use?
2009/8/27 Michael Wild <them...@gmail.com>
David is probably right, that you need to add the dependency on
"generate".
However, only add it's target name to both the COMMAND and DEPENDS
arguments, CMake should figure this out.
Michael
On 27. Aug, 2009, at 21:31, Swaroop Ramachandra wrote:
Still no luck :( . I really wonder why it is trying to execute my
"generate"
binary even before it is built. Here are my updated lines of code.
#Trying to compile and run generate.c. generate.c creates a new file
someoutput.txt and copies all data from someinput.txt to
someoutput.txt
add_executable(generate server/generate.c)
add_custom_command(
# I want to generate someoutput.txt
OUTPUT ${CMAKE_BINARY_DIR}/server/someoutput.txt
# using the generate program. cmake knows that "generate" refers
to the
above target
COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt
# only run if sominput.txt changed
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt
${CMAKE_BINARY_DIR}/bin/generate${CMAKE_EXECUTABLE_SUFFIX}
# tell to run in current binary dir
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
# some nice comment in the output
COMMENT "Generating ${CMAKE_BINARY_DIR}/someoutput.txt"
VERBATIM
)
2009/8/27 David Cole <david.c...@kitware.com>
Use full path file names as DEPENDS arguments.
Also: depend on the executable file too so that cmake knows not
to try to
run the custom command before the executable is built...
i.e. :
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt
${CMAKE_BINARY_DIR}/bin/generate${CMAKE_EXECUTABLE_SUFFIX}
HTH,
David
On Thu, Aug 27, 2009 at 2:41 PM, Swaroop Ramachandra <
swaroo...@gmail.com
wrote:
Hi Michael,
Thanks for your reply. I still have the same problem.
*
*
gmake-3.81[2]: bin/generate: Command not found
lin: gmake-3.81[2]: *** [bin/generate] Error 127
lin: gmake-3.81[1]: *** [CMakeFiles/generate.dir/all] Error 2
Here's my code as is:
#Trying to compile and run generate.c. generate.c creates a new
file
someoutput.txt and copies all data from someinput.txt to
someoutput.txt
add_executable(generate server/generate.c)
add_custom_command(
# I want to generate someoutput.txt
OUTPUT ${CMAKE_BINARY_DIR}/server/someoutput.txt
# using the generate program. cmake knows that "generate" refers
to the
above target
COMMAND generate ${CMAKE_CURRENT_SOURCE_DIR}/server/someinput.txt
# only run if sominput.txt changed
DEPENDS server/someinput.txt
# tell to run in current binary dir
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
# some nice comment in the output
COMMENT "Generating ${CMAKE_BINARY_DIR}/someoutput.txt"
VERBATIM
)
Still the same issue. I guess it is trying to execute my
"generate" even
before it is compiled. Does CMake see that in the line "COMMAND"
generate
refers to the compiled one? Again, since generate is created in
round
one of
make, the second run sees the "generate" and runs fine.
Any help is greatly appreciated! Thanks for your time!
Regards,
Swaroop
2009/8/27 Michael Wild <them...@gmail.com>
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
--
Samuel Goldwyn<
http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html>
- "I'm
willing to admit that I may not always be right, but I am never
wrong."
_______________________________________________
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
--
Ted Turner <http://www.brainyquote.com/quotes/authors/t/ted_turner.html
>
-
"Sports is like a war without the killing."
--
Samuel Goldwyn<http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html
>
- "I'm willing to admit that I may not always be right, but I am never
wrong."
_______________________________________________
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