diff --git a/cmake/lite.cmake b/cmake/lite.cmake new file mode 100644 index 0000000000000000000000000000000000000000..35d5f7e287128d9ffbb15ad795974be2556a13c4 --- /dev/null +++ b/cmake/lite.cmake @@ -0,0 +1,79 @@ +# Bundle several static libraries into one. +function(bundle_static_library tgt_name bundled_tgt_name fake_target) + list(APPEND static_libs ${tgt_name}) + + function(_recursively_collect_dependencies input_target) + set(_input_link_libraries LINK_LIBRARIES) + get_target_property(_input_type ${input_target} TYPE) + if (${_input_type} STREQUAL "INTERFACE_LIBRARY") + set(_input_link_libraries INTERFACE_LINK_LIBRARIES) + endif() + get_target_property(public_dependencies ${input_target} ${_input_link_libraries}) + foreach(dependency IN LISTS public_dependencies) + if(TARGET ${dependency}) + get_target_property(alias ${dependency} ALIASED_TARGET) + if (TARGET ${alias}) + set(dependency ${alias}) + endif() + get_target_property(_type ${dependency} TYPE) + if (${_type} STREQUAL "STATIC_LIBRARY") + list(APPEND static_libs ${dependency}) + endif() + + get_property(library_already_added + GLOBAL PROPERTY _${tgt_name}_static_bundle_${dependency}) + if (NOT library_already_added) + set_property(GLOBAL PROPERTY _${tgt_name}_static_bundle_${dependency} ON) + _recursively_collect_dependencies(${dependency}) + endif() + endif() + endforeach() + set(static_libs ${static_libs} PARENT_SCOPE) + endfunction() + + _recursively_collect_dependencies(${tgt_name}) + + list(REMOVE_DUPLICATES static_libs) + + set(bundled_tgt_full_name + ${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX}) + + message(STATUS "+++++ bundled_tgt_full_name: ${bundled_tgt_full_name}") + + file(WRITE ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in + "CREATE ${bundled_tgt_full_name}\n" ) + + foreach(tgt IN LISTS static_libs) + file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in + "ADDLIB $\n") + endforeach() + + file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in "SAVE\n") + file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in "END\n") + + file(GENERATE + OUTPUT ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar + INPUT ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in) + + set(ar_tool ${CMAKE_AR}) + if (CMAKE_INTERPROCEDURAL_OPTIMIZATION) + set(ar_tool ${CMAKE_CXX_COMPILER_AR}) + endif() + + add_custom_command( + COMMAND ${ar_tool} -M < ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar + OUTPUT ${bundled_tgt_full_name} + COMMENT "Bundling ${bundled_tgt_name}" + VERBATIM) + + add_custom_target(${fake_target} ALL DEPENDS ${bundled_tgt_full_name}) + add_dependencies(${fake_target} ${tgt_name}) + + add_library(${bundled_tgt_name} STATIC IMPORTED) + set_target_properties(${bundled_tgt_name} + PROPERTIES + IMPORTED_LOCATION ${bundled_tgt_full_name} + INTERFACE_INCLUDE_DIRECTORIES $) + add_dependencies(${bundled_tgt_name} ${fake_target}) + +endfunction() diff --git a/paddle/fluid/lite/CMakeLists.txt b/paddle/fluid/lite/CMakeLists.txt index aae03d3aba8a908d5678bf04bb4c04eb60028de3..6c867084212ff4db317a86c2c63211fd6490aec0 100644 --- a/paddle/fluid/lite/CMakeLists.txt +++ b/paddle/fluid/lite/CMakeLists.txt @@ -2,6 +2,8 @@ if (NOT WITH_LITE) return() endif() +include(lite) + message(WARNING "Lite enabled!") message(STATUS "LIGHT_FRAMEWORK:\t${LITE_WITH_LIGHT_WEIGHT_FRAMEWORK}") message(STATUS "LITE_WITH_CUDA:\t${LITE_WITH_CUDA}") @@ -85,9 +87,9 @@ function (lite_deps TARGET) endif() set(${TARGET} ${deps} PARENT_SCOPE) - endfunction() + # A fake target to include all the libraries and tests the lite module depends. add_custom_target(lite_compile_deps COMMAND echo 1) @@ -95,6 +97,10 @@ add_custom_target(lite_compile_deps COMMAND echo 1) # 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 + +set(__lite_cc_files "";"") +set(__lite_cc_files "${CMAKE_BINARY_DIR}/lite_cc_files.txt") +file(WRITE ${__lite_cc_files} "") # clean # cc_library with branch support. # The branches: # X86_DEPS: works only when LITE_WITH_X86 is ON. @@ -126,8 +132,14 @@ function(lite_cc_library TARGET) cc_library(${TARGET} SRCS ${args_SRCS} DEPS ${deps} ${args_DEPS}) endif() + foreach(cc_file ${args_SRCS}) + file(APPEND ${__lite_cc_files} "${cc_file}\n") + endforeach() + # collect targets need to compile for lite - add_dependencies(lite_compile_deps ${TARGET}) + if (args_SRCS) + add_dependencies(lite_compile_deps ${TARGET}) + endif() # register a library name. file(APPEND ${offline_lib_registry_file} "${TARGET}\n") @@ -224,9 +236,9 @@ add_custom_target(publish_inference_cxx_lib ${TARGET} COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/cxx/lib" 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}/paddle/fluid/lite/api/libpaddle_api_full.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib" + COMMAND cp "${CMAKE_BINARY_DIR}/libpaddle_api_full_bundled.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib" ) -add_dependencies(publish_inference_cxx_lib paddle_api_full) +add_dependencies(publish_inference_cxx_lib bundle_full_api) add_dependencies(publish_inference_lite publish_inference_cxx_lib) if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) @@ -235,9 +247,9 @@ if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) add_custom_target(publish_inference_mobile_lib ${TARGET} COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/mobile/lib" COMMAND mkdir -p "${INFER_LITE_PUBLISH_ROOT}/mobile/include" - COMMAND cp "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/api/paddle_*.h" "${INFER_LITE_PUBLISH_ROOT}/cxx/include" - COMMAND cp "${CMAKE_BINARY_DIR}/paddle/fluid/lite/api/libpaddle_api_light.a" "${INFER_LITE_PUBLISH_ROOT}/cxx/lib" + COMMAND cp "${CMAKE_SOURCE_DIR}/paddle/fluid/lite/api/paddle_*.h" "${INFER_LITE_PUBLISH_ROOT}/mobile/include" + COMMAND cp "${CMAKE_BINARY_DIR}/libpaddle_api_light_bundled.a" "${INFER_LITE_PUBLISH_ROOT}/mobile/lib" ) - add_dependencies(publish_inference_mobile_lib paddle_api_light) + add_dependencies(publish_inference_mobile_lib paddle_api_light bundle_light_api) add_dependencies(publish_inference_lite publish_inference_mobile_lib) endif() diff --git a/paddle/fluid/lite/api/CMakeLists.txt b/paddle/fluid/lite/api/CMakeLists.txt index 02c86017089e9600906cc370fbc0a8534f7507aa..15285707c994d77574e5db7f81f6fb258987318c 100644 --- a/paddle/fluid/lite/api/CMakeLists.txt +++ b/paddle/fluid/lite/api/CMakeLists.txt @@ -102,9 +102,20 @@ lite_cc_test(test_apis_lite SRCS apis_test.cc lite_cc_library(paddle_api_lite SRCS paddle_api.cc DEPS op_params_lite) -lite_cc_library(paddle_api_full SRCS cxx_api_impl.cc DEPS cxx_api_lite paddle_api_lite light_api_lite) +#----------------------------------------------------------------------------------------------------- +# The final inference library for both CxxConfig and MobileConfig. +lite_cc_library(paddle_api_full SRCS cxx_api_impl.cc DEPS cxx_api_lite paddle_api_lite light_api_lite + ${ops_lite} + ARM_DEPS ${arm_kernels} + ) +# The final inference library for just MobileConfig. lite_cc_library(paddle_api_light SRCS light_api_impl.cc DEPS light_api_lite paddle_api_lite) +bundle_static_library(paddle_api_full paddle_api_full_bundled bundle_full_api) +bundle_static_library(paddle_api_light paddle_api_light_bundled bundle_light_api) +#----------------------------------------------------------------------------------------------------- + + lite_cc_test(test_paddle_api_lite SRCS paddle_api_test.cc DEPS paddle_api_full paddle_api_light ${ops_lite} ARM_DEPS ${arm_kernels}