ExternalProjectUpdateTest.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Set the ExternalProject GIT_TAG to desired_tag, and make sure the
  2. # resulting checked out version is resulting_sha and rebuild.
  3. # This check's the correct behavior of the ExternalProject UPDATE_COMMAND.
  4. # Also verify that a fetch only occurs when fetch_expected is 1.
  5. macro(check_a_tag desired_tag resulting_sha fetch_expected)
  6. message( STATUS "Checking ExternalProjectUpdate to tag: ${desired_tag}" )
  7. # Remove the FETCH_HEAD file, so we can check if it gets replaced with a 'git
  8. # fetch'.
  9. set( FETCH_HEAD_file ${ExternalProjectUpdate_BINARY_DIR}/CMakeExternals/Source/TutorialStep1-GIT/.git/FETCH_HEAD )
  10. file( REMOVE ${FETCH_HEAD_file} )
  11. # Configure
  12. execute_process(COMMAND ${CMAKE_COMMAND}
  13. -G ${CMAKE_GENERATOR} -T "${CMAKE_GENERATOR_TOOLSET}"
  14. -A "${CMAKE_GENERATOR_PLATFORM}"
  15. -DTEST_GIT_TAG:STRING=${desired_tag}
  16. ${ExternalProjectUpdate_SOURCE_DIR}
  17. WORKING_DIRECTORY ${ExternalProjectUpdate_BINARY_DIR}
  18. RESULT_VARIABLE error_code
  19. )
  20. if(error_code)
  21. message(FATAL_ERROR "Could not configure the project.")
  22. endif()
  23. # Build
  24. execute_process(COMMAND ${CMAKE_COMMAND}
  25. --build ${ExternalProjectUpdate_BINARY_DIR}
  26. RESULT_VARIABLE error_code
  27. )
  28. if(error_code)
  29. message(FATAL_ERROR "Could not build the project.")
  30. endif()
  31. # Check the resulting SHA
  32. execute_process(COMMAND ${GIT_EXECUTABLE}
  33. rev-list --max-count=1 HEAD
  34. WORKING_DIRECTORY ${ExternalProjectUpdate_BINARY_DIR}/CMakeExternals/Source/TutorialStep1-GIT
  35. RESULT_VARIABLE error_code
  36. OUTPUT_VARIABLE tag_sha
  37. OUTPUT_STRIP_TRAILING_WHITESPACE
  38. )
  39. if(error_code)
  40. message(FATAL_ERROR "Could not check the sha.")
  41. endif()
  42. if(NOT (${tag_sha} STREQUAL ${resulting_sha}))
  43. message(FATAL_ERROR "UPDATE_COMMAND produced
  44. ${tag_sha}
  45. when
  46. ${resulting_sha}
  47. was expected."
  48. )
  49. endif()
  50. if( NOT EXISTS ${FETCH_HEAD_file} AND ${fetch_expected})
  51. message( FATAL_ERROR "Fetch did NOT occur when it was expected.")
  52. endif()
  53. if( EXISTS ${FETCH_HEAD_file} AND NOT ${fetch_expected})
  54. message( FATAL_ERROR "Fetch DID occur when it was not expected.")
  55. endif()
  56. message( STATUS "Checking ExternalProjectUpdate to tag: ${desired_tag} (disconnected)" )
  57. # Remove the FETCH_HEAD file, so we can check if it gets replaced with a 'git
  58. # fetch'.
  59. set( FETCH_HEAD_file ${ExternalProjectUpdate_BINARY_DIR}/CMakeExternals/Source/TutorialStep2-GIT/.git/FETCH_HEAD )
  60. file( REMOVE ${FETCH_HEAD_file} )
  61. # Check initial SHA
  62. execute_process(COMMAND ${GIT_EXECUTABLE}
  63. rev-list --max-count=1 HEAD
  64. WORKING_DIRECTORY ${ExternalProjectUpdate_BINARY_DIR}/CMakeExternals/Source/TutorialStep2-GIT
  65. RESULT_VARIABLE error_code
  66. OUTPUT_VARIABLE initial_sha
  67. OUTPUT_STRIP_TRAILING_WHITESPACE
  68. )
  69. # Configure
  70. execute_process(COMMAND ${CMAKE_COMMAND}
  71. -G ${CMAKE_GENERATOR} -T "${CMAKE_GENERATOR_TOOLSET}"
  72. -A "${CMAKE_GENERATOR_PLATFORM}"
  73. -DTEST_GIT_TAG:STRING=${desired_tag}
  74. ${ExternalProjectUpdate_SOURCE_DIR}
  75. WORKING_DIRECTORY ${ExternalProjectUpdate_BINARY_DIR}
  76. RESULT_VARIABLE error_code
  77. )
  78. if(error_code)
  79. message(FATAL_ERROR "Could not configure the project.")
  80. endif()
  81. # Build
  82. execute_process(COMMAND ${CMAKE_COMMAND}
  83. --build ${ExternalProjectUpdate_BINARY_DIR}
  84. RESULT_VARIABLE error_code
  85. )
  86. if(error_code)
  87. message(FATAL_ERROR "Could not build the project.")
  88. endif()
  89. if( EXISTS ${FETCH_HEAD_file} )
  90. message( FATAL_ERROR "Fetch occurred when it was not expected.")
  91. endif()
  92. # Check the resulting SHA
  93. execute_process(COMMAND ${GIT_EXECUTABLE}
  94. rev-list --max-count=1 HEAD
  95. WORKING_DIRECTORY ${ExternalProjectUpdate_BINARY_DIR}/CMakeExternals/Source/TutorialStep2-GIT
  96. RESULT_VARIABLE error_code
  97. OUTPUT_VARIABLE tag_sha
  98. OUTPUT_STRIP_TRAILING_WHITESPACE
  99. )
  100. if(error_code)
  101. message(FATAL_ERROR "Could not check the sha.")
  102. endif()
  103. if(NOT (${tag_sha} STREQUAL ${initial_sha}))
  104. message(FATAL_ERROR "Update occurred when it was not expected.")
  105. endif()
  106. # Update
  107. execute_process(COMMAND ${CMAKE_COMMAND}
  108. --build ${ExternalProjectUpdate_BINARY_DIR}
  109. --target TutorialStep2-GIT-update
  110. RESULT_VARIABLE error_code
  111. )
  112. if(error_code)
  113. message(FATAL_ERROR "Could not build the project.")
  114. endif()
  115. # Check the resulting SHA
  116. execute_process(COMMAND ${GIT_EXECUTABLE}
  117. rev-list --max-count=1 HEAD
  118. WORKING_DIRECTORY ${ExternalProjectUpdate_BINARY_DIR}/CMakeExternals/Source/TutorialStep2-GIT
  119. RESULT_VARIABLE error_code
  120. OUTPUT_VARIABLE tag_sha
  121. OUTPUT_STRIP_TRAILING_WHITESPACE
  122. )
  123. if(error_code)
  124. message(FATAL_ERROR "Could not check the sha.")
  125. endif()
  126. if(NOT (${tag_sha} STREQUAL ${resulting_sha}))
  127. message(FATAL_ERROR "UPDATE_COMMAND produced
  128. ${tag_sha}
  129. when
  130. ${resulting_sha}
  131. was expected."
  132. )
  133. endif()
  134. if( NOT EXISTS ${FETCH_HEAD_file} AND ${fetch_expected})
  135. message( FATAL_ERROR "Fetch did NOT occur when it was expected.")
  136. endif()
  137. if( EXISTS ${FETCH_HEAD_file} AND NOT ${fetch_expected})
  138. message( FATAL_ERROR "Fetch DID occur when it was not expected.")
  139. endif()
  140. endmacro()
  141. find_package(Git)
  142. set(do_git_tests 0)
  143. if(GIT_EXECUTABLE)
  144. set(do_git_tests 1)
  145. execute_process(
  146. COMMAND "${GIT_EXECUTABLE}" --version
  147. OUTPUT_VARIABLE ov
  148. OUTPUT_STRIP_TRAILING_WHITESPACE
  149. )
  150. string(REGEX REPLACE "^git version (.+)$" "\\1" git_version "${ov}")
  151. message(STATUS "git_version='${git_version}'")
  152. if(git_version VERSION_LESS 1.6.5)
  153. message(STATUS "No ExternalProject git tests with git client less than version 1.6.5")
  154. set(do_git_tests 0)
  155. endif()
  156. endif()
  157. if(do_git_tests)
  158. check_a_tag(origin/master 5842b503ba4113976d9bb28d57b5aee1ad2736b7 1)
  159. check_a_tag(tag1 d1970730310fe8bc07e73f15dc570071f9f9654a 1)
  160. # With the Git UPDATE_COMMAND performance patch, this will not required a
  161. # 'git fetch'
  162. check_a_tag(tag1 d1970730310fe8bc07e73f15dc570071f9f9654a 0)
  163. check_a_tag(tag2 5842b503ba4113976d9bb28d57b5aee1ad2736b7 1)
  164. check_a_tag(d19707303 d1970730310fe8bc07e73f15dc570071f9f9654a 1)
  165. check_a_tag(d19707303 d1970730310fe8bc07e73f15dc570071f9f9654a 0)
  166. check_a_tag(origin/master 5842b503ba4113976d9bb28d57b5aee1ad2736b7 1)
  167. # This is a remote symbolic ref, so it will always trigger a 'git fetch'
  168. check_a_tag(origin/master 5842b503ba4113976d9bb28d57b5aee1ad2736b7 1)
  169. endif()