PushToAndroidDevice.cmake 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This function handles pushing all of the test files needed to the device.
  4. # It places the data files in the object store and makes links to them from
  5. # the appropriate directories.
  6. #
  7. # This function accepts the following named parameters:
  8. # DIRS : one or more directories needed for testing.
  9. # FILES : one or more files needed for testing.
  10. # LIBS : one or more libraries needed for testing.
  11. # DIRS_DEST : specify where the directories should be installed.
  12. # FILES_DEST : specify where the files should be installed.
  13. # LIBS_DEST : specify where the libraries should be installed.
  14. # DEV_OBJ_STORE : specify where the actual data files should be placed.
  15. # DEV_TEST_DIR : specify the root file for the module test directory.
  16. # The DEV_OBJ_STORE and DEV_TEST_DIR variables are required.
  17. # The parameters to this function should be set to the list of directories,
  18. # files, and libraries that need to be installed prior to testing.
  19. function(android_push_test_files_to_device)
  20. # The functions in the module need the adb executable.
  21. find_program(adb_executable adb)
  22. if(NOT adb_executable)
  23. message(FATAL_ERROR "could not find adb")
  24. endif()
  25. function(execute_adb_command)
  26. execute_process(COMMAND ${adb_executable} ${ARGN} RESULT_VARIABLE res_var OUTPUT_VARIABLE out_var ERROR_VARIABLE err_var)
  27. set(out_var ${out_var} PARENT_SCOPE)
  28. if(res_var)
  29. string(REGEX REPLACE ";" " " com "${ARGN}")
  30. message(FATAL_ERROR "Error occurred during adb command: adb ${com}\nError: ${err_var}.")
  31. endif()
  32. endfunction()
  33. # Checks to make sure that a given file exists on the device. If it does,
  34. # if(file_exists) will return true.
  35. macro(check_device_file_exists device_file file_exists)
  36. set(${file_exists} "")
  37. execute_process(
  38. COMMAND ${adb_executable} shell ls ${device_file}
  39. OUTPUT_VARIABLE out_var ERROR_VARIABLE out_var)
  40. if(NOT out_var) # when a directory exists but is empty the output is empty
  41. set(${file_exists} "YES")
  42. else()
  43. string(FIND ${out_var} "No such file or directory" no_file_exists)
  44. if(${no_file_exists} STREQUAL "-1") # -1 means the file exists
  45. set(${file_exists} "YES")
  46. endif()
  47. endif()
  48. endmacro()
  49. # Checks to see if a filename matches a regex.
  50. function(filename_regex filename reg_ex)
  51. string(REGEX MATCH ${reg_ex} filename_match ${filename})
  52. set(filename_match ${filename_match} PARENT_SCOPE)
  53. endfunction()
  54. # If a file with given name exists in the CMAKE_BINARY_DIR then use that file.
  55. # Otherwise use the file with root in CMAKE_CURRENT_SOURCE_DIR.
  56. macro(set_absolute_path relative_path absolute_path)
  57. set(${absolute_path} ${arg_src_dir}/${relative_path})
  58. if(EXISTS ${CMAKE_BINARY_DIR}/${relative_path})
  59. set(${absolute_path} ${CMAKE_BINARY_DIR}/${relative_path})
  60. endif()
  61. if(NOT EXISTS ${${absolute_path}})
  62. if(EXISTS ${relative_path})
  63. set(${absolute_path} ${relative_path})
  64. else()
  65. message(FATAL_ERROR "Cannot find file for specified path: ${relative_path}")
  66. endif()
  67. endif()
  68. endmacro()
  69. # This function pushes the data into the device object store and
  70. # creates a link to that data file in a specified location.
  71. #
  72. # This function requires the following un-named parameters:
  73. # data_path : absolute path to data to load into dev obj store.
  74. # dev_object_store : absolute path to the device object store directory.
  75. # link_origin : absolute path to the origin of the link to the dev obj store data file.
  76. function(push_and_link data_path dev_object_store link_origin)
  77. FILE(SHA1 ${data_path} hash_val)
  78. set(obj_store_dst ${dev_object_store}/${hash_val})
  79. check_device_file_exists(${obj_store_dst} obj_store_file_exists)
  80. # TODO: Verify that the object store file is indeed hashed correctly. Could use md5.
  81. if(NOT obj_store_file_exists)
  82. execute_adb_command(push ${data_path} ${obj_store_dst})
  83. endif()
  84. check_device_file_exists(${link_origin} link_exists)
  85. if(link_exists)
  86. execute_adb_command(shell rm -f ${link_origin})
  87. endif()
  88. foreach(ex ${arg_no_link_regex})
  89. filename_regex(${data_path} ${ex})
  90. LIST(APPEND match_ex ${filename_match})
  91. endforeach()
  92. if(match_ex)
  93. execute_adb_command(shell cp ${obj_store_dst} ${link_origin})
  94. else()
  95. execute_adb_command(shell ln -s ${obj_store_dst} ${link_origin})
  96. endif()
  97. endfunction()
  98. #----------------------------------------------------------------------------
  99. #--------------------Beginning of actual function----------------------------
  100. #----------------------------------------------------------------------------
  101. set(oneValueArgs FILES_DEST LIBS_DEST DEV_TEST_DIR DEV_OBJ_STORE)
  102. set(multiValueArgs FILES LIBS)
  103. cmake_parse_arguments(_ptd "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
  104. # Setup of object store and test dir.
  105. check_device_file_exists(${_ptd_DEV_OBJ_STORE} dev_obj_store_exists)
  106. if(NOT dev_obj_store_exists)
  107. execute_adb_command(shell mkdir -p ${_ptd_DEV_OBJ_STORE})
  108. endif()
  109. check_device_file_exists(${_ptd_DEV_TEST_DIR} test_dir_exists)
  110. if(test_dir_exists)
  111. # This is protected in the SetupProjectTests module.
  112. execute_adb_command(shell rm -r ${_ptd_DEV_TEST_DIR})
  113. endif()
  114. execute_adb_command(shell mkdir -p ${_ptd_DEV_TEST_DIR})
  115. # Looping over the various types of test data possible.
  116. foreach(TYPE ${multiValueArgs})
  117. if(_ptd_${TYPE})
  118. # determine if the data type destination has been explicitly specified.
  119. if(_ptd_${TYPE}_DEST)
  120. set(dest ${_ptd_${TYPE}_DEST})
  121. else()
  122. if(${TYPE} STREQUAL LIBS)
  123. set(dest ${_ptd_DEV_TEST_DIR}/lib)
  124. else()
  125. set(dest ${_ptd_DEV_TEST_DIR})
  126. endif()
  127. endif()
  128. execute_adb_command(shell mkdir -p ${dest})
  129. # Loop over the files passed in
  130. foreach(relative_path ${_ptd_${TYPE}})
  131. # The absolute path can be through the source directory or the build directory.
  132. # If the file/dir exists in the build directory that version is chosen.
  133. set_absolute_path(${relative_path} absolute_path)
  134. # Need to transfer all data files in the data directories to the device
  135. # except those explicitly ignored.
  136. if(${TYPE} STREQUAL FILES)
  137. get_filename_component(file_dir ${relative_path} DIRECTORY)
  138. # dest was determined earlier, relative_path is a dir, file is path from relative path to a data
  139. set(cur_dest ${dest}/${relative_path})
  140. set(on_dev_dir ${dest}/${file_dir})
  141. execute_adb_command(shell mkdir -p ${on_dev_dir})
  142. if(IS_SYMLINK ${absolute_path})
  143. get_filename_component(real_data_origin ${absolute_path} REALPATH)
  144. push_and_link(${real_data_origin} ${_ptd_DEV_OBJ_STORE} ${cur_dest})
  145. else()
  146. push_and_link(${absolute_path} ${_ptd_DEV_OBJ_STORE} ${cur_dest})
  147. endif()
  148. else() # LIBS
  149. execute_adb_command(push ${absolute_path} ${dest})
  150. endif()
  151. endforeach()
  152. endif()
  153. endforeach()
  154. endfunction()
  155. android_push_test_files_to_device(
  156. FILES_DEST ${arg_files_dest}
  157. LIBS_DEST ${arg_libs_dest}
  158. DEV_TEST_DIR ${arg_dev_test_dir}
  159. DEV_OBJ_STORE ${arg_dev_obj_store}
  160. FILES ${arg_files}
  161. LIBS ${arg_libs}
  162. )