[CMake] Avoid overwriting files at installation time

Brad King brad.king at kitware.com
Wed Aug 30 15:07:16 EDT 2006


Marco Canini wrote:
> Hi,
> that's correct.
> In this case a config file should be installed only if it doesn't exist
> in order to preserve what the user wrote.
> But I guess cmake doesn't support it yet.

This is what the INSTALL(CODE) or INSTALL(SCRIPT) commands are meant to
do: create custom installations.  Try code like this:

INSTALL(CODE "
SET(CONFIG \$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/myproj/config)
IF(NOT EXISTS \"\${CONFIG}\")
  CONFIGURE_FILE(\"${CMAKE_CURRENT_SOURCE_DIR}/config.default\"
                 \"\${CONFIG}\" COPY_ONLY)
ENDIF(NOT EXISTS \"\${CONFIG}\")
")

This will put code verbatim into cmake_install.cmake which is run at
install time.  Note that all the quotes and dollars in the script are
escaped so they won't be evaluated when CMake interprets this long
string argument in the CMakeLists.txt file.  However the dollar for
CMAKE_CURRENT_SOURCE_DIR is not escaped so it is replaced immediately
with the proper value.

-Brad


More information about the CMake mailing list