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

Commit d42826d

Browse files
authored
Merge pull request #20 from arduino-cmake/feature/sketchbook
Added support for Arduino IDE's Sketchbook
2 parents ac8a988 + 0c74536 commit d42826d

File tree

5 files changed

+118
-22
lines changed

5 files changed

+118
-22
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/BuildSystemInitializer.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ include(AvrToolsFinder)
22
include(VersionDetector)
33
include(PlatformInitializer)
44
include(LinuxDistDetector)
5+
include(SketchbookFinder)
56

67
function(find_required_platform_tools)
78

@@ -23,6 +24,9 @@ function(initialize_build_system)
2324
if (CMAKE_HOST_UNIX AND NOT CMAKE_HOST_APPLE) # Detect host's Linux distribution
2425
detect_host_linux_distribution()
2526
endif ()
27+
if (AUTO_SET_SKETCHBOOK_PATH)
28+
find_sketchbook_path()
29+
endif ()
2630

2731
initialize_arduino_platform()
2832

cmake/Platform/System/DefaultsManager.cmake

Lines changed: 12 additions & 3 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

@@ -39,15 +42,21 @@ endfunction()
3942
function(set_default_arduino_cmake_options)
4043

4144
option(USE_DEFAULT_PLATFORM_IF_NONE_EXISTING
42-
"Whether to use Arduino as default platform if none is supplied" ON)
45+
"Whether to use Arduino as default platform if none is supplied"
46+
ON)
4347
option(USE_CUSTOM_PLATFORM_HEADER
4448
"Whether to expect and use a custom-supplied platform header, \
45-
skipping the selection algorithm" OFF)
49+
skipping the selection algorithm"
50+
OFF)
4651
option(USE_ARCHLINUX_BUILTIN_SUPPORT
47-
"Whether to use Arduino CMake's built-in support for the archlinux distribution" ON)
52+
"Whether to use Arduino CMake's built-in support for the archlinux distribution"
53+
ON)
4854
option(CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS
4955
"Whether to convert sketches to source files even if converted sources already exist"
5056
OFF)
57+
option(AUTO_SET_SKETCHBOOK_PATH
58+
"Whether Arduino IDE's Sketchbook Location should be automatically found"
59+
OFF)
5160

5261
endfunction()
5362

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#=============================================================================#
2+
# Gets the path of the user's Arduino IDE preferences file, usually located in a hidden directory
3+
# under the home directory.
4+
# _return_var - Name of variable in parent-scope holding the return value.
5+
# Returns - Filtered list of architectures.
6+
#=============================================================================#
7+
function(_get_user_preferences_file_path _return_var)
8+
9+
set(preferences_file_name preferences.txt)
10+
11+
if (${CMAKE_HOST_UNIX})
12+
if (${CMAKE_HOST_APPLE}) # Mac OS X
13+
set(dir_path "$ENV{HOME}/Library/Processing/${preferences_file_name}")
14+
else () # Linux
15+
set(dir_path "$ENV{HOME}/.processing/${preferences_file_name}")
16+
endif ()
17+
else () # Windows
18+
string(REPLACE "\\" "/" home_path $ENV{HOMEPATH})
19+
string(REPLACE "\\" "/" home_drive $ENV{HOMEDRIVE})
20+
string(CONCAT home_path ${home_drive} ${home_path})
21+
set(dir_path "${home_path}/AppData/Local/arduino15/${preferences_file_name}")
22+
endif ()
23+
24+
set(${_return_var} ${dir_path} PARENT_SCOPE)
25+
26+
endfunction()
27+
28+
#=============================================================================#
29+
# Finds the location of Arduino IDE's Sketchbook directory, where all libraries and sketches
30+
# are downloaded to.
31+
#=============================================================================#
32+
function(find_sketchbook_path)
33+
34+
_get_user_preferences_file_path(arduino_ide_preferences_file)
35+
if (NOT EXISTS "${arduino_ide_preferences_file}")
36+
string(CONCAT error_message
37+
"Arduino IDE preferences file couldn't be found at "
38+
"${arduino_ide_preferences_file}.\n"
39+
"ARDUINO_CMAKE_SKETCHBOOK_PATH should be manually set to the real Sketchbook path")
40+
message(WARNING ${error_message})
41+
return()
42+
endif ()
43+
44+
file(STRINGS "${arduino_ide_preferences_file}" arduino_ide_preferences)
45+
list(FILTER arduino_ide_preferences INCLUDE REGEX "sketchbook")
46+
_get_property_value(${arduino_ide_preferences} sketchbook_path)
47+
48+
set(ARDUINO_CMAKE_SKETCHBOOK_PATH "${sketchbook_path}" CACHE PATH
49+
"Arduino IDE's Sketchbook Path")
50+
51+
endfunction()

cmake/Platform/Targets/ArduinoLibraryTarget.cmake

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,18 @@ function(find_arduino_library _target_name _library_name _board_id)
103103
if (NOT find_lib_3RD_PARTY)
104104
convert_string_to_pascal_case(${_library_name} _library_name)
105105
endif ()
106-
set(library_path "${ARDUINO_SDK_LIBRARIES_PATH}/${_library_name}")
107-
set(library_properties_path "${library_path}/library.properties")
108106

109-
if (NOT EXISTS "${library_properties_path}")
107+
find_file(library_properties_file library.properties
108+
PATHS ${ARDUINO_SDK_LIBRARIES_PATH} ${ARDUINO_CMAKE_SKETCHBOOK_PATH}/libraries
109+
PATH_SUFFIXES ${_library_name}
110+
NO_DEFAULT_PATH
111+
NO_CMAKE_FIND_ROOT_PATH)
112+
113+
if (${library_properties_file} MATCHES "NOTFOUND")
110114
message(SEND_ERROR "Couldn't find library named ${_library_name}")
111115
else () # Library is found
112-
_get_library_architecture("${library_properties_path}" lib_arch)
116+
get_filename_component(library_path ${library_properties_file} DIRECTORY)
117+
_get_library_architecture("${library_properties_file}" lib_arch)
113118
if (lib_arch)
114119
if ("${lib_arch}" MATCHES "UNSUPPORTED")
115120
string(CONCAT error_message
@@ -120,19 +125,14 @@ function(find_arduino_library _target_name _library_name _board_id)
120125
endif ()
121126
endif ()
122127

123-
find_header_files("${library_path}/src" library_headers RECURSE)
124-
128+
find_library_header_files("${library_path}" library_headers)
125129
if (NOT library_headers)
126-
set(error_message
127-
"${_library_name} doesn't have any header files under the 'src' directory")
130+
set(error_message "Couldn't find any header files for the ${_library_name} library")
128131
message(SEND_ERROR "${error_message}")
129132
else ()
130-
find_source_files("${library_path}/src" library_sources RECURSE)
131-
133+
find_library_source_files("${library_path}" library_sources)
132134
if (NOT library_sources)
133-
set(error_message
134-
"${_library_name} doesn't have any source files \
135-
under the 'src' directory")
135+
set(error_message "Couldn't find any source files for the ${_library_name} library")
136136
message(SEND_ERROR "${error_message}")
137137
else ()
138138
set(sources ${library_headers} ${library_sources})
@@ -141,6 +141,8 @@ function(find_arduino_library _target_name _library_name _board_id)
141141
endif ()
142142
endif ()
143143

144+
unset(library_properties_file CACHE)
145+
144146
endfunction()
145147

146148
#=============================================================================#

0 commit comments

Comments
 (0)