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

CMake toolchain file doesn't support building with clang in r16 beta 1 #504

Closed
tiwoc opened this issue Sep 4, 2017 · 8 comments
Closed

Comments

@tiwoc
Copy link

tiwoc commented Sep 4, 2017

Description

When using the android.toolchain.cmake file shipped with NDK r16 beta 1 with the clang compiler, the build fails due to wrong CMAKE_C_COMPILER and CMAKE_CXX_COMPILER paths.

Here's a minimal example, run with CMake 3.9.1:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(CMakeAndroidTest)
add_executable(cmake-android-test main.cpp)

main.cpp:

int main() {
    return 0;
}
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/ndk-r16-beta1/build/cmake/android.toolchain.cmake -DANDROID_TOOLCHAIN=clang
-- Android: Targeting API '14' with architecture 'arm', ABI 'armeabi-v7a', and processor 'armv7-a'
-- Android: Selected Clang toolchain 'arm-linux-androideabi-clang' with GCC toolchain 'arm-linux-androideabi-4.9'
CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_C_COMPILER:

    /path/to/ndk-r16-beta1/toolchains//prebuilt/linux-x86_64/bin/clang

  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.


CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_CXX_COMPILER:

    /path/to/ndk-r16-beta1/toolchains//prebuilt/linux-x86_64/bin/clang++

  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.

Notice the missing llvm in the path used by CMake.

When setting ANDROID_TOOLCHAIN to gcc or downgrading to NDK r15c, the build is fixed. Also, downgrading to CMake 3.6 (which predates the built-in Android support in CMake) fixes the build. The issue also occurs when invoking the built-in Android support of CMake directly, see my bug report at CMake.

Workaround

Change android.toolchain.cmake:

- set(CMAKE_SYSTEM_NAME Android)
+ set(CMAKE_SYSTEM_NAME CustomAndroid)

This prevents CMake from using its built-in Android support.

Environment Details

  • NDK Version: 16.0.4293906-beta1
  • Build sytem: cmake
  • Host OS: Fedora 26, x86_64
  • Compiler: clang
  • ABI: all
  • STL: all
  • NDK API level: all
@AstralStorm
Copy link

AstralStorm commented Sep 5, 2017

This has been fixed (and works for me) with 16.0.4293906 rc1. Albeit I tried with Android Studio version of CMake - 3.6.4111459.

@tiwoc
Copy link
Author

tiwoc commented Sep 5, 2017

It has not been fixed yet. We both used the same build. When you use CMake 3.6.x or below, the issue doesn't occur because CMake's built-in Android support was only added in 3.7. The issue is that CMake's Android support and the NDK 16's toolchain file don't work together properly.

@tiwoc
Copy link
Author

tiwoc commented Sep 5, 2017

This will be fixed in CMake 3.9.2: https://gitlab.kitware.com/cmake/cmake/merge_requests/1237

If nothing is done on the NDK side, NDK 16 won't work with CMake 3.7 to 3.9.1. This is fine by me, but should be decided by the NDK maintainers.

@DanAlbert
Copy link
Member

In theory https://android-review.googlesource.com/c/platform/ndk/+/485299 protects us from all the cmake version incompatibilities. Longer term we'll be exporting most of the tables CMake cares about so the two stop fighting each other, but this works as a stopgap.

@hpg-mheck
Copy link

hpg-mheck commented Nov 25, 2017

I feel compelled to point out that this causes failures in simple "Hello World" projects out of the box. These sort of experiences are exactly why I've aborted using five different installs, over the years, of Android Studio, always waiting for a high enough minor version that things were stable-- only to never see things quite get there, but instead to see a major version bump, and all of the instability naturally associated with X.0.[0,1]. I keep wondering if Android Studio is ever going to be a project that knows how to push the release until ALL of the "simple" new project cases actually build.

@hpg-mheck
Copy link

hpg-mheck commented Nov 25, 2017

In any case, steps to reproduce for Hello World are:

  1. Restart a fresh Android Studio 3.0.1 install on Fedora 27 Linux. NUKE THE WORLD before you do this, rm -rf anything with "ndroid" in the name anywhere on the filesystem, or use a VM. Pessimistic clean, please.
  2. "Start a New Android Studio Project".
  3. Ensure "Include C++ support" is checked.
  4. Click "Phone and Tablet", API 21 (Android 5.0) and click Next.
  5. Select "Basic Activity" and click Next.
  6. Click Next.
  7. Leave C++ standard at Toolchain Default, check Exception Support, click Finish.
  8. Gradle project sync will immediately fail:

Error:(61, 0) The C compiler "/home/mheck/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang" is not able to compile a simple test program.
Open File

@DanAlbert
Copy link
Member

Trivial cases are not broken. We have tests that show they work.

If you're experiencing the problem discussed in this bug, you're not using the out of the box experience. Studio provides its own version of CMake (a version of 3.6 with a few modifications to better support IDE functionality) and uses the CMake toolchain file shipped with the NDK. This bug is regarding using a CMake other than the one that was shipped with Studio.

Are you using NDK r16? If not, you don't have the fix for this.

If you are, please file a bug against Studio (http://b.android.com). As I've said, our test cases show no issues here, so if the hello world use case is broken it may be that studio is using the NDK incorrectly.

@Ni30patel
Copy link

@DanAlbert I am facing similar kind of problem but its with r17 of ndk . I am just creating simple hello world project using C++ support using android stuido as @hpg-mheck done. After building of project gives me below error.

Caused by: org.gradle.internal.UncheckedException: Build command failed. Error while executing process D:\Android_Studio\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Android_Studio\Workspace\NDKTestSample\app.externalNativeBuild\cmake\debug\x86 --target native-lib} [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o FAILED: D:\Android_Studio\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android --gcc-toolchain=D:/Android_Studio/sdk/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64 --sysroot=D:/Android_Studio/sdk/ndk-bundle/sysroot -Dnative_lib_EXPORTS -isystem D:/Android_Studio/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include -isystem D:/Android_Studio/sdk/ndk-bundle/sources/android/support/include -isystem D:/Android_Studio/sdk/ndk-bundle/sources/cxx-stl/llvm-libc++abi/include -isystem D:/Android_Studio/sdk/ndk-bundle/sysroot/usr/include/i686-linux-android -D__ANDROID_API__=15 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles\native-lib.dir\src\main\cpp\native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c D:\Android_Studio\Workspace\NDKTestSample\app\src\main\cpp\native-lib.cpp error: error reading 'D:\Android_Studio\Workspace\NDKTestSample\app\src\main\cpp\native-lib.cpp' 1 error generated. ninja: build stopped: subcommand failed.

NDK Version: 17.1.4828580
Build system: default ndk system. I dont know much about it. Nothing customized to build
Host OS: Windows 10
Compiler: Clang
ABI: all
Cmake: 3.6.4111459
android studio: 3.1.3
Gradle: 4.4
Android Plugin for Gradle: 3.1.3
I have already reported it here and on stackoverflow

Dschoordsch pushed a commit to heremaps/gluecodium that referenced this issue Nov 15, 2018
The CMake toolchain shipped with the NDK doesn't work with CMake-3.7.2
or 3.9.1. To ensure compatibility, use the shipped cmake binary. See
android/ndk#254 or
https://gitlab.kitware.com/cmake/cmake/issues/16708 as well as
android/ndk#504

Change-Id: I31366cd813edc44d0e62021872c63d9fc06ffb7c
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

5 participants