inference_lib.cmake 8.0 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.

L
Luo Tao 已提交
15
# make package for paddle fluid shared and static library
L
Luo Tao 已提交
16
function(copy TARGET)
Y
Yan Chunwei 已提交
17 18 19
    if (NOT ON_INFER)
      message(WARNING "Turn on the ON_INFER flag when building inference_lib only.")
    endif()
L
Luo Tao 已提交
20 21 22 23
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DSTS DEPS)
    cmake_parse_arguments(copy_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
24
    set(fluid_lib_dist_dep ${TARGET} ${fluid_lib_dist_dep} PARENT_SCOPE)
L
Luo Tao 已提交
25 26 27 28 29 30 31

    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")
32

L
Luo Tao 已提交
33 34 35 36
    add_custom_target(${TARGET} DEPENDS ${copy_lib_DEPS})
    foreach(index RANGE ${len})
        list(GET copy_lib_SRCS ${index} src)
        list(GET copy_lib_DSTS ${index} dst)
37
        add_custom_command(TARGET ${TARGET} PRE_BUILD
38 39 40
          COMMAND mkdir -p "${dst}"
          COMMAND cp -r "${src}" "${dst}"
          COMMENT "copying ${src} -> ${dst}")
L
Luo Tao 已提交
41 42 43
    endforeach()
endfunction()

L
Luo Tao 已提交
44
# third party
45
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/eigen3")
L
Luo Tao 已提交
46 47 48
copy(eigen3_lib
  SRCS ${EIGEN_INCLUDE_DIR}/Eigen/Core ${EIGEN_INCLUDE_DIR}/Eigen/src ${EIGEN_INCLUDE_DIR}/unsupported/Eigen
  DSTS ${dst_dir}/Eigen ${dst_dir}/Eigen ${dst_dir}/unsupported
T
tensor-tang 已提交
49
  DEPS eigen3
L
Luo Tao 已提交
50 51
)

52
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/gflags")
L
Luo Tao 已提交
53 54 55
copy(gflags_lib
  SRCS ${GFLAGS_INCLUDE_DIR} ${GFLAGS_LIBRARIES}
  DSTS ${dst_dir} ${dst_dir}/lib
T
tensor-tang 已提交
56
  DEPS gflags
L
Luo Tao 已提交
57 58
)

59
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/glog")
L
Luo Tao 已提交
60 61 62
copy(glog_lib
  SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES}
  DSTS ${dst_dir} ${dst_dir}/lib
T
tensor-tang 已提交
63
  DEPS glog
L
Luo Tao 已提交
64 65
)

66
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/boost/")
Q
qiaolongfei 已提交
67
copy(boost_lib
Q
qiaolongfei 已提交
68
  SRCS ${BOOST_INCLUDE_DIR}/boost
Q
qiaolongfei 已提交
69
  DSTS ${dst_dir}
T
tensor-tang 已提交
70
  DEPS boost
Q
qiaolongfei 已提交
71 72
)

73 74 75 76 77 78 79
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/xxhash")
copy(xxhash_lib
  SRCS ${XXHASH_INCLUDE_DIR} ${XXHASH_LIBRARIES}
  DSTS ${dst_dir} ${dst_dir}/lib
  DEPS xxhash
)

80
if(NOT PROTOBUF_FOUND)
81
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/protobuf")
L
Luo Tao 已提交
82
    copy(protobuf_lib
83
      SRCS ${PROTOBUF_INCLUDE_DIR} ${PROTOBUF_LIBRARY}
L
Luo Tao 已提交
84
      DSTS ${dst_dir} ${dst_dir}/lib
T
tensor-tang 已提交
85
      DEPS extern_protobuf
L
Luo Tao 已提交
86
    )
87 88 89
endif()

if(NOT CBLAS_FOUND)
90
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/openblas")
91 92 93
    copy(openblas_lib
      SRCS ${CBLAS_INSTALL_DIR}/lib ${CBLAS_INSTALL_DIR}/include
      DSTS ${dst_dir} ${dst_dir}
T
tensor-tang 已提交
94
      DEPS extern_openblas
95
    )
L
Luo Tao 已提交
96
elseif (WITH_MKLML)
97
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/mklml")
98
    copy(mklml_lib
L
Luo Tao 已提交
99 100
      SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_INC_DIR}
      DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}
T
tensor-tang 已提交
101
      DEPS mklml
102
    )
103
endif()
L
Luo Tao 已提交
104

Q
qiaolongfei 已提交
105
if(WITH_MKLDNN)
106
  set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/mkldnn")
Q
qiaolongfei 已提交
107
  copy(mkldnn_lib
Q
qiaolongfei 已提交
108
    SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB}
Q
qiaolongfei 已提交
109
    DSTS ${dst_dir} ${dst_dir}/lib
T
tensor-tang 已提交
110
    DEPS mkldnn
Q
qiaolongfei 已提交
111 112 113
  )
endif()

D
dzhwinter 已提交
114
if (NOT WIN32)
115
if(NOT MOBILE_INFERENCE AND NOT RPI)
116
  set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappy")
117 118
  copy(snappy_lib
    SRCS ${SNAPPY_INCLUDE_DIR} ${SNAPPY_LIBRARIES}
T
tensor-tang 已提交
119 120
    DSTS ${dst_dir} ${dst_dir}/lib
    DEPS snappy)
121

122
  set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappystream")
123 124
  copy(snappystream_lib
    SRCS ${SNAPPYSTREAM_INCLUDE_DIR} ${SNAPPYSTREAM_LIBRARIES}
T
tensor-tang 已提交
125 126
    DSTS ${dst_dir} ${dst_dir}/lib
    DEPS snappystream)
127

128
  set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/zlib")
129 130
  copy(zlib_lib
    SRCS ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARIES}
T
tensor-tang 已提交
131 132
    DSTS ${dst_dir} ${dst_dir}/lib
    DEPS zlib)
133
endif()
D
dzhwinter 已提交
134
endif(NOT WIN32)
135

L
Luo Tao 已提交
136
# paddle fluid module
137
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
138
set(dst_dir "${FLUID_INSTALL_DIR}/paddle/fluid")
L
Luo Tao 已提交
139
set(module "framework")
D
dzhwinter 已提交
140
if (NOT WIN32)
L
luotao1 已提交
141 142 143
set(framework_lib_deps framework_py_proto)
endif(NOT WIN32)
copy(framework_lib DEPS ${framework_lib_deps}
D
dzhwinter 已提交
144
  SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
L
luotao1 已提交
145 146
       ${src_dir}/${module}/ir/*.h
  DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ${dst_dir}/${module}/ir
D
dzhwinter 已提交
147
)
L
Luo Tao 已提交
148

L
Luo Tao 已提交
149 150 151 152
set(module "memory")
copy(memory_lib
  SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/detail/*.h
  DSTS ${dst_dir}/${module} ${dst_dir}/${module}/detail
L
Luo Tao 已提交
153 154
)

Y
Yan Chunwei 已提交
155
set(inference_deps paddle_fluid_shared paddle_fluid)
L
Luo Tao 已提交
156

157
set(module "inference/api")
158
if (WITH_ANAKIN AND WITH_MKL)
159 160 161
    copy(anakin_inference_lib DEPS paddle_inference_api inference_anakin_api
        SRCS
        ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/libinference_anakin_api* # compiled anakin api
L
Luo Tao 已提交
162
        ${ANAKIN_INSTALL_DIR} # anakin release
T
Tao Luo 已提交
163
        DSTS ${FLUID_INSTALL_DIR}/third_party/install/anakin ${FLUID_INSTALL_DIR}/third_party/install/anakin)
164
     list(APPEND inference_deps anakin_inference_lib)
165 166
endif()

Y
Yan Chunwei 已提交
167 168 169
set(module "inference")
copy(inference_lib DEPS ${inference_deps}
  SRCS ${src_dir}/${module}/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/inference/libpaddle_fluid.*
T
Tao Luo 已提交
170
       ${src_dir}/${module}/api/paddle_inference_api.h
L
luotao1 已提交
171
       ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/paddle_inference_pass.h
T
Tao Luo 已提交
172
  DSTS ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}
Y
Yan Chunwei 已提交
173 174
)

L
Luo Tao 已提交
175
set(module "platform")
X
Xin Pan 已提交
176
copy(platform_lib DEPS profiler_py_proto
L
Luo Tao 已提交
177 178 179
  SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/dynload/*.h ${src_dir}/${module}/details/*.h
  DSTS ${dst_dir}/${module} ${dst_dir}/${module}/dynload ${dst_dir}/${module}/details
)
L
Luo Tao 已提交
180

L
Luo Tao 已提交
181 182 183 184
set(module "string")
copy(string_lib
  SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/tinyformat/*.h
  DSTS ${dst_dir}/${module} ${dst_dir}/${module}/tinyformat
L
Luo Tao 已提交
185 186
)

187 188
set(module "pybind")
copy(pybind_lib
Q
qiaolongfei 已提交
189
  SRCS ${CMAKE_CURRENT_BINARY_DIR}/paddle/fluid/${module}/pybind.h
190 191 192
  DSTS ${dst_dir}/${module}
)

193 194 195
# CMakeCache Info
copy(cmake_cache
  SRCS ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt
196
  DSTS ${FLUID_INSTALL_DIR})
197

198
# This command generates a complete fluid library for both train and inference
199
add_custom_target(fluid_lib_dist DEPENDS ${fluid_lib_dist_dep})
200

T
Tao Luo 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
# Following commands generate a inference-only fluid library
# third_party, version.txt and CMakeCache.txt are the same position with ${FLUID_INSTALL_DIR}
copy(third_party DEPS fluid_lib_dist
  SRCS ${FLUID_INSTALL_DIR}/third_party ${FLUID_INSTALL_DIR}/CMakeCache.txt
  DSTS ${FLUID_INFERENCE_INSTALL_DIR} ${FLUID_INFERENCE_INSTALL_DIR}
)

# only need libpaddle_fluid.so/a and paddle_inference_api.h for inference-only library
copy(inference_api_lib DEPS fluid_lib_dist
  SRCS ${FLUID_INSTALL_DIR}/paddle/fluid/inference/libpaddle_fluid.*
       ${FLUID_INSTALL_DIR}/paddle/fluid/inference/paddle_inference_api.h
  DSTS ${FLUID_INFERENCE_INSTALL_DIR}/paddle/lib ${FLUID_INFERENCE_INSTALL_DIR}/paddle/include
)

add_custom_target(inference_lib_dist DEPENDS third_party inference_api_lib)

217
# paddle fluid version
T
Tao Luo 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
function(version version_file)
  execute_process(
    COMMAND ${GIT_EXECUTABLE} log --pretty=format:%H -1
    WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
    OUTPUT_VARIABLE PADDLE_GIT_COMMIT)
  file(WRITE ${version_file}
    "GIT COMMIT ID: ${PADDLE_GIT_COMMIT}\n"
    "WITH_MKL: ${WITH_MKL}\n"
    "WITH_MKLDNN: ${WITH_MKLDNN}\n"
    "WITH_GPU: ${WITH_GPU}\n")
  if(WITH_GPU)
    file(APPEND ${version_file}
      "CUDA version: ${CUDA_VERSION}\n"
      "CUDNN version: v${CUDNN_MAJOR_VERSION}\n")
  endif()
endfunction()
version(${FLUID_INSTALL_DIR}/version.txt)
version(${FLUID_INFERENCE_INSTALL_DIR}/version.txt)