protobuf.cmake 13.1 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
# 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.

15
include(ExternalProject)
Y
Yu Yang 已提交
16
# Always invoke `FIND_PACKAGE(Protobuf)` for importing function protobuf_generate_cpp
17 18
if(NOT WIN32)
  find_package(Protobuf QUIET)
W
Wilber 已提交
19
endif()
D
dzhwinter 已提交
20

21 22 23 24 25 26 27 28
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 已提交
29
function(protobuf_generate_python SRCS)
30 31 32 33 34 35 36
  # 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()
Y
Yu Yang 已提交
37

38 39
  if(PROTOBUF_GENERATE_CPP_APPEND_PATH)
    # Create an include path for each file specified
Y
Yu Yang 已提交
40
    foreach(FIL ${ARGN})
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
      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})
      endif()
    endforeach()
  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()
Y
Yu Yang 已提交
62
    endforeach()
63
  endif()
Y
Yu Yang 已提交
64

65 66 67 68 69 70 71 72 73 74 75 76 77
  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"
R
risemeup1 已提交
78 79
      COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --python_out ${PADDLE_BINARY_DIR}
              -I${PADDLE_SOURCE_DIR} ${ABS_FIL}
80 81 82 83 84 85 86 87
      DEPENDS ${ABS_FIL} ${PROTOBUF_PROTOC_EXECUTABLE}
      COMMENT "Running Python protocol buffer compiler on ${FIL}"
      VERBATIM)
  endforeach()

  set(${SRCS}
      ${${SRCS}}
      PARENT_SCOPE)
Y
Yu Yang 已提交
88
endfunction()
L
liaogang 已提交
89

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

95 96 97 98 99 100
  message(STATUS "Protobuf protoc executable: ${PROTOBUF_PROTOC_EXECUTABLE}")
  message(STATUS "Protobuf-lite library: ${PROTOBUF_LITE_LIBRARY}")
  message(STATUS "Protobuf library: ${PROTOBUF_LIBRARY}")
  message(STATUS "Protoc library: ${PROTOBUF_PROTOC_LIBRARY}")
  message(STATUS "Protobuf version: ${PROTOBUF_VERSION}")
  include_directories(${PROTOBUF_INCLUDE_DIR})
101

102 103 104 105 106 107 108 109
  # Assuming that all the protobuf libraries are of the same type.
  if(${PROTOBUF_LIBRARY} MATCHES ${CMAKE_STATIC_LIBRARY_SUFFIX})
    set(protobuf_LIBTYPE STATIC)
  elseif(${PROTOBUF_LIBRARY} MATCHES "${CMAKE_SHARED_LIBRARY_SUFFIX}$")
    set(protobuf_LIBTYPE SHARED)
  else()
    message(FATAL_ERROR "Unknown library type: ${PROTOBUF_LIBRARY}")
  endif()
110

111 112
  add_library(protobuf ${protobuf_LIBTYPE} IMPORTED GLOBAL)
  set_property(TARGET protobuf PROPERTY IMPORTED_LOCATION ${PROTOBUF_LIBRARY})
113

114 115 116
  add_library(protobuf_lite ${protobuf_LIBTYPE} IMPORTED GLOBAL)
  set_property(TARGET protobuf_lite PROPERTY IMPORTED_LOCATION
                                             ${PROTOBUF_LITE_LIBRARY})
117

118 119
  add_library(libprotoc ${protobuf_LIBTYPE} IMPORTED GLOBAL)
  set_property(TARGET libprotoc PROPERTY IMPORTED_LOCATION ${PROTOC_LIBRARY})
Y
Yu Yang 已提交
120

121 122 123 124 125 126
  add_executable(protoc IMPORTED GLOBAL)
  set_property(TARGET protoc PROPERTY IMPORTED_LOCATION
                                      ${PROTOBUF_PROTOC_EXECUTABLE})
  # FIND_Protobuf.cmake uses `Protobuf_PROTOC_EXECUTABLE`.
  # make `protobuf_generate_cpp` happy.
  set(Protobuf_PROTOC_EXECUTABLE ${PROTOBUF_PROTOC_EXECUTABLE})
Y
Yu Yang 已提交
127

128 129 130 131 132 133
  foreach(dep ${protobuf_DEPS})
    add_dependencies(protobuf ${dep})
    add_dependencies(protobuf_lite ${dep})
    add_dependencies(libprotoc ${dep})
    add_dependencies(protoc ${dep})
  endforeach()
134

135
  return()
Y
Yu Yang 已提交
136
endmacro()
137
macro(SET_PROTOBUF_VERSION)
138 139 140 141 142
  exec_program(
    ${PROTOBUF_PROTOC_EXECUTABLE} ARGS
    --version
    OUTPUT_VARIABLE PROTOBUF_VERSION)
  string(REGEX MATCH "[0-9]+.[0-9]+" PROTOBUF_VERSION "${PROTOBUF_VERSION}")
143
endmacro()
Y
Yu Yang 已提交
144

145 146 147 148 149
set(PROTOBUF_ROOT
    ""
    CACHE PATH "Folder contains protobuf")
if(WIN32)
  set(PROTOBUF_ROOT ${THIRD_PARTY_PATH}/install/protobuf)
W
Wilber 已提交
150
endif()
D
dzhwinter 已提交
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
if(NOT "${PROTOBUF_ROOT}" STREQUAL "")
  find_path(
    PROTOBUF_INCLUDE_DIR google/protobuf/message.h
    PATHS ${PROTOBUF_ROOT}/include
    NO_DEFAULT_PATH)
  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)
  find_program(
    PROTOBUF_PROTOC_EXECUTABLE protoc
    PATHS ${PROTOBUF_ROOT}/bin
    NO_DEFAULT_PATH)
  if(PROTOBUF_INCLUDE_DIR
     AND PROTOBUF_LIBRARY
     AND PROTOBUF_LITE_LIBRARY
     AND PROTOBUF_PROTOC_LIBRARY
     AND PROTOBUF_PROTOC_EXECUTABLE)
    set(PROTOBUF_FOUND true)
    message(STATUS "Using custom protobuf library in ${PROTOBUF_ROOT}.")
    set_protobuf_version()
    prompt_protobuf_lib()
  endif()
Y
Yu Yang 已提交
183 184
endif()

185 186 187 188 189 190
function(build_protobuf TARGET_NAME BUILD_FOR_HOST)
  string(REPLACE "extern_" "" TARGET_DIR_NAME "${TARGET_NAME}")
  set(PROTOBUF_PREFIX_DIR ${THIRD_PARTY_PATH}/${TARGET_DIR_NAME})
  set(PROTOBUF_SOURCE_DIR
      ${THIRD_PARTY_PATH}/${TARGET_DIR_NAME}/src/${TARGET_NAME})
  set(PROTOBUF_INSTALL_DIR ${THIRD_PARTY_PATH}/install/${TARGET_DIR_NAME})
191

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  set(${TARGET_NAME}_INCLUDE_DIR
      "${PROTOBUF_INSTALL_DIR}/include"
      PARENT_SCOPE)
  set(PROTOBUF_INCLUDE_DIR
      "${PROTOBUF_INSTALL_DIR}/include"
      PARENT_SCOPE)
  set(${TARGET_NAME}_LITE_LIBRARY
      "${PROTOBUF_INSTALL_DIR}/lib/libprotobuf-lite${CMAKE_STATIC_LIBRARY_SUFFIX}"
      PARENT_SCOPE)
  set(${TARGET_NAME}_LIBRARY
      "${PROTOBUF_INSTALL_DIR}/lib/libprotobuf${CMAKE_STATIC_LIBRARY_SUFFIX}"
      PARENT_SCOPE)
  set(${TARGET_NAME}_PROTOC_LIBRARY
      "${PROTOBUF_INSTALL_DIR}/lib/libprotoc${CMAKE_STATIC_LIBRARY_SUFFIX}"
      PARENT_SCOPE)
  set(${TARGET_NAME}_PROTOC_EXECUTABLE
      "${PROTOBUF_INSTALL_DIR}/bin/protoc${CMAKE_EXECUTABLE_SUFFIX}"
      PARENT_SCOPE)
L
liaogang 已提交
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  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_C_FLAGS=${CMAKE_C_FLAGS}"
        "-DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG}"
        "-DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE}"
        "-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
        "-DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE}"
        "-DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG}"
        "-Dprotobuf_WITH_ZLIB=ON"
        "-DZLIB_ROOT:FILEPATH=${ZLIB_ROOT}"
        ${EXTERNAL_OPTIONAL_ARGS})
    set(OPTIONAL_CACHE_ARGS "-DZLIB_ROOT:STRING=${ZLIB_ROOT}")
  endif()
  if(WIN32)
    set(OPTIONAL_ARGS
        ${OPTIONAL_ARGS} "-DCMAKE_GENERATOR=${CMAKE_GENERATOR}"
        "-DCMAKE_GENERATOR_PLATFORM=${CMAKE_GENERATOR_PLATFORM}"
        "-Dprotobuf_MSVC_STATIC_RUNTIME=${MSVC_STATIC_CRT}")
  endif()
236

237
  if(WITH_ASCEND AND NOT WITH_ASCEND_CXX11)
238
    set(PROTOBUF_REPOSITORY https://gitee.com/tianjianhe/protobuf.git)
R
risemeup1 已提交
239
    set(PROTOBUF_TAG v21.12)
240 241
  elseif(WITH_ASCEND_CL AND NOT WITH_ASCEND_CXX11)
    set(PROTOBUF_REPOSITORY https://gitee.com/tianjianhe/protobuf.git)
R
risemeup1 已提交
242
    set(PROTOBUF_TAG v21.12)
243 244
  elseif(WITH_IPU)
    set(PROTOBUF_REPOSITORY ${GIT_URL}/protocolbuffers/protobuf.git)
R
risemeup1 已提交
245
    set(PROTOBUF_TAG v21.12)
246 247 248 249 250 251
  elseif(WIN32)
    set(PROTOBUF_REPOSITORY ${GIT_URL}/protocolbuffers/protobuf.git)
    # Change the tag to support building with vs2019
    set(PROTOBUF_TAG 01a05a53f40ca2ac5f0af10c6cc0810bee39b792)
  else()
    set(PROTOBUF_REPOSITORY ${GIT_URL}/protocolbuffers/protobuf.git)
R
risemeup1 已提交
252
    set(PROTOBUF_TAG v21.12)
253 254 255
    if(WITH_GPU)
      if(${CMAKE_CUDA_COMPILER_VERSION} LESS 12.0
         AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 12.0)
R
risemeup1 已提交
256
        set(PROTOBUF_TAG v21.12)
257 258
      endif()
    endif()
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  endif()
  if(WITH_ARM_BRPC)
    set(ARM_PROTOBUF_URL
        "https://paddlerec.bj.bcebos.com/online_infer/arm_brpc_ubuntu18/arm_protobuf.tar.gz"
        CACHE STRING "" FORCE)
    file(
      WRITE ${PROTOBUF_SOURCE_DIR}/CMakeLists.txt
      "PROJECT(ARM_PROTOBUF)\n"
      "cmake_minimum_required(VERSION 3.0)\n"
      "install(DIRECTORY arm_protobuf/bin  arm_protobuf/include arm_protobuf/lib \n"
      "        DESTINATION . USE_SOURCE_PERMISSIONS)\n")
    ExternalProject_Add(
      ${TARGET_NAME}
      ${EXTERNAL_PROJECT_LOG_ARGS} ${SHALLOW_CLONE}
      PREFIX ${PROTOBUF_PREFIX_DIR}
      DOWNLOAD_DIR ${PROTOBUF_SOURCE_DIR}
      DOWNLOAD_COMMAND rm -rf arm_protobuf.tar.gz && wget --no-check-certificate
                       ${ARM_PROTOBUF_URL} && tar zxvf arm_protobuf.tar.gz
      UPDATE_COMMAND ""
      CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${PROTOBUF_INSTALL_DIR}
                 -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
      CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${PROTOBUF_INSTALL_DIR}
                       -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/lib/libprotobuf${CMAKE_STATIC_LIBRARY_SUFFIX}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/lib/libprotobuf-lite${CMAKE_STATIC_LIBRARY_SUFFIX}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/lib/libprotoc${CMAKE_STATIC_LIBRARY_SUFFIX}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/bin/protoc${CMAKE_EXECUTABLE_SUFFIX})
  else()
    ExternalProject_Add(
      ${TARGET_NAME}
      ${EXTERNAL_PROJECT_LOG_ARGS} ${SHALLOW_CLONE}
      GIT_REPOSITORY ${PROTOBUF_REPOSITORY}
      GIT_TAG ${PROTOBUF_TAG}
      PREFIX ${PROTOBUF_PREFIX_DIR}
      UPDATE_COMMAND ""
      DEPENDS zlib
      CONFIGURE_COMMAND
        ${CMAKE_COMMAND} ${PROTOBUF_SOURCE_DIR}/cmake ${OPTIONAL_ARGS}
301
        -G${CMAKE_GENERATOR} -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_SKIP_RPATH=ON
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        -DCMAKE_POSITION_INDEPENDENT_CODE=ON
        -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE}
        -DCMAKE_INSTALL_PREFIX=${PROTOBUF_INSTALL_DIR}
        -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_SHARED_LIBS=OFF
      CMAKE_CACHE_ARGS
        -DCMAKE_INSTALL_PREFIX:PATH=${PROTOBUF_INSTALL_DIR}
        -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE}
        -DCMAKE_VERBOSE_MAKEFILE:BOOL=OFF
        -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
        ${OPTIONAL_CACHE_ARGS}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/lib/libprotobuf${CMAKE_STATIC_LIBRARY_SUFFIX}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/lib/libprotobuf-lite${CMAKE_STATIC_LIBRARY_SUFFIX}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/lib/libprotoc${CMAKE_STATIC_LIBRARY_SUFFIX}
      BUILD_BYPRODUCTS
        ${PROTOBUF_INSTALL_DIR}/bin/protoc${CMAKE_EXECUTABLE_SUFFIX})
  endif()
endfunction()
322

323
if(WITH_ASCEND OR WITH_ASCEND_CL)
R
risemeup1 已提交
324
  set(PROTOBUF_VERSION 21.12)
J
jianghaicheng 已提交
325
elseif(WITH_IPU)
R
risemeup1 已提交
326
  set(PROTOBUF_VERSION 21.12)
327
elseif(WITH_ARM_BRPC)
R
risemeup1 已提交
328
  set(PROTOBUF_VERSION 21.12-baidu-ee-common)
329
else()
R
risemeup1 已提交
330
  set(PROTOBUF_VERSION 21.12)
331 332 333
  if(WITH_GPU)
    if(${CMAKE_CUDA_COMPILER_VERSION} LESS 12.0
       AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 12.0)
R
risemeup1 已提交
334
      set(PROTOBUF_VERSION 21.12)
335 336
    endif()
  endif()
337
endif()
D
dzhwinter 已提交
338

339 340
if(NOT PROTOBUF_FOUND)
  build_protobuf(extern_protobuf FALSE)
341

342 343 344 345 346 347 348 349 350 351 352 353
  set(PROTOBUF_INCLUDE_DIR
      ${extern_protobuf_INCLUDE_DIR}
      CACHE PATH "protobuf include directory." FORCE)
  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)
354

355 356 357 358 359 360 361
  set(PROTOBUF_PROTOC_EXECUTABLE
      ${extern_protobuf_PROTOC_EXECUTABLE}
      CACHE FILEPATH "protobuf executable." FORCE)
  # `EXTERN_PROTOBUF_DEPEND` used in cmake function `proto_library` to ensure
  # `protoc.exe` existed before calling it.
  set(EXTERN_PROTOBUF_DEPEND extern_protobuf)
  prompt_protobuf_lib(extern_protobuf)
W
Wilber 已提交
362
endif()