inference_lib.cmake 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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
# make package for paddle inference shared and static library
16 17 18
set(PADDLE_INFERENCE_INSTALL_DIR
    "${CMAKE_BINARY_DIR}/paddle_inference_install_dir"
    CACHE STRING "A path setting paddle inference shared and static libraries")
19

20 21
# At present, the size of static lib in Windows is very large,
# so we need to crop the library size.
22
if(WIN32)
23 24 25 26 27 28
  #todo: remove the option
  option(WITH_STATIC_LIB
         "Compile demo with static/shared library, default use dynamic." OFF)
  if(NOT PYTHON_EXECUTABLE)
    find_package(PythonInterp REQUIRED)
  endif()
29 30
endif()

31
set(COPY_SCRIPT_DIR ${PADDLE_SOURCE_DIR}/cmake)
L
Luo Tao 已提交
32
function(copy TARGET)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  set(options "")
  set(oneValueArgs "")
  set(multiValueArgs SRCS DSTS)
  cmake_parse_arguments(copy_lib "${options}" "${oneValueArgs}"
                        "${multiValueArgs}" ${ARGN})

  list(LENGTH copy_lib_SRCS copy_lib_SRCS_len)
  list(LENGTH copy_lib_DSTS copy_lib_DSTS_len)
  if(NOT ${copy_lib_SRCS_len} EQUAL ${copy_lib_DSTS_len})
    message(
      FATAL_ERROR
        "${TARGET} source numbers are not equal to destination numbers")
  endif()
  math(EXPR len "${copy_lib_SRCS_len} - 1")
  foreach(index RANGE ${len})
    list(GET copy_lib_SRCS ${index} src)
    list(GET copy_lib_DSTS ${index} dst)
    if(WIN32) #windows
      file(TO_NATIVE_PATH ${src} native_src)
      file(TO_NATIVE_PATH ${dst} native_dst)
      add_custom_command(
        TARGET ${TARGET}
        POST_BUILD
        COMMAND ${PYTHON_EXECUTABLE} ${COPY_SCRIPT_DIR}/copyfile.py
                ${native_src} ${native_dst})
W
Wilber 已提交
58
    else() #not windows
59 60 61 62 63 64
      add_custom_command(
        TARGET ${TARGET}
        POST_BUILD
        COMMAND mkdir -p "${dst}"
        COMMAND cp -r "${src}" "${dst}"
        COMMENT "copying ${src} -> ${dst}")
W
Wilber 已提交
65
    endif() # not windows
66
  endforeach()
L
Luo Tao 已提交
67 68
endfunction()

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
function(copy_part_of_thrid_party TARGET DST)
  if(${CBLAS_PROVIDER} STREQUAL MKLML)
    set(dst_dir "${DST}/third_party/install/mklml")
    if(WIN32)
      copy(
        ${TARGET}
        SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_SHARED_LIB}
             ${MKLML_SHARED_IOMP_LIB} ${MKLML_INC_DIR}
        DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}/lib
             ${dst_dir})
    else()
      copy(
        ${TARGET}
        SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_INC_DIR}
        DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir})
      if(WITH_STRIP)
        add_custom_command(
          TARGET ${TARGET}
          POST_BUILD
          COMMAND strip -s ${dst_dir}/lib/libiomp5.so
          COMMAND strip -s ${dst_dir}/lib/libmklml_intel.so
          COMMENT "striping libiomp5.so\nstriping libmklml_intel.so")
      endif()
92
    endif()
93 94 95 96 97 98 99 100 101 102 103 104 105
  elseif(${CBLAS_PROVIDER} STREQUAL EXTERN_OPENBLAS)
    set(dst_dir "${DST}/third_party/install/openblas")
    if(WIN32)
      copy(
        ${TARGET}
        SRCS ${CBLAS_INSTALL_DIR}/lib ${OPENBLAS_SHARED_LIB}
             ${CBLAS_INSTALL_DIR}/include
        DSTS ${dst_dir} ${dst_dir}/lib ${dst_dir})
    else()
      copy(
        ${TARGET}
        SRCS ${CBLAS_INSTALL_DIR}/lib ${CBLAS_INSTALL_DIR}/include
        DSTS ${dst_dir} ${dst_dir})
106
    endif()
107 108 109 110 111 112 113 114

    if(WITH_SPARSELT)
      set(dst_dir "${DST}/third_party/install/cusparselt")
      copy(
        ${TARGET}
        SRCS ${CUSPARSELT_INC_DIR} ${CUSPARSELT_LIB_DIR}
        DSTS ${dst_dir} ${dst_dir})
    endif()
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  endif()

  if(WITH_MKLDNN)
    set(dst_dir "${DST}/third_party/install/mkldnn")
    if(WIN32)
      copy(
        ${TARGET}
        SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB} ${MKLDNN_LIB}
        DSTS ${dst_dir} ${dst_dir}/lib ${dst_dir}/lib)
    else()
      copy(
        ${TARGET}
        SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB}
        DSTS ${dst_dir} ${dst_dir}/lib)
      if(WITH_STRIP)
        add_custom_command(
          TARGET ${TARGET}
          POST_BUILD
          COMMAND strip -s ${dst_dir}/lib/libmkldnn.so.0
          COMMENT "striping libmkldnn.so.0")
      endif()
      add_custom_command(
        TARGET ${TARGET}
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E create_symlink libmkldnn.so.0
                ${dst_dir}/lib/libdnnl.so.1
        COMMAND ${CMAKE_COMMAND} -E create_symlink libmkldnn.so.0
                ${dst_dir}/lib/libdnnl.so.2
        COMMENT "Make a symbol link of libmkldnn.so.0")
144
    endif()
145 146 147 148 149 150 151 152 153 154
  endif()

  if(WITH_ONNXRUNTIME)
    set(dst_dir "${DST}/third_party/install/onnxruntime")
    copy(
      ${TARGET}
      SRCS ${ONNXRUNTIME_INC_DIR} ${ONNXRUNTIME_LIB_DIR}
      DSTS ${dst_dir} ${dst_dir})

    set(dst_dir "${DST}/third_party/install/paddle2onnx")
155 156 157 158
    copy(
      ${TARGET}
      SRCS ${PADDLE2ONNX_INC_DIR}/paddle2onnx ${PADDLE2ONNX_LIB_DIR}
      DSTS ${dst_dir}/include ${dst_dir})
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  endif()

  set(dst_dir "${DST}/third_party/install/gflags")
  copy(
    ${TARGET}
    SRCS ${GFLAGS_INCLUDE_DIR} ${GFLAGS_LIBRARIES}
    DSTS ${dst_dir} ${dst_dir}/lib)

  set(dst_dir "${DST}/third_party/install/glog")
  copy(
    ${TARGET}
    SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES}
    DSTS ${dst_dir} ${dst_dir}/lib)

  set(dst_dir "${DST}/third_party/install/utf8proc")
  copy(
    ${TARGET}
    SRCS ${UTF8PROC_INSTALL_DIR}/include ${UTF8PROC_LIBRARIES}
    DSTS ${dst_dir} ${dst_dir}/lib)

  if(WITH_CRYPTO)
    set(dst_dir "${DST}/third_party/install/cryptopp")
    copy(
      ${TARGET}
      SRCS ${CRYPTOPP_INCLUDE_DIR} ${CRYPTOPP_LIBRARIES}
      DSTS ${dst_dir} ${dst_dir}/lib)
  endif()

  set(dst_dir "${DST}/third_party/install/xxhash")
  copy(
    ${TARGET}
    SRCS ${XXHASH_INCLUDE_DIR} ${XXHASH_LIBRARIES}
    DSTS ${dst_dir} ${dst_dir}/lib)

  if(NOT PROTOBUF_FOUND OR WIN32)
    set(dst_dir "${DST}/third_party/install/protobuf")
    copy(
      ${TARGET}
      SRCS ${PROTOBUF_INCLUDE_DIR} ${PROTOBUF_LIBRARY}
      DSTS ${dst_dir} ${dst_dir}/lib)
  endif()

  if(LITE_BINARY_DIR)
    set(dst_dir "${DST}/third_party/install/lite")
    copy(
      ${TARGET}
      SRCS ${LITE_BINARY_DIR}/${LITE_OUTPUT_BIN_DIR}/*
      DSTS ${dst_dir})
  endif()
208 209
endfunction()

210
# inference library for only inference
211 212
set(inference_lib_deps third_party paddle_inference paddle_inference_c
                       paddle_inference_shared paddle_inference_c_shared)
213
add_custom_target(inference_lib_dist ALL DEPENDS ${inference_lib_deps})
214

215
set(dst_dir "${PADDLE_INFERENCE_INSTALL_DIR}/third_party/threadpool")
216 217 218 219
copy(
  inference_lib_dist
  SRCS ${THREADPOOL_INCLUDE_DIR}/ThreadPool.h
  DSTS ${dst_dir})
220

221
# GPU must copy externalErrorMsg.pb
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
if(WITH_GPU)
  set(dst_dir "${PADDLE_INFERENCE_INSTALL_DIR}/third_party/externalError/data")
  copy(
    inference_lib_dist
    SRCS ${externalError_INCLUDE_DIR}
    DSTS ${dst_dir})
endif()

if(WITH_XPU)
  set(dst_dir "${PADDLE_INFERENCE_INSTALL_DIR}/third_party/install/xpu")
  copy(
    inference_lib_dist
    SRCS ${XPU_INC_DIR} ${XPU_LIB_DIR}
    DSTS ${dst_dir} ${dst_dir})
endif()
237

238
# CMakeCache Info
239 240 241 242
copy(
  inference_lib_dist
  SRCS ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR})
243

244
copy_part_of_thrid_party(inference_lib_dist ${PADDLE_INFERENCE_INSTALL_DIR})
245

246
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
247

P
peizhilin 已提交
248
if(WIN32)
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  if(WITH_STATIC_LIB)
    set(paddle_inference_lib
        $<TARGET_FILE_DIR:paddle_inference>/libpaddle_inference.lib
        $<TARGET_FILE_DIR:paddle_inference>/paddle_inference.*)
  else()
    set(paddle_inference_lib
        $<TARGET_FILE_DIR:paddle_inference_shared>/paddle_inference.dll
        $<TARGET_FILE_DIR:paddle_inference_shared>/paddle_inference.lib)
  endif()
  copy(
    inference_lib_dist
    SRCS ${src_dir}/inference/api/paddle_*.h ${paddle_inference_lib}
    DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include
         ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/lib
         ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/lib)
W
Wilber 已提交
264
else()
265 266 267 268 269 270 271
  set(paddle_inference_lib
      ${PADDLE_BINARY_DIR}/paddle/fluid/inference/libpaddle_inference.*)
  copy(
    inference_lib_dist
    SRCS ${src_dir}/inference/api/paddle_*.h ${paddle_inference_lib}
    DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include
         ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/lib)
W
Wilber 已提交
272
endif()
273

274 275 276 277 278 279 280 281
copy(
  inference_lib_dist
  SRCS ${CMAKE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/internal)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/fluid/framework/io/crypto/cipher.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/crypto/)
M
MRXLT 已提交
282
include_directories(${CMAKE_BINARY_DIR}/../paddle/fluid/framework/io)
283

284
# copy api headers for phi & custom op
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/api/ext/*.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/api/ext/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/api/include/*.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/api/include/
)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/api/all.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/api/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/common/*.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/common/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/core/macros.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/core/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/core/visit_type.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/core/)
310 311 312 313
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/phi/core/hostdevice.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/core/)
Y
YuanRisheng 已提交
314 315 316 317
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/fluid/platform/init_phi.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/phi/)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/any.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/utils/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/optional.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/utils/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/none.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/utils/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/utils/flat_hash_map.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/utils/)
copy(
  inference_lib_dist
  SRCS ${PADDLE_SOURCE_DIR}/paddle/extension.h
  DSTS ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/include/experimental/)
338

339 340
# the header file of phi is copied to the experimental directory,
# the include path of phi needs to be changed to adapt to inference api path
341 342 343 344 345
add_custom_command(
  TARGET inference_lib_dist
  POST_BUILD
  COMMAND ${CMAKE_COMMAND} -P "${PADDLE_SOURCE_DIR}/cmake/phi_header.cmake"
  COMMENT "Change phi header include path to adapt to inference api path")
346

347
# CAPI inference library for only inference
348 349 350
set(PADDLE_INFERENCE_C_INSTALL_DIR
    "${CMAKE_BINARY_DIR}/paddle_inference_c_install_dir"
    CACHE STRING "A path setting CAPI paddle inference shared")
351
copy_part_of_thrid_party(inference_lib_dist ${PADDLE_INFERENCE_C_INSTALL_DIR})
352 353

set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
354
if(WIN32)
355 356
  set(paddle_inference_c_lib
      $<TARGET_FILE_DIR:paddle_inference_c>/paddle_inference_c.*)
W
Wilber 已提交
357
else()
358 359 360
  set(paddle_inference_c_lib
      ${PADDLE_BINARY_DIR}/paddle/fluid/inference/capi_exp/libpaddle_inference_c.*
  )
W
Wilber 已提交
361
endif()
362

363 364 365 366
if(WITH_INFERENCE_NVTX AND NOT WIN32)
  add_definitions(-DPADDLE_WITH_INFERENCE_NVTX)
endif()

367 368 369 370 371
copy(
  inference_lib_dist
  SRCS ${src_dir}/inference/capi_exp/pd_*.h ${paddle_inference_c_lib}
  DSTS ${PADDLE_INFERENCE_C_INSTALL_DIR}/paddle/include
       ${PADDLE_INFERENCE_C_INSTALL_DIR}/paddle/lib)
372

373
if(WITH_STRIP AND NOT WIN32)
374 375 376 377 378 379 380 381 382 383
  add_custom_command(
    TARGET inference_lib_dist
    POST_BUILD
    COMMAND
      strip -s
      ${PADDLE_INFERENCE_C_INSTALL_DIR}/paddle/lib/libpaddle_inference_c.so
    COMMAND strip -s
            ${PADDLE_INFERENCE_INSTALL_DIR}/paddle/lib/libpaddle_inference.so
    COMMENT "striping libpaddle_inference_c.so\nstriping libpaddle_inference.so"
  )
384 385
endif()

386 387
version(${PADDLE_INFERENCE_INSTALL_DIR}/version.txt)
version(${PADDLE_INFERENCE_C_INSTALL_DIR}/version.txt)