Debian Codegraph @

cmake/Modules/vtkEncodeString.cmake

raw
1 # Taken from VTK 9.1.0
2 #
3 # /*=========================================================================
4 #
5 # Program: Visualization Toolkit
6 # Module: Copyright.txt
7 #
8 # Copyright (c) 1993-2015 Ken Martin, Will Schroeder, Bill Lorensen
9 # All rights reserved.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions are met:
13 #
14 # * Redistributions of source code must retain the above copyright notice,
15 # this list of conditions and the following disclaimer.
16 #
17 # * Redistributions in binary form must reproduce the above copyright notice,
18 # this list of conditions and the following disclaimer in the documentation
19 # and/or other materials provided with the distribution.
20 #
21 # * Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
22 # of any contributors may be used to endorse or promote products derived
23 # from this software without specific prior written permission.
24 #
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
26 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
29 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #
36 # =========================================================================*/
37
38 #[==[
39 @file vtkEncodeString.cmake
40
41 This module contains the @ref vtk_encode_string function which may be used to
42 turn a file into a C string. This is primarily used within a program so that
43 the content does not need to be retrieved from the filesystem at runtime, but
44 can still be developed as a standalone file.
45 #]==]
46
47 set(_vtkEncodeString_script_file "${CMAKE_CURRENT_LIST_FILE}")
48
49 #[==[
50 @brief Encode a file as a C string at build time
51
52 Adds a rule to turn a file into a C string. Note that any Unicode characters
53 will not be replaced with escaping, so it is recommended to avoid their usage
54 in the input.
55
56 ~~~
57 vtk_encode_string
58 INPUT <input>
59 [NAME <name>]
60 [EXPORT_SYMBOL <symbol>]
61 [EXPORT_HEADER <header>]
62 [HEADER_OUTPUT <variable>]
63 [SOURCE_OUTPUT <variable>]
64 [BINARY] [NUL_TERMINATE])
65 ~~~
66
67 The only required variable is `INPUT`, however, it is likely that at least one
68 of `HEADER_OUTPUT` or `SOURCE_OUTPUT` will be required to add them to a
69 library.
70
71 * `INPUT`: (Required) The path to the file to be embedded. If a relative path
72 is given, it will be interpreted as being relative to
73 `CMAKE_CURRENT_SOURCE_DIR`.
74 * `NAME`: This is the base name of the files that will be generated as well
75 as the variable name for the C string. It defaults to the basename of the
76 input without extensions.
77 * `EXPORT_SYMBOL`: The symbol to use for exporting the variable. By default,
78 it will not be exported. If set, `EXPORT_HEADER` must also be set.
79 * `EXPORT_HEADER`: The header to include for providing the given export
80 symbol. If set, `EXPORT_SYMBOL` should also be set.
81 * `HEADER_OUTPUT`: The variable to store the generated header path.
82 * `SOURCE_OUTPUT`: The variable to store the generated source path.
83 * `BINARY`: If given, the data will be written as an array of `unsigned char`
84 bytes.
85 * `NUL_TERMINATE`: If given, the binary data will be `NUL`-terminated. Only
86 makes sense with the `BINARY` flag. This is intended to be used if
87 embedding a file as a C string exceeds compiler limits on string literals
88 in various compilers.
89 #]==]
90 function (vtk_encode_string)
91 cmake_parse_arguments(PARSE_ARGV 0 _vtk_encode_string
92 "BINARY;NUL_TERMINATE"
93 "INPUT;NAME;EXPORT_SYMBOL;EXPORT_HEADER;HEADER_OUTPUT;SOURCE_OUTPUT"
94 "")
95
96 if (_vtk_encode_string_UNPARSED_ARGUMENTS)
97 message(FATAL_ERROR
98 "Unrecognized arguments to vtk_encode_string: "
99 "${_vtk_encode_string_UNPARSED_ARGUMENTS}")
100 endif ()
101
102 if (NOT DEFINED _vtk_encode_string_INPUT)
103 message(FATAL_ERROR
104 "Missing `INPUT` for vtk_encode_string.")
105 endif ()
106
107 if (NOT DEFINED _vtk_encode_string_NAME)
108 get_filename_component(_vtk_encode_string_NAME
109 "${_vtk_encode_string_INPUT}" NAME_WE)
110 endif ()
111
112 if (DEFINED _vtk_encode_string_EXPORT_SYMBOL AND
113 NOT DEFINED _vtk_encode_string_EXPORT_HEADER)
114 message(FATAL_ERROR
115 "Missing `EXPORT_HEADER` when using `EXPORT_SYMBOL`.")
116 endif ()
117
118 if (DEFINED _vtk_encode_string_EXPORT_HEADER AND
119 NOT DEFINED _vtk_encode_string_EXPORT_SYMBOL)
120 message(WARNING
121 "Missing `EXPORT_SYMBOL` when using `EXPORT_HEADER`.")
122 endif ()
123
124 if (NOT _vtk_encode_string_BINARY AND _vtk_encode_string_NUL_TERMINATE)
125 message(FATAL_ERROR
126 "The `NUL_TERMINATE` flag only makes sense with the `BINARY` flag.")
127 endif ()
128
129 set(_vtk_encode_string_header
130 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_encode_string_NAME}.h")
131 set(_vtk_encode_string_source
132 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_encode_string_NAME}.cxx")
133
134 if (IS_ABSOLUTE "${_vtk_encode_string_INPUT}")
135 set(_vtk_encode_string_input
136 "${_vtk_encode_string_INPUT}")
137 else ()
138 set(_vtk_encode_string_input
139 "${CMAKE_CURRENT_SOURCE_DIR}/${_vtk_encode_string_INPUT}")
140 endif ()
141
142 add_custom_command(
143 OUTPUT ${_vtk_encode_string_header}
144 ${_vtk_encode_string_source}
145 DEPENDS "${_vtkEncodeString_script_file}"
146 "${_vtk_encode_string_input}"
147 COMMAND "${CMAKE_COMMAND}"
148 "-Dsource_dir=${CMAKE_CURRENT_SOURCE_DIR}"
149 "-Dbinary_dir=${CMAKE_CURRENT_BINARY_DIR}"
150 "-Dsource_file=${_vtk_encode_string_input}"
151 "-Doutput_name=${_vtk_encode_string_NAME}"
152 "-Dexport_symbol=${_vtk_encode_string_EXPORT_SYMBOL}"
153 "-Dexport_header=${_vtk_encode_string_EXPORT_HEADER}"
154 "-Dbinary=${_vtk_encode_string_BINARY}"
155 "-Dnul_terminate=${_vtk_encode_string_NUL_TERMINATE}"
156 "-D_vtk_encode_string_run=ON"
157 -P "${_vtkEncodeString_script_file}")
158
159 if (DEFINED _vtk_encode_string_SOURCE_OUTPUT)
160 set("${_vtk_encode_string_SOURCE_OUTPUT}"
161 "${_vtk_encode_string_source}"
162 PARENT_SCOPE)
163 endif ()
164
165 if (DEFINED _vtk_encode_string_HEADER_OUTPUT)
166 set("${_vtk_encode_string_HEADER_OUTPUT}"
167 "${_vtk_encode_string_header}"
168 PARENT_SCOPE)
169 endif ()
170 endfunction ()
171
172 if (_vtk_encode_string_run AND CMAKE_SCRIPT_MODE_FILE)
173 set(output_header "${binary_dir}/${output_name}.h")
174 set(output_source "${binary_dir}/${output_name}.cxx")
175
176 file(WRITE "${output_header}" "")
177 file(WRITE "${output_source}" "")
178
179 file(APPEND "${output_header}"
180 "#ifndef ${output_name}_h\n#define ${output_name}_h\n\n")
181 if (export_symbol)
182 file(APPEND "${output_header}"
183 "#include \"${export_header}\"\n\n${export_symbol} ")
184 endif ()
185
186 if (IS_ABSOLUTE "${source_file}")
187 set(source_file_full "${source_file}")
188 else ()
189 set(source_file_full "${source_dir}/${source_file}")
190 endif ()
191 set(hex_arg)
192 if (binary)
193 set(hex_arg HEX)
194 endif ()
195 file(READ "${source_file_full}" original_content ${hex_arg})
196
197 if (binary)
198 if (nul_terminate)
199 string(APPEND original_content "00")
200 endif ()
201 string(LENGTH "${original_content}" output_size)
202 math(EXPR output_size "${output_size} / 2")
203 file(APPEND "${output_header}"
204 "extern const unsigned char ${output_name}[${output_size}];\n\n#endif\n")
205
206 file(APPEND "${output_source}"
207 "#include \"${output_name}.h\"\n\nconst unsigned char ${output_name}[${output_size}] = {\n")
208 string(REGEX REPLACE "\([0-9a-f][0-9a-f]\)" ",0x\\1" escaped_content "${original_content}")
209 # Hard line wrap the file.
210 string(REGEX REPLACE "\(..........................................................................,\)" "\\1\n" escaped_content "${escaped_content}")
211 # Remove the leading comma.
212 string(REGEX REPLACE "^," "" escaped_content "${escaped_content}")
213 file(APPEND "${output_source}"
214 "${escaped_content}\n")
215 file(APPEND "${output_source}"
216 "};\n")
217 else ()
218 file(APPEND "${output_header}"
219 "extern const char *${output_name};\n\n#endif\n")
220
221 # Escape literal backslashes.
222 string(REPLACE "\\" "\\\\" escaped_content "${original_content}")
223 # Escape literal double quotes.
224 string(REPLACE "\"" "\\\"" escaped_content "${escaped_content}")
225 # Turn newlines into newlines in the C string.
226 string(REPLACE "\n" "\\n\"\n\"" escaped_content "${escaped_content}")
227
228 file(APPEND "${output_source}"
229 "#include \"${output_name}.h\"\n\nconst char *${output_name} =\n")
230 file(APPEND "${output_source}"
231 "\"${escaped_content}\";\n")
232 endif ()
233 endif ()