Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to build release version of any ndk sample (hello-jni) using cmake, because of linker error - Optimization level must be between 0 and 3 #721

Closed
TsvetanBogoev opened this issue Jun 18, 2018 · 17 comments

Comments

@TsvetanBogoev
Copy link

TsvetanBogoev commented Jun 18, 2018

Description

I am not able to build any of the ndk-samples in release. The problem is that it always passes " -plugin-opt=Os" to the linker.

Environment Details

  • NDK Version: 17.1.4828580
  • Host OS: Linux ubuntu 16.04
  • Compiler: cmake/clang

Here is the output of ./gradlew assembleRelease
`tsvetan@tsvetan-ubuntu:~/work/ndk-samples/hello-jni$ ./gradlew assembleRelease

Task :app:externalNativeBuildArm7Release FAILED
Build hello-jni armeabi-v7a
[1/1] : && /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi --gcc-toolchain=/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/tsvetan/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem /home/tsvetan/Android/Sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Os -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -flto -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -L/home/tsvetan/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libhello-jni.so -o /home/tsvetan/work/ndk-samples/hello-jni/app/build/intermediates/cmake/arm7/release/obj/armeabi-v7a/libhello-jni.so CMakeFiles/hello-jni.dir/hello-jni.c.o -landroid -llog -v -latomic -lm && :
FAILED: : && /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi --gcc-toolchain=/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/tsvetan/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem /home/tsvetan/Android/Sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Os -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -flto -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -L/home/tsvetan/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libhello-jni.so -o /home/tsvetan/work/ndk-samples/hello-jni/app/build/intermediates/cmake/arm7/release/obj/armeabi-v7a/libhello-jni.so CMakeFiles/hello-jni.dir/hello-jni.c.o -landroid -llog -v -latomic -lm && :
Android (4691093 based on r316199) clang version 6.0.2 (https://android.googlesource.com/toolchain/clang 183abd29fc496f55536e7d904e0abae47888fc7f) (https://android.googlesource.com/toolchain/llvm 34361f192e41ed6e4e8f9aca80a4ea7e9856f327) (based on LLVM 6.0.2svn)
Target: armv7-none-linux-android
Thread model: posix
InstalledDir: /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Selected GCC installation: /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
Candidate multilib: thumb;@mthumb
Candidate multilib: armv7-a;@march=armv7-a
Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb
Candidate multilib: .;
Selected multilib: armv7-a/thumb;@march=armv7-a@mthumb
"/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" --sysroot=/home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm -z relro -X --eh-frame-hdr -m armelf_linux_eabi -shared -o /home/tsvetan/work/ndk-samples/hello-jni/app/build/intermediates/cmake/arm7/release/obj/armeabi-v7a/libhello-jni.so /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib/../lib/crtbegin_so.o -L/home/tsvetan/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/6.0.2/lib/linux/arm -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/armv7-a/thumb -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a/thumb -L/home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib/../lib -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a/thumb -L/home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib -plugin /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so -plugin-opt=mcpu=generic -plugin-opt=Os -plugin-opt=-function-sections --exclude-libs libgcc.a --exclude-libs libatomic.a --build-id --warn-shared-textrel --fatal-warnings --fix-cortex-a8 --exclude-libs libunwind.a --no-undefined -z noexecstack -z relro -z now -soname libhello-jni.so CMakeFiles/hello-jni.dir/hello-jni.c.o -landroid -llog -latomic -lm -lgcc -ldl -lc -lgcc -ldl /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib/../lib/crtend_so.o
/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: fatal error: Optimization level must be between 0 and 3
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:externalNativeBuildArm7Release'.
    Build command failed.
    Error while executing process /home/tsvetan/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/tsvetan/work/ndk-samples/hello-jni/app/.externalNativeBuild/cmake/arm7Release/armeabi-v7a --target hello-jni}
    [1/1] : && /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi --gcc-toolchain=/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/tsvetan/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem /home/tsvetan/Android/Sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Os -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -flto -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -L/home/tsvetan/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libhello-jni.so -o /home/tsvetan/work/ndk-samples/hello-jni/app/build/intermediates/cmake/arm7/release/obj/armeabi-v7a/libhello-jni.so CMakeFiles/hello-jni.dir/hello-jni.c.o -landroid -llog -v -latomic -lm && :
    FAILED: : && /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi --gcc-toolchain=/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/tsvetan/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem /home/tsvetan/Android/Sdk/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=23 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -Os -DNDEBUG -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -flto -Wl,--fix-cortex-a8 -Wl,--exclude-libs,libunwind.a -L/home/tsvetan/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libhello-jni.so -o /home/tsvetan/work/ndk-samples/hello-jni/app/build/intermediates/cmake/arm7/release/obj/armeabi-v7a/libhello-jni.so CMakeFiles/hello-jni.dir/hello-jni.c.o -landroid -llog -v -latomic -lm && :
    Android (4691093 based on r316199) clang version 6.0.2 (https://android.googlesource.com/toolchain/clang 183abd29fc496f55536e7d904e0abae47888fc7f) (https://android.googlesource.com/toolchain/llvm 34361f192e41ed6e4e8f9aca80a4ea7e9856f327) (based on LLVM 6.0.2svn)
    Target: armv7-none-linux-android
    Thread model: posix
    InstalledDir: /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin
    Found candidate GCC installation: /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
    Selected GCC installation: /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x
    Candidate multilib: thumb;@mthumb
    Candidate multilib: armv7-a;@march=armv7-a
    Candidate multilib: armv7-a/thumb;@march=armv7-a@mthumb
    Candidate multilib: .;
    Selected multilib: armv7-a/thumb;@march=armv7-a@mthumb
    "/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" --sysroot=/home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm -z relro -X --eh-frame-hdr -m armelf_linux_eabi -shared -o /home/tsvetan/work/ndk-samples/hello-jni/app/build/intermediates/cmake/arm7/release/obj/armeabi-v7a/libhello-jni.so /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib/../lib/crtbegin_so.o -L/home/tsvetan/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/6.0.2/lib/linux/arm -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/armv7-a/thumb -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a/thumb -L/home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib/../lib -L/home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a/thumb -L/home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib -plugin /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/../lib64/LLVMgold.so -plugin-opt=mcpu=generic -plugin-opt=Os -plugin-opt=-function-sections --exclude-libs libgcc.a --exclude-libs libatomic.a --build-id --warn-shared-textrel --fatal-warnings --fix-cortex-a8 --exclude-libs libunwind.a --no-undefined -z noexecstack -z relro -z now -soname libhello-jni.so CMakeFiles/hello-jni.dir/hello-jni.c.o -landroid -llog -latomic -lm -lgcc -ldl -lc -lgcc -ldl /home/tsvetan/Android/Sdk/ndk-bundle/platforms/android-23/arch-arm/usr/lib/../lib/crtend_so.o
    /home/tsvetan/Android/Sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: fatal error: Optimization level must be between 0 and 3
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 0s
17 actionable tasks: 1 executed, 16 up-to-date
`

@enh
Copy link
Contributor

enh commented Jul 3, 2018

i would have suggested filing this at https://github.com/googlesamples/android-ndk/issues for @ggfan but github search claims these lines aren't coming from the samples, so maybe these -plugin-opt= arguments are coming from gradle and you want @jomof instead?

@pirama-arumuga-nainar
Copy link
Collaborator

The linker options are added by Clang due to -flto. With -flto, the Clang driver passes the optimization level to the LLVM Gold plugin (that does the link-time optimization).

We'd seen this earlier (not sure whether the platform or the NDK). I'd filed https://bugs.llvm.org//show_bug.cgi?id=32155 and uploaded https://reviews.llvm.org/D30920 but the patch did not get any consensus. I'll revive the discussion.

@TsvetanBogoev In the meantime, disabling LTO or using O1-O3 should get you unblocked.

@TsvetanBogoev
Copy link
Author

Here is a workaround I found: the solution is to not pass Os option to Clang LTO plugin ("-plugin-opt=Os") and this can be done by removing -Os option from ANDROID_COMPILER_FLAGS_RELEASE, instead pass this option directly to your makefiles, this way it will not be added to Clang LTO plugin. But since ANDROID_COMPILER_FLAGS_RELEASE is not user variable, the only way to do this is to comment two lines (510, 512) in ndk-bundle/build/cmake/android.toolchain.cmake inside Android Sdk folder

@jfirebaugh
Copy link

Is there any workaround that does not involve manually patching the NDK? We would like to use LTO for the Mapbox Maps SDK for Android, but are currently blocked by this issue (we need to also use -Os).

@alexgarret
Copy link

Hello guys,
I am facing the same issue with the NDKr18b, is there any official workaround ?

@gloinart
Copy link

I have the same issue (Optimization level must be between 0 and 3) in NDK19.2, when I pass -flto via gradle . What can I do, I want link time optimization!

@vok1980
Copy link

vok1980 commented May 21, 2019

What can I do, I want link time optimization!

Just set cmake -DCMAKE_CXX_FLAGS_RELEASE=-O2 ...

@gloinart
Copy link

So I cannot have link time optimization and -O3 at the same time?

@stephenhines
Copy link
Collaborator

O3 is fine. It's the -Os that is the problem. Stop passing that as part of "-plugin-opt=Os".

@DanAlbert
Copy link
Member

Stop passing that as part of "-plugin-opt=Os".

No one is doing this intentionally. Clang does this automatically when it is given -Os or -Oz, which is the NDK's default in some cases (arm32?)

@vok1980
Copy link

vok1980 commented May 22, 2019

So I cannot have link time optimization and -O3 at the same time?

You can pass any value from 0 to 3.

@gloinart
Copy link

If anyone else has this problem, add set(
CMAKE_CXX_FLAGS_RELEASE
"${CMAKE_CXX_FLAGS_RELEASE} -O3 -flto=full"
)
to you CMakeLists.txt rather than in the build.gradle file cppFlags parameter.

@ericoporto
Copy link

this issue is still happening.

@pirama-arumuga-nainar
Copy link
Collaborator

Fixed in upstream by https://reviews.llvm.org/D79919. The fix should be included in clang-r399163 or newer.

@snnn
Copy link

snnn commented Jul 30, 2021

I can confirm NDK r22 works good. The bug has been fixed in the release. But r21 is not ok.

alhah added a commit to alhah/leakcanary that referenced this issue Sep 20, 2021
Signed-off-by: xueqiushi <xueqiushi@kuaishou.com>
alhah added a commit to alhah/leakcanary that referenced this issue Sep 20, 2021
1. Support x86/x86_64 for emulator.
2. Optimize symbol search performance & reliability, support symbol in .gnu_debugdata.
3. Android R fast dump uses another implementation, which is consistent with the lower version.
4. Use c++17 instead of c++11 for make_unique.
5. Workaround for NDK bug: android/ndk#721

Signed-off-by: xueqiushi <xueqiushi@kuaishou.com>
@ericoporto
Copy link

@DanAlbert can you direct which commit/PR closed this issue?

@DanAlbert
Copy link
Member

#721 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests