Skip to content

Fix an issue in wasm nortti build and add minimal build support for vcpkg #24012

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

Merged
merged 26 commits into from
Apr 22, 2025
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
7 changes: 2 additions & 5 deletions cmake/external/onnxruntime_external_deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,8 @@ onnxruntime_fetchcontent_declare(
EXCLUDE_FROM_ALL
FIND_PACKAGE_ARGS NAMES ONNX onnx
)
if (NOT onnxruntime_MINIMAL_BUILD)
onnxruntime_fetchcontent_makeavailable(onnx)
else()
include(onnx_minimal)
endif()

onnxruntime_fetchcontent_makeavailable(onnx)

if(TARGET ONNX::onnx AND NOT TARGET onnx)
message(STATUS "Aliasing ONNX::onnx to onnx")
Expand Down
65 changes: 55 additions & 10 deletions cmake/patches/onnx/onnx.patch
Original file line number Diff line number Diff line change
@@ -1,16 +1,61 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6d7ca846..69aa622f 100644
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d15d97ed..bdacac99 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -499,6 +499,7 @@ if (MSVC)
@@ -27,6 +27,7 @@ option(ONNX_USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
option(ONNX_DISABLE_EXCEPTIONS "Disable exception handling." OFF)
option(ONNX_DISABLE_STATIC_REGISTRATION "Disable static registration for onnx operator schemas." OFF)
option(ONNX_USE_UNITY_BUILD "Enable Unity (Jumbo) build for" OFF)
+option(ONNX_MINIMAL_BUILD "Build only essential ONNX components" OFF)

if(NOT DEFINED ONNX_ML)
if(DEFINED ENV{ONNX_ML})
@@ -457,14 +458,28 @@ relative_protobuf_generate_cpp(gen_onnx_data_proto
list(APPEND ONNX_PROTO_SRCS ${__tmp_srcs})
list(APPEND ONNX_PROTO_HDRS ${__tmp_hdrs})

-file(GLOB_RECURSE __tmp_srcs "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc")
-file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h"
- "${ONNX_ROOT}/onnx/test/cpp/*.cc"
- "${ONNX_ROOT}/onnx/backend/test/cpp/*.cc"
- "${ONNX_ROOT}/onnx/backend/test/cpp/*.h")
-list(REMOVE_ITEM __tmp_srcs "${ONNX_ROOT}/onnx/cpp2py_export.cc")
-list(REMOVE_ITEM __tmp_srcs ${onnx_gtests_src})
-list(APPEND ONNX_SRCS ${__tmp_srcs})
+if(ONNX_MINIMAL_BUILD)
+ message(STATUS "Configuring ONNX minimal build")
+ set(ONNX_SRCS
+ "${ONNX_ROOT}/onnx/common/common.h"
+ "${ONNX_ROOT}/onnx/defs/data_type_utils.h"
+ "${ONNX_ROOT}/onnx/defs/data_type_utils.cc"
+ )
+ # Ensure ONNX_ML is treated as ON for minimal build consistency with ORT's file
+ set(ONNX_ML ON CACHE BOOL "Enable traditional ML API." FORCE)
+ # Minimal build doesn't need Python or tests
+ set(ONNX_BUILD_PYTHON OFF CACHE BOOL "Build Python binaries" FORCE)
+ set(ONNX_BUILD_TESTS OFF CACHE BOOL "Build ONNX C++ APIs Tests" FORCE)
+else()
+ file(GLOB_RECURSE __tmp_srcs "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc")
+ file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h"
+ "${ONNX_ROOT}/onnx/test/cpp/*.cc"
+ "${ONNX_ROOT}/onnx/backend/test/cpp/*.cc"
+ "${ONNX_ROOT}/onnx/backend/test/cpp/*.h")
+ list(REMOVE_ITEM __tmp_srcs "${ONNX_ROOT}/onnx/cpp2py_export.cc")
+ list(REMOVE_ITEM __tmp_srcs ${onnx_gtests_src})
+ list(APPEND ONNX_SRCS ${__tmp_srcs})
+endif()

add_library(onnx_proto ${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS})
add_dependencies(onnx_proto gen_onnx_operators_proto gen_onnx_data_proto)
@@ -496,6 +511,7 @@ if (MSVC)
endif()
else()
# On non-Windows, hide all symbols we don't need
+ set(EXTRA_FLAGS "-Wno-unused-parameter")
set(ONNX_API_DEFINE "-DONNX_API=__attribute__\(\(__visibility__\(\"default\"\)\)\)")
set_target_properties(onnx_proto PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(onnx_proto PROPERTIES VISIBILITY_INLINES_HIDDEN 1)
@@ -653,20 +654,9 @@ endif()
@@ -631,20 +647,9 @@ endif()
if(MSVC)
target_compile_options(onnx_proto
PRIVATE /MP
Expand All @@ -36,15 +81,15 @@ index b847798e..a6c31904 100644
--- a/onnx/common/file_utils.h
+++ b/onnx/common/file_utils.h
@@ -6,7 +6,6 @@

#pragma once

-#include <filesystem>
#include <fstream>
#include <string>

@@ -17,8 +16,7 @@ namespace ONNX_NAMESPACE {

template <typename T>
void LoadProtoFromPath(const std::string proto_path, T& proto) {
- std::filesystem::path proto_u8_path = std::filesystem::u8path(proto_path);
Expand All @@ -60,7 +105,7 @@ index 0aab3e26..398ac2d6 100644
@@ -47,10 +47,28 @@
#define ONNX_API ONNX_IMPORT
#endif

+#if defined(__GNUC__)
+#pragma GCC diagnostic push
+
Expand All @@ -80,7 +125,7 @@ index 0aab3e26..398ac2d6 100644
#else
#include "onnx/onnx.pb.h"
#endif

+#if defined(__GNUC__)
+#pragma GCC diagnostic pop
+#endif
Expand Down
112 changes: 110 additions & 2 deletions cmake/vcpkg-ports/onnx/binskim.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d15d97ed..fe4bd27f 100644
index d15d97ed..bdacac99 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -631,20 +631,9 @@ endif()
@@ -27,6 +27,7 @@ option(ONNX_USE_LITE_PROTO "Use lite protobuf instead of full." OFF)
option(ONNX_DISABLE_EXCEPTIONS "Disable exception handling." OFF)
option(ONNX_DISABLE_STATIC_REGISTRATION "Disable static registration for onnx operator schemas." OFF)
option(ONNX_USE_UNITY_BUILD "Enable Unity (Jumbo) build for" OFF)
+option(ONNX_MINIMAL_BUILD "Build only essential ONNX components" OFF)

if(NOT DEFINED ONNX_ML)
if(DEFINED ENV{ONNX_ML})
@@ -457,14 +458,28 @@ relative_protobuf_generate_cpp(gen_onnx_data_proto
list(APPEND ONNX_PROTO_SRCS ${__tmp_srcs})
list(APPEND ONNX_PROTO_HDRS ${__tmp_hdrs})

-file(GLOB_RECURSE __tmp_srcs "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc")
-file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h"
- "${ONNX_ROOT}/onnx/test/cpp/*.cc"
- "${ONNX_ROOT}/onnx/backend/test/cpp/*.cc"
- "${ONNX_ROOT}/onnx/backend/test/cpp/*.h")
-list(REMOVE_ITEM __tmp_srcs "${ONNX_ROOT}/onnx/cpp2py_export.cc")
-list(REMOVE_ITEM __tmp_srcs ${onnx_gtests_src})
-list(APPEND ONNX_SRCS ${__tmp_srcs})
+if(ONNX_MINIMAL_BUILD)
+ message(STATUS "Configuring ONNX minimal build")
+ set(ONNX_SRCS
+ "${ONNX_ROOT}/onnx/common/common.h"
+ "${ONNX_ROOT}/onnx/defs/data_type_utils.h"
+ "${ONNX_ROOT}/onnx/defs/data_type_utils.cc"
+ )
+ # Ensure ONNX_ML is treated as ON for minimal build consistency with ORT's file
+ set(ONNX_ML ON CACHE BOOL "Enable traditional ML API." FORCE)
+ # Minimal build doesn't need Python or tests
+ set(ONNX_BUILD_PYTHON OFF CACHE BOOL "Build Python binaries" FORCE)
+ set(ONNX_BUILD_TESTS OFF CACHE BOOL "Build ONNX C++ APIs Tests" FORCE)
+else()
+ file(GLOB_RECURSE __tmp_srcs "${ONNX_ROOT}/onnx/*.h" "${ONNX_ROOT}/onnx/*.cc")
+ file(GLOB_RECURSE onnx_gtests_src "${ONNX_ROOT}/onnx/test/cpp/*.h"
+ "${ONNX_ROOT}/onnx/test/cpp/*.cc"
+ "${ONNX_ROOT}/onnx/backend/test/cpp/*.cc"
+ "${ONNX_ROOT}/onnx/backend/test/cpp/*.h")
+ list(REMOVE_ITEM __tmp_srcs "${ONNX_ROOT}/onnx/cpp2py_export.cc")
+ list(REMOVE_ITEM __tmp_srcs ${onnx_gtests_src})
+ list(APPEND ONNX_SRCS ${__tmp_srcs})
+endif()

add_library(onnx_proto ${ONNX_PROTO_SRCS} ${ONNX_PROTO_HDRS})
add_dependencies(onnx_proto gen_onnx_operators_proto gen_onnx_data_proto)
@@ -496,6 +511,7 @@ if (MSVC)
endif()
else()
# On non-Windows, hide all symbols we don't need
+ set(EXTRA_FLAGS "-Wno-unused-parameter")
set(ONNX_API_DEFINE "-DONNX_API=__attribute__\(\(__visibility__\(\"default\"\)\)\)")
set_target_properties(onnx_proto PROPERTIES CXX_VISIBILITY_PRESET hidden)
set_target_properties(onnx_proto PROPERTIES VISIBILITY_INLINES_HIDDEN 1)
@@ -631,20 +647,9 @@ endif()
if(MSVC)
target_compile_options(onnx_proto
PRIVATE /MP
Expand All @@ -23,3 +76,58 @@ index d15d97ed..fe4bd27f 100644
${EXTRA_FLAGS})
if(ONNX_USE_PROTOBUF_SHARED_LIBS)
target_compile_options(onnx_proto
diff --git a/onnx/common/file_utils.h b/onnx/common/file_utils.h
index b847798e..a6c31904 100644
--- a/onnx/common/file_utils.h
+++ b/onnx/common/file_utils.h
@@ -6,7 +6,6 @@

#pragma once

-#include <filesystem>
#include <fstream>
#include <string>

@@ -17,8 +16,7 @@ namespace ONNX_NAMESPACE {

template <typename T>
void LoadProtoFromPath(const std::string proto_path, T& proto) {
- std::filesystem::path proto_u8_path = std::filesystem::u8path(proto_path);
- std::fstream proto_stream(proto_u8_path, std::ios::in | std::ios::binary);
+ std::fstream proto_stream(proto_path, std::ios::in | std::ios::binary);
if (!proto_stream.good()) {
fail_check("Unable to open proto file: ", proto_path, ". Please check if it is a valid proto. ");
}
diff --git a/onnx/onnx_pb.h b/onnx/onnx_pb.h
index 0aab3e26..398ac2d6 100644
--- a/onnx/onnx_pb.h
+++ b/onnx/onnx_pb.h
@@ -47,10 +47,28 @@
#define ONNX_API ONNX_IMPORT
#endif

+#if defined(__GNUC__)
+#pragma GCC diagnostic push
+
+// In file included from onnx/onnx-ml.pb.h:30:
+// In file included from google/protobuf/extension_set.h:53:
+// google/protobuf/parse_context.h:328:47: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
+#if defined(__has_warning)
+#if __has_warning("-Wshorten-64-to-32")
+#pragma GCC diagnostic ignored "-Wshorten-64-to-32"
+#endif
+#endif // defined(__has_warning)
+
+#endif // defined(__GNUC__)
+
#ifdef ONNX_ML
#include "onnx/onnx-ml.pb.h"
#else
#include "onnx/onnx.pb.h"
#endif

+#if defined(__GNUC__)
+#pragma GCC diagnostic pop
+#endif
+
#endif // ! ONNX_ONNX_PB_H
20 changes: 16 additions & 4 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,17 @@ def generate_vcpkg_install_options(build_dir, args):
folder_name_parts = []
if args.enable_address_sanitizer:
folder_name_parts.append("asan")
if args.use_binskim_compliant_compile_flags and not args.android:
if args.use_binskim_compliant_compile_flags and not args.android and not args.build_wasm:
folder_name_parts.append("binskim")
if args.disable_rtti:
folder_name_parts.append("nortti")
if args.build_wasm and not args.disable_wasm_exception_catching and not args.disable_exceptions:
folder_name_parts.append("exception_catching")
if args.disable_exceptions:
folder_name_parts.append("noexception")
if len(folder_name_parts) == 0:
if args.minimal_build is not None:
folder_name_parts.append("minimal")
if args.build_wasm or len(folder_name_parts) == 0:
# It's hard to tell whether we must use a custom triplet or not. The official triplets work fine for most common situations. However, if a Windows build has set msvc toolset version via args.msvc_toolset then we need to, because we need to ensure all the source code are compiled by the same MSVC toolset version otherwise we will hit link errors like "error LNK2019: unresolved external symbol __std_mismatch_4 referenced in function ..."
# So, to be safe we always use a custom triplet.
folder_name = "default"
Expand Down Expand Up @@ -507,7 +511,8 @@ def generate_build_tree(
"-Donnxruntime_DISABLE_OPTIONAL_TYPE=" + ("ON" if disable_optional_type else "OFF"),
"-Donnxruntime_CUDA_MINIMAL=" + ("ON" if args.enable_cuda_minimal_build else "OFF"),
]

if args.minimal_build is not None:
add_default_definition(cmake_extra_defines, "ONNX_MINIMAL_BUILD", "ON")
if args.rv64:
add_default_definition(cmake_extra_defines, "onnxruntime_CROSS_COMPILING", "ON")
if not args.riscv_toolchain_root:
Expand Down Expand Up @@ -607,7 +612,14 @@ def generate_build_tree(
add_default_definition(
cmake_extra_defines, "VCPKG_CHAINLOAD_TOOLCHAIN_FILE", str(empty_toolchain_file.absolute())
)
generate_vcpkg_triplets_for_emscripten(build_dir, emscripten_root_path)
generate_vcpkg_triplets_for_emscripten(
build_dir,
emscripten_root_path,
not args.disable_rtti,
not args.disable_wasm_exception_catching,
args.minimal_build is not None,
args.enable_address_sanitizer,
)
elif args.android:
generate_android_triplets(build_dir, args.android_cpp_shared, args.android_api)
elif is_windows():
Expand Down
Empty file modified tools/doc/builddoc.sh
100644 → 100755
Empty file.
Loading
Loading