[CMake] Do an execute_process before compilation

Darius Blaszyk dhkblaszyk at zeelandnet.nl
Sat Aug 15 14:37:33 EDT 2009


On Sat, 2009-08-15 at 14:47 +0200, Michael Wild wrote:
> On 15. Aug, 2009, at 14:00, Darius Blaszyk wrote:
> 
> > On Sat, 2009-08-15 at 11:05 +0200, Michael Wild wrote:
> >> On 15. Aug, 2009, at 10:21, Darius Blaszyk wrote:
> >>
> >>> My application links an object file in. This objectfile is not a c+ 
> >>> + -
> >>> code file but some other file that needs to be linked in. The
> >>> problem I
> >>> have now is that each time I change this file I need to run the  
> >>> linker
> >>> from a shell script manually and only then I can issue a make  
> >>> command.
> >>> Is there a way to automate that? In other words can cmake do an
> >>> execute_process before attempting to compile my application if I
> >>> issue a
> >>> make?
> >>>
> >>> Regards, Darius
> >>>
> >>
> >> Have a look at ADD_CUSTOM_COMMAND(TARGET PRE_LINK ...)
> >>
> >> Michael
> >
> > I did a test, but the make process complained "Cannot find source file
> > myobj.o". Which seems to be logical because the .o file needs to be
> > created first by ADD_CUSTOM_COMMAND.
> > What I have now is:
> >
> > ADD_CUSTOM_COMMAND(TARGET mytest_gui PRE_LINK COMMAND ld -r -b  
> > binary -o
> > mytest_glade.o mytest.glade)
> > ADD_CUSTOM_COMMAND(TARGET mytest_gui PRE_LINK COMMAND objcopy
> > --rename-section .data=.rodata,alloc,load,readonly,data,contents
> > mytest_glade.o mytest_glade.o)
> > ADD_EXECUTABLE(mytest_gui main.cpp mytest_glade.o)
> > TARGET_LINK_LIBRARIES(mytest_gui ${GTKMM_LIBRARIES})
> >
> > This obviously does not work because of a missing mytest_glade.o, but
> > how to circumvent that?
> >
> > Darius
> >
> 
> Well then, how do you create the mytest_glade.o in the first place  
> (say, without CMake)?
I was using a bash script. I had to run it manually after each change to the .glade file.

> Besides, you are adding two custom commands in the PRE_LINK stage to a  
> target, both operating on the same file. However, their execution  
> order is not specified. Am I assuming correctly, that you are using  
> the "ld -r -b" command to create a "resource file" from the glade file  
> which you then link into your binary (I never do such "crazy" stuff,  
> hence the question)? I probably would do that like this:
Exactly

> -- >8 --
> 
> set( GLADE_BIN_RSRC ${CMAKE_CURRENT_BINARY_DIR}$ 
> {CMAKE_FILES_DIRECTORY}/mytest_gui.dir/mytest_glade.o )
> add_custom_command( OUTPUT ${GLADE_BIN_RSRC}
>    COMMAND ld -r -b binary -o ${GLADE_BIN_RSRC} $ 
> {CMAKE_CURRENT_SOURCE_DIR}/mytest.glade
>    COMMAND objcopy --rename- 
> section .data=.rodata,alloc,load,readonly,data,contents $ 
> {GLADE_BIN_RSRC} ${GLADE_BIN_RSRC}
>    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/mytest.glade
>    COMMENT "Creating resource file ${GLADE_BIN_RSRC}
>    VERBATIM
> )
> 
> add_executable( mytest_gui main.cpp ${GLADE_BIN_RSRC} )
> 
> -- 8< --
> 
> This specifies a single rule how to generate the mytest_glade.o file  
> from the mytest.glade and then simply adds it to the add_executable  
> call. CMake now knows that mytest_gui depends on mytest_glade.o, and  
> also how to create it from mytest.glade.
> 
> HTH and works
Wow! Thank you very much. It works like a charm.

Darius



More information about the CMake mailing list