CMakeLists.txt 13.2 KB
Newer Older
1 2 3 4
if (NOT WITH_LITE)
    return()
endif()

S
superjomn 已提交
5 6
include(lite)

S
superjomn 已提交
7
message(WARNING "Lite enabled!")
8 9 10 11
message(STATUS "LIGHT_FRAMEWORK:\t${LITE_WITH_LIGHT_WEIGHT_FRAMEWORK}")
message(STATUS "LITE_WITH_CUDA:\t${LITE_WITH_CUDA}")
message(STATUS "LITE_WITH_X86:\t${LITE_WITH_X86}")
message(STATUS "LITE_WITH_ARM:\t${LITE_WITH_ARM}")
Y
Yan Chunwei 已提交
12
message(STATUS "LITE_WITH_PROFILE:\t${LITE_WITH_PROFILE}")
S
Superjomn 已提交
13

S
superjomn 已提交
14
set(LITE_MODEL_DIR "${THIRD_PARTY_PATH}/install")
C
Chunwei 已提交
15

S
sangoly 已提交
16
set(LITE_ON_MOBILE ${LITE_WITH_LIGHT_WEIGHT_FRAMEWORK})
C
Chunwei 已提交
17

Z
Zhen Wang 已提交
18
set(LITE_URL "http://paddle-inference-dist.bj.bcebos.com" CACHE STRING "inference download url")
S
superjomn 已提交
19 20 21 22 23 24 25 26 27 28

function(lite_download_and_uncompress INSTALL_DIR URL FILENAME)
    message(STATUS "Download inference test stuff from ${URL}/${FILENAME}")
    string(REGEX REPLACE "[-%.]" "_" FILENAME_EX ${FILENAME})
    set(EXTERNAL_PROJECT_NAME "extern_lite_download_${FILENAME_EX}")
    set(UNPACK_DIR "${INSTALL_DIR}/src/${EXTERNAL_PROJECT_NAME}")
    ExternalProject_Add(
            ${EXTERNAL_PROJECT_NAME}
            ${EXTERNAL_PROJECT_LOG_ARGS}
            PREFIX                ${INSTALL_DIR}
C
Chunwei 已提交
29
            DOWNLOAD_COMMAND      wget --no-check-certificate -q -O ${INSTALL_DIR}/${FILENAME} ${URL}/${FILENAME} && ${CMAKE_COMMAND} -E tar xzf ${INSTALL_DIR}/${FILENAME}
S
superjomn 已提交
30 31 32 33 34 35 36 37 38
            DOWNLOAD_DIR          ${INSTALL_DIR}
            DOWNLOAD_NO_PROGRESS  1
            CONFIGURE_COMMAND     ""
            BUILD_COMMAND         ""
            UPDATE_COMMAND        ""
            INSTALL_COMMAND       ""
    )
endfunction()

39
function (lite_deps TARGET)
40 41
  set(options "")
  set(oneValueArgs "")
C
Chunwei 已提交
42
  set(multiValueArgs DEPS X86_DEPS CUDA_DEPS ARM_DEPS PROFILE_DEPS LIGHT_DEPS HVY_DEPS CL_DEPS ARGS)
43 44
  cmake_parse_arguments(lite_deps "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

45
  set(deps ${lite_deps_DEPS})
46 47 48

  if(LITE_WITH_X86)
    foreach(var ${lite_deps_X86_DEPS})
49 50
      set(deps ${deps} ${var})
    endforeach(var)
51 52 53 54
  endif()

  if(LITE_WITH_CUDA)
    foreach(var ${lite_deps_CUDA_DEPS})
55 56
      set(deps ${deps} ${var})
    endforeach(var)
57 58 59 60
  endif()

  if(LITE_WITH_ARM)
    foreach(var ${lite_deps_ARM_DEPS})
61 62
      set(deps ${deps} ${var})
    endforeach(var)
63
  endif()
64

Y
Yan Chunwei 已提交
65 66 67 68 69 70
  if(LITE_WITH_PROFILE)
    foreach(var ${lite_deps_PROFILE_DEPS})
      set(deps ${deps} ${var})
    endforeach(var)
  endif()

71 72 73 74 75 76 77 78 79 80 81 82
  if(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK)
    foreach(var ${lite_deps_LIGHT_DEPS})
      set(deps ${deps} ${var})
    endforeach(var)
  endif()

  if (NOT LITE_WITH_LIGHT_WEIGHT_FRAMEWORK)
    foreach(var ${lite_deps_HVY_DEPS})
      set(deps ${deps} ${var})
    endforeach(var)
  endif()

C
Chunwei 已提交
83 84 85 86 87 88
  if (LITE_WITH_OPENCL)
    foreach(var ${lite_deps_CL_DEPS})
      set(deps ${deps} ${var})
    endforeach(var)
  endif()

89
  set(${TARGET} ${deps} PARENT_SCOPE)
90 91
endfunction()

S
superjomn 已提交
92

C
Chunwei 已提交
93 94 95
# A fake target to include all the libraries and tests the lite module depends.
add_custom_target(lite_compile_deps COMMAND echo 1)

Y
Yan Chunwei 已提交
96 97 98 99
# Add names for lite libraries for latter compile. We use this name list to avoid compiling
# the whole fluid project to accelerate the compile speed.
set(offline_lib_registry_file "${CMAKE_BINARY_DIR}/lite_libs.txt")
file(WRITE ${offline_lib_registry_file} "") # clean
S
superjomn 已提交
100

H
Huihuang Zheng 已提交
101
set(__lite_cc_files "" ; "")
S
superjomn 已提交
102 103
set(__lite_cc_files "${CMAKE_BINARY_DIR}/lite_cc_files.txt")
file(WRITE ${__lite_cc_files} "") # clean
H
Huihuang Zheng 已提交
104 105


106 107 108 109 110 111 112 113
# cc_library with branch support.
# The branches:
#  X86_DEPS: works only when LITE_WITH_X86 is ON.
#  CUDA_DEPS:     LITE_WITH_CUDA
#  ARM_DEPS:      LITE_WITH_ARM
#  PROFILE_DEPS:  LITE_WITH_PROFILE
#  LIGHT_DEPS:    LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
#  HVY_DEPS:      NOT LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
114
function(lite_cc_library TARGET)
H
Huihuang Zheng 已提交
115
    set(options SHARED shared STATIC static MODULE module)
116
    set(oneValueArgs "")
Z
Zhen Wang 已提交
117
    set(multiValueArgs SRCS DEPS X86_DEPS CUDA_DEPS CL_DEPS ARM_DEPS PROFILE_DEPS LIGHT_DEPS
118
      HVY_DEPS ARGS)
119 120 121 122 123 124 125
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    set(deps "")
    lite_deps(deps
            DEPS ${args_DEPS}
            X86_DEPS ${args_X86_DEPS}
            CUDA_DEPS ${args_CUDA_DEPS}
Z
Zhen Wang 已提交
126
            CL_DEPS ${args_CL_DEPS}
Y
Yan Chunwei 已提交
127
            ARM_DEPS ${args_ARM_DEPS}
128 129 130 131
            PROFILE_DEPS ${args_PROFILE_DEPS}
            LIGHT_DEPS ${args_LIGHT_DEPS}
            HVY_DEPS ${args_HVY_DEPS}
            )
H
Huihuang Zheng 已提交
132 133

    if (args_SHARED OR ARGS_shared)
C
Chunwei 已提交
134
        cc_library(${TARGET} SRCS ${args_SRCS} DEPS ${deps} ${args_DEPS} SHARED)
H
Huihuang Zheng 已提交
135 136 137
    elseif (args_MODULE OR ARGS_module)
        add_library(${TARGET} MODULE ${args_SRCS})
        add_dependencies(${TARGET} ${deps} ${args_DEPS})
C
Chunwei 已提交
138 139 140
    else()
        cc_library(${TARGET} SRCS ${args_SRCS} DEPS ${deps} ${args_DEPS})
    endif()
Z
Zhen Wang 已提交
141
    target_compile_options(${TARGET} BEFORE PRIVATE -Wno-ignored-qualifiers)
Y
Yan Chunwei 已提交
142

S
superjomn 已提交
143 144 145 146
    foreach(cc_file ${args_SRCS})
        file(APPEND ${__lite_cc_files} "${cc_file}\n")
    endforeach()

C
Chunwei 已提交
147
    # collect targets need to compile for lite
S
superjomn 已提交
148 149 150
    if (args_SRCS)
        add_dependencies(lite_compile_deps ${TARGET})
    endif()
C
Chunwei 已提交
151

Y
Yan Chunwei 已提交
152 153
    # register a library name.
    file(APPEND ${offline_lib_registry_file} "${TARGET}\n")
154 155
endfunction()

Y
Yan Chunwei 已提交
156 157 158
function(lite_cc_binary TARGET)
    set(options "")
    set(oneValueArgs "")
Z
Zhen Wang 已提交
159
    set(multiValueArgs SRCS DEPS X86_DEPS CUDA_DEPS CL_DEPS ARM_DEPS PROFILE_DEPS
160
      LIGHT_DEPS HVY_DEPS ARGS)
Y
Yan Chunwei 已提交
161 162 163 164 165 166 167
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    set(deps "")
    lite_deps(deps
            DEPS ${args_DEPS}
            X86_DEPS ${args_X86_DEPS}
            CUDA_DEPS ${args_CUDA_DEPS}
Z
Zhen Wang 已提交
168
            CL_DEPS ${args_CL_DEPS}
Y
Yan Chunwei 已提交
169 170
            ARM_DEPS ${args_ARM_DEPS}
            PROFILE_DEPS ${args_PROFILE_DEPS}
171 172
            LIGHT_DEPS ${args_LIGHT_DEPS}
            HVY_DEPS ${args_HVY_DEPS}
Y
Yan Chunwei 已提交
173
            )
Y
Yan Chunwei 已提交
174
    cc_binary(${TARGET} SRCS ${args_SRCS} DEPS ${deps} ${args_DEPS})
Z
Zhen Wang 已提交
175
    target_compile_options(${TARGET} BEFORE PRIVATE -Wno-ignored-qualifiers)
C
Chunwei 已提交
176 177
    # collect targets need to compile for lite
    add_dependencies(lite_compile_deps ${TARGET})
Y
Yan Chunwei 已提交
178 179 180 181 182 183
endfunction()

# Add a unit-test name to file for latter offline manual test.
set(offline_test_registry_file "${CMAKE_BINARY_DIR}/lite_tests.txt")
file(WRITE ${offline_test_registry_file} "") # clean
# Test lite modules.
184 185 186
function(lite_cc_test TARGET)
    set(options "")
    set(oneValueArgs "")
Z
Zhen Wang 已提交
187
    set(multiValueArgs SRCS DEPS X86_DEPS CUDA_DEPS CL_DEPS ARM_DEPS PROFILE_DEPS
188 189
      LIGHT_DEPS HVY_DEPS
      ARGS)
190 191 192 193 194 195 196
    cmake_parse_arguments(args "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    set(deps "")
    lite_deps(deps
            DEPS ${args_DEPS}
            X86_DEPS ${args_X86_DEPS}
            CUDA_DEPS ${args_CUDA_DEPS}
Z
Zhen Wang 已提交
197
            CL_DEPS ${args_CL_DEPS}
198
            ARM_DEPS ${args_ARM_DEPS}
Y
Yan Chunwei 已提交
199
            PROFILE_DEPS ${args_PROFILE_DEPS}
200 201
            LIGHT_DEPS ${args_LIGHT_DEPS}
            HVY_DEPS ${args_HVY_DEPS}
202
            )
Y
Yan Chunwei 已提交
203
    _lite_cc_test(${TARGET} SRCS ${args_SRCS} DEPS ${deps} ARGS ${args_ARGS})
Z
Zhen Wang 已提交
204
    target_compile_options(${TARGET} BEFORE PRIVATE -Wno-ignored-qualifiers)
Y
Yan Chunwei 已提交
205
    file(APPEND ${offline_test_registry_file} "${TARGET}\n")
C
Chunwei 已提交
206 207 208

    # collect targets need to compile for lite
    add_dependencies(lite_compile_deps ${TARGET})
209
endfunction()
S
superjomn 已提交
210

Z
Zhen Wang 已提交
211 212
add_subdirectory(operators)
add_subdirectory(kernels)
S
update  
superjomn 已提交
213
add_subdirectory(core)
S
superjomn 已提交
214
add_subdirectory(x86)
T
tensor-tang 已提交
215
add_subdirectory(arm)
216
add_subdirectory(host)
217
add_subdirectory(cuda)
Z
Zhen Wang 已提交
218
add_subdirectory(opencl)
S
superjomn 已提交
219
add_subdirectory(model_parser)
S
superjomn 已提交
220
add_subdirectory(utils)
S
superjomn 已提交
221
add_subdirectory(api)
222
add_subdirectory(gen_code)
223
add_subdirectory(tools)
C
Chunwei 已提交
224 225 226

if (WITH_TESTING)
    lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "lite_naive_model.tar.gz")
S
sangoly 已提交
227
    if(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK)
228
	lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "mobilenet_v1.tar.gz")
S
sangoly 已提交
229
        lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "mobilenet_v2_relu.tar.gz")
230
        lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "resnet50.tar.gz")
S
sangoly 已提交
231
        lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "inception_v4_simple.tar.gz")
S
sangoly 已提交
232
    endif()
233 234 235
    if(NOT LITE_WITH_LIGHT_WEIGHT_FRAMEWORK)
        lite_download_and_uncompress(${LITE_MODEL_DIR} ${LITE_URL} "GoogleNet_inference.tar.gz")
    endif()
C
Chunwei 已提交
236
endif()
C
Chunwei 已提交
237

238 239 240 241 242 243 244 245 246 247 248 249 250
if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK AND LITE_WITH_ARM)
    # for publish
    set(INFER_LITE_PUBLISH_ROOT "${CMAKE_BINARY_DIR}/inference_lite_lib.${ARM_TARGET_OS}.${ARM_TARGET_ARCH_ABI}")
    if (LITE_WITH_OPENCL) 
        set(INFER_LITE_PUBLISH_ROOT "${INFER_LITE_PUBLISH_ROOT}.opencl")
    endif(LITE_WITH_OPENCL)
    message(STATUS "publish inference lib to ${INFER_LITE_PUBLISH_ROOT}")

    # The final target for publish lite lib
    add_custom_target(publish_inference_lite)
    
    # add cxx lib
    add_custom_target(publish_inference_cxx_lib ${TARGET}
H
Huihuang Zheng 已提交
251
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
252 253 254 255
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/bin"
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/cxx/include"
            COMMAND cp "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/api/paddle_*.h" "${INFER_LITE_PUBLISH_ROOT}/cxx/include"
            COMMAND cp "${CMAKE_BINARY_DIR}/libpaddle_api_full_bundled.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
S
sangoly 已提交
256
            COMMAND cp "${CMAKE_BINARY_DIR}/libpaddle_api_light_bundled.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib"
257 258 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
            COMMAND cp "${CMAKE_BINARY_DIR}/paddle/fluid/lite/api/model_optimize_tool" "${INFER_LITE_PUBLISH_ROOT}/bin"
            COMMAND cp "${CMAKE_BINARY_DIR}/paddle/fluid/lite/gen_code/paddle_code_generator" "${INFER_LITE_PUBLISH_ROOT}/bin"
            )
    add_dependencies(publish_inference_cxx_lib model_optimize_tool)
    add_dependencies(publish_inference_cxx_lib paddle_code_generator)
    add_dependencies(publish_inference_cxx_lib bundle_full_api)
    add_dependencies(publish_inference_cxx_lib bundle_light_api)
    add_dependencies(publish_inference_lite publish_inference_cxx_lib)

    if (LITE_WITH_JAVA)
    # add java lib
        add_custom_target(publish_inference_java_lib ${TARGET}
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/java/so"
            COMMAND cp "${CMAKE_BINARY_DIR}/paddle/fluid/lite/api/android/jni/libpaddle_lite_jni.so" "${INFER_LITE_PUBLISH_ROOT}/java/so"
            COMMAND cp -r "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/api/android/jni/src" "${INFER_LITE_PUBLISH_ROOT}/java"
        )
        add_dependencies(publish_inference_java_lib paddle_lite_jni)
        add_dependencies(publish_inference_lite publish_inference_java_lib)
    endif()
    
    if ((ARM_TARGET_OS STREQUAL "android") AND (NOT LITE_WITH_OPENCL) AND
            ((ARM_TARGET_ARCH_ABI STREQUAL armv7) OR (ARM_TARGET_ARCH_ABI STREQUAL armv8)))
        # copy
        add_custom_target(publish_inference_android_cxx_demos ${TARGET}
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/third_party"
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/cxx"
            COMMAND cp -r "${CMAKE_BINARY_DIR}/third_party/install/glog" "${INFER_LITE_PUBLISH_ROOT}/third_party"
            COMMAND cp -r "${CMAKE_BINARY_DIR}/third_party/install/gflags" "${INFER_LITE_PUBLISH_ROOT}/third_party"
            COMMAND cp "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/demo/cxx/Makefile.def" "${INFER_LITE_PUBLISH_ROOT}/demo/cxx"
            COMMAND cp -r "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/demo/cxx/mobile_full" "${INFER_LITE_PUBLISH_ROOT}/demo/cxx"
            COMMAND cp "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/demo/cxx/makefiles/mobile_full/Makefile.${ARM_TARGET_OS}.${ARM_TARGET_ARCH_ABI}" "${INFER_LITE_PUBLISH_ROOT}/demo/cxx/mobile_full/Makefile"
S
sangoly 已提交
288
            COMMAND cp -r "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/demo/cxx/mobile_light" "${INFER_LITE_PUBLISH_ROOT}/demo/cxx"
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
            COMMAND cp "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/demo/cxx/makefiles/mobile_light/Makefile.${ARM_TARGET_OS}.${ARM_TARGET_ARCH_ABI}" "${INFER_LITE_PUBLISH_ROOT}/demo/cxx/mobile_light/Makefile"
        )
        add_dependencies(publish_inference_android_cxx_demos glog gflags)
        add_dependencies(publish_inference_cxx_lib publish_inference_android_cxx_demos)
    
        if (LITE_WITH_JAVA)
            # copy java mobile_light demo/lib
            add_custom_target(publish_inference_android_java_demo ${TARGET}
                    COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/java"
                    COMMAND cp -r "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/demo/java/android" "${INFER_LITE_PUBLISH_ROOT}/demo/java"
                    COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/java/android/PaddlePredictor/app/src/main/jniLibs/arm7"
                    COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/java/android/PaddlePredictor/app/src/main/jniLibs/arm8"
                    COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/java/android/PaddlePredictor/app/src/main/jniLibs/arm64-v8a"
                    COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/java/android/PaddlePredictor/app/src/main/jniLibs/armeabi-v7a"
                    COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/demo/java/android/PaddlePredictor/app/src/main/jniLibs/x86"
C
Chunwei 已提交
304
            )
305 306 307 308 309 310 311 312 313 314
            add_dependencies(publish_inference_java_lib publish_inference_android_java_demo)
        endif()
    endif()
    
    if (LITE_WITH_OPENCL) 
        add_custom_target(publish_inference_opencl ${TARGET}
            COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/opencl"
            COMMAND cp -r "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/opencl/cl_kernel" "${INFER_LITE_PUBLISH_ROOT}/opencl"
        )
        add_dependencies(publish_inference_cxx_lib publish_inference_opencl)
S
sangoly 已提交
315
    endif()
C
Chunwei 已提交
316
endif()