VTK  9.3.1
vtkHashSource.cmake
Go to the documentation of this file.
1 #[==[
2 @file vtkHashSource.cmake
3 
4 This module contains the @ref vtk_hash_source function which may be used to
5 generate a hash from a file and place that in a generated header.
6 #]==]
7 
8 set(_vtkHashSource_script_file "${CMAKE_CURRENT_LIST_FILE}")
9 
10 #[==[
11 @brief Generate a header containing the hash of a file
12 
13 Add a rule to turn a file into a MD5 hash and place that in a C string.
14 
15 ~~~
16 vtk_hash_source(
17  INPUT <input>
18  [NAME <name>]
19  [ALGORITHM <algorithm>]
20  [HEADER_OUTPUT <header>])
21 ~~~
22 
23 The only required variable is `INPUT`.
24 
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.
34 #]==]
35 function (vtk_hash_source)
36  cmake_parse_arguments(PARSE_ARGV 0 _vtk_hash_source
37  ""
38  "INPUT;NAME;ALGORITHM;HEADER_OUTPUT"
39  "")
40 
41  if (_vtk_hash_source_UNPARSED_ARGUMENTS)
42  message(FATAL_ERROR
43  "Unrecognized arguments to vtk_hash_source: "
44  "${_vtk_hash_source_UNPARSED_ARGUMENTS}")
45  endif ()
46 
47  if (NOT DEFINED _vtk_hash_source_INPUT)
48  message(FATAL_ERROR
49  "Missing `INPUT` for vtk_hash_source.")
50  endif ()
51 
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")
56  endif ()
57 
58  if (NOT DEFINED _vtk_hash_source_ALGORITHM)
59  set(_vtk_hash_source_ALGORITHM MD5)
60  endif ()
61 
62  if (IS_ABSOLUTE "${_vtk_hash_source_INPUT}")
63  set(_vtk_hash_source_input
64  "${_vtk_hash_source_INPUT}")
65  else ()
66  set(_vtk_hash_source_input
67  "${CMAKE_CURRENT_SOURCE_DIR}/${_vtk_hash_source_INPUT}")
68  endif ()
69 
70  set(_vtk_hash_source_header
71  "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_hash_source_NAME}.h")
72 
73  add_custom_command(
74  OUTPUT "${_vtk_hash_source_header}"
75  DEPENDS "${_vtkHashSource_script_file}"
76  "${_vtk_hash_source_input}"
77  COMMAND "${CMAKE_COMMAND}"
78  "-Dinput_file=${_vtk_hash_source_input}"
79  "-Doutput_file=${_vtk_hash_source_header}"
80  "-Doutput_name=${_vtk_hash_source_NAME}"
81  "-Dalgorithm=${_vtk_hash_source_ALGORITHM}"
82  "-D_vtk_hash_source_run=ON"
83  -P "${_vtkHashSource_script_file}")
84 
85  if (DEFINED _vtk_hash_source_HEADER_OUTPUT)
86  set("${_vtk_hash_source_HEADER_OUTPUT}"
87  "${_vtk_hash_source_header}"
88  PARENT_SCOPE)
89  endif ()
90 endfunction()
91 
92 if (_vtk_hash_source_run AND CMAKE_SCRIPT_MODE_FILE)
93  file(${algorithm} "${input_file}" file_hash)
94  file(WRITE "${output_file}"
95  "#ifndef ${output_name}\n #define ${output_name} \"${file_hash}\"\n#endif\n")
96 endif ()