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

Commit 0720c5d

Browse files
committed
Added utility function to get common root directory of sources.
The `add_arduino_library` function has been updated to use this function in order to remove the inconvenient 'library_root_dir' argument it used to accept. As such, updated all other code using this API.
1 parent 1ddf71f commit 0720c5d

File tree

6 files changed

+63
-10
lines changed

6 files changed

+63
-10
lines changed

cmake/Platform/Arduino.cmake

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Sources)
99
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Libraries)
1010
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Targets)
1111

12-
include(MathUtils)
13-
include(ListUtils)
14-
include(StringUtils)
15-
include(PropertyUtils)
16-
include(PlatformLibraryUtils)
17-
include(CMakeArgumentsUtils)
12+
include(Utilities)
1813

1914
include(BoardManager)
2015
include(RecipeParser)

cmake/Platform/Libraries/LibrariesFinder.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function(find_arduino_library _target_name _library_name _board_id)
4848
else ()
4949
set(sources ${library_headers} ${library_sources})
5050

51-
add_arduino_library(${_target_name} ${_board_id} ${library_path} "${sources}"
51+
add_arduino_library(${_target_name} ${_board_id} ${sources}
5252
LIB_PROPS_FILE ${library_properties_file})
5353
endif ()
5454

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function(add_arduino_library _target_name _board_id)
1111
cmake_parse_arguments(parsed_args "" "LIB_PROPS_FILE" "" ${ARGN})
1212
parse_sources_arguments(parsed_sources "" "LIB_PROPS_FILE" "" "${ARGN}")
1313

14-
resolve_library_architecture(${_library_root_dir} "${_sources}" arch_resolved_sources "${ARGN}")
14+
get_sources_root_directory("${parsed_sources}" library_root_dir)
1515

1616
if (parsed_args_LIB_PROPS_FILE)
1717
resolve_library_architecture(${library_root_dir} "${parsed_sources}" arch_resolved_sources
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#=============================================================================#
2+
# Finds the shallowest path among the given sources, where shallowest is the path having
3+
# the least nesting level, i.e. The least number of '/' separators in its' path.
4+
# _sources - List of sources paths to find shallowest path from.
5+
# _return_var - Name of variable in parent-scope holding the return value.
6+
# Returns - Shallowest path among given sources (Lowest nesting level).
7+
#=============================================================================#
8+
function(get_shallowest_directory_structure_path _sources _return_var)
9+
10+
set(min_nesting_level 9999)
11+
12+
foreach (source ${_sources})
13+
14+
string(REGEX MATCHALL "/" nesting_regex_match ${source})
15+
16+
list(LENGTH nesting_regex_match source_nesting_level)
17+
18+
if (${source_nesting_level} LESS ${min_nesting_level})
19+
set(min_nested_path ${source})
20+
set(min_nesting_level ${source_nesting_level})
21+
endif ()
22+
23+
endforeach ()
24+
25+
set(${_return_var} ${min_nested_path} PARENT_SCOPE)
26+
27+
endfunction()
28+
29+
#=============================================================================#
30+
# Gets the path of the common root directory of all given sources.
31+
# It is expected that indeed all sources will have the same, common root directory.
32+
# E.g. src/foo.h and src/utility/bar.h both have 'src' in common.
33+
# However, if src/foo.c is a relative path under the C:\ drive (in Windows), and src/bar.c is
34+
# a relative path under the D:\ drive - This is invalid and the function will misbehave.
35+
# _sources - List of sources that have a common root directory which needs to be found.
36+
# _return_var - Name of variable in parent-scope holding the return value.
37+
# Returns - Path to the common root directory of the given list of sources.
38+
#=============================================================================#
39+
function(get_sources_root_directory _sources _return_var)
40+
41+
get_shallowest_directory_structure_path("${_sources}" shallowest_path)
42+
43+
get_filename_component(root_dir ${shallowest_path} DIRECTORY)
44+
45+
if ("${root_dir}" MATCHES ".+src$") # 'src' directory has been retrieved as shallowest path
46+
# The actual root directory is one level above 'src'
47+
get_filename_component(root_dir ${root_dir} DIRECTORY)
48+
endif ()
49+
50+
set(${_return_var} ${root_dir} PARENT_SCOPE)
51+
52+
endfunction()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include(MathUtils)
2+
include(ListUtils)
3+
include(StringUtils)
4+
include(PathUtils)
5+
include(PropertyUtils)
6+
include(PlatformLibraryUtils)
7+
include(CMakeArgumentsUtils)

examples/3rd-party-library/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ add_arduino_executable(3rd_Party_Arduino_Library ${board_id} 3rd_party.cpp
99
target_include_directories(3rd_Party_Arduino_Library PRIVATE include)
1010

1111
# Add the "NeoPixel" library manually using the library addition API
12-
add_arduino_library(Adafruit_NeoPixel ${board_id} libraries/Adafruit_NeoPixel
13-
libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp)
12+
add_arduino_library(Adafruit_NeoPixel ${board_id} libraries/Adafruit_NeoPixel/Adafruit_NeoPixel.cpp)
1413
target_include_directories(Adafruit_NeoPixel PUBLIC libraries/Adafruit_NeoPixel)
1514

1615
# Find the "GFX" library by 'tricking' the framework to use current directory as the Sketchbook path,

0 commit comments

Comments
 (0)