verify-snippet.cmake 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Performs generic (non-project specific) validation of v1 Snippet File Contents
  2. include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
  3. function(snippet_has_fields snippet contents)
  4. get_filename_component(filename "${snippet}" NAME)
  5. json_has_key("${snippet}" "${contents}" role)
  6. json_has_key("${snippet}" "${contents}" result)
  7. json_has_key("${snippet}" "${contents}" workingDir)
  8. if (NOT filename MATCHES "^build-*")
  9. json_has_key("${snippet}" "${contents}" command)
  10. endif()
  11. if (filename MATCHES "^link-*")
  12. json_has_key("${snippet}" "${contents}" target)
  13. json_has_key("${snippet}" "${contents}" outputs)
  14. json_has_key("${snippet}" "${contents}" outputSizes)
  15. json_has_key("${snippet}" "${contents}" targetType)
  16. json_has_key("${snippet}" "${contents}" targetLabels)
  17. json_has_key("${snippet}" "${contents}" config)
  18. elseif (filename MATCHES "^compile-*")
  19. json_has_key("${snippet}" "${contents}" target)
  20. json_has_key("${snippet}" "${contents}" outputs)
  21. json_has_key("${snippet}" "${contents}" outputSizes)
  22. json_has_key("${snippet}" "${contents}" source)
  23. json_has_key("${snippet}" "${contents}" language)
  24. json_has_key("${snippet}" "${contents}" config)
  25. elseif (filename MATCHES "^custom-*")
  26. json_has_key("${snippet}" "${contents}" outputs)
  27. json_has_key("${snippet}" "${contents}" outputSizes)
  28. elseif (filename MATCHES "^test-*")
  29. json_has_key("${snippet}" "${contents}" testName)
  30. json_has_key("${snippet}" "${contents}" config)
  31. endif()
  32. if(ARGS_DYNAMIC_QUERY)
  33. json_has_key("${snippet}" "${contents}" dynamicSystemInformation)
  34. string(JSON dynamicSystemInfo ERROR_VARIABLE noInfo GET "${contents}" dynamicSystemInformation)
  35. if (noInfo MATCHES NOTFOUND)
  36. json_has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
  37. json_has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
  38. json_has_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
  39. json_has_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
  40. endif()
  41. else()
  42. json_missing_key("${snippet}" "${contents}" dynamicSystemInformation)
  43. string(JSON dynamicSystemInfo ERROR_VARIABLE noInfo GET "${contents}" dynamicSystemInformation)
  44. if (noInfo MATCHES NOTFOUND)
  45. json_missing_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
  46. json_missing_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
  47. json_missing_key("${snippet}" ${dynamicSystemInfo} beforeCPULoadAverage)
  48. json_missing_key("${snippet}" ${dynamicSystemInfo} beforeHostMemoryUsed)
  49. endif()
  50. endif()
  51. return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
  52. endfunction()
  53. function(snippet_valid_timing contents)
  54. string(JSON start GET "${contents}" timeStart)
  55. string(JSON duration GET "${contents}" duration)
  56. if (start LESS 0)
  57. snippet_error("${snippet}" "Negative time start: ${start}")
  58. endif()
  59. if (duration LESS 0)
  60. json_error("${snippet}" "Negative duration: ${end}")
  61. endif()
  62. return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
  63. endfunction()
  64. function(verify_snippet_data snippet contents)
  65. snippet_has_fields("${snippet}" "${contents}")
  66. snippet_valid_timing("${contents}")
  67. string(JSON version GET "${contents}" version)
  68. if (NOT version EQUAL 1)
  69. json_error("${snippet}" "Version must be 1, got: ${version}")
  70. endif()
  71. string(JSON outputs ERROR_VARIABLE noOutputs GET "${contents}" outputs)
  72. if (NOT outputs MATCHES NOTFOUND)
  73. string(JSON outputSizes ERROR_VARIABLE noOutputSizes GET "${contents}" outputSizes)
  74. list(LENGTH outputs outputsLen)
  75. list(LENGTH outputSizes outputSizesLen)
  76. if (outputSizes MATCHES NOTFOUND OR NOT outputsLen EQUAL outputSizesLen)
  77. json_error("${snippet}" "outputs and outputSizes do not match")
  78. endif()
  79. endif()
  80. return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED role)
  81. endfunction()
  82. function(verify_snippet_file snippet contents)
  83. verify_snippet_data("${snippet}" "${contents}")
  84. string(JSON role GET "${contents}" role)
  85. get_filename_component(filename "${snippet}" NAME)
  86. if (NOT filename MATCHES "^${role}-")
  87. json_error("${snippet}" "Role \"${role}\" doesn't match snippet filename")
  88. endif()
  89. return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED role)
  90. endfunction()