2@file vtkHashSource.cmake
5generate a hash from a file and place that in a generated header.
8set(_vtkHashSource_script_file
"${CMAKE_CURRENT_LIST_FILE}")
11@brief Generate a header containing the hash of a file
13Add a rule to turn a file into a MD5 hash and place that in a C
string.
19 [ALGORITHM <algorithm>]
20 [HEADER_OUTPUT <header>])
23The only required variable is `INPUT`.
25 * `INPUT`: (Required) The path to the file to process. If a relative path
26 is given, it will be interpreted as being relative to
27 `CMAKE_CURRENT_SOURCE_DIR`.
28 * `NAME`: This is the base
name of the header file that will be generated as
29 well as the variable
name for the C
string. It defaults to basename of the
30 input suffixed with `Hash`.
31 * `ALGORITHM`: This is the hashing algorithm to use. Supported values are
32 MD5, SHA1, SHA224, SHA256, SHA384, and SHA512. If not specified, MD5 is assumed.
33 * `HEADER_OUTPUT`: the variable to store the generated header path.
36 cmake_parse_arguments(PARSE_ARGV 0 _vtk_hash_source
38 "INPUT;NAME;ALGORITHM;HEADER_OUTPUT"
41 if (_vtk_hash_source_UNPARSED_ARGUMENTS)
43 "Unrecognized arguments to vtk_hash_source: "
44 "${_vtk_hash_source_UNPARSED_ARGUMENTS}")
47 if (NOT DEFINED _vtk_hash_source_INPUT)
49 "Missing `INPUT` for vtk_hash_source.")
52 if (NOT DEFINED _vtk_hash_source_NAME)
53 get_filename_component(_vtk_hash_source_NAME
54 "${_vtk_hash_source_INPUT}" NAME_WE)
55 set(_vtk_hash_source_NAME
"${_vtk_hash_source_NAME}Hash")
58 if (NOT DEFINED _vtk_hash_source_ALGORITHM)
59 set(_vtk_hash_source_ALGORITHM MD5)
62 if (IS_ABSOLUTE
"${_vtk_hash_source_INPUT}")
63 set(_vtk_hash_source_input
64 "${_vtk_hash_source_INPUT}")
66 set(_vtk_hash_source_input
67 "${CMAKE_CURRENT_SOURCE_DIR}/${_vtk_hash_source_INPUT}")
70 set(_vtk_hash_source_header
71 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_hash_source_NAME}.h")
73 set(_vtk_hash_source_depends_args)
74 if (CMAKE_VERSION VERSION_GREATER_EQUAL
"3.27")
75 list(APPEND _vtk_hash_source_depends_args
76 DEPENDS_EXPLICIT_ONLY)
80 OUTPUT
"${_vtk_hash_source_header}"
81 DEPENDS
"${_vtkHashSource_script_file}"
82 "${_vtk_hash_source_input}"
83 COMMAND
"${CMAKE_COMMAND}"
84 "-Dinput_file=${_vtk_hash_source_input}"
85 "-Doutput_file=${_vtk_hash_source_header}"
86 "-Doutput_name=${_vtk_hash_source_NAME}"
87 "-Dalgorithm=${_vtk_hash_source_ALGORITHM}"
88 "-D_vtk_hash_source_run=ON"
89 -P
"${_vtkHashSource_script_file}"
90 ${_vtk_hash_source_depends_args})
92 if (DEFINED _vtk_hash_source_HEADER_OUTPUT)
93 set(
"${_vtk_hash_source_HEADER_OUTPUT}"
94 "${_vtk_hash_source_header}"
99if (_vtk_hash_source_run AND CMAKE_SCRIPT_MODE_FILE)
100 file(${algorithm}
"${input_file}" file_hash)
101 file(WRITE
"${output_file}"
102 "#ifndef ${output_name}\n #define ${output_name} \"${file_hash}\"\n#endif\n")
function vtk_hash_source()
Generate a header containing the hash of a file.