[Cmake] CMakeCommand feature in CMake

Andy Cedilnik Andy.Cedilnik at kitware.com
Fri Mar 15 16:44:53 EST 2002


Hello!

I added a new feature to cmake which is ccommand. What this is is
essentially a wrapper around system tools and it can provide a
platform independent way of executing system commands during build.

For example I am building a shared library and would like it to be
renamed to a unified form such as vtkXY.shl. This is usefull if
you want to load it to your program during runtime as a let say
module. So, the obvious way to do that is to add custom rule
which copies library after the build process is done. However,
how do you copy a file? Well, cp, copy, ... Huh?

Here it is:

ccommand copy libvtkXY.so vtkXY.shl

Now, what do we put to CMakeLists.txt?

   SET(MY_LIB_SUFFIX ${CMAKE_SHLIB_SUFFIX})
   SET(MY_LIB_PREFIX "lib")
   IF(WIN32)
     IF(NOT CYGWIN)
       SET(MY_LIB_PREFIX "")
       SET(MY_LIB_SUFFIX ".dll")
     ENDIF(NOT CYGWIN)
   ENDIF(WIN32)

   SET(MY_LIB_NAME ${MY_LIB_PREFIX}vtkXY${MY_LIB_SUFFIX})

This gives us the name of the original library. There might be a
better way, but this is what I did. Now, on all unix like systems
and on windows when using nmake, the rule is:

  ADD_CUSTOM_COMMAND(
       SOURCE vtkXY
       COMMAND ${CCOMMAND_COMMAND}
       ARGS copy
       ${EXECUTABLE_OUTPUT_PATH}/${MY_LIB_NAME}
       ${EXECUTABLE_OUTPUT_PATH}/vtkXY.pvm
       TARGET vtkXY )

This will after the library is build copy the file to the new
location (or the new name). Unfortunately when using Visual
Studio, you have a problem. Visual studio builds files in
directories Debug,Release,RelWithDeb, and MinSizeRel.
That is why the whole code would look like this:

   IF(CMAKE_BUILD_TOOL MATCHES msdev)
     FOREACH(WINDOWS_BUILD_TYPE Debug Release RelWithDeb MinSizeRel)
       ADD_CUSTOM_COMMAND(
         SOURCE vtkXY
         COMMAND ${CCOMMAND_COMMAND}
         ARGS copy
         ${EXECUTABLE_OUTPUT_PATH}/${WINDOWS_BUILD_TYPE}/${MY_LIB_NAME}
         ${EXECUTABLE_OUTPUT_PATH}/${WINDOWS_BUILD_TYPE}/vtkXY.pvm
         TARGET vtkXY )
     ENDFOREACH(WINDOWS_BUILD_TYPE Debug Release RelWithDeb MinSizeRel)
   ELSE(CMAKE_BUILD_TOOL MATCHES msdev)
     ADD_CUSTOM_COMMAND(
       SOURCE vtkXY
       COMMAND ${CCOMMAND_COMMAND}
       ARGS copy
       ${EXECUTABLE_OUTPUT_PATH}/${MY_LIB_NAME}
       ${EXECUTABLE_OUTPUT_PATH}/vtkXY.pvm
       TARGET vtkXY )
   ENDIF(CMAKE_BUILD_TOOL MATCHES msdev)

Unfortunately this will produce three errors every time it builds.

Currently ccommand knows two commands:
ccommand copy file1 file2
ccommand remove file1 file2 ...

You get the picture.
Please comment.

				Andy




More information about the CMake mailing list