Skip to content

Commit 1d2f1d3

Browse files
cyyeverCopilot
andauthored
Revert ONNX CMake changes (onnx#7515)
### Motivation and Context It turns out that the CMake object libraries have portability issues on some platforms. Fixes onnx#7514 --------- Signed-off-by: Yuanyuan Chen <cyyever@outlook.com> Signed-off-by: cyy <cyyever@outlook.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 82c1c48 commit 1d2f1d3

File tree

4 files changed

+81
-93
lines changed

4 files changed

+81
-93
lines changed

CMakeLists.txt

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,6 @@ relative_protobuf_generate_cpp(ONNX_PROTO_SRCS
397397
onnx/onnx-operators.in.proto
398398
onnx/onnx-data.in.proto)
399399

400-
add_library(onnx_proto_object OBJECT ${ONNX_PROTO_SRCS})
401-
402400
file(GLOB_RECURSE __tmp_srcs "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc")
403401
file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h"
404402
"${ONNX_ROOT}/onnx/test/cpp/*.cc"
@@ -414,57 +412,14 @@ if(ONNX_USE_LITE_PROTO)
414412
set(LINKED_PROTOBUF_TARGET protobuf::libprotobuf-lite)
415413
endif()
416414
endif()
417-
add_onnx_global_defines(onnx_proto_object)
418-
target_include_directories(onnx_proto_object PUBLIC
419-
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
420-
if(MSVC)
421-
# For disabling Protobuf related warnings
422-
set(protobuf_warnings
423-
/wd4146 # unary minus operator applied to unsigned type,
424-
# result still unsigned
425-
/wd4244 # 'argument': conversion from 'google::
426-
# protobuf::uint64' to 'int', possible
427-
# loss of data
428-
/wd4267 # Conversion from 'size_t' to 'int',
429-
# possible loss of data
430-
/wd4141 # 'inline': used more than once
431-
)
432-
endif()
433-
add_library(onnx_proto)
434-
target_link_libraries(onnx_proto PUBLIC $<BUILD_INTERFACE:onnx_proto_object>)
435-
if(ONNX_ML)
436-
target_compile_definitions(onnx_proto PUBLIC ONNX_ML=1)
437-
endif()
438-
target_compile_definitions(onnx_proto PUBLIC ONNX_NAMESPACE=${ONNX_NAMESPACE})
439-
440-
# onnx_object and onnx_proto_object are collections of C++ object files.
441-
# They are introduced to help onnx_cpp2py_export bypass library boundaries and use ONNX private symbols.
442-
add_library(onnx_object OBJECT ${ONNX_SRCS})
443-
add_dependencies(onnx_object onnx_proto_object)
444-
target_include_directories(onnx_object PUBLIC
445-
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
446-
set_target_properties(onnx_object PROPERTIES CXX_VISIBILITY_PRESET hidden)
447-
set_target_properties(onnx_object PROPERTIES VISIBILITY_INLINES_HIDDEN ON)
448-
target_include_directories(onnx_object PUBLIC $<BUILD_INTERFACE:${ONNX_ROOT}>)
449-
add_onnx_global_defines(onnx_object)
450-
target_link_libraries(onnx_proto_object PUBLIC ${LINKED_PROTOBUF_TARGET})
451-
target_link_libraries(onnx_object PUBLIC ${LINKED_PROTOBUF_TARGET})
452-
foreach(ABSL_USED_TARGET IN LISTS protobuf_ABSL_USED_TARGETS)
453-
if(TARGET ${ABSL_USED_TARGET})
454-
target_link_libraries(onnx_proto_object PUBLIC ${ABSL_USED_TARGET})
455-
target_link_libraries(onnx_object PUBLIC ${ABSL_USED_TARGET})
456-
endif()
457-
endforeach()
458-
add_library(onnx)
459-
target_link_libraries(onnx PUBLIC $<BUILD_INTERFACE:onnx_object> $<BUILD_INTERFACE:onnx_proto_object>)
460-
target_include_directories(onnx PUBLIC $<INSTALL_INTERFACE:include>)
461-
if(ONNX_ML)
462-
target_compile_definitions(onnx PUBLIC ONNX_ML=1)
463-
endif()
464-
target_compile_definitions(onnx PUBLIC ONNX_NAMESPACE=${ONNX_NAMESPACE})
415+
add_library(onnx_proto ${ONNX_PROTO_SRCS})
416+
add_onnx_global_defines(onnx_proto)
465417

466-
target_compile_options(onnx_object PUBLIC ${protobuf_warnings})
467-
target_compile_options(onnx_proto_object PUBLIC ${protobuf_warnings})
418+
add_library(onnx ${ONNX_SRCS} ${ONNX_PROTO_SRCS})
419+
add_dependencies(onnx onnx_proto)
420+
set_target_properties(onnx PROPERTIES CXX_VISIBILITY_PRESET hidden)
421+
set_target_properties(onnx PROPERTIES VISIBILITY_INLINES_HIDDEN ON)
422+
add_onnx_global_defines(onnx)
468423

469424
if(ONNX_BUILD_PYTHON)
470425
# find system nanobind
@@ -485,33 +440,15 @@ if(ONNX_BUILD_PYTHON)
485440
nanobind_add_module(
486441
onnx_cpp2py_export
487442
NB_STATIC NB_DOMAIN onnx STABLE_ABI FREE_THREADED LTO
488-
"${ONNX_ROOT}/onnx/cpp2py_export.cc")
489-
490-
target_link_libraries(onnx_cpp2py_export PRIVATE $<BUILD_INTERFACE:onnx_object> $<BUILD_INTERFACE:onnx_proto_object>)
491-
# Prevent "undefined symbol: _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv"
492-
# (std::filesystem::__cxx11::path::_M_split_cmpts()) on gcc 8
493-
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
494-
target_link_libraries(onnx_cpp2py_export PRIVATE "-lstdc++fs")
495-
endif()
443+
"${ONNX_ROOT}/onnx/cpp2py_export.cc" ${ONNX_SRCS} ${ONNX_PROTO_SRCS})
444+
add_dependencies(onnx_cpp2py_export onnx)
496445
endif()
497446

498-
if(MSVC)
499-
add_msvc_runtime_flag(onnx_proto_object)
500-
add_msvc_runtime_flag(onnx_object)
501-
if(TARGET onnx_cpp2py_export)
502-
add_msvc_runtime_flag(onnx_cpp2py_export)
503-
endif()
504-
if(ONNX_WERROR)
505-
target_compile_options(onnx_object PRIVATE "/WX")
506-
endif()
507-
else()
508-
target_compile_options(onnx_object PRIVATE -Wall -Wextra)
509-
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13)
510-
target_compile_options(onnx_object PRIVATE "-Wno-stringop-overflow")
511-
endif()
512-
if(ONNX_WERROR)
513-
target_compile_options(onnx_object PRIVATE "-Werror")
514-
endif()
447+
add_onnx_compile_options(onnx_proto)
448+
add_onnx_compile_options(onnx)
449+
if(TARGET onnx_cpp2py_export)
450+
add_onnx_global_defines(onnx_cpp2py_export)
451+
add_onnx_compile_options(onnx_cpp2py_export)
515452
endif()
516453
if(ONNX_USE_ASAN AND NOT MSVC)
517454
find_package(Sanitizer REQUIRED)

cmake/Utils.cmake

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
# Add MSVC RunTime Flag
44
function(add_msvc_runtime_flag lib)
55
if(ONNX_USE_MSVC_STATIC_RUNTIME)
6-
target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MT> $<$<CONFIG:Debug>:/MTd>)
6+
target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MT>
7+
$<$<CONFIG:Debug>:/MTd>)
78
else()
8-
target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MD> $<$<CONFIG:Debug>:/MDd>)
9+
target_compile_options(${lib} PRIVATE $<$<NOT:$<CONFIG:Debug>>:/MD>
10+
$<$<CONFIG:Debug>:/MDd>)
911
endif()
1012
endfunction()
1113

1214
function(add_onnx_global_defines target)
13-
target_compile_definitions(${target} PUBLIC "ONNX_NAMESPACE=${ONNX_NAMESPACE}")
15+
target_compile_definitions(${target}
16+
PUBLIC "ONNX_NAMESPACE=${ONNX_NAMESPACE}")
1417

1518
if(ONNX_ML)
1619
target_compile_definitions(${target} PUBLIC "ONNX_ML=1")
@@ -21,6 +24,54 @@ function(add_onnx_global_defines target)
2124
endif()
2225

2326
if(ONNX_DISABLE_STATIC_REGISTRATION)
24-
target_compile_definitions(${target} PUBLIC "__ONNX_DISABLE_STATIC_REGISTRATION")
27+
target_compile_definitions(${target}
28+
PUBLIC "__ONNX_DISABLE_STATIC_REGISTRATION")
29+
endif()
30+
endfunction()
31+
32+
function(add_onnx_compile_options target)
33+
if(MSVC)
34+
# For disabling Protobuf related warnings
35+
set(protobuf_warnings
36+
/wd4146 # unary minus operator applied to unsigned type, result still
37+
# unsigned
38+
/wd4244 # 'argument': conversion from 'google::protobuf::uint64' to
39+
# 'int', possible loss of data
40+
/wd4267 # Conversion from 'size_t' to 'int', possible loss of data
41+
/wd4141 # 'inline': used more than once
42+
/wd4047 # '=': 'uintptr_t' differs in levels of indirection from 'void *'
43+
)
44+
add_msvc_runtime_flag(${target})
45+
target_compile_options(${target} PUBLIC ${protobuf_warnings})
46+
if(ONNX_WERROR)
47+
target_compile_options(${target} PRIVATE "/WX")
48+
endif()
49+
else()
50+
target_compile_options(${target} PRIVATE -Wall -Wextra)
51+
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION
52+
VERSION_GREATER_EQUAL 13)
53+
target_compile_options(${target} PRIVATE "-Wno-stringop-overflow")
54+
endif()
55+
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
56+
target_compile_options(${target} PRIVATE "-Wno-shorten-64-to-32")
57+
endif()
58+
if(ONNX_WERROR)
59+
target_compile_options(${target} PRIVATE "-Werror")
60+
endif()
61+
endif()
62+
target_include_directories(
63+
${target}
64+
PUBLIC $<BUILD_INTERFACE:${ONNX_ROOT}> $<INSTALL_INTERFACE:include>
65+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
66+
target_link_libraries(${target} PUBLIC ${LINKED_PROTOBUF_TARGET})
67+
foreach(ABSL_USED_TARGET IN LISTS protobuf_ABSL_USED_TARGETS)
68+
if(TARGET ${ABSL_USED_TARGET})
69+
target_link_libraries(${target} PUBLIC ${ABSL_USED_TARGET})
70+
endif()
71+
endforeach()
72+
# Prevent "undefined symbol: _ZNSt10filesystem7__cxx114path14_M_split_cmptsEv"
73+
# (std::filesystem::__cxx11::path::_M_split_cmpts()) on gcc 8
74+
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
75+
target_link_libraries(${target} PRIVATE "-lstdc++fs")
2576
endif()
2677
endfunction()

cmake/summary.cmake

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,29 @@ function(onnx_print_configuration_summary)
2727
message(STATUS " BUILD_SHARED_LIBS : ${BUILD_SHARED_LIBS}")
2828
message(STATUS "")
2929

30-
get_target_property(tmp onnx_object COMPILE_OPTIONS)
30+
get_target_property(tmp onnx COMPILE_OPTIONS)
3131
message(STATUS " onnx compile options : ${tmp}")
32-
get_target_property(tmp onnx_proto_object COMPILE_OPTIONS)
32+
get_target_property(tmp onnx_proto COMPILE_OPTIONS)
3333
if(tmp)
3434
message(STATUS " onnx_proto compile options : ${tmp}")
3535
endif()
36-
get_target_property(tmp onnx_object COMPILE_DEFINITIONS)
36+
get_target_property(tmp onnx COMPILE_DEFINITIONS)
3737
message(STATUS " onnx compile definitions : ${tmp}")
38-
get_target_property(tmp onnx_proto_object COMPILE_DEFINITIONS)
38+
get_target_property(tmp onnx_proto COMPILE_DEFINITIONS)
3939
message(STATUS " onnx_proto compile definitions : ${tmp}")
40-
get_target_property(tmp onnx_object LINK_OPTIONS)
40+
get_target_property(tmp onnx LINK_OPTIONS)
4141
if(tmp)
4242
message(STATUS " onnx link options : ${tmp}")
4343
endif()
44-
get_target_property(tmp onnx_proto_object LINK_OPTIONS)
44+
get_target_property(tmp onnx_proto LINK_OPTIONS)
4545
if(tmp)
4646
message(STATUS " onnx_proto link options : ${tmp}")
4747
endif()
48-
get_target_property(tmp onnx_object LINK_LIBRARIES)
48+
get_target_property(tmp onnx LINK_LIBRARIES)
4949
if(tmp)
5050
message(STATUS " onnx link libraries : ${tmp}")
5151
endif()
52-
get_target_property(tmp onnx_proto_object LINK_LIBRARIES)
52+
get_target_property(tmp onnx_proto LINK_LIBRARIES)
5353
if(tmp)
5454
message(STATUS " onnx_proto link libraries : ${tmp}")
5555
endif()

onnx/cpp2py_export.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,11 @@ NB_MODULE(onnx_cpp2py_export, onnx_cpp2py_export) {
358358
self->SetName(std::move(name)).SetDomain(std::move(domain)).SinceVersion(since_version).SetDoc(doc);
359359
self->SetNodeDeterminism(node_determinism);
360360
// Add inputs and outputs
361-
for (auto i = 0; i < inputs.size(); ++i) {
362-
self->Input(i, std::move(inputs[i]));
361+
for (size_t i = 0; i < inputs.size(); ++i) {
362+
self->Input(static_cast<int>(i), std::move(inputs[i]));
363363
}
364-
for (auto i = 0; i < outputs.size(); ++i) {
365-
self->Output(i, std::move(outputs[i]));
364+
for (size_t i = 0; i < outputs.size(); ++i) {
365+
self->Output(static_cast<int>(i), std::move(outputs[i]));
366366
}
367367
// Add type constraints
368368
for (auto& type_constraint : type_constraints) {

0 commit comments

Comments
 (0)