From 3b1b41ec0bb70f9224e30c2d3045c4033be7e33b Mon Sep 17 00:00:00 2001 From: youzainn <70636343+youzainn@users.noreply.github.com> Date: Thu, 3 Sep 2020 15:51:28 +0800 Subject: [PATCH] Add some compile options, add vulkan dependency export (#2062) * vulkan cmake export templete * 1) vulkan cmake dependency export. 2) support opencv_world import. 3) add BUILD_WITH_STATIC_CRT option * Threads dependency * NCNN_BUILD_WITH_STATIC_CRT option * we do not support cmake before version 3.15 for option NCNN_BUILD_WITH_STATIC_CRT --- CMakeLists.txt | 13 +++++++++++++ cmake/ncnnConfig.cmake.in | 23 ++++++++++++++++++++++- examples/CMakeLists.txt | 12 ++++++++++-- tools/quantize/CMakeLists.txt | 6 +++++- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ee1fcf8a..ed612bd80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,21 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE release CACHE STRING "Choose the type of build" FORCE) endif() +if(CMAKE_MAJOR_VERSION GREATER_EQUAL 3 AND CMAKE_MINOR_VERSION GREATER_EQUAL 15) + # enable CMAKE_MSVC_RUNTIME_LIBRARY + cmake_policy(SET CMP0091 NEW) +endif() + project(ncnn) +if(MSVC AND CMAKE_MAJOR_VERSION GREATER_EQUAL 3 AND CMAKE_MINOR_VERSION GREATER_EQUAL 15) + option(NCNN_BUILD_WITH_STATIC_CRT "Enables use of statically linked CRT for statically linked ncnn" OFF) + if(NCNN_BUILD_WITH_STATIC_CRT) + # cmake before version 3.15 not work + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") + endif() +endif() + option(NCNN_OPENMP "openmp support" ON) option(NCNN_STDIO "load model from external file" ON) option(NCNN_STRING "plain and verbose string" ON) diff --git a/cmake/ncnnConfig.cmake.in b/cmake/ncnnConfig.cmake.in index 92dc4f85b..aee4225c6 100644 --- a/cmake/ncnnConfig.cmake.in +++ b/cmake/ncnnConfig.cmake.in @@ -1,5 +1,7 @@ set(NCNN_OPENMP @NCNN_OPENMP@) set(NCNN_VULKAN @NCNN_VULKAN@) +set(NCNN_VULKAN_ONLINE_SPIRV @NCNN_VULKAN_ONLINE_SPIRV@) +set(NCNN_SYSTEM_GLSLANG @NCNN_SYSTEM_GLSLANG@) if(NCNN_OPENMP) find_package(OpenMP) @@ -7,6 +9,25 @@ endif() if(NCNN_VULKAN) find_package(Vulkan REQUIRED) -endif() + + if(NCNN_VULKAN_ONLINE_SPIRV) + if(NCNN_SYSTEM_GLSLANG) + set(GLSLANG_TARGET_DIR "@GLSLANG_TARGET_DIR@") + else() + set(GLSLANG_TARGET_DIR "${CMAKE_CURRENT_LIST_DIR}/..") + endif(NCNN_SYSTEM_GLSLANG) + + find_package(Threads) + + include(${GLSLANG_TARGET_DIR}/OSDependentTargets.cmake) + include(${GLSLANG_TARGET_DIR}/OGLCompilerTargets.cmake) + if(EXISTS "${GLSLANG_TARGET_DIR}/HLSLTargets.cmake") + # hlsl support can be optional + include("${GLSLANG_TARGET_DIR}/HLSLTargets.cmake") + endif() + include(${GLSLANG_TARGET_DIR}/glslangTargets.cmake) + include(${GLSLANG_TARGET_DIR}/SPIRVTargets.cmake) + endif(NCNN_VULKAN_ONLINE_SPIRV) +endif(NCNN_VULKAN) include(${CMAKE_CURRENT_LIST_DIR}/ncnn.cmake) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index c75fa69a7..dfaf19ec7 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,18 +1,26 @@ - macro(ncnn_add_example name) add_executable(${name} ${name}.cpp) + target_include_directories(${name} PRIVATE ${OpenCV_INCLUDE_DIRS}) target_link_libraries(${name} PRIVATE ncnn ${OpenCV_LIBS}) # add test to a virtual project group set_property(TARGET ${name} PROPERTY FOLDER "examples") endmacro() -find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs videoio) +find_package(OpenCV QUIET COMPONENTS opencv_world) +if(NOT OpenCV_FOUND) + find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs videoio) +endif() if(NOT OpenCV_FOUND) find_package(OpenCV QUIET COMPONENTS core highgui imgproc) endif() if(OpenCV_FOUND) + message(STATUS "OpenCV library: ${OpenCV_INSTALL_PATH}") + message(STATUS " version: ${OpenCV_VERSION}") + message(STATUS " libraries: ${OpenCV_LIBS}") + message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") + if(${OpenCV_VERSION_MAJOR} GREATER 3) set(CMAKE_CXX_STANDARD 11) endif() diff --git a/tools/quantize/CMakeLists.txt b/tools/quantize/CMakeLists.txt index 63b62930d..4a129d00e 100644 --- a/tools/quantize/CMakeLists.txt +++ b/tools/quantize/CMakeLists.txt @@ -1,4 +1,7 @@ -find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) +find_package(OpenCV QUIET COMPONENTS opencv_world) +if(NOT OpenCV_FOUND) + find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs) +endif() if(NOT OpenCV_FOUND) find_package(OpenCV QUIET COMPONENTS core highgui imgproc) endif() @@ -6,6 +9,7 @@ endif() if(OpenCV_FOUND) add_executable(ncnn2table ncnn2table.cpp) target_compile_definitions(ncnn2table PRIVATE -DOpenCV_VERSION_MAJOR=${OpenCV_VERSION_MAJOR}) + target_include_directories(ncnn2table PRIVATE ${OpenCV_INCLUDE_DIRS}) target_link_libraries(ncnn2table PRIVATE ncnn ${OpenCV_LIBS}) add_executable(ncnn2int8 ncnn2int8.cpp)