Skip to content

Commit

Permalink
[Feature] support build single sdk library (#806)
Browse files Browse the repository at this point in the history
* build single lib for c api

* update csharp doc & project

* update test build

* fix test build

* fix
  • Loading branch information
irexyc authored Jul 28, 2022
1 parent b763611 commit b6b22a1
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ project(MMDeploy VERSION 0.6.0)

set(CMAKE_CXX_STANDARD 17)

set(MMDEPLOY_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(MMDEPLOY_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(MMDEPLOY_VERSION_PATCH ${PROJECT_VERSION_PATCH})

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
if (MSVC)
Expand All @@ -21,6 +24,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# options
option(MMDEPLOY_SHARED_LIBS "build shared libs" ON)
option(MMDEPLOY_BUILD_SDK "build MMDeploy SDK" OFF)
option(MMDEPLOY_BUILD_SDK_MONOLITHIC "build single lib for SDK API" OFF)
option(MMDEPLOY_BUILD_TEST "build unittests" OFF)
option(MMDEPLOY_BUILD_SDK_PYTHON_API "build SDK Python API" OFF)
option(MMDEPLOY_BUILD_SDK_CXX_API "build SDK C++ API" OFF)
Expand Down
6 changes: 5 additions & 1 deletion cmake/MMDeployConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ set(MMDEPLOY_TARGET_DEVICES @MMDEPLOY_TARGET_DEVICES@)
set(MMDEPLOY_TARGET_BACKENDS @MMDEPLOY_TARGET_BACKENDS@)
set(MMDEPLOY_BUILD_TYPE @CMAKE_BUILD_TYPE@)
set(MMDEPLOY_BUILD_SHARED @MMDEPLOY_SHARED_LIBS@)
set(MMDEPLOY_BUILD_SDK_MONOLITHIC @MMDEPLOY_BUILD_SDK_MONOLITHIC@)
set(MMDEPLOY_VERSION_MAJOR @MMDEPLOY_VERSION_MAJOR@)
set(MMDEPLOY_VERSION_MINOR @MMDEPLOY_VERSION_MINOR@)
set(MMDEPLOY_VERSION_PATCH @MMDEPLOY_VERSION_PATCH@)

if (NOT MMDEPLOY_BUILD_SHARED)
if (NOT MMDEPLOY_BUILD_SHARED AND NOT MMDEPLOY_BUILD_SDK_MONOLITHIC)
if ("cuda" IN_LIST MMDEPLOY_TARGET_DEVICES)
find_package(CUDA REQUIRED)
if(MSVC)
Expand Down
54 changes: 39 additions & 15 deletions csrc/mmdeploy/apis/c/mmdeploy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

project(capis)

set(CAPI_OBJS)

macro(add_object name)
add_library(${name} OBJECT ${ARGN})
set_target_properties(${name} PROPERTIES POSITION_INDEPENDENT_CODE 1)
target_compile_definitions(${name} PRIVATE -DMMDEPLOY_API_EXPORTS=1)
if (NOT MSVC)
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fvisibility=hidden>)
endif ()
target_link_libraries(${name} PRIVATE mmdeploy::core)
set(CAPI_OBJS ${CAPI_OBJS} ${name})
mmdeploy_export(${name})
endmacro()

set(COMMON_LIST
common
model
Expand All @@ -12,8 +26,10 @@ set(TASK_LIST ${MMDEPLOY_TASKS})

foreach (TASK ${COMMON_LIST})
set(TARGET_NAME mmdeploy_${TASK})
mmdeploy_add_library(${TARGET_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}.cpp)
target_link_libraries(${TARGET_NAME} PRIVATE mmdeploy::core)
set(OBJECT_NAME mmdeploy_${TASK}_obj)
add_object(${OBJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}.cpp)
mmdeploy_add_library(${TARGET_NAME})
target_link_libraries(${TARGET_NAME} PRIVATE ${OBJECT_NAME})
target_include_directories(${TARGET_NAME} PUBLIC
$<INSTALL_INTERFACE:include>)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}.h
Expand All @@ -27,24 +43,32 @@ target_link_libraries(mmdeploy_pipeline PUBLIC

foreach (TASK ${TASK_LIST})
set(TARGET_NAME mmdeploy_${TASK})
mmdeploy_add_library(${TARGET_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}.cpp)

target_link_libraries(${TARGET_NAME} PRIVATE
mmdeploy_pipeline mmdeploy::core)
set(OBJECT_NAME mmdeploy_${TASK}_obj)
add_object(${OBJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}.cpp)
mmdeploy_add_library(${TARGET_NAME})
target_link_libraries(${TARGET_NAME} PRIVATE ${OBJECT_NAME}
mmdeploy_pipeline)
target_include_directories(${TARGET_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
$<INSTALL_INTERFACE:include>)
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${TASK}.h
DESTINATION include/mmdeploy)
endforeach ()

if (MMDEPLOY_BUILD_SDK_CSHARP_API)
# build MMDeployExtern.dll just for csharp nuget package.
# no Installation for c/c++ package.
file(GLOB SRCS "*.c" "*.cpp")
add_library(MMDeployExtern SHARED ${SRCS})
target_compile_definitions(MMDeployExtern PRIVATE -DMMDEPLOY_API_EXPORTS=1)
mmdeploy_load_static(MMDeployExtern MMDeployStaticModules)
mmdeploy_load_dynamic(MMDeployExtern MMDeployDynamicModules)
target_link_libraries(MMDeployExtern PRIVATE MMDeployLibs)
if (MMDEPLOY_BUILD_SDK_CSHARP_API OR MMDEPLOY_BUILD_SDK_MONOLITHIC)
add_library(mmdeploy SHARED)
mmdeploy_load_static(mmdeploy MMDeployStaticModules)
mmdeploy_load_dynamic(mmdeploy MMDeployDynamicModules)
target_link_libraries(mmdeploy PRIVATE ${CAPI_OBJS})
target_include_directories(mmdeploy PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>
$<INSTALL_INTERFACE:include>)
set(MMDEPLOY_VERSION ${MMDEPLOY_VERSION_MAJOR}
.${MMDEPLOY_VERSION_MINOR}
.${MMDEPLOY_VERSION_PATCH})
string(REPLACE ";" "" MMDEPLOY_VERSION ${MMDEPLOY_VERSION})
set_target_properties(mmdeploy PROPERTIES
VERSION ${MMDEPLOY_VERSION}
SOVERSION ${MMDEPLOY_VERSION_MAJOR})
mmdeploy_export(mmdeploy)
endif ()
4 changes: 2 additions & 2 deletions csrc/mmdeploy/apis/csharp/MMDeploy/MMDeploy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
</PropertyGroup>

<PropertyGroup>
<MMDeployExternalNativeDlls>$(MSBuildThisFileDirectory)\..\..\..\..\..</MMDeployExternalNativeDlls>
<MMDeployNativeDlls>$(MSBuildThisFileDirectory)\..\..\..\..\..</MMDeployNativeDlls>
</PropertyGroup>
<ItemGroup>
<Content CopyToOutputDirectory="PreserveNewest" Include="$(MMDeployExternalNativeDlls)\build\bin\Release\MMDeployExtern.dll" Pack="true" PackagePath="runtimes\win-x64\native\MMDeployExtern.dll" />
<Content CopyToOutputDirectory="PreserveNewest" Include="$(MMDeployNativeDlls)\build\bin\Release\mmdeploy.dll" Pack="true" PackagePath="runtimes\win-x64\native\mmdeploy.dll" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
/// </summary>
internal static partial class NativeMethods
{
public const string DllExtern = "MMDeployExtern";
public const string DllExtern = "mmdeploy";
}
}
2 changes: 1 addition & 1 deletion csrc/mmdeploy/apis/csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To use the nuget package, you also need to download the backend dependencies. Fo

Before building the c# api, you need to build sdk first. Please follow this [tutorial](../../../docs/en/build/windows.md)/[教程](../../../docs/zh_cn/build/windows.md) to build sdk. Remember to set the MMDEPLOY_BUILD_SDK_CSHARP_API option to ON. We recommend setting `MMDEPLOY_SHARED_LIBS` to OFF and use the static third party libraries(pplcv, opencv, etc.). If so, you only need add the backend dependencies to your system path, or you need to add all dependencies.

If you follow the tutorial, the MMDeployExtern.dll will be built in `build\bin\release`. Make sure the expected dll is in that path or the next step will throw a file-not-exist error.
If you follow the tutorial, the mmdeploy.dll will be built in `build\bin\release`. Make sure the expected dll is in that path or the next step will throw a file-not-exist error.

**Step 1.** Build MMDeploy nuget package.

Expand Down
16 changes: 10 additions & 6 deletions demo/csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ function(add_example task name)
# Disable new dtags so that executables can run even without LD_LIBRARY_PATH set
target_link_libraries(${name} PRIVATE -Wl,--disable-new-dtags)
endif ()
# Load MMDeploy modules
mmdeploy_load_static(${name} MMDeployStaticModules)
mmdeploy_load_dynamic(${name} MMDeployDynamicModules)
# Link to MMDeploy libraries
target_link_libraries(${name} PRIVATE MMDeployLibs ${OpenCV_LIBS})
if (MMDEPLOY_BUILD_SDK_MONOLITHIC)
target_link_libraries(${name} PRIVATE mmdeploy ${OpenCV_LIBS})
else ()
# Load MMDeploy modules
mmdeploy_load_static(${name} MMDeployStaticModules)
mmdeploy_load_dynamic(${name} MMDeployDynamicModules)
# Link to MMDeploy libraries
target_link_libraries(${name} PRIVATE MMDeployLibs ${OpenCV_LIBS})

install(TARGETS ${name} RUNTIME DESTINATION bin)
install(TARGETS ${name} RUNTIME DESTINATION bin)
endif ()
endif ()
endfunction()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ endif ()
mmdeploy_load_static(mmdeploy_tests MMDeployStaticModules)
mmdeploy_load_dynamic(mmdeploy_tests MMDeployDynamicModules)
target_link_libraries(mmdeploy_tests PRIVATE
MMDeployLibs
mmdeploy_transform
mmdeploy_opencv_utils)
MMDeployLibs
mmdeploy_transform
mmdeploy_opencv_utils)

0 comments on commit b6b22a1

Please sign in to comment.