mmdeploy/cmake/MMDeploy.cmake
lzhangzz 640aa03538
Support Windows (#106)
* minor changes

* support windows

* fix GCC build

* fix lint

* reformat

* fix Windows build

* fix GCC build

* search backend ops for onnxruntime

* fix lint

* fix lint

* code clean-up

* code clean-up

* fix clang build

* fix trt support

* fix cmake for ncnn

* fix cmake for openvino

* fix SDK Python API

* handle ops for other backends (ncnn, trt)

* handle SDK Python API library location

* robustify linkage

* fix cuda

* minor fix for openvino & ncnn

* use CMAKE_CUDA_ARCHITECTURES if set

* fix cuda preprocessor

* fix misc

* fix pplnn & pplcv, drop support for pplcv<0.6.0

* robustify cmake

* update build.md (#2)

* build dynamic modules as module library & fix demo (partially)

* fix candidate path for mmdeploy_python

* move "enable CUDA" to cmake config for demo

* refine demo cmake

* add comment

* fix ubuntu build

* revert docs/en/build.md

* fix C API

* fix lint

* Windows build doc (#3)

* check in docs related to mmdeploy build on windows

* update build guide on windows platform

* update build guide on windows platform

* make path of thirdparty libraries consistent

* make path consistency

* correct build command for custom ops

* correct build command for sdk

* update sdk build instructions

* update doc

* correct build command

* fix lint

* correct build command and fix lint

Co-authored-by: lvhan <lvhan@pjlab.org>

* trailing whitespace (#4)

* minor fix

* fix sr sdk model

* fix type deduction

* fix cudaFree after driver shutting down

* update ppl.cv installation warning (#5)

* fix device allocator threshold & fix lint

* update doc (#6)

* update ppl.cv installation warning

* missing 'git clone'

Co-authored-by: chenxin <chenxin2@sensetime.com>
Co-authored-by: zhangli <zhangli@sensetime.com>
Co-authored-by: lvhan028 <lvhan_028@163.com>
Co-authored-by: lvhan <lvhan@pjlab.org>
2022-02-24 20:08:44 +08:00

152 lines
5.2 KiB
CMake

# Copyright (c) OpenMMLab. All rights reserved.
function (mmdeploy_export NAME)
set(_LIB_DIR lib)
if (MSVC)
set(_LIB_DIR bin)
endif ()
install(TARGETS ${NAME}
EXPORT MMDeployTargets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION ${_LIB_DIR}
RUNTIME DESTINATION bin)
endfunction ()
function (mmdeploy_add_library NAME)
cmake_parse_arguments(_MMDEPLOY "EXCLUDE" "" "" ${ARGN})
add_library(${NAME} ${_MMDEPLOY_UNPARSED_ARGUMENTS})
target_compile_definitions(${NAME} PRIVATE -DMMDEPLOY_API_EXPORTS=1)
get_target_property(_TYPE ${NAME} TYPE)
if (_TYPE STREQUAL STATIC_LIBRARY)
set_target_properties(${NAME} PROPERTIES POSITION_INDEPENDENT_CODE 1)
elseif (_TYPE STREQUAL SHARED_LIBRARY)
else ()
message(FATAL_ERROR "unsupported type: ${_TYPE}")
endif ()
if (NOT _MMDEPLOY_EXCLUDE)
target_link_libraries(MMDeployLibs INTERFACE ${NAME})
mmdeploy_export(${NAME})
endif ()
endfunction ()
function (mmdeploy_add_module NAME)
# EXCLUDE: exclude from registering & exporting as SDK module
# LIBRARY: the module is also a library (add_libray with SHARED instead of MODULE)
cmake_parse_arguments(_MMDEPLOY "EXCLUDE;LIBRARY" "" "" ${ARGN})
# search for add_library keywords
cmake_parse_arguments(_KW "STATIC;SHARED;MODULE" "" "" ${_MMDEPLOY_UNPARSED_ARGUMENTS})
set(_MAYBE_MODULE)
# no library type specified
if (NOT (_KW_STATIC OR _KW_SHARED OR _KW_MODULE))
# shared but not marked as a library, build module library so that no .lib dependency
# will be generated for MSVC
if (MSVC AND BUILD_SHARED_LIBS AND NOT _MMDEPLOY_LIBRARY)
set(_MAYBE_MODULE MODULE)
endif ()
endif ()
add_library(${NAME} ${_MAYBE_MODULE} ${_MMDEPLOY_UNPARSED_ARGUMENTS})
# automatically link mmdeploy::core if exists
if (TARGET mmdeploy::core)
target_link_libraries(${NAME} PRIVATE mmdeploy::core)
endif ()
# export public symbols when marked as a library
if (_MMDEPLOY_LIBRARY)
target_compile_definitions(${NAME} PRIVATE -DMMDEPLOY_API_EXPORTS=1)
endif ()
get_target_property(_TYPE ${NAME} TYPE)
if (_TYPE STREQUAL STATIC_LIBRARY)
set_target_properties(${NAME} PROPERTIES POSITION_INDEPENDENT_CODE 1)
if (MSVC)
target_link_options(${NAME} INTERFACE "/WHOLEARCHIVE:${NAME}")
endif ()
# register static modules
if (NOT _MMDEPLOY_EXCLUDE)
target_link_libraries(MMDeployStaticModules INTERFACE ${NAME})
endif ()
elseif (_TYPE STREQUAL SHARED_LIBRARY OR _TYPE STREQUAL MODULE_LIBRARY)
# register dynamic modules
if (NOT _MMDEPLOY_EXCLUDE)
target_link_libraries(MMDeployDynamicModules INTERFACE ${NAME})
endif ()
else ()
message(FATAL_ERROR "unsupported type: ${_TYPE}")
endif ()
if (NOT _MMDEPLOY_EXCLUDE)
mmdeploy_export(${NAME})
endif ()
endfunction ()
function (_mmdeploy_flatten_modules RETVAL)
set(_RETVAL)
foreach (ARG IN LISTS ARGN)
get_target_property(TYPE ${ARG} TYPE)
if (TYPE STREQUAL "INTERFACE_LIBRARY")
get_target_property(LIBS ${ARG} INTERFACE_LINK_LIBRARIES)
if (LIBS)
# pattern for 3.17+
list(FILTER LIBS EXCLUDE REGEX "^::@")
# pattern for 3.13-3.16
list(TRANSFORM LIBS REPLACE "(.+)::@.*" "\\1")
list(APPEND _RETVAL ${LIBS})
endif ()
else ()
list(APPEND _RETVAL ${ARG})
endif ()
endforeach ()
set(${RETVAL} ${_RETVAL} PARENT_SCOPE)
endfunction ()
function (mmdeploy_load_static NAME)
if (MSVC)
target_link_libraries(${NAME} PRIVATE ${ARGN})
else ()
_mmdeploy_flatten_modules(_MODULE_LIST ${ARGN})
target_link_libraries(${NAME} PRIVATE
-Wl,--whole-archive
${_MODULE_LIST}
-Wl,--no-whole-archive)
endif ()
endfunction ()
function (mmdeploy_load_dynamic NAME)
_mmdeploy_flatten_modules(_MODULE_LIST ${ARGN})
if (MSVC)
if (NOT _MODULE_LIST)
return ()
endif ()
# MSVC has nothing like "-Wl,--no-as-needed ... -Wl,--as-needed", as a
# workaround we build a static module which loads the dynamic modules
set(_MODULE_STR ${_MODULE_LIST})
list(TRANSFORM _MODULE_STR REPLACE "(.+)" "\"\\1\"")
string(JOIN ",\n " _MODULE_STR ${_MODULE_STR})
set(_MMDEPLOY_DYNAMIC_MODULES ${_MODULE_STR})
set(_LOADER_NAME ${NAME}_loader)
add_dependencies(${NAME} ${_MODULE_LIST})
set(_LOADER_PATH ${CMAKE_BINARY_DIR}/${_LOADER_NAME}.cpp)
# ! CMAKE_CURRENT_FUNCTION_LIST_DIR requires cmake 3.17+
configure_file(
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/loader.cpp.in
${_LOADER_PATH})
mmdeploy_add_module(${_LOADER_NAME} STATIC EXCLUDE ${_LOADER_PATH})
mmdeploy_load_static(${NAME} ${_LOADER_NAME})
else ()
target_link_libraries(${NAME} PRIVATE
-Wl,--no-as-needed
${_MODULE_LIST}
-Wl,--as-needed)
endif ()
endfunction ()