[BUG] Incorrect math.h included from cmath for NDK r21
Description
When building GRPC 1.27 (also applicable all 1.24+ I tried earlier) or OpenCV 3.4.x (also applicable to 4.x) build fails due to inability to find math functions (from global namespace) included from cmath -> math.h. Most probable explanation is that sysroot/usr/include/c++/v1/cmath includes "generic C" sysroot/usr/include/math.h (where missing functions declared as macros and therefore no global namespace functions exeist) instead of "C++" sysroot/usr/include/c++/v1/math.h (that has inline declaration of missing functions and includes generic sysroot/usr/include/math.h via "include_next")
Problem doesn't exist on NDK r18-, where c++ support headers are explicitly taken from NDK/sources/cxx-stl/llvm-libc++/include/ and go ahead of NDK/sysroot/usr/include/ c headers in my command line for grpc/opencv. In contrast, NDK r19+ hides all include and library paths and there is no way to to specify c++ version of math.h instead of generic c version ...
Attached are grpc and opencv cmake build logs for arm32/api19 and arm64api23 (normal and verbose versions):
grpc.arm32ap19.log grpc.arm64api23.log opencv.arm32ap19.log opencv.arm64api23.log verbose-grpc.arm32ap19.log verbose-grpc.arm64api23.log verbose-opencv.arm32ap19.log verbose-opencv.arm64api23.log
Explicit change in NDKs "c++/v1/cmath" header from "include <math.h>" to "include <c++/v1/math.h>" results in inability to find generic c math.h with "include_next <math.h>"
Environment Details
- NDK Version: r21c a.k.a. 21.2.6472646 (also applicable to all earlier NDKs r19+ with new sysroot under llvm toolchain)
- Build system: cmake 3.16.x/3.17.x with toolchain file from NDK
- Host OS: Linux (CentOS 8)
- ABI/API: armeabi-v7a/19, arm64-v8a/23
It would be great if android team explicitly specifies list and the order of hidden includes by c/c++ llvm compiler in the documentation. same for list and the order of hidden library directories used by c/c++ llvm linker. i.e. what is hidden by "--target=..." c/c++ option
same problem on latest r21d
r21d was finished well before this bug was filed. Give us some time, we'll address it :)
Although at a glance this looks like a configuration error on your end. This looks a lot like what happens when you try to manually include the NDK's headers. That's all automatic and if you do any of it manually things end up out of order and it breaks.
It would be great if android team explicitly specifies list and the order of hidden includes by c/c++ llvm compiler in the documentation. same for list and the order of hidden library directories used by c/c++ llvm linker. i.e. what is hidden by "--target=..." c/c++ option
This is an implementation detail subject to change and shouldn't matter to the user at all. Could you help me understand why this information would be helpful?
I don't think that it is grpc/opencv build script problem. I've attached verbose build logs that show what cmd line has been generated by cmake 3.16/3.17 with NDK's toolchain file: you can see this nice --target={triple}{api} and nothing else i.e. exactly what is expected by NDK r19+ clang ... unfortunately there is a problem inside clang wrapper: either include directory is missing, or their order maybe incorrect.
for pre-r19 NDKs (with unified headers in sysroot) I had to create list of "-isystem" dirs manually, and it consists of: sources/cxx-stl/llvm-libc++/include sources/android/support/include sources/cxx-stl/llvm-libc++abi/include sysroot/usr/include sysroot/usr/include/{triple} when I used cmake to build some package, cmake created same/similar list of isystem dir for me.
Now, r19+ clang wrapper should use these: llvm...sysroot/usr/include/c++/v1 llvm...sysroot/usr/local/include llvm...sysroot/usr/include llvm...sysroot/usr/include/{triple} and also include/c++/4.9.x
@timofey-retailnext please follow https://github.com/VlSomers/native-opencv-android-template to build OpenCV 4.3 (or some earlier version) for Android.
@timofey-retailnext please follow https://github.com/VlSomers/native-opencv-android-template to build OpenCV 4.3 (or some earlier version) for Android.
@alexcohn , your example demonstrates usage of pre-built opencv binaries produced by opencv team (so called 'opencv sdk for android"), but it doesn't show how to build these libraries from sources with latest Android NDKs that have unified toolchain sysroot. And I'm sure that opencv team uses buch older Android NDK (one with unified headers or maybe with deprecated headers) - see their cmake toolchain file. Also, similar problem of wrong headers exists for Google GRPC.
Explicit change in NDKs "c++/v1/cmath" header from "include <math.h>" to "include <c++/v1/math.h>" results in inability to find generic c math.h with "include_next <math.h>"
That's not what I see in my r21c. Is that a local change? Nothing looks suspicious in the logs, but that change would certainly explain the problem.
Explicit change in NDKs "c++/v1/cmath" header from "include <math.h>" to "include <c++/v1/math.h>" results in inability to find generic c math.h with "include_next <math.h>"
That's not what I see in my r21c. Is that a local change? Nothing looks suspicious in the logs, but that change would certainly explain the problem.
@DanAlbert , I'm using standard NDK r21c (or r21d) without any modifications. Everything that I've reported, happens with standard unmodified NDK installations. Header cange I've mentioned above (change from math.h to c++/v1/math.h) was a naive attempt to fix this math.h issue. As I said earlier, it failed (and I returned everything back).
We're going to need a repro case then. Nothing obviously wrong on either end here. I can trivially prove that the NDK toolchain has no problem with #include <cmath> for the general case, and I don't see any red flags in the command lines in those logs.
We're going to need a repro case then. ok, need some time for that.
Instructions bon how to set up the build you described might be sufficient. Where do I get each of the packages, what versions, what hoops do I need to jump through to piece them all together, configure flags, etc. Unless I'm underestimating the hoops that should be enough to give me something to poke at :)
The preprocessed output of one of the failed cases might give some clues as well, and would be easier to gather. Just replace -c with -E and replace the -o argument with wherever you want the output to go. That includes information about what header was included when and can give some clues as to what went wrong.
Also adding -v to the compiler arguments and sending that output helps, since it'll show the compiler's include ordering, including the defaults. That might already be in one of the files above. I don't remember if I saw that earlier and it's a pain to view on a phone :)
@alexcohn , your example demonstrates usage of pre-built opencv binaries produced by opencv team (so called 'opencv sdk for android"), but it doesn't show how to build these libraries from sources with latest Android NDKs that have unified toolchain sysroot.
Sorry for that mess. But I have just recompiled the latest master of OpenCV with NDK 21.3.6528147 and cmake 3.10.2.4988404 (from Android SDK) and it all just worked.
python ../opencv/platforms/android/build_sdk.py --extra_modules_path ../opencv_contrib/modules . ../opencv
Closing for now. Will reopen if we get a repro case.
@DanAlbert , sorry, here is the sample. This case differs from real log producer in a) it builds ssl internally, while my real code uses prebuilt one b). make -j1 (real one uses multi-threaded)
What I noticed is that something introduces "-isystem=SYSROOT/usr/include" into command line ... Not sure if it is NDK's cmake.toolchain.file or cmake android support scripts, or something specific to GRPC build. But this looks like a the reason for reordered headers, isn't it ?
If that is enough, please reopen this for re-evaluation ...
As I remember, starting from cmake 3.16, they provide android support natively, i.e they do not need "toolchain files" at all. However NDK continues to offer a "toolchain file" for use with cmake. So what is recommended cmake build strategy (for 3.16+, latest now is 3.17.3) for NDKs r19+:
- use native cmake support / no toolchain file
- continue using toolchain file since it is compatible with both older (pre 3.16) and newer cmake releases ?
Was able to repro the failure with r21 and the problem is gone in r22.
As I remember, starting from cmake 3.16, they provide android support natively, i.e they do not need "toolchain files" at all.
They've supported some NDK since 3.7, the problem is that they're often out of sync and the full version matrix is not tested by either side afaik.
So what is recommended cmake build strategy (for 3.16+, latest now is 3.17.3) for NDKs r19+:
https://developer.android.com/ndk/guides/cmake#the_cmake_toolchain_file