verify-snippet.cmake 4.5 KB

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