Skip to content

Commit

Permalink
Added build support for Android platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-schick committed Sep 10, 2020
1 parent 7e43c5d commit 53f037e
Show file tree
Hide file tree
Showing 17 changed files with 543 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

# build directory
build*/
# extern directory
extern/*

# CMake
CMakeCache.txt
Expand Down
104 changes: 52 additions & 52 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
cmake_minimum_required(VERSION 3.12)
project(OTExtension LANGUAGES CXX)

if (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
message(FATAL_ERROR "ENCRYPTO_utils require at least g++-8")
cmake_minimum_required(VERSION 3.13)
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules")
include(OTExtensionCacheVariables)
if(ANDROID)
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 17)
endif()

option(OTEXTENSION_BUILD_EXE "Build executables" OFF)

find_package(ENCRYPTO_utils QUIET)
if(ENCRYPTO_utils_FOUND)
message(STATUS "Found ENCRYPTO_utils")
elseif(NOT ENCRYPTO_utils_FOUND AND NOT TARGET ENCRYPTO_utils::encrypto_utils)
message("ENCRYPTO_utils was not found: add ENCRYPTO_utils subdirectory")
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/extern/ENCRYPTO_utils/CMakeLists.txt")
find_package(Git REQUIRED)
message("initialize Git submodule: extern/ENCRYPTO_utils")
execute_process(COMMAND git submodule update --init extern/ENCRYPTO_utils
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")
endif()
add_subdirectory(extern/ENCRYPTO_utils)
project(OTExtension LANGUAGES C CXX)

if(ANDROID)
if(ANDROID_ARM_NEON)
set(OTExtension_INSTALL_PREFIX "platforms/${ANDROID_PLATFORM}/arch-${ANDROID_SYSROOT_ABI}/NEON")
else(ANDROID_ARM_NEON)
set(OTExtension_INSTALL_PREFIX "platforms/${ANDROID_PLATFORM}/arch-${ANDROID_SYSROOT_ABI}")
endif(ANDROID_ARM_NEON)
set(OTExtension_INSTALL_INCLUDE "${OTExtension_INSTALL_PREFIX}/include")
set(OTExtension_INSTALL_LIB "${OTExtension_INSTALL_PREFIX}/lib")
set(OTExtension_INSTALL_ARCHIVE "${OTExtension_INSTALL_PREFIX}/lib")
set(OTExtension_INSTALL_CONFIG "")
else(ANDROID)
set(OTExtension_INSTALL_PREFIX "")
set(OTExtension_INSTALL_INCLUDE "include")
set(OTExtension_INSTALL_LIB "lib")
set(OTExtension_INSTALL_ARCHIVE "lib")
set(OTExtension_INSTALL_CONFIG "")
endif(ANDROID)

if(ANDROID AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${ANDROID_NDK}/usr/local"
CACHE PATH "Default install directory for android builds." FORCE)
endif()

include(AddRelic)
include(AddENCRYPTO_utils)
find_package(Threads REQUIRED)


add_library(otextension
add_library(otextension ${OTExtension_LIBRARY_TYPE}
ot/alsz-ot-ext-rec.cpp
ot/alsz-ot-ext-snd.cpp
# ot/asharov-lindell.cpp
ot/iknp-ot-ext-rec.cpp
ot/iknp-ot-ext-snd.cpp
ot/kk-ot-ext-rec.cpp
ot/kk-ot-ext-snd.cpp
ot/naor-pinkas.cpp
# ot/naor-pinkas_noro.cpp
ot/nnob-ot-ext-rec.cpp
ot/nnob-ot-ext-snd.cpp
ot/ot-ext.cpp
Expand All @@ -41,55 +52,44 @@ add_library(otextension
ot/pvwddh.cpp
ot/simpleot.cpp
)
set_target_properties(otextension PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)

add_library(OTExtension::otextension ALIAS otextension)

target_compile_features(otextension PUBLIC cxx_std_17)
target_compile_options(otextension PRIVATE "-Wall" "-Wextra")
#target_compile_options(otextension PUBLIC "-fno-omit-frame-pointer" "-fsanitize=address")
#target_link_options(otextension PUBLIC "-fno-omit-frame-pointer" "-fsanitize=address")

target_include_directories(otextension
PUBLIC
$<INSTALL_INTERFACE:include>
$<INSTALL_INTERFACE:${OTExtension_INSTALL_INCLUDE}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)


target_link_libraries(otextension
PUBLIC ENCRYPTO_utils::encrypto_utils
PUBLIC Relic::relic
PUBLIC Threads::Threads
)


include(InstallConfig)
install_config(OTExtension_INSTALL_LOCATION "OTExtension" OTExtension::otextension)

install(TARGETS otextension
EXPORT "${PROJECT_NAME}Targets"
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
INCLUDES DESTINATION lib
)
ARCHIVE DESTINATION "${OTExtension_INSTALL_ARCHIVE}"
LIBRARY DESTINATION "${OTExtension_INSTALL_LIB}"
INCLUDES DESTINATION "${OTExtension_INSTALL_INCLUDE}")
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/ot"
DESTINATION include
FILES_MATCHING PATTERN "*.h"
)
export(TARGETS otextension NAMESPACE "${PROJECT_NAME}::" FILE "${PROJECT_NAME}Targets.cmake")
DESTINATION "${OTExtension_INSTALL_INCLUDE}"
FILES_MATCHING REGEX ".*\\.h(pp|h)?$" REGEX ".*cmake.*" EXCLUDE)
export(TARGETS otextension ${OTExtension_DEPENDENCIES_TO_EXPORT}
NAMESPACE "${PROJECT_NAME}::"
FILE "${PROJECT_NAME}Targets.cmake")
install(EXPORT "${PROJECT_NAME}Targets"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "lib/cmake/${PROJECT_NAME}"
)


include(CMakePackageConfigHelpers)

configure_package_config_file("${CMAKE_CURRENT_LIST_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION "lib/cmake/${PROJECT_NAME}"
)

install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION "lib/cmake/${PROJECT_NAME}"
)
DESTINATION "${OTExtension_INSTALL_LOCATION}")

if(OTEXTENSION_BUILD_EXE)
if(OTExtension_BUILD_EXE)
add_subdirectory(mains)
endif(OTEXTENSION_BUILD_EXE)
endif(OTExtension_BUILD_EXE)
5 changes: 5 additions & 0 deletions cmake/BoostConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
get_filename_component(Boost_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)

if(NOT TARGET Boost::boost)
include("${Boost_CMAKE_DIR}/BoostTargets.cmake")
endif()
14 changes: 14 additions & 0 deletions cmake/ForwardConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include(CMakeFindDependencyMacro)
set(DEPENDENCY_NAME "@DEPENDENCY_NAME@")
if(ANDROID AND ANDROID_ARM_NEON)
set(PREFIX "${DEPENDENCY_NAME}-${ANDROID_PLATFORM}-${ANDROID_SYSROOT_ABI}-NEON")
elseif(ANDROID AND NOT ANDROID_ARM_NEON)
set(PREFIX "${DEPENDENCY_NAME}-${ANDROID_PLATFORM}-${ANDROID_SYSROOT_ABI}")
else()
set(PREFIX "${DEPENDENCY_NAME}")
endif()
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${PREFIX}/${PREFIX}Config.cmake")
include("${CMAKE_CURRENT_LIST_DIR}/${PREFIX}/${PREFIX}Config.cmake")
else()
set(${DEPENDENCY_NAME}_FOUND FALSE)
endif()
19 changes: 19 additions & 0 deletions cmake/ImportIntoAndroidStudio.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

set(TARGETS "@DEPENDENCY_TARGETS@")
function(import_into_android_studio IMPORT_LOCATION)
if(ANDROID AND EXISTS "${IMPORT_LOCATION}")
foreach(target ${TARGETS})
get_target_property(library_type ${target} TYPE)
if("${library_type}" STREQUAL "SHARED_LIBRARY")
get_target_property(lib_location ${target} LOCATION)
file(COPY "${lib_location}" DESTINATION "${IMPORT_LOCATION}")
endif()
endforeach()
endif()
endfunction()

if(NOT IMPORT_LOCATION)
import_into_android_studio("${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}")
else()
import_into_android_studio("${IMPORT_LOCATION}")
endif()
22 changes: 22 additions & 0 deletions cmake/Modules/AddENCRYPTO_utils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.13)

if(NOT TARGET ENCRYPTO_utils::encrypto_utils)
if(NOT ENCRYPTO_utils_LIBRARY_TYPE)
set(ENCRYPTO_utils_LIBRARY_TYPE ${OTExtension_LIBRARY_TYPE})
endif()
file(GLOB ENCRYPTO_utils_FILE_LIST "${PROJECT_SOURCE_DIR}/extern/ENCRYPTO_utils/*")
list(LENGTH ENCRYPTO_utils_FILE_LIST ENCRYPTO_utils_NUM_FILES)
#if ENCRYPTO_utils directory is empty
if(ENCRYPTO_utils_NUM_FILES EQUAL 0)
message(STATUS "ENCRYPTO_utils was not found. Fetching ENCRYPTO_utils...")
include(FetchENCRYPTO_utils)
else()
message(STATUS "ENCRYPTO_utils was found in: ${PROJECT_SOURCE_DIR}/extern/ENCRYPTO_utils")
set(ENCRYPTO_utils_SOURCE "${PROJECT_SOURCE_DIR}/extern/ENCRYPTO_utils"
CACHE PATH
"Path to ENCRYPTO_utils source."
FORCE
)
include(FetchENCRYPTO_utils)
endif()
endif()
24 changes: 24 additions & 0 deletions cmake/Modules/AddRelic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.13)

find_package(Relic QUIET)
if(Relic_FOUND OR TARGET Relic::relic)
message(STATUS "Found Relic.")
elseif(NOT Relic_FOUND AND NOT TARGET Relic::relic)
if("${Relic_SOURCE}" STREQUAL "")
file(GLOB Relic_FILE_LIST "${PROJECT_SOURCE_DIR}/extern/Relic/*")
list(LENGTH Relic_FILE_LIST Relic_NUM_FILES)
#if Relic directory is empty
if(Relic_NUM_FILES EQUAL 0)
message(STATUS "Relic was not found. Fetching Relic...")
else()
message(STATUS "Relic was found in: ${PROJECT_SOURCE_DIR}/extern/Relic")
set(Relic_SOURCE "${PROJECT_SOURCE_DIR}/extern/Relic")
endif()
endif()
include(FetchRelic)
if(ANDROID AND Relic_LIBRARY_TYPE STREQUAL "SHARED")
set(CMAKE_SHARED_LINKER_FLAGS ${TMP})
endif()
include(ExternalBuildHelper)
install_imported_library(Relic::relic "Relic")
endif()
45 changes: 45 additions & 0 deletions cmake/Modules/AndroidCacheVariables.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
set(ANDROID_NDK CACHE PATH "Path to Android NDK.")
set(ANDROID_NATIVE_API_LEVEL CACHE STRING "Android API level to compile for. Acceptable values are: [0-9]+ or android-[0-9]+")
set(ANDROID_PLATFORM CACHE STRING "Alternative way to set Android API level. Acceptable values are: latest or android-[0-9]+")
set(ANDROID_TOOLCHAIN_FILE CACHE PATH "Android toolchain file.")
set(ANDROID_ABI CACHE STRING "Target CPU of android device, like e.g. \"armeabi-v7a\".")
set_property(CACHE ANDROID_ABI PROPERTY STRINGS
"armeabi-v7a"
"armeabi-v7a with NEON"
"arm64-v8a"
"x86"
"x86_64"
)

#Check if user wants to build for Android
if(NOT "${CMAKE_ANDROID_NDK}" STREQUAL "")
set(ANDROID_NDK "${CMAKE_ANDROID_NDK}")
endif()
if(NOT "${ANDROID_NDK}" STREQUAL "")
set(ANDROID ON)
elseif(NOT "${ANDROID_TOOLCHAIN_FILE}" STREQUAL "" AND EXISTS "${ANDROID_TOOLCHAIN_FILE}")
set(ANDROID ON)
elseif(NOT "${CMAKE_TOOLCHAIN_FILE}" STREQUAL "" AND EXISTS "${CMAKE_TOOLCHAIN_FILE}")
set(ANDROID ON)
endif()

#Set CMAKE_TOOLCHAIN_FILE and CMAKE_INSTALL_PREFIX for Android builds
if(ANDROID)
#CMAKE_TOOLCHAIN_FILE was not set, but ANDROID_TOOLCHAIN_FILE was set
if("${CMAKE_TOOLCHAIN_FILE}" STREQUAL "" AND NOT "${ANDROID_TOOLCHAIN_FILE}" STREQUAL "")
set(CMAKE_TOOLCHAIN_FILE "${ANDROID_TOOLCHAIN_FILE}")
#Neither toolchain file was set, use toolchain in NDK
elseif("${CMAKE_TOOLCHAIN_FILE}" STREQUAL "" AND "${ANDROID_TOOLCHAIN_FILE}" STREQUAL "")
set(CMAKE_TOOLCHAIN_FILE "${ANDROID_NDK}/build/cmake/android.toolchain.cmake")
else()
set(ANDROID_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_FILE}")
endif()
if(NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
message(FATAL_ERROR
"Could not find file: ${CMAKE_TOOLCHAIN_FILE}. Your NDK might be outdated."
)
endif()
if(NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "${ANDROID_NDK}" CACHE PATH "Default install directory for android builds." FORCE)
endif()
endif(ANDROID)
4 changes: 4 additions & 0 deletions cmake/Modules/FetchENCRYPTO_utils.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.13)

include(FetchHelper)
fetch_helper(ENCRYPTO_utils)
42 changes: 42 additions & 0 deletions cmake/Modules/FetchHelper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.13)
macro(fetch_helper content_name)
string(TOLOWER "${content_name}" LOWER_CASE_${content_name})
if(NOT "${DEPENDENCY_DIR}" STREQUAL "")
set(${content_name}_DOWNLOAD_DIR_COMMAND DOWNLOAD_DIR ${DEPENDENCY_DIR})
else()
set(${content_name}_DOWNLOAD_DIR_COMMAND "")
endif()
if(NOT "${${content_name}_SOURCE}" STREQUAL "")
set(${content_name}_DOWNLOAD_COMMAND1 URL ${${content_name}_SOURCE})
set(${content_name}_DOWNLOAD_COMMAND2 "")
else()
set(${content_name}_DOWNLOAD_COMMAND1 GIT_REPOSITORY ${${content_name}_REPOSITORY})
set(${content_name}_DOWNLOAD_COMMAND2 GIT_TAG ${${content_name}_TAG})
endif()
include(FetchContent)
FetchContent_Declare(${content_name}
${${content_name}_DOWNLOAD_COMMAND1}
${${content_name}_DOWNLOAD_COMMAND2}
${${content_name}_DOWNLOAD_DIR_COMMAND}
)
FetchContent_GetProperties(${content_name})
if(NOT ${LOWER_CASE_${content_name}}_POPULATED)
FetchContent_Populate(${content_name})
if(NOT "${ARGV1}" STREQUAL "")
message(STATUS "Applying patches to ${content_name}...")
include("Patch${content_name}")
endif()
if(NOT "${ARGV2}" STREQUAL "")
add_subdirectory(
${${LOWER_CASE_${content_name}}_SOURCE_DIR}
${${LOWER_CASE_${content_name}}_BINARY_DIR}
EXCLUDE_FROM_ALL
)
else()
add_subdirectory(
${${LOWER_CASE_${content_name}}_SOURCE_DIR}
${${LOWER_CASE_${content_name}}_BINARY_DIR}
)
endif()
endif()
endmacro()
47 changes: 47 additions & 0 deletions cmake/Modules/FetchRelic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
cmake_minimum_required(VERSION 3.13)

set(Relic_SOURCE
CACHE PATH
"Path to Relic source."
)
set(Relic_URL
https://github.com/relic-toolkit/relic/archive/a13dcefef1b81a51f2661910200aa76ab3599273.zip
CACHE STRING
"URL of Relic project."
)
set(Relic_URL_HASH
SHA256=fb327f7350c563433797b5c9ac4e8d08a6989afec5f8f58dbe5d44d621240d65
CACHE STRING
"Hash of Relic archive."
)

include(FetchHelper)
#EXCLUDE_FROM_ALL to prevent some rogue config files to be installed
fetch_helper(Relic patch EXCLUDE_FROM_ALL)

set(Relic_INCLUDE_DIRS "${relic_SOURCE_DIR}/include" "${relic_SOURCE_DIR}/include/low" "${relic_BINARY_DIR}/include")
set(Relic_LIB_DIR "${CMAKE_BINARY_DIR}/lib")
if(Relic_LIBRARY_TYPE STREQUAL "STATIC")
if(LABEL)
set(Relic_TARGET "relic_s_${LABEL}")
else()
set(Relic_TARGET "relic_s")
endif()
elseif(${Relic_LIBRARY_TYPE} STREQUAL "SHARED")
if(LABEL)
set(Relic_TARGET "relic_${LABEL}")
else()
set(Relic_TARGET "relic")
endif()
endif()
set(Relic_LIB_NAME "${Relic_TARGET}")
#Allow relic target to be installed (but keep install deactivated for config files etc.)
set_target_properties(${Relic_TARGET} PROPERTIES EXCLUDE_FROM_ALL 0)
add_library(Relic::relic ${Relic_LIBRARY_TYPE} IMPORTED GLOBAL)
add_dependencies(Relic::relic ${Relic_TARGET})
target_include_directories(Relic::relic INTERFACE "${Relic_INCLUDE_DIRS}")
set_target_properties(Relic::relic PROPERTIES IMPORTED_LOCATION "${Relic_LIB_DIR}/${CMAKE_${Relic_LIBRARY_TYPE}_LIBRARY_PREFIX}${Relic_TARGET}${CMAKE_${Relic_LIBRARY_TYPE}_LIBRARY_SUFFIX}")
if(ANDROID)
find_library(ANDROID_LOG_LIB log)
target_link_libraries(Relic::relic INTERFACE "${ANDROID_LOG_LIB}")
endif()
Loading

0 comments on commit 53f037e

Please sign in to comment.