Toolchain_android.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. if(LY_TOOLCHAIN_NDK_API_LEVEL)
  9. return()
  10. endif()
  11. # Verify that the NDK environment is set and points to the support NDK
  12. if(NOT LY_NDK_DIR)
  13. if(DEFINED ENV{LY_NDK_DIR})
  14. set(LY_NDK_DIR $ENV{LY_NDK_DIR})
  15. endif()
  16. endif()
  17. file(TO_CMAKE_PATH "${LY_NDK_DIR}" LY_NDK_DIR)
  18. if(NOT LY_NDK_DIR)
  19. message(FATAL_ERROR "Environment and cache var for NDK is empty. Could not find the NDK installation folder")
  20. endif()
  21. set(LY_ANDROID_NDK_TOOLCHAIN ${LY_NDK_DIR}/build/cmake/android.toolchain.cmake)
  22. if(NOT LY_NDK_DIR)
  23. message(FATAL_ERROR "Invalid NDK Environment. Unable to locate android toolchain file: " ${LY_NDK_DIR})
  24. endif()
  25. # Set some default variables that are used by the NDK's toolchain file before processing
  26. if(NOT ANDROID_ABI)
  27. set(ANDROID_ABI arm64-v8a)
  28. endif()
  29. # Only the 64-bit ANDROID ABIs arm supported
  30. if(NOT ANDROID_ABI MATCHES "^arm64-")
  31. message(FATAL_ERROR "Only the 64-bit ANDROID_ABI's are supported. arm64-v8a can be used if not set")
  32. endif()
  33. set(MIN_NATIVE_API_LEVEL 24)
  34. if(NOT ANDROID_NATIVE_API_LEVEL)
  35. set(ANDROID_NATIVE_API_LEVEL ${MIN_NATIVE_API_LEVEL})
  36. endif()
  37. if(${ANDROID_NATIVE_API_LEVEL} VERSION_LESS ${MIN_NATIVE_API_LEVEL})
  38. message(FATAL_ERROR "Unsupported Android native API version ${ANDROID_NATIVE_API_LEVEL}. Must be ${MIN_NATIVE_API_LEVEL} or above")
  39. endif()
  40. set(ANDROID_PLATFORM android-${ANDROID_NATIVE_API_LEVEL})
  41. # 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
  42. set(BACKUP_CMAKE_FIND_ROOT_PATH ${CMAKE_FIND_ROOT_PATH})
  43. include(${LY_ANDROID_NDK_TOOLCHAIN})
  44. set(CMAKE_FIND_ROOT_PATH ${BACKUP_CMAKE_FIND_ROOT_PATH})
  45. # The CMake Android-Initialize.cmake(https://gitlab.kitware.com/cmake/cmake/-/blob/v3.21.2/Modules/Platform/Android-Initialize.cmake#L61)
  46. # script sets CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH to OFF resulting in find_program calls being unable to locate host binaries
  47. # https://gitlab.kitware.com/cmake/cmake/-/issues/22634
  48. # Setting back to true to fix our find_program calls
  49. set(CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH ON)
  50. # Force the ANDROID_LINKER_FLAGS that are set in the NDK's toolchain file into the LINKER_FLAGS for the build and reset
  51. # the standard libraries
  52. set(LINKER_FLAGS ${ANDROID_LINKER_FLAGS})
  53. set(CMAKE_CXX_STANDARD_LIBRARIES "")
  54. # We need to pass down the Android API Level, and the Package Revision's Major and Minor number as preprocessor values.
  55. # We will extract them from 'ANDROID_NDK_SOURCE_PROPERTIES' which will read from the NDK's properties file.
  56. # (note: we cannot use 'ANDROID_NDK_REVISION' because the toolchain combines the Major and Minor revisions
  57. string(REGEX MATCHALL "Pkg.Revision = (([0-9]+).([0-9]+).[0-9]+)" LY_NDK_PKG_REVISION_LINE ${ANDROID_NDK_SOURCE_PROPERTIES})
  58. set(LY_TOOLCHAIN_NDK_PKG_MAJOR ${CMAKE_MATCH_2})
  59. set(LY_TOOLCHAIN_NDK_PKG_MINOR ${CMAKE_MATCH_3})
  60. set(LY_TOOLCHAIN_NDK_API_LEVEL ${ANDROID_PLATFORM_LEVEL})
  61. set(MIN_NDK_VERSION 21)
  62. if(${LY_TOOLCHAIN_NDK_PKG_MAJOR} VERSION_LESS ${MIN_NDK_VERSION})
  63. message(FATAL_ERROR "Unsupported NDK Version ${LY_TOOLCHAIN_NDK_PKG_MAJOR}.${LY_TOOLCHAIN_NDK_PKG_MINOR}. Must be version ${MIN_NDK_VERSION} or above")
  64. else()
  65. message(STATUS "Detected NDK Version ${LY_TOOLCHAIN_NDK_PKG_MAJOR}.${LY_TOOLCHAIN_NDK_PKG_MINOR}")
  66. endif()
  67. list(APPEND CMAKE_TRY_COMPILE_PLATFORM_VARIABLES LY_NDK_DIR)
  68. # The Native Activity Glue source file needs to be included in any project that will be loaded
  69. # through the android launcher APK. This source file resides directly in the NDK source folder structure
  70. # based on the configured NDK Path set with ${LY_NDK_DIR}
  71. # Locate and verify the source folder based on the NDK path
  72. set(LY_NDK_NATIVE_APP_GLUE_SRC_DIR "${LY_NDK_DIR}/sources/android/native_app_glue")
  73. file(TO_CMAKE_PATH ${LY_NDK_NATIVE_APP_GLUE_SRC_DIR} LY_NDK_NATIVE_APP_GLUE_SRC_DIR)
  74. if(NOT IS_DIRECTORY "${LY_NDK_NATIVE_APP_GLUE_SRC_DIR}")
  75. message(FATAL_ERROR "Could not find android native app glue directory: ${LY_NDK_NATIVE_APP_GLUE_SRC_DIR}")
  76. endif()