From e1aff790f28219dee3d8e2cf0c2891634ccc9fba Mon Sep 17 00:00:00 2001 From: Thad House Date: Wed, 21 Jul 2021 09:55:30 -0700 Subject: [PATCH 1/4] Add android native library build support --- .azure/azure-pipelines.ci.yml | 8 ++++++++ CMakeLists.txt | 16 ++++++++++++++++ scripts/build.ps1 | 19 ++++++++++++++++++- scripts/get-buildconfig.ps1 | 6 +++++- src/inc/CMakeLists.txt | 2 +- src/platform/platform_posix.c | 7 ++++++- 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/.azure/azure-pipelines.ci.yml b/.azure/azure-pipelines.ci.yml index 7674eb7214..413e11d32a 100644 --- a/.azure/azure-pipelines.ci.yml +++ b/.azure/azure-pipelines.ci.yml @@ -283,6 +283,14 @@ stages: tls: openssl ubuntuVersion: 18.04 extraBuildArgs: -DisableLogs -ToolchainFile cmake/toolchains/arm-pi-gnueabihf.toolchain.cmake + + - template: ./templates/build-config-user.yml + parameters: + image: ubuntu-latest + platform: android + arch: arm64 + tls: openssl + - template: ./templates/build-config-user.yml parameters: image: ubuntu-latest diff --git a/CMakeLists.txt b/CMakeLists.txt index ba7f3cf805..18dced19cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -488,6 +488,22 @@ if(QUIC_TLS STREQUAL "openssl") set(OPENSSL_CONFIG_CMD ${CMAKE_CURRENT_SOURCE_DIR}/submodules/openssl/config) endif() list(APPEND OPENSSL_CONFIG_FLAGS -isysroot ${CMAKE_OSX_SYSROOT}) + elseif(ANDROID) + message(STATUS "${ANDROID_ABI}") + if(ANDROID_ABI STREQUAL "arm64-v8a") + set(OPENSSL_BUILD_TYPE android-arm64) + elseif(ANDROID_ABI STREQUAL "armeabi-v7a") + set(OPENSSL_BUILD_TYPE android-arm) + elseif(ANDROID_ABI STREQUAL "x86") + set(OPENSSL_BUILD_TYPE android-x86) + elseif(ANDROID_ABI STREQUAL "x86_64") + set(OPENSSL_BUILD_TYPE android-x86_64) + else() + message(FATAL_ERROR "Unknown android abi type") + endif() + set(OPENSSL_CONFIG_CMD ${CMAKE_CURRENT_SOURCE_DIR}/submodules/openssl/Configure + ${OPENSSL_BUILD_TYPE} + -D__ANDROID_API__=29) else() set(OPENSSL_CONFIG_CMD ${CMAKE_CURRENT_SOURCE_DIR}/submodules/openssl/config CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER}) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 9dcf0cbd70..d0928204ae 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -108,7 +108,7 @@ param ( [string]$Arch = "", [Parameter(Mandatory = $false)] - [ValidateSet("uwp", "windows", "linux", "macos")] # For future expansion + [ValidateSet("uwp", "windows", "linux", "macos", "android")] # For future expansion [string]$Platform = "", [Parameter(Mandatory = $false)] @@ -250,6 +250,10 @@ function Log($msg) { Write-Host "[$(Get-Date)] $msg" } +if ($Platform -eq "android") { + $DisableLogs = $true +} + # Executes cmake with the given arguments. function CMake-Execute([String]$Arguments) { Log "cmake $($Arguments)" @@ -346,6 +350,19 @@ function CMake-Generate { if ($UseSystemOpenSSLCrypto) { $Arguments += " -DQUIC_USE_SYSTEM_LIBCRYPTO=on" } + if ($Platform -eq "android") { + switch ($Arch) { + "x86" { $Arguments += " -DANDROID_ABI=x86"} + "x64" { $Arguments += " -DANDROID_ABI=x86_64" } + "arm" { $Arguments += " -DANDROID_ABI=armeabi-v7a" } + "arm64" { $Arguments += " -DANDROID_ABI=arm64-v8a" } + } + $Arguments += " -DANDROID_PLATFORM=android-29" + $NDK = $env:ANDROID_NDK_HOME + $NdkToolchainFile = "$NDK/build/cmake/android.toolchain.cmake" + $Arguments += " -DANDROID_NDK=$NDK" + $Arguments += " -DCMAKE_TOOLCHAIN_FILE=$NdkToolchainFile" + } $Arguments += " -DQUIC_LIBRARY_NAME=$LibraryName" $Arguments += " ../../.." diff --git a/scripts/get-buildconfig.ps1 b/scripts/get-buildconfig.ps1 index 853c9b62de..e295b5a2be 100644 --- a/scripts/get-buildconfig.ps1 +++ b/scripts/get-buildconfig.ps1 @@ -30,7 +30,7 @@ param ( [string]$Arch = "", [Parameter(Mandatory = $false)] - [ValidateSet("uwp", "windows", "linux", "macos", "")] # For future expansion + [ValidateSet("uwp", "windows", "linux", "macos", "android", "")] # For future expansion [string]$Platform = "", [Parameter(Mandatory = $false)] @@ -44,6 +44,10 @@ param ( Set-StrictMode -Version 'Latest' $PSDefaultParameterValues['*:ErrorAction'] = 'Stop' +if ($Platform -eq "android" -and $Arch -eq "") { + $Arch = "arm64" +} + if ("" -eq $Arch) { if ($IsMacOS) { $RunningArch = uname -m diff --git a/src/inc/CMakeLists.txt b/src/inc/CMakeLists.txt index 2376777164..e6eef3eada 100644 --- a/src/inc/CMakeLists.txt +++ b/src/inc/CMakeLists.txt @@ -26,7 +26,7 @@ if(QUIC_ENABLE_LOGGING) endif() endif() -if (NOT MSVC AND NOT APPLE) +if (NOT MSVC AND NOT APPLE AND NOT ANDROID) find_library(ATOMIC NAMES atomic libatomic.so.1) if (ATOMIC) message(STATUS "Found libatomic: ${ATOMIC}") diff --git a/src/platform/platform_posix.c b/src/platform/platform_posix.c index 90895781fd..67f86c02bd 100644 --- a/src/platform/platform_posix.c +++ b/src/platform/platform_posix.c @@ -591,7 +591,7 @@ CxPlatThreadCreate( #endif // !CXPLAT_USE_CUSTOM_THREAD_CONTEXT -#ifndef __GLIBC__ +#if !defined(__GLIBC__) && !defined(__ANDROID__) if (Status == QUIC_STATUS_SUCCESS) { if (Config->Flags & CXPLAT_THREAD_FLAG_SET_AFFINITIZE) { cpu_set_t CpuSet; @@ -619,6 +619,7 @@ CxPlatSetCurrentThreadProcessorAffinity( _In_ uint16_t ProcessorIndex ) { +#ifndef __ANDROID__ cpu_set_t CpuSet; pthread_t Thread = pthread_self(); CPU_ZERO(&CpuSet); @@ -632,6 +633,10 @@ CxPlatSetCurrentThreadProcessorAffinity( } return QUIC_STATUS_SUCCESS; +#else + UNREFERENCED_PARAMETER(ProcessorIndex); + return QUIC_STATUS_SUCCESS; +#endif } #elif defined(CX_PLATFORM_DARWIN) From 6a9ea48b3f44fac6b956bb30dda419ef48391627 Mon Sep 17 00:00:00 2001 From: Thad House Date: Wed, 21 Jul 2021 10:15:07 -0700 Subject: [PATCH 2/4] Get building --- scripts/build.ps1 | 7 ++----- scripts/get-buildconfig.ps1 | 9 +++++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index d0928204ae..824f97a2a1 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -250,10 +250,6 @@ function Log($msg) { Write-Host "[$(Get-Date)] $msg" } -if ($Platform -eq "android") { - $DisableLogs = $true -} - # Executes cmake with the given arguments. function CMake-Execute([String]$Arguments) { Log "cmake $($Arguments)" @@ -351,6 +347,7 @@ function CMake-Generate { $Arguments += " -DQUIC_USE_SYSTEM_LIBCRYPTO=on" } if ($Platform -eq "android") { + $env:PATH = "$env:ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$env:PATH" switch ($Arch) { "x86" { $Arguments += " -DANDROID_ABI=x86"} "x64" { $Arguments += " -DANDROID_ABI=x86_64" } @@ -358,7 +355,7 @@ function CMake-Generate { "arm64" { $Arguments += " -DANDROID_ABI=arm64-v8a" } } $Arguments += " -DANDROID_PLATFORM=android-29" - $NDK = $env:ANDROID_NDK_HOME + $NDK = $env:ANDROID_NDK_HOME $NdkToolchainFile = "$NDK/build/cmake/android.toolchain.cmake" $Arguments += " -DANDROID_NDK=$NDK" $Arguments += " -DCMAKE_TOOLCHAIN_FILE=$NdkToolchainFile" diff --git a/scripts/get-buildconfig.ps1 b/scripts/get-buildconfig.ps1 index e295b5a2be..74999ae2b7 100644 --- a/scripts/get-buildconfig.ps1 +++ b/scripts/get-buildconfig.ps1 @@ -44,8 +44,13 @@ param ( Set-StrictMode -Version 'Latest' $PSDefaultParameterValues['*:ErrorAction'] = 'Stop' -if ($Platform -eq "android" -and $Arch -eq "") { - $Arch = "arm64" +if ($Platform -eq "android") { + if (!$IsLinux) { + Write-Error "Can only build android on linux" + } + if ($Arch -eq "") { + $Arch = "arm64" + } } if ("" -eq $Arch) { From c4569acc6004992318631c687d342a08574c900d Mon Sep 17 00:00:00 2001 From: Thad House Date: Wed, 21 Jul 2021 10:19:25 -0700 Subject: [PATCH 3/4] 1 more build fix, add x64 build --- .azure/azure-pipelines.ci.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.azure/azure-pipelines.ci.yml b/.azure/azure-pipelines.ci.yml index 413e11d32a..36262c6d1d 100644 --- a/.azure/azure-pipelines.ci.yml +++ b/.azure/azure-pipelines.ci.yml @@ -290,6 +290,15 @@ stages: platform: android arch: arm64 tls: openssl + extraBuildArgs: -DisableLogs + + - template: ./templates/build-config-user.yml + parameters: + image: ubuntu-latest + platform: android + arch: x64 + tls: openssl + extraBuildArgs: -DisableLogs - template: ./templates/build-config-user.yml parameters: From bb8956e45cca8f3f807984b0a28f2a01e6b9438c Mon Sep 17 00:00:00 2001 From: Nick Banks Date: Wed, 21 Jul 2021 14:37:02 -0500 Subject: [PATCH 4/4] Fix spacing --- scripts/build.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 824f97a2a1..e19dbd3165 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -355,7 +355,7 @@ function CMake-Generate { "arm64" { $Arguments += " -DANDROID_ABI=arm64-v8a" } } $Arguments += " -DANDROID_PLATFORM=android-29" - $NDK = $env:ANDROID_NDK_HOME + $NDK = $env:ANDROID_NDK_HOME $NdkToolchainFile = "$NDK/build/cmake/android.toolchain.cmake" $Arguments += " -DANDROID_NDK=$NDK" $Arguments += " -DCMAKE_TOOLCHAIN_FILE=$NdkToolchainFile"