Pol
Pol

Reputation: 4008

How to set build environment variables to use Clang as part of NDK?

The latest NDK release r11 says:

We strongly recommend switching to Clang.

How do you this in practice when using the NDK to build open source libraries like OpenSSL and you need to set all the usual environment variables like CPP, CC, CXX, etc...?

All I see in llvm/prebuilt/darwin-x86_64/bin/ is clang and clang++ but no cpp or ld tool, etc... contrary to what's in arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin which has every tool possible under the sun.

Are we supposed to mix and match? i.e. use ld and cpp from arm-linux-androideabi-4.9 while clang from llvm?

[Update] For reference, here's my configuration for using the r10 NDK:

API_LEVEL="18"

HOST="arm-linux-androideabi"

export ANDROID_NDK="$HOME/Library/Android/sdk/ndk-bundle"

export NDK_PLATFORM="$ANDROID_NDK/platforms/android-$API_LEVEL/arch-arm"
export NDK_TOOLCHAIN="$ANDROID_NDK/toolchains/$HOST-4.9/prebuilt/darwin-x86_64"

export CPP="$NDK_TOOLCHAIN/bin/$HOST-cpp --sysroot=$NDK_PLATFORM"
export CC="$NDK_TOOLCHAIN/bin/$HOST-gcc --sysroot=$NDK_PLATFORM"
export CXX="$NDK_TOOLCHAIN/bin/$HOST-g++ --sysroot=$NDK_PLATFORM"
export LD="$NDK_TOOLCHAIN/bin/$HOST-ld --sysroot=$NDK_PLATFORM"
export AR="$NDK_TOOLCHAIN/bin/$HOST-ar"
export RANLIB="$NDK_TOOLCHAIN/bin/$HOST-ranlib"

Upvotes: 3

Views: 8345

Answers (2)

Dan Albert
Dan Albert

Reputation: 10499

An alternative way to do this is to use build/tools/make_standalone_toolchain.py. That will build a full toolchain directory where you can directly invoke clang without having to worry about managing -gcc-toolchain and --sysroot yourself.

EDIT: Updated to refer to the new tool and removed the section of warnings since the problems there should all be fixed now.

Upvotes: 5

mstorsjo
mstorsjo

Reputation: 13317

Yes, when using clang, it falls back to using some of the normal gnu binutils from the normal gcc toolchain. (The clang/llvm environment is getting a linker of their own, lld, but I believe the builds in the NDK still just uses the gnu binutils version).

Contrary to the gcc toolchains, there's only one llvm/clang toolchain in the NDK, which can target all the necessary arches. This also means that you need to pass parameters to it to tell it what you're intending it to compile for.

For your build script, you should be able to replace at least CC and CXX with the following:

export CC="$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang -target armv7-none-linux-androideabi -gcc-toolchain $NDK_TOOLCHAIN"
export CXX="$ANDROID_NDK/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ -target armv7-none-linux-androideabi -gcc-toolchain $NDK_TOOLCHAIN"

For CPP, AR and RANLIB, you're probably best off just using the same as you are now. For LD, if you currently are referring directly to the low level ld tool, you should probably stick to that. If you're linking using the gcc frontend, you should switch that to using clang instead. But in that case, the scripts would probably be using $CC for linking instead of $LD anyway.

Upvotes: 5

Related Questions