hook.cmake 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. cmake_minimum_required(VERSION 3.30)
  2. include(${CMAKE_CURRENT_LIST_DIR}/json.cmake)
  3. include(${CMAKE_CURRENT_LIST_DIR}/verify-snippet.cmake)
  4. # Test CALLBACK script. Prints output information and verifies index file
  5. # Called as: cmake -P hook.cmake [CheckForStaticQuery?] [index.json]
  6. set(index ${CMAKE_ARGV4})
  7. if (NOT ${CMAKE_ARGV3})
  8. set(hasStaticInfo "UNEXPECTED")
  9. endif()
  10. read_json("${index}" contents)
  11. string(JSON hook GET "${contents}" hook)
  12. # Output is verified by *-stdout.txt files that the HOOK is run
  13. message(STATUS ${hook})
  14. # Not a check-*.cmake script, this is called as an instrumentation CALLBACK
  15. set(ERROR_MESSAGE "")
  16. function(add_error error)
  17. string(APPEND ERROR_MESSAGE "${error}\n")
  18. return(PROPAGATE ERROR_MESSAGE)
  19. endfunction()
  20. function(has_key_index key json)
  21. cmake_parse_arguments(ARG "UNEXPECTED" "" "" ${ARGN})
  22. unset(missingKey)
  23. string(JSON ${key} ERROR_VARIABLE missingKey GET "${json}" ${key})
  24. if (NOT ARG_UNEXPECTED AND NOT missingKey MATCHES NOTFOUND)
  25. add_error("\nKey \"${key}\" not in index:\n${json}")
  26. elseif(ARG_UNEXPECTED AND missingKey MATCHES NOTFOUND)
  27. add_error("\nUnexpected key \"${key}\" in index:\n${json}")
  28. endif()
  29. return(PROPAGATE ERROR_MESSAGE ${key})
  30. endfunction()
  31. has_key_index(version "${contents}")
  32. has_key_index(buildDir "${contents}")
  33. has_key_index(dataDir "${contents}")
  34. has_key_index(snippets "${contents}")
  35. if (NOT version EQUAL 1)
  36. add_error("Version must be 1, got: ${version}")
  37. endif()
  38. string(JSON length LENGTH "${snippets}")
  39. math(EXPR length "${length}-1")
  40. foreach(i RANGE ${length})
  41. string(JSON filename GET "${snippets}" ${i})
  42. if (NOT EXISTS ${dataDir}/${filename})
  43. add_error("Listed snippet: ${dataDir}/${filename} does not exist")
  44. endif()
  45. read_json(${dataDir}/${filename} snippet_contents)
  46. verify_snippet(${dataDir}/${filename} "${snippet_contents}")
  47. endforeach()
  48. has_key_index(staticSystemInformation "${contents}" ${hasStaticInfo})
  49. if (NOT hasStaticInfo STREQUAL UNEXPECTED)
  50. has_key_index(OSName "${staticSystemInformation}" ${hasStaticInfo})
  51. has_key_index(OSPlatform "${staticSystemInformation}" ${hasStaticInfo})
  52. has_key_index(OSRelease "${staticSystemInformation}" ${hasStaticInfo})
  53. has_key_index(OSVersion "${staticSystemInformation}" ${hasStaticInfo})
  54. has_key_index(familyId "${staticSystemInformation}" ${hasStaticInfo})
  55. has_key_index(hostname "${staticSystemInformation}" ${hasStaticInfo})
  56. has_key_index(is64Bits "${staticSystemInformation}" ${hasStaticInfo})
  57. has_key_index(modelId "${staticSystemInformation}" ${hasStaticInfo})
  58. has_key_index(numberOfLogicalCPU "${staticSystemInformation}" ${hasStaticInfo})
  59. has_key_index(numberOfPhysicalCPU "${staticSystemInformation}" ${hasStaticInfo})
  60. has_key_index(processorAPICID "${staticSystemInformation}" ${hasStaticInfo})
  61. has_key_index(processorCacheSize "${staticSystemInformation}" ${hasStaticInfo})
  62. has_key_index(processorClockFrequency "${staticSystemInformation}" ${hasStaticInfo})
  63. has_key_index(processorName "${staticSystemInformation}" ${hasStaticInfo})
  64. has_key_index(totalPhysicalMemory "${staticSystemInformation}" ${hasStaticInfo})
  65. has_key_index(totalVirtualMemory "${staticSystemInformation}" ${hasStaticInfo})
  66. has_key_index(vendorID "${staticSystemInformation}" ${hasStaticInfo})
  67. has_key_index(vendorString "${staticSystemInformation}" ${hasStaticInfo})
  68. endif()
  69. get_filename_component(dataDir ${index} DIRECTORY)
  70. get_filename_component(v1 ${dataDir} DIRECTORY)
  71. if (EXISTS ${v1}/${hook}.hook)
  72. add_error("Received multiple triggers of the same hook: ${hook}")
  73. endif()
  74. file(WRITE ${v1}/${hook}.hook "${ERROR_MESSAGE}")
  75. if (NOT ERROR_MESSAGE MATCHES "^$")
  76. message(FATAL_ERROR ${ERROR_MESSAGE})
  77. endif()