From 47c4144bd1178daf31006726eef2b4a85eee15c6 Mon Sep 17 00:00:00 2001 From: Scott McKay Date: Mon, 24 Aug 2020 19:24:13 +1000 Subject: [PATCH] Add gcc/clang flags to make binary smaller (https://interrupt.memfault.com/blog/best-and-worst-gcc-clang-compiler-flags#-ffunction-sections--fdata-sections----gc-sections) (#4895) Add gcc/clang flags to make binary smaller. ~10% reduction for Android baseline build (minimal build with no ops, no exceptions, no rtti). --- cmake/CMakeLists.txt | 9 +++++++++ onnxruntime/core/framework/execution_provider.cc | 2 ++ onnxruntime/core/framework/kernel_registry.cc | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 9bcf9943dc..c555fdc4c8 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -212,6 +212,15 @@ if(onnxruntime_MINIMAL_BUILD) add_compile_definitions(ORT_MINIMAL_BUILD) set(onnxruntime_REDUCED_OPS_BUILD ON) # TODO Defaulting to ON. TBD if we should always do that. set(onnxruntime_DISABLE_RTTI ON) + + if (MSVC) + # add MSVC specific flags to reduce build size here + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections") + # TODO: May need to use -dead_strip instead of --gc-sections for iOS(XCode) + add_link_options(-Wl,--gc-sections) + endif() + endif() if(onnxruntime_DISABLE_RTTI) diff --git a/onnxruntime/core/framework/execution_provider.cc b/onnxruntime/core/framework/execution_provider.cc index f771f33f00..54c8e79be4 100644 --- a/onnxruntime/core/framework/execution_provider.cc +++ b/onnxruntime/core/framework/execution_provider.cc @@ -41,6 +41,8 @@ IExecutionProvider::GetCapability(const onnxruntime::GraphViewer& graph, } } #else + ORT_UNUSED_PARAMETER(graph); + ORT_UNUSED_PARAMETER(kernel_registries); ORT_NOT_IMPLEMENTED("IExecutionProvider::GetCapability is not supported in this build."); #endif diff --git a/onnxruntime/core/framework/kernel_registry.cc b/onnxruntime/core/framework/kernel_registry.cc index 97dc6cbd47..49505297be 100644 --- a/onnxruntime/core/framework/kernel_registry.cc +++ b/onnxruntime/core/framework/kernel_registry.cc @@ -292,7 +292,7 @@ Status KernelRegistry::TryFindKernel(const onnxruntime::Node& node, return Status(ONNXRUNTIME, FAIL, "Kernel not found"); #else - ORT_THROW("Kernel hash must be provided in minimal build.") + ORT_THROW("Kernel hash must be provided in minimal build."); #endif }