Elffuss BraidHub

kikocis/ncnn public

master · 4.4 KiBRawPermalink
1
2if(NOT CMAKE_VERSION VERSION_LESS "3.15")
3 # enable CMAKE_MSVC_RUNTIME_LIBRARY
4 cmake_policy(SET CMP0091 NEW)
5endif()
6
7project(pnnx)
8cmake_minimum_required(VERSION 3.12)
9
10if(POLICY CMP0074)
11 cmake_policy(SET CMP0074 NEW)
12endif()
13
14if(MSVC AND NOT CMAKE_VERSION VERSION_LESS "3.15")
15 option(PNNX_BUILD_WITH_STATIC_CRT "Enables use of statically linked CRT for statically linked pnnx" OFF)
16 if(PNNX_BUILD_WITH_STATIC_CRT)
17 # cmake before version 3.15 not work
18 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
19 endif()
20endif()
21
22list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
23include(PNNXPyTorch)
24
25# c++14 is required for using torch headers
26set(CMAKE_CXX_STANDARD 14)
27
28# set(CMAKE_BUILD_TYPE debug)
29# set(CMAKE_BUILD_TYPE relwithdebinfo)
30# set(CMAKE_BUILD_TYPE release)
31
32option(PNNX_COVERAGE "build for coverage" OFF)
33
34# set(Torch_INSTALL_DIR "/home/nihui/osd/pnnx/install" CACHE STRING "")
35# set(TorchVision_INSTALL_DIR "/home/nihui/osd/pnnx/install" CACHE STRING "")
36
37# set(Torch_DIR "${Torch_INSTALL_DIR}/share/cmake/Torch")
38set(TorchVision_DIR "${TorchVision_INSTALL_DIR}/share/cmake/TorchVision")
39
40find_package(Python3 COMPONENTS Interpreter Development)
41
42PNNXProbeForPyTorchInstall()
43find_package(Torch REQUIRED)
44
45find_package(TorchVision QUIET)
46
47message(STATUS "Torch_VERSION = ${Torch_VERSION}")
48message(STATUS "Torch_VERSION_MAJOR = ${Torch_VERSION_MAJOR}")
49message(STATUS "Torch_VERSION_MINOR = ${Torch_VERSION_MINOR}")
50message(STATUS "Torch_VERSION_PATCH = ${Torch_VERSION_PATCH}")
51
52if(Torch_VERSION VERSION_LESS "1.8")
53 message(FATAL_ERROR "pnnx only supports PyTorch >= 1.8")
54endif()
55
56if(Torch_VERSION VERSION_GREATER_EQUAL "2.1")
57 # c++17 is required for using torch 2.1+ headers
58 set(CMAKE_CXX_STANDARD 17)
59endif()
60
61# find torchvision library
62find_library(TORCHVISION_LIBRARY torchvision PATHS "${TorchVision_INSTALL_DIR}/lib" "${TorchVision_INSTALL_DIR}/lib64")
63if(TORCHVISION_LIBRARY)
64 message(STATUS "Found TorchVision: ${TORCHVISION_LIBRARY}")
65 if(APPLE)
66 set(TORCHVISION_LIBRARY "-Wl,-force_load,${TORCHVISION_LIBRARY}")
67 elseif(MSVC)
68 set(TORCHVISION_LIBRARY "-WHOLEARCHIVE:${TORCHVISION_LIBRARY}")
69 else()
70 set(TORCHVISION_LIBRARY "-Wl,--whole-archive ${TORCHVISION_LIBRARY} -Wl,--no-whole-archive")
71 endif()
72 set(TorchVision_FOUND TRUE)
73 message(STATUS "Building with TorchVision")
74else()
75 message(WARNING "static library ${TORCHVISION_LIBRARY} not found.")
76 set(TorchVision_FOUND FALSE)
77 message(WARNING "Building without TorchVision")
78endif()
79
80include_directories(${TORCH_INCLUDE_DIRS})
81
82if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
83 # test if libtorch and protobuf has the same cxxabi version
84 include(CheckCXXSourceCompiles)
85 set(CMAKE_REQUIRED_FLAGS "${TORCH_CXX_FLAGS}")
86 check_cxx_source_compiles("#include <cxxabi.h>\n#if _GLIBCXX_USE_CXX11_ABI\nint main() { return 0; }\n#endif" PNNX_TORCH_USE_CXX11_ABI)
87 unset(CMAKE_REQUIRED_FLAGS)
88 check_cxx_source_compiles("#include <cxxabi.h>\n#if _GLIBCXX_USE_CXX11_ABI\nint main() { return 0; }\n#endif" PNNX_COMPILER_USE_CXX11_ABI)
89endif()
90
91if((PNNX_TORCH_USE_CXX11_ABI AND PNNX_COMPILER_USE_CXX11_ABI) OR (NOT PNNX_TORCH_USE_CXX11_ABI AND NOT PNNX_COMPILER_USE_CXX11_ABI))
92 find_package(protobuf CONFIG)
93
94 if(protobuf_FOUND)
95 set(PROTOBUF_FOUND ${protobuf_FOUND})
96 set(PROTOBUF_VERSION ${protobuf_VERSION})
97 else()
98 # fallback to system
99 find_package(Protobuf)
100 set(PROTOBUF_FOUND ${Protobuf_FOUND})
101 set(PROTOBUF_VERSION ${Protobuf_VERSION})
102 if(TARGET protobuf::protoc)
103 set_target_properties(protobuf::protoc PROPERTIES IMPORTED_LOCATION_RELEASE "${PROTOBUF_PROTOC_EXECUTABLE}")
104 endif()
105 endif()
106endif()
107
108# https://github.com/supertone-inc/onnxruntime-build
109set(onnxruntime_INSTALL_DIR "/home/nihui/osd/pnnx/install" CACHE STRING "")
110find_library(onnxruntime_LIB NAMES onnxruntime PATHS ${onnxruntime_INSTALL_DIR}/lib64 ${onnxruntime_INSTALL_DIR}/lib)
111if(onnxruntime_LIB)
112 set(onnxruntime_FOUND TRUE)
113 add_library(onnxruntime::onnxruntime STATIC IMPORTED)
114 set_target_properties(onnxruntime::onnxruntime PROPERTIES IMPORTED_LOCATION ${onnxruntime_LIB})
115 set_target_properties(onnxruntime::onnxruntime PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${onnxruntime_INSTALL_DIR}/include)
116else()
117 set(onnxruntime_FOUND FALSE)
118endif()
119
120add_subdirectory(src)
121
122enable_testing()
123add_subdirectory(tests)