2@file vtkEncodeString.cmake
5turn a file into a C
string. This is primarily used within a program so that
6the
content does not need to be retrieved from the filesystem at runtime, but
7can still be developed as a standalone file.
10set(_vtkEncodeString_script_file
"${CMAKE_CURRENT_LIST_FILE}")
13@brief Encode a file as a C
string at build
time
15Adds a rule to turn a file into a C
string. Note that any Unicode characters
16will not be replaced with escaping, so it is recommended to avoid their usage
23 [EXPORT_SYMBOL <symbol>]
24 [EXPORT_HEADER <header>]
25 [HEADER_OUTPUT <variable>]
26 [SOURCE_OUTPUT <variable>]
28 [ABI_MANGLE_SYMBOL_BEGIN <being>]
29 [ABI_MANGLE_SYMBOL_END <end>]
30 [ABI_MANGLE_HEADER <header>]
32 [BINARY] [NUL_TERMINATE])
35The only required variable is `INPUT`, however, it is likely that at least one
36of `HEADER_OUTPUT` or `SOURCE_OUTPUT` will be required to add them to a
39 * `INPUT`: (Required) The path to the file to be embedded. If a relative path
40 is given, it will be interpreted as being relative to
41 `CMAKE_CURRENT_SOURCE_DIR`.
42 * `NAME`: This is the base
name of the files that will be generated as well
43 as the variable
name for the C
string. It defaults to the basename of the
44 input without extensions.
45 * `EXPORT_SYMBOL`: The symbol to use for exporting the variable. By default,
46 it will not be exported. If set, `EXPORT_HEADER` must also be set.
47 * `EXPORT_HEADER`: The header to include for providing the given export
48 symbol. If set, `EXPORT_SYMBOL` should also be set.
49 * `HEADER_OUTPUT`: The variable to store the generated header path.
50 * `SOURCE_OUTPUT`: The variable to store the generated
source path.
51 * `BINARY`: If given, the
data will be written as an array of `unsigned char`
53 * `NUL_TERMINATE`: If given, the binary
data will be `NUL`-terminated. Only
54 makes sense with the `BINARY` flag. This is intended to be used
if
55 embedding a file as a C
string exceeds compiler limits
on string literals
57 * `ABI_MANGLE_SYMBOL_BEGIN`:
Open a mangling namespace with the given symbol.
58 If given, `ABI_MANGLE_SYMBOL_END` and `ABI_MANGLE_HEADER` must also be set.
59 * `ABI_MANGLE_SYMBOL_END`: Close a mangling namespace with the given symbol.
60 If given, `ABI_MANGLE_SYMBOL_BEGIN` and `ABI_MANGLE_HEADER` must also be set.
61 * `ABI_MANGLE_HEADER`: The header which provides the ABI mangling symbols.
62 If given, `ABI_MANGLE_SYMBOL_BEGIN` and `ABI_MANGLE_SYMBOL_END` must also
66 cmake_parse_arguments(PARSE_ARGV 0 _vtk_encode_string
67 "BINARY;NUL_TERMINATE"
68 "INPUT;NAME;EXPORT_SYMBOL;EXPORT_HEADER;HEADER_OUTPUT;SOURCE_OUTPUT;ABI_MANGLE_SYMBOL_BEGIN;ABI_MANGLE_SYMBOL_END;ABI_MANGLE_HEADER"
71 if (_vtk_encode_string_UNPARSED_ARGUMENTS)
73 "Unrecognized arguments to vtk_encode_string: "
74 "${_vtk_encode_string_UNPARSED_ARGUMENTS}")
77 if (NOT DEFINED _vtk_encode_string_INPUT)
79 "Missing `INPUT` for vtk_encode_string.")
82 if (NOT DEFINED _vtk_encode_string_NAME)
83 get_filename_component(_vtk_encode_string_NAME
84 "${_vtk_encode_string_INPUT}" NAME_WE)
87 if (DEFINED _vtk_encode_string_EXPORT_SYMBOL AND
88 NOT DEFINED _vtk_encode_string_EXPORT_HEADER)
90 "Missing `EXPORT_HEADER` when using `EXPORT_SYMBOL`.")
93 if (DEFINED _vtk_encode_string_EXPORT_HEADER AND
94 NOT DEFINED _vtk_encode_string_EXPORT_SYMBOL)
96 "Missing `EXPORT_SYMBOL` when using `EXPORT_HEADER`.")
99 if (DEFINED _vtk_encode_string_ABI_MANGLE_SYMBOL_BEGIN AND
100 (NOT DEFINED _vtk_encode_string_ABI_MANGLE_SYMBOL_END OR
101 NOT DEFINED _vtk_encode_string_ABI_MANGLE_HEADER))
103 "Missing `ABI_MANGLE_SYMBOL_END` or `ABI_MANGLE_HEADER` when using "
104 "`ABI_MANGLE_SYMBOL_BEGIN`.")
107 if (DEFINED _vtk_encode_string_ABI_MANGLE_SYMBOL_END AND
108 (NOT DEFINED _vtk_encode_string_ABI_MANGLE_SYMBOL_BEGIN OR
109 NOT DEFINED _vtk_encode_string_ABI_MANGLE_HEADER))
111 "Missing `ABI_MANGLE_SYMBOL_BEGIN` or `ABI_MANGLE_HEADER` when using "
112 "`ABI_MANGLE_SYMBOL_END`.")
115 if (DEFINED _vtk_encode_string_ABI_MANGLE_HEADER AND
116 (NOT DEFINED _vtk_encode_string_ABI_MANGLE_SYMBOL_BEGIN OR
117 NOT DEFINED _vtk_encode_string_ABI_MANGLE_SYMBOL_END))
119 "Missing `ABI_MANGLE_SYMBOL_BEGIN` or `ABI_MANGLE_SYMBOL_END` when "
120 "using `ABI_MANGLE_HEADER`.")
123 if (NOT _vtk_encode_string_BINARY AND _vtk_encode_string_NUL_TERMINATE)
125 "The `NUL_TERMINATE` flag only makes sense with the `BINARY` flag.")
128 set(_vtk_encode_string_header
129 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_encode_string_NAME}.h")
130 set(_vtk_encode_string_source
131 "${CMAKE_CURRENT_BINARY_DIR}/${_vtk_encode_string_NAME}.cxx")
133 if (IS_ABSOLUTE
"${_vtk_encode_string_INPUT}")
134 set(_vtk_encode_string_input
135 "${_vtk_encode_string_INPUT}")
137 set(_vtk_encode_string_input
138 "${CMAKE_CURRENT_SOURCE_DIR}/${_vtk_encode_string_INPUT}")
141 set(_vtk_encode_string_depends_args)
142 if (CMAKE_VERSION VERSION_GREATER_EQUAL
"3.27")
143 list(APPEND _vtk_encode_string_depends_args
144 DEPENDS_EXPLICIT_ONLY)
148 OUTPUT ${_vtk_encode_string_header}
149 ${_vtk_encode_string_source}
150 DEPENDS
"${_vtkEncodeString_script_file}"
151 "${_vtk_encode_string_input}"
152 COMMAND
"${CMAKE_COMMAND}"
153 "-Dsource_dir=${CMAKE_CURRENT_SOURCE_DIR}"
154 "-Dbinary_dir=${CMAKE_CURRENT_BINARY_DIR}"
155 "-Dsource_file=${_vtk_encode_string_input}"
156 "-Doutput_name=${_vtk_encode_string_NAME}"
157 "-Dexport_symbol=${_vtk_encode_string_EXPORT_SYMBOL}"
158 "-Dexport_header=${_vtk_encode_string_EXPORT_HEADER}"
159 "-Dabi_mangle_symbol_begin=${_vtk_encode_string_ABI_MANGLE_SYMBOL_BEGIN}"
160 "-Dabi_mangle_symbol_end=${_vtk_encode_string_ABI_MANGLE_SYMBOL_END}"
161 "-Dabi_mangle_header=${_vtk_encode_string_ABI_MANGLE_HEADER}"
162 "-Dbinary=${_vtk_encode_string_BINARY}"
163 "-Dnul_terminate=${_vtk_encode_string_NUL_TERMINATE}"
164 "-D_vtk_encode_string_run=ON"
165 -P
"${_vtkEncodeString_script_file}"
166 ${_vtk_encode_string_depends_args})
168 if (DEFINED _vtk_encode_string_SOURCE_OUTPUT)
169 set(
"${_vtk_encode_string_SOURCE_OUTPUT}"
170 "${_vtk_encode_string_source}"
174 if (DEFINED _vtk_encode_string_HEADER_OUTPUT)
175 set(
"${_vtk_encode_string_HEADER_OUTPUT}"
176 "${_vtk_encode_string_header}"
181if (_vtk_encode_string_run AND CMAKE_SCRIPT_MODE_FILE)
182 set(output_header
"${binary_dir}/${output_name}.h")
183 set(output_source
"${binary_dir}/${output_name}.cxx")
185 set(license_topfile
"// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen\n// SPDX-License-Identifier: BSD-3-Clause\n")
186 file(WRITE
"${output_header}" ${license_topfile})
187 file(WRITE
"${output_source}" ${license_topfile})
189 file(APPEND
"${output_header}"
190 "#ifndef ${output_name}_h\n#define ${output_name}_h\n\n")
192 file(APPEND
"${output_header}"
193 "#include \"${export_header}\"\n")
195 if (abi_mangle_header AND abi_mangle_symbol_begin)
196 file(APPEND
"${output_header}"
197 "#include \"${abi_mangle_header}\"\n\n${abi_mangle_symbol_begin}\n\n")
200 file(APPEND
"${output_header}"
204 if (IS_ABSOLUTE
"${source_file}")
205 set(source_file_full
"${source_file}")
207 set(source_file_full
"${source_dir}/${source_file}")
213 file(READ
"${source_file_full}" original_content ${hex_arg})
217 string(APPEND original_content
"00")
219 string(LENGTH
"${original_content}" output_size)
220 math(EXPR output_size
"${output_size} / 2")
221 file(APPEND
"${output_header}"
222 "extern const unsigned char ${output_name}[${output_size}];\n\n")
223 if (abi_mangle_symbol_end)
224 file(APPEND
"${output_header}"
225 "${abi_mangle_symbol_end}\n")
227 file(APPEND
"${output_header}"
230 file(APPEND
"${output_source}"
231 "#include \"${output_name}.h\"\n\n")
232 if (abi_mangle_symbol_begin)
233 file(APPEND
"${output_source}"
234 "${abi_mangle_symbol_begin}\n\n")
236 file(APPEND
"${output_source}"
237 "const unsigned char ${output_name}[${output_size}] = {\n")
238 string(REGEX REPLACE
"\([0-9a-f][0-9a-f]\)" ",0x\\1" escaped_content
"${original_content}")
239 # Hard line wrap the file.
240 string(REGEX REPLACE
"\(..........................................................................,\)" "\\1\n" escaped_content
"${escaped_content}")
241 # Remove the leading comma.
242 string(REGEX REPLACE
"^," "" escaped_content
"${escaped_content}")
243 file(APPEND
"${output_source}"
244 "${escaped_content}\n")
245 file(APPEND
"${output_source}"
247 if (abi_mangle_symbol_end)
248 file(APPEND
"${output_source}"
249 "${abi_mangle_symbol_end}\n")
252 file(APPEND
"${output_header}"
253 "extern const char *${output_name};\n\n")
254 if (abi_mangle_symbol_end)
255 file(APPEND
"${output_header}"
256 "${abi_mangle_symbol_end}\n\n")
258 file(APPEND
"${output_header}"
261 # Escape literal backslashes.
262 string(REPLACE
"\\" "\\\\" escaped_content
"${original_content}")
263 # Escape literal double quotes.
264 string(REPLACE
"\"" "\\\"" escaped_content
"${escaped_content}")
265 # Turn newlines into newlines in the C string.
266 string(REPLACE
"\n" "\\n\"\n\"" escaped_content
"${escaped_content}")
268 file(APPEND
"${output_source}"
269 "#include \"${output_name}.h\"\n\n")
270 if (abi_mangle_symbol_begin)
271 file(APPEND
"${output_source}"
272 "${abi_mangle_symbol_begin}\n\n")
274 file(APPEND
"${output_source}"
275 "const char *${output_name} =\n")
276 file(APPEND
"${output_source}"
277 "\"${escaped_content}\";\n")
278 if (abi_mangle_symbol_end)
279 file(APPEND
"${output_source}"
280 "\n${abi_mangle_symbol_end}\n")
VTKIOHDF_EXPORT bool Open(const char *fileName, hid_t &fileID)
Open a VTK HDF file and checks if it is valid.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
function vtk_encode_string()
Encode a file as a C string at build time.