From 921029babd3a21f3d901a08d359ce6e9b8098c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Fri, 1 Jul 2022 00:26:14 +0200 Subject: [PATCH] Update Android NDK and set optimization flags (#68354) Brings in new cmake 2.23.1 and Android NDK23c which fixes an issue with the binary size and perf of libmonosgen-2.0.so In NDK23b they decided to no longer pass -O2 compiler optimization flag (for arm64, armv7 used -Oz) as part of the Android toolchain but delegate to upstream CMake behavior: https://github.com/android/ndk/wiki/Changelog-r23 and https://github.com/android/ndk/issues/1536 CMake defaults to -O3 for Release builds but unfortunately this causes quite a noticable binary size increase and perf regression. The Xamarin Android team measured startup time on an average of 10 runs of `dotnet new maui` on a Pixel 5: ``` -O3: 893.7ms -O2: 600.2ms -Oz: 649.1ms ``` We now explicitly pass in -O2 for Android builds. Fixes https://github.com/dotnet/runtime/issues/68330 --- eng/native/build-commons.sh | 4 ++-- eng/native/configureoptimization.cmake | 7 ++++++- eng/pipelines/common/platform-matrix.yml | 12 ++++++------ src/mono/CMakeLists.txt | 3 +++ src/mono/Directory.Build.props | 4 ++-- src/mono/mono.proj | 3 +-- src/native/libs/CMakeLists.txt | 8 +++++++- src/tasks/AndroidAppBuilder/ApkBuilder.cs | 2 +- 8 files changed, 28 insertions(+), 15 deletions(-) diff --git a/eng/native/build-commons.sh b/eng/native/build-commons.sh index 1b28031ab4d..3f05aef7f53 100755 --- a/eng/native/build-commons.sh +++ b/eng/native/build-commons.sh @@ -84,8 +84,8 @@ build_native() exit 1 fi - # keep ANDROID_NATIVE_API_LEVEL in sync with src/mono/Directory.Build.props - cmakeArgs="-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 $cmakeArgs" + # keep ANDROID_PLATFORM in sync with src/mono/Directory.Build.props + cmakeArgs="-DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake -DANDROID_PLATFORM=android-21 $cmakeArgs" # Don't try to set CC/CXX in init-compiler.sh - it's handled in android.toolchain.cmake already __Compiler="default" diff --git a/eng/native/configureoptimization.cmake b/eng/native/configureoptimization.cmake index 50c7b1cfa8d..f6b5da696f9 100644 --- a/eng/native/configureoptimization.cmake +++ b/eng/native/configureoptimization.cmake @@ -12,6 +12,11 @@ if(CLR_CMAKE_HOST_WIN32) elseif(CLR_CMAKE_HOST_UNIX) add_compile_options($<$:-O0>) add_compile_options($<$:-O2>) - add_compile_options($<$:-O3>) + if(CLR_CMAKE_TARGET_ANDROID) + # -O2 optimization generates faster/smaller code on Android + add_compile_options($<$:-O2>) + else() + add_compile_options($<$:-O3>) + endif() add_compile_options($<$:-O2>) endif() diff --git a/eng/pipelines/common/platform-matrix.yml b/eng/pipelines/common/platform-matrix.yml index 7a48e79bfcd..4795d00b0ab 100644 --- a/eng/pipelines/common/platform-matrix.yml +++ b/eng/pipelines/common/platform-matrix.yml @@ -207,7 +207,7 @@ jobs: platform: Linux_bionic_arm64 shouldContinueOnError: ${{ parameters.shouldContinueOnError }} container: - image: ubuntu-18.04-android-20220131172314-3983b4e + image: ubuntu-18.04-android-20220628174908-5789942 registry: mcr jobParameters: runtimeFlavor: mono @@ -236,7 +236,7 @@ jobs: platform: Linux_bionic_x64 shouldContinueOnError: ${{ parameters.shouldContinueOnError }} container: - image: ubuntu-18.04-android-20220131172314-3983b4e + image: ubuntu-18.04-android-20220628174908-5789942 registry: mcr jobParameters: runtimeFlavor: mono @@ -494,7 +494,7 @@ jobs: platform: Android_x64 shouldContinueOnError: ${{ parameters.shouldContinueOnError }} container: - image: ubuntu-18.04-android-20220131172314-3983b4e + image: ubuntu-18.04-android-20220628174908-5789942 registry: mcr jobParameters: runtimeFlavor: mono @@ -519,7 +519,7 @@ jobs: platform: Android_x86 shouldContinueOnError: ${{ parameters.shouldContinueOnError }} container: - image: ubuntu-18.04-android-20220131172314-3983b4e + image: ubuntu-18.04-android-20220628174908-5789942 registry: mcr jobParameters: runtimeFlavor: mono @@ -544,7 +544,7 @@ jobs: platform: Android_arm shouldContinueOnError: ${{ parameters.shouldContinueOnError }} container: - image: ubuntu-18.04-android-20220131172314-3983b4e + image: ubuntu-18.04-android-20220628174908-5789942 registry: mcr jobParameters: runtimeFlavor: mono @@ -569,7 +569,7 @@ jobs: platform: Android_arm64 shouldContinueOnError: ${{ parameters.shouldContinueOnError }} container: - image: ubuntu-18.04-android-20220131172314-3983b4e + image: ubuntu-18.04-android-20220628174908-5789942 registry: mcr jobParameters: runtimeFlavor: mono diff --git a/src/mono/CMakeLists.txt b/src/mono/CMakeLists.txt index 2be6fc84683..c50c91f0a53 100644 --- a/src/mono/CMakeLists.txt +++ b/src/mono/CMakeLists.txt @@ -314,6 +314,9 @@ elseif(TARGET_SYSTEM_NAME STREQUAL "Linux") set(TARGET_LINUX 1) elseif(TARGET_SYSTEM_NAME STREQUAL "Android") set(TARGET_ANDROID 1) + if (CMAKE_BUILD_TYPE STREQUAL "Release") + add_compile_options(-O2) + endif() elseif(TARGET_SYSTEM_NAME STREQUAL "Emscripten") set(TARGET_BROWSER 1) if (CMAKE_BUILD_TYPE STREQUAL "Release") diff --git a/src/mono/Directory.Build.props b/src/mono/Directory.Build.props index 0ab0f8ac2a5..b64f87e11d9 100644 --- a/src/mono/Directory.Build.props +++ b/src/mono/Directory.Build.props @@ -28,8 +28,8 @@ - - 21 + + 21 diff --git a/src/mono/mono.proj b/src/mono/mono.proj index 7cd03731fcd..a9fc80619da 100644 --- a/src/mono/mono.proj +++ b/src/mono/mono.proj @@ -420,8 +420,7 @@ <_MonoCMakeArgs Include="-DANDROID_NDK=$(ANDROID_NDK_ROOT)"/> <_MonoCMakeArgs Include="-DANDROID_STL=none"/> <_MonoCMakeArgs Include="-DANDROID_CPP_FEATURES="no-rtti no-exceptions""/> - <_MonoCMakeArgs Include="-DANDROID_NATIVE_API_LEVEL=$(AndroidApiVersion)"/> - <_MonoCMakeArgs Include="-DANDROID_PLATFORM=android-$(AndroidApiVersion)"/> + <_MonoCMakeArgs Include="-DANDROID_PLATFORM=android-$(AndroidApiLevelMin)"/> <_MonoCMakeArgs Condition="'$(Platform)' == 'arm64'" Include="-DANDROID_ABI=arm64-v8a" /> <_MonoCMakeArgs Condition="'$(Platform)' == 'arm'" Include="-DANDROID_ABI=armeabi-v7a" /> <_MonoCMakeArgs Condition="'$(Platform)' == 'x86'" Include="-DANDROID_ABI=x86" /> diff --git a/src/native/libs/CMakeLists.txt b/src/native/libs/CMakeLists.txt index 577a6dee6b7..04088e9e41d 100644 --- a/src/native/libs/CMakeLists.txt +++ b/src/native/libs/CMakeLists.txt @@ -103,7 +103,13 @@ if (CLR_CMAKE_TARGET_UNIX OR CLR_CMAKE_TARGET_BROWSER) if (CLR_CMAKE_TARGET_ARCH_ARMV7L AND DEFINED ENV{CROSSCOMPILE} AND CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9) add_compile_options (-O1) else () - add_compile_options (-O3) + if(CLR_CMAKE_TARGET_ANDROID) + # -O2 optimization generates faster/smaller code on Android + # TODO: This duplicates the settings in eng/native/configureoptimization.cmake, we should unify it + add_compile_options (-O2) + else() + add_compile_options (-O3) + endif () endif () else () message(FATAL_ERROR "Unknown build type. Set CMAKE_BUILD_TYPE to DEBUG or RELEASE.") diff --git a/src/tasks/AndroidAppBuilder/ApkBuilder.cs b/src/tasks/AndroidAppBuilder/ApkBuilder.cs index ace6fcb0c0c..9ed098ac55a 100644 --- a/src/tasks/AndroidAppBuilder/ApkBuilder.cs +++ b/src/tasks/AndroidAppBuilder/ApkBuilder.cs @@ -334,7 +334,7 @@ public ApkBuilder(TaskLoggingHelper logger) File.WriteAllText(Path.Combine(OutputDir, "monodroid.c"), Utils.GetEmbeddedResource("monodroid.c")); string cmakeGenArgs = $"-DCMAKE_TOOLCHAIN_FILE={androidToolchain} -DANDROID_ABI=\"{abi}\" -DANDROID_STL=none " + - $"-DANDROID_NATIVE_API_LEVEL={MinApiLevel} -B monodroid"; + $"-DANDROID_PLATFORM=android-{MinApiLevel} -B monodroid"; string cmakeBuildArgs = "--build monodroid"; -- GitLab