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