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

Commit b49c5a5

Browse files
committed
Fixed bug where only a single sketch was read from list to add to target.
Solution is to use argument-source parsing as used in the 'Executable' API. Also fixed bug where sketch-to-source conversion would fail when the matching index to insert the platform's header include line to was 0, i.e. the first line. At last, removed the 'header-validation' process in case a header included by a sketch doesn't resolve to any known library - It's simply impossible to validate it against the target properties since it's something known only at cmake's generation, not configuration.
1 parent 330dd79 commit b49c5a5

File tree

8 files changed

+104
-51
lines changed

8 files changed

+104
-51
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,6 @@ fabric.properties
120120
[Aa]ssets/*
121121
/examples/blink-example/Blink.cpp
122122
/examples/servo-knob-example/Knob.cpp
123-
/examples/sketch/sketch.cpp
123+
/examples/sketch/sketch1.cpp
124+
/examples/sketch/sketch2.cpp
124125
/docs/wiki
Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
#=============================================================================#
2-
# Validates a header file is included by the given target.
3-
# i.e The header is located under one of the target's include directories.
4-
# _target_name - Name of the target to add the sketch file to.
5-
# _return_var - Name of variable in parent-scope holding the return value.
6-
# Returns - True if header is included by the target, false otherwise.
7-
#=============================================================================#
8-
function(_validate_target_includes_header _target_name _header _return_var)
9-
10-
get_target_property(target_include_dirs ${_target_name} INCLUDE_DIRECTORIES)
11-
foreach (include_dir ${target_include_dirs})
12-
find_header_files("${include_dir}" include_dir_headers)
13-
if (${_header} IN_LIST include_dir_headers) # Header is included in the target
14-
set(${_return_var} True)
15-
return()
16-
endif ()
17-
endforeach ()
18-
19-
set(${_return_var} False)
20-
21-
endfunction()
22-
231
#=============================================================================#
242
# Resolves the header files included in a sketch by linking their appropriate library if necessary
253
# or by validating they're included by the sketch target.
@@ -30,31 +8,41 @@ endfunction()
308
function(resolve_sketch_headers _target_name _board_id _sketch_file)
319

3210
get_source_file_included_headers("${_sketch_file}" sketch_headers)
11+
3312
foreach (header ${sketch_headers})
13+
3414
# Header name without extension (such as '.h') can represent an Arduino/Platform library
3515
# So first we should check whether it's a library
3616
get_name_without_file_extension("${header}" header_we)
3717

3818
is_platform_library(${header_we} is_header_platform_lib)
19+
3920
if (is_header_platform_lib)
21+
4022
string(TOLOWER ${header_we} header_we_lower)
23+
4124
link_platform_library(${_target_name} ${header_we_lower} ${_board_id})
25+
4226
else ()
43-
find_arduino_library(${header_we}_sketch_lib ${header_we} ${_board_id})
44-
# If library isn't found, display a wraning since it might be a user library
45-
if (NOT TARGET ${header_we}_sketch_lib OR "${${header_we}_sketch_lib}" MATCHES "NOTFOUND")
46-
_validate_target_includes_header(${_target_name} ${header} is_header_validated)
47-
if (NOT is_header_validated)
48-
# Header hasn't been found in any of the target's include directories, Display warning
49-
message(WARNING "The header '${header_we}' is used by the \
50-
'${_sketch_file}' sketch \
51-
but it isn't a Arduino/Platform library, nor it's linked \
52-
to the target manually!")
53-
endif ()
27+
28+
# Pass the '3RD_PARTY' option to avoid name-conversion
29+
find_arduino_library(${header_we}_sketch_lib ${header_we} ${_board_id} 3RD_PARTY QUIET)
30+
31+
# If library isn't found, display a status since it might be a user library
32+
if (NOT TARGET ${header_we}_sketch_lib OR
33+
"${${header_we}_sketch_lib}" MATCHES "NOTFOUND")
34+
35+
message(STATUS "The header '${header_we}' is used by the '${_sketch_file}' sketch, "
36+
"but it isn't part of an Arduino nor a Platform library.\n\t"
37+
"However, it may be part of a user library but "
38+
"you'd have to check this manually!")
39+
5440
else ()
5541
link_arduino_library(${_target_name} ${header_we}_sketch_lib ${_board_id})
5642
endif ()
43+
5744
endif ()
45+
5846
endforeach ()
5947

6048
endfunction()

cmake/Platform/Sketches/SketchManager.cmake

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ include(SketchHeadersManager)
1212
function(_get_converted_source_desired_path _sketch_file _return_var)
1313

1414
get_filename_component(sketch_file_name "${_sketch_file}" NAME_WE)
15+
1516
set(desired_source_path "${CMAKE_CURRENT_SOURCE_DIR}/${sketch_file_name}.cpp")
17+
1618
set(${_return_var} ${desired_source_path} PARENT_SCOPE)
1719

1820
endfunction()
@@ -27,29 +29,34 @@ endfunction()
2729
#=============================================================================#
2830
function(add_sketch_to_target _target_name _board_id _sketch_file)
2931

30-
_get_converted_source_desired_path("${_sketch_file}" sketch_converted_source_path)
32+
_get_converted_source_desired_path(${_sketch_file} sketch_converted_source_path)
3133

3234
# Only perform conversion if policy is set or if sketch hasn't been converted yet
3335
if (CONVERT_SKETCHES_IF_CONVERTED_SOURCES_EXISTS OR
34-
NOT EXISTS "${sketch_converted_source_path}")
35-
resolve_sketch_headers(${_target_name} ${_board_id} "${_sketch_file}")
36-
convert_sketch_to_source("${_sketch_file}" "${sketch_converted_source_path}")
36+
NOT EXISTS ${sketch_converted_source_path})
37+
38+
resolve_sketch_headers(${_target_name} ${_board_id} ${_sketch_file})
39+
40+
convert_sketch_to_source(${_sketch_file} ${sketch_converted_source_path})
41+
3742
endif ()
3843

39-
target_sources(${_target_name} PRIVATE "${sketch_converted_source_path}")
44+
target_sources(${_target_name} PRIVATE ${sketch_converted_source_path})
4045

4146
endfunction()
4247

4348
#=============================================================================#
4449
# Adds a list of sketch files as converted sources to the given target.
4550
# _target_name - Name of the target to add the sketch file to.
4651
# _board_id - ID of the board to bind to the target (Each target can have a single board).
47-
# _sketch_files - List of paths to sketch files to add to the target.
52+
# [Sketches] - List of paths to sketch files to add to the target.
4853
#=============================================================================#
49-
function(target_sketches _target_name _board_id _sketch_files)
54+
function(target_sketches _target_name _board_id)
55+
56+
parse_sources_arguments(parsed_sketches "" "" "" "${ARGN}")
5057

51-
foreach (sketch_file ${_sketch_files})
52-
add_sketch_to_target(${_target_name} ${_board_id} "${sketch_file}")
58+
foreach (sketch_file ${parsed_sketches})
59+
add_sketch_to_target(${_target_name} ${_board_id} ${sketch_file})
5360
endforeach ()
5461

5562
endfunction()

cmake/Platform/Sketches/SketchSourceConverter.cmake

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
function(_write_source_file _sketch_loc _file_path)
77

88
file(WRITE "${_file_path}" "") # Clear previous file's contents
9+
910
foreach (loc ${_sketch_loc})
11+
1012
string(REGEX REPLACE "^(.+)${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT}(.*)$" "\\1;\\2"
1113
original_loc "${loc}")
14+
1215
file(APPEND "${_file_path}" "${original_loc}")
16+
1317
endforeach ()
1418

1519
endfunction()
@@ -28,21 +32,30 @@ endfunction()
2832
function(_get_matching_header_insertion_index _sketch_loc _active_index _return_var)
2933

3034
decrement_integer(_active_index 1)
35+
3136
list(GET _sketch_loc ${_active_index} previous_loc)
3237

3338
if ("${previous_loc}" MATCHES "^//") # Simple one-line comment
3439
set(matching_index ${_active_index})
40+
3541
elseif ("${previous_loc}" MATCHES "\\*/$") # End of multi-line comment
42+
3643
foreach (index RANGE ${_active_index} 0)
44+
3745
list(GET _sketch_loc ${index} multi_comment_line)
46+
3847
if ("${multi_comment_line}" MATCHES "^\\/\\*") # Start of multi-line comment
3948
set(matching_index ${index})
4049
break()
4150
endif ()
51+
4252
endforeach ()
53+
4354
else () # Previous line isn't a comment - Return original index
55+
4456
increment_integer(_active_index 1)
4557
set(matching_index ${_active_index})
58+
4659
endif ()
4760

4861
set(${_return_var} ${matching_index} PARENT_SCOPE)
@@ -59,36 +72,57 @@ endfunction()
5972
function(convert_sketch_to_source _sketch_file _converted_source_path)
6073

6174
file(STRINGS "${_sketch_file}" sketch_loc)
75+
6276
list(LENGTH sketch_loc num_of_loc)
6377
decrement_integer(num_of_loc 1)
6478

6579
set(refined_sketch)
80+
set(header_inserted FALSE)
81+
6682
set(header_insert_pattern
6783
"${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN}|${ARDUINO_CMAKE_FUNCTION_REGEX_PATTERN}")
68-
set(header_inserted FALSE)
6984

7085
foreach (loc_index RANGE 0 ${num_of_loc})
86+
7187
list(GET sketch_loc ${loc_index} loc)
88+
7289
if (NOT ${header_inserted})
90+
7391
if ("${loc}" MATCHES "${header_insert_pattern}")
92+
7493
_get_matching_header_insertion_index("${sketch_loc}" ${loc_index} header_index)
75-
if (${header_index} GREATER_EQUAL ${loc_index})
76-
decrement_integer(header_index 1)
77-
set(include_line "\n#include <${ARDUINO_CMAKE_PLATFORM_HEADER_NAME}>")
94+
95+
if (${header_index} LESS ${loc_index})
96+
set(include_line "${ARDUINO_CMAKE_INCLUDE_PLATFORM_HEADER_STRING}\n\n")
7897
else ()
79-
set(include_line "#include <${ARDUINO_CMAKE_PLATFORM_HEADER_NAME}>\n\n")
98+
99+
set(include_line "\n${ARDUINO_CMAKE_INCLUDE_PLATFORM_HEADER_STRING}")
100+
101+
if (${header_index} GREATER 0 AND ${header_index} GREATER_EQUAL ${loc_index})
102+
decrement_integer(header_index 1)
103+
endif ()
104+
80105
endif ()
106+
81107
list(INSERT refined_sketch ${header_index} ${include_line})
108+
82109
set(header_inserted TRUE)
110+
83111
endif ()
112+
84113
endif ()
114+
85115
if ("${loc}" STREQUAL "")
86116
list(APPEND refined_sketch "\n")
87117
else ()
118+
88119
string(REGEX REPLACE "^(.+);(.*)$" "\\1${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT}\\2"
89120
refined_loc "${loc}")
121+
90122
list(APPEND refined_sketch "${refined_loc}\n")
123+
91124
endif ()
125+
92126
endforeach ()
93127

94128
_write_source_file("${refined_sketch}" "${_converted_source_path}")

cmake/Platform/System/DefaultsManager.cmake

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#=============================================================================#
2-
# Sets search patterns used internaly by the platform for searching purposes.
2+
# Sets various patterns used internaly by the framework for all kinds of purposes.
3+
#=============================================================================#
4+
function(set_internal_patterns)
5+
6+
set(ARDUINO_CMAKE_INCLUDE_PLATFORM_HEADER_STRING
7+
"#include <${ARDUINO_CMAKE_PLATFORM_HEADER_NAME}>" CACHE STRING
8+
"Pattern to use in sketch-to-source conversion when inserting platform's header include")
9+
10+
endfunction()
11+
12+
#=============================================================================#
13+
# Sets search patterns used internaly by the framework for searching purposes.
314
#=============================================================================#
415
function(set_internal_search_patterns)
516

@@ -34,7 +45,7 @@ function(set_source_files_patterns)
3445
endfunction()
3546

3647
#=============================================================================#
37-
# Sets various options specific for the Arduino-CMake platform.
48+
# Sets various options specific for the Arduino-CMake framework.
3849
#=============================================================================#
3950
function(set_default_arduino_cmake_options)
4051

@@ -57,6 +68,9 @@ function(set_default_arduino_cmake_options)
5768

5869
endfunction()
5970

71+
#=============================================================================#
72+
# Sets default paths used by the framework
73+
#=============================================================================#
6074
function(set_default_paths)
6175

6276
set(ARDUINO_CMAKE_LIBRARY_PROPERTIES_FILE_NAME "library.properties" CACHE STRING
@@ -69,6 +83,7 @@ endfunction()
6983
#=============================================================================#
7084
function(set_arduino_cmake_defaults)
7185

86+
set_internal_patterns()
7287
set_internal_search_patterns()
7388
set_source_files_patterns()
7489
set_default_arduino_cmake_options()

examples/sketch/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ project(Sketch)
44
get_board_id(board_id nano atmega328)
55

66
add_arduino_executable(Sketch ${board_id})
7-
target_sketches(Sketch ${board_id} sketch.ino)
7+
target_sketches(Sketch ${board_id} sketch1.ino sketch2.pde)
File renamed without changes.

examples/sketch/sketch2.pde

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <HardwareSerial.h>
2+
3+
int x = 5;
4+
5+
void foo()
6+
{
7+
Serial.print(x);
8+
}

0 commit comments

Comments
 (0)