diff --git a/cmake/MMDeploy.cmake b/cmake/MMDeploy.cmake index f35ea1e1c6..304c7b1bc1 100644 --- a/cmake/MMDeploy.cmake +++ b/cmake/MMDeploy.cmake @@ -12,9 +12,25 @@ function (mmdeploy_export_impl NAME) RUNTIME DESTINATION bin) endfunction () +function (mmdeploy_add_rpath NAME) + if (MSVC) + return() + elseif(APPLE) + set_target_properties(${NAME} PROPERTIES + INSTALL_RPATH "@loader_path" + BUILD_RPATH "@loader_path") + else () + set_target_properties(${NAME} PROPERTIES + INSTALL_RPATH "\$ORIGIN" + BUILD_RPATH "\$ORIGIN") + target_link_libraries(${NAME} PRIVATE -Wl,--disable-new-dtags) + endif () +endfunction () + macro(mmdeploy_add_net NAME) if (MMDEPLOY_DYNAMIC_BACKEND) mmdeploy_add_library(${NAME} SHARED ${ARGN}) + mmdeploy_add_rpath(${NAME}) # DYNAMIC_BACKEND implies BUILD_SDK_MONOLITHIC mmdeploy_export_impl(${NAME}) target_link_libraries(${PROJECT_NAME} PRIVATE mmdeploy) diff --git a/csrc/mmdeploy/apis/c/CMakeLists.txt b/csrc/mmdeploy/apis/c/CMakeLists.txt index c2028bd8b5..f08fa8cf86 100644 --- a/csrc/mmdeploy/apis/c/CMakeLists.txt +++ b/csrc/mmdeploy/apis/c/CMakeLists.txt @@ -1,6 +1,7 @@ # Copyright (c) OpenMMLab. All rights reserved. project(capis) +include(${CMAKE_SOURCE_DIR}/cmake/MMDeploy.cmake) set(CAPI_OBJS) @@ -80,14 +81,6 @@ if (MMDEPLOY_BUILD_SDK_CSHARP_API OR MMDEPLOY_BUILD_SDK_MONOLITHIC) set_target_properties(mmdeploy PROPERTIES VERSION ${MMDEPLOY_VERSION} SOVERSION ${MMDEPLOY_VERSION_MAJOR}) - if (APPLE) - set_target_properties(mmdeploy PROPERTIES - INSTALL_RPATH "@loader_path" - BUILD_RPATH "@loader_path") - else () - set_target_properties(mmdeploy PROPERTIES - INSTALL_RPATH "\$ORIGIN" - BUILD_RPATH "\$ORIGIN") - endif () + mmdeploy_add_rpath(mmdeploy) mmdeploy_export_impl(mmdeploy) endif () diff --git a/csrc/mmdeploy/backend_ops/onnxruntime/CMakeLists.txt b/csrc/mmdeploy/backend_ops/onnxruntime/CMakeLists.txt index 19c21a48cf..9548110be6 100644 --- a/csrc/mmdeploy/backend_ops/onnxruntime/CMakeLists.txt +++ b/csrc/mmdeploy/backend_ops/onnxruntime/CMakeLists.txt @@ -3,6 +3,7 @@ project(mmdeploy_onnxruntime_ops) include(${CMAKE_SOURCE_DIR}/cmake/modules/FindONNXRUNTIME.cmake) +include(${CMAKE_SOURCE_DIR}/cmake/MMDeploy.cmake) # add plugin source file(GLOB_RECURSE ORT_OPS_SRCS *.cpp) @@ -22,15 +23,7 @@ target_link_libraries(${PROJECT_NAME}_obj PUBLIC onnxruntime) mmdeploy_add_library(${PROJECT_NAME} SHARED EXCLUDE "") target_link_libraries(${PROJECT_NAME} PUBLIC ${PROJECT_NAME}_obj) -if (APPLE) - set_target_properties(${PROJECT_NAME} PROPERTIES - BUILD_RPATH "@loader_path" - INSTALL_RPATH "@loader_path") -else () - set_target_properties(${PROJECT_NAME} PROPERTIES - BUILD_RPATH "\$ORIGIN" - INSTALL_RPATH "\$ORIGIN") -endif () +mmdeploy_add_rpath(${PROJECT_NAME}) add_library(mmdeploy::onnxruntime::ops ALIAS ${PROJECT_NAME}) set(_ORT_OPS_DIR ${CMAKE_SOURCE_DIR}/mmdeploy/lib) diff --git a/csrc/mmdeploy/model/CMakeLists.txt b/csrc/mmdeploy/model/CMakeLists.txt index f6f3ec63b5..01cbe77076 100644 --- a/csrc/mmdeploy/model/CMakeLists.txt +++ b/csrc/mmdeploy/model/CMakeLists.txt @@ -5,18 +5,102 @@ project(mmdeploy_model) set(MODEL_NAMES "directory_model") if (${MMDEPLOY_ZIP_MODEL}) set(MODEL_NAMES ${MODEL_NAMES} "zip_model") + + if (MSVC) + set(zlib zlibstatic) + set(LIB_PREFIX "") + set(LIB_SUFFIX ".lib") + else() + set(zlib z) + set(LIB_PREFIX "lib") + set(LIB_SUFFIX ".a") + endif() + set(ziplib zip) + + set(zlib_name ${LIB_PREFIX}${zlib}${LIB_SUFFIX}) + set(ziplib_name ${LIB_PREFIX}${ziplib}${LIB_SUFFIX}) + + include(ExternalProject) + set(ZLIB_BUILD_DIR ${CMAKE_BINARY_DIR}/zlib-build) + set(ZLIB_INSTALL_DIR ${CMAKE_BINARY_DIR}/zlib-install) + set(ZLIB_INCLUDE_DIR ${ZLIB_INSTALL_DIR}/include) + set(ZLIB_LIBRARY_DIR ${ZLIB_INSTALL_DIR}/lib) + ExternalProject_Add( + zlib-external + GIT_REPOSITORY https://github.com/madler/zlib + GIT_TAG v1.2.13 + CMAKE_ARGS + -DCMAKE_INSTALL_PREFIX=${ZLIB_INSTALL_DIR} + -DCMAKE_POSITION_INDEPENDENT_CODE=ON + UPDATE_DISCONNECTED 1 + ) + add_custom_target(static_zlib ALL + COMMAND ${CMAKE_COMMAND} -E remove ${ZLIB_LIBRARY_DIR}/zlib.lib + COMMAND ${CMAKE_COMMAND} -E remove ${ZLIB_LIBRARY_DIR}/libz.so + COMMAND ${CMAKE_COMMAND} -E remove ${ZLIB_LIBRARY_DIR}/libz.dylib + ) + add_dependencies(static_zlib zlib-external) + + add_library(${zlib} STATIC IMPORTED) + set_target_properties(${zlib} + PROPERTIES IMPORTED_LOCATION ${ZLIB_LIBRARY_DIR}/${zlib_name}) + add_dependencies(${zlib} zlib-external) + + set(LIBZIP_BUILD_DIR ${CMAKE_BINARY_DIR}/libzip-build) + set(LIBZIP_INSTALL_DIR ${CMAKE_BINARY_DIR}/libzip-install) + set(LIBZIP_INCLUDE_DIR ${LIBZIP_INSTALL_DIR}/include) + set(LIBZIP_LIBRARY_DIR ${LIBZIP_INSTALL_DIR}/lib) + ExternalProject_Add( + libzip-external + GIT_REPOSITORY https://github.com/nih-at/libzip + GIT_TAG v1.9.2 + CMAKE_ARGS + -DZLIB_ROOT=${ZLIB_INSTALL_DIR} + -DCMAKE_INSTALL_PREFIX=${LIBZIP_INSTALL_DIR} + -DCMAKE_INSTALL_LIBDIR=lib + -DCMAKE_POSITION_INDEPENDENT_CODE=ON + -DBUILD_SHARED_LIBS=OFF + -DLIBZIP_DO_INSTALL=ON + -DBUILD_TOOLS=OFF + -DBUILD_DOC=OFF + -DBUILD_REGRESS=OFF + -DBUILD_EXAMPLES=OFF + -DENABLE_OPENSSL=OFF + -DENABLE_COMMONCRYPTO=OFF + -DENABLE_GNUTLS=OFF + -DENABLE_MBEDTLS=OFF + -DENABLE_WINDOWS_CRYPTO=OFF + -DENABLE_BZIP2=OFF + -DENABLE_LZMA=OFF + -DENABLE_ZSTD=OFF + PREFIX libzip + BINARY_DIR ${LIBZIP_BUILD_DIR} + # INSTALL_COMMAND "" + UPDATE_DISCONNECTED 1 + ) + add_library(${ziplib} STATIC IMPORTED) + set_target_properties(${ziplib} + PROPERTIES IMPORTED_LOCATION ${LIBZIP_LIBRARY_DIR}/${ziplib_name}) + add_dependencies(libzip-external static_zlib) + add_dependencies(${ziplib} libzip-external) + set(ziplibs ${ziplib} ${zlib}) + if (NOT MMDEPLOY_BUILD_SDK_MONOLITHIC) + install(FILES ${LIBZIP_LIBRARY_DIR}/${ziplib_name} + DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) + install(FILES ${ZLIB_LIBRARY_DIR}/${zlib_name} + DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) + endif() endif () foreach (MODEL_NAME ${MODEL_NAMES}) set(TARGET_MODEL_NAME mmdeploy_${MODEL_NAME}) mmdeploy_add_module(${TARGET_MODEL_NAME} ${MODEL_NAME}_impl.cpp) if (${MODEL_NAME} STREQUAL "zip_model") - find_package(libzip QUIET) - if (libzip_FOUND) - target_link_libraries(${TARGET_MODEL_NAME} PUBLIC libzip::zip) - else () - target_link_libraries(${TARGET_MODEL_NAME} PUBLIC zip) - endif () + target_link_libraries(${TARGET_MODEL_NAME} PRIVATE ${ziplibs}) + target_link_directories(${TARGET_MODEL_NAME} INTERFACE + $) + target_include_directories(${TARGET_MODEL_NAME} PRIVATE + $) endif () add_library(mmdeploy::${MODEL_NAME} ALIAS ${TARGET_MODEL_NAME}) endforeach () diff --git a/tools/package_tools/generate_build_config.py b/tools/package_tools/generate_build_config.py index 1040bb73fd..14f25660ed 100644 --- a/tools/package_tools/generate_build_config.py +++ b/tools/package_tools/generate_build_config.py @@ -121,6 +121,7 @@ def generate_config(args): cmake_cfg[ 'MMDEPLOY_DYNAMIC_BACKEND'] = 'ON' \ if args.sdk_dynamic_net else 'OFF' + cmake_cfg['MMDEPLOY_ZIP_MODEL'] = 'ON' if args.opencv_dir: cmake_cfg['OpenCV_DIR'] = args.opencv_dir