| 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 |
# ExtractSymbols |
| 11 |
# -------------- |
| 12 |
# |
| 13 |
# This is a helper script for FindABImap.cmake. |
| 14 |
# |
| 15 |
# Extract symbols from header files and output a list to a file. |
| 16 |
# This script is run in build time to extract symbols from the provided header |
| 17 |
# files. This way, symbols added or removed can be checked and used to update |
| 18 |
# the symbol version script. |
| 19 |
# |
| 20 |
# All symbols followed by the character ``'('`` are extracted. If a |
| 21 |
# ``FILTER_PATTERN`` is provided, only the lines containing the given string are |
| 22 |
# considered. |
| 23 |
# |
| 24 |
# Expected defined variables |
| 25 |
# -------------------------- |
| 26 |
# |
| 27 |
# ``HEADERS_LIST_FILE``: |
| 28 |
# Required, expects a file containing the list of header files to be parsed. |
| 29 |
# |
| 30 |
# ``OUTPUT_PATH``: |
| 31 |
# Required, expects the output file path. |
| 32 |
# |
| 33 |
# Optionally defined variables |
| 34 |
# ---------------------------- |
| 35 |
# |
| 36 |
# ``FILTER_PATTERN``: |
| 37 |
# Expects a string. Only lines containing the given string will be considered |
| 38 |
# when extracting symbols. |
| 39 |
# |
| 40 |
|
| 41 |
if (NOT DEFINED OUTPUT_PATH) |
| 42 |
message(SEND_ERROR "OUTPUT_PATH not defined") |
| 43 |
endif() |
| 44 |
|
| 45 |
if (NOT DEFINED HEADERS_LIST_FILE) |
| 46 |
message(SEND_ERROR "HEADERS not defined") |
| 47 |
endif() |
| 48 |
|
| 49 |
file(READ ${HEADERS_LIST_FILE} HEADERS_LIST) |
| 50 |
|
| 51 |
set(symbols) |
| 52 |
foreach(header ${HEADERS_LIST}) |
| 53 |
file(READ ${header} header_content) |
| 54 |
|
| 55 |
# Filter only lines containing the FILTER_PATTERN |
| 56 |
# separated from the function name with one optional newline |
| 57 |
string(REGEX MATCHALL |
| 58 |
"${FILTER_PATTERN}[^(\n]*\n?[^(\n]*[(]" |
| 59 |
contain_filter |
| 60 |
"${header_content}" |
| 61 |
) |
| 62 |
|
| 63 |
# Remove the optional newline now |
| 64 |
string(REGEX REPLACE |
| 65 |
"(.+)\n?(.*)" |
| 66 |
"\\1\\2" |
| 67 |
oneline |
| 68 |
"${contain_filter}" |
| 69 |
) |
| 70 |
|
| 71 |
# Remove function-like macros |
| 72 |
# and anything with two underscores that sounds suspicious |
| 73 |
foreach(line ${oneline}) |
| 74 |
if (NOT ${line} MATCHES ".*(#[ ]*define|__)") |
| 75 |
list(APPEND not_macro ${line}) |
| 76 |
endif() |
| 77 |
endforeach() |
| 78 |
|
| 79 |
set(functions) |
| 80 |
|
| 81 |
# Get only the function names followed by '(' |
| 82 |
foreach(line ${not_macro}) |
| 83 |
string(REGEX MATCHALL "[a-zA-Z0-9_]+[ ]*[(]" func ${line}) |
| 84 |
list(APPEND functions ${func}) |
| 85 |
endforeach() |
| 86 |
|
| 87 |
set(extracted_symbols) |
| 88 |
|
| 89 |
# Remove '(' |
| 90 |
foreach(line ${functions}) |
| 91 |
string(REGEX REPLACE "[(]" "" symbol ${line}) |
| 92 |
string(STRIP "${symbol}" symbol) |
| 93 |
list(APPEND extracted_symbols ${symbol}) |
| 94 |
endforeach() |
| 95 |
|
| 96 |
list(APPEND symbols ${extracted_symbols}) |
| 97 |
endforeach() |
| 98 |
|
| 99 |
list(REMOVE_DUPLICATES symbols) |
| 100 |
|
| 101 |
list(SORT symbols) |
| 102 |
|
| 103 |
string(REPLACE ";" "\n" symbols_list "${symbols}") |
| 104 |
|
| 105 |
file(WRITE ${OUTPUT_PATH} "${symbols_list}") |