inference_lib.cmake 9.7 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 17 18 19 20
function(copy TARGET)
    set(options "")
    set(oneValueArgs "")
    set(multiValueArgs SRCS DSTS DEPS)
    cmake_parse_arguments(copy_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
21
    set(fluid_lib_dist_dep ${TARGET} ${fluid_lib_dist_dep} PARENT_SCOPE)
L
Luo Tao 已提交
22 23 24

    list(LENGTH copy_lib_SRCS copy_lib_SRCS_len)
    list(LENGTH copy_lib_DSTS copy_lib_DSTS_len)
25
    if (NOT ${copy_lib_SRCS_len} EQUAL ${copy_lib_DSTS_len})
L
Luo Tao 已提交
26
        message(FATAL_ERROR "${TARGET} source numbers are not equal to destination numbers")
27
    endif ()
L
Luo Tao 已提交
28
    math(EXPR len "${copy_lib_SRCS_len} - 1")
29

L
Luo Tao 已提交
30
    add_custom_target(${TARGET} DEPENDS ${copy_lib_DEPS})
31
    foreach (index RANGE ${len})
L
Luo Tao 已提交
32 33
        list(GET copy_lib_SRCS ${index} src)
        list(GET copy_lib_DSTS ${index} dst)
34
        if (WIN32)
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
            # windows cmd shell will not expand wildcard automatically.
            # below expand the files,libs and copy them by rules.
            file(GLOB header_files ${src} "*.h")
            file(GLOB static_lib_files ${src} "*.lib")
            file(GLOB dll_lib_files ${src} "*.dll")
            set(src_files ${header_files} ${static_lib_files} ${dll_lib_files})

            if (NOT "${src_files}" STREQUAL "")
                list(REMOVE_DUPLICATES src_files)
            endif ()
            add_custom_command(TARGET ${TARGET} PRE_BUILD
                    COMMAND ${CMAKE_COMMAND} -E make_directory "${dst}"
                    )
            foreach (src_file ${src_files})
                add_custom_command(TARGET ${TARGET} PRE_BUILD
                        COMMAND ${CMAKE_COMMAND} -E copy "${src_file}" "${dst}"
                        COMMENT "copying ${src_file} -> ${dst}")
            endforeach ()
        else (WIN32) # not windows
            add_custom_command(TARGET ${TARGET} PRE_BUILD
                    COMMAND mkdir -p "${dst}"
                    COMMAND cp -r "${src}" "${dst}"
                    COMMENT "copying ${src} -> ${dst}")
        endif (WIN32) # not windows
    endforeach ()
L
Luo Tao 已提交
60 61
endfunction()

L
Luo Tao 已提交
62
# third party
63
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/eigen3")
L
Luo Tao 已提交
64
copy(eigen3_lib
65 66 67 68
        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
        DEPS eigen3
        )
L
Luo Tao 已提交
69

70
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/gflags")
L
Luo Tao 已提交
71
copy(gflags_lib
72 73 74 75
        SRCS ${GFLAGS_INCLUDE_DIR} ${GFLAGS_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS gflags
        )
L
Luo Tao 已提交
76

77
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/glog")
L
Luo Tao 已提交
78
copy(glog_lib
79 80 81 82
        SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS glog
        )
L
Luo Tao 已提交
83

84
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/boost/")
Q
qiaolongfei 已提交
85
copy(boost_lib
86 87 88 89
        SRCS ${BOOST_INCLUDE_DIR}/boost
        DSTS ${dst_dir}
        DEPS boost
        )
Q
qiaolongfei 已提交
90

91 92
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/xxhash")
copy(xxhash_lib
93 94 95 96
        SRCS ${XXHASH_INCLUDE_DIR} ${XXHASH_LIBRARIES}
        DSTS ${dst_dir} ${dst_dir}/lib
        DEPS xxhash
        )
97

98
if (NOT PROTOBUF_FOUND)
99
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/protobuf")
L
Luo Tao 已提交
100
    copy(protobuf_lib
101 102 103 104 105
            SRCS ${PROTOBUF_INCLUDE_DIR} ${PROTOBUF_LIBRARY}
            DSTS ${dst_dir} ${dst_dir}/lib
            DEPS extern_protobuf
            )
endif ()
106

107
if (NOT CBLAS_FOUND)
108
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/openblas")
109
    copy(openblas_lib
110 111 112 113
            SRCS ${CBLAS_INSTALL_DIR}/lib ${CBLAS_INSTALL_DIR}/include
            DSTS ${dst_dir} ${dst_dir}
            DEPS extern_openblas
            )
L
Luo Tao 已提交
114
elseif (WITH_MKLML)
115
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/mklml")
116
    copy(mklml_lib
117 118 119 120 121 122 123 124 125 126 127 128 129 130
            SRCS ${MKLML_LIB} ${MKLML_IOMP_LIB} ${MKLML_INC_DIR}
            DSTS ${dst_dir}/lib ${dst_dir}/lib ${dst_dir}
            DEPS mklml
            )
endif ()

if (WITH_MKLDNN)
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/mkldnn")
    copy(mkldnn_lib
            SRCS ${MKLDNN_INC_DIR} ${MKLDNN_SHARED_LIB}
            DSTS ${dst_dir} ${dst_dir}/lib
            DEPS mkldnn
            )
endif ()
Q
qiaolongfei 已提交
131

132 133 134 135 136 137 138 139 140
if (WITH_NGRAPH)
    set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/ngraph")
    copy(ngraph_lib
            SRCS ${NGRAPH_INC_DIR} ${NGRAPH_LIB_DIR}
            DSTS ${dst_dir} ${dst_dir}
            DEPS ngraph
            )
endif ()

D
dzhwinter 已提交
141
if (NOT WIN32)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    if (NOT MOBILE_INFERENCE AND NOT RPI)
        set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappy")
        copy(snappy_lib
                SRCS ${SNAPPY_INCLUDE_DIR} ${SNAPPY_LIBRARIES}
                DSTS ${dst_dir} ${dst_dir}/lib
                DEPS snappy)

        set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappystream")
        copy(snappystream_lib
                SRCS ${SNAPPYSTREAM_INCLUDE_DIR} ${SNAPPYSTREAM_LIBRARIES}
                DSTS ${dst_dir} ${dst_dir}/lib
                DEPS snappystream)

        set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/zlib")
        copy(zlib_lib
                SRCS ${ZLIB_INCLUDE_DIR} ${ZLIB_LIBRARIES}
                DSTS ${dst_dir} ${dst_dir}/lib
                DEPS zlib)
    endif ()
endif (NOT WIN32)
162

L
Luo Tao 已提交
163
# paddle fluid module
164
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
165
set(dst_dir "${FLUID_INSTALL_DIR}/paddle/fluid")
L
Luo Tao 已提交
166
set(module "framework")
D
dzhwinter 已提交
167
if (NOT WIN32)
168 169
    set(framework_lib_deps framework_py_proto)
endif (NOT WIN32)
L
luotao1 已提交
170
copy(framework_lib DEPS ${framework_lib_deps}
171 172 173 174
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
        ${src_dir}/${module}/ir/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ${dst_dir}/${module}/ir
        )
L
Luo Tao 已提交
175

L
Luo Tao 已提交
176 177
set(module "memory")
copy(memory_lib
W
Wojciech Uss 已提交
178 179
        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
180
        )
L
Luo Tao 已提交
181

Y
Yan Chunwei 已提交
182
set(inference_deps paddle_fluid_shared paddle_fluid)
L
Luo Tao 已提交
183

184
set(module "inference/api")
185
if (WITH_ANAKIN AND WITH_MKL)
186
    copy(anakin_inference_lib DEPS paddle_inference_api inference_anakin_api
187 188 189 190 191 192
            SRCS
            ${PADDLE_BINARY_DIR}/paddle/fluid/inference/api/libinference_anakin_api* # compiled anakin api
            ${ANAKIN_INSTALL_DIR} # anakin release
            DSTS ${FLUID_INSTALL_DIR}/third_party/install/anakin ${FLUID_INSTALL_DIR}/third_party/install/anakin)
    list(APPEND inference_deps anakin_inference_lib)
endif ()
193

Y
Yan Chunwei 已提交
194 195 196
set(module "inference")
copy(inference_lib DEPS ${inference_deps}
  SRCS ${src_dir}/${module}/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/inference/libpaddle_fluid.*
197
       ${src_dir}/${module}/api/paddle_*.h
T
Tao Luo 已提交
198
  DSTS ${dst_dir}/${module} ${dst_dir}/${module} ${dst_dir}/${module}
P
peizhilin 已提交
199
        )
Y
Yan Chunwei 已提交
200

L
Luo Tao 已提交
201
set(module "platform")
X
Xin Pan 已提交
202
copy(platform_lib DEPS profiler_py_proto
203 204 205
        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 已提交
206

L
Luo Tao 已提交
207 208
set(module "string")
copy(string_lib
209 210 211
        SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/tinyformat/*.h
        DSTS ${dst_dir}/${module} ${dst_dir}/${module}/tinyformat
        )
L
Luo Tao 已提交
212

213 214
set(module "pybind")
copy(pybind_lib
215 216 217
        SRCS ${CMAKE_CURRENT_BINARY_DIR}/paddle/fluid/${module}/pybind.h
        DSTS ${dst_dir}/${module}
        )
218

219 220
# CMakeCache Info
copy(cmake_cache
221 222
        SRCS ${CMAKE_CURRENT_BINARY_DIR}/CMakeCache.txt
        DSTS ${FLUID_INSTALL_DIR})
223

224
# This command generates a complete fluid library for both train and inference
225
add_custom_target(fluid_lib_dist DEPENDS ${fluid_lib_dist_dep})
226

T
Tao Luo 已提交
227 228 229
# 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
230 231 232
        SRCS ${FLUID_INSTALL_DIR}/third_party ${FLUID_INSTALL_DIR}/CMakeCache.txt
        DSTS ${FLUID_INFERENCE_INSTALL_DIR} ${FLUID_INFERENCE_INSTALL_DIR}
        )
T
Tao Luo 已提交
233

234
# only need libpaddle_fluid.so/a and paddle_*.h for inference-only library
T
Tao Luo 已提交
235 236
copy(inference_api_lib DEPS fluid_lib_dist
  SRCS ${FLUID_INSTALL_DIR}/paddle/fluid/inference/libpaddle_fluid.*
237
       ${FLUID_INSTALL_DIR}/paddle/fluid/inference/paddle_*.h
T
Tao Luo 已提交
238 239 240 241 242
  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)

243
# paddle fluid version
T
Tao Luo 已提交
244
function(version version_file)
245 246 247 248 249 250 251 252 253 254 255 256 257 258
    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 已提交
259 260 261
endfunction()
version(${FLUID_INSTALL_DIR}/version.txt)
version(${FLUID_INFERENCE_INSTALL_DIR}/version.txt)