Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix build with new protobuf target for pnnx #5034

Merged
merged 3 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tools/pnnx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ set(TorchVision_INSTALL_DIR "/home/nihui/osd/vision/build/install" CACHE STRING
#set(Torch_DIR "${Torch_INSTALL_DIR}/share/cmake/Torch")
set(TorchVision_DIR "${TorchVision_INSTALL_DIR}/share/cmake/TorchVision")

find_package(protobuf CONFIG)

if(protobuf_FOUND)
set(PROTOBUF_FOUND ${protobuf_FOUND})
set(PROTOBUF_VERSION ${protobuf_VERSION})
else()
# fallback to system
find_package(Protobuf)
set(PROTOBUF_FOUND ${Protobuf_FOUND})
set(PROTOBUF_VERSION ${Protobuf_VERSION})
if(TARGET protobuf::protoc)
set_target_properties(protobuf::protoc PROPERTIES IMPORTED_LOCATION_RELEASE "${PROTOBUF_PROTOC_EXECUTABLE}")
endif()
endif()

find_package(Python3 COMPONENTS Interpreter Development)

PNNXProbeForPyTorchInstall()
Expand Down
49 changes: 33 additions & 16 deletions tools/pnnx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,23 +557,40 @@ set(pnnx_pass_ncnn_SRCS
pass_ncnn/torchvision_DeformConv2d.cpp
)

find_package(Protobuf)
if(PROTOBUF_FOUND)
protobuf_generate_cpp(ONNX_PROTO_SRCS ONNX_PROTO_HDRS onnx.proto)

add_library(pnnx2onnx STATIC
save_onnx.cpp
save_onnx_cxxabi_bridge.cpp
${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS}
)

target_include_directories(pnnx2onnx PRIVATE ${PROTOBUF_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(pnnx2onnx PRIVATE ${PROTOBUF_LIBRARIES})

# libtorch is usually compiled with old cxx11 abi
set_source_files_properties(save_onnx_cxxabi_bridge.cpp PROPERTIES COMPILE_FLAGS "${TORCH_CXX_FLAGS}")

message(STATUS "Building with onnx-zero")
if(DEFINED PROTOBUF_VERSION AND PROTOBUF_VERSION VERSION_GREATER_EQUAL 3.22)
set(CMAKE_CXX_STANDARD 17)
endif()

if(Protobuf_FOUND OR protobuf_MODULE_COMPATIBLE)
protobuf_generate_cpp(ONNX_PROTO_SRCS ONNX_PROTO_HDRS onnx.proto)

add_library(pnnx2onnx STATIC
save_onnx.cpp
save_onnx_cxxabi_bridge.cpp
${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS}
)

target_include_directories(pnnx2onnx PRIVATE ${PROTOBUF_INCLUDE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(pnnx2onnx PRIVATE ${PROTOBUF_LIBRARIES})

# libtorch is usually compiled with old cxx11 abi
set_source_files_properties(save_onnx_cxxabi_bridge.cpp PROPERTIES COMPILE_FLAGS "${TORCH_CXX_FLAGS}")

message(STATUS "Building with onnx-zero")
else()

add_library(pnnx2onnx STATIC
save_onnx.cpp
save_onnx_cxxabi_bridge.cpp
onnx.proto
)
set_source_files_properties(save_onnx_cxxabi_bridge.cpp PROPERTIES COMPILE_FLAGS "${TORCH_CXX_FLAGS}")
target_include_directories(pnnx2onnx PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate(TARGET pnnx2onnx)
target_link_libraries(pnnx2onnx PRIVATE protobuf::libprotobuf)
message(STATUS "Building with onnx-zero")
endif()
else()
message(STATUS "Building without onnx-zero")
endif()
Expand Down
Loading