From f28dc9a68d9db8f711410a60fc57030cfc68ad6e Mon Sep 17 00:00:00 2001 From: Luo Tao Date: Tue, 6 Feb 2018 22:13:27 +0800 Subject: [PATCH] refine inference_lib.cmake --- cmake/inference_lib.cmake | 114 ++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/cmake/inference_lib.cmake b/cmake/inference_lib.cmake index d71fbce3820..7d535543584 100644 --- a/cmake/inference_lib.cmake +++ b/cmake/inference_lib.cmake @@ -1,72 +1,88 @@ # make package for paddle fluid shared and static library +function(copy TARGET) + set(options "") + set(oneValueArgs "") + set(multiValueArgs SRCS DSTS DEPS) + 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) + 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") + + 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) + add_custom_command(TARGET ${TARGET} PRE_BUILD COMMAND mkdir -p "${dst}") + if(IS_DIRECTORY ${src}) + add_custom_command(TARGET ${TARGET} PRE_BUILD COMMAND cp -r "${src}" "${dst}") + else() + add_custom_command(TARGET ${TARGET} PRE_BUILD COMMAND cp "${src}" "${dst}") + endif() + endforeach() +endfunction() + # third party -set(lib_dir "${CMAKE_INSTALL_PREFIX}/third_party/eigen3") -add_custom_target(eigen3_lib - COMMAND mkdir -p "${lib_dir}/Eigen" "${lib_dir}/unsupported" - COMMAND cp "${EIGEN_INCLUDE_DIR}/Eigen/Core" "${lib_dir}/Eigen" - COMMAND cp -r "${EIGEN_INCLUDE_DIR}/Eigen/src" "${lib_dir}/Eigen" - COMMAND cp -r "${EIGEN_INCLUDE_DIR}/unsupported/Eigen" "${lib_dir}/unsupported" +set(dst_dir "${CMAKE_INSTALL_PREFIX}/third_party/eigen3") +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 ) -set(lib_dir "${CMAKE_INSTALL_PREFIX}/third_party/install/gflags") -add_custom_target(gflags_lib - COMMAND mkdir -p "${lib_dir}/lib" - COMMAND cp -r "${GFLAGS_INCLUDE_DIR}" "${lib_dir}" - COMMAND cp "${GFLAGS_LIBRARIES}" "${lib_dir}/lib" +set(dst_dir "${CMAKE_INSTALL_PREFIX}/third_party/install/gflags") +copy(gflags_lib + SRCS ${GFLAGS_INCLUDE_DIR} ${GFLAGS_LIBRARIES} + DSTS ${dst_dir} ${dst_dir}/lib ) -set(lib_dir "${CMAKE_INSTALL_PREFIX}/third_party/install/glog") -add_custom_target(glog_lib - COMMAND mkdir -p "${lib_dir}/lib" - COMMAND cp -r "${GLOG_INCLUDE_DIR}" "${lib_dir}" - COMMAND cp "${GLOG_LIBRARIES}" "${lib_dir}/lib" +set(dst_dir "${CMAKE_INSTALL_PREFIX}/third_party/install/glog") +copy(glog_lib + SRCS ${GLOG_INCLUDE_DIR} ${GLOG_LIBRARIES} + DSTS ${dst_dir} ${dst_dir}/lib ) IF(NOT PROTOBUF_FOUND) - set(lib_dir "${CMAKE_INSTALL_PREFIX}/third_party/install/protobuf") - add_custom_target(protobuf_lib - COMMAND mkdir -p "${lib_dir}/lib" - COMMAND cp -r "${PROTOBUF_INCLUDE_DIR}" "${lib_dir}" - COMMAND cp "${PROTOBUF_LITE_LIBRARY}" "${lib_dir}/lib" + set(dst_dir "${CMAKE_INSTALL_PREFIX}/third_party/install/protobuf") + copy(protobuf_lib + SRCS ${PROTOBUF_INCLUDE_DIR} ${PROTOBUF_LITE_LIBRARY} + DSTS ${dst_dir} ${dst_dir}/lib ) ENDIF(NOT PROTOBUF_FOUND) # paddle fluid module -set(lib_dir "${CMAKE_INSTALL_PREFIX}/paddle/framework") -add_custom_target(framework_lib DEPENDS framework_py_proto - COMMAND mkdir -p "${lib_dir}/details" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/framework/*.h" "${lib_dir}" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/framework/details/*.h" "${lib_dir}/details" - COMMAND cp "${PADDLE_BINARY_DIR}/paddle/framework/framework.pb.h" "${lib_dir}" +set(src_dir "${PADDLE_SOURCE_DIR}/paddle") +set(dst_dir "${CMAKE_INSTALL_PREFIX}/paddle") +set(module "framework") +copy(framework_lib DEPS framework_py_proto + SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/framework/framework.pb.h + DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module} ) -set(lib_dir "${CMAKE_INSTALL_PREFIX}/paddle/memory") -add_custom_target(memory_lib - COMMAND mkdir -p "${lib_dir}/detail" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/memory/*.h" "${lib_dir}" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/memory/detail/*.h" "${lib_dir}/detail" +set(module "memory") +copy(memory_lib + SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/detail/*.h + DSTS ${dst_dir}/${module} ${dst_dir}/${module}/detail ) -set(lib_dir "${CMAKE_INSTALL_PREFIX}/paddle/inference") -add_custom_target(inference_lib DEPENDS paddle_fluid_shared - COMMAND mkdir -p "${lib_dir}" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/inference/*.h" "${lib_dir}" - COMMAND cp "${PADDLE_BINARY_DIR}/paddle/inference/libpaddle_fluid.so" "${lib_dir}" +set(module "inference") +copy(inference_lib DEPENDS paddle_fluid_shared + SRCS ${src_dir}/${module}/*.h ${PADDLE_BINARY_DIR}/paddle/inference/libpaddle_fluid.so + DSTS ${dst_dir}/${module} ${dst_dir}/${module} ) -set(lib_dir "${CMAKE_INSTALL_PREFIX}/paddle/platform") -add_custom_target(platform_lib - COMMAND mkdir -p "${lib_dir}/dynload" "${lib_dir}/details" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/platform/*.h" "${lib_dir}" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/platform/dynload/*.h" "${lib_dir}/dynload" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/platform/details/*.h" "${lib_dir}/details" -) +set(module "platform") +copy(platform_lib + 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 +) -set(lib_dir "${CMAKE_INSTALL_PREFIX}/paddle/string") -add_custom_target(string_lib - COMMAND mkdir -p "${lib_dir}/tinyformat" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/string/*.h" "${lib_dir}" - COMMAND cp "${PADDLE_SOURCE_DIR}/paddle/string/tinyformat/*.h" "${lib_dir}/tinyformat" +set(module "string") +copy(string_lib + SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/tinyformat/*.h + DSTS ${dst_dir}/${module} ${dst_dir}/${module}/tinyformat ) add_custom_target(inference_lib_dist DEPENDS -- GitLab