check-data-dir.cmake 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. include(${CMAKE_CURRENT_LIST_DIR}/verify-snippet.cmake)
  2. include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
  3. file(GLOB snippets LIST_DIRECTORIES false ${v1}/data/*)
  4. if (NOT snippets)
  5. add_error("No snippet files generated")
  6. endif()
  7. set(FOUND_SNIPPETS "")
  8. foreach(snippet IN LISTS snippets)
  9. get_filename_component(filename "${snippet}" NAME)
  10. read_json("${snippet}" contents)
  11. # Verify snippet file is valid
  12. verify_snippet_file("${snippet}" "${contents}")
  13. # Append to list of collected snippet roles
  14. if (NOT role IN_LIST FOUND_SNIPPETS)
  15. list(APPEND FOUND_SNIPPETS ${role})
  16. endif()
  17. # Verify target
  18. string(JSON target ERROR_VARIABLE noTarget GET "${contents}" target)
  19. if (NOT target MATCHES NOTFOUND)
  20. set(targets "main;lib;customTarget;TARGET_NAME")
  21. if (NOT ${target} IN_LIST targets)
  22. json_error("${snippet}" "Unexpected target: ${target}")
  23. endif()
  24. endif()
  25. # Verify output
  26. string(JSON result GET "${contents}" result)
  27. if (NOT ${result} EQUAL 0)
  28. json_error("${snippet}" "Compile command had non-0 result")
  29. endif()
  30. # Verify contents of compile-* Snippets
  31. if (filename MATCHES "^compile-")
  32. string(JSON target GET "${contents}" target)
  33. string(JSON source GET "${contents}" source)
  34. string(JSON language GET "${contents}" language)
  35. if (NOT language MATCHES "C\\+\\+")
  36. json_error("${snippet}" "Expected C++ compile language")
  37. endif()
  38. if (NOT source MATCHES "${target}.cxx$")
  39. json_error("${snippet}" "Unexpected source file")
  40. endif()
  41. endif()
  42. # Verify contents of link-* Snippets
  43. if (filename MATCHES "^link-")
  44. string(JSON target GET "${contents}" target)
  45. string(JSON targetType GET "${contents}" targetType)
  46. string(JSON targetLabels GET "${contents}" targetLabels)
  47. if (target MATCHES "main")
  48. if (NOT targetType MATCHES "EXECUTABLE")
  49. json_error("${snippet}" "Expected EXECUTABLE, target type was ${targetType}")
  50. endif()
  51. string(JSON nlabels LENGTH "${targetLabels}")
  52. if (NOT nlabels STREQUAL 2)
  53. json_error("${snippet}" "Missing Target Labels for: ${target}")
  54. else()
  55. string(JSON label1 GET "${contents}" targetLabels 0)
  56. string(JSON label2 GET "${contents}" targetLabels 1)
  57. if (NOT label1 MATCHES "label1" OR NOT label2 MATCHES "label2")
  58. json_error("${snippet}" "Missing Target Labels for: ${target}")
  59. endif()
  60. endif()
  61. endif()
  62. if (target MATCHES "lib")
  63. if (NOT targetType MATCHES "STATIC_LIBRARY")
  64. json_error("${snippet}" "Expected STATIC_LIBRARY, target type was ${targetType}")
  65. endif()
  66. string(JSON nlabels LENGTH "${targetLabels}")
  67. if (NOT nlabels STREQUAL 1)
  68. json_error("${snippet}" "Missing Target Labels for: ${target}")
  69. else()
  70. string(JSON label ERROR_VARIABLE noLabels GET "${contents}" targetLabels 0)
  71. if (NOT label MATCHES "label3")
  72. json_error("${snippet}" "Missing Target Labels for: ${target}")
  73. endif()
  74. endif()
  75. endif()
  76. endif()
  77. # Verify contents of custom-* Snippets
  78. if (filename MATCHES "^custom-")
  79. string(JSON outputs GET "${contents}" outputs)
  80. # if "outputs" is CMakeFiles/customTarget, should not have a "target"
  81. if (outputs MATCHES "customTarget")
  82. json_missing_key("${snippet}" "${contents}" target)
  83. # if "outputs" is empty list, should have "target" main
  84. elseif (outputs MATCHES "\\[\\]")
  85. json_assert_key("${snippet}" "${contents}" target main)
  86. # if "outputs" is includes output1, should also include output2, and no target
  87. elseif (outputs MATCHES "output1")
  88. if (NOT outputs MATCHES "output2")
  89. json_error("${snippet}" "Custom command missing outputs")
  90. endif()
  91. json_missing_key("${snippet}" "${contents}" target)
  92. # unrecognized outputs
  93. else()
  94. json_error("${snippet}" "Custom command has unexpected outputs\n${outputs}")
  95. endif()
  96. endif()
  97. # Verify contents of test-* Snippets
  98. if (filename MATCHES "^test-")
  99. string(JSON testName GET "${contents}" testName)
  100. if (NOT testName STREQUAL "test")
  101. json_error("${snippet}" "Unexpected testName: ${testName}")
  102. endif()
  103. endif()
  104. # Verify that Config is Debug
  105. if (filename MATCHES "^test|^compile|^link|^custom|^install")
  106. string(JSON config GET "${contents}" config)
  107. if (NOT config STREQUAL "Debug")
  108. json_error(${snippet} "Unexpected config: ${config}")
  109. endif()
  110. endif()
  111. # Verify command args were passed
  112. if (filename MATCHES "^cmakeBuild|^ctest")
  113. string(JSON command GET "${contents}" command)
  114. if (NOT command MATCHES "Debug")
  115. json_error(${snippet} "Command value missing passed arguments")
  116. endif()
  117. endif()
  118. endforeach()
  119. # Verify that listed snippets match expected roles
  120. set(EXPECTED_SNIPPETS configure generate)
  121. if (ARGS_BUILD)
  122. list(APPEND EXPECTED_SNIPPETS compile link custom cmakeBuild)
  123. endif()
  124. if (ARGS_TEST)
  125. list(APPEND EXPECTED_SNIPPETS ctest test)
  126. endif()
  127. if (ARGS_INSTALL)
  128. list(APPEND EXPECTED_SNIPPETS cmakeInstall)
  129. if (ARGS_INSTALL_PARALLEL)
  130. list(APPEND EXPECTED_SNIPPETS install)
  131. endif()
  132. endif()
  133. foreach(role IN LISTS EXPECTED_SNIPPETS)
  134. list(FIND FOUND_SNIPPETS "${role}" found)
  135. if (found EQUAL -1)
  136. add_error("No snippet files of role \"${role}\" were found in ${v1}")
  137. endif()
  138. endforeach()
  139. foreach(role IN LISTS FOUND_SNIPPETS)
  140. list(FIND EXPECTED_SNIPPETS "${role}" found)
  141. if (${found} EQUAL -1)
  142. add_error("Found unexpected snippet file of role \"${role}\" in ${v1}")
  143. endif()
  144. endforeach()
  145. # Verify test/install artifacts
  146. if (ARGS_INSTALL AND NOT EXISTS ${RunCMake_TEST_BINARY_DIR}/install)
  147. add_error("ctest --instrument launcher failed to install the project")
  148. endif()
  149. if (ARGS_TEST AND NOT EXISTS ${RunCMake_TEST_BINARY_DIR}/Testing)
  150. add_error("ctest --instrument launcher failed to test the project")
  151. endif()