[CMake] How to executing a script right after an add_test command ?

Delphinus Delphinus tibouroxane at yahoo.fr
Thu Sep 8 11:59:51 EDT 2011


On Thu, Sep 8, 2011 at 3:39 PM, David Cole <david.cole at kitware.com> wrote:

> On Thu, Sep 8, 2011 at 9:30 AM, Delphinus Delphinus wrote:
> > Hi all,
> >
> > I am using the Qt module QTestLib to do some tests on my application
> > (windows, MSVC 2008) that has been all set up with CMake.
> >
> > A test I have created with QTestLib is executed, all output is sent to
> the
> > command window. Since I want it to a file, I used what Qt suggest to
> > redirect all output: -xml -o my_file.xml
> > so I do this in my CMake file:
> >
> > add_test ( NAME   ${PROJECT_NAME}   COMMAND   ${PROJECT_NAME} -xml -o
> > log/foo.xml )
> >
> > So now, when I do the "run_test" through visual, my test is run and my
> file
> > is created (so far so good!).
> >
> >
> > My problem is that I want the name of the file to be time and date
> dependant
> > (log20110908140230.xml - a year, month, day, hour, minute and second
> > format).
> >
> > So I have considered the following solution (I did not succeed in
> > implementing it so far):
> > I want to execute a script, just after the execution of my test that will
> > rename the result file generated.
> > --> Here I don't know how I can tell CMake to execute my script just when
> > the test has been executed ?
> >
> > NB: I do NOT have any problem creating a script that rename a foo.xml
> file
> > to a log20110908140230.xml file (my script.bat file is already ready).
> The
> > problem is how to make this script to be executed just after the test
> > execution.
> >
> >
> > Any insight is welcomed on how to implement my suggestion (or if you have
> > another choice, I'm all ears :)! ).
> >
> >
>
> You can either:
>
> (1) execute your script as an additional test that depends on the
> first one (so that guarantees it to run *after* the test, even in
> parallel testing scenarios, although not necessarily *immediately*
> after)
>
> or
>
> (2) execute a single script as the test itself, and then inside that
> script, execute the real test first, followed immediately by the
> script your already have...
>
> I would do option #2 if I were you. :-)
>
> You can probably find other examples of this by googling around for
> "add_test script cmake" ( one of the hits is another reply I made
> previously on this list:
> http://www.cmake.org/pipermail/cmake/2011-May/044180.html )
>
>
> HTH,
> David
>


Thanks. And yes, it helped !

I chose option #2.

I created a runAndRename.cmake file:
<<<<<
macro(COMMAND_EXEC1 P_COMMAND)
    execute_process(COMMAND ${P_COMMAND} -xml -o log/foo.xml RESULT_VARIABLE
RESULT_VAR_COMMAND)
    if(RESULT_VAR_COMMAND)
        message(FATAL_ERROR "Error ${P_COMMAND}")
    endif()
endmacro()

macro(COMMAND_EXEC2 P_COMMAND)
    execute_process(COMMAND ${P_COMMAND} WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR} RESULT_VARIABLE RESULT_VAR_COMMAND)
    if(RESULT_VAR_COMMAND)
        message(FATAL_ERROR "Error ${P_COMMAND}")
    endif()
endmacro()

COMMAND_EXEC1(${COMMAND1})
COMMAND_EXEC2(${COMMAND2})
>>>>>

And then in my CMakeLists.txt, I have now:
<<<<<
add_test( NAME ${PROJECT_NAME} COMMAND "${CMAKE_COMMAND}"
-DCOMMAND1=$<TARGET_FILE:${PROJECT_NAME}>

-DCOMMAND2=renameExe
                                                          -P
"${CMAKE_CURRENT_SOURCE_DIR}/runAndRename.cmake")
>>>>>

It is working for me, even though there is still a lot of room for
improvement:
- I did not find the proper way to pass the needed arguments (-xml -o
filename) ... so I created two macros :(.
- And I spent most of my time figuring out that I should "transform"
myScript.BAT to a myScript.EXE to make my second part work !

I'll look into those later...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110908/d04e15af/attachment.htm>


More information about the CMake mailing list