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

Commit b0df5ab

Browse files
committed
Refactored the entire library architecture processing logic.
It also came with a cost of requiring the library's root directory in the `add_library` API.
1 parent 7d3a876 commit b0df5ab

15 files changed

+177
-106
lines changed

cmake/Platform/Arduino.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ include(TargetFlagsManager)
2121
include(SourcesManager)
2222
include(SketchManager)
2323
include(DefaultsManager)
24-
include(ArchitectureValidator)
24+
include(SupportedArchitecturesRetriever)
2525

2626
include(Libraries)
2727

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
include(LibraryArchitectureParser)
2+
include(LibrarySourcesArchitectureResolver)
23
include(LibraryFlagsManager)
34
include(LibrariesFinder)

cmake/Platform/Libraries/LibrariesFinder.cmake

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ function(find_arduino_library _target_name _library_name _board_id)
2828
message(SEND_ERROR "Couldn't find library named ${_library_name}")
2929
else () # Library is found
3030
get_filename_component(library_path ${library_properties_file} DIRECTORY)
31-
get_library_architecture("${library_properties_file}" lib_arch)
32-
if (lib_arch)
33-
if ("${lib_arch}" MATCHES "UNSUPPORTED")
34-
string(CONCAT error_message
35-
"${_library_name} "
36-
"library isn't supported on the platform's architecture "
37-
"${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE}")
38-
message(SEND_ERROR ${error_message})
39-
endif ()
40-
endif ()
4131

4232
find_library_header_files("${library_path}" library_headers)
4333
if (NOT library_headers)
@@ -46,7 +36,7 @@ function(find_arduino_library _target_name _library_name _board_id)
4636
else ()
4737
if (parsed_args_HEADER_ONLY)
4838
add_arduino_header_only_library(${_target_name} ${_board_id}
49-
ARCH ${lib_arch}
39+
ARCH ${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE}
5040
HEADERS ${library_headers})
5141
else ()
5242
find_library_source_files("${library_path}" library_sources)
@@ -58,8 +48,8 @@ function(find_arduino_library _target_name _library_name _board_id)
5848
message(SEND_ERROR "${error_message}")
5949
else ()
6050
set(sources ${library_headers} ${library_sources})
61-
add_arduino_library(${_target_name} ${_board_id} "${sources}"
62-
ARCH ${lib_arch})
51+
add_arduino_library(${_target_name} ${_board_id} ${library_path} "${sources}"
52+
LIB_PROPS_FILE ${library_properties_file})
6353
endif ()
6454
endif ()
6555
endif ()
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1+
function(get_arduino_library_supported_architectures _library_properties_file _return_var)
2+
3+
file(STRINGS ${_library_properties_file} library_properties)
4+
5+
list(FILTER library_properties INCLUDE REGEX "arch")
6+
_get_property_value("${library_properties}" _library_arch_list)
7+
string(REPLACE "," ";" _library_arch_list ${_library_arch_list}) # Turn into a valid list
8+
9+
set(${_return_var} ${_library_arch_list} PARENT_SCOPE)
10+
11+
endfunction()
12+
113
#=============================================================================#
214
# Gets the library architecure if any, read from the given library properties file
315
# which includes this information.
4-
# _library_properties_file - Full path to a library's properties file.
16+
# _library_arch_list - List of architectures supported by the library,
17+
# inferred from its' 'library.properties' file.
518
# _return_var - Name of variable in parent-scope holding the return value.
6-
# Returns - If library is architecure neutral, nothing is returned.
19+
# Returns - If library is architecure agnostic (Supports all), nothing is returned.
720
# If library doesn't support the current platform's architecture,
821
# "UNSUPPORTED" string is returned.
922
# Otherwise, the platform's architecture is returned.
1023
#=============================================================================#
11-
function(get_library_architecture _library_properties_file _return_var)
12-
13-
file(STRINGS ${_library_properties_file} library_properties)
14-
15-
list(FILTER library_properties INCLUDE REGEX "arch")
16-
_get_property_value("${library_properties}" arch_list)
17-
string(REPLACE "," ";" arch_list ${arch_list}) # Turn into a valid list
24+
function(is_library_supports_platform_architecture _library_arch_list _return_var)
1825

19-
if ("${arch_list}" MATCHES "\\*")
20-
return() # Any architecture is supported, return nothing
26+
if ("${_library_arch_list}" MATCHES "\\*") # Any architecture is supported
27+
set(result TRUE)
2128
else ()
22-
list(LENGTH arch_list num_of_supported_archs)
23-
if (${num_of_supported_archs} GREATER 1) # Number of architectures is supported
24-
list(FIND arch_list ${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE} platform_arch_index)
25-
if (${platform_arch_index} LESS 0) # Our arch isn't supported
26-
set(__arch "UNSUPPORTED")
27-
else () # Our arch is indeed supported
28-
set(__arch ${arch_list})
29-
endif ()
29+
if (${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE} IN_LIST _library_arch_list)
30+
set(result TRUE) # Our platform's arch is supported
3031
else ()
31-
list(GET arch_list 0 __arch)
32-
if (NOT "${__arch}" STREQUAL "${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE}")
33-
set(__arch "UNSUPPORTED") # Our arch isn't supported
34-
endif ()
32+
set(result FALSE) # Our arch isn't supported
3533
endif ()
3634
endif ()
3735

38-
set(${_return_var} ${__arch} PARENT_SCOPE)
36+
set(${_return_var} ${result} PARENT_SCOPE)
3937

4038
endfunction()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function(_filter_unsupported_arch_sources _unsupported_archs_regex _sources _return_var)
2+
3+
#string(LENGTH _unsupported_archs_regex num_of_unsupported_archs)
4+
5+
if (NOT "${_unsupported_archs_regex}" STREQUAL "") # Not all architectures are supported
6+
# Filter sources dependant on unsupported architectures
7+
list(FILTER _sources EXCLUDE REGEX ${_unsupported_archs_regex})
8+
endif ()
9+
10+
set(${_return_var} ${_sources} PARENT_SCOPE)
11+
12+
endfunction()
13+
14+
function(resolve_library_sources_by_architecture _library_root_dir _library_sources _return_var)
15+
16+
cmake_parse_arguments(parsed_args "" "LIB_PROPS_FILE" "" ${ARGN})
17+
18+
if (parsed_args_LIB_PROPS_FILE) # Library properties file is given
19+
set(lib_props_file ${parsed_args_LIB_PROPS_FILE})
20+
else () # Try to automatically find file from sources
21+
22+
# Get the absolute root directory (full path)
23+
get_filename_component(absolute_lib_root_dir ${_library_root_dir} ABSOLUTE)
24+
25+
if (EXISTS ${absolute_lib_root_dir}/library.properties)
26+
set(lib_props_file ${absolute_lib_root_dir}/library.properties)
27+
28+
else () # Properties file can't be found - Warn user and assume library is arch-agnostic
29+
30+
get_filename_component(library_name ${absolute_lib_root_dir} NAME)
31+
message(WARNING "\"${library_name}\" library's properties file can't be found "
32+
"under its' root directory - Assuming the library "
33+
"is architecture-agnostic (supports all architectures)")
34+
set(${_return_var} ${_library_sources} PARENT_SCOPE)
35+
return()
36+
37+
endif ()
38+
39+
endif ()
40+
41+
get_arduino_library_supported_architectures("${lib_props_file}" lib_archs)
42+
is_library_supports_platform_architecture(${lib_archs} arch_supported_by_lib)
43+
44+
if (NOT ${arch_supported_by_lib})
45+
string(CONCAT error_message
46+
"The ${_library_name} "
47+
"library isn't supported on the platform's architecture "
48+
"${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE}")
49+
message(SEND_ERROR ${error_message})
50+
endif ()
51+
52+
get_unsupported_architectures("${lib_archs}" unsupported_archs REGEX)
53+
54+
# Filter any sources that aren't supported by the platform's architecture
55+
_filter_unsupported_arch_sources("${unsupported_archs}" "${_library_sources}" valid_sources)
56+
57+
set(${_return_var} "${valid_sources}" PARENT_SCOPE)
58+
59+
endfunction()

cmake/Platform/Other/ArchitectureValidator.cmake

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#=============================================================================#
2+
# Gets a filtered list of architectures that aren't compliant with the platform's architecture.
3+
# e.g If a list contains 'avr' and 'nrf52', while our arch is 'avr', 'nrf52' will be returned.
4+
# _arch_list - List of all architectures probably read from a library's properties file
5+
# [REGEX] - Returns list in a regex-compatible mode, allowing caller to
6+
# use result in search patterns. This is currently the only supported mode.
7+
# _return_var - Name of variable in parent-scope holding the return value.
8+
# Returns - Filtered list of architectures.
9+
#=============================================================================#
10+
function(get_unsupported_architectures _arch_list _return_var)
11+
12+
cmake_parse_arguments(parsed_args "REGEX" "" "" ${ARGN})
13+
14+
if ("${_arch_list}" MATCHES "\\*") # All architectures are supported, return nothing
15+
return()
16+
endif ()
17+
18+
list(FILTER _arch_list EXCLUDE REGEX ${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE})
19+
set(unsupported_arch_list ${_arch_list}) # Just for better readability
20+
21+
if (parsed_args_REGEX) # Return in regex format
22+
23+
foreach (arch ${unsupported_arch_list})
24+
# Append every unsupported-architecture and "|" to represent "or" in regex-fomart
25+
string(APPEND unsupported_archs_regex "${arch}" "|")
26+
endforeach ()
27+
28+
# Remove last "|" as it's unnecessary - There's no element after it
29+
string(LENGTH ${unsupported_archs_regex} str_len)
30+
decrement_integer(str_len 1) # Decrement string's length by 1 to trim last char ('|')
31+
string(SUBSTRING ${unsupported_archs_regex} 0 ${str_len} unsupported_archs_regex)
32+
33+
# prepare for generalized function return
34+
set(unsupported_arch_list ${unsupported_archs_regex})
35+
36+
endif ()
37+
38+
set(${_return_var} ${unsupported_arch_list} PARENT_SCOPE)
39+
40+
endfunction()

cmake/Platform/Targets/ArduinoCMakeLibraryTarget.cmake

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,7 @@
1010
#=============================================================================#
1111
function(_add_arduino_cmake_library _target_name _board_id _sources)
1212

13-
cmake_parse_arguments(parsed_args "INTERFACE" "ARCH" "" ${ARGN})
14-
15-
if (parsed_args_ARCH) # Treat architecture-specific libraries differently
16-
# Filter any sources that aren't supported by the platform's architecture
17-
list(LENGTH library_ARCH num_of_libs_archs)
18-
if (${num_of_libs_archs} GREATER 1)
19-
# Exclude all unsupported architectures, request filter in regex mode
20-
get_unsupported_architectures("${parsed_args_ARCH}" arch_filter REGEX)
21-
set(filter_type EXCLUDE)
22-
else ()
23-
set(arch_filter "src\\/[^/]+\\.|${parsed_args_ARCH}")
24-
set(filter_type INCLUDE)
25-
endif ()
26-
list(FILTER _sources ${filter_type} REGEX ${arch_filter})
27-
endif ()
13+
cmake_parse_arguments(parsed_args "INTERFACE" "" "" ${ARGN})
2814

2915
if (parsed_args_INTERFACE)
3016
add_library(${_target_name} INTERFACE)
@@ -40,11 +26,9 @@ function(_add_arduino_cmake_library _target_name _board_id _sources)
4026

4127
set_library_flags(${_target_name} ${_board_id} ${scope})
4228

43-
if (parsed_args_ARCH)
44-
string(TOUPPER ${parsed_args_ARCH} upper_arch)
45-
set(arch_definition "ARDUINO_ARCH_${upper_arch}")
46-
target_compile_definitions(${_target_name} ${scope} ${arch_definition})
47-
endif ()
29+
string(TOUPPER ${ARDUINO_CMAKE_PLATFORM_ARCHITECTURE} upper_arch)
30+
set(arch_definition "ARDUINO_ARCH_${upper_arch}")
31+
target_compile_definitions(${_target_name} ${scope} ${arch_definition})
4832

4933
endfunction()
5034

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
# _target_name - Name of the library target to be created. Usually library's real name.
55
# _board_id - Board ID associated with the linked Core Lib.
66
# _sources - Source and header files to create target from.
7+
# [LIB_PROPS_FILE] - Full path to the library's properties file. Optional.
78
#=============================================================================#
8-
function(add_arduino_library _target_name _board_id _sources)
9+
function(add_arduino_library _target_name _board_id _library_root_dir _sources)
10+
11+
resolve_library_sources_by_architecture(${_library_root_dir} "${_sources}" arch_resolved_sources
12+
"${ARGN}")
13+
14+
_add_arduino_cmake_library(${_target_name} ${_board_id} "${arch_resolved_sources}" "${ARGN}")
15+
16+
find_dependent_platform_libraries("${arch_resolved_sources}" lib_platform_libs)
917

10-
_add_arduino_cmake_library(${_target_name} ${_board_id} "${_sources}" "${ARGN}")
11-
find_dependent_platform_libraries("${_sources}" lib_platform_libs)
1218
foreach (platform_lib ${lib_platform_libs})
1319
link_platform_library(${_target_name} ${platform_lib} ${_board_id})
1420
endforeach ()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <Arduino.h>
2+
#include "NeoPixelTest.hpp"
3+
#include "GFXTest.h"
4+
5+
void setup()
6+
{
7+
testNeoPixel();
8+
testGFX();
9+
}
10+
11+
void loop()
12+
{
13+
14+
}

0 commit comments

Comments
 (0)