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

Commit 982e8b9

Browse files
committed
Finish sketch-libs
2 parents 90a0a0d + 1c4b035 commit 982e8b9

File tree

10 files changed

+195
-65
lines changed

10 files changed

+195
-65
lines changed

cmake/Platform/Arduino.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Utilities)
44
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/System)
55
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Other)
66
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Targets)
7+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/Sketches)
78

89
include(MathUtils)
910
include(ListUtils)
@@ -25,6 +26,5 @@ include(ArduinoCMakeLibraryTarget)
2526
include(ArduinoLibraryTarget)
2627
include(PlatformLibraryTarget)
2728
include(ArduinoExampleTarget)
28-
include(SketchTarget)
2929

3030
initialize_build_system()

cmake/Platform/Other/SourcesManager.cmake

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,6 @@ function(find_sketch_files _base_path _return_var)
7373

7474
endfunction()
7575

76-
#=============================================================================#
77-
# Sets a pre-defined source and header file patterns to use when searching for sources.
78-
#=============================================================================#
79-
function(set_source_files_pattern)
80-
81-
set(ARDUINO_CMAKE_SOURCE_FILES_PATTERN *.c *.cc *.cpp *.cxx *.[Ss] CACHE STRING
82-
"Source Files Pattern")
83-
set(ARDUINO_CMAKE_HEADER_FILES_PATTERN *.h *.hh *.hpp *.hxx CACHE STRING
84-
"Header Files Pattern")
85-
set(ARDUINO_CMAKE_SKETCH_FILES_PATTERN *.ino *.pde CACHE STRING
86-
"Sketch Files Pattern")
87-
88-
endfunction()
89-
9076
#=============================================================================#
9177
# Gets all '#include' lines of the given source file.
9278
# _source_file - Source file to get its' includes.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#=============================================================================#
2+
# Retrieves all headers used by a sketch, which is much like extracting the headers included
3+
# by a source file. Headers are returned by their name, with extension (such as '.h').
4+
# _sketch_file - Path to a sketch file to add to the target.
5+
# _return_var - Name of variable in parent-scope holding the return value.
6+
# Returns - List of headers names with extension that are included by the given sketch file.
7+
#=============================================================================#
8+
function(_get_sketch_headers _sketch_file _return_var)
9+
10+
file(STRINGS "${_sketch_file}" sketch_loc) # Loc = Lines of code
11+
list(FILTER sketch_loc INCLUDE REGEX ${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN})
12+
13+
# Extract header names from inclusion
14+
foreach (loc ${sketch_loc})
15+
string(REGEX MATCH ${ARDUINO_CMAKE_HEADER_NAME_REGEX_PATTERN} ${loc} match)
16+
list(APPEND headers ${CMAKE_MATCH_1})
17+
endforeach ()
18+
19+
set(${_return_var} ${headers} PARENT_SCOPE)
20+
21+
endfunction()
22+
23+
#=============================================================================#
24+
# Validates a header file is included by the given target.
25+
# i.e The header is located under one of the target's include directories.
26+
# _target_name - Name of the target to add the sketch file to.
27+
# _return_var - Name of variable in parent-scope holding the return value.
28+
# Returns - True if header is included by the target, false otherwise.
29+
#=============================================================================#
30+
function(_validate_target_includes_header _target_name _header _return_var)
31+
32+
get_target_property(target_include_dirs ${_target_name} INCLUDE_DIRECTORIES)
33+
foreach (include_dir ${target_include_dirs})
34+
find_header_files("${include_dir}" include_dir_headers)
35+
if (${_header} IN_LIST include_dir_headers) # Header is included in the target
36+
set(${_return_var} True)
37+
return()
38+
endif ()
39+
endforeach ()
40+
41+
set(${_return_var} False)
42+
43+
endfunction()
44+
45+
#=============================================================================#
46+
# Resolves the header files included in a sketch by linking their appropriate library if necessary
47+
# or by validating they're included by the sketch target.
48+
# _target_name - Name of the target to add the sketch file to.
49+
# _board_id - ID of the board to bind to the target (Each target can have a single board).
50+
# _sketch_file - Path to a sketch file to add to the target.
51+
#=============================================================================#
52+
function(resolve_sketch_headers _target_name _board_id _sketch_file)
53+
54+
_get_sketch_headers("${_sketch_file}" sketch_headers)
55+
foreach (header ${sketch_headers})
56+
# Header name without extension (such as '.h') can represent an Arduino/Platform library
57+
# So first we should check whether it's a library
58+
string(REGEX MATCH "(.+)\\." "${header}" header_we_match)
59+
set(header_we ${CMAKE_MATCH_1})
60+
61+
if (${header_we} IN_LIST ARDUINO_CMAKE_PLATFORM_LIBRARIES)
62+
link_platform_library(${_target_name} ${header_we} ${_board_id})
63+
else ()
64+
find_arduino_library(${header_we}_sketch_lib ${header_we} ${_board_id})
65+
# If library isn't found, display a wraning since it might be a user library
66+
if (NOT ${header_we}_sketch_lib OR "${${header_we}_sketch_lib}" MATCHES "NOTFOUND")
67+
_validate_target_includes_header(${_target_name} ${header} is_header_validated)
68+
if (NOT is_header_validated)
69+
# Header hasn't been found in any of the target's include directories, Display warning
70+
message(WARNING "The header '${_header}' is used by the \
71+
'${_sketch_file}' sketch \
72+
but it isn't a Arduino/Platform library, nor it's linked \
73+
to the target manually!")
74+
endif ()
75+
else ()
76+
link_arduino_library(${_target_name} ${header_we}_sketch_lib ${_board_id})
77+
endif ()
78+
endif ()
79+
endforeach ()
80+
81+
endfunction()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
include(SketchSourceConverter)
2+
include(SketchHeadersManager)
3+
4+
#=============================================================================#
5+
# Returns a desired path for sources converted from sketches.
6+
# It can't be resolved just by a cache variable since sketches may belong each to a different project,
7+
# thus having different path to be returned.
8+
# _sketch_file - Path to a sketch file to find the desired path to its converted source.
9+
# _return_var - Name of variable in parent-scope holding the return value.
10+
# Returns - Desired path for the source file converted from the given sketch
11+
#=============================================================================#
12+
function(_get_converted_source_desired_path _sketch_file _return_var)
13+
14+
get_filename_component(sketch_file_name "${_sketch_file}" NAME_WE)
15+
set(desired_source_path "${CMAKE_CURRENT_SOURCE_DIR}/${sketch_file_name}.cpp")
16+
set(${_return_var} ${desired_source_path} PARENT_SCOPE)
17+
18+
endfunction()
19+
20+
#=============================================================================#
21+
# Adds the sketch file to the given target with the given board ID.
22+
# Each sketch is converted to a valid '.cpp' source file under the project's source directory.
23+
# The function also finds and links any libraries the sketch uses to the target.
24+
# _target_name - Name of the target to add the sketch file to.
25+
# _board_id - ID of the board to bind to the target (Each target can have a single board).
26+
# _sketch_file - Path to a sketch file to add to the target.
27+
#=============================================================================#
28+
function(add_sketch_to_target _target_name _board_id _sketch_file)
29+
30+
resolve_sketch_headers(${_target_name} ${_board_id} "${_sketch_file}")
31+
_get_converted_source_desired_path("${_sketch_file}" sketch_converted_source_path)
32+
33+
# Only perform conversion if policy is set or if sketch hasn't been converted yet
34+
if (CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS OR NOT EXISTS "${sketch_file}")
35+
convert_sketch_to_source("${_sketch_file}" "${sketch_converted_source_path}")
36+
endif ()
37+
38+
target_sources(${_target_name} PRIVATE "${sketch_converted_source_path}")
39+
40+
endfunction()
41+
42+
#=============================================================================#
43+
# Adds a list of sketch files as converted sources to the given target.
44+
# _target_name - Name of the target to add the sketch file to.
45+
# _board_id - ID of the board to bind to the target (Each target can have a single board).
46+
# _sketch_files - List of paths to sketch files to add to the target.
47+
#=============================================================================#
48+
function(target_sketches _target_name _board_id _sketch_files)
49+
50+
foreach (sketch_file ${_sketch_files})
51+
add_sketch_to_target(${_target_name} ${_board_id} "${sketch_file}")
52+
endforeach ()
53+
54+
endfunction()

cmake/Platform/Other/SketchManager.cmake renamed to cmake/Platform/Sketches/SketchSourceConverter.cmake

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ endfunction()
5454
# During the conversion process the platform's main header file is inserted to the source file
5555
# since it's critical for it to include it - Something that doesn't happen in "Standard" sketches.
5656
# _sketch_file - Full path to the original sketch file (Read from).
57-
# _target_file - Full path to the converted target source file (Written to).
57+
# _converted_source_path - Full path to the converted target source file (Written to).
5858
#=============================================================================#
59-
function(convert_sketch_to_source_file _sketch_file _target_file)
59+
function(convert_sketch_to_source _sketch_file _converted_source_path)
6060

6161
file(STRINGS "${_sketch_file}" sketch_loc)
6262
list(LENGTH sketch_loc num_of_loc)
6363
decrement_integer(num_of_loc 1)
6464

6565
set(refined_sketch)
66-
set(header_insert_pattern "#include|^([a-z]|[A-Z])+.*\(([a-z]|[A-Z])*\)")
66+
set(header_insert_pattern
67+
"${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN}|${ARDUINO_CMAKE_FUNCTION_REGEX_PATTERN}")
6768
set(header_inserted FALSE)
6869

6970
foreach (loc_index RANGE 0 ${num_of_loc})
@@ -90,29 +91,6 @@ function(convert_sketch_to_source_file _sketch_file _target_file)
9091
endif ()
9192
endforeach ()
9293

93-
_write_source_file("${refined_sketch}" "${target_source_path}")
94-
95-
endfunction()
96-
97-
#=============================================================================#
98-
# Converts all the given sketch file into valid 'cpp' source files and returns their paths.
99-
# _sketch_files - List of paths to original sketch files.
100-
# _return_var - Name of variable in parent-scope holding the return value.
101-
# Returns - List of paths representing post-conversion sources.
102-
#=============================================================================#
103-
function(get_sources_from_sketches _sketch_files _return_var)
104-
105-
set(sources)
106-
foreach (sketch ${_sketch_files})
107-
get_filename_component(sketch_file_name "${sketch}" NAME_WE)
108-
set(target_source_path "${CMAKE_CURRENT_SOURCE_DIR}/${sketch_file_name}.cpp")
109-
# Only convert sketch if it hasn't been converted yet
110-
if (NOT EXISTS "${target_source_path}")
111-
convert_sketch_to_source_file("${sketch}" "${target_source_path}")
112-
endif ()
113-
list(APPEND sources "${target_source_path}")
114-
endforeach ()
115-
116-
set(${_return_var} ${sources} PARENT_SCOPE)
94+
_write_source_file("${refined_sketch}" "${_converted_source_path}")
11795

11896
endfunction()

cmake/Platform/System/BuildSystemInitializer.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ endfunction()
1717
#=============================================================================#
1818
function(initialize_build_system)
1919

20-
set_source_files_pattern()
2120
set_arduino_cmake_defaults()
2221
find_required_platform_tools()
2322
detect_sdk_version()
Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
#=============================================================================#
2-
# Sets various defaults related directly to the Arduino-CMake platform.
2+
# Sets search patterns used internaly by the platform for searching purposes.
33
#=============================================================================#
4-
function(set_arduino_cmake_defaults)
4+
function(set_internal_search_patterns)
5+
6+
set(ARDUINO_CMAKE_SEMICOLON_REPLACEMENT "!@&#%" CACHE STRING
7+
"String replacement for the semicolon char, required when treating lists as code")
8+
set(ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN "^#include[<\"]" CACHE STRING
9+
"Regex pattern matching header inclusion in a source file")
10+
set(ARDUINO_CMAKE_FUNCTION_REGEX_PATTERN "^([a-z]|[A-Z])+.*\(([a-z]|[A-Z])*\)" CACHE STRING
11+
"Regex pattern matching a function signature in a source file")
12+
set(ARDUINO_CMAKE_HEADER_NAME_REGEX_PATTERN
13+
"${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN}(.+)[>\"]$" CACHE STRING
14+
"Regex pattern matching a header's name when wrapped in inclusion line")
15+
16+
endfunction()
17+
18+
#=============================================================================#
19+
# Sets globb patterns for various types of source files, used mostly for searching purposes.
20+
#=============================================================================#
21+
function(set_source_files_patterns)
22+
23+
set(ARDUINO_CMAKE_SOURCE_FILES_PATTERN *.c *.cc *.cpp *.cxx *.[Ss] CACHE STRING
24+
"Source Files Pattern")
25+
set(ARDUINO_CMAKE_HEADER_FILES_PATTERN *.h *.hh *.hpp *.hxx CACHE STRING
26+
"Header Files Pattern")
27+
set(ARDUINO_CMAKE_SKETCH_FILES_PATTERN *.ino *.pde CACHE STRING
28+
"Sketch Files Pattern")
29+
30+
endfunction()
31+
32+
#=============================================================================#
33+
# Sets various options specific for the Arduino-CMake platform.
34+
#=============================================================================#
35+
function(set_default_arduino_cmake_options)
536

637
option(USE_DEFAULT_PLATFORM_IF_NONE_EXISTING
738
"Whether to use Arduino as default platform if none is supplied" ON)
@@ -10,7 +41,19 @@ function(set_arduino_cmake_defaults)
1041
skipping the selection algorithm" OFF)
1142
option(USE_ARCHLINUX_BUILTIN_SUPPORT
1243
"Whether to use Arduino CMake's built-in support for the archlinux distribution" ON)
13-
set(ARDUINO_CMAKE_SEMICOLON_REPLACEMENT "!@&#%" CACHE STRING
14-
"String replacement for the semicolon char, required when treating lists as code")
44+
option(CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS
45+
"Whether to convert sketches to source files even if converted sources already exist"
46+
OFF)
47+
48+
endfunction()
49+
50+
#=============================================================================#
51+
# Sets various defaults used throughout the platform.
52+
#=============================================================================#
53+
function(set_arduino_cmake_defaults)
54+
55+
set_internal_search_patterns()
56+
set_source_files_patterns()
57+
set_default_arduino_cmake_options()
1558

1659
endfunction()

cmake/Platform/Targets/ArduinoExampleTarget.cmake

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ function(add_arduino_example _target_name _board_id _example_name)
1313

1414
find_arduino_example_sources("${ARDUINO_SDK_EXAMPLES_PATH}"
1515
${_example_name} example_sketches ${ARGN})
16-
get_sources_from_sketches("${example_sketches}" example_sources)
17-
add_arduino_executable(${_target_name} ${_board_id} ${example_sources})
16+
# First create the target (Without sources), then add sketches as converted sources
17+
add_arduino_executable(${_target_name} ${_board_id} "")
18+
target_sketches(${_target_name} ${_board_id} "${example_sketches}")
1819

1920
endfunction()
2021

@@ -40,8 +41,8 @@ function(add_arduino_library_example _target_name _library_target_name _library_
4041

4142
find_arduino_library_example_sources("${ARDUINO_SDK_LIBRARIES_PATH}/${_library_name}"
4243
${_example_name} example_sketches ${ARGN})
43-
get_sources_from_sketches("${example_sketches}" example_sources)
44-
add_arduino_executable(${_target_name} ${_board_id} ${example_sources})
44+
add_arduino_executable(${_target_name} ${_board_id} "")
45+
target_sketches(${_target_name} ${_board_id} "${example_sketches}")
4546
link_arduino_library(${_target_name} ${_library_target_name} ${_board_id})
4647

4748
endfunction()

cmake/Platform/Targets/SketchTarget.cmake

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/sketch/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ cmake_minimum_required(VERSION 3.8)
33
project(Sketch LANGUAGES C CXX ASM)
44
get_board_id(board_id nano atmega328)
55

6-
add_sketch_target(Sketch ${board_id} sketch.ino)
6+
add_arduino_executable(Sketch ${board_id} "")
7+
target_sketches(Sketch ${board_id} sketch.ino)

0 commit comments

Comments
 (0)