openscenegraph / OpenSceneGraph

OpenSceneGraph git repository
http://www.openscenegraph.org
Other
3.24k stars 1.42k forks source link

CMake fails to generate Android build using NDK 19c and after. #728

Closed ccochran closed 5 years ago

ccochran commented 5 years ago

The Android build works fine in all previous versions of the NDK (18 and below), however as of 19c (latest) CMake 3.14 errors out, running on macOS. I think @tomhog solved a very similar issue for iOS recently, maybe he could provide some insight. I've been trying to fix without success, however I did notice clang was upgraded to 8.0.2 from the 7 series in NDK 19c.

CMake (3.14) command:

cmake .. \
-DCMAKE_BUILD_TYPE="Release" \
-DCMAKE_INSTALL_PREFIX=./osg-arm64-v8a-install \
-DCMAKE_SYSTEM_NAME="Android" \
-DCMAKE_SYSTEM_VERSION=28 \
-DCMAKE_ANDROID_NDK=/Applications/android/android-ndk-r19c \
-DOPENGL_PROFILE="GLES1" \
-DDYNAMIC_OPENTHREADS=OFF \
-DDYNAMIC_OPENSCENEGRAPH=OFF \
-DCMAKE_ANDROID_ARCH_ABI=arm64-v8a

Output:

-- Android: Targeting API '28' with architecture 'arm64', ABI 'arm64-v8a', and processor 'aarch64'
-- Android: Selected Clang toolchain 'aarch64-linux-android-clang' with GCC toolchain ''
-- The C compiler identification is Clang 8.0.2
-- The CXX compiler identification is Clang 8.0.2
-- Check for working C compiler: /Applications/android/android-ndk-r19c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang
-- Check for working C compiler: /Applications/android/android-ndk-r19c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -- broken
CMake Error at /Applications/CMake.app/Contents/share/cmake-3.14/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "/Applications/android/android-ndk-r19c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /Users/ccochran/Documents/Developer/git/third-party/OpenSceneGraph/build-android-arm64-v8a-ndk19/CMakeFiles/CMakeTmp

    Run Build Command(s):/usr/bin/make cmTC_436d9/fast 
    /Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_436d9.dir/build.make CMakeFiles/cmTC_436d9.dir/build
    Building C object CMakeFiles/cmTC_436d9.dir/testCCompiler.c.o
    /Applications/android/android-ndk-r19c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=aarch64-none-linux-android --gcc-toolchain=/Applications/android/android-ndk-r19c/toolchains//prebuilt/darwin-x86_64 --sysroot=/Applications/android/android-ndk-r19c/sysroot  -isystem /Applications/android/android-ndk-r19c/sysroot/usr/include -isystem /Applications/android/android-ndk-r19c/sysroot/usr/include/aarch64-linux-android  -funwind-tables -no-canonical-prefixes -D__ANDROID_API__=28 -fexceptions  -g -fPIE   -o CMakeFiles/cmTC_436d9.dir/testCCompiler.c.o   -c /Users/ccochran/Documents/Developer/git/third-party/OpenSceneGraph/build-android-arm64-v8a-ndk19/CMakeFiles/CMakeTmp/testCCompiler.c
    Linking C executable cmTC_436d9
    /Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/cmTC_436d9.dir/link.txt --verbose=1
    /Applications/android/android-ndk-r19c/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang --target=aarch64-none-linux-android --gcc-toolchain=/Applications/android/android-ndk-r19c/toolchains//prebuilt/darwin-x86_64 --sysroot=/Applications/android/android-ndk-r19c/platforms/android-28/arch-arm64 -funwind-tables -no-canonical-prefixes -D__ANDROID_API__=28 -fexceptions  -g  -Wl,--gc-sections  CMakeFiles/cmTC_436d9.dir/testCCompiler.c.o  -o cmTC_436d9 
    ld: unknown option: --sysroot=/Applications/android/android-ndk-r19c/platforms/android-28/arch-arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[1]: *** [cmTC_436d9] Error 1
    make: *** [cmTC_436d9/fast] Error 2

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:113 (PROJECT)

-- Configuring incomplete, errors occurred!
ccochran commented 5 years ago

I'm making progress on this issue... it's a matter of setting the correct toolchain path.

tomhog commented 5 years ago

Hi @ccochran

If it's any help we have VulkanSceneGraph working with the latest NDK on Android

https://github.com/vsg-dev/VulkanSceneGraphPrototype/blob/master/CMakeLists.txt

The CMakeLists.txt is a lot simpler than osg and is using C++17 so maybe the defaults for the newest NDK aren't playing nicely with some of the older features osg is needing.

If you have any more issues post them here and I can take a peek (sorry for the slow reply on this one)

ccochran commented 5 years ago

Hi @tomhog,

Thanks, I've been peeking over there ;) -- NP I know you guys are hard at work on VSG.

From what I understand, CMake now natively supports Android but conflicts with the NDK version of CMake (each have their own toolchain). In addition, OSG has a ~6 year old toolchain that threw me off.

In any case, I successfully built OSG 3.6.4 and 3rd party decencies for Android 64bit platforms (I only need png, jpeg, freetype, and zlib) and my app is running solid on Android 64bit test devices. I used the CMake 3.14 native toolchain, however now I'm experimenting with using the NDK's toolchain because I feel like that would be more appropriate.

Once I get things sorted out I'll submit a PR with changes for review.

Are you using the NDK CMake (3.6 i think) and toolchain or CMake's native toolchain for Vulkan? Thanks

ccochran commented 5 years ago

I successfully built OSG for Android using NDK r19c, CMake 3.14, and most importantly, the NDK toolchain. Although CMake now supports Android natively with its own toolchain and settings, its use will soon be deprecated due to conflicts with Google's toolchains https://gitlab.kitware.com/cmake/cmake/issues/18787.

Here is an excerpt from my script I wrote to build OSG for all Android ABIs.

cmake ../ \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_TOOLCHAIN_FILE=${ndkHome}/build/cmake/android.toolchain.cmake \
      -DANDROID_STL=c++_static \
      -DANDROID_TOOLCHAIN=clang \
      -DANDROID_PLATFORM=android-21 \
      -DANDROID_ABI=${abi} \
      -DCMAKE_INSTALL_PREFIX="../${installDir}/${abi}" \
      -DOPENGL_PROFILE="GLES1" \
      -DDYNAMIC_OPENTHREADS=OFF \
      -DDYNAMIC_OPENSCENEGRAPH=OFF

This was the most useful post that helped me along the way: https://gitlab.kitware.com/cmake/cmake/issues/18739#note_510784

@tomhog you may want to consider using the NDK toolchain for VSG based on these findings.