protobuf.cmake 11.3 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2
#
L
liaogang 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
6
#
L
liaogang 已提交
7
# http://www.apache.org/licenses/LICENSE-2.0
8
#
L
liaogang 已提交
9 10 11 12 13 14 15
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

INCLUDE(ExternalProject)
Y
Yu Yang 已提交
16
# Always invoke `FIND_PACKAGE(Protobuf)` for importing function protobuf_generate_cpp
D
dzhwinter 已提交
17
IF(NOT WIN32)
Y
Yu Yang 已提交
18
FIND_PACKAGE(Protobuf QUIET)
D
dzhwinter 已提交
19
ENDIF(NOT WIN32)
Y
Yu Yang 已提交
20 21 22 23
macro(UNSET_VAR VAR_NAME)
    UNSET(${VAR_NAME} CACHE)
    UNSET(${VAR_NAME})
endmacro()
D
dzhwinter 已提交
24

Y
Yu Yang 已提交
25 26 27 28 29 30 31 32
UNSET_VAR(PROTOBUF_INCLUDE_DIR)
UNSET_VAR(PROTOBUF_FOUND)
UNSET_VAR(PROTOBUF_PROTOC_EXECUTABLE)
UNSET_VAR(PROTOBUF_PROTOC_LIBRARY)
UNSET_VAR(PROTOBUF_LITE_LIBRARY)
UNSET_VAR(PROTOBUF_LIBRARY)
UNSET_VAR(PROTOBUF_INCLUDE_DIR)
UNSET_VAR(Protobuf_PROTOC_EXECUTABLE)
Y
Yu Yang 已提交
33 34 35 36 37 38 39 40 41
function(protobuf_generate_python SRCS)
    # shameless copy from https://github.com/Kitware/CMake/blob/master/Modules/FindProtobuf.cmake
    if(NOT ARGN)
        message(SEND_ERROR "Error: PROTOBUF_GENERATE_PYTHON() called without any proto files")
        return()
    endif()

    if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
        # Create an include path for each file specified
42 43
        foreach(FIL ${ARGN})
            get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
Y
Yu Yang 已提交
44 45 46 47
            get_filename_component(ABS_PATH ${ABS_FIL} PATH)
            list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
            if(${_contains_already} EQUAL -1)
                list(APPEND _protobuf_include_path -I ${ABS_PATH})
48 49
            endif()
        endforeach()
Y
Yu Yang 已提交
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 78 79 80 81 82 83 84 85 86 87
    else()
        set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR})
    endif()
    if(DEFINED PROTOBUF_IMPORT_DIRS AND NOT DEFINED Protobuf_IMPORT_DIRS)
        set(Protobuf_IMPORT_DIRS "${PROTOBUF_IMPORT_DIRS}")
    endif()

    if(DEFINED Protobuf_IMPORT_DIRS)
        foreach(DIR ${Protobuf_IMPORT_DIRS})
            get_filename_component(ABS_PATH ${DIR} ABSOLUTE)
            list(FIND _protobuf_include_path ${ABS_PATH} _contains_already)
            if(${_contains_already} EQUAL -1)
                list(APPEND _protobuf_include_path -I ${ABS_PATH})
            endif()
        endforeach()
    endif()

    set(${SRCS})
    foreach(FIL ${ARGN})
        get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
        get_filename_component(FIL_WE ${FIL} NAME_WE)
        if(NOT PROTOBUF_GENERATE_CPP_APPEND_PATH)
            get_filename_component(FIL_DIR ${FIL} DIRECTORY)
            if(FIL_DIR)
                set(FIL_WE "${FIL_DIR}/${FIL_WE}")
            endif()
        endif()
        list(APPEND ${SRCS} "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py")
        add_custom_command(
                OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}_pb2.py"
                COMMAND  ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${CMAKE_CURRENT_BINARY_DIR} ${_protobuf_include_path} ${ABS_FIL}
                DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
                COMMENT "Running Python protocol buffer compiler on ${FIL}"
                VERBATIM )
    endforeach()

    set(${SRCS} ${${SRCS}} PARENT_SCOPE)
endfunction()
L
liaogang 已提交
88

89 90
# Print and set the protobuf library information,
# finish this cmake process and exit from this file.
Y
Yu Yang 已提交
91
macro(PROMPT_PROTOBUF_LIB)
92 93
    SET(protobuf_DEPS ${ARGN})

Y
Yu Yang 已提交
94
    MESSAGE(STATUS "Protobuf protoc executable: ${PROTOBUF_PROTOC_EXECUTABLE}")
D
dzhwinter 已提交
95
    MESSAGE(STATUS "Protobuf-lite library: ${PROTOBUF_LITE_LIBRARY}")
Y
Yu Yang 已提交
96
    MESSAGE(STATUS "Protobuf library: ${PROTOBUF_LIBRARY}")
D
dzhwinter 已提交
97
    MESSAGE(STATUS "Protoc library: ${PROTOBUF_PROTOC_LIBRARY}")
98
    MESSAGE(STATUS "Protobuf version: ${PROTOBUF_VERSION}")
Y
Yu Yang 已提交
99
    INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
100 101

    # Assuming that all the protobuf libraries are of the same type.
D
dzhwinter 已提交
102
    IF(${PROTOBUF_LIBRARY} MATCHES ${CMAKE_STATIC_LIBRARY_SUFFIX})
103
        SET(protobuf_LIBTYPE STATIC)
104
    ELSEIF(${PROTOBUF_LIBRARY} MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}$")
105 106 107 108 109 110 111 112 113 114 115
        SET(protobuf_LIBTYPE SHARED)
    ELSE()
        MESSAGE(FATAL_ERROR "Unknown library type: ${PROTOBUF_LIBRARY}")
    ENDIF()

    ADD_LIBRARY(protobuf ${protobuf_LIBTYPE} IMPORTED GLOBAL)
    SET_PROPERTY(TARGET protobuf PROPERTY IMPORTED_LOCATION ${PROTOBUF_LIBRARY})

    ADD_LIBRARY(protobuf_lite ${protobuf_LIBTYPE} IMPORTED GLOBAL)
    SET_PROPERTY(TARGET protobuf_lite PROPERTY IMPORTED_LOCATION ${PROTOBUF_LITE_LIBRARY})

Y
Yu Yang 已提交
116 117 118 119 120
    ADD_LIBRARY(libprotoc ${protobuf_LIBTYPE} IMPORTED GLOBAL)
    SET_PROPERTY(TARGET libprotoc PROPERTY IMPORTED_LOCATION ${PROTOC_LIBRARY})

    ADD_EXECUTABLE(protoc IMPORTED GLOBAL)
    SET_PROPERTY(TARGET protoc PROPERTY IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE})
Y
Yu Yang 已提交
121 122 123
    # FIND_Protobuf.cmake uses `Protobuf_PROTOC_EXECUTABLE`.
    # make `protobuf_generate_cpp` happy.
    SET(Protobuf_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
Y
Yu Yang 已提交
124

125 126 127
    FOREACH(dep ${protobuf_DEPS})
        ADD_DEPENDENCIES(protobuf ${dep})
        ADD_DEPENDENCIES(protobuf_lite ${dep})
Y
Yu Yang 已提交
128
        ADD_DEPENDENCIES(libprotoc ${dep})
129 130 131 132
        ADD_DEPENDENCIES(protoc ${dep})
    ENDFOREACH()

    LIST(APPEND external_project_dependencies protobuf)
Y
Yu Yang 已提交
133 134
    RETURN()
endmacro()
135 136 137 138
macro(SET_PROTOBUF_VERSION)
    EXEC_PROGRAM(${PROTOBUF_PROTOC_EXECUTABLE} ARGS --version OUTPUT_VARIABLE PROTOBUF_VERSION)
    STRING(REGEX MATCH "[0-9]+.[0-9]+" PROTOBUF_VERSION "${PROTOBUF_VERSION}")
endmacro()
Y
Yu Yang 已提交
139 140

set(PROTOBUF_ROOT "" CACHE PATH "Folder contains protobuf")
D
dzhwinter 已提交
141 142 143 144 145
IF (WIN32)
    SET(PROTOBUF_ROOT ${THIRD_PARTY_PATH}/install/protobuf)
    MESSAGE(WARNING, "In windows, protobuf only support msvc build, please build it manually and put it at " ${PROTOBUF_ROOT})
ENDIF(WIN32)

Y
Yu Yang 已提交
146
if (NOT "${PROTOBUF_ROOT}" STREQUAL "")
D
dzhwinter 已提交
147

Y
Yu Yang 已提交
148
    find_path(PROTOBUF_INCLUDE_DIR google/protobuf/message.h PATHS ${PROTOBUF_ROOT}/include NO_DEFAULT_PATH)
D
dzhwinter 已提交
149 150 151
    find_library(PROTOBUF_LIBRARY protobuf libprotobuf.lib PATHS ${PROTOBUF_ROOT}/lib NO_DEFAULT_PATH)
    find_library(PROTOBUF_LITE_LIBRARY protobuf-lite libprotobuf-lite.lib PATHS ${PROTOBUF_ROOT}/lib NO_DEFAULT_PATH)
    find_library(PROTOBUF_PROTOC_LIBRARY protoc libprotoc.lib PATHS ${PROTOBUF_ROOT}/lib NO_DEFAULT_PATH)
Y
Yu Yang 已提交
152
    find_program(PROTOBUF_PROTOC_EXECUTABLE protoc PATHS ${PROTOBUF_ROOT}/bin NO_DEFAULT_PATH)
Y
Yu Yang 已提交
153
    if (PROTOBUF_INCLUDE_DIR AND PROTOBUF_LIBRARY AND PROTOBUF_LITE_LIBRARY AND PROTOBUF_PROTOC_LIBRARY AND PROTOBUF_PROTOC_EXECUTABLE)
Y
Yu Yang 已提交
154
        message(STATUS "Using custom protobuf library in ${PROTOBUF_ROOT}.")
D
dzhwinter 已提交
155
        SET(PROTOBUF_FOUND true)
156
        SET_PROTOBUF_VERSION()
Y
Yu Yang 已提交
157 158
        PROMPT_PROTOBUF_LIB()
    else()
D
dzhwinter 已提交
159
        message(WARNING "Cannot find protobuf library in ${PROTOBUF_ROOT}")
Y
Yu Yang 已提交
160 161 162
    endif()
endif()

163
FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST)
164 165 166
    STRING(REPLACE "extern_" "" TARGET_DIR_NAME "${TARGET_NAME}")
    SET(PROTOBUF_SOURCES_DIR ${THIRD_PARTY_PATH}/${TARGET_DIR_NAME})
    SET(PROTOBUF_INSTALL_DIR ${THIRD_PARTY_PATH}/install/${TARGET_DIR_NAME})
L
liaogang 已提交
167

168
    SET(${TARGET_NAME}_INCLUDE_DIR "${PROTOBUF_INSTALL_DIR}/include" PARENT_SCOPE)
169
    SET(PROTOBUF_INCLUDE_DIR "${PROTOBUF_INSTALL_DIR}/include" PARENT_SCOPE)
170
    SET(${TARGET_NAME}_LITE_LIBRARY
171
        "${PROTOBUF_INSTALL_DIR}/lib/libprotobuf-lite${CMAKE_STATIC_LIBRARY_SUFFIX}"
172 173
         PARENT_SCOPE)
    SET(${TARGET_NAME}_LIBRARY
174
        "${PROTOBUF_INSTALL_DIR}/lib/libprotobuf${CMAKE_STATIC_LIBRARY_SUFFIX}"
175 176
         PARENT_SCOPE)
    SET(${TARGET_NAME}_PROTOC_LIBRARY
177
        "${PROTOBUF_INSTALL_DIR}/lib/libprotoc${CMAKE_STATIC_LIBRARY_SUFFIX}"
178 179
         PARENT_SCOPE)
    SET(${TARGET_NAME}_PROTOC_EXECUTABLE
180
        "${PROTOBUF_INSTALL_DIR}/bin/protoc${CMAKE_EXECUTABLE_SUFFIX}"
181
         PARENT_SCOPE)
182

183 184 185 186 187 188 189 190 191 192 193
    SET(OPTIONAL_CACHE_ARGS "")
    SET(OPTIONAL_ARGS "")
    IF(BUILD_FOR_HOST)
        SET(OPTIONAL_ARGS "-Dprotobuf_WITH_ZLIB=OFF")
    ELSE()
        SET(OPTIONAL_ARGS
            "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
            "-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
            "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
            "-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}"
            "-Dprotobuf_WITH_ZLIB=ON"
194 195
            "-DZLIB_ROOT:FILEPATH=${ZLIB_ROOT}"
            ${EXTERNAL_OPTIONAL_ARGS})
196 197
        SET(OPTIONAL_CACHE_ARGS "-DZLIB_ROOT:STRING=${ZLIB_ROOT}")
    ENDIF()
L
liaogang 已提交
198

199 200 201
    SET(PROTOBUF_REPO "https://github.com/google/protobuf.git")
    SET(PROTOBUF_TAG "9f75c5aa851cd877fb0d93ccc31b8567a6706546")
    IF(MOBILE_INFERENCE)
D
dangqingqing 已提交
202 203
        # The reason why the official version is not used is described in
        # https://github.com/PaddlePaddle/Paddle/issues/6114
204 205 206 207 208 209 210
        SET(PROTOBUF_REPO "https://github.com/qingqing01/protobuf.git")
        SET(PROTOBUF_TAG "v3.2.0")
        IF(NOT BUILD_FOR_HOST)
            SET(OPTIONAL_ARGS ${OPTIONAL_ARGS} "-Dprotobuf_BUILD_PROTOC_BINARIES=OFF")
        ENDIF()
    ENDIF()

211
    ExternalProject_Add(
212
        ${TARGET_NAME}
213 214 215 216
        ${EXTERNAL_PROJECT_LOG_ARGS}
        PREFIX          ${PROTOBUF_SOURCES_DIR}
        UPDATE_COMMAND  ""
        DEPENDS         zlib
217 218
        GIT_REPOSITORY  ${PROTOBUF_REPO}
        GIT_TAG         ${PROTOBUF_TAG}
219
        CONFIGURE_COMMAND
220 221
        ${CMAKE_COMMAND} ${PROTOBUF_SOURCES_DIR}/src/${TARGET_NAME}/cmake
            ${OPTIONAL_ARGS}
L
liaogang 已提交
222
            -Dprotobuf_BUILD_TESTS=OFF
223
            -DCMAKE_SKIP_RPATH=ON
L
liaogang 已提交
224
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON
225
            -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
L
liaogang 已提交
226 227 228
            -DCMAKE_INSTALL_PREFIX=${PROTOBUF_INSTALL_DIR}
            -DCMAKE_INSTALL_LIBDIR=lib
        CMAKE_CACHE_ARGS
L
liaogang 已提交
229
            -DCMAKE_INSTALL_PREFIX:PATH=${PROTOBUF_INSTALL_DIR}
230
            -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
L
liaogang 已提交
231 232
            -DCMAKE_VERBOSE_MAKEFILE:BOOL=OFF
            -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
233
            ${OPTIONAL_CACHE_ARGS}
234
    )
235 236
ENDFUNCTION()

237 238 239 240 241
IF(NOT MOBILE_INFERENCE)
    SET(PROTOBUF_VERSION 3.1)
ELSE()
    SET(PROTOBUF_VERSION 3.2)
ENDIF()
242
IF(CMAKE_CROSSCOMPILING)
243
    build_protobuf(protobuf_host TRUE)
244 245
    LIST(APPEND external_project_dependencies protobuf_host)

246 247 248 249
    SET(PROTOBUF_PROTOC_EXECUTABLE ${protobuf_host_PROTOC_EXECUTABLE}
        CACHE FILEPATH "protobuf executable." FORCE)
ENDIF()

D
dzhwinter 已提交
250

251
IF(NOT PROTOBUF_FOUND)
252
    build_protobuf(extern_protobuf FALSE)
253

254
    SET(PROTOBUF_INCLUDE_DIR ${extern_protobuf_INCLUDE_DIR}
255
        CACHE PATH "protobuf include directory." FORCE)
256 257 258 259 260 261 262
    SET(PROTOBUF_LITE_LIBRARY ${extern_protobuf_LITE_LIBRARY}
        CACHE FILEPATH "protobuf lite library." FORCE)
    SET(PROTOBUF_LIBRARY ${extern_protobuf_LIBRARY}
        CACHE FILEPATH "protobuf library." FORCE)
    SET(PROTOBUF_PROTOC_LIBRARY ${extern_protobuf_PROTOC_LIBRARY}
        CACHE FILEPATH "protoc library." FORCE)

L
Luo Tao 已提交
263
    IF(WITH_C_API)
264 265
        INSTALL(DIRECTORY ${PROTOBUF_INCLUDE_DIR} DESTINATION third_party/protobuf)
        IF(ANDROID)
266
            INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib/${ANDROID_ABI})
267
        ELSE()
268
            INSTALL(FILES ${PROTOBUF_LITE_LIBRARY} DESTINATION third_party/protobuf/lib)
269 270 271
        ENDIF()
    ENDIF()

272 273 274 275
    IF(CMAKE_CROSSCOMPILING)
        PROMPT_PROTOBUF_LIB(protobuf_host extern_protobuf)
    ELSE()
        SET(PROTOBUF_PROTOC_EXECUTABLE ${extern_protobuf_PROTOC_EXECUTABLE}
276
            CACHE FILEPATH "protobuf executable." FORCE)
277
        PROMPT_PROTOBUF_LIB(extern_protobuf)
278
    ENDIF()
279
ENDIF(NOT PROTOBUF_FOUND)