BASIS  version 1.2.3 (revision 2104)
InstallationTools.cmake
Go to the documentation of this file.
00001 ##############################################################################
00002 # @file  InstallationTools.cmake
00003 # @brief CMake functions used for installation.
00004 #
00005 # Copyright (c) 2011, 2012 University of Pennsylvania. All rights reserved.<br />
00006 # See https://www.cbica.upenn.edu/sbia/software/license.html or COPYING file.
00007 #
00008 # Contact: SBIA Group <sbia-software at uphs.upenn.edu>
00009 #
00010 # @ingroup CMakeTools
00011 ##############################################################################
00012 
00013 ## @addtogroup CMakeUtilities
00014 # @{
00015 
00016 
00017 # ============================================================================
00018 # Installation
00019 # ============================================================================
00020 
00021 # ----------------------------------------------------------------------------
00022 ## @brief Specify rules to run at install time.
00023 #
00024 # This function replaces CMake's
00025 # <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:install">
00026 # install()</a> command.
00027 #
00028 # @sa http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:install
00029 #
00030 # @ingroup CMakeAPI
00031 function (basis_install)
00032   install (${ARGN})
00033 endfunction ()
00034 
00035 # ----------------------------------------------------------------------------
00036 ## @brief Install content of source directory excluding typical files.
00037 #
00038 # Files which are excluded are typical backup files, system files, files
00039 # from revision control systems, and CMakeLists.txt files.
00040 #
00041 # Example:
00042 # @code
00043 # basis_install_directory("${INSTALL_DATA_DIR}")
00044 # basis_install_directory(. "${INSTALL_DATA_DIR}")
00045 # basis_install_directory("${CMAKE_CURRENT_SOURCE_DIR}" "${INSTALL_DATA_DIR}")
00046 # basis_install_directory(images "${INSTALL_DATA_DIR}/images")
00047 # @endcode
00048 #
00049 # @param [in] ARGN The first two arguments are extracted from the beginning
00050 #                  of this list in the named order (without option name),
00051 #                  and the remaining arguments are passed on to CMake's
00052 #                  <a href="http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:install">
00053 #                  <tt>install(DIRECTORY)</tt></a> command.
00054 # @par
00055 # <table border="0">
00056 #   <tr>
00057 #     @tp @b SOURCE @endtp
00058 #     <td>Source directory. Defaults to current source directory
00059 #         if only one argument, the @p DESTINATION, is given./td>
00060 #   </tr>
00061 #   <tr>
00062 #     @tp @b DESTINATION @endtp
00063 #     <td>Destination directory.</td>
00064 #   </tr>
00065 # </table>
00066 #
00067 # @sa http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:install
00068 #
00069 # @ingroup CMakeAPI
00070 function (basis_install_directory)
00071   if (ARGC EQUAL 1)
00072     set (SOURCE      "${CMAKE_CURRENT_SOURCE_DIR}")
00073     set (DESTINATION "${ARGV0}")
00074     set (OPTIONS     "${ARGV}")
00075     list (REMOVE_AT OPTIONS 0)
00076   elseif (ARGC GREATER 1)
00077     set (SOURCE      "${ARGV0}")
00078     set (DESTINATION "${ARGV1}")
00079     set (OPTIONS     "${ARGV}")
00080     list (REMOVE_AT OPTIONS 0 1)
00081   else ()
00082     message (FATAL_ERROR "Too few arguments given!")
00083   endif ()
00084   basis_sanitize_for_regex (REGEX "${PROJECT_SOURCE_DIR}")
00085   if (IS_ABSOLUTE "${DESTINATION}")
00086     set (DESTINATION_ABSDIR "${DESTINATION}")
00087   else ()
00088     set (DESTINATION_ABSDIR "${INSTALL_PREFIX}/${DESTINATION}")
00089   endif ()
00090   if ("${DESTINATION_ABSDIR}" MATCHES "^${REGEX}")
00091     message (FATAL_ERROR "Installation directory ${DESTINATION_ABSDIR} is inside the project source tree!")
00092   endif ()
00093   install (
00094     DIRECTORY   "${SOURCE}/"
00095     DESTINATION "${DESTINATION}"
00096     ${OPTIONS}
00097     PATTERN     CMakeLists.txt EXCLUDE
00098     PATTERN     *~             EXCLUDE
00099     PATTERN     .svn           EXCLUDE
00100     PATTERN     .git           EXCLUDE
00101     PATTERN     .DS_Store      EXCLUDE
00102   )
00103 endfunction ()
00104 
00105 # ----------------------------------------------------------------------------
00106 ## @brief Add installation rule to create a symbolic link.
00107 #
00108 # Note that the installation rule will only be effective on a Unix-like
00109 # system, i.e., one which supports the creation of a symbolic link.
00110 #
00111 # @param [in] OLD  The value of the symbolic link.
00112 # @param [in] NEW  The name of the symbolic link.
00113 #
00114 # @returns Adds installation rule to create the symbolic link @p NEW.
00115 #
00116 # @ingroup CMakeAPI
00117 function (basis_install_link OLD NEW)
00118   # Attention: CMAKE_INSTALL_PREFIX must be used instead of INSTALL_PREFIX.
00119   set (CMD_IN
00120     "
00121     set (OLD \"@OLD@\")
00122     set (NEW \"@NEW@\")
00123 
00124     if (NOT IS_ABSOLUTE \"\${OLD}\")
00125       set (OLD \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/\${OLD}\")
00126     endif ()
00127     if (NOT IS_ABSOLUTE \"\${NEW}\")
00128       set (NEW \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/\${NEW}\")
00129     endif ()
00130 
00131     if (IS_SYMLINK \"\${NEW}\")
00132       file (REMOVE \"\${NEW}\")
00133     endif ()
00134 
00135     if (EXISTS \"\${NEW}\")
00136       message (STATUS \"Skipping: \${NEW} -> \${OLD}\")
00137     else ()
00138       message (STATUS \"Installing: \${NEW} -> \${OLD}\")
00139 
00140       get_filename_component (SYMDIR \"\${NEW}\" PATH)
00141 
00142       file (RELATIVE_PATH OLD \"\${SYMDIR}\" \"\${OLD}\")
00143 
00144       if (NOT EXISTS \${SYMDIR})
00145         file (MAKE_DIRECTORY \"\${SYMDIR}\")
00146       endif ()
00147 
00148       execute_process (
00149         COMMAND \"${CMAKE_COMMAND}\" -E create_symlink \"\${OLD}\" \"\${NEW}\"
00150         RESULT_VARIABLE RETVAL
00151       )
00152 
00153       if (NOT RETVAL EQUAL 0)
00154         message (ERROR \"Failed to create (symbolic) link \${NEW} -> \${OLD}\")
00155       else ()
00156         list (APPEND CMAKE_INSTALL_MANIFEST_FILES \"\${NEW}\")
00157       endif ()
00158     endif ()
00159     "
00160   )
00161 
00162   string (CONFIGURE "${CMD_IN}" CMD @ONLY)
00163   install (CODE "${CMD}")
00164 endfunction ()
00165 
00166 # ----------------------------------------------------------------------------
00167 ## @brief Adds installation rules to create default symbolic links.
00168 #
00169 # This function creates for each main executable a symbolic link directly
00170 # in the directory @c INSTALL_PREFIX/bin if @c INSTALL_SINFIX is TRUE and the
00171 # software is installed on a Unix-like system, i.e., one which
00172 # supports the creation of symbolic links.
00173 #
00174 # @returns Adds installation command for creation of symbolic links in the
00175 #          installation tree.
00176 function (basis_install_links)
00177   if (NOT UNIX)
00178     return ()
00179   endif ()
00180 
00181   # main executables
00182   basis_get_project_property (TARGETS PROPERTY TARGETS)
00183   foreach (TARGET_UID ${TARGETS})
00184     get_target_property (IMPORTED ${TARGET_UID} "IMPORTED")
00185 
00186     if (NOT IMPORTED)
00187       get_target_property (BASIS_TYPE ${TARGET_UID} "BASIS_TYPE")
00188       get_target_property (LIBEXEC    ${TARGET_UID} "LIBEXEC")
00189       get_target_property (TEST       ${TARGET_UID} "TEST")
00190 
00191       if (BASIS_TYPE MATCHES "EXECUTABLE" AND NOT LIBEXEC AND NOT TEST)
00192         get_target_property (SYMLINK_NAME ${TARGET_UID} "SYMLINK_NAME")
00193         if (NOT "${SYMLINK_NAME}" MATCHES "^none$|^None$|^NONE$")
00194           get_target_property (SYMLINK_PREFIX ${TARGET_UID} "SYMLINK_PREFIX")
00195           get_target_property (SYMLINK_SUFFIX ${TARGET_UID} "SYMLINK_SUFFIX")
00196           get_target_property (INSTALL_DIR    ${TARGET_UID} "RUNTIME_INSTALL_DIRECTORY")
00197 
00198           basis_get_target_location (OUTPUT_NAME ${TARGET_UID} NAME)
00199 
00200           if (NOT SYMLINK_NAME)
00201             set (SYMLINK_NAME "${OUTPUT_NAME}")
00202           endif ()
00203           if (SYMLINK_PREFIX)
00204             set (SYMLINK_NAME "${SYMLINK_PREFIX}${SYMLINK_NAME}")
00205           endif ()
00206           if (SYMLINK_SUFFIX)
00207             set (SYMLINK_NAME "${SYMLINK_NAME}${SYMLINK_SUFFIX}")
00208           endif ()
00209 
00210           # avoid creation of symbolic link if there would be a conflict with
00211           # the subdirectory in bin/ where the actual executables are installed
00212           if (INSTALL_SINFIX AND "${SYMLINK_NAME}" STREQUAL "${BASIS_INSALL_SINFIX}")
00213             message (STATUS \"Skipping: ${INSTALL_DIR}/${OUTPUT_NAME} -> ${INSTALL_PREFIX}/bin/${SYMLINK_NAME}\")
00214           else ()
00215             basis_install_link (
00216               "${INSTALL_DIR}/${OUTPUT_NAME}"
00217               "bin/${SYMLINK_NAME}"
00218             )
00219           endif ()
00220         endif ()
00221       endif ()
00222     endif ()
00223   endforeach ()
00224 endfunction ()
00225 
00226 # ============================================================================
00227 # Deinstallation
00228 # ============================================================================
00229 
00230 # ----------------------------------------------------------------------------
00231 ## @brief Add uninstall target.
00232 #
00233 # @returns Adds the custom target @c uninstall and code to
00234 #          <tt>cmake_install.cmake</tt> to install an uninstaller.
00235 function (basis_add_uninstall)
00236   # add uninstall target
00237   configure_file (
00238     ${BASIS_MODULE_PATH}/cmake_uninstall.cmake.in
00239     ${PROJECT_BINARY_DIR}/cmake_uninstall.cmake
00240     @ONLY
00241   )
00242   add_custom_target (
00243     uninstall
00244     COMMAND ${CMAKE_COMMAND} -P "${PROJECT_BINARY_DIR}/cmake_uninstall.cmake"
00245     COMMENT "Uninstalling..."
00246   )
00247 endfunction ()
00248 
00249 
00250 ## @}
00251 # end of Doxygen group