|
| 1 | +#=============================================================================# |
| 2 | +# Retrieves all headers included by a source file. |
| 3 | +# Headers are returned by their name, with extension (such as '.h'). |
| 4 | +# _source_file - Path to a source file to get its' included headers. |
| 5 | +# [WE] - Return headers without extension, just their names. |
| 6 | +# _return_var - Name of variable in parent-scope holding the return value. |
| 7 | +# Returns - List of headers names with extension that are included by the given source file. |
| 8 | +#=============================================================================# |
| 9 | +function(_get_source_included_headers _source_file _return_var) |
| 10 | + |
| 11 | + cmake_parse_arguments(parsed_args "WE" "" "" ${ARGN}) |
| 12 | + |
| 13 | + file(STRINGS "${_source_file}" source_lines) # Loc = Lines of code |
| 14 | + |
| 15 | + list(FILTER source_lines INCLUDE REGEX ${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN}) |
| 16 | + |
| 17 | + # Extract header names from inclusion |
| 18 | + foreach (loc ${source_lines}) |
| 19 | + |
| 20 | + string(REGEX MATCH ${ARDUINO_CMAKE_HEADER_NAME_REGEX_PATTERN} match ${loc}) |
| 21 | + |
| 22 | + if (parsed_args_WE) |
| 23 | + get_name_without_file_extension("${CMAKE_MATCH_1}" header_name) |
| 24 | + else () |
| 25 | + set(header_name ${CMAKE_MATCH_1}) |
| 26 | + endif () |
| 27 | + |
| 28 | + list(APPEND headers ${header_name}) |
| 29 | + |
| 30 | + endforeach () |
| 31 | + |
| 32 | + set(${_return_var} ${headers} PARENT_SCOPE) |
| 33 | + |
| 34 | +endfunction() |
| 35 | + |
| 36 | +#=============================================================================# |
| 37 | +# Retrieves all headers used by a source file, possibly recursively (Headers used by headers). |
| 38 | +# _source_file - Path to a source file to get its' used headers. |
| 39 | +# [RECURSIVE] - Whether to search for headers recursively. |
| 40 | +# _return_var - Name of variable in parent-scope holding the return value. |
| 41 | +# Returns - List of full paths to the headers that are used by the given source file. |
| 42 | +#=============================================================================# |
| 43 | +function(get_source_headers _source_file _include_dirs _return_var) |
| 44 | + |
| 45 | + cmake_parse_arguments(parsed_args "RECURSIVE" "" "" ${ARGN}) |
| 46 | + |
| 47 | + _get_source_included_headers(${_source_file} included_headers WE) |
| 48 | + |
| 49 | + foreach (header ${included_headers}) |
| 50 | + |
| 51 | + _check_header_existance(${header} ${_include_dirs} header_path) |
| 52 | + if ("${header_path}" MATCHES "NOTFOUND") |
| 53 | + continue() |
| 54 | + endif () |
| 55 | + |
| 56 | + list(APPEND total_included_headers ${header_path}) |
| 57 | + |
| 58 | + if (parsed_args_RECURSIVE) |
| 59 | + _get_header_internal_headers(${header_path} ${_include_dirs} recursive_included_headers) |
| 60 | + endif () |
| 61 | + |
| 62 | + endforeach () |
| 63 | + |
| 64 | + list(REMOVE_DUPLICATES total_included_headers) |
| 65 | + |
| 66 | + set(${_return_var} ${total_included_headers} PARENT_SCOPE) |
| 67 | + |
| 68 | +endfunction() |
0 commit comments