Skip to content

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

Description

@TsvetanBogoev

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
`

Activity

  1. enh commented on Jul 3, 2018

    @enh
    Contributor

    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?

  2. pirama-arumuga-nainar commented on Jul 3, 2018

    @pirama-arumuga-nainar
    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.

  3. TsvetanBogoev commented on Jul 13, 2018

    @TsvetanBogoev
    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

  4. jfirebaugh commented on Jul 27, 2018

    @jfirebaugh

    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).

  5. added this to the external dependency milestone on Oct 19, 2018
  6. alexgarret commented on Mar 29, 2019

    @alexgarret

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

  7. gloinart commented on May 21, 2019

    @gloinart

    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!

  8. vok1980 commented on May 21, 2019

    @vok1980

    What can I do, I want link time optimization!

    Just set cmake -DCMAKE_CXX_FLAGS_RELEASE=-O2 ...

  9. gloinart commented on May 21, 2019

    @gloinart

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

  10. stephenhines commented on May 21, 2019

    @stephenhines

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

  11. DanAlbert commented on May 21, 2019

    @DanAlbert
    Contributor

    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?)

  12. vok1980 commented on May 22, 2019

    @vok1980

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

    You can pass any value from 0 to 3.

  13. gloinart commented on May 24, 2019

    @gloinart

    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.

  14. ericoporto commented on Feb 20, 2020

    @ericoporto

    this issue is still happening.

  15. pirama-arumuga-nainar commented on Jul 27, 2020

    @pirama-arumuga-nainar
    Collaborator

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

  16. snnn commented on Jul 30, 2021

    @snnn

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

  17. ericoporto commented on Feb 4, 2022

    @ericoporto

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

  18. DanAlbert commented on Feb 16, 2022

    @DanAlbert
    Contributor
  19. added a commit that references this issue on Dec 1, 2022
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions