inference_lib.cmake 9.4 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
16

17 18 19 20 21 22
if(WIN32)
    if(NOT PYTHON_EXECUTABLE)
	FIND_PACKAGE(PythonInterp REQUIRED)
    endif()
endif()

23
set(COPY_SCRIPT_DIR ${PADDLE_SOURCE_DIR}/cmake)
L
Luo Tao 已提交
24 25 26
function(copy TARGET)
    set(options "")
    set(oneValueArgs "")
27
    set(multiValueArgs SRCS DSTS)
L
Luo Tao 已提交
28 29 30 31
    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)
32
    if (NOT ${copy_lib_SRCS_len} EQUAL ${copy_lib_DSTS_len})
L
Luo Tao 已提交
33
        message(FATAL_ERROR "${TARGET} source numbers are not equal to destination numbers")
34
    endif ()
L
Luo Tao 已提交
35
    math(EXPR len "${copy_lib_SRCS_len} - 1")
36
    foreach (index RANGE ${len})
L
Luo Tao 已提交
37 38
        list(GET copy_lib_SRCS ${index} src)
        list(GET copy_lib_DSTS ${index} dst)
39 40 41 42 43 44
        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})
        else (WIN32) #not windows
45
            add_custom_command(TARGET ${TARGET} POST_BUILD
46 47 48 49 50
                    COMMAND mkdir -p "${dst}"
                    COMMAND cp -r "${src}" "${dst}"
                    COMMENT "copying ${src} -> ${dst}")
        endif (WIN32) # not windows
    endforeach ()
L
Luo Tao 已提交
51 52
endfunction()

L
Luo Tao 已提交
53
# third party
54 55 56 57
set(third_party_deps eigen3 gflags glog boost xxhash zlib)
if(NOT PROTOBUF_FOUND OR WIN32)
    list(APPEND third_party_deps extern_protobuf)
endif ()
L
Luo Tao 已提交
58

59 60 61 62 63
if (WITH_MKLML)
    list(APPEND third_party_deps mklml)
elseif (NOT CBLAS_FOUND OR WIN32)
    list(APPEND third_party_deps extern_openblas)
endif ()
L
Luo Tao 已提交
64

65 66 67
if (WITH_MKLDNN)
    list(APPEND third_party_deps mkldnn_shared_lib)
endif ()
L
Luo Tao 已提交
68

69 70 71
if (WITH_NGRAPH)
    list(APPEND third_party_deps ngraph)
endif ()
Q
qiaolongfei 已提交
72

73
add_custom_target(third_party DEPENDS ${third_party_deps})
74

75 76 77
# inference-only library
set(inference_lib_deps third_party paddle_fluid paddle_fluid_shared)
add_custom_target(inference_lib_dist DEPENDS ${inference_lib_deps})
78

79 80 81 82 83 84 85 86 87 88 89 90
set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/eigen3")
copy(inference_lib_dist
    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)

set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/boost")
copy(inference_lib_dist
    SRCS ${BOOST_INCLUDE_DIR}/boost
    DSTS ${dst_dir})

if(WITH_MKLML)
    set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/mklml")
91
    if(WIN32)
92 93 94 95 96
        copy(inference_lib_dist
            SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_SHARED_LIB}
                ${MKLML_SHARED_LIB_DEPS} ${MKLML_SHARED_IOMP_LIB} ${MKLML_INC_DIR}
            DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}/lib
                ${dst_dir}/lib ${dst_dir}/lib ${dst_dir})
97
    else()
98 99 100
        copy(inference_lib_dist
            SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_INC_DIR}
            DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir})
101
    endif()
P
peizhilin 已提交
102
elseif (NOT CBLAS_FOUND OR WIN32)
103 104
    set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/openblas")
    copy(inference_lib_dist
P
peizhilin 已提交
105
            SRCS ${CBLAS_INSTALL_DIR}/lib ${CBLAS_INSTALL_DIR}/include
106
            DSTS ${dst_dir} ${dst_dir})
107
endif ()
Q
qiaolongfei 已提交
108

109 110 111 112 113 114 115 116 117 118 119 120
if(WITH_MKLDNN)
set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/mkldnn")
if(WIN32)
    copy(inference_lib_dist
        SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB} ${MKLDNN_LIB}
        DSTS ${dst_dir} ${dst_dir}/lib ${dst_dir}/lib)
else()
    copy(inference_lib_dist
        SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB}
        DSTS ${dst_dir} ${dst_dir}/lib)
endif()
endif()
121

122 123 124 125
set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/gflags")
copy(inference_lib_dist
        SRCS ${GFLAGS_INCLUDE_DIR} ${GFLAGS_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib)
126

127 128 129 130
set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/glog")
copy(inference_lib_dist
        SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib)
131

132 133 134 135
set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/xxhash")
copy(inference_lib_dist
        SRCS ${XXHASH_INCLUDE_DIR} ${XXHASH_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib)
L
Luo Tao 已提交
136

137 138 139 140
set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/zlib")
copy(inference_lib_dist
        SRCS ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib)
L
Luo Tao 已提交
141

142 143 144 145 146 147
if (NOT PROTOBUF_FOUND OR WIN32)
    set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/protobuf")
    copy(inference_lib_dist
            SRCS ${PROTOBUF_INCLUDE_DIR} ${PROTOBUF_LIBRARY}
            DSTS ${dst_dir} ${dst_dir}/lib)
endif ()
L
Luo Tao 已提交
148

149 150 151 152 153 154
if (WITH_NGRAPH)
    set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/ngraph")
    copy(inference_lib_dist
            SRCS ${NGRAPH_INC_DIR} ${NGRAPH_LIB_DIR}
            DSTS ${dst_dir} ${dst_dir})
endif ()
155

N
nhzlx 已提交
156
if (TENSORRT_FOUND)
157 158
    set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/tensorrt")
    copy(inference_lib_dist
159
        SRCS ${TENSORRT_ROOT}/include/Nv*.h ${TENSORRT_ROOT}/lib/*nvinfer*
160
        DSTS ${dst_dir}/include ${dst_dir}/lib)
N
nhzlx 已提交
161 162
endif ()

163
if (ANAKIN_FOUND)
164 165
    set(dst_dir "${FLUID_INFERENCE_INSTALL_DIR}/third_party/install/anakin")
    copy(inference_lib_dist
166
        SRCS ${ANAKIN_ROOT}/*
167
        DSTS ${dst_dir})
168
endif ()
N
nhzlx 已提交
169

170 171 172 173 174
copy(inference_lib_dist
     SRCS ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt
     DSTS ${FLUID_INFERENCE_INSTALL_DIR})

set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
P
peizhilin 已提交
175 176 177 178 179
if(WIN32)
    set(paddle_fluid_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/${CMAKE_BUILD_TYPE}/libpaddle_fluid.*)
else(WIN32)
    set(paddle_fluid_lib ${PADDLE_BINARY_DIR}/paddle/fluid/inference/libpaddle_fluid.*)
endif(WIN32)
180 181 182 183 184 185 186 187 188 189 190 191 192 193

copy(inference_lib_dist
     SRCS  ${src_dir}/inference/api/paddle_*.h ${paddle_fluid_lib}
     DSTS  ${FLUID_INFERENCE_INSTALL_DIR}/paddle/include ${FLUID_INFERENCE_INSTALL_DIR}/paddle/lib)


# fluid library for both train and inference
set(fluid_lib_deps inference_lib_dist)
add_custom_target(fluid_lib_dist ALL DEPENDS ${fluid_lib_deps})

set(dst_dir "${FLUID_INSTALL_DIR}/paddle/fluid")
set(module "inference")
copy(fluid_lib_dist
  SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/api/paddle_*.h ${paddle_fluid_lib} 
T
Tao Luo 已提交
194
  DSTS ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}
195 196 197 198 199 200 201 202 203 204 205 206 207 208
)

set(module "framework")
set(framework_lib_deps framework_proto)
add_dependencies(fluid_lib_dist ${framework_lib_deps})
copy(fluid_lib_dist
    SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/data_feed.pb.h ${src_dir}/${module}/ir/memory_optimize_pass/*.h
    ${src_dir}/${module}/ir/*.h ${src_dir}/${module}/fleet/*.h
    DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}/ir/memory_optimize_pass ${dst_dir}/${module}/ir ${dst_dir}/${module}/fleet)

set(module "memory")
copy(fluid_lib_dist
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/detail/*.h ${src_dir}/${module}/allocation/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/detail ${dst_dir}/${module}/allocation
P
peizhilin 已提交
209
        )
Y
Yan Chunwei 已提交
210

L
Luo Tao 已提交
211
set(module "platform")
212 213 214 215 216
set(platform_lib_deps profiler_proto)
add_dependencies(fluid_lib_dist ${platform_lib_deps})
copy(fluid_lib_dist
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/dynload/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/platform/profiler.pb.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/dynload ${dst_dir}/${module}/details ${dst_dir}/${module}
217
        )
L
Luo Tao 已提交
218

L
Luo Tao 已提交
219
set(module "string")
220
copy(fluid_lib_dist
221 222 223
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/tinyformat/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/tinyformat
        )
L
Luo Tao 已提交
224

225
set(module "pybind")
226
copy(fluid_lib_dist
227 228 229
        SRCS ${CMAKE_CURRENT_BINARY_DIR}/paddle/fluid/${module}/pybind.h
        DSTS ${dst_dir}/${module}
        )
230

231
# CMakeCache Info
232 233 234
copy(fluid_lib_dist
        SRCS ${FLUID_INFERENCE_INSTALL_DIR}/third_party ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt
        DSTS ${FLUID_INSTALL_DIR} ${FLUID_INSTALL_DIR}
235
        )
T
Tao Luo 已提交
236

237
# paddle fluid version
T
Tao Luo 已提交
238
function(version version_file)
239 240 241 242 243 244 245 246 247 248 249 250 251 252
    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 ()
T
Tao Luo 已提交
253 254 255
endfunction()
version(${FLUID_INSTALL_DIR}/version.txt)
version(${FLUID_INFERENCE_INSTALL_DIR}/version.txt)