未验证 提交 921029ba 编写于 作者: A Alexander Köplinger 提交者: GitHub

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
上级 ac39683d
......@@ -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"
......
......@@ -12,6 +12,11 @@ if(CLR_CMAKE_HOST_WIN32)
elseif(CLR_CMAKE_HOST_UNIX)
add_compile_options($<$<CONFIG:Debug>:-O0>)
add_compile_options($<$<CONFIG:Checked>:-O2>)
if(CLR_CMAKE_TARGET_ANDROID)
# -O2 optimization generates faster/smaller code on Android
add_compile_options($<$<CONFIG:Release>:-O2>)
else()
add_compile_options($<$<CONFIG:Release>:-O3>)
endif()
add_compile_options($<$<CONFIG:RelWithDebInfo>:-O2>)
endif()
......@@ -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
......
......@@ -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")
......
......@@ -28,8 +28,8 @@
<watchOS64_32Version></watchOS64_32Version>
<macOSVersion></macOSVersion>
<!-- Version of the Android SDK we target, keep in sync with eng/native/build-commons.sh -->
<AndroidApiVersion>21</AndroidApiVersion>
<!-- Minimum version of the Android API level we target, keep in sync with eng/native/build-commons.sh -->
<AndroidApiLevelMin>21</AndroidApiLevelMin>
</PropertyGroup>
<!-- Output paths -->
......
......@@ -420,8 +420,7 @@
<_MonoCMakeArgs Include="-DANDROID_NDK=$(ANDROID_NDK_ROOT)"/>
<_MonoCMakeArgs Include="-DANDROID_STL=none"/>
<_MonoCMakeArgs Include="-DANDROID_CPP_FEATURES=&quot;no-rtti no-exceptions&quot;"/>
<_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" />
......
......@@ -103,8 +103,14 @@ 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 ()
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.")
endif ()
......
......@@ -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";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册