Reputation: 55
I'm trying to cross compile grpc C++ (https://github.com/grpc/grpc) for Android from Ubuntu using Android NDK CLANG compiler (arm-linux-androideabi-clang++).
I've tried below way to compile it for Android.
export PATH=/root/Android/arm-26-toolchain-clang/bin:$PATH
export SYSROOT=/root/Android/arm-26-toolchain-clang/sysroot
export CC="arm-linux-androideabi-clang --sysroot $SYSROOT"
export CXX="arm-linux-androideabi-clang++ --sysroot $SYSROOT"
make CFLAGS='-std=c11 -march=armv7-a -D__ANDROID_API__=26' CXXFLAGS='- std=c++11 -frtti -fexceptions -march=armv7-a -D__ANDROID_API__=26' LIBS='-llog -lz -lc++_static' -j2
It is throwing error while compiling zlib.c - error: implicit declaration of function 'lseek' is invalid in C99
How to specify to build using C11 using the above CLANG compiler?
When I allowed it to move ahead ignoring this error, it stopped at below -
./src/core/lib/surface/completion_queue.h:92:5: error: unknown type name
'grpc_experimental_completion_queue_functor'
grpc_experimental_completion_queue_functor* shutdown_callback);
I tried to compile the example from https://github.com/grpc/grpc/tree/master/examples/android/helloworld but it throws error in compiling boringssl.
Could you please help cross compiling gRPC for android for toolchain arm-linux-androideabi?
Thanks
Upvotes: 1
Views: 3300
Reputation: 1369
You may build grpc libraries using cmake
toolchain:
cd <your build directory>
export ANDROID_NDK=<path to your NDK>
cmake <path to your grpc source directory>\
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake\
-DANDROID_ABI=armeabi-v7a\
-DANDROID_PLATFORM=android-26\
-DANDROID_STL=c++_static\
-DRUN_HAVE_STD_REGEX=0\
-DRUN_HAVE_POSIX_REGEX=0\
-DRUN_HAVE_STEADY_CLOCK=0\
-DCMAKE_BUILD_TYPE=Release
cmake --build . --target grpc++
When the build is finished, all required static libraries should be generated:
find . -type f -name "*.a"
./libgrpc.a
./third_party/zlib/libz.a
./third_party/cares/cares/lib/libcares.a
./third_party/boringssl/crypto/libcrypto.a
./third_party/boringssl/ssl/libssl.a
./third_party/protobuf/libprotobuf.a
./libgpr.a
./libaddress_sorting.a
./libgrpc++.a
Upvotes: 2
Reputation: 55
I could build it following the cross compile instructions in the Makefile in grpc github repository. Created a shell script like below and it worked -
export GRPC_CROSS_COMPILE=true
export PATH=/home/ubuntu/stand-alone-toolchain/arm-26-toolchain-clang/bin:$PATH
export SYSROOT=/home/ubuntu/stand-alone-toolchain/arm-26-toolchain-clang/sysroot
export HOST_CC="/usr/bin/gcc"
export HOST_CXX="/usr/bin/g++"
export HOST_LD="/usr/bin/ld"
export CC="arm-linux-androideabi-clang --sysroot $SYSROOT"
export CXX="arm-linux-androideabi-clang++ --sysroot $SYSROOT"
export LD="arm-linux-androideabi-clang++"
export LDXX="arm-linux-androideabi-clang++"
export AR="arm-linux-androideabi-ar"
export STRIP="arm-linux-androideabi-strip"
export PROTOBUF_CONFIG_OPTS="--host=arm-linux-androideabi --with-sysroot=${SYSROOT} --with-protoc=/usr/local/bin/protoc CFLAGS='-march=armv7-a -D__ANDROID_API__=26' CXXFLAGS='-frtti -fexceptions -march=armv7-a -D__ANDROID_API__=26' LIBS='-llog -lz -lc++_static'"
export HAS_PKG_CONFIG=false
export GRPC_CROSS_LDOPTS="-L$SYSROOT -L/home/ubuntu/cross-grpc/grpc"
export GRPC_CROSS_AROPTS="rc --target=elf32-little"
make
Upvotes: 2