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

Improved the build process for cpprestsdk on Android #714

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 99 additions & 102 deletions Build_android/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

set -e

# Note: we require android ndk r10e available from
# http://dl.google.com/android/ndk/android-ndk-r10e-linux-x86_64.tar.bz2
# http://dl.google.com/android/ndk/android-ndk-r10e-windows-x86_64.zip
# The Android NDK r10e or later is required. To download, see the following link:
# https://developer.android.com/ndk/downloads/index.html

# -----------------
# Parse args
Expand All @@ -25,14 +24,24 @@ DO_BOOST=1
DO_OPENSSL=1
DO_CPPRESTSDK=1

BOOSTVER=1.59.0
OPENSSLVER=1.0.2k

API=15
STL=c++_static

function usage {
echo "Usage: $0 [--skip-boost] [--skip-openssl] [--skip-cpprestsdk] [-h] [--ndk <android-ndk>]"
echo ""
echo " --skip-boost Skip fetching and compiling boost"
echo " --skip-openssl Skip fetching and compiling openssl"
echo " --skip-cpprestsdk Skip compiling cpprestsdk"
echo " -h,--help,-? Display this information"
echo " --boost <version> Override the Boost version to build (default is ${BOOSTVER})"
echo " --openssl <version> Override the OpenSSL version to build (default is ${OPENSSLVER})"
echo " --ndk <android-ndk> If specified, overrides the ANDROID_NDK environment variable"
echo " --api <level> Selects the Android API level (default is ${API})"
echo " --stl <runtime> Selects the C++ runtime (c++_shared, c++_static, gnustl_shared, gnustl_static)"
echo " -h,--help,-? Display this information"
}

while [[ $# > 0 ]]
Expand All @@ -47,14 +56,32 @@ do
"--skip-cpprestsdk")
DO_CPPRESTSDK=0
;;
"-?"|"-h"|"--help")
usage
exit
"--boost")
shift
DO_BOOST=1
BOOSTVER=$1
;;
"--openssl")
shift
DO_OPENSSL=1
OPENSSLVER=$1
;;
"--ndk")
shift
export ANDROID_NDK=$1
;;
"--api")
shift
API=$1
;;
"--stl")
shift
STL=$1
;;
"-?"|"-h"|"--help")
usage
exit
;;
*)
usage
exit 1
Expand Down Expand Up @@ -107,13 +134,12 @@ then
(
if [ ! -d "openssl" ]; then mkdir openssl; fi
cd openssl
cp "${DIR}/openssl/Makefile" .
export ANDROID_NDK_ROOT="${NDK_DIR}"
make all
cp -af "${DIR}/openssl/." .
make all ANDROID_NDK="${NDK_DIR}" ANDROID_TOOLCHAIN=clang ANDROID_GCC_VERSION=4.9 ANDROID_API=$API ANDROID_ABI=armeabi-v7a OPENSSL_PREFIX=armeabi-v7a OPENSSL_VERSION=$OPENSSLVER
make all ANDROID_NDK="${NDK_DIR}" ANDROID_TOOLCHAIN=clang ANDROID_GCC_VERSION=4.9 ANDROID_API=$API ANDROID_ABI=x86 OPENSSL_PREFIX=x86 OPENSSL_VERSION=$OPENSSLVER
)
fi


# -----
# Boost
# -----
Expand All @@ -136,106 +162,77 @@ then
touch cpprestsdk.patched.stamp
fi

PATH="$PATH:$NDK_DIR" ./build-android.sh --boost=1.55.0 --with-libraries=random,date_time,filesystem,system,thread,chrono "${NDK_DIR}" || exit 1
)

(
if [ ! -d "Boost-for-Android-x86" ]
then
git clone Boost-for-Android Boost-for-Android-x86
fi
cd Boost-for-Android-x86
if [ ! -e "cpprestsdk.patched.stamp" ]
then
git checkout 1c95d349d5f92c5ac1c24e0ec6985272a3e3883c
git reset --hard HEAD
git apply "$DIR/boost-for-android-x86.patch"
ln -s ../Boost-for-Android/boost_1_55_0.tar.bz2 .
touch cpprestsdk.patched.stamp
fi

PATH="$PATH:$NDK_DIR" ./build-android.sh --boost=1.55.0 --with-libraries=atomic,random,date_time,filesystem,system,thread,chrono "${NDK_DIR}" || exit 1
)
PATH="$PATH:$NDK_DIR" \
CXXFLAGS="-std=gnu++11" \
./build-android.sh \
--boost=$BOOSTVER \
--arch=armeabi-v7a,x86 \
--with-libraries=atomic,random,date_time,filesystem,system,thread,chrono \
--api=$API \
--stl=$STL \
"${NDK_DIR}" || exit 1
)
fi

if [ "${DO_CPPRESTSDK}" == "1" ]
then
(
# -------------
# android-cmake
# -------------
if [ ! -e android-cmake ]
then
git clone https://github.com/taka-no-me/android-cmake.git
fi

# ----------
# casablanca
# ----------

(
mkdir -p build.armv7.debug
cd build.armv7.debug
cmake "$DIR/../Release/" \
-DCMAKE_TOOLCHAIN_FILE=../android-cmake/android.toolchain.cmake \
-DANDROID_ABI=armeabi-v7a \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-clang3.8 \
-DANDROID_STL=none \
-DANDROID_STL_FORCE_FEATURES=ON \
-DANDROID_NATIVE_API_LEVEL=android-9 \
-DANDROID_GOLD_LINKER=OFF \
-DCMAKE_BUILD_TYPE=Debug \
-DANDROID_NDK="${ANDROID_NDK}"
make -j 1
)
if [ "${DO_CPPRESTSDK}" == "1" ]
then
(

(
mkdir -p build.armv7.release
cd build.armv7.release
cmake "$DIR/../Release/" \
-DCMAKE_TOOLCHAIN_FILE=../android-cmake/android.toolchain.cmake \
-DANDROID_ABI=armeabi-v7a \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-clang3.8 \
-DANDROID_STL=none \
-DANDROID_STL_FORCE_FEATURES=ON \
-DANDROID_NDK="${ANDROID_NDK}" \
-DANDROID_NATIVE_API_LEVEL=android-9 \
-DANDROID_GOLD_LINKER=OFF \
-DCMAKE_BUILD_TYPE=Release
make -j 1
)
# Determine which CMake toolchain configuartion to use
if [ -f "${NDK_DIR}/build/cmake/android.toolchain.cmake" ]; then
# Use the builtin CMake toolchain configuration that comes with the NDK
function build_cpprestsdk {
(
mkdir -p $1
cd $1
cmake "${DIR}/../Release/" \
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
-DANDROID_NDK="${ANDROID_NDK}" \
-DANDROID_TOOLCHAIN=clang \
-DANDROID_STL=$STL \
-DANDROID_ABI=$2 \
-DANDROID_PLATFORM=android-$API \
-DANDROID_NATIVE_API_LEVEL=android-$API \
-DBOOST_VERSION="${BOOSTVER}" \
-DCMAKE_BUILD_TYPE=$3
make -j 1
)
}
else
# Download android-cmake for use with NDK r10e
if [ ! -e android-cmake ]; then
git clone https://github.com/taka-no-me/android-cmake.git
fi

(
mkdir -p build.x86.debug
cd build.x86.debug
cmake "$DIR/../Release/" \
-DCMAKE_TOOLCHAIN_FILE=../android-cmake/android.toolchain.cmake \
-DANDROID_ABI=x86 \
-DANDROID_TOOLCHAIN_NAME=x86-clang3.8 \
-DANDROID_STL=none \
-DANDROID_STL_FORCE_FEATURES=ON \
-DANDROID_NATIVE_API_LEVEL=android-9 \
-DANDROID_GOLD_LINKER=OFF \
-DCMAKE_BUILD_TYPE=Debug \
-DANDROID_NDK="${ANDROID_NDK}"
make -j 1
)
function build_cpprestsdk {
(
mkdir -p $1
cd $1
cmake "${DIR}/../Release/" \
-DCMAKE_TOOLCHAIN_FILE="../android-cmake/android.toolchain.cmake" \
-DANDROID_NDK="${ANDROID_NDK}" \
-DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-clang3.8 \
-DANDROID_STL=$STL \
-DANDROID_ABI=$2 \
-DANDROID_STL_FORCE_FEATURES=ON \
-DANDROID_NATIVE_API_LEVEL=android-$API \
-DANDROID_GOLD_LINKER=OFF \
-DBOOST_VERSION="${BOOSTVER}" \
-DCMAKE_BUILD_TYPE=$3
make -j 1
)
}
fi

# Build the cpprestsdk for each target configuration
build_cpprestsdk build.armv7.debug armeabi-v7a Debug
build_cpprestsdk build.armv7.release armeabi-v7a Release
build_cpprestsdk build.x86.debug x86 Debug
build_cpprestsdk build.x86.release x86 Release

(
mkdir -p build.x86.release
cd build.x86.release
cmake "$DIR/../Release/" \
-DCMAKE_TOOLCHAIN_FILE=../android-cmake/android.toolchain.cmake \
-DANDROID_ABI=x86 \
-DANDROID_TOOLCHAIN_NAME=x86-clang3.8 \
-DANDROID_STL=none \
-DANDROID_STL_FORCE_FEATURES=ON \
-DANDROID_NDK="${ANDROID_NDK}" \
-DANDROID_NATIVE_API_LEVEL=android-9 \
-DANDROID_GOLD_LINKER=OFF \
-DCMAKE_BUILD_TYPE=Release
make -j 1
)
)
fi
28 changes: 4 additions & 24 deletions Release/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,15 @@ else()
endif()

set(WARNINGS)
set(ANDROID_STL_FLAGS)
set(ANDROID_LIBS)

# Platform (not compiler) specific settings
if(IOS)
# The cxx_flags must be reset here, because the ios-cmake toolchain file unfortunately sets "-headerpad_max_install_names" which is not a valid clang flag.
set(CMAKE_CXX_FLAGS "-fvisibility=hidden -fvisibility-inlines-hidden")
elseif(ANDROID)
if(ARM)
set(LIBCXX_STL "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/thumb/libgnustl_static.a")
else()
set(LIBCXX_STL "${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/libs/x86/libgnustl_static.a")
endif()
# These are used in the shared library case
set(ANDROID_STL_FLAGS
${LIBCXX_STL}
atomic
dl
gcc
c
m
-nodefaultlibs
)

include_directories(SYSTEM
"${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/include"
"${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include"
"${ANDROID_NDK}/sources/cxx-stl/gnu-libstdc++/4.8/include/backward"
)
set(ANDROID_LIBS atomic dl)
elseif(UNIX) # This includes OSX
elseif(WIN32)
add_definitions(-DUNICODE -D_UNICODE -DWIN32 -D_SCL_SECURE_NO_WARNINGS)
Expand All @@ -162,8 +143,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR IOS)
message("-- Setting clang options")

if(ANDROID)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-attributes -Wno-pointer-arith")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic -Wno-attributes -Wno-pointer-arith")
elseif(CMAKE_SYSTEM_NAME MATCHES "Linux")
set(WARNINGS -Wall -Wextra -Wcast-qual -Wconversion -Wformat=2 -Winit-self -Winvalid-pch -Wmissing-format-attribute -Wmissing-include-dirs -Wpacked -Wredundant-decls)
set(LINUX_SUPPRESSIONS -Wno-overloaded-virtual -Wno-sign-conversion -Wno-deprecated -Wno-unknown-pragmas -Wno-reorder -Wno-char-subscripts -Wno-switch -Wno-unused-parameter -Wno-unused-variable -Wno-deprecated -Wno-unused-value -Wno-unknown-warning-option -Wno-return-type-c-linkage -Wno-unused-function -Wno-sign-compare -Wno-shorten-64-to-32 -Wno-reorder -Wno-unused-local-typedefs)
Expand Down Expand Up @@ -233,7 +213,7 @@ function(add_casablanca_test NAME SOURCES_VAR)
cpprest
common_utilities
unittestpp
${ANDROID_STL_FLAGS}
${ANDROID_LIBS}
)
if (BUILD_SHARED_LIBS)
add_test(NAME ${NAME}
Expand Down
30 changes: 25 additions & 5 deletions Release/cmake/cpprest_find_boost.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
macro(cpprestsdk_find_boost_android_package)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
if(CMAKE_HOST_WIN32)
set(WIN32 1)
set(UNIX)
elseif(CMAKE_HOST_APPLE)
set(APPLE 1)
set(UNIX)
endif()
find_package(${ARGN})
set(APPLE)
set(WIN32)
set(UNIX 1)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
endmacro()

function(cpprest_find_boost)
if(TARGET cpprestsdk_boost_internal)
return()
Expand All @@ -10,13 +30,13 @@ function(cpprest_find_boost)
elseif(ANDROID)
set(Boost_COMPILER "-clang")
if(ARM)
set(BOOST_ROOT "${CMAKE_BINARY_DIR}/../Boost-for-Android/build" CACHE INTERNAL "")
set(BOOST_LIBRARYDIR "${CMAKE_BINARY_DIR}/../Boost-for-Android/build/lib" CACHE INTERNAL "")
set(BOOST_ROOT "${CMAKE_BINARY_DIR}/../Boost-for-Android/build/out/armeabi-v7a" CACHE INTERNAL "")
set(BOOST_LIBRARYDIR "${CMAKE_BINARY_DIR}/../Boost-for-Android/build/out/armeabi-v7a/lib" CACHE INTERNAL "")
else()
set(BOOST_ROOT "${CMAKE_BINARY_DIR}/../Boost-for-Android-x86/build" CACHE INTERNAL "")
set(BOOST_LIBRARYDIR "${CMAKE_BINARY_DIR}/../Boost-for-Android-x86/build/lib" CACHE INTERNAL "")
set(BOOST_ROOT "${CMAKE_BINARY_DIR}/../Boost-for-Android/build/out/x86" CACHE INTERNAL "")
set(BOOST_LIBRARYDIR "${CMAKE_BINARY_DIR}/../Boost-for-Android/build/out/x86/lib" CACHE INTERNAL "")
endif()
find_host_package(Boost 1.55 EXACT REQUIRED COMPONENTS random system thread filesystem chrono atomic)
cpprestsdk_find_boost_android_package(Boost ${BOOST_VERSION} EXACT REQUIRED COMPONENTS random system thread filesystem chrono atomic)
elseif(UNIX)
find_package(Boost REQUIRED COMPONENTS random system thread filesystem chrono atomic date_time regex)
else()
Expand Down
6 changes: 4 additions & 2 deletions Release/include/cpprest/details/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
#include "cpprest/details/cpprest_compat.h"

#ifndef _WIN32
# define __STDC_LIMIT_MACROS
# include <stdint.h>
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#include <stdint.h>
#else
#include <cstdint>
#endif
Expand Down
2 changes: 1 addition & 1 deletion Release/src/http/client/http_client_asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ class asio_context : public request_context, public std::enable_shared_from_this
timedout
};

#if defined(ANDROID) || defined(__ANDROID__)
#if (defined(ANDROID) || defined(__ANDROID__)) && !defined(_LIBCPP_VERSION)
boost::chrono::microseconds m_duration;
#else
std::chrono::microseconds m_duration;
Expand Down
2 changes: 1 addition & 1 deletion Release/src/pch/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#include <atomic>
#include <signal.h>
#include "pthread.h"
#if (defined(ANDROID) || defined(__ANDROID__))
#if (defined(ANDROID) || defined(__ANDROID__)) && !defined(_LIBCPP_VERSION)
// Boost doesn't recognize libstdcpp on top of clang correctly
#include "boost/config.hpp"
#include "boost/config/stdlib/libstdcpp3.hpp"
Expand Down