FindSdlAndroidBuildTools.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #[=======================================================================[
  2. FindSdlAndroidBuildTools
  3. ----------------------
  4. Locate the Android build tools directory.
  5. Imported targets
  6. ^^^^^^^^^^^^^^^^
  7. This find module defines the following :prop_tgt:`IMPORTED` target(s):
  8. <none>
  9. Result variables
  10. ^^^^^^^^^^^^^^^^
  11. This module will set the following variables in your project:
  12. `` SdlAndroidBuildTools_FOUND
  13. if false, no Android build tools have been found
  14. `` SDL_ANDROID_BUILD_TOOLS_ROOT
  15. path of the Android build tools root directory if found
  16. `` SDL_ANDROID_BUILD_TOOLS_VERSION
  17. the human-readable string containing the android build tools version if found
  18. Cache variables
  19. ^^^^^^^^^^^^^^^
  20. These variables may optionally be set to help this module find the correct files:
  21. ``SDL_ANDROID_BUILD_TOOLS_ROOT``
  22. path of the Android build tools root directory
  23. Variables for locating Android platform
  24. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  25. This module responds to the flags:
  26. ``SDL_ANDROID_HOME
  27. First, this module will look for platforms in this CMake variable.
  28. ``ANDROID_HOME
  29. If no platform was found in `SDL_ANDROID_HOME`, then try `ANDROID_HOME`.
  30. ``$ENV{ANDROID_HOME}
  31. If no platform was found in neither `SDL_ANDROID_HOME` or `ANDROID_HOME`, then try `ANDROID_HOME}`
  32. #]=======================================================================]
  33. cmake_minimum_required(VERSION 3.7)
  34. if(NOT PROJECT_NAME MATCHES "^SDL.*")
  35. message(WARNING "This module is internal to SDL and is currently not supported.")
  36. endif()
  37. function(_sdl_is_valid_android_build_tools_root RESULT VERSION BUILD_TOOLS_ROOT)
  38. set(result TRUE)
  39. set(version -1)
  40. string(REGEX MATCH "/([0-9.]+)$" root_match "${BUILD_TOOLS_ROOT}")
  41. if(root_match
  42. AND EXISTS "${BUILD_TOOLS_ROOT}/aapt2"
  43. AND EXISTS "${BUILD_TOOLS_ROOT}/apksigner"
  44. AND EXISTS "${BUILD_TOOLS_ROOT}/d8"
  45. AND EXISTS "${BUILD_TOOLS_ROOT}/zipalign")
  46. set(result "${BUILD_TOOLS_ROOT}")
  47. set(version "${CMAKE_MATCH_1}")
  48. endif()
  49. set(${RESULT} ${result} PARENT_SCOPE)
  50. set(${VERSION} ${version} PARENT_SCOPE)
  51. endfunction()
  52. function(_find_sdl_android_build_tools_root ROOT)
  53. cmake_parse_arguments(fsabtr "" "" "" ${ARGN})
  54. set(homes ${SDL_ANDROID_HOME} ${ANDROID_HOME} $ENV{ANDROID_HOME})
  55. set(root ${ROOT}-NOTFOUND)
  56. foreach(home IN LISTS homes)
  57. if(NOT IS_DIRECTORY "${home}")
  58. continue()
  59. endif()
  60. file(GLOB build_tools_roots LIST_DIRECTORIES true "${home}/build-tools/*")
  61. set(max_build_tools_version -1)
  62. set(max_build_tools_root "")
  63. foreach(build_tools_root IN LISTS build_tools_roots)
  64. _sdl_is_valid_android_build_tools_root(is_valid build_tools_version "${build_tools_root}")
  65. if(is_valid AND build_tools_version GREATER max_build_tools_version)
  66. set(max_build_tools_version "${build_tools_version}")
  67. set(max_build_tools_root "${build_tools_root}")
  68. endif()
  69. endforeach()
  70. if(max_build_tools_version GREATER -1)
  71. set(root ${max_build_tools_root})
  72. break()
  73. endif()
  74. endforeach()
  75. set(${ROOT} ${root} PARENT_SCOPE)
  76. endfunction()
  77. if(NOT DEFINED SDL_ANDROID_BUILD_TOOLS_ROOT)
  78. _find_sdl_android_build_tools_root(SDL_ANDROID_BUILD_TOOLS_ROOT)
  79. set(SDL_ANDROID_BUILD_TOOLS_ROOT "${SDL_ANDROID_BUILD_TOOLS_ROOT}" CACHE PATH "Path of Android build tools")
  80. endif()
  81. include(FindPackageHandleStandardArgs)
  82. find_package_handle_standard_args(SdlAndroidBuildTools
  83. VERSION_VAR SDL_ANDROID_BUILD_TOOLS_VERSION
  84. REQUIRED_VARS SDL_ANDROID_BUILD_TOOLS_ROOT
  85. )