verify-snippet.cmake 3.9 KB

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