|
| 1 | +#=============================================================================# |
| 2 | +# Retrieves all headers used by a sketch, which is much like extracting the headers included |
| 3 | +# by a source file. Headers are returned by their name, with extension (such as '.h'). |
| 4 | +# _sketch_file - Path to a sketch file to add to the target. |
| 5 | +# _return_var - Name of variable in parent-scope holding the return value. |
| 6 | +# Returns - List of headers names with extension that are included by the given sketch file. |
| 7 | +#=============================================================================# |
| 8 | +function(_get_sketch_headers _sketch_file _return_var) |
| 9 | + |
| 10 | + file(STRINGS "${_sketch_file}" sketch_loc) # Loc = Lines of code |
| 11 | + list(FILTER sketch_loc INCLUDE REGEX ${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN}) |
| 12 | + |
| 13 | + # Extract header names from inclusion |
| 14 | + foreach (loc ${sketch_loc}) |
| 15 | + string(REGEX MATCH ${ARDUINO_CMAKE_HEADER_NAME_REGEX_PATTERN} ${loc} match) |
| 16 | + list(APPEND headers ${CMAKE_MATCH_1}) |
| 17 | + endforeach () |
| 18 | + |
| 19 | + set(${_return_var} ${headers} PARENT_SCOPE) |
| 20 | + |
| 21 | +endfunction() |
| 22 | + |
| 23 | +#=============================================================================# |
| 24 | +# Validates a header file is included by the given target. |
| 25 | +# i.e The header is located under one of the target's include directories. |
| 26 | +# _target_name - Name of the target to add the sketch file to. |
| 27 | +# _return_var - Name of variable in parent-scope holding the return value. |
| 28 | +# Returns - True if header is included by the target, false otherwise. |
| 29 | +#=============================================================================# |
| 30 | +function(_validate_target_includes_header _target_name _header _return_var) |
| 31 | + |
| 32 | + get_target_property(target_include_dirs ${_target_name} INCLUDE_DIRECTORIES) |
| 33 | + foreach (include_dir ${target_include_dirs}) |
| 34 | + find_header_files("${include_dir}" include_dir_headers) |
| 35 | + if (${_header} IN_LIST include_dir_headers) # Header is included in the target |
| 36 | + set(${_return_var} True) |
| 37 | + return() |
| 38 | + endif () |
| 39 | + endforeach () |
| 40 | + |
| 41 | + set(${_return_var} False) |
| 42 | + |
| 43 | +endfunction() |
| 44 | + |
| 45 | +#=============================================================================# |
| 46 | +# Resolves the header files included in a sketch by linking their appropriate library if necessary |
| 47 | +# or by validating they're included by the sketch target. |
| 48 | +# _target_name - Name of the target to add the sketch file to. |
| 49 | +# _board_id - ID of the board to bind to the target (Each target can have a single board). |
| 50 | +# _sketch_file - Path to a sketch file to add to the target. |
| 51 | +#=============================================================================# |
| 52 | +function(resolve_sketch_headers _target_name _board_id _sketch_file) |
| 53 | + |
| 54 | + _get_sketch_headers("${_sketch_file}" sketch_headers) |
| 55 | + foreach (header ${sketch_headers}) |
| 56 | + # Header name without extension (such as '.h') can represent an Arduino/Platform library |
| 57 | + # So first we should check whether it's a library |
| 58 | + string(REGEX MATCH "(.+)\\." "${header}" header_we_match) |
| 59 | + set(header_we ${CMAKE_MATCH_1}) |
| 60 | + |
| 61 | + if (${header_we} IN_LIST ARDUINO_CMAKE_PLATFORM_LIBRARIES) |
| 62 | + link_platform_library(${_target_name} ${header_we} ${_board_id}) |
| 63 | + else () |
| 64 | + find_arduino_library(${header_we}_sketch_lib ${header_we} ${_board_id}) |
| 65 | + # If library isn't found, display a wraning since it might be a user library |
| 66 | + if (NOT ${header_we}_sketch_lib OR "${${header_we}_sketch_lib}" MATCHES "NOTFOUND") |
| 67 | + _validate_target_includes_header(${_target_name} ${header} is_header_validated) |
| 68 | + if (NOT is_header_validated) |
| 69 | + # Header hasn't been found in any of the target's include directories, Display warning |
| 70 | + message(WARNING "The header '${_header}' is used by the \ |
| 71 | + '${_sketch_file}' sketch \ |
| 72 | + but it isn't a Arduino/Platform library, nor it's linked \ |
| 73 | + to the target manually!") |
| 74 | + endif () |
| 75 | + else () |
| 76 | + link_arduino_library(${_target_name} ${header_we}_sketch_lib ${_board_id}) |
| 77 | + endif () |
| 78 | + endif () |
| 79 | + endforeach () |
| 80 | + |
| 81 | +endfunction() |
0 commit comments