Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 719e69c

Browse files
committed
Refactored files structure, added header-retrieving API
1 parent be6055f commit 719e69c

12 files changed

+189
-135
lines changed

cmake/Platform/Arduino.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ include(Boards)
1717

1818
include(RecipeParser)
1919
include(TargetFlagsManager)
20-
include(SourcesManager)
2120

21+
include(Sources)
2222
include(Sketches)
2323

2424
include(DefaultsManager)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function(generate_function_prototype _function_line _source_lines _declaration_line_index)
2+
3+
4+
endfunction()

cmake/Platform/Sketches/SketchHeadersManager.cmake

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#=============================================================================#
2+
# Resolves the header files included in a sketch by linking their appropriate library if necessary
3+
# or by validating they're included by the sketch target.
4+
# _target_name - Name of the target to add the sketch file to.
5+
# _sketch_file - Path to a sketch file to add to the target.
6+
# _return_var - Name of variable in parent-scope holding the return value.
7+
# Returns - List of all unique header files used by the sketch file, recursively.
8+
#=============================================================================#
9+
function(resolve_sketch_headers _target_name _sketch_file _return_var)
10+
11+
_get_source_included_headers("${_sketch_file}" sketch_headers)
12+
13+
foreach (header ${sketch_headers})
14+
15+
# Header name without extension (such as '.h') can represent an Arduino/Platform library
16+
# So first we should check whether it's a library
17+
get_name_without_file_extension("${header}" header_we)
18+
19+
is_header_discoverable_by_target(${header_we} ${_target_name} known_header)
20+
21+
if (NOT ${known_header})
22+
message(STATUS "The '${header_we}' header used by the '${_sketch_file}' sketch can't be resolved. "
23+
"It's probably a user-header which location is unknown to the framework.")
24+
endif ()
25+
26+
endforeach ()
27+
28+
endfunction()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#=============================================================================#
2+
# Resolves the header files included in a sketch by linking their appropriate library if necessary
3+
# or by validating they're included by the sketch target.
4+
# _target_name - Name of the target to add the sketch file to.
5+
# _sketch_file - Path to a sketch file to add to the target.
6+
#=============================================================================#
7+
function(resolve_sketch_libraries _target_name _sketch_file)
8+
9+
_get_source_included_headers("${_sketch_file}" sketch_headers)
10+
11+
foreach (header ${sketch_headers})
12+
13+
# Header name without extension (such as '.h') can represent an Arduino/Platform library
14+
# So first we should check whether it's a library
15+
get_name_without_file_extension("${header}" header_we)
16+
17+
# Pass the '3RD_PARTY' option to avoid name-conversion
18+
find_arduino_library(${header_we}_sketch_lib ${header_we} 3RD_PARTY QUIET)
19+
20+
if (TARGET ${header_we}_sketch_lib)
21+
link_arduino_library(${_target_name} ${header_we}_sketch_lib)
22+
endif ()
23+
24+
endforeach ()
25+
26+
endfunction()

cmake/Platform/Sketches/SketchManager.cmake

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ function(add_sketch_to_target _target_name _sketch_file)
2828
_get_converted_source_desired_path(${_sketch_file} sketch_converted_source_path)
2929

3030
# Only perform conversion if policy is set or if sketch hasn't been converted yet
31-
if (CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS OR
32-
NOT EXISTS ${sketch_converted_source_path})
31+
if (CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS OR NOT EXISTS ${sketch_converted_source_path})
3332

34-
resolve_sketch_headers(${_target_name} ${_sketch_file})
33+
resolve_sketch_libraries(${_target_name} ${_sketch_file})
3534

3635
convert_sketch_to_source(${_sketch_file} ${sketch_converted_source_path})
3736

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
include(SketchHeadersManager)
1+
include(SketchHeadersResolver)
2+
include(SketchLibrariesResolver)
23
include(SketchSourceConverter)
34
include(SketchManager)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
function(_check_header_existance _header_we _dir_list _return_var)
2+
3+
foreach (include_dir ${_dir_list})
4+
5+
find_header_files("${include_dir}" include_dir_headers RECURSE)
6+
7+
foreach (included_header ${include_dir_headers})
8+
get_name_without_file_extension(${included_header} included_header_we)
9+
if ("${included_header_we}" STREQUAL "${_header_we}")
10+
set(_return_var ${included_header} PARENT_SCOPE)
11+
return()
12+
endif ()
13+
endforeach ()
14+
15+
endforeach ()
16+
17+
set(_return_var NOTFOUND PARENT_SCOPE)
18+
19+
endfunction()
20+
21+
function(is_header_discoverable_by_target _header_we _target_name _return_var)
22+
23+
# Get target's direct include dirs
24+
get_target_property(target_include_dirs ${_target_name} INCLUDE_DIRECTORIES)
25+
26+
# Get include dirs of targets linked to the given target
27+
get_target_property(target_linked_libs ${_target_name} LINK_LIBRARIES)
28+
29+
# Explictly add include dirs of all linked libraries (given they're valid cmake targets)
30+
foreach (linked_lib ${target_linked_libs})
31+
32+
if (NOT TARGET ${linked_lib})
33+
continue()
34+
endif ()
35+
36+
get_target_property(lib_include_dirs ${linked_lib} INCLUDE_DIRECTORIES)
37+
if (NOT "${lib_include_dirs}" MATCHES "NOTFOUND") # Library has include dirs
38+
list(APPEND include_dirs ${lib_include_dirs})
39+
endif ()
40+
41+
endforeach ()
42+
43+
if (NOT "${target_include_dirs}" MATCHES "NOTFOUND") # Target has direct include dirs
44+
list(APPEND include_dirs ${target_include_dirs})
45+
endif ()
46+
47+
_check_header_existance(${_header_we} ${include_dirs} header_found)
48+
49+
set(_return_var ${header_found} PARENT_SCOPE)
50+
51+
endfunction()
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include(SourceSeeker)
2+
include(ExampleSourcesSeeker)
3+
include(ArduinoLibrarySourcesSeeker)
4+
include(HeaderExistanceChecker)
5+
include(IncludedHeadersRetriever)

0 commit comments

Comments
 (0)