Files
o3de/cmake/Platform/Android/Toolchain_android.cmake
T
lumberyard-employee-dm f09055af42 Fixed CMake use with Android NDK 23 (#6460)
* Fixed CMake use with Android NDK 23

This was done by only setting the `ANDROID_ARM_MODE` if the ANDROID_ABI
starts with `armeabi`.

The Android arm mode setting can't be used using non-`armeabi` ABIs.
In O3DE we default to `arm64-v8a` `ANDROID_ABI`
So `ANDROID_ARM_MODE` must not be set.

The CMake [Android-Determine.cmake](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Platform/Android-Determine.cmake#L573-585) which is used to detect platform-wide information when the CMAKE_SYSTEM_NAME is set to android, enforces that if the `CMAKE_ANDROID_ARCH_ABI` doesn't start with `armeabi`, then it will fatal error if the CMAKE_ANDROID_ARM_MODE option is set.

In Android NDK 21 the `ANDROID_ARM_MODE` variable is used to set the CMAKE_ANDROID_ARM_MODE variable if the [ANDROID_ABI](https://android.googlesource.com/platform/ndk/+/refs/tags/ndk-r21e/build/cmake/android.toolchain.cmake#700) starts with `armeabi`.

This meant when using Android NDK 21, the CMake Android-Determine.cmake module would succeed, due
to the CMAKE_ANDROID_ARM_MODE not being set.

In Android NDK 23 the `ANDROID_ARM_MODE` now will set the
`CMAKE_ANDROID_ARM_MODE` variable to `TRUE` if it isn't defined.

Added an `--extra-cmake-configure-args` option to the `generate_android_project.py` script which can be used to append user specified CMake arguments to the cmake configure step (`cmake -B <build-dir> -S <source-dir> -DCMAKE_TOOLCHAIN_FILE=<android-toolchain-file> <extra-args>`)

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Updated the O3DE Android toolchain wrapper to fatal error if the 64-bit
arm ABI isn't used.

Also removed the unneccessary setting of the ANDROID_ARM_MODE and
ANDROID_ARM_NEON option option now that the armeabi cannot be specified
as an ABI.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2022-01-10 19:11:00 -06:00

86 lines
3.5 KiB
CMake

#
# Copyright (c) Contributors to the Open 3D Engine Project.
# For complete copyright and license terms please see the LICENSE at the root of this distribution.
#
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
#
if(LY_TOOLCHAIN_NDK_API_LEVEL)
return()
endif()
# Verify that the NDK environment is set and points to the support NDK
if(NOT ${LY_NDK_DIR})
if($ENV{LY_NDK_DIR})
set(LY_NDK_DIR $ENV{LY_NDK_DIR})
endif()
endif()
file(TO_CMAKE_PATH "${LY_NDK_DIR}" LY_NDK_DIR)
if(NOT LY_NDK_DIR)
message(FATAL_ERROR "Environment and cache var for NDK is empty. Could not find the NDK installation folder")
endif()
set(LY_ANDROID_NDK_TOOLCHAIN ${LY_NDK_DIR}/build/cmake/android.toolchain.cmake)
if(NOT LY_NDK_DIR)
message(FATAL_ERROR "Invalid NDK Environment. Unable to locate android toolchain file: " ${LY_NDK_DIR})
endif()
# Set some default variables that are used by the NDK's toolchain file before processing
if(NOT ANDROID_ABI)
set(ANDROID_ABI arm64-v8a)
endif()
# Only the 64-bit ANDROID ABIs arm supported
if(NOT ANDROID_ABI MATCHES "^arm64-")
message(FATAL_ERROR "Only the 64-bit ANDROID_ABI's are supported. arm64-v8a can be used if not set")
endif()
if(NOT ANDROID_NATIVE_API_LEVEL)
set(ANDROID_NATIVE_API_LEVEL 21)
endif()
# Make a backup of the CMAKE_FIND_ROOT_PATH since it will be altered by the NDK toolchain file and needs to be restored after the input
set(BACKUP_CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH})
include(${LY_ANDROID_NDK_TOOLCHAIN})
set(CMAKE_FIND_ROOT_PATH ${BACKUP_CMAKE_FIND_ROOT_PATH})
# Force the ANDROID_LINKER_FLAGS that are set in the NDK's toolchain file into the LINKER_FLAGS for the build and reset
# the standard libraries
set(LINKER_FLAGS ${ANDROID_LINKER_FLAGS})
set(CMAKE_CXX_STANDARD_LIBRARIES "")
# We need to pass down the Android API Level, and the Package Revision's Major and Minor number as preprocessor values.
# We will extract them from 'ANDROID_NDK_SOURCE_PROPERTIES' which will read from the NDK's properties file.
# (note: we cannot use 'ANDROID_NDK_REVISION' because the toolchain combines the Major and Minor revisions
string(REGEX MATCHALL "Pkg.Revision = (([0-9]+).([0-9]+).[0-9]+)" LY_NDK_PKG_REVISION_LINE ${ANDROID_NDK_SOURCE_PROPERTIES})
set(LY_TOOLCHAIN_NDK_PKG_MAJOR ${CMAKE_MATCH_2})
set(LY_TOOLCHAIN_NDK_PKG_MINOR ${CMAKE_MATCH_3})
set(LY_TOOLCHAIN_NDK_API_LEVEL ${ANDROID_PLATFORM_LEVEL})
set(MIN_NDK_VERSION 21)
if(${LY_TOOLCHAIN_NDK_PKG_MAJOR} VERSION_LESS ${MIN_NDK_VERSION})
message(FATAL_ERROR "Unsupported NDK Version ${LY_TOOLCHAIN_NDK_PKG_MAJOR}.${LY_TOOLCHAIN_NDK_PKG_MINOR}.${LY_TOOLCHAIN_NDK_API_LEVEL}. Must be version ${MIN_NDK_VERSION} or above")
else()
message(STATUS "Detected NDK Version ${LY_TOOLCHAIN_NDK_PKG_MAJOR}.${LY_TOOLCHAIN_NDK_PKG_MINOR}.${LY_TOOLCHAIN_NDK_PKG_MINOR}")
endif()
list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES LY_NDK_DIR)
# The Native Activity Glue source file needs to be included in any project that will be loaded
# through the android launcher APK. This source file resides directly in the NDK source folder structure
# based on the configured NDK Path set with ${LY_NDK_DIR}
# Locate and verify the source folder based on the NDK path
set(LY_NDK_NATIVE_APP_GLUE_SRC_DIR "${LY_NDK_DIR}/sources/android/native_app_glue")
file(TO_CMAKE_PATH ${LY_NDK_NATIVE_APP_GLUE_SRC_DIR} LY_NDK_NATIVE_APP_GLUE_SRC_DIR)
if(NOT IS_DIRECTORY "${LY_NDK_NATIVE_APP_GLUE_SRC_DIR}")
message(FATAL_ERROR "Could not find android native app glue directory: ${LY_NDK_NATIVE_APP_GLUE_SRC_DIR}")
endif()