AndroidTestUtilities.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[======================================================================[.rst:
  4. AndroidTestUtilities
  5. ------------------------
  6. Create a test that automatically loads specified data onto an Android device.
  7. Introduction
  8. ^^^^^^^^^^^^
  9. Use this module to push data needed for testing an Android device behavior
  10. onto a connected Android device. The module will accept files and libraries as
  11. well as separate destinations for each. It will create a test that loads the
  12. files into a device object store and link to them from the specified
  13. destination. The files are only uploaded if they are not already in the object
  14. store.
  15. For example:
  16. .. code-block:: cmake
  17. include(AndroidTestUtilities)
  18. android_add_test_data(
  19. example_setup_test
  20. FILES <files>...
  21. LIBS <libs>...
  22. DEVICE_TEST_DIR "/data/local/tests/example"
  23. DEVICE_OBJECT_STORE "/sdcard/.ExternalData/SHA"
  24. )
  25. At build time a test named "example_setup_test" will be created. Run this test
  26. on the command line with :manual:`ctest(1)` to load the data onto the Android
  27. device.
  28. Module Functions
  29. ^^^^^^^^^^^^^^^^
  30. .. command:: android_add_test_data
  31. .. code-block:: cmake
  32. android_add_test_data(<test-name>
  33. [FILES <files>...] [FILES_DEST <device-dir>]
  34. [LIBS <libs>...] [LIBS_DEST <device-dir>]
  35. [DEVICE_OBJECT_STORE <device-dir>]
  36. [DEVICE_TEST_DIR <device-dir>]
  37. [NO_LINK_REGEX <strings>...]
  38. )
  39. The ``android_add_test_data`` function is used to copy files and libraries
  40. needed to run project-specific tests. On the host operating system, this is
  41. done at build time. For on-device testing, the files are loaded onto the
  42. device by the manufactured test at run time.
  43. This function accepts the following named parameters:
  44. ``FILES <files>...``
  45. zero or more files needed for testing
  46. ``LIBS <libs>...``
  47. zero or more libraries needed for testing
  48. ``FILES_DEST <device-dir>``
  49. absolute path where the data files are expected to be
  50. ``LIBS_DEST <device-dir>``
  51. absolute path where the libraries are expected to be
  52. ``DEVICE_OBJECT_STORE <device-dir>``
  53. absolute path to the location where the data is stored on-device
  54. ``DEVICE_TEST_DIR <device-dir>``
  55. absolute path to the root directory of the on-device test location
  56. ``NO_LINK_REGEX <strings>...``
  57. list of regex strings matching the names of files that should be
  58. copied from the object store to the testing directory
  59. #]======================================================================]
  60. include(${CMAKE_CURRENT_LIST_DIR}/ExternalData.cmake)
  61. # The parameters to this function should be set to the list of directories,
  62. # files, and libraries that need to be installed prior to testing.
  63. function(android_add_test_data test_name)
  64. # As the names suggest, oneValueArgs lists the arguments that specify a
  65. # single value, while multiValueArgs can contain one or more values.
  66. set(keywordArgs)
  67. set(oneValueArgs FILES_DEST LIBS_DEST DEVICE_OBJECT_STORE DEVICE_TEST_DIR)
  68. set(multiValueArgs FILES LIBS NO_LINK_REGEX)
  69. # For example, if you called this function with FILES </path/to/file>
  70. # then this path would be stored in the variable AST_FILES.
  71. # The AST prefix stands for the name of this function (android_add_test_data).
  72. cmake_parse_arguments(AST "${keywordArgs}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  73. if(NOT AST_DEVICE_TEST_DIR)
  74. message(FATAL_ERROR "-- You must specify the location of the on device test directory.")
  75. endif()
  76. if(NOT AST_DEVICE_OBJECT_STORE)
  77. message(FATAL_ERROR "-- You must specify the location of the on device object store.")
  78. endif()
  79. if(${AST_DEVICE_TEST_DIR} STREQUAL "/")
  80. message(FATAL_ERROR "-- The device test directory cannot be '/'")
  81. endif()
  82. # Copy all test data files into the binary directory, where tests are run.
  83. # ExternalData will handle fetching DATA{...} references.
  84. string(REPLACE "|" ";" hash_algs "${_ExternalData_REGEX_EXT}")
  85. # Convert ExternalData placeholder file names to DATA{} syntax.
  86. foreach(alg ${hash_algs})
  87. string(REGEX REPLACE "([^ ;]+)\\.${alg}" "DATA{\\1}" AST_FILES "${AST_FILES}")
  88. endforeach()
  89. set(DATA_TARGET_NAME "${test_name}")
  90. string(FIND "${AST_FILES}" "DATA{" data_files_found)
  91. if(${data_files_found} GREATER "-1")
  92. # Use ExternalData if any DATA{} files were found.
  93. ExternalData_Expand_Arguments(
  94. ${DATA_TARGET_NAME}
  95. extern_data_output
  96. ${AST_FILES})
  97. ExternalData_Add_Target(${DATA_TARGET_NAME})
  98. else()
  99. add_custom_target(${DATA_TARGET_NAME} ALL)
  100. set(extern_data_output ${AST_FILES})
  101. endif()
  102. # For regular files on Linux, just copy them directly.
  103. foreach(path ${AST_FILES})
  104. foreach(output ${extern_data_output})
  105. if(${output} STREQUAL ${path})
  106. # Check if a destination was specified. If not, we copy by default
  107. # into this project's binary directory, preserving its relative path.
  108. if(AST_${VAR}_DEST)
  109. set(DEST ${CMAKE_BINARY_DIR}/${parent_dir}/${AST_${VAR}_DEST})
  110. else()
  111. get_filename_component(parent_dir ${path} DIRECTORY)
  112. set(DEST "${CMAKE_BINARY_DIR}/${parent_dir}")
  113. endif()
  114. get_filename_component(extern_data_source ${output} REALPATH)
  115. get_filename_component(extern_data_basename ${output} NAME)
  116. add_custom_command(
  117. TARGET ${DATA_TARGET_NAME} POST_BUILD
  118. DEPENDS ${extern_data_source}
  119. COMMAND ${CMAKE_COMMAND} -E copy_if_different ${extern_data_source} ${DEST}/${extern_data_basename}
  120. )
  121. endif()
  122. endforeach()
  123. endforeach()
  124. if(ANDROID)
  125. string(REGEX REPLACE "DATA{([^ ;]+)}" "\\1" processed_FILES "${AST_FILES}")
  126. add_test(
  127. NAME ${test_name}
  128. COMMAND ${CMAKE_COMMAND}
  129. "-Darg_files_dest=${AST_FILES_DEST}"
  130. "-Darg_libs_dest=${AST_LIBS_DEST}"
  131. "-Darg_dev_test_dir=${AST_DEVICE_TEST_DIR}"
  132. "-Darg_dev_obj_store=${AST_DEVICE_OBJECT_STORE}"
  133. "-Darg_no_link_regex=${AST_NO_LINK_REGEX}"
  134. "-Darg_files=${processed_FILES}"
  135. "-Darg_libs=${AST_LIBS}"
  136. "-Darg_src_dir=${CMAKE_CURRENT_SOURCE_DIR}"
  137. -P ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/AndroidTestUtilities/PushToAndroidDevice.cmake)
  138. endif()
  139. endfunction()