From 1e4ee94f073c21476a9626c9c7d457a001d23373 Mon Sep 17 00:00:00 2001 From: Markus Frosch Date: Tue, 31 Jul 2018 16:46:11 +0200 Subject: [PATCH] cmake: Improve InstallConfig for stable path handling * similar to install() a non absolute path is prefixed with CMAKE_INSTALL_PREFIX on runtime * in case of CPack path with be prefixed with share/skel/ * DESTDIR is prefixed as well Also see https://cmake.org/cmake/help/latest/command/install.html --- cmake/InstallConfig.cmake | 45 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/cmake/InstallConfig.cmake b/cmake/InstallConfig.cmake index f01357c79..aa39b9dfb 100644 --- a/cmake/InstallConfig.cmake +++ b/cmake/InstallConfig.cmake @@ -15,38 +15,47 @@ # along with this program; if not, write to the Free Software Foundation # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. +# Install $src into directory $dest - usually only used for config files +# +# * similar to install() a non absolute path is prefixed with CMAKE_INSTALL_PREFIX on runtime +# * in case of CPack path with be prefixed with share/skel/ +# * DESTDIR is prefixed as well +# +# also see https://cmake.org/cmake/help/latest/command/install.html function(install_if_not_exists src dest) - set(real_dest "${dest}") if(NOT IS_ABSOLUTE "${src}") set(src "${CMAKE_CURRENT_SOURCE_DIR}/${src}") endif() + get_filename_component(src_name "${src}" NAME) - get_filename_component(basename_dest "${src}" NAME) - string(REPLACE "/" "\\\\" nsis_src "${src}") - string(REPLACE "/" "\\\\" nsis_dest_dir "${real_dest}") - string(REPLACE "/" "\\\\" nsis_dest "${real_dest}/${basename_dest}") + install(CODE " - if(\"\$ENV{DESTDIR}\" STREQUAL \"\") - set(target_dir \${CMAKE_INSTALL_PREFIX}) + set(dest \"${dest}\") + + if (\"\${CMAKE_INSTALL_PREFIX}\" MATCHES .*/_CPack_Packages/.*) + set(dest \"share/skel/\${dest}\") + set(force_overwrite TRUE) else() - set(target_dir \$ENV{DESTDIR}) + set(force_overwrite FALSE) endif() - if(\${CMAKE_INSTALL_PREFIX} MATCHES .*/_CPack_Packages/.* OR NOT EXISTS \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/\${skel_prefix}${dest}/${src_name}\") - message(STATUS \"Installing: \$ENV{DESTDIR}${dest}/${src_name}\") - if(\${CMAKE_INSTALL_PREFIX} MATCHES .*/_CPack_Packages/.*) - set(skel_prefix \"share/skel/\") - else() - set(skel_prefix \"\") - endif() - execute_process(COMMAND \${CMAKE_COMMAND} -E copy \"${src}\" - \"\${target_dir}/\${skel_prefix}${dest}/${src_name}\" + + if(NOT IS_ABSOLUTE \"\${dest}\") + set(dest \"\${CMAKE_INSTALL_PREFIX}/\${dest}\") + endif() + + set(full_dest \"\$ENV{DESTDIR}\${dest}/${src_name}\") + + if(force_overwrite OR NOT EXISTS \"\${full_dest}\") + message(STATUS \"Installing: ${src} into \${full_dest}\") + + execute_process(COMMAND \${CMAKE_COMMAND} -E copy \"${src}\" \"\${full_dest}\" RESULT_VARIABLE copy_result ERROR_VARIABLE error_output) if(copy_result) message(FATAL_ERROR \${error_output}) endif() else() - message(STATUS \"Skipping : \${target_dir}/${dest}/${src_name}\") + message(STATUS \"Skipping : \${full_dest}\") endif() ") endfunction(install_if_not_exists)