Skip to content

Commit

Permalink
[Feature] Enable read zip model in prebuild package. (#2185)
Browse files Browse the repository at this point in the history
* support libzip

* support windows

* fix

* update rpath

* default build sdk zip model
  • Loading branch information
irexyc authored Jun 28, 2023
1 parent 39c3b75 commit d7489c8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 24 deletions.
16 changes: 16 additions & 0 deletions cmake/MMDeploy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 2 additions & 9 deletions csrc/mmdeploy/apis/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.

project(capis)
include(${CMAKE_SOURCE_DIR}/cmake/MMDeploy.cmake)

set(CAPI_OBJS)

Expand Down Expand Up @@ -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 ()
11 changes: 2 additions & 9 deletions csrc/mmdeploy/backend_ops/onnxruntime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
96 changes: 90 additions & 6 deletions csrc/mmdeploy/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
$<INSTALL_INTERFACE:lib>)
target_include_directories(${TARGET_MODEL_NAME} PRIVATE
$<BUILD_INTERFACE:${LIBZIP_INCLUDE_DIR}>)
endif ()
add_library(mmdeploy::${MODEL_NAME} ALIAS ${TARGET_MODEL_NAME})
endforeach ()
1 change: 1 addition & 0 deletions tools/package_tools/generate_build_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d7489c8

Please sign in to comment.