FindSdlAndroidPlatform.cmake 3.7 KB

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