VTK  9.7.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  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)
77  endif ()
78 
79  add_custom_command(
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})
91 
92  if (DEFINED _vtk_hash_source_HEADER_OUTPUT)
93  set("${_vtk_hash_source_HEADER_OUTPUT}"
94  "${_vtk_hash_source_header}"
95  PARENT_SCOPE)
96  endif ()
97 endfunction()
98 
99 if (_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")
103 endif ()
if(abs(tt-1.0)< eps)
Definition: PyrC1Basis.h:1