From f77ab4fea6220d803b6b54dcab84e2105a8d0f28 Mon Sep 17 00:00:00 2001 From: Edward Chen <18449977+edgchen1@users.noreply.github.com> Date: Mon, 1 Aug 2022 12:49:03 -0700 Subject: [PATCH] Manually add optimization flag for Android Release builds. (#12390) With recent versions of NDK (since 23), the `-O` optimization level compile flag is not being passed when building in the "Release" configuration. More details here: https://github.com/android/ndk/issues/1740 Our "Release" Android builds have been built without the optimization flag since we upgraded from NDK 21. This change is a workaround to manually add `-O3` for "Release" Android builds. --- cmake/CMakeLists.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index cb09051936..2395641eb5 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -318,6 +318,16 @@ if (onnxruntime_EXTENDED_MINIMAL_BUILD AND NOT onnxruntime_MINIMAL_BUILD) set(onnxruntime_MINIMAL_BUILD ON) endif() +# work around Android NDK bug which doesn't include -O flag for Release configuration +# https://github.com/android/ndk/issues/1740 +# TODO: remove this when the NDK version(s) we support get fixed +if (CMAKE_SYSTEM_NAME STREQUAL "Android") + # NB: attempting to match the effects of this fix: https://android-review.googlesource.com/c/platform/ndk/+/2168845 + string(APPEND CMAKE_C_FLAGS_RELEASE " -O3") + string(APPEND CMAKE_CXX_FLAGS_RELEASE " -O3") + string(APPEND CMAKE_ASM_FLAGS_RELEASE " -O3") +endif() + # Enable space optimization for gcc/clang # Cannot use "-ffunction-sections -fdata-sections" if we enable bitcode (iOS) if (NOT MSVC AND NOT onnxruntime_ENABLE_BITCODE)