SdlAndroidScript.cmake 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #[=======================================================================[
  2. This CMake script is meant to be used in CMake script mode (cmake -P).
  3. It wraps commands that communicate with an actual Android device.
  4. Because
  5. #]=======================================================================]
  6. cmake_minimum_required(VERSION 3.16...3.28)
  7. if(NOT CMAKE_SCRIPT_MODE_FILE)
  8. message(FATAL_ERROR "This file can only be used in CMake script mode")
  9. endif()
  10. if(NOT ADB)
  11. set(ADB "adb")
  12. endif()
  13. if(NOT ACTION)
  14. message(FATAL_ERROR "Missing ACTION argument")
  15. endif()
  16. if(ACTION STREQUAL "uninstall")
  17. # The uninstall action attempts to uninstall all packages. All failures are ignored.
  18. foreach(package IN LISTS PACKAGES)
  19. message("Uninstalling ${package} ...")
  20. execute_process(
  21. COMMAND ${ADB} uninstall ${package}
  22. RESULT_VARIABLE res
  23. )
  24. message("... result=${res}")
  25. endforeach()
  26. elseif(ACTION STREQUAL "install")
  27. # The install actions attempts to install APK's to an Android device using adb. Failures are ignored.
  28. set(failed_apks "")
  29. foreach(apk IN LISTS APKS)
  30. message("Installing ${apk} ...")
  31. execute_process(
  32. COMMAND ${ADB} install -d -r --streaming ${apk}
  33. RESULT_VARIABLE res
  34. )
  35. message("... result=${res}")
  36. if(NOT res EQUAL 0)
  37. list(APPEND failed_apks ${apk})
  38. endif()
  39. endforeach()
  40. if(failed_apks)
  41. message(FATAL_ERROR "Failed to install ${failed_apks}")
  42. endif()
  43. elseif(ACTION STREQUAL "build-install-run")
  44. if(NOT EXECUTABLES)
  45. message(FATAL_ERROR "Missing EXECUTABLES (don't know what executables to build/install and start")
  46. endif()
  47. if(NOT BUILD_FOLDER)
  48. message(FATAL_ERROR "Missing BUILD_FOLDER (don't know where to build the APK's")
  49. endif()
  50. set(install_targets "")
  51. foreach(executable IN LISTS EXECUTABLES)
  52. list(APPEND install_targets "install-${executable}")
  53. endforeach()
  54. execute_process(
  55. COMMAND ${CMAKE_COMMAND} --build "${BUILD_FOLDER}" --target ${install_targets}
  56. RESULT_VARIABLE res
  57. )
  58. if(NOT res EQUAL 0)
  59. message(FATAL_ERROR "Failed to install APK(s) for ${EXECUTABLES}")
  60. endif()
  61. list(GET EXECUTABLES 0 start_executable)
  62. execute_process(
  63. COMMAND ${CMAKE_COMMAND} --build "${BUILD_FOLDER}" --target start-${start_executable}
  64. RESULT_VARIABLE res
  65. )
  66. else()
  67. message(FATAL_ERROR "Unknown ACTION=${ACTION}")
  68. endif()