diff --git a/build/cmake/android-legacy.toolchain.cmake b/build/cmake/android-legacy.toolchain.cmake index 9d9ae353..3a0dd713 100644 --- a/build/cmake/android-legacy.toolchain.cmake +++ b/build/cmake/android-legacy.toolchain.cmake @@ -457,6 +457,7 @@ list(APPEND ANDROID_LINKER_FLAGS -Wl,--gc-sections) list(APPEND ANDROID_LINKER_FLAGS_EXE -Wl,--gc-sections) # Debug and release flags. +list(APPEND ANDROID_COMPILER_FLAGS_RELEASE -O3) list(APPEND ANDROID_COMPILER_FLAGS_RELEASE -DNDEBUG) if(ANDROID_TOOLCHAIN STREQUAL clang) list(APPEND ANDROID_COMPILER_FLAGS_DEBUG -fno-limit-debug-info) diff --git a/docs/changelogs/Changelog-r25.md b/docs/changelogs/Changelog-r25.md index 0c4cf0ac..78a34650 100644 --- a/docs/changelogs/Changelog-r25.md +++ b/docs/changelogs/Changelog-r25.md @@ -11,6 +11,17 @@ directly, see the [build system maintainers guide]. [Android Studio site]: http://tools.android.com/filing-bugs [build system maintainers guide]: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md + +## r25b + +* [Issue 1740]: Fixed the legacy toolchain when using CMake's `Release` build + configuration. Since r23b it has not be receiving any optimization flag. It + will now receive `-O3`. If you're building with AGP and haven't overridden + AGP's default CMake modes, this change does not affect you, as AGP uses + `RelWithDebInfo` by default. + +[Issue 1740]: https://github.com/android/ndk/issues/1740 + ## Changes * Includes Android 13 Beta 2 APIs. diff --git a/tests/build/cmake_default_flags/test.py b/tests/build/cmake_default_flags/test.py index a7ead75a..ca367a29 100644 --- a/tests/build/cmake_default_flags/test.py +++ b/tests/build/cmake_default_flags/test.py @@ -18,17 +18,38 @@ from typing import Optional from ndk.test.spec import BuildConfiguration -from ndk.testing.flag_verifier import FlagVerifier +from ndk.testing.flag_verifier import FlagVerifier, FlagVerifierResult -def run_test(ndk_path: str, - config: BuildConfiguration) -> tuple[bool, Optional[str]]: - """Check that the CMake toolchain uses the correct default flags. +def check_configuration( + ndk_path: str, + build_config: BuildConfiguration, + cmake_config: str, + expected_flags: list[str], + unexpected_flags: list[str], +) -> FlagVerifierResult: + verifier = FlagVerifier(Path("project"), Path(ndk_path), build_config) + for flag in expected_flags: + verifier.expect_flag(flag) + for flag in unexpected_flags: + verifier.expect_not_flag(flag) + return verifier.verify_cmake([f"-DCMAKE_BUILD_TYPE={cmake_config}"]) - Currently this only tests the optimization flags for RelWithDebInfo, but - it's probably worth expanding in the future. - """ - verifier = FlagVerifier(Path('project'), Path(ndk_path), config) - verifier.expect_flag('-O2') - return verifier.verify_cmake(['-DCMAKE_BUILD_TYPE=RelWithDebInfo' - ]).make_test_result_tuple() + +def run_test(ndk_path: str, config: BuildConfiguration) -> tuple[bool, Optional[str]]: + """Check that the CMake toolchain uses the correct default flags.""" + verify_configs: dict[str, tuple[list[str], list[str]]] = { + # No flag is the same as -O0. As long as no other opt flag is used, the default + # is fine. + "Debug": ([], ["-O1", "-O2", "-O3", "-Os", "-Oz"]), + "MinSizeRel": (["-Os"], ["-O0", "-O1", "-O2", "-O3", "-Oz"]), + "Release": (["-O3"], ["-O0", "-O1", "-O2", "-Os", "-Oz"]), + "RelWithDebInfo": (["-O2"], ["-O0", "-O1", "-O3", "-Os", "-Oz"]), + } + for cmake_config, (expected_flags, unexpected_flags) in verify_configs.items(): + result = check_configuration( + ndk_path, config, cmake_config, expected_flags, unexpected_flags + ) + if result.failed(): + return result.make_test_result_tuple() + return result.make_test_result_tuple()