aboutsummaryrefslogtreecommitdiff
path: root/cmake/Modules/ExtractSymbols.cmake
blob: d37778e188d2e3f02f4ebdf7c29ad6430af346cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#
#  Copyright (c) 2018 Anderson Toshiyuki Sasaki <ansasaki@redhat.com>
#
#  Redistribution and use is allowed according to the terms of the New
#  BSD license.
#  For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#

#.rst:
# ExtractSymbols
# --------------
#
# This is a helper script for FindABImap.cmake.
#
# Extract symbols from header files and output a list to a file.
# This script is run in build time to extract symbols from the provided header
# files. This way, symbols added or removed can be checked and used to update
# the symbol version script.
#
# All symbols followed by the character ``'('`` are extracted. If a
# ``FILTER_PATTERN`` is provided, only the lines containing the given string are
# considered.
#
# Expected defined variables
# --------------------------
#
# ``HEADERS_LIST_FILE``:
#   Required, expects a file containing the list of header files to be parsed.
#
# ``OUTPUT_PATH``:
#   Required, expects the output file path.
#
# Optionally defined variables
# ----------------------------
#
# ``FILTER_PATTERN``:
#   Expects a string. Only lines containing the given string will be considered
#   when extracting symbols.
#

if (NOT DEFINED OUTPUT_PATH)
    message(SEND_ERROR "OUTPUT_PATH not defined")
endif()

if (NOT DEFINED HEADERS_LIST_FILE)
    message(SEND_ERROR "HEADERS not defined")
endif()

file(READ ${HEADERS_LIST_FILE} HEADERS_LIST)

set(symbols)
foreach(header ${HEADERS_LIST})

    # Filter only lines containing the FILTER_PATTERN
    file(STRINGS ${header} contain_filter
      REGEX "^.*${FILTER_PATTERN}.*[(]"
    )

    # Remove function-like macros
    foreach(line ${contain_filter})
        if (NOT ${line} MATCHES ".*#[ ]*define")
            list(APPEND not_macro ${line})
        endif()
    endforeach()

    set(functions)

    # Get only the function names followed by '('
    foreach(line ${not_macro})
        string(REGEX MATCHALL "[a-zA-Z0-9_]+[ ]*[(]" func ${line})
        list(APPEND functions ${func})
    endforeach()

    set(extracted_symbols)

    # Remove '('
    foreach(line ${functions})
        string(REGEX REPLACE "[(]" "" symbol ${line})
        string(STRIP "${symbol}" symbol)
        list(APPEND extracted_symbols ${symbol})
    endforeach()

    list(APPEND symbols ${extracted_symbols})
endforeach()

list(REMOVE_DUPLICATES symbols)

file(WRITE ${OUTPUT_PATH} "${symbols}")