Skip to content

Commit 0260a62

Browse files
committed
add static library of onnx onnx_proto for xcode
1 parent cbd4cec commit 0260a62

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

cmake/external/onnxruntime_external_deps.cmake

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,7 @@ onnxruntime_fetchcontent_declare(
530530

531531
onnxruntime_fetchcontent_makeavailable(onnx)
532532

533-
# https://github.com/onnx/onnx/pull/7087 introduces onnx_object, which
534-
# creates a target onnx_proto_object. If onnx_proto target does not exist,
535-
# we alias onnx_proto_object to onnx_proto for backward compatibility.
536-
if (NOT TARGET onnx_proto AND TARGET onnx_proto_object)
537-
message(STATUS "Creating onnx_proto static library from onnx_proto_object")
538-
add_library(onnx_proto STATIC $<TARGET_OBJECTS:onnx_proto_object>)
539-
target_link_libraries(onnx_proto PUBLIC ${PROTOBUF_LIB})
540-
target_include_directories(onnx_proto PUBLIC $<TARGET_PROPERTY:onnx_proto_object,INTERFACE_INCLUDE_DIRECTORIES>)
541-
target_compile_definitions(onnx_proto PUBLIC $<TARGET_PROPERTY:onnx_proto_object,INTERFACE_COMPILE_DEFINITIONS>)
542-
endif()
543-
533+
# In case onnx is found via find_package and targets are created with different names
544534
if(TARGET ONNX::onnx AND NOT TARGET onnx)
545535
message(STATUS "Aliasing ONNX::onnx to onnx")
546536
add_library(onnx ALIAS ONNX::onnx)
@@ -549,6 +539,33 @@ if(TARGET ONNX::onnx_proto AND NOT TARGET onnx_proto)
549539
message(STATUS "Aliasing ONNX::onnx_proto to onnx_proto")
550540
add_library(onnx_proto ALIAS ONNX::onnx_proto)
551541
endif()
542+
543+
# https://github.com/onnx/onnx/pull/7087 introduces onnx_object, which creates wrapper
544+
# libraries (onnx, onnx_proto) that link to object libraries. The Xcode generator doesn't
545+
# handle these empty wrapper libraries properly - it doesn't generate .a files.
546+
# We recreate them as proper static libraries from the object files.
547+
if (CMAKE_GENERATOR MATCHES "Xcode" AND TARGET onnx_proto_object AND TARGET onnx_object)
548+
message(STATUS "Creating ONNX static libraries from object files (Xcode workaround)")
549+
550+
# Create proper static library for onnx_proto
551+
add_library(onnx_proto_lib STATIC $<TARGET_OBJECTS:onnx_proto_object>)
552+
target_link_libraries(onnx_proto_lib PUBLIC ${PROTOBUF_LIB})
553+
target_include_directories(onnx_proto_lib PUBLIC $<TARGET_PROPERTY:onnx_proto_object,INTERFACE_INCLUDE_DIRECTORIES>)
554+
target_compile_definitions(onnx_proto_lib PUBLIC $<TARGET_PROPERTY:onnx_proto_object,INTERFACE_COMPILE_DEFINITIONS>)
555+
556+
# Create proper static library for onnx
557+
add_library(onnx_lib STATIC $<TARGET_OBJECTS:onnx_object> $<TARGET_OBJECTS:onnx_proto_object>)
558+
target_link_libraries(onnx_lib PUBLIC ${PROTOBUF_LIB})
559+
target_include_directories(onnx_lib PUBLIC $<TARGET_PROPERTY:onnx_object,INTERFACE_INCLUDE_DIRECTORIES>)
560+
target_compile_definitions(onnx_lib PUBLIC $<TARGET_PROPERTY:onnx_object,INTERFACE_COMPILE_DEFINITIONS>)
561+
562+
set(ONNX_LIB onnx_lib)
563+
set(ONNX_PROTO_LIB onnx_proto_lib)
564+
else()
565+
set(ONNX_LIB onnx)
566+
set(ONNX_PROTO_LIB onnx_proto)
567+
endif()
568+
552569
if(onnxruntime_USE_VCPKG)
553570
find_package(Eigen3 CONFIG REQUIRED)
554571
else()
@@ -589,17 +606,17 @@ if (onnxruntime_USE_XNNPACK)
589606
endif()
590607

591608
set(onnxruntime_EXTERNAL_LIBRARIES ${onnxruntime_EXTERNAL_LIBRARIES_XNNPACK} ${WIL_TARGET} nlohmann_json::nlohmann_json
592-
onnx onnx_proto ${PROTOBUF_LIB} re2::re2 Boost::mp11 safeint_interface
609+
${ONNX_LIB} ${ONNX_PROTO_LIB} ${PROTOBUF_LIB} re2::re2 Boost::mp11 safeint_interface
593610
flatbuffers::flatbuffers ${GSL_TARGET} ${ABSEIL_LIBS} date::date Eigen3::Eigen)
594611

595612
# The source code of onnx_proto is generated, we must build this lib first before starting to compile the other source code that uses ONNX protobuf types.
596613
# The other libs do not have the problem. All the sources are already there. We can compile them in any order.
597-
set(onnxruntime_EXTERNAL_DEPENDENCIES onnx_proto flatbuffers::flatbuffers)
614+
set(onnxruntime_EXTERNAL_DEPENDENCIES ${ONNX_PROTO_LIB} flatbuffers::flatbuffers)
598615

599616
if(NOT (onnx_FOUND OR ONNX_FOUND)) # building ONNX from source
600-
target_compile_definitions(onnx PUBLIC $<TARGET_PROPERTY:onnx_proto,INTERFACE_COMPILE_DEFINITIONS> PRIVATE "__ONNX_DISABLE_STATIC_REGISTRATION")
617+
target_compile_definitions(${ONNX_LIB} PUBLIC $<TARGET_PROPERTY:${ONNX_PROTO_LIB},INTERFACE_COMPILE_DEFINITIONS> PRIVATE "__ONNX_DISABLE_STATIC_REGISTRATION")
601618
if (NOT onnxruntime_USE_FULL_PROTOBUF)
602-
target_compile_definitions(onnx PUBLIC "__ONNX_NO_DOC_STRINGS")
619+
target_compile_definitions(${ONNX_LIB} PUBLIC "__ONNX_NO_DOC_STRINGS")
603620
endif()
604621
endif()
605622

0 commit comments

Comments
 (0)