Last active
April 2, 2016 02:39
-
-
Save gordonmcshane/452ad8c270a0aca6a96faf66b5bd5e39 to your computer and use it in GitHub Desktop.
Protocol Buffers Android build script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#fail fast | |
set -e | |
OUTPUT=`pwd` | |
TEMP=$(mktemp -d /tmp/build_protobuf.XXXXXX) | |
TOOLCHAIN_PATH=$TEMP/toolchain | |
ARCH=arm | |
TOOLCHAIN=arm-linux-androideabi-clang | |
HOST_TRIPLET=arm-linux-androideabi | |
#ARCH=x86 | |
#TOOLCHAIN=x86-clang | |
#HOST_TRIPLET=x86-linux-android | |
PLATFORM_VERSION=android-19 | |
PROTOBUF_VER=2.6.1 | |
NATIVE_PROTOC=protoc #assumes its available via path | |
WARNING_SUPRESSIONS="-Wno-unused-const-variable \ | |
-Wno-unused-local-typedef \ | |
-Wno-unused-function" | |
export PATH=$TOOLCHAIN_PATH/bin:$PATH | |
export SYSROOT=$TOOLCHAIN_PATH/sysroot | |
export CC=$TOOLCHAIN_PATH/bin/clang | |
export CXX=$TOOLCHAIN_PATH/bin/clang++ | |
case "$ARCH" in | |
arm) | |
CFLAGS="$CFLAGS -march=armv7-a -mfloat-abi=softfp -mfpu=neon" | |
CXXFLAGS="$CXXFLAGS -march=armv7-a -mfloat-abi=softfp -mfpu=neon" | |
export LDFLAGS="-march=armv7-a -Wl,--fix-cortex-a8" | |
;; | |
x86) | |
# x86 flags | |
;; | |
esac | |
export CFLAGS="$CFLAGS $(echo $WARNING_SUPRESSIONS | xargs)" | |
export CXXFLAGS="$CXXFLAGS $(echo $WARNING_SUPRESSIONS | xargs)" | |
if [ -d $TOOLCHAIN_PATH ]; then | |
echo "Deleting prior toolchain" | |
rm -r $TOOLCHAIN_PATH | |
fi | |
echo "CFLAGS = $CFLAGS" | |
echo "CXXFLAGS = $CXXFLAGS" | |
echo "LDFLAGS = $LDFLAGS" | |
echo "Creating standalone android toolchain in $TOOLCHAIN_PATH" | |
TOOLCHAIN_BUILD=$NDK_ROOT/build/tools/make-standalone-toolchain.sh | |
$TOOLCHAIN_BUILD --toolchain=$TOOLCHAIN \ | |
--arch=$ARCH \ | |
--platform=$PLATFORM_VERSION \ | |
--install-dir=$TOOLCHAIN_PATH | |
echo "Downloading protobuf $PROTOBUF_VER source from github" | |
curl -L -f \ | |
--output $TEMP/protobuf-$PROTOBUF_VER.tar.gz \ | |
"https://github.com/google/protobuf/releases/download/v$PROTOBUF_VER/protobuf-$PROTOBUF_VER.tar.gz" | |
if [ -d $TEMP/protobuf-$PROTOBUF_VER ]; then | |
echo "Deleting existing protobuf work path" | |
rm -rf $TEMP/protobuf-$PROTOBUF_VER | |
fi | |
cd $TEMP | |
echo "Untarring protobuf" | |
tar xvf $TEMP/protobuf-$PROTOBUF_VER.tar.gz | |
cd $TEMP/protobuf-$PROTOBUF_VER/ | |
mkdir install | |
echo "Autotools configuration" | |
./configure --prefix=$(pwd)/install \ | |
--with-sysroot=$SYSROOT \ | |
--host=$HOST_TRIPLET \ | |
--disable-shared \ | |
--enable-cross-compile \ | |
--with-protoc=$NATIVE_PROTOC > /dev/null | |
echo "Compiling" | |
make > /dev/null && make install > /dev/null | |
`find $TOOLCHAIN_PATH/bin/ -name "*readelf"` -A install/lib/libprotobuf-lite.a | tail -n 20 | |
echo "Compilation successful" | |
echo "Copying libs to lib/" | |
if [ ! -d $OUTPUT/lib ]; then | |
mkdir $OUTPUT/lib | |
fi | |
cp install/lib/libprotobuf.a $OUTPUT/lib/libprotobuf.a | |
cp install/lib/libprotobuf-lite.a $OUTPUT/lib/libprotobuf-lite.a | |
cp install/lib/libprotoc.a $OUTPUT/lib/libprotoc.a | |
echo "Copying headers to include/" | |
if [ ! -d $OUTPUT/include ]; then | |
mkdir $OUTPUT/include | |
fi | |
cp -R install/include $OUTPUT/ | |
echo "Copying binaries (protoc) to bin/" | |
if [ ! -d $OUTPUT/bin ]; then | |
mkdir $OUTPUT/bin | |
fi | |
cp install/bin/protoc $OUTPUT/bin/protoc | |
rm -r $TEMP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment