verify-snippet.cmake 4.7 KB

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