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

Commit 7343ef8

Browse files
committed
Added feature to search for library-specific sources under a directory.
It allows to find libraries that have their sources directly under the root directory (As opposed to built-in libraries that have theirs' under 'src') using the `find_arduino_library` function.
1 parent b8e05c2 commit 7343ef8

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

cmake/Platform/Sources/SourceSeeker.cmake

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ function(_find_sources _base_path _pattern _return_var)
2525

2626
endfunction()
2727

28+
#=============================================================================#
29+
# Finds header files matching the pre-defined header-file pattern under the given path.
30+
# This functions searchs explicitly for header-files such as '*.h'.
31+
# Search could also be recursive (With sub-directories) if the optional 'RECURSE' option is passed.
32+
# _base_path - Top-Directory path to search source files in.
33+
# _return_var - Name of variable in parent-scope holding the return value.
34+
# Returns - List of header files in the given path
35+
#=============================================================================#
36+
function(find_header_files _base_path _return_var)
37+
38+
_find_sources("${_base_path}" "${ARDUINO_CMAKE_HEADER_FILES_PATTERN}" headers ${ARGN})
39+
set(${_return_var} "${headers}" PARENT_SCOPE)
40+
41+
endfunction()
42+
2843
#=============================================================================#
2944
# Finds source files matching the pre-defined source-file pattern under the given path.
3045
# This functions searchs explicitly for source-files such as '*.c'.
@@ -41,20 +56,35 @@ function(find_source_files _base_path _return_var)
4156
endfunction()
4257

4358
#=============================================================================#
44-
# Finds header files matching the pre-defined header-file pattern under the given path.
45-
# This functions searchs explicitly for header-files such as '*.h'.
46-
# Search could also be recursive (With sub-directories) if the optional 'RECURSE' option is passed.
59+
# Recursively finds header files under the given path, excluding those that don't belong to a library,
60+
# such as files under the 'exmaples' directory (In case sources reside under lib's root directory).
4761
# _base_path - Top-Directory path to search source files in.
4862
# _return_var - Name of variable in parent-scope holding the return value.
49-
# Returns - List of header files in the given path
63+
# Returns - List of source files in the given path
5064
#=============================================================================#
51-
function(find_header_files _base_path _return_var)
65+
function(find_library_header_files _base_path _return_var)
5266

53-
_find_sources("${_base_path}" "${ARDUINO_CMAKE_HEADER_FILES_PATTERN}" headers ${ARGN})
67+
find_header_files(${_base_path} headers RECURSE) # Library headers are always searched recursively
68+
list(FILTER headers EXCLUDE REGEX "${ARDUINO_CMAKE_EXCLUDED_LIBRARY_SOURCES_PATTERN}")
5469
set(${_return_var} "${headers}" PARENT_SCOPE)
5570

5671
endfunction()
5772

73+
#=============================================================================#
74+
# Recursively finds source files under the given path, excluding those that don't belong to a library,
75+
# such as files under the 'exmaples' directory (In case sources reside under lib's root directory).
76+
# _base_path - Top-Directory path to search source files in.
77+
# _return_var - Name of variable in parent-scope holding the return value.
78+
# Returns - List of source files in the given path
79+
#=============================================================================#
80+
function(find_library_source_files _base_path _return_var)
81+
82+
find_source_files(${_base_path} sources RECURSE) # Library sources are always searched recursively
83+
list(FILTER sources EXCLUDE REGEX "${ARDUINO_CMAKE_EXCLUDED_LIBRARY_SOURCES_PATTERN}")
84+
set(${_return_var} "${sources}" PARENT_SCOPE)
85+
86+
endfunction()
87+
5888
#=============================================================================#
5989
# Finds sketch files matching the pre-defined sketch-file pattern under the given path.
6090
# This functions searchs explicitly for sketch-files such as '*.ino'.

cmake/Platform/System/DefaultsManager.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ function(set_source_files_patterns)
3030
"Header Files Pattern")
3131
set(ARDUINO_CMAKE_SKETCH_FILES_PATTERN *.ino *.pde CACHE STRING
3232
"Sketch Files Pattern")
33+
set(ARDUINO_CMAKE_EXCLUDED_LIBRARY_SOURCES_PATTERN "examples?/.+" CACHE STRING
34+
"Regex pattern matching all sources that should be excluded from search
35+
when dealing with libraries")
3336

3437
endfunction()
3538

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,14 @@ function(find_arduino_library _target_name _library_name _board_id)
124124
endif ()
125125
endif ()
126126

127-
find_header_files("${library_path}/src" library_headers RECURSE)
128-
127+
find_library_header_files("${library_path}" library_headers)
129128
if (NOT library_headers)
130-
set(error_message
131-
"${_library_name} doesn't have any header files under the 'src' directory")
129+
set(error_message "Couldn't find any header files for the ${_library_name} library")
132130
message(SEND_ERROR "${error_message}")
133131
else ()
134-
find_source_files("${library_path}/src" library_sources RECURSE)
135-
132+
find_library_source_files("${library_path}" library_sources)
136133
if (NOT library_sources)
137-
set(error_message
138-
"${_library_name} doesn't have any source files \
139-
under the 'src' directory")
134+
set(error_message "Couldn't find any source files for the ${_library_name} library")
140135
message(SEND_ERROR "${error_message}")
141136
else ()
142137
set(sources ${library_headers} ${library_sources})

0 commit comments

Comments
 (0)