verify-trace.cmake 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Performs generic (non-project specific) validation of Trace File Contents
  2. include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
  3. include(${CMAKE_CURRENT_LIST_DIR}/verify-snippet.cmake)
  4. function(trace_entry_has_fields trace entry)
  5. json_has_key("${trace}" "${entry}" cat)
  6. json_has_key("${trace}" "${entry}" dur)
  7. json_has_key("${trace}" "${entry}" name)
  8. json_has_key("${trace}" "${entry}" ph)
  9. json_has_key("${trace}" "${entry}" pid)
  10. json_has_key("${trace}" "${entry}" tid)
  11. json_has_key("${trace}" "${entry}" ts)
  12. json_has_key("${trace}" "${entry}" args)
  13. return(PROPAGATE RunCMake_TEST_FAILED ERROR_MESSAGE)
  14. endfunction()
  15. function(trace_valid_entry trace entry)
  16. string(JSON ph GET "${entry}" ph)
  17. if (NOT ph STREQUAL "X")
  18. json_error("${trace}"
  19. "Invalid event \'${ph}\' (only complete events \'X\' expected)")
  20. endif()
  21. string(JSON start GET "${entry}" ts)
  22. if (start LESS 0)
  23. json_error("${trace}" "Negative time start: ${start}")
  24. endif()
  25. string(JSON duration GET "${entry}" dur)
  26. if (duration LESS 0)
  27. json_error("${trace}" "Negative duration: ${duration}")
  28. endif()
  29. string(JSON pid GET "${entry}" pid)
  30. if (NOT pid EQUAL 0)
  31. json_error("${trace}" "Invalid PID: ${pid}")
  32. endif()
  33. string(JSON tid GET "${entry}" tid)
  34. if (tid LESS 0)
  35. json_error("${trace}" "Invalid TID: ${tid}")
  36. endif()
  37. # Validate "args" as snippet data
  38. string(JSON args GET "${entry}" args)
  39. verify_snippet_data("${trace}" "${args}")
  40. # Check the formation of the "name" based on the snippet data
  41. string(JSON name GET "${entry}" name)
  42. string(JSON cat GET "${entry}" cat)
  43. set(error_name OFF)
  44. if (cat STREQUAL "compile")
  45. string(JSON source GET "${args}" source)
  46. if (NOT name STREQUAL "compile: ${source}")
  47. set(error_name ON)
  48. endif()
  49. elseif (cat STREQUAL "link")
  50. string(JSON target GET "${args}" target)
  51. if (NOT name STREQUAL "link: ${target}")
  52. set(error_name ON)
  53. endif()
  54. elseif (cat STREQUAL "custom" OR cat STREQUAL "install")
  55. string(JSON command GET "${args}" command)
  56. if (NOT name STREQUAL command)
  57. set(error_name ON)
  58. endif()
  59. elseif (cat STREQUAL "test")
  60. string(JSON testName GET "${args}" testName)
  61. if (NOT name STREQUAL "test: ${testName}")
  62. set(error_name ON)
  63. endif()
  64. else()
  65. string(JSON role GET "${args}" role)
  66. if (NOT name STREQUAL role)
  67. set(error_name ON)
  68. endif()
  69. endif()
  70. if (error_name)
  71. json_error("${trace}" "Invalid name: ${name}")
  72. endif()
  73. return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED)
  74. endfunction()
  75. function(verify_trace_entry trace entry)
  76. trace_entry_has_fields("${trace}" "${entry}")
  77. trace_valid_entry("${trace}" "${entry}")
  78. return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED)
  79. endfunction()
  80. function(verify_trace_file_name index_file trace_file)
  81. cmake_path(GET trace_file FILENAME trace_filename)
  82. cmake_path(GET index_file FILENAME index_filename)
  83. set(timestamp_regex "^(index|trace)-([A-Z0-9\\-]+)\\.json")
  84. if ("${trace_filename}" MATCHES "${timestamp_regex}")
  85. set(trace_timestamp "${CMAKE_MATCH_2}")
  86. else()
  87. add_error("Unable to parse timestamp from trace file name: \'${trace_filename}\'")
  88. endif()
  89. if ("${index_filename}" MATCHES "${timestamp_regex}")
  90. set(index_timestamp "${CMAKE_MATCH_2}")
  91. else()
  92. add_error("Unable to parse timestamp from index file name: \'${index_filename}\'")
  93. endif()
  94. if (NOT "${trace_timestamp}" STREQUAL "${index_timestamp}")
  95. add_error("Trace file timestamp \'${trace_filename}\' does not match the index \'${index_file}\'")
  96. endif()
  97. return(PROPAGATE ERROR_MESSAGE RunCMake_TEST_FAILED)
  98. endfunction()