BuildUtilsCore.cmake 9.3 KB
Newer Older
X
xj.lin 已提交
1 2
# Define a function that check last file modification
function(Check_Last_Modify cache_check_lists_file_path working_dir last_modified_commit_id)
Y
youny626 已提交
3 4
    if (EXISTS "${working_dir}")
        if (EXISTS "${cache_check_lists_file_path}")
X
xj.lin 已提交
5 6 7 8
            set(GIT_LOG_SKIP_NUM 0)
            set(_MATCH_ALL ON CACHE BOOL "Match all")
            set(_LOOP_STATUS ON CACHE BOOL "Whether out of loop")
            file(STRINGS ${cache_check_lists_file_path} CACHE_IGNORE_TXT)
Y
youny626 已提交
9 10 11
            while (_LOOP_STATUS)
                foreach (_IGNORE_ENTRY ${CACHE_IGNORE_TXT})
                    if (NOT _IGNORE_ENTRY MATCHES "^[^#]+")
X
xj.lin 已提交
12
                        continue()
Y
youny626 已提交
13
                    endif ()
X
xj.lin 已提交
14 15 16

                    set(_MATCH_ALL OFF)
                    execute_process(COMMAND git log --no-merges -1 --skip=${GIT_LOG_SKIP_NUM} --name-status --pretty= WORKING_DIRECTORY ${working_dir} OUTPUT_VARIABLE CHANGE_FILES)
Y
youny626 已提交
17
                    if (NOT CHANGE_FILES STREQUAL "")
X
xj.lin 已提交
18
                        string(REPLACE "\n" ";" _CHANGE_FILES ${CHANGE_FILES})
Y
youny626 已提交
19
                        foreach (_FILE_ENTRY ${_CHANGE_FILES})
X
xj.lin 已提交
20 21 22 23
                            string(REGEX MATCH "[^ \t]+$" _FILE_NAME ${_FILE_ENTRY})
                            execute_process(COMMAND sh -c "echo ${_FILE_NAME} | grep ${_IGNORE_ENTRY}" RESULT_VARIABLE return_code)
                            if (return_code EQUAL 0)
                                execute_process(COMMAND git log --no-merges -1 --skip=${GIT_LOG_SKIP_NUM} --pretty=%H WORKING_DIRECTORY ${working_dir} OUTPUT_VARIABLE LAST_MODIFIED_COMMIT_ID)
Y
youny626 已提交
24
                                set(${last_modified_commit_id} ${LAST_MODIFIED_COMMIT_ID} PARENT_SCOPE)
X
xj.lin 已提交
25
                                set(_LOOP_STATUS OFF)
Y
youny626 已提交
26 27 28
                            endif ()
                        endforeach ()
                    else ()
X
xj.lin 已提交
29
                        set(_LOOP_STATUS OFF)
Y
youny626 已提交
30 31
                    endif ()
                endforeach ()
X
xj.lin 已提交
32

Y
youny626 已提交
33
                if (_MATCH_ALL)
X
xj.lin 已提交
34
                    execute_process(COMMAND git log --no-merges -1 --skip=${GIT_LOG_SKIP_NUM} --pretty=%H WORKING_DIRECTORY ${working_dir} OUTPUT_VARIABLE LAST_MODIFIED_COMMIT_ID)
Y
youny626 已提交
35
                    set(${last_modified_commit_id} ${LAST_MODIFIED_COMMIT_ID} PARENT_SCOPE)
X
xj.lin 已提交
36
                    set(_LOOP_STATUS OFF)
Y
youny626 已提交
37
                endif ()
X
xj.lin 已提交
38 39

                math(EXPR GIT_LOG_SKIP_NUM "${GIT_LOG_SKIP_NUM} + 1")
Y
youny626 已提交
40 41
            endwhile (_LOOP_STATUS)
        else ()
X
xj.lin 已提交
42
            execute_process(COMMAND git log --no-merges -1 --skip=${GIT_LOG_SKIP_NUM} --pretty=%H WORKING_DIRECTORY ${working_dir} OUTPUT_VARIABLE LAST_MODIFIED_COMMIT_ID)
Y
youny626 已提交
43 44 45
            set(${last_modified_commit_id} ${LAST_MODIFIED_COMMIT_ID} PARENT_SCOPE)
        endif ()
    else ()
X
xj.lin 已提交
46
        message(FATAL_ERROR "The directory ${working_dir} does not exist")
Y
youny626 已提交
47
    endif ()
X
xj.lin 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
endfunction()

# Define a function that extracts a cached package
function(ExternalProject_Use_Cache project_name package_file install_path)
    message(STATUS "Will use cached package file: ${package_file}")

    ExternalProject_Add(${project_name}
            DOWNLOAD_COMMAND ${CMAKE_COMMAND} -E echo
            "No download step needed (using cached package)"
            CONFIGURE_COMMAND ${CMAKE_COMMAND} -E echo
            "No configure step needed (using cached package)"
            BUILD_COMMAND ${CMAKE_COMMAND} -E echo
            "No build step needed (using cached package)"
            INSTALL_COMMAND ${CMAKE_COMMAND} -E echo
            "No install step needed (using cached package)"
            )

    # We want our tar files to contain the Install/<package> prefix (not for any
    # very special reason, only for consistency and so that we can identify them
    # in the extraction logs) which means that we must extract them in the
    # binary (top-level build) directory to have them installed in the right
    # place for subsequent ExternalProjects to pick them up. It seems that the
    # only way to control the working directory is with Add_Step!
    ExternalProject_Add_Step(${project_name} extract
            ALWAYS 1
            COMMAND
            ${CMAKE_COMMAND} -E echo
            "Extracting ${package_file} to ${install_path}"
            COMMAND
            ${CMAKE_COMMAND} -E tar xzvf ${package_file} ${install_path}
S
starlord 已提交
78
            WORKING_DIRECTORY ${INDEX_BINARY_DIR}
X
xj.lin 已提交
79 80 81 82 83 84 85
            )

    ExternalProject_Add_StepTargets(${project_name} extract)
endfunction()

# Define a function that to create a new cached package
function(ExternalProject_Create_Cache project_name package_file install_path cache_username cache_password cache_path)
Y
youny626 已提交
86
    if (EXISTS ${package_file})
X
xj.lin 已提交
87 88
        message(STATUS "Removing existing package file: ${package_file}")
        file(REMOVE ${package_file})
Y
youny626 已提交
89
    endif ()
X
xj.lin 已提交
90

91
    string(REGEX REPLACE "(.+)/.+$" "\\1" package_dir ${package_file})
Y
youny626 已提交
92
    if (NOT EXISTS ${package_dir})
93
        file(MAKE_DIRECTORY ${package_dir})
Y
youny626 已提交
94
    endif ()
95

X
xj.lin 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    message(STATUS "Will create cached package file: ${package_file}")

    ExternalProject_Add_Step(${project_name} package
            DEPENDEES install
            BYPRODUCTS ${package_file}
            COMMAND ${CMAKE_COMMAND} -E echo "Updating cached package file: ${package_file}"
            COMMAND ${CMAKE_COMMAND} -E tar czvf ${package_file} ${install_path}
            COMMAND ${CMAKE_COMMAND} -E echo "Uploading package file ${package_file} to ${cache_path}"
            COMMAND curl -u${cache_username}:${cache_password} -T ${package_file} ${cache_path}
            )

    ExternalProject_Add_StepTargets(${project_name} package)
endfunction()

function(ADD_THIRDPARTY_LIB LIB_NAME)
    set(options)
    set(one_value_args SHARED_LIB STATIC_LIB)
    set(multi_value_args DEPS INCLUDE_DIRECTORIES)
    cmake_parse_arguments(ARG
            "${options}"
            "${one_value_args}"
            "${multi_value_args}"
            ${ARGN})
Y
youny626 已提交
119
    if (ARG_UNPARSED_ARGUMENTS)
X
xj.lin 已提交
120
        message(SEND_ERROR "Error: unrecognized arguments: ${ARG_UNPARSED_ARGUMENTS}")
Y
youny626 已提交
121
    endif ()
X
xj.lin 已提交
122

Y
youny626 已提交
123 124
    if (ARG_STATIC_LIB AND ARG_SHARED_LIB)
        if (NOT ARG_STATIC_LIB)
X
xj.lin 已提交
125
            message(FATAL_ERROR "No static or shared library provided for ${LIB_NAME}")
Y
youny626 已提交
126
        endif ()
X
xj.lin 已提交
127 128 129 130 131

        set(AUG_LIB_NAME "${LIB_NAME}_static")
        add_library(${AUG_LIB_NAME} STATIC IMPORTED)
        set_target_properties(${AUG_LIB_NAME}
                PROPERTIES IMPORTED_LOCATION "${ARG_STATIC_LIB}")
Y
youny626 已提交
132
        if (ARG_DEPS)
X
xj.lin 已提交
133 134
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_LINK_LIBRARIES "${ARG_DEPS}")
Y
youny626 已提交
135
        endif ()
X
xj.lin 已提交
136
        message(STATUS "Added static library dependency ${AUG_LIB_NAME}: ${ARG_STATIC_LIB}")
Y
youny626 已提交
137
        if (ARG_INCLUDE_DIRECTORIES)
X
xj.lin 已提交
138 139 140
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                    "${ARG_INCLUDE_DIRECTORIES}")
Y
youny626 已提交
141
        endif ()
X
xj.lin 已提交
142 143 144 145

        set(AUG_LIB_NAME "${LIB_NAME}_shared")
        add_library(${AUG_LIB_NAME} SHARED IMPORTED)

Y
youny626 已提交
146
        if (WIN32)
X
xj.lin 已提交
147 148 149
            # Mark the ".lib" location as part of a Windows DLL
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES IMPORTED_IMPLIB "${ARG_SHARED_LIB}")
Y
youny626 已提交
150
        else ()
X
xj.lin 已提交
151 152
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES IMPORTED_LOCATION "${ARG_SHARED_LIB}")
Y
youny626 已提交
153 154
        endif ()
        if (ARG_DEPS)
X
xj.lin 已提交
155 156
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_LINK_LIBRARIES "${ARG_DEPS}")
Y
youny626 已提交
157
        endif ()
X
xj.lin 已提交
158
        message(STATUS "Added shared library dependency ${AUG_LIB_NAME}: ${ARG_SHARED_LIB}")
Y
youny626 已提交
159
        if (ARG_INCLUDE_DIRECTORIES)
X
xj.lin 已提交
160 161 162
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                    "${ARG_INCLUDE_DIRECTORIES}")
Y
youny626 已提交
163 164
        endif ()
    elseif (ARG_STATIC_LIB)
X
xj.lin 已提交
165 166 167 168
        set(AUG_LIB_NAME "${LIB_NAME}_static")
        add_library(${AUG_LIB_NAME} STATIC IMPORTED)
        set_target_properties(${AUG_LIB_NAME}
                PROPERTIES IMPORTED_LOCATION "${ARG_STATIC_LIB}")
Y
youny626 已提交
169
        if (ARG_DEPS)
X
xj.lin 已提交
170 171
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_LINK_LIBRARIES "${ARG_DEPS}")
Y
youny626 已提交
172
        endif ()
X
xj.lin 已提交
173
        message(STATUS "Added static library dependency ${AUG_LIB_NAME}: ${ARG_STATIC_LIB}")
Y
youny626 已提交
174
        if (ARG_INCLUDE_DIRECTORIES)
X
xj.lin 已提交
175 176 177
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                    "${ARG_INCLUDE_DIRECTORIES}")
Y
youny626 已提交
178 179
        endif ()
    elseif (ARG_SHARED_LIB)
X
xj.lin 已提交
180 181 182
        set(AUG_LIB_NAME "${LIB_NAME}_shared")
        add_library(${AUG_LIB_NAME} SHARED IMPORTED)

Y
youny626 已提交
183
        if (WIN32)
X
xj.lin 已提交
184 185 186
            # Mark the ".lib" location as part of a Windows DLL
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES IMPORTED_IMPLIB "${ARG_SHARED_LIB}")
Y
youny626 已提交
187
        else ()
X
xj.lin 已提交
188 189
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES IMPORTED_LOCATION "${ARG_SHARED_LIB}")
Y
youny626 已提交
190
        endif ()
X
xj.lin 已提交
191
        message(STATUS "Added shared library dependency ${AUG_LIB_NAME}: ${ARG_SHARED_LIB}")
Y
youny626 已提交
192
        if (ARG_DEPS)
X
xj.lin 已提交
193 194
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_LINK_LIBRARIES "${ARG_DEPS}")
Y
youny626 已提交
195 196
        endif ()
        if (ARG_INCLUDE_DIRECTORIES)
X
xj.lin 已提交
197 198 199
            set_target_properties(${AUG_LIB_NAME}
                    PROPERTIES INTERFACE_INCLUDE_DIRECTORIES
                    "${ARG_INCLUDE_DIRECTORIES}")
Y
youny626 已提交
200 201
        endif ()
    else ()
X
xj.lin 已提交
202
        message(FATAL_ERROR "No static or shared library provided for ${LIB_NAME}")
Y
youny626 已提交
203
    endif ()
X
xj.lin 已提交
204
endfunction()