1
1
#=============================================================================#
2
2
# Writes the given lines of code belonging to the sketch to the given file path.
3
- # _sketch_loc - List of lines-of-code belonging to the sketch.
3
+ # _sketch_lines - List of lines-of-code belonging to the sketch.
4
4
# _file_path - Full path to the written source file.
5
5
#=============================================================================#
6
- function (_write_source_file _sketch_loc _file_path)
6
+ function (_write_source_file _sketch_lines _file_path)
7
7
8
8
file (WRITE "${_file_path} " "" ) # Clear previous file's contents
9
9
10
- foreach (loc ${_sketch_loc } )
11
- string ( REGEX REPLACE "^(.+) ${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT} (.*)$" " \\ 1; \\ 2" original_loc " ${loc} " )
12
- file (APPEND "${_file_path} " "${original_loc } " )
10
+ foreach (line ${_sketch_lines } )
11
+ escape_semicolon_in_string( " ${line} " original_line REVERSE )
12
+ file (APPEND "${_file_path} " "${original_line } " )
13
13
endforeach ()
14
14
15
15
endfunction ()
@@ -20,12 +20,12 @@ endfunction()
20
20
# best fitted the insertion, however, it might need a bit more optimization. Why?
21
21
# Because above those lines there might be a comment, or a comment block,
22
22
# all of which should be taken into account in order to minimize the effect on code's readability.
23
- # _sketch_loc - List of lines-of-code belonging to the sketch.
24
- # _active_index - Index that indicates the best-not-optimized loc to insert header to.
23
+ # _sketch_lines - List of lines-of-code belonging to the sketch.
24
+ # _active_index - Index that indicates the best-not-optimized line to insert header to.
25
25
# _return_var - Name of variable in parent-scope holding the return value.
26
26
# Returns - Best fitted index to insert platform's main header '#include' to.
27
27
#=============================================================================#
28
- function (_get_matching_header_insertion_index _sketch_loc _active_index _return_var)
28
+ function (_get_matching_header_insertion_index _sketch_lines _active_index _return_var)
29
29
30
30
if (${_active_index} EQUAL 0) # First line in a file will always result in the 1st index
31
31
set (${_return_var} 0 PARENT_SCOPE)
@@ -34,14 +34,14 @@ function(_get_matching_header_insertion_index _sketch_loc _active_index _return_
34
34
decrement_integer(_active_index 1)
35
35
endif ()
36
36
37
- list (GET _sketch_loc ${_active_index} previous_loc)
37
+ list (GET _sketch_lines ${_active_index} previous_loc)
38
38
39
39
if ("${previous_loc} " MATCHES "^//" ) # Simple one-line comment
40
40
set (matching_index ${_active_index} )
41
41
elseif ("${previous_loc} " MATCHES "\\ */$" ) # End of multi-line comment
42
42
43
43
foreach (index RANGE ${_active_index} 0)
44
- list (GET _sketch_loc ${index} multi_comment_line)
44
+ list (GET _sketch_lines ${index} multi_comment_line)
45
45
46
46
if ("${multi_comment_line} " MATCHES "^\\ /\\ *" ) # Start of multi-line comment
47
47
set (matching_index ${index} )
@@ -63,63 +63,86 @@ endfunction()
63
63
# _sketch_lines - List of code lines read from the converted sketch file.
64
64
# _insertion_line_index - Index of a code line at which the header should be inserted
65
65
#=============================================================================#
66
- macro (_insert_platform_header_include_line _sketch_lines _insertion_line_index)
66
+ macro (_insert_line _inserted_line _sketch_lines _insertion_line_index)
67
67
68
68
_get_matching_header_insertion_index("${_sketch_lines} " ${_insertion_line_index} header_index)
69
69
70
70
if (${header_index} LESS ${_insertion_line_index} )
71
- set (formatted_include_line ${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE } "\n\n " )
71
+ set (formatted_include_line ${_inserted_line } "\n\n " )
72
72
elseif (${header_index} EQUAL 0)
73
- set (formatted_include_line ${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE } "\n " )
73
+ set (formatted_include_line ${_inserted_line } "\n " )
74
74
else ()
75
- set (formatted_include_line "\n " ${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE } )
75
+ set (formatted_include_line "\n " ${_inserted_line } )
76
76
77
77
if (${header_index} GREATER_EQUAL ${_insertion_line_index} )
78
78
decrement_integer(header_index 1)
79
79
string (APPEND formatted_include_line "\n " )
80
80
endif ()
81
81
endif ()
82
82
83
- list (INSERT refined_sketch ${header_index} ${formatted_include_line} )
83
+ list (INSERT converted_source ${header_index} ${formatted_include_line} )
84
+
85
+ endmacro ()
86
+
87
+ macro (_insert_prototypes _prototypes _sketch_lines _insertion_line_index)
88
+
89
+ foreach (prototype ${_prototypes} )
90
+ # Add semicolon ';' to make it a declaration
91
+ escape_semicolon_in_string("${prototype} ;" formatted_prototype)
92
+
93
+ _insert_line("${formatted_prototype} " "${sketch_lines} " ${line_index} )
94
+ increment_integer(_insertion_line_index 1)
95
+ endforeach ()
84
96
85
97
endmacro ()
86
98
87
99
#=============================================================================#
88
100
# Converts the given sketch file into a valid 'cpp' source file under the project's working dir.
89
101
# During the conversion process the platform's main header file is inserted to the source file
90
102
# since it's critical for it to include it - Something that doesn't happen in "Standard" sketches.
91
- # _sketch_file - Full path to the original sketch file (Read from).
92
- # _converted_source_path - Full path to the converted target source file (Written to).
103
+ # _sketch_file - Full path to the original sketch file (Read from).
104
+ # _converted_source_path - Full path to the converted target source file (Written to).
105
+ # _sketch_prototypes - List of prototypes to genereate, i.e. function definitions without a declaration.
93
106
#=============================================================================#
94
- function (convert_sketch_to_source _sketch_file _converted_source_path)
107
+ function (convert_sketch_to_source _sketch_file _converted_source_path _sketch_prototypes )
95
108
96
- file (STRINGS "${_sketch_file} " sketch_loc )
109
+ file (STRINGS "${_sketch_file} " sketch_lines )
97
110
111
+ set (function_prototype_pattern
112
+ "${ARDUINO_CMAKE_FUNCTION_DECLARATION_REGEX_PATTERN} |${ARDUINO_CMAKE_FUNCTION_DEFINITION_REGEX_PATTERN} " )
98
113
set (header_insert_pattern
99
- "${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN} |${ARDUINO_CMAKE_FUNCTION_DEFINITION_REGEX_PATTERN} " )
114
+ "${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN} |${function_prototype_pattern} " )
115
+
100
116
set (header_inserted FALSE )
117
+ set (prototypes_inserted FALSE )
101
118
102
- list (LENGTH sketch_loc num_of_loc)
103
- decrement_integer(num_of_loc 1)
119
+ list_max_index("${sketch_lines} " lines_count)
120
+ #[[list(LENGTH sketch_lines lines_count)
121
+ decrement_integer(lines_count 1)]]
104
122
105
- foreach (loc_index RANGE 0 ${num_of_loc } )
123
+ foreach (line_index RANGE ${lines_count } )
106
124
107
- list (GET sketch_loc ${loc_index} loc )
125
+ list (GET sketch_lines ${line_index} line )
108
126
109
- if (NOT ${header_inserted} AND "${loc} " MATCHES "${header_insert_pattern} " )
110
- _insert_platform_header_include_line("${sketch_loc} " ${loc_index} )
111
- set (header_inserted TRUE )
127
+ if (NOT ${header_inserted} )
128
+ if ("${line} " MATCHES "${header_insert_pattern} " )
129
+ _insert_line("${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE} " "${sketch_lines} " ${line_index} )
130
+ set (header_inserted TRUE )
131
+ endif ()
132
+ elseif (NOT ${prototypes_inserted} AND "${line} " MATCHES "${function_prototype_pattern} " )
133
+ _insert_prototypes("${_sketch_prototypes} " "${sketch_lines} " ${line_index} )
134
+ set (prototypes_inserted TRUE )
112
135
endif ()
113
136
114
- if ("${loc } " STREQUAL "" )
115
- list (APPEND refined_sketch "\n " )
137
+ if ("${line } " STREQUAL "" )
138
+ list (APPEND converted_source "\n " )
116
139
else ()
117
- string ( REGEX REPLACE "^(.+);(.*)$" " \\ 1 ${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT} \\ 2" refined_loc " ${loc} " )
118
- list (APPEND refined_sketch "${refined_loc } \n " )
140
+ escape_semicolon_in_string( " ${line} " formatted_line )
141
+ list (APPEND converted_source "${formatted_line } \n " )
119
142
endif ()
120
143
121
144
endforeach ()
122
145
123
- _write_source_file("${refined_sketch } " "${_converted_source_path} " )
146
+ _write_source_file("${converted_source } " "${_converted_source_path} " )
124
147
125
148
endfunction ()
0 commit comments