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
This commit is contained in:
Markus Frosch 2018-07-31 16:46:11 +02:00 committed by Gunnar Beutner
parent 8bac1dc99e
commit 1e4ee94f07
1 changed files with 27 additions and 18 deletions

View File

@ -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 \"\")
if(NOT IS_ABSOLUTE \"\${dest}\")
set(dest \"\${CMAKE_INSTALL_PREFIX}/\${dest}\")
endif()
execute_process(COMMAND \${CMAKE_COMMAND} -E copy \"${src}\"
\"\${target_dir}/\${skel_prefix}${dest}/${src_name}\"
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)