Skip to content

[BUG] LTO with CMake is fully broken #2268

Backlog

Description

@nigolatesla00

Description

Minimal reproducer

CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(main C)

add_executable(main main.c)
set_property(TARGET main PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)

main.c:

int main(void) { return 0; }

Steps to reproduce

Configure:

$ ${ANDROID_HOME}/cmake/3.22.1/bin/cmake \
	-DCMAKE_TOOLCHAIN_FILE=${ANDROID_HOME}/ndk/30.0.16248370/build/cmake/android.toolchain.cmake \
	-DANDROID_ABI=arm64-v8a \
	-DANDROID_PLATFORM=android-24 \
	-B build \
	-S .

Build:

$ cmake --build ./build
[...]
[100%] Linking C executable main
clang: error: invalid linker name in argument '-fuse-ld=gold'
clang: error: invalid linker name in argument '-fuse-ld=gold'
make[2]: *** [CMakeFiles/main.dir/build.make:404: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:1161: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:156: all] Error 2

My AI slop report tells me this happens because the use of -fuse-ld=gold is guarded by a CMAKE_ANDROID_NDK_VERSION check inside CMake's own toolchain files (i.e., it performs an if <ndk_version> < 22 check before adding the flag). However, since neither AGP nor the NDK's CMake toolchain defines this variable, its value always evaluates to 0, so the flag is always added.

It also tells me this was fixed upstream in CMake 3.26, but since AGP still uses CMake 3.22 by default, this bug continues to affect everyone.

I am using a supported NDK

  • I have checked and the NDK I'm using is currently supported

Affected versions

r30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions