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

Commit d6ce2f7

Browse files
committed
Added support for platform libraries using the find API
1 parent b93aef6 commit d6ce2f7

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

cmake/Platform/Libraries/LibrariesFinder.cmake

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ function(find_arduino_library _target_name _library_name)
2727

2828
find_file(library_path
2929
NAMES ${_library_name}
30-
PATHS ${ARDUINO_SDK_LIBRARIES_PATH} ${ARDUINO_CMAKE_SKETCHBOOK_PATH}
31-
${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}
30+
PATHS ${ARDUINO_CMAKE_PLATFORM_LIBRARIES_PATH} ${ARDUINO_SDK_LIBRARIES_PATH}
31+
${ARDUINO_CMAKE_SKETCHBOOK_PATH} ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}
3232
PATH_SUFFIXES libraries dependencies
3333
NO_DEFAULT_PATH
3434
NO_CMAKE_FIND_ROOT_PATH)
@@ -37,7 +37,6 @@ function(find_arduino_library _target_name _library_name)
3737
message(SEND_ERROR "Couldn't find library named ${_library_name}")
3838

3939
else () # Library is found
40-
4140
find_library_header_files("${library_path}" library_headers)
4241

4342
if (NOT library_headers)
@@ -50,12 +49,10 @@ function(find_arduino_library _target_name _library_name)
5049
endif ()
5150

5251
else ()
53-
5452
if (parsed_args_HEADER_ONLY)
5553
add_arduino_header_only_library(${_target_name} ${library_headers})
5654

5755
else ()
58-
5956
find_library_source_files("${library_path}" library_sources)
6057

6158
if (NOT library_sources)
@@ -68,19 +65,20 @@ function(find_arduino_library _target_name _library_name)
6865
"If so, please pass the HEADER_ONLY option "
6966
"as an argument to the function")
7067
endif ()
71-
7268
else ()
73-
7469
set(sources ${library_headers} ${library_sources})
7570

76-
add_arduino_library(${_target_name} ${sources})
71+
is_platform_library(${_library_name} is_plib)
72+
if (is_plib)
73+
add_arduino_library(${_target_name} PLATFORM ${sources})
74+
else ()
75+
add_arduino_library(${_target_name} ${sources})
76+
endif ()
7777

7878
endif ()
7979

8080
endif ()
81-
8281
endif ()
83-
8482
endif ()
8583

8684
_cleanup_find_arduino_library()

cmake/Platform/System/PlatformElementsManager.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,20 @@ function(find_platform_libraries)
118118
NO_CMAKE_FIND_ROOT_PATH)
119119

120120
file(GLOB sub-dir "${ARDUINO_CMAKE_PLATFORM_LIBRARIES_PATH}/*")
121-
set(platform_lib_list)
121+
122122
foreach (dir ${sub-dir})
123123
if (IS_DIRECTORY ${dir})
124124
get_filename_component(platform_lib ${dir} NAME)
125125
string(TOLOWER ${platform_lib} platform_lib)
126+
126127
set(ARDUINO_CMAKE_LIBRARY_${platform_lib}_PATH ${dir} CACHE INTERNAL
127128
"Path to ${platform_lib} platform library")
129+
128130
list(APPEND platform_lib_list ${platform_lib})
129131
endif ()
130132
endforeach ()
131133

132-
set(ARDUINO_CMAKE_PLATFORM_LIBRARIES "${platform_lib_list}" CACHE STRING
133-
"List of existing platform libraries")
134+
set(ARDUINO_CMAKE_PLATFORM_LIBRARIES "${platform_lib_list}" CACHE STRING "List of existing platform libraries")
134135

135136
endfunction()
136137

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
# Creates a library target for the given name and sources.
33
# As it's an Arduino library, it also finds and links all dependent platform libraries (if any).
44
# _target_name - Name of the library target to be created. Usually library's real name.
5+
# [PLATFORM] -
56
# [Sources] - List of source files (Could also be headers for code-inspection in some IDEs)
67
# to create the executable from, similar to CMake's built-in add_executable.
78
#=============================================================================#
89
function(add_arduino_library _target_name)
910

10-
parse_sources_arguments(parsed_sources "" "" "" "${ARGN}")
11+
# First parse keyword arguments
12+
cmake_parse_arguments(parsed_args "PLATFORM" "" "" ${ARGN})
13+
# Then parse unlimited sources
14+
parse_sources_arguments(parsed_sources "PLATFORM" "" "" "${ARGN}")
1115

1216
if (parsed_sources)
1317

@@ -23,9 +27,13 @@ function(add_arduino_library _target_name)
2327

2428
_add_arduino_cmake_library(${_target_name} "${arch_resolved_sources}")
2529

26-
else() # No sources have been provided at this stage, simply create a library target
30+
else () # No sources have been provided at this stage, simply create a library target
2731
_add_arduino_cmake_library(${_target_name} "")
28-
endif()
32+
endif ()
33+
34+
if (parsed_args_PLATFORM)
35+
return()
36+
endif ()
2937

3038
find_dependent_platform_libraries("${arch_resolved_sources}" lib_platform_libs)
3139

examples/platform-library/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ arduino_cmake_project(Platform_Library BOARD_NAME nano BOARD_CPU atmega328)
66

77
add_arduino_executable(Platform_Library platformLibrary.cpp)
88

9-
link_platform_library(Platform_Library Wire)
10-
link_platform_library(Platform_Library SPI)
9+
find_arduino_library(wire Wire)
10+
link_arduino_library(Platform_Library wire)
11+
12+
find_arduino_library(spi SPI)
13+
link_arduino_library(Platform_Library spi)

examples/platform-library/platformLibrary.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <Arduino.h>
2+
#include <Wire.h>
3+
#include <SPI.h>
24

35
void setup()
46
{
57
pinMode(LED_BUILTIN, OUTPUT);
8+
SPIClass::setDataMode(0);
69
}
710

811
void loop()

0 commit comments

Comments
 (0)