Add support for building with the Android NDK.

Previously I've been using the Linaro toolchains and just building
static binaries. However, the Linaro toolchains have a broken
pthread_rwlock_wrlock—it does nothing and then unlocking corrupts the
lock.

Building with the Android NDK avoids this.

These build instructions depend on
https://github.com/taka-no-me/android-cmake which people will need to
clone into util/ if they want to use the Android NDK.

Change-Id: Ic64919f9399af2a57e8df4fb4b3400865ddb2427
Reviewed-on: https://boringssl-review.googlesource.com/4600
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
Adam Langley 2015-04-28 17:46:58 -07:00
parent f868409124
commit 843ab66e17
7 changed files with 45 additions and 22 deletions

View File

@ -43,17 +43,37 @@ automatically.
Note that the default build flags in the top-level CMakeLists.txt are for Note that the default build flags in the top-level CMakeLists.txt are for
debugging - optimisation isn't enabled. debugging - optimisation isn't enabled.
If you want to cross-compile then there are example toolchain files for 32-bit If you want to cross-compile then there is an example toolchain file for
Intel and ARM in util/. Wipe out the build directory, recreate it and run cmake 32-bit Intel in util/. Wipe out the build directory, recreate it and run cmake
like this: like this:
cmake -DCMAKE_TOOLCHAIN_FILE=../util/arm-toolchain.cmake -GNinja .. cmake -DCMAKE_TOOLCHAIN_FILE=../util/32-bit-toolchain.cmake -GNinja ..
If you want to build as a shared library, pass -DBUILD_SHARED_LIBS=1. On If you want to build as a shared library, pass -DBUILD_SHARED_LIBS=1. On
Windows, where functions need to be tagged with "dllimport" when coming from a Windows, where functions need to be tagged with "dllimport" when coming from a
shared library, define BORINGSSL_SHARED_LIBRARY in any code which #includes the shared library, define BORINGSSL_SHARED_LIBRARY in any code which #includes the
BoringSSL headers. BoringSSL headers.
Building for Android:
It's possible to build BoringSSL with the Android NDK using CMake. This has
been tested with version 10d of the NDK.
Unpack the Android NDK somewhere and export ANDROID_NDK to point to the
directory. Clone https://github.com/taka-no-me/android-cmake into util/.
Then make a build directory as above and run CMake *twice* like this:
cmake -DANDROID_NATIVE_API_LEVEL=android-9 \
-DANDROID_ABI=armeabi-v7a \
-DCMAKE_TOOLCHAIN_FILE=../util/android-cmake/android.toolchain.cmake \
-GNinja ..
Once you've run that twice, ninja should produce Android-compatible binaries.
You can replace "armeabi-v7a" in the above with "arm64-v8a" to build aarch64
binaries.
Known Limitations on Windows: Known Limitations on Windows:
* Versions of cmake since 3.0.2 have a bug in its Ninja generator that causes * Versions of cmake since 3.0.2 have a bug in its Ninja generator that causes

View File

@ -2,9 +2,16 @@ cmake_minimum_required (VERSION 2.8.10)
project (BoringSSL) project (BoringSSL)
find_package(Perl REQUIRED) if(ANDROID)
# Android-NDK CMake files reconfigure the path and so Go and Perl won't be
# found. However, ninja will still find them in $PATH if we just name them.
set(PERL_EXECUTABLE "perl")
set(GO_EXECUTABLE "go")
else()
find_package(Perl REQUIRED)
find_program(GO_EXECUTABLE go)
endif()
find_program(GO_EXECUTABLE go)
if (NOT GO_EXECUTABLE) if (NOT GO_EXECUTABLE)
message(FATAL_ERROR "Could not find Go") message(FATAL_ERROR "Could not find Go")
endif() endif()
@ -97,12 +104,21 @@ elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
set(ARCH "x86") set(ARCH "x86")
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm") elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm")
set(ARCH "arm") set(ARCH "arm")
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "armv7-a")
set(ARCH "arm")
elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
set(ARCH "aarch64") set(ARCH "aarch64")
else() else()
message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR}) message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
endif() endif()
if (ANDROID AND ${ARCH} STREQUAL "arm")
# The Android-NDK CMake files somehow fail to set the -march flag for
# assembly files. Without this flag, the compiler believes that it's
# building for ARMv5.
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=armv7-a")
endif()
if (${ARCH} STREQUAL "x86" AND APPLE) if (${ARCH} STREQUAL "x86" AND APPLE)
# With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X, # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
# but clang defaults to 64-bit builds on OS X unless otherwise told. # but clang defaults to 64-bit builds on OS X unless otherwise told.

View File

@ -191,7 +191,7 @@ add_library(
$<TARGET_OBJECTS:pkcs8> $<TARGET_OBJECTS:pkcs8>
) )
if(NOT MSVC) if(NOT MSVC AND NOT ANDROID)
target_link_libraries(crypto pthread) target_link_libraries(crypto pthread)
endif() endif()

View File

@ -30,9 +30,6 @@
unsigned long getauxval(unsigned long type) __attribute__((weak)); unsigned long getauxval(unsigned long type) __attribute__((weak));
static const unsigned long AT_HWCAP = 16;
static const unsigned long AT_HWCAP2 = 26;
char CRYPTO_is_NEON_capable(void) { char CRYPTO_is_NEON_capable(void) {
return (OPENSSL_armcap_P & ARMV7_NEON) != 0; return (OPENSSL_armcap_P & ARMV7_NEON) != 0;
} }
@ -136,6 +133,7 @@ void OPENSSL_cpuid_setup(void) {
return; return;
} }
static const unsigned long AT_HWCAP = 16;
unsigned long hwcap = getauxval(AT_HWCAP); unsigned long hwcap = getauxval(AT_HWCAP);
#if defined(OPENSSL_ARM) #if defined(OPENSSL_ARM)
@ -146,6 +144,7 @@ void OPENSSL_cpuid_setup(void) {
/* In 32-bit mode, the ARMv8 feature bits are in a different aux vector /* In 32-bit mode, the ARMv8 feature bits are in a different aux vector
* value. */ * value. */
static const unsigned long AT_HWCAP2 = 26;
hwcap = getauxval(AT_HWCAP2); hwcap = getauxval(AT_HWCAP2);
/* See /usr/include/asm/hwcap.h on an ARM installation for the source of /* See /usr/include/asm/hwcap.h on an ARM installation for the source of

View File

@ -15,7 +15,7 @@ add_executable(
transport_common.cc transport_common.cc
) )
if (APPLE OR WIN32) if (APPLE OR WIN32 OR ANDROID)
target_link_libraries(bssl ssl crypto) target_link_libraries(bssl ssl crypto)
else() else()
target_link_libraries(bssl ssl crypto -lrt) target_link_libraries(bssl ssl crypto -lrt)

View File

@ -1,6 +0,0 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR "aarch64")
set(CMAKE_CXX_COMPILER "/opt/gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++")
set(CMAKE_C_COMPILER "/opt/gcc-linaro-4.9-2014.11-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc")
set(CMAKE_EXE_LINKER_FLAGS "-static")

View File

@ -1,6 +0,0 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR "arm")
set(CMAKE_CXX_COMPILER "/opt/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++")
set(CMAKE_C_COMPILER "/opt/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc")
set(CMAKE_EXE_LINKER_FLAGS "-static")