| 1 | |
| 2 | if(NOT CMAKE_VERSION VERSION_LESS "3.15") |
| 3 | # enable CMAKE_MSVC_RUNTIME_LIBRARY |
| 4 | cmake_policy(SET CMP0091 NEW) |
| 5 | endif() |
| 6 | |
| 7 | project(pnnx) |
| 8 | cmake_minimum_required(VERSION 3.12) |
| 9 | |
| 10 | if(POLICY CMP0074) |
| 11 | cmake_policy(SET CMP0074 NEW) |
| 12 | endif() |
| 13 | |
| 14 | if(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() |
| 20 | endif() |
| 21 | |
| 22 | list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
| 23 | include(PNNXPyTorch) |
| 24 | |
| 25 | # c++14 is required for using torch headers |
| 26 | set(CMAKE_CXX_STANDARD 14) |
| 27 | |
| 28 | # set(CMAKE_BUILD_TYPE debug) |
| 29 | # set(CMAKE_BUILD_TYPE relwithdebinfo) |
| 30 | # set(CMAKE_BUILD_TYPE release) |
| 31 | |
| 32 | option(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") |
| 38 | set(TorchVision_DIR "${TorchVision_INSTALL_DIR}/share/cmake/TorchVision") |
| 39 | |
| 40 | find_package(Python3 COMPONENTS Interpreter Development) |
| 41 | |
| 42 | PNNXProbeForPyTorchInstall() |
| 43 | find_package(Torch REQUIRED) |
| 44 | |
| 45 | find_package(TorchVision QUIET) |
| 46 | |
| 47 | message(STATUS "Torch_VERSION = ${Torch_VERSION}") |
| 48 | message(STATUS "Torch_VERSION_MAJOR = ${Torch_VERSION_MAJOR}") |
| 49 | message(STATUS "Torch_VERSION_MINOR = ${Torch_VERSION_MINOR}") |
| 50 | message(STATUS "Torch_VERSION_PATCH = ${Torch_VERSION_PATCH}") |
| 51 | |
| 52 | if(Torch_VERSION VERSION_LESS "1.8") |
| 53 | message(FATAL_ERROR "pnnx only supports PyTorch >= 1.8") |
| 54 | endif() |
| 55 | |
| 56 | if(Torch_VERSION VERSION_GREATER_EQUAL "2.1") |
| 57 | # c++17 is required for using torch 2.1+ headers |
| 58 | set(CMAKE_CXX_STANDARD 17) |
| 59 | endif() |
| 60 | |
| 61 | # find torchvision library |
| 62 | find_library(TORCHVISION_LIBRARY torchvision PATHS "${TorchVision_INSTALL_DIR}/lib" "${TorchVision_INSTALL_DIR}/lib64") |
| 63 | if(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") |
| 74 | else() |
| 75 | message(WARNING "static library ${TORCHVISION_LIBRARY} not found.") |
| 76 | set(TorchVision_FOUND FALSE) |
| 77 | message(WARNING "Building without TorchVision") |
| 78 | endif() |
| 79 | |
| 80 | include_directories(${TORCH_INCLUDE_DIRS}) |
| 81 | |
| 82 | if("${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) |
| 89 | endif() |
| 90 | |
| 91 | if((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() |
| 106 | endif() |
| 107 | |
| 108 | # https://github.com/supertone-inc/onnxruntime-build |
| 109 | set(onnxruntime_INSTALL_DIR "/home/nihui/osd/pnnx/install" CACHE STRING "") |
| 110 | find_library(onnxruntime_LIB NAMES onnxruntime PATHS ${onnxruntime_INSTALL_DIR}/lib64 ${onnxruntime_INSTALL_DIR}/lib) |
| 111 | if(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) |
| 116 | else() |
| 117 | set(onnxruntime_FOUND FALSE) |
| 118 | endif() |
| 119 | |
| 120 | add_subdirectory(src) |
| 121 | |
| 122 | enable_testing() |
| 123 | add_subdirectory(tests) |