Skip to content

Commit

Permalink
[Fix] Fix recent build problems (open-mmlab#544)
Browse files Browse the repository at this point in the history
* simplify deps management

* skip visibility flags for MSVC

* simplify cuda deps

* naming

* workaround for cmake<3.17

* add spdlog dependency

* move the enablement of CUDA to top level CMakeLists.txt

* fix MSVC build

* fix lint

* fix build for backend ops only

* remove comment

* allow to use apis/python as a standalone project

* remove redundant cmake code

* control shared or static lib using `MMDEPLOY_SHARED_LIBS` instead of `BUILD_SHARED_LIBS`

* fix MSVC build

* update docs
  • Loading branch information
lzhangzz authored Jun 6, 2022
1 parent 51f630b commit 567ec62
Show file tree
Hide file tree
Showing 67 changed files with 248 additions and 319 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "third_party/pybind11"]
path = third_party/pybind11
url = https://github.com/pybind/pybind11.git
[submodule "third_party/spdlog"]
path = third_party/spdlog
url = https://github.com/gabime/spdlog.git
57 changes: 41 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
if (NOT DEFINED CMAKE_INSTALL_PREFIX)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "MMDeploy's installation directory")
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "installation directory")
endif ()
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")

Expand All @@ -19,21 +19,28 @@ endif ()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# options
option(BUILD_SHARED_LIBS "build shared lib" ON)
option(MMDEPLOY_BUILD_MODEL_OPTIMIZER "build MMDeploy's model optimizer" ON)
option(MMDEPLOY_BUILD_SDK "build MMDeploy's SDK" OFF)
option(MMDEPLOY_ZIP_MODEL "support sdk model in zip format" OFF)
option(MMDEPLOY_BUILD_SDK_CSHARP_API "build MMDeployExtern.dll for C# API" OFF)
option(MMDEPLOY_BUILD_TEST "build MMDeploy's csrc's unittest" OFF)
set(MMDEPLOY_TARGET_DEVICES
"cpu" CACHE STRING "MMDeploy's target devices")
set(MMDEPLOY_TARGET_BACKENDS "" CACHE STRING "MMDeploy's target inference engines")
set(MMDEPLOY_CODEBASES "all" CACHE STRING "select OpenMMLab's codebases")
option(MMDEPLOY_SHARED_LIBS "build shared libs" ON)
option(MMDEPLOY_BUILD_SDK "build MMDeploy SDK" 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)
option(MMDEPLOY_BUILD_SDK_CSHARP_API "build SDK C# API support" OFF)
option(MMDEPLOY_SPDLOG_EXTERNAL "use external spdlog" OFF)
option(MMDEPLOY_ZIP_MODEL "support SDK model in zip format" OFF)
set(MMDEPLOY_TARGET_DEVICES "cpu" CACHE STRING "target devices to support")
set(MMDEPLOY_TARGET_BACKENDS "" CACHE STRING "target inference engines to support")
set(MMDEPLOY_CODEBASES "all" CACHE STRING "select OpenMMLab codebases")

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "choose 'Release' as default build type" FORCE)
endif ()

if (MMDEPLOY_SHARED_LIBS)
set(MMDEPLOY_LIB_TYPE SHARED)
else ()
set(MMDEPLOY_LIB_TYPE STATIC)
endif ()

# when CUDA devices are enabled, the environment variable ASAN_OPTIONS=protect_shadow_gap=0
# must be set at runtime
if (MMDEPLOY_ASAN_ENABLE)
Expand All @@ -53,16 +60,20 @@ if (MSVC)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/Zc:preprocessor>) # /experimental:preprocessor on VS2017
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus>)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/wd4251>)
else ()
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fvisibility=hidden>)
endif ()

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

add_library(MMDeployStaticModules INTERFACE)
add_library(MMDeployDynamicModules INTERFACE)
add_library(MMDeployLibs INTERFACE)

if ((cuda IN_LIST MMDEPLOY_TARGET_DEVICES) OR (trt IN_LIST MMDEPLOY_TARGET_BACKENDS))
include(cmake/cuda.cmake NO_POLICY_SCOPE)
endif ()

# this must come after including cuda.cmake because policies in function scope is captured
# at function definition
include(cmake/MMDeploy.cmake)

add_subdirectory(csrc)

if (MMDEPLOY_BUILD_SDK)
Expand All @@ -84,6 +95,9 @@ if (MMDEPLOY_BUILD_SDK)
FILE MMDeployTargets.cmake
DESTINATION lib/cmake/MMDeploy)

if (MMDEPLOY_SPDLOG_EXTERNAL)
set(SPDLOG_DEPENDENCY "find_package(spdlog QUIET)")
endif ()
# append backend deps
mmdeploy_add_deps(trt BACKENDS ${MMDEPLOY_TARGET_BACKENDS} DEPS TENSORRT CUDNN)
mmdeploy_add_deps(ort BACKENDS ${MMDEPLOY_TARGET_BACKENDS} DEPS ONNXRUNTIME)
Expand All @@ -110,13 +124,24 @@ if (MMDEPLOY_BUILD_SDK)
${CMAKE_CURRENT_BINARY_DIR}/MMDeployConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/MMDeployConfigVersion.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/MMDeploy.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/loader.cpp.in
DESTINATION lib/cmake/MMDeploy
)

if (MSVC)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/loader.cpp.in
DESTINATION lib/cmake/MMDeploy
)
endif ()

install(DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules
DESTINATION lib/cmake/MMDeploy
)

install(DIRECTORY ${CMAKE_SOURCE_DIR}/demo/csrc/ DESTINATION example)

if (${CMAKE_VERSION} VERSION_LESS "3.17.0")
install(SCRIPT cmake/post-install.cmake)
endif ()
endif ()
30 changes: 23 additions & 7 deletions cmake/MMDeploy.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,18 @@ endfunction ()


function (mmdeploy_add_library NAME)
# EXCLUDE: exclude from registering & exporting
cmake_parse_arguments(_MMDEPLOY "EXCLUDE" "" "" ${ARGN})
add_library(${NAME} ${_MMDEPLOY_UNPARSED_ARGUMENTS})
# search for add_library keywords
cmake_parse_arguments(_TYPE "STATIC;SHARED;MODULE" "" "" ${_MMDEPLOY_UNPARSED_ARGUMENTS})
set(_MAYBE_TYPE)
if (NOT (_TYPE_STATIC OR _TYPE_SHARED OR _TYPE_MODULE))
set(_MAYBE_TYPE ${MMDEPLOY_LIB_TYPE})
endif ()
add_library(${NAME} ${_MAYBE_TYPE} ${_MMDEPLOY_UNPARSED_ARGUMENTS})
if (NOT MSVC)
target_compile_options(${NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fvisibility=hidden>)
endif ()
target_compile_definitions(${NAME} PRIVATE -DMMDEPLOY_API_EXPORTS=1)
get_target_property(_TYPE ${NAME} TYPE)
if (_TYPE STREQUAL STATIC_LIBRARY)
Expand All @@ -36,19 +46,25 @@ function (mmdeploy_add_module NAME)
# 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})
cmake_parse_arguments(_TYPE "STATIC;SHARED;MODULE" "" "" ${_MMDEPLOY_UNPARSED_ARGUMENTS})

set(_MAYBE_MODULE)
set(_MAYBE_TYPE)
# no library type specified
if (NOT (_KW_STATIC OR _KW_SHARED OR _KW_MODULE))
if (NOT (_TYPE_STATIC OR _TYPE_SHARED OR _TYPE_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)
if (MSVC AND MMDEPLOY_SHARED_LIBS AND NOT _MMDEPLOY_LIBRARY)
set(_MAYBE_TYPE MODULE)
else ()
set(_MAYBE_TYPE ${MMDEPLOY_LIB_TYPE})
endif ()
endif ()

add_library(${NAME} ${_MAYBE_MODULE} ${_MMDEPLOY_UNPARSED_ARGUMENTS})
add_library(${NAME} ${_MAYBE_TYPE} ${_MMDEPLOY_UNPARSED_ARGUMENTS})

if (NOT MSVC)
target_compile_options(${NAME} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-fvisibility=hidden>)
endif ()

# automatically link mmdeploy::core if exists
if (TARGET mmdeploy::core)
Expand Down
4 changes: 2 additions & 2 deletions cmake/MMDeployConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set(MMDEPLOY_CODEBASES @MMDEPLOY_CODEBASES@)
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 @BUILD_SHARED_LIBS@)
set(MMDEPLOY_BUILD_SHARED @MMDEPLOY_SHARED_LIBS@)

if (NOT MMDEPLOY_BUILD_SHARED)
if ("cuda" IN_LIST MMDEPLOY_TARGET_DEVICES)
Expand All @@ -26,6 +26,7 @@ endif ()

set(MMDEPLOY_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/modules")
list(APPEND CMAKE_MODULE_PATH ${MMDEPLOY_MODULE_PATH})
@SPDLOG_DEPENDENCY@
@TENSORRT_DEPENDENCY@
@CUDNN_DEPENDENCY@
@ONNXRUNTIME_DEPENDENCY@
Expand All @@ -34,7 +35,6 @@ list(APPEND CMAKE_MODULE_PATH ${MMDEPLOY_MODULE_PATH})
@pplnn_DEPENDENCY@
list(REMOVE_ITEM CMAKE_MODULE_PATH ${MMDEPLOY_MODULE_PATH})

find_package(spdlog REQUIRED)
find_package(OpenCV REQUIRED)

set(THREADS_PREFER_PTHREAD_FLAG ON)
Expand Down
5 changes: 2 additions & 3 deletions cmake/cuda.cmake
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Copyright (c) OpenMMLab. All rights reserved.
#message(STATUS "CMAKE_VERSION: ${CMAKE_VERSION}")

if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.18.0")
# suppress 'CMAKE_CUDA_ARCHITECTURES' warning
cmake_policy(SET CMP0104 OLD)
endif ()

if (MSVC)
if (MSVC OR (NOT DEFINED CMAKE_CUDA_RUNTIME_LIBRARY))
# use shared, on windows, python api can't build with static lib.
set(CMAKE_CUDA_RUNTIME_LIBRARY Shared)
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
endif ()

# nvcc compiler settings
find_package(CUDA REQUIRED)
#message(STATUS "CUDA VERSION: ${CUDA_VERSION_STRING}")

if (MSVC)
set(CMAKE_CUDA_COMPILER ${CUDA_TOOLKIT_ROOT_DIR}/bin/nvcc.exe)
Expand Down
10 changes: 10 additions & 0 deletions cmake/post-install.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


set(_TARGETS_PATH ${CMAKE_INSTALL_PREFIX}/lib/cmake/MMDeploy/MMDeployTargets.cmake)

file(READ ${_TARGETS_PATH} _MMDEPLOY_TARGETS)

string(REGEX REPLACE "::@<0x[a-z0-9]+>" ""
_MMDEPLOY_TARGETS_FIXED "${_MMDEPLOY_TARGETS}")

file(WRITE ${_TARGETS_PATH} "${_MMDEPLOY_TARGETS_FIXED}")
2 changes: 1 addition & 1 deletion cmake/tensorrt.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
include(${CMAKE_SOURCE_DIR}/cmake/cuda.cmake)

include(${CMAKE_SOURCE_DIR}/cmake/modules/FindTENSORRT.cmake)
include(${CMAKE_SOURCE_DIR}/cmake/modules/FindCUDNN.cmake)
find_path(
Expand Down
3 changes: 3 additions & 0 deletions csrc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
add_subdirectory(backend_ops)

if (MMDEPLOY_BUILD_SDK)
# include OpenCV for SDK modules since many of them depends on it
include(${CMAKE_SOURCE_DIR}/cmake/opencv.cmake)

add_subdirectory(core)
add_subdirectory(execution)
add_subdirectory(utils)
Expand Down
4 changes: 1 addition & 3 deletions csrc/apis/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
cmake_minimum_required(VERSION 3.14)
project(capis)

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

set(COMMON_LIST
common
Expand Down
4 changes: 2 additions & 2 deletions csrc/apis/csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To use the nuget package, you also need to download the backend dependencies. Fo

**Step 0.** Build sdk.

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_CSHARP_EXTERN option to ON. We recommend setting `BUILD_SHARED_LIBS` to OFF and use the static third party libraries(spdlog, pplcv, opencv, etc.). If so, you only need add the backend dependencies to your system path, or you need to add all dependencies.
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.

Expand All @@ -42,4 +42,4 @@ dotnet build --configuration Release -p:Version=1.0.0

You can set the package-version through `Properties -> Package Version`. The default version is 1.0.0 if you don't set it.

If you meets some missing dependencies error, follow the vs instructions.
If you encounter missing dependencies, follow the instructions for MSVC.
21 changes: 11 additions & 10 deletions csrc/apis/python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Copyright (c) OpenMMLab. All rights reserved.

cmake_minimum_required(VERSION 3.14)
project(mmdeploy_python)

if ("cuda" IN_LIST MMDEPLOY_TARGET_DEVICES)
include(${CMAKE_SOURCE_DIR}/cmake/cuda.cmake)
endif ()
set(MMDEPLOY_PYTHON_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/common.cpp)

if (NOT TARGET pybind11)
set(CMAKE_CXX_STANDARD 17)

if (${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
add_subdirectory(${CMAKE_SOURCE_DIR}/../../../third_party/pybind11
${CMAKE_CURRENT_BINARY_DIR}/pybind11)
find_package(MMDeploy REQUIRED)
elseif (NOT TARGET pybind11)
add_subdirectory(${CMAKE_SOURCE_DIR}/third_party/pybind11 pybind11)
endif ()

set(MMDEPLOY_PYTHON_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
${CMAKE_CURRENT_SOURCE_DIR}/executor.cpp)

macro(mmdeploy_python_add_module name)
if (TARGET mmdeploy_${name})
list(APPEND MMDEPLOY_PYTHON_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.cpp)
Expand All @@ -36,5 +37,5 @@ mmdeploy_load_dynamic(${PROJECT_NAME} MMDeployDynamicModules)
target_link_libraries(${PROJECT_NAME} PRIVATE MMDeployLibs)

target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../..)
${CMAKE_CURRENT_SOURCE_DIR}/..
${CMAKE_CURRENT_SOURCE_DIR})
8 changes: 5 additions & 3 deletions csrc/apis/python/common.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) OpenMMLab. All rights reserved.

#include "apis/python/common.h"

#include "core/value.h"
#include "common.h"

namespace mmdeploy {

Expand Down Expand Up @@ -34,6 +32,8 @@ mm_mat_t GetMat(const PyImage& img) {
return mat;
}

#if 0

py::object ConvertToPyObject(const Value& value) {
switch (value.type()) {
case ValueType::kNull:
Expand Down Expand Up @@ -102,6 +102,8 @@ Value ConvertToValue(const py::object& obj) {
}
}

#endif

} // namespace mmdeploy

PYBIND11_MODULE(mmdeploy_python, m) {
Expand Down
2 changes: 1 addition & 1 deletion csrc/apis/python/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <stdexcept>

#include "apis/c/common.h"
#include "c/common.h"
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
Expand Down
12 changes: 1 addition & 11 deletions csrc/apis/python/executor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) OpenMMLab. All rights reserved.

#include "apis/python/common.h"
#include "common.h"
#include "core/utils/formatter.h"
#include "execution/execution.h"
#include "execution/schedulers/inlined_scheduler.h"
Expand Down Expand Up @@ -47,16 +47,6 @@ using _python::PySender;
static void register_python_executor(py::module& m) {
py::class_<PySender, std::unique_ptr<PySender>>(m, "PySender")
.def("__await__", &PySender::__await__);

// m.def("test_async", [](const py::object& x) {
// static StaticThreadPool pool;
// TypeErasedScheduler<Value> scheduler{pool.GetScheduler()};
// auto sender = TransferJust(scheduler, ConvertToValue(x)) | Then([](Value x) {
// // std::this_thread::sleep_for(std::chrono::milliseconds(10));
// return Value(x.get<int>() * x.get<int>());
// });
// return std::make_unique<PySender>(std::move(sender));
// });
}

class PythonExecutorRegisterer {
Expand Down
Loading

0 comments on commit 567ec62

Please sign in to comment.