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

Commit ee58a0c

Browse files
committed
Added feature to add all sources inside list of directories to target.
It mostly allows users to append additional directories of a library to their library target, since now libraries only search in the 'src' sub-directory or root directory by default, and this behavior can't be changed. Also separated source-finding function related to libraries to a separate module. Updated '3rd-party-library' example to use new API.
1 parent d8e840e commit ee58a0c

File tree

6 files changed

+115
-45
lines changed

6 files changed

+115
-45
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#=============================================================================#
2+
# Recursively finds header files under the given path, excluding those that don't belong to a library,
3+
# such as files under the 'exmaples' directory (In case sources reside under lib's root directory).
4+
# _base_path - Top-Directory path to search source files in.
5+
# _return_var - Name of variable in parent-scope holding the return value.
6+
# Returns - List of source files in the given path
7+
#=============================================================================#
8+
function(find_library_header_files _base_path _return_var)
9+
10+
if (EXISTS ${_base_path}/src) # 'src' sub-dir exists and should contain sources
11+
# Headers are always searched recursively under the 'src' sub-dir
12+
find_header_files(${_base_path}/src headers RECURSE)
13+
else ()
14+
find_header_files(${_base_path} headers)
15+
endif ()
16+
17+
set(${_return_var} "${headers}" PARENT_SCOPE)
18+
19+
endfunction()
20+
21+
#=============================================================================#
22+
# Recursively finds source files under the given path, excluding those that don't belong to a library,
23+
# such as files under the 'exmaples' directory (In case sources reside under lib's root directory).
24+
# _base_path - Top-Directory path to search source files in.
25+
# _return_var - Name of variable in parent-scope holding the return value.
26+
# Returns - List of source files in the given path
27+
#=============================================================================#
28+
function(find_library_source_files _base_path _return_var)
29+
30+
if (EXISTS ${_base_path}/src)
31+
# Sources are always searched recursively under the 'src' sub-dir
32+
find_source_files(${_base_path}/src sources RECURSE)
33+
else ()
34+
find_source_files(${_base_path} sources)
35+
endif ()
36+
37+
set(${_return_var} "${sources}" PARENT_SCOPE)
38+
39+
endfunction()

cmake/Platform/Sources/SourceSeeker.cmake

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -55,46 +55,6 @@ function(find_source_files _base_path _return_var)
5555

5656
endfunction()
5757

58-
#=============================================================================#
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).
61-
# _base_path - Top-Directory path to search source files in.
62-
# _return_var - Name of variable in parent-scope holding the return value.
63-
# Returns - List of source files in the given path
64-
#=============================================================================#
65-
function(find_library_header_files _base_path _return_var)
66-
67-
if (EXISTS ${_base_path}/src) # 'src' sub-dir exists and should contain sources
68-
# Headers are always searched recursively under the 'src' sub-dir
69-
find_header_files(${_base_path}/src headers RECURSE)
70-
else ()
71-
find_header_files(${_base_path} headers)
72-
endif ()
73-
74-
set(${_return_var} "${headers}" PARENT_SCOPE)
75-
76-
endfunction()
77-
78-
#=============================================================================#
79-
# Recursively finds source files under the given path, excluding those that don't belong to a library,
80-
# such as files under the 'exmaples' directory (In case sources reside under lib's root directory).
81-
# _base_path - Top-Directory path to search source files in.
82-
# _return_var - Name of variable in parent-scope holding the return value.
83-
# Returns - List of source files in the given path
84-
#=============================================================================#
85-
function(find_library_source_files _base_path _return_var)
86-
87-
if (EXISTS ${_base_path}/src)
88-
# Sources are always searched recursively under the 'src' sub-dir
89-
find_source_files(${_base_path}/src sources RECURSE)
90-
else ()
91-
find_source_files(${_base_path} sources)
92-
endif ()
93-
94-
set(${_return_var} "${sources}" PARENT_SCOPE)
95-
96-
endfunction()
97-
9858
#=============================================================================#
9959
# Finds sketch files matching the pre-defined sketch-file pattern under the given path.
10060
# This functions searchs explicitly for sketch-files such as '*.ino'.

cmake/Platform/Sources/SourcesManager.cmake

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,64 @@
11
include(SourceSeeker)
22
include(ExampleSourcesSeeker)
3+
include(ArduinoLibrarySourcesSeeker)
4+
5+
#=============================================================================#
6+
# Appends all sources and headers under the given directory to the givne target.
7+
# This could also be done recursively if the RECURSE option is provided.
8+
# _target_name - Name of the target which sources will be appended to.
9+
# [DIRS] - Indefinite list of directories which its' sources should be appended.
10+
# [RECURSE] - Whether search should be done recursively or not.
11+
# This affects all given directories.
12+
#=============================================================================#
13+
function(target_source_directories _target_name)
14+
15+
cmake_parse_arguments(parsed_args "RECURSE" "" "DIRS" ${ARGN})
16+
17+
if (NOT TARGET ${_target_name})
18+
message(FATAL_ERROR "Can't add sources to the ${_target_name} target as it doesn't exist!")
19+
endif ()
20+
21+
if (NOT parsed_args_DIRS)
22+
message(FATAL_ERROR "Source dirctories must be provided with the DIRS keyword before them!")
23+
endif ()
24+
25+
set(source_dirs ${parsed_args_DIRS})
26+
27+
list(REMOVE_DUPLICATES source_dirs)
28+
29+
if (parsed_args_RECURSE) # Search recursively
30+
foreach (source_dir ${source_dirs})
31+
find_header_files(${source_dir} headers RECURSE)
32+
find_source_files(${source_dir} sources RECURSE)
33+
34+
list(APPEND collective_headers ${headers})
35+
list(APPEND collective_sources ${sources})
36+
endforeach ()
37+
else ()
38+
foreach (source_dir ${source_dirs})
39+
find_header_files(${source_dir} headers)
40+
find_source_files(${source_dir} sources)
41+
42+
list(APPEND collective_headers ${headers})
43+
list(APPEND collective_sources ${sources})
44+
endforeach ()
45+
endif ()
46+
47+
if (collective_headers)
48+
list(REMOVE_DUPLICATES collective_headers)
49+
endif ()
50+
51+
if (collective_sources)
52+
list(REMOVE_DUPLICATES collective_sources)
53+
endif ()
54+
55+
# Treat headers' parent directories as include directories of the target
56+
get_headers_parent_directories("${collective_headers}" include_dirs)
57+
target_include_directories(${_target_name} PUBLIC ${include_dirs})
58+
59+
target_sources(${_target_name} PUBLIC ${collective_sources})
60+
61+
endfunction()
362

463
#=============================================================================#
564
# Gets all '#include' lines of the given source file.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
cmake_minimum_required(VERSION 3.8.2)
22

3-
project(3rd_Party_Arduino_Library LANGUAGES C CXX ASM)
3+
project(3rd_Party_Arduino_Library)
44
get_board_id(board_id nano atmega328)
55

6-
add_arduino_executable(3rd_Party_Arduino_Library ${board_id} test.cpp)
6+
add_arduino_executable(3rd_Party_Arduino_Library ${board_id} testNeoPixel.cpp testGFX.cpp)
77

88
add_arduino_library(Adafruit_NeoPixel ${board_id} libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp)
99
target_include_directories(Adafruit_NeoPixel PUBLIC libraries/Adafruit_NeoPixel)
1010

1111
# 'Trick' the framework to use current directory as Sketchbook, allowing us to use the 'find' API
1212
set(ARDUINO_CMAKE_SKETCHBOOK_PATH "${CMAKE_CURRENT_LIST_DIR}")
1313
find_arduino_library(Adafruit_GFX Adafruit-GFX-Library ${board_id} 3RD_PARTY)
14+
target_source_directories(Adafruit_GFX
15+
DIRS libraries/Adafruit-GFX-Library/Fonts)
1416

1517
link_arduino_library(3rd_Party_Arduino_Library Adafruit_NeoPixel ${board_id})
1618
link_arduino_library(3rd_Party_Arduino_Library Adafruit_GFX ${board_id})

examples/3rd-party-library/test.cpp renamed to examples/3rd-party-library/testGFX.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
#include <Adafruit_NeoPixel.h>
21
#include <Adafruit_GFX.h>
32

4-
Adafruit_NeoPixel neoPixel;
53
Adafruit_GFX_Button gfxButton;
64

75
void setup()
86
{
9-
neoPixel.clear();
107
gfxButton.isPressed();
118
}
129

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <Adafruit_NeoPixel.h>
2+
3+
Adafruit_NeoPixel neoPixel;
4+
5+
void setup()
6+
{
7+
neoPixel.clear();
8+
}
9+
10+
void loop()
11+
{
12+
13+
}

0 commit comments

Comments
 (0)