vtkHashSource.cmake 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #[==[
  2. @file vtkHashSource.cmake
  3. This module contains the @ref vtk_hash_source function which may be used to
  4. generate a hash from a file and place that in a generated header.
  5. #]==]
  6. set(_vtkHashSource_script_file "${CMAKE_CURRENT_LIST_FILE}")
  7. include(CMakeParseArguments)
  8. #[==[
  9. @brief Generate a header containing the hash of a file
  10. Add a rule to turn a file into a MD5 hash and place that in a C string.
  11. ~~~
  12. vtk_hash_source(
  13. INPUT <input>
  14. [NAME <name>]
  15. [ALGORITHM <algorithm>]
  16. [HEADER_OUTPUT <header>])
  17. ~~~
  18. The only required variable is `INPUT`.
  19. * `INPUT`: (Required) The path to the file to process. If a relative path
  20. is given, it will be interpreted as being relative to
  21. `CMAKE_CURRENT_SOURCE_DIR`.
  22. * `NAME`: This is the base name of the header file that will be generated as
  23. well as the variable name for the C string. It defaults to basename of the
  24. input suffixed with `Hash`.
  25. * `ALGORITHM`: This is the hashing algorithm to use. Supported values are
  26. MD5, SHA1, SHA224, SHA256, SHA384, and SHA512. If not specified, MD5 is assumed.
  27. * `HEADER_OUTPUT`: the variable to store the generated header path.
  28. #]==]
  29. function (vtk_hash_source)
  30. cmake_parse_arguments(_vtk_hash_source
  31. ""
  32. "INPUT;NAME;ALGORITHM;HEADER_OUTPUT"
  33. ""
  34. ${ARGN})
  35. if (_vtk_hash_source_UNPARSED_ARGUMENTS)
  36. message(FATAL_ERROR
  37. "Unrecognized arguments to vtk_hash_source: "
  38. "${_vtk_hash_source_UNPARSED_ARGUMENTS}")
  39. endif ()
  40. if (NOT DEFINED _vtk_hash_source_INPUT)
  41. message(FATAL_ERROR
  42. "Missing `INPUT` for vtk_hash_source.")
  43. endif ()
  44. if (NOT DEFINED _vtk_hash_source_NAME)
  45. get_filename_component(_vtk_hash_source_NAME
  46. "${_vtk_hash_source_INPUT}" NAME_WE)
  47. set(_vtk_hash_source_NAME "${_vtk_hash_source_NAME}Hash")
  48. endif ()
  49. if (NOT DEFINED _vtk_hash_source_ALGORITHM)
  50. set(_vtk_hash_source_ALGORITHM MD5)
  51. endif ()
  52. if (IS_ABSOLUTE "${_vtk_hash_source_INPUT}")
  53. set(_vtk_hash_source_input
  54. "${_vtk_hash_source_INPUT}")
  55. else ()
  56. set(_vtk_hash_source_input
  57. "${CMAKE_CURRENT_SOURCE_DIR}/${_vtk_hash_source_INPUT}")
  58. endif ()
  59. set(_vtk_hash_source_header
  60. "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_hash_source_NAME}.h")
  61. add_custom_command(
  62. OUTPUT "${_vtk_hash_source_header}"
  63. DEPENDS "${_vtkHashSource_script_file}"
  64. "${_vtk_hash_source_input}"
  65. COMMAND "${CMAKE_COMMAND}"
  66. "-Dinput_file=${_vtk_hash_source_input}"
  67. "-Doutput_file=${_vtk_hash_source_header}"
  68. "-Doutput_name=${_vtk_hash_source_NAME}"
  69. "-Dalgorithm=${_vtk_hash_source_ALGORITHM}"
  70. "-D_vtk_hash_source_run=ON"
  71. -P "${_vtkHashSource_script_file}")
  72. if (DEFINED _vtk_hash_source_HEADER_OUTPUT)
  73. set("${_vtk_hash_source_HEADER_OUTPUT}"
  74. "${_vtk_hash_source_header}"
  75. PARENT_SCOPE)
  76. endif ()
  77. endfunction()
  78. if (_vtk_hash_source_run AND CMAKE_SCRIPT_MODE_FILE)
  79. file(${algorithm} "${input_file}" file_hash)
  80. file(WRITE "${output_file}"
  81. "#ifndef ${output_name}\n #define ${output_name} \"${file_hash}\"\n#endif\n")
  82. endif ()