Debian Codegraph @

cmake/Modules/FindABIMap.cmake

raw
1 #
2 # Copyright (c) 2018 Anderson Toshiyuki Sasaki <[email protected]>
3 #
4 # Redistribution and use is allowed according to the terms of the New
5 # BSD license.
6 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
7 #
8
9 #.rst:
10 # FindABIMap
11 # ----------
12 #
13 # This file provides functions to generate the symbol version script. It uses
14 # the ``abimap`` tool to generate and update the linker script file. It can be
15 # installed by calling::
16 #
17 # $ pip install abimap
18 #
19 # The ``function generate_map_file`` generates a symbol version script
20 # containing the provided symbols. It defines a custom command which sets
21 # ``target_name`` as its ``OUTPUT``.
22 #
23 # The experimental function ``extract_symbols()`` is provided as a simple
24 # parser to extract the symbols from C header files. It simply extracts symbols
25 # followed by an opening '``(``'. It is recommended to use a filter pattern to
26 # select the lines to be considered. It defines a custom command which sets
27 # ``target_name`` as its output.
28 #
29 # The helper function ``get_files_list()`` is provided to find files given a
30 # name pattern. It defines a custom command which sets ``target_name`` as its
31 # output.
32 #
33 # Functions provided
34 # ------------------
35 #
36 # ::
37 #
38 # generate_map_file(target_name
39 # RELEASE_NAME_VERSION release_name
40 # SYMBOLS symbols_target
41 # [CURRENT_MAP cur_map]
42 # [FINAL]
43 # [BREAK_ABI]
44 # [COPY_TO output]
45 # )
46 #
47 # ``target_name``:
48 # Required, expects the name of the file to receive the generated symbol
49 # version script. It should be added as a dependency for the library. Use the
50 # linker option ``--version-script filename`` to add the version information
51 # to the symbols when building the library.
52 #
53 # ``RELEASE_NAME_VERSION``:
54 # Required, expects a string containing the name and version information to be
55 # added to the symbols in the format ``lib_name_1_2_3``.
56 #
57 # ``SYMBOLS``:
58 # Required, expects a target with the property ``LIST_FILE`` containing a path
59 # to a file containing the list of symbols to be added to the symbol version
60 # script.
61 #
62 # ``CURRENT_MAP``:
63 # Optional. If given, the new set of symbols will be checked against the
64 # ones contained in the ``cur_map`` file and updated properly. If an
65 # incompatible change is detected and ``BREAK_ABI`` is not defined, the build
66 # will fail.
67 #
68 # ``FINAL``:
69 # Optional. If given, will provide the ``--final`` option to ``abimap`` tool,
70 # which will mark the modified release in the symbol version script with a
71 # special comment, preventing later changes. This option should be set when
72 # creating a library release and the resulting map file should be stored with
73 # the source code.
74 #
75 # ``BREAK_ABI``:
76 # Optional. If provided, will use ``abimap`` ``--allow-abi-break`` option, which
77 # accepts incompatible changes to the set of symbols. This is necessary if any
78 # previously existing symbol were removed.
79 #
80 # ``COPY_TO``:
81 # Optional, expects a string containing the path to where the generated
82 # map file will be copied.
83 #
84 # Example:
85 #
86 # .. code-block:: cmake
87 #
88 # find_package(ABIMap)
89 # generate_map_file("lib.map"
90 # RELEASE_NAME_VERSION "lib_1_0_0"
91 # SYMBOLS symbols
92 # )
93 #
94 # Where the target ``symbols`` has its property ``LIST_FILE`` set to the path to
95 # a file containing::
96 #
97 # ``symbol1``
98 # ``symbol2``
99 #
100 # This example would result in the symbol version script to be created in
101 # ``${CMAKE_CURRENT_BINARY_DIR}/lib.map`` containing the provided symbols.
102 #
103 # ::
104 #
105 # get_files_list(target_name
106 # DIRECTORIES dir1 [dir2 ...]
107 # FILES_PATTERNS exp1 [exp2 ...]
108 # [COPY_TO output]
109 # )
110 #
111 # ``target_name``:
112 # Required, expects the name of the target to be created. A file named as
113 # ``${target_name}.list`` will be created in
114 # ``${CMAKE_CURRENT_BINARY_DIR}`` to receive the list of files found.
115 #
116 # ``DIRECTORIES``:
117 # Required, expects a list of directories paths. Only absolute paths are
118 # supported.
119 #
120 # ``FILES_PATTERN``:
121 # Required, expects a list of matching expressions to find the files to be
122 # considered in the directories.
123 #
124 # ``COPY_TO``:
125 # Optional, expects a string containing the path to where the file containing
126 # the list of files will be copied.
127 #
128 # This command searches the directories provided in ``DIRECTORIES`` for files
129 # matching any of the patterns provided in ``FILES_PATTERNS``. The obtained list
130 # is written to the path specified by ``output``. A target named ``target_name``
131 # will be created and its property ``LIST_FILE`` will be set to contain
132 # ``${CMAKE_CURRENT_BINARY_DIR}/${target_name}.list``
133 #
134 # Example:
135 #
136 # .. code-block:: cmake
137 #
138 # find_package(ABIMap)
139 # get_files_list(target
140 # DIRECTORIES "/include/mylib"
141 # FILES_PATTERNS "*.h"
142 # COPY_TO "my_list.txt"
143 # )
144 #
145 # Consider that ``/include/mylib`` contains 3 files, ``h1.h``, ``h2.h``, and
146 # ``h3.hpp``
147 #
148 # Will result in a file ``my_list.txt`` containing::
149 #
150 # ``h1.h;h2.h``
151 #
152 # And the target ``target`` will have its property ``LIST_FILE`` set to contain
153 # ``${CMAKE_CURRENT_BINARY_DIR}/target.list``
154 #
155 # ::
156 #
157 # extract_symbols(target_name
158 # HEADERS_LIST headers_list_target
159 # [FILTER_PATTERN pattern]
160 # [COPY_TO output]
161 # )
162 #
163 # ``target_name``:
164 # Required, expects the name of the target to be created. A file named after
165 # the string given in ``target_name`` will be created in
166 # ``${CMAKE_CURRENT_BINARY_DIR}`` to receive the list of symbols.
167 #
168 # ``HEADERS_LIST``:
169 # Required, expects a target with the property ``LIST_FILE`` set, containing a
170 # file path. Such file must contain a list of files paths.
171 #
172 # ``FILTER_PATTERN``:
173 # Optional, expects a string. Only the lines containing the filter pattern
174 # will be considered.
175 #
176 # ``COPY_TO``:
177 # Optional, expects a string containing the path to where the file containing
178 # the found symbols will be copied.
179 #
180 # This command extracts the symbols from the files listed in
181 # ``headers_list`` and write them on the ``output`` file. If ``pattern``
182 # is provided, then only the lines containing the string given in ``pattern``
183 # will be considered. It is recommended to provide a ``FILTER_PATTERN`` to mark
184 # the lines containing exported function declaration, since this function is
185 # experimental and can return wrong symbols when parsing the header files. A
186 # target named ``target_name`` will be created with the property ``LIST_FILE``
187 # set to contain ``${CMAKE_CURRENT_BINARY_DIR}/${target_name}.list``.
188 #
189 # Example:
190 #
191 # .. code-block:: cmake
192 #
193 # find_package(ABIMap)
194 # extract_symbols("lib.symbols"
195 # HEADERS_LIST "headers_target"
196 # FILTER_PATTERN "API_FUNCTION"
197 # )
198 #
199 # Where ``LIST_FILE`` property in ``headers_target`` points to a file
200 # containing::
201 #
202 # header1.h;header2.h
203 #
204 # Where ``header1.h`` contains::
205 #
206 # API_FUNCTION int exported_func1(int a, int b);
207 #
208 # ``header2.h`` contains::
209 #
210 # API_FUNCTION int exported_func2(int a);
211 #
212 # int private_func2(int b);
213 #
214 # Will result in a file ``lib.symbols.list`` in ``${CMAKE_CURRENT_BINARY_DIR}``
215 # containing::
216 #
217 # ``exported_func1``
218 # ``exported_func2``
219 #
220
221 # Search for python which is required
222 if (ABIMap_FIND_REQURIED)
223 find_package(Python REQUIRED)
224 else()
225 find_package(Python)
226 endif()
227
228 if (TARGET Python::Interpreter)
229 # Search for abimap tool used to generate the map files
230 find_program(ABIMAP_EXECUTABLE NAMES abimap DOC "path to the abimap executable")
231 mark_as_advanced(ABIMAP_EXECUTABLE)
232
233 if (NOT ABIMAP_EXECUTABLE AND UNIX)
234 message(STATUS "Could not find `abimap` in PATH."
235 " It can be found in PyPI as `abimap`"
236 " (try `pip install abimap`)")
237 endif ()
238
239 if (ABIMAP_EXECUTABLE)
240 # Get the abimap version
241 execute_process(COMMAND ${ABIMAP_EXECUTABLE} version
242 OUTPUT_VARIABLE ABIMAP_VERSION_STRING
243 OUTPUT_STRIP_TRAILING_WHITESPACE)
244
245 # If the version string starts with abimap-, strip it
246 if ("abimap" STRLESS_EQUAL ${ABIMAP_VERSION_STRING})
247 string(REGEX REPLACE "abimap-" "" ABIMAP_VERSION_STRING "${ABIMAP_VERSION_STRING}")
248 endif()
249 endif()
250
251 include(FindPackageHandleStandardArgs)
252 find_package_handle_standard_args(ABIMap
253 REQUIRED_VARS ABIMAP_EXECUTABLE
254 VERSION_VAR ABIMAP_VERSION_STRING)
255 endif()
256
257
258 if (ABIMAP_FOUND)
259
260 # Define helper scripts
261 set(_EXTRACT_SYMBOLS_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/ExtractSymbols.cmake)
262 set(_GENERATE_MAP_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/GenerateMap.cmake)
263 set(_GET_FILES_LIST_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/GetFilesList.cmake)
264
265 function(get_file_list _TARGET_NAME)
266
267 set(one_value_arguments
268 COPY_TO
269 )
270
271 set(multi_value_arguments
272 DIRECTORIES
273 FILES_PATTERNS
274 )
275
276 cmake_parse_arguments(_get_files_list
277 ""
278 "${one_value_arguments}"
279 "${multi_value_arguments}"
280 ${ARGN}
281 )
282
283 # The DIRS argument is required
284 if (NOT DEFINED _get_files_list_DIRECTORIES)
285 message(FATAL_ERROR "No directories paths provided. Provide a list of"
286 " directories paths containing header files.")
287 endif()
288
289 # The FILES_PATTERNS argument is required
290 if (NOT DEFINED _get_files_list_FILES_PATTERNS)
291 message(FATAL_ERROR "No matching expressions provided. Provide a list"
292 " of matching patterns for the header files.")
293 endif()
294
295 set(_FILES_LIST_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${_TARGET_NAME}.list)
296
297 get_filename_component(_get_files_list_OUTPUT_PATH
298 "${_FILES_LIST_OUTPUT_PATH}"
299 ABSOLUTE)
300
301 add_custom_target(
302 ${_TARGET_NAME}_int ALL
303 COMMAND ${CMAKE_COMMAND}
304 -DOUTPUT_PATH=${_get_files_list_OUTPUT_PATH}
305 -DDIRECTORIES=${_get_files_list_DIRECTORIES}
306 -DFILES_PATTERNS=${_get_files_list_FILES_PATTERNS}
307 -P ${_GET_FILES_LIST_SCRIPT}
308 COMMENT
309 "Searching for files"
310 VERBATIM
311 )
312
313 if (DEFINED _get_files_list_COPY_TO)
314 # Copy the generated file back to the COPY_TO
315 add_custom_target(${_TARGET_NAME} ALL
316 COMMAND
317 ${CMAKE_COMMAND} -E copy_if_different
318 ${_FILES_LIST_OUTPUT_PATH} ${_get_files_list_COPY_TO}
319 DEPENDS ${_TARGET_NAME}_int
320 COMMENT "Copying ${_TARGET_NAME} to ${_get_files_list_COPY_TO}"
321 VERBATIM
322 )
323 else()
324 add_custom_target(${_TARGET_NAME} ALL
325 DEPENDS ${_TARGET_NAME}_int
326 )
327 endif()
328
329 set_target_properties(${_TARGET_NAME}
330 PROPERTIES LIST_FILE ${_FILES_LIST_OUTPUT_PATH}
331 )
332
333 endfunction()
334
335 function(extract_symbols _TARGET_NAME)
336
337 set(one_value_arguments
338 FILTER_PATTERN
339 HEADERS_LIST
340 COPY_TO
341 )
342
343 set(multi_value_arguments
344 )
345
346 cmake_parse_arguments(_extract_symbols
347 ""
348 "${one_value_arguments}"
349 "${multi_value_arguments}"
350 ${ARGN}
351 )
352
353 # The HEADERS_LIST_FILE argument is required
354 if (NOT DEFINED _extract_symbols_HEADERS_LIST)
355 message(FATAL_ERROR "No target provided in HEADERS_LIST. Provide a"
356 " target with the property LIST_FILE set as the"
357 " path to the file containing the list of headers.")
358 endif()
359
360 get_filename_component(_SYMBOLS_OUTPUT_PATH
361 "${CMAKE_CURRENT_BINARY_DIR}/${_TARGET_NAME}.list"
362 ABSOLUTE
363 )
364
365 get_target_property(_HEADERS_LIST_FILE
366 ${_extract_symbols_HEADERS_LIST}
367 LIST_FILE
368 )
369
370 add_custom_target(
371 ${_TARGET_NAME}_int ALL
372 COMMAND ${CMAKE_COMMAND}
373 -DOUTPUT_PATH=${_SYMBOLS_OUTPUT_PATH}
374 -DHEADERS_LIST_FILE=${_HEADERS_LIST_FILE}
375 -DFILTER_PATTERN=${_extract_symbols_FILTER_PATTERN}
376 -P ${_EXTRACT_SYMBOLS_SCRIPT}
377 DEPENDS ${_extract_symbols_HEADERS_LIST}
378 COMMENT "Extracting symbols from headers"
379 VERBATIM
380 )
381
382 if (DEFINED _extract_symbols_COPY_TO)
383 # Copy the generated file back to the COPY_TO
384 add_custom_target(${_TARGET_NAME} ALL
385 COMMAND
386 ${CMAKE_COMMAND} -E copy_if_different
387 ${_SYMBOLS_OUTPUT_PATH} ${_extract_symbols_COPY_TO}
388 DEPENDS ${_TARGET_NAME}_int
389 COMMENT "Copying ${_TARGET_NAME} to ${_extract_symbols_COPY_TO}"
390 VERBATIM
391 )
392 else()
393 add_custom_target(${_TARGET_NAME} ALL
394 DEPENDS ${_TARGET_NAME}_int
395 )
396 endif()
397
398 set_target_properties(${_TARGET_NAME}
399 PROPERTIES LIST_FILE ${_SYMBOLS_OUTPUT_PATH}
400 )
401
402 endfunction()
403
404 function(generate_map_file _TARGET_NAME)
405
406 set(options
407 FINAL
408 BREAK_ABI
409 )
410
411 set(one_value_arguments
412 RELEASE_NAME_VERSION
413 SYMBOLS
414 CURRENT_MAP
415 COPY_TO
416 )
417
418 set(multi_value_arguments
419 )
420
421 cmake_parse_arguments(_generate_map_file
422 "${options}"
423 "${one_value_arguments}"
424 "${multi_value_arguments}"
425 ${ARGN}
426 )
427
428 if (NOT DEFINED _generate_map_file_SYMBOLS)
429 message(FATAL_ERROR "No target provided in SYMBOLS. Provide a target"
430 " with the property LIST_FILE set as the path to"
431 " the file containing the list of symbols.")
432 endif()
433
434 if (NOT DEFINED _generate_map_file_RELEASE_NAME_VERSION)
435 message(FATAL_ERROR "Release name and version not provided."
436 " (e.g. libname_1_0_0)")
437 endif()
438
439
440 get_target_property(_SYMBOLS_FILE
441 ${_generate_map_file_SYMBOLS}
442 LIST_FILE
443 )
444
445 # Set generated map file path
446 get_filename_component(_MAP_OUTPUT_PATH
447 "${CMAKE_CURRENT_BINARY_DIR}/${_TARGET_NAME}"
448 ABSOLUTE
449 )
450
451 add_custom_target(
452 ${_TARGET_NAME}_int ALL
453 COMMAND ${CMAKE_COMMAND}
454 -DABIMAP_EXECUTABLE=${ABIMAP_EXECUTABLE}
455 -DSYMBOLS=${_SYMBOLS_FILE}
456 -DCURRENT_MAP=${_generate_map_file_CURRENT_MAP}
457 -DOUTPUT_PATH=${_MAP_OUTPUT_PATH}
458 -DFINAL=${_generate_map_file_FINAL}
459 -DBREAK_ABI=${_generate_map_file_BREAK_ABI}
460 -DRELEASE_NAME_VERSION=${_generate_map_file_RELEASE_NAME_VERSION}
461 -P ${_GENERATE_MAP_SCRIPT}
462 DEPENDS ${_generate_map_file_SYMBOLS}
463 COMMENT "Generating the map ${_TARGET_NAME}"
464 VERBATIM
465 )
466
467 # Add a custom command setting the map as OUTPUT to allow it to be added as
468 # a generated source
469 add_custom_command(
470 OUTPUT ${_MAP_OUTPUT_PATH}
471 DEPENDS ${_TARGET_NAME}_copy
472 )
473
474 if (DEFINED _generate_map_file_COPY_TO)
475 # Copy the generated map back to the COPY_TO
476 add_custom_target(${_TARGET_NAME}_copy ALL
477 COMMAND
478 ${CMAKE_COMMAND} -E copy_if_different ${_MAP_OUTPUT_PATH}
479 ${_generate_map_file_COPY_TO}
480 DEPENDS ${_TARGET_NAME}_int
481 COMMENT "Copying ${_MAP_OUTPUT_PATH} to ${_generate_map_file_COPY_TO}"
482 VERBATIM
483 )
484 else()
485 add_custom_target(${_TARGET_NAME}_copy ALL
486 DEPENDS ${_TARGET_NAME}_int
487 )
488 endif()
489 endfunction()
490
491 endif (ABIMAP_FOUND)