CMakeCommon.cmake 6.3 KB
Newer Older
1
function (write_lib_files PKG_INSTALL_LIBS OUTPUT_FILE)
2 3 4
    # Find library file and add the versioned form of each library
    set(PKG_INSTALL_LIBS_FILES)
    foreach(l ${PKG_INSTALL_LIBS})
5 6 7 8 9
        IF(${CMAKE_MAJOR_VERSION} LESS 3)
            get_target_property(TARGET_LOCATION ${l} LOCATION)
        ELSE ()
            SET(TARGET_LOCATION $<TARGET_LINKER_FILE:${l}>)
        ENDIF()
10 11 12 13 14 15 16 17 18 19 20 21
        if (NOT TARGET_LOCATION)
            message(FATAL_ERROR "Target '${l}' could not be found.")
        endif ()
        list(APPEND PKG_INSTALL_LIBS_FILES ${TARGET_LOCATION})
        if (APPLE)
            list(APPEND PKG_INSTALL_LIBS_FILES 
                        ${TARGET_LOCATION}.${VERSION_MAJOR_MINOR})
        else ()
            list(APPEND PKG_INSTALL_LIBS_FILES 
                        ${TARGET_LOCATION}.${NEKTAR_VERSION})
        endif()
    endforeach()
22 23

    # Output the list of files to be installed in the package
24 25 26 27 28 29
    IF(${CMAKE_MAJOR_VERSION} LESS 3)
        file(WRITE "${OUTPUT_FILE}" "${PKG_INSTALL_LIBS_FILES}")
    ELSE ()
        file(GENERATE OUTPUT "${OUTPUT_FILE}"
             CONTENT "${PKG_INSTALL_LIBS_FILES}")
    ENDIF ()
30 31
endfunction ()

32
function (write_bin_files PKG_INSTALL_BINS OUTPUT_FILE)
33 34 35
    # Find binary files
    set(PKG_INSTALL_BINS_FILES)
    foreach(b ${PKG_INSTALL_BINS})
36 37 38 39 40
        IF(${CMAKE_MAJOR_VERSION} LESS 3)
            get_target_property(TARGET_LOCATION ${b} LOCATION)
        ELSE ()
            SET(TARGET_LOCATION $<TARGET_FILE:${b}>)
        ENDIF ()
41
        if (NOT TARGET_LOCATION)
42
            message(FATAL_ERROR "Target '${b}' could not be found.")
43 44 45
        endif ()
        list(APPEND PKG_INSTALL_BINS_FILES ${TARGET_LOCATION})
    endforeach()
46 47

    # Output the list of files to be installed in the package
48 49 50 51 52 53
    IF(${CMAKE_MAJOR_VERSION} LESS 3)
        file(WRITE "${OUTPUT_FILE}" "${PKG_INSTALL_BINS_FILES}")
    ELSE ()
        file(GENERATE OUTPUT "${OUTPUT_FILE}"
             CONTENT "${PKG_INSTALL_BINS_FILES}")
    ENDIF ()
54 55
endfunction ()

56
macro (add_deb_package)
57 58 59 60 61
    if (DPKG)
        set(options "")
        set(oneValueArgs NAME SUMMARY DESCRIPTION)
        set(multiValueArgs INSTALL_LIBS INSTALL_BINS BREAKS CONFLICTS DEPENDS)
        cmake_parse_arguments(PKG "${options}"
62 63
            "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

64
        set(BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${PKG_NAME}-deb)
65

66
        file(MAKE_DIRECTORY "${BUILD_DIR}/targets")
67 68 69 70
        write_lib_files("${PKG_INSTALL_LIBS}" 
                        "${BUILD_DIR}/targets/install_libs.txt")
        write_bin_files("${PKG_INSTALL_BINS}"
                        "${BUILD_DIR}/targets/install_bins.txt")
Chris Cantwell's avatar
Chris Cantwell committed
71

72 73
        # Configure project for this package
        configure_file(CMakeListsDpkg.txt.in
74
                ${BUILD_DIR}/CMakeLists.txt @ONLY)
75

76 77 78 79 80 81 82 83 84 85 86 87
        add_custom_target(
            pkg-deb-${PKG_NAME}
            rm -f ${BUILD_DIR}/CPackConfig.cmake
            COMMAND ${CMAKE_COMMAND} .
            COMMAND ${CMAKE_CPACK_COMMAND} --config CPackConfig.cmake
            WORKING_DIRECTORY ${BUILD_DIR}
        )
        if (PKG_INSTALL_LIBS OR PKG_INSTALL_BINS)
            add_dependencies(pkg-deb-${PKG_NAME}
                ${PKG_INSTALL_LIBS} ${PKG_INSTALL_BINS})
        endif ()
        add_dependencies(pkg-deb pkg-deb-${PKG_NAME})
88
    endif ()
89 90 91
endmacro (add_deb_package)

macro (add_rpm_package)
92 93 94 95 96
    if (RPMBUILD)
        set(options "")
        set(oneValueArgs NAME SUMMARY DESCRIPTION)
        set(multiValueArgs INSTALL_LIBS INSTALL_BINS BREAKS CONFLICTS DEPENDS)
        cmake_parse_arguments(PKG "${options}"
97 98
            "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

99
        set(BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${PKG_NAME}-rpm)
100

101
        file(MAKE_DIRECTORY "${BUILD_DIR}/targets")
102 103 104 105
        write_lib_files("${PKG_INSTALL_LIBS}"
                        "${BUILD_DIR}/targets/install_libs.txt")
        write_bin_files("${PKG_INSTALL_BINS}"
                        "${BUILD_DIR}/targets/install_bins.txt")
Chris Cantwell's avatar
Chris Cantwell committed
106

107
        configure_file(CMakeListsRpm.txt.in
108
                ${BUILD_DIR}/CMakeLists.txt @ONLY)
109 110 111 112 113 114 115 116 117 118 119 120
        add_custom_target(
            pkg-rpm-${PKG_NAME}
            rm -f ${BUILD_DIR}/CPackConfig.cmake
            COMMAND ${CMAKE_COMMAND} .
            COMMAND ${CMAKE_CPACK_COMMAND} --config CPackConfig.cmake
            WORKING_DIRECTORY ${BUILD_DIR}
        )
        if (PKG_INSTALL_LIBS OR PKG_INSTALL_BINS)
            add_dependencies(pkg-rpm-${PKG_NAME}
                ${PKG_INSTALL_LIBS} ${PKG_INSTALL_BINS})
        endif ()
        add_dependencies(pkg-rpm pkg-rpm-${PKG_NAME})
121
    endif ()
122 123
endmacro (add_rpm_package)

124 125 126 127 128 129 130 131
macro (add_tgz_package)
    set(options "")
    set(oneValueArgs NAME SUMMARY DESCRIPTION)
    set(multiValueArgs INSTALL_LIBS INSTALL_BINS BREAKS CONFLICTS DEPENDS)
    cmake_parse_arguments(PKG "${options}"
            "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

    set(BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/${PKG_NAME}-tgz)
132

133
    file(MAKE_DIRECTORY "${BUILD_DIR}/targets")
134 135 136 137
    write_lib_files("${PKG_INSTALL_LIBS}"
                    "${BUILD_DIR}/targets/install_libs.txt")
    write_bin_files("${PKG_INSTALL_BINS}"
                    "${BUILD_DIR}/targets/install_bins.txt")
138

139 140 141 142 143 144
    configure_file(CMakeListsTgz.txt.in
                ${BUILD_DIR}/CMakeLists.txt @ONLY)
    add_custom_target(
        pkg-tgz-${PKG_NAME}
        rm -f ${BUILD_DIR}/CPackConfig.cmake
        COMMAND ${CMAKE_COMMAND} .
145
        COMMAND ${CMAKE_CPACK_COMMAND} --config CPackConfig.cmake
146 147
        WORKING_DIRECTORY ${BUILD_DIR}
    )
148 149 150 151
    if (PKG_INSTALL_LIBS OR PKG_INSTALL_BINS)
        add_dependencies(pkg-tgz-${PKG_NAME}
            ${PKG_INSTALL_LIBS} ${PKG_INSTALL_BINS})
    endif()
152 153 154
    add_dependencies(pkg-tgz pkg-tgz-${PKG_NAME})
endmacro (add_tgz_package)

155 156
# Base packaging target
add_custom_target(pkg)
157

158
# Check if we can build DEB files
159 160 161
find_program(DPKG "dpkg")
mark_as_advanced(DPKG)
find_program(DPKGSHLIBDEPS "dpkg-shlibdeps")
162
mark_as_advanced(DPKGSHLIBDEPS)
163 164 165 166 167 168 169 170
if (DPKG)
    if (NOT DPKGSHLIBDEPS)
        MESSAGE(FATAL_ERROR "dpkg-shlibdeps program not found but is required.")
    endif ()
    add_custom_target(pkg-deb)
    add_dependencies(pkg pkg-deb)
endif (DPKG)

171 172 173
# Check if we can build RPM files
find_program(RPMBUILD "rpmbuild")
mark_as_advanced(RPMBUILD)
174 175 176 177 178
if (RPMBUILD)
    add_custom_target(pkg-rpm)
    add_dependencies(pkg pkg-rpm)
endif (RPMBUILD)

179
# Binary archive target
180 181
add_custom_target(pkg-tgz)
add_dependencies(pkg pkg-tgz)