Repository navigation
Description
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
enh commentedon 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 commentedon Jul 3, 2018
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 commentedon Jul 13, 2018
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 commentedon Jul 27, 2018
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 commentedon Mar 29, 2019
Hello guys,
I am facing the same issue with the NDKr18b, is there any official workaround ?
gloinart commentedon May 21, 2019
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 commentedon May 21, 2019
Just set
cmake -DCMAKE_CXX_FLAGS_RELEASE=-O2 ...gloinart commentedon May 21, 2019
So I cannot have link time optimization and -O3 at the same time?
stephenhines commentedon May 21, 2019
O3 is fine. It's the -Os that is the problem. Stop passing that as part of "-plugin-opt=Os".
DanAlbert commentedon May 21, 2019
No one is doing this intentionally. Clang does this automatically when it is given
-Osor-Oz, which is the NDK's default in some cases (arm32?)vok1980 commentedon May 22, 2019
You can pass any value from 0 to 3.
gloinart commentedon May 24, 2019
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 commentedon Feb 20, 2020
this issue is still happening.
pirama-arumuga-nainar commentedon Jul 27, 2020
Fixed in upstream by https://reviews.llvm.org/D79919. The fix should be included in clang-r399163 or newer.
snnn commentedon Jul 30, 2021
I can confirm NDK r22 works good. The bug has been fixed in the release. But r21 is not ok.
ericoporto commentedon Feb 4, 2022
@DanAlbert can you direct which commit/PR closed this issue?
DanAlbert commentedon Feb 16, 2022
#721 (comment)
Workaround ndk optimization bugs