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

Commit 463f035

Browse files
committed
Improved header resolving when converting sketches to sources
1 parent 74f2456 commit 463f035

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

cmake/Platform/Sketches/SketchHeadersManager.cmake

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,54 @@
1+
function(_check_header_existance _header_we _dir_list _return_var)
2+
3+
foreach (include_dir ${_dir_list})
4+
find_header_files("${include_dir}" include_dir_headers RECURSE)
5+
6+
foreach (included_header ${include_dir_headers})
7+
get_name_without_file_extension(${included_header} included_header_we)
8+
if ("${included_header_we}" STREQUAL "${_header_we}")
9+
set(_return_var TRUE PARENT_SCOPE)
10+
return()
11+
endif ()
12+
endforeach ()
13+
14+
endforeach ()
15+
16+
set(_return_var FALSE PARENT_SCOPE)
17+
18+
endfunction()
19+
20+
function(_is_known_header _header_we _target_name _return_var)
21+
22+
# Get target's direct include dirs
23+
get_target_property(target_include_dirs ${_target_name} INCLUDE_DIRECTORIES)
24+
25+
# Get include dirs of targets linked to the given target
26+
get_target_property(target_linked_libs ${_target_name} LINK_LIBRARIES)
27+
28+
# Explictly add include dirs of all linked libraries (given they're valid cmake targets)
29+
foreach (linked_lib ${target_linked_libs})
30+
31+
if (NOT TARGET ${linked_lib})
32+
continue()
33+
endif ()
34+
35+
get_target_property(lib_include_dirs ${linked_lib} INCLUDE_DIRECTORIES)
36+
if (NOT "${lib_include_dirs}" MATCHES "NOTFOUND") # Library has include dirs
37+
list(APPEND include_dirs ${lib_include_dirs})
38+
endif ()
39+
40+
endforeach ()
41+
42+
if (NOT "${target_include_dirs}" MATCHES "NOTFOUND") # Target has direct include dirs
43+
list(APPEND include_dirs ${target_include_dirs})
44+
endif ()
45+
46+
_check_header_existance(${_header_we} ${include_dirs} header_found)
47+
48+
set(_return_var ${header_found} PARENT_SCOPE)
49+
50+
endfunction()
51+
152
#=============================================================================#
253
# Resolves the header files included in a sketch by linking their appropriate library if necessary
354
# or by validating they're included by the sketch target.
@@ -14,32 +65,21 @@ function(resolve_sketch_headers _target_name _sketch_file)
1465
# So first we should check whether it's a library
1566
get_name_without_file_extension("${header}" header_we)
1667

17-
is_platform_library(${header_we} is_header_platform_lib)
68+
# Pass the '3RD_PARTY' option to avoid name-conversion
69+
find_arduino_library(${header_we}_sketch_lib ${header_we} 3RD_PARTY QUIET)
1870

19-
if (is_header_platform_lib)
71+
if (NOT TARGET ${header_we}_sketch_lib OR "${${header_we}_sketch_lib}" MATCHES "NOTFOUND")
2072

21-
string(TOLOWER ${header_we} header_we_lower)
73+
# The header name doesn't conform to a name of a known library, search individual headers instead
74+
_is_known_header(${header_we} ${_target_name} known_header)
2275

23-
link_platform_library(${_target_name} ${header_we_lower})
24-
25-
else ()
26-
27-
# Pass the '3RD_PARTY' option to avoid name-conversion
28-
find_arduino_library(${header_we}_sketch_lib ${header_we} 3RD_PARTY QUIET)
29-
30-
# If library isn't found, display a status since it might be a user library
31-
if (NOT TARGET ${header_we}_sketch_lib OR
32-
"${${header_we}_sketch_lib}" MATCHES "NOTFOUND")
33-
34-
message(STATUS "The header '${header_we}' is used by the '${_sketch_file}' sketch, "
35-
"but it isn't part of an Arduino nor a Platform library.\n\t"
36-
"However, it may be part of a user library but "
37-
"you'd have to check this manually!")
38-
39-
else ()
40-
link_arduino_library(${_target_name} ${header_we}_sketch_lib)
76+
if (NOT ${known_header})
77+
message(STATUS "The '${header_we}' header used by the '${_sketch_file}' sketch can't be resolved. "
78+
"It's probably a user-header which location is unknown to the framework.")
4179
endif ()
4280

81+
else ()
82+
link_arduino_library(${_target_name} ${header_we}_sketch_lib)
4383
endif ()
4484

4585
endforeach ()

cmake/Platform/System/DefaultsManager.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ function(set_internal_search_patterns)
55

66
set(ARDUINO_CMAKE_SEMICOLON_REPLACEMENT "!@&#%" CACHE STRING
77
"String replacement for the semicolon char, required when treating lists as code")
8+
89
set(ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN "^#include.*[<\"]" CACHE STRING
910
"Regex pattern matching header inclusion in a source file")
11+
1012
set(ARDUINO_CMAKE_HEADER_NAME_REGEX_PATTERN
1113
"${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN}(.+)[>\"]$" CACHE STRING
1214
"Regex pattern matching a header's name when wrapped in inclusion line")
15+
1316
set(ARDUINO_CMAKE_HEADER_FILE_EXTENSION_REGEX_PATTERN ".+\\.h.*$" CACHE STRING
1417
"Regex pattern matching all header file extensions")
15-
set(ARDUINO_CMAKE_NAME_WE_REGEX_PATTERN "(.+)\\." CACHE STRING
18+
19+
set(ARDUINO_CMAKE_NAME_WE_REGEX_PATTERN "([^\\/]+)\\." CACHE STRING
1620
"Regex pattern matching name without file extension")
21+
1722
set(ARDUINO_CMAKE_FUNCTION_REGEX_PATTERN "^([a-z]|[A-Z])+.*\(([a-z]|[A-Z])*\)" CACHE STRING
1823
"Regex pattern matching a function signature in a source file")
1924

cmake/Platform/Utilities/PlatformLibraryUtils.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
function(is_platform_library _name _return_var)
88

99
string(TOLOWER "${_name}" name_lower)
10+
1011
if ("${name_lower}" IN_LIST ARDUINO_CMAKE_PLATFORM_LIBRARIES)
1112
set(lib_found TRUE)
1213
else ()

examples/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ cmake_minimum_required(VERSION 3.8.2)
22

33
project(Examples LANGUAGES C CXX ASM)
44

5-
add_subdirectory(hello-world)
5+
#[[add_subdirectory(hello-world)
66
add_subdirectory(arduino-library)
77
add_subdirectory(platform-library)
88
add_subdirectory(3rd-party-library)
99
add_subdirectory(header-only-library)
1010
add_subdirectory(blink-example)
1111
add_subdirectory(servo-knob-example)
1212
add_subdirectory(sketch)
13-
add_subdirectory(misc)
13+
add_subdirectory(misc)]]
14+
15+
add_subdirectory(sketch)

0 commit comments

Comments
 (0)