RunCMakeTest.cmake 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. include(RunCMake)
  2. # Detect ninja version so we know what tests can be supported.
  3. execute_process(
  4. COMMAND "${RunCMake_MAKE_PROGRAM}" --version
  5. OUTPUT_VARIABLE ninja_out
  6. ERROR_VARIABLE ninja_out
  7. RESULT_VARIABLE ninja_res
  8. OUTPUT_STRIP_TRAILING_WHITESPACE
  9. )
  10. if(ninja_res EQUAL 0 AND "x${ninja_out}" MATCHES "^x[0-9]+\\.[0-9]+")
  11. set(ninja_version "${ninja_out}")
  12. message(STATUS "ninja version: ${ninja_version}")
  13. else()
  14. message(FATAL_ERROR "'ninja --version' reported:\n${ninja_out}")
  15. endif()
  16. function(run_NinjaToolMissing)
  17. set(RunCMake_MAKE_PROGRAM ninja-tool-missing)
  18. run_cmake(NinjaToolMissing)
  19. endfunction()
  20. run_NinjaToolMissing()
  21. function(run_CMP0058 case)
  22. # Use a single build tree for a few tests without cleaning.
  23. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/CMP0058-${case}-build)
  24. set(RunCMake_TEST_NO_CLEAN 1)
  25. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  26. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  27. run_cmake(CMP0058-${case})
  28. run_cmake_command(CMP0058-${case}-build ${CMAKE_COMMAND} --build .)
  29. endfunction()
  30. run_CMP0058(OLD-no)
  31. run_CMP0058(OLD-by)
  32. run_CMP0058(WARN-no)
  33. run_CMP0058(WARN-by)
  34. run_CMP0058(NEW-no)
  35. run_CMP0058(NEW-by)
  36. run_cmake(CustomCommandDepfile)
  37. function(run_SubDir)
  38. # Use a single build tree for a few tests without cleaning.
  39. set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/SubDir-build)
  40. set(RunCMake_TEST_NO_CLEAN 1)
  41. file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
  42. file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
  43. run_cmake(SubDir)
  44. if(WIN32)
  45. set(SubDir_all [[SubDir\all]])
  46. set(SubDir_test [[SubDir\test]])
  47. set(SubDir_install [[SubDir\install]])
  48. set(SubDirBinary_test [[SubDirBinary\test]])
  49. set(SubDirBinary_all [[SubDirBinary\all]])
  50. set(SubDirBinary_install [[SubDirBinary\install]])
  51. else()
  52. set(SubDir_all [[SubDir/all]])
  53. set(SubDir_test [[SubDir/test]])
  54. set(SubDir_install [[SubDir/install]])
  55. set(SubDirBinary_all [[SubDirBinary/all]])
  56. set(SubDirBinary_test [[SubDirBinary/test]])
  57. set(SubDirBinary_install [[SubDirBinary/install]])
  58. endif()
  59. run_cmake_command(SubDir-build ${CMAKE_COMMAND} --build . --target ${SubDir_all})
  60. run_cmake_command(SubDir-test ${CMAKE_COMMAND} --build . --target ${SubDir_test})
  61. run_cmake_command(SubDir-install ${CMAKE_COMMAND} --build . --target ${SubDir_install})
  62. run_cmake_command(SubDirBinary-build ${CMAKE_COMMAND} --build . --target ${SubDirBinary_all})
  63. run_cmake_command(SubDirBinary-test ${CMAKE_COMMAND} --build . --target ${SubDirBinary_test})
  64. run_cmake_command(SubDirBinary-install ${CMAKE_COMMAND} --build . --target ${SubDirBinary_install})
  65. endfunction()
  66. run_SubDir()
  67. function(run_ninja dir)
  68. execute_process(
  69. COMMAND "${RunCMake_MAKE_PROGRAM}" ${ARGN}
  70. WORKING_DIRECTORY "${dir}"
  71. OUTPUT_VARIABLE ninja_stdout
  72. ERROR_VARIABLE ninja_stderr
  73. RESULT_VARIABLE ninja_result
  74. )
  75. if(NOT ninja_result EQUAL 0)
  76. message(STATUS "
  77. ============ beginning of ninja's stdout ============
  78. ${ninja_stdout}
  79. =============== end of ninja's stdout ===============
  80. ")
  81. message(STATUS "
  82. ============ beginning of ninja's stderr ============
  83. ${ninja_stderr}
  84. =============== end of ninja's stderr ===============
  85. ")
  86. message(FATAL_ERROR
  87. "top ninja build failed exited with status ${ninja_result}")
  88. endif()
  89. endfunction(run_ninja)
  90. function(sleep delay)
  91. execute_process(
  92. COMMAND ${CMAKE_COMMAND} -E sleep ${delay}
  93. RESULT_VARIABLE result
  94. )
  95. if(NOT result EQUAL 0)
  96. message(FATAL_ERROR "failed to sleep for ${delay} second.")
  97. endif()
  98. endfunction(sleep)
  99. function(touch path)
  100. execute_process(
  101. COMMAND ${CMAKE_COMMAND} -E touch ${path}
  102. RESULT_VARIABLE result
  103. )
  104. if(NOT result EQUAL 0)
  105. message(FATAL_ERROR "failed to touch main ${path} file.")
  106. endif()
  107. endfunction(touch)
  108. macro(ninja_escape_path path out)
  109. string(REPLACE "\$ " "\$\$" "${out}" "${path}")
  110. string(REPLACE " " "\$ " "${out}" "${${out}}")
  111. string(REPLACE ":" "\$:" "${out}" "${${out}}")
  112. endmacro(ninja_escape_path)
  113. macro(shell_escape string out)
  114. string(REPLACE "\"" "\\\"" "${out}" "${string}")
  115. endmacro(shell_escape)
  116. function(run_sub_cmake test ninja_output_path_prefix)
  117. set(top_build_dir "${RunCMake_BINARY_DIR}/${test}-build/")
  118. file(REMOVE_RECURSE "${top_build_dir}")
  119. file(MAKE_DIRECTORY "${top_build_dir}")
  120. ninja_escape_path("${ninja_output_path_prefix}"
  121. escaped_ninja_output_path_prefix)
  122. # Generate top build ninja file.
  123. set(top_build_ninja "${top_build_dir}/build.ninja")
  124. shell_escape("${top_build_ninja}" escaped_top_build_ninja)
  125. set(build_ninja_dep "${top_build_dir}/build_ninja_dep")
  126. ninja_escape_path("${build_ninja_dep}" escaped_build_ninja_dep)
  127. shell_escape("${CMAKE_COMMAND}" escaped_CMAKE_COMMAND)
  128. file(WRITE "${build_ninja_dep}" "fake dependency of top build.ninja file\n")
  129. if(WIN32)
  130. set(cmd_prefix "cmd.exe /C \"")
  131. set(cmd_suffix "\"")
  132. else()
  133. set(cmd_prefix "")
  134. set(cmd_suffix "")
  135. endif()
  136. file(WRITE "${top_build_ninja}" "\
  137. subninja ${escaped_ninja_output_path_prefix}/build.ninja
  138. default ${escaped_ninja_output_path_prefix}/all
  139. # Sleep for 1 second before to regenerate to make sure the timestamp of
  140. # the top build.ninja will be strictly greater than the timestamp of the
  141. # sub/build.ninja file. We assume the system as 1 sec timestamp resolution.
  142. rule RERUN
  143. command = ${cmd_prefix}\"${escaped_CMAKE_COMMAND}\" -E sleep 1 && \"${escaped_CMAKE_COMMAND}\" -E touch \"${escaped_top_build_ninja}\"${cmd_suffix}
  144. description = Testing regeneration
  145. generator = 1
  146. build build.ninja: RERUN ${escaped_build_ninja_dep} || ${escaped_ninja_output_path_prefix}/build.ninja
  147. pool = console
  148. ")
  149. # Run sub cmake project.
  150. set(RunCMake_TEST_OPTIONS "-DCMAKE_NINJA_OUTPUT_PATH_PREFIX=${ninja_output_path_prefix}")
  151. set(RunCMake_TEST_BINARY_DIR "${top_build_dir}/${ninja_output_path_prefix}")
  152. run_cmake(${test})
  153. # Check there is no 'default' statement in Ninja file generated by CMake.
  154. set(sub_build_ninja "${RunCMake_TEST_BINARY_DIR}/build.ninja")
  155. file(READ "${sub_build_ninja}" sub_build_ninja_file)
  156. if(sub_build_ninja_file MATCHES "\ndefault [^\n][^\n]*all\n")
  157. message(FATAL_ERROR
  158. "unexpected 'default' statement found in '${sub_build_ninja}'")
  159. endif()
  160. # Run ninja from the top build directory.
  161. run_ninja("${top_build_dir}")
  162. # Test regeneration rules run in order.
  163. set(main_cmakelists "${RunCMake_SOURCE_DIR}/CMakeLists.txt")
  164. sleep(1) # Assume the system as 1 sec timestamp resolution.
  165. touch("${main_cmakelists}")
  166. touch("${build_ninja_dep}")
  167. run_ninja("${top_build_dir}")
  168. file(TIMESTAMP "${main_cmakelists}" mtime_main_cmakelists UTC)
  169. file(TIMESTAMP "${sub_build_ninja}" mtime_sub_build_ninja UTC)
  170. file(TIMESTAMP "${top_build_ninja}" mtime_top_build_ninja UTC)
  171. # Check sub build.ninja is regenerated.
  172. if(mtime_main_cmakelists STRGREATER mtime_sub_build_ninja)
  173. message(FATAL_ERROR
  174. "sub build.ninja not regenerated:
  175. CMakeLists.txt = ${mtime_main_cmakelists}
  176. sub/build.ninja = ${mtime_sub_build_ninja}")
  177. endif()
  178. # Check top build.ninja is regenerated after sub build.ninja.
  179. if(NOT mtime_top_build_ninja STRGREATER mtime_sub_build_ninja)
  180. message(FATAL_ERROR
  181. "top build.ninja not regenerated strictly after sub build.ninja:
  182. sub/build.ninja = ${mtime_sub_build_ninja}
  183. build.ninja = ${mtime_top_build_ninja}")
  184. endif()
  185. endfunction(run_sub_cmake)
  186. if("${ninja_version}" VERSION_LESS 1.6)
  187. message(WARNING "Ninja is too old; skipping rest of test.")
  188. return()
  189. endif()
  190. foreach(ninja_output_path_prefix "sub space" "sub")
  191. run_sub_cmake(Executable "${ninja_output_path_prefix}")
  192. run_sub_cmake(StaticLib "${ninja_output_path_prefix}")
  193. run_sub_cmake(SharedLib "${ninja_output_path_prefix}")
  194. run_sub_cmake(TwoLibs "${ninja_output_path_prefix}")
  195. run_sub_cmake(SubDirPrefix "${ninja_output_path_prefix}")
  196. run_sub_cmake(CustomCommandWorkingDirectory "${ninja_output_path_prefix}")
  197. endforeach(ninja_output_path_prefix)