@@ -8,12 +8,8 @@ function(_write_source_file _sketch_loc _file_path)
88 file (WRITE "${_file_path} " "" ) # Clear previous file's contents
99
1010 foreach (loc ${_sketch_loc} )
11-
12- string (REGEX REPLACE "^(.+)${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT} (.*)$" "\\ 1;\\ 2"
13- original_loc "${loc} " )
14-
11+ string (REGEX REPLACE "^(.+)${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT} (.*)$" "\\ 1;\\ 2" original_loc "${loc} " )
1512 file (APPEND "${_file_path} " "${original_loc} " )
16-
1713 endforeach ()
1814
1915endfunction ()
@@ -42,31 +38,52 @@ function(_get_matching_header_insertion_index _sketch_loc _active_index _return_
4238
4339 if ("${previous_loc} " MATCHES "^//" ) # Simple one-line comment
4440 set (matching_index ${_active_index} )
45-
4641 elseif ("${previous_loc} " MATCHES "\\ */$" ) # End of multi-line comment
4742
4843 foreach (index RANGE ${_active_index} 0)
49-
5044 list (GET _sketch_loc ${index} multi_comment_line)
5145
5246 if ("${multi_comment_line} " MATCHES "^\\ /\\ *" ) # Start of multi-line comment
5347 set (matching_index ${index} )
5448 break ()
5549 endif ()
56-
5750 endforeach ()
5851
5952 else () # Previous line isn't a comment - Return original index
60-
6153 increment_integer(_active_index 1)
6254 set (matching_index ${_active_index} )
63-
6455 endif ()
6556
6657 set (${_return_var} ${matching_index} PARENT_SCOPE)
6758
6859endfunction ()
6960
61+ #=============================================================================#
62+ # Inline macro which handles the process of inserting a line including the platform header.
63+ # _sketch_lines - List of code lines read from the converted sketch file.
64+ # _insertion_line_index - Index of a code line at which the header should be inserted
65+ #=============================================================================#
66+ macro (_insert_platform_header_include_line _sketch_lines _insertion_line_index)
67+
68+ _get_matching_header_insertion_index("${_sketch_lines} " ${_insertion_line_index} header_index)
69+
70+ if (${header_index} LESS ${_insertion_line_index} )
71+ set (formatted_include_line ${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE} "\n\n " )
72+ elseif (${header_index} EQUAL 0)
73+ set (formatted_include_line ${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE} "\n " )
74+ else ()
75+ set (formatted_include_line "\n " ${ARDUINO_CMAKE_PLATFORM_HEADER_INCLUDE_LINE} )
76+
77+ if (${header_index} GREATER_EQUAL ${_insertion_line_index} )
78+ decrement_integer(header_index 1)
79+ string (APPEND formatted_include_line "\n " )
80+ endif ()
81+ endif ()
82+
83+ list (INSERT refined_sketch ${header_index} ${formatted_include_line} )
84+
85+ endmacro ()
86+
7087#=============================================================================#
7188# Converts the given sketch file into a valid 'cpp' source file under the project's working dir.
7289# During the conversion process the platform's main header file is inserted to the source file
@@ -78,63 +95,26 @@ function(convert_sketch_to_source _sketch_file _converted_source_path)
7895
7996 file (STRINGS "${_sketch_file} " sketch_loc)
8097
81- list (LENGTH sketch_loc num_of_loc)
82- decrement_integer(num_of_loc 1)
83-
84- set (refined_sketch)
98+ set (header_insert_pattern "${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN} |${ARDUINO_CMAKE_FUNCTION_REGEX_PATTERN} " )
8599 set (header_inserted FALSE )
86100
87- set (header_insert_pattern
88- "${ARDUINO_CMAKE_HEADER_INCLUDE_REGEX_PATTERN} |${ARDUINO_CMAKE_FUNCTION_REGEX_PATTERN} " )
89- set (include_line "#include <${ARDUINO_CMAKE_PLATFORM_HEADER_NAME} >" )
101+ list (LENGTH sketch_loc num_of_loc)
102+ decrement_integer(num_of_loc 1)
90103
91104 foreach (loc_index RANGE 0 ${num_of_loc} )
92105
93106 list (GET sketch_loc ${loc_index} loc)
94107
95- if (NOT ${header_inserted} )
96-
97- if ("${loc} " MATCHES "${header_insert_pattern} " )
98-
99- _get_matching_header_insertion_index("${sketch_loc} " ${loc_index} header_index)
100-
101- if (${header_index} LESS ${loc_index} )
102- set (formatted_include_line ${include_line} "\n\n " )
103-
104- elseif (${header_index} EQUAL 0)
105- set (formatted_include_line ${include_line} "\n " )
106-
107- else ()
108-
109- set (formatted_include_line "\n " ${include_line} )
110-
111- if (${header_index} GREATER_EQUAL ${loc_index} )
112-
113- decrement_integer(header_index 1)
114-
115- string (APPEND formatted_include_line "\n " )
116-
117- endif ()
118-
119- endif ()
120-
121- list (INSERT refined_sketch ${header_index} ${formatted_include_line} )
122-
123- set (header_inserted TRUE )
124-
125- endif ()
126-
108+ if (NOT ${header_inserted} AND "${loc} " MATCHES "${header_insert_pattern} " )
109+ _insert_platform_header_include_line("${sketch_loc} " ${loc_index} )
110+ set (header_inserted TRUE )
127111 endif ()
128112
129113 if ("${loc} " STREQUAL "" )
130114 list (APPEND refined_sketch "\n " )
131115 else ()
132-
133- string (REGEX REPLACE "^(.+);(.*)$" "\\ 1${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT} \\ 2"
134- refined_loc "${loc} " )
135-
116+ string (REGEX REPLACE "^(.+);(.*)$" "\\ 1${ARDUINO_CMAKE_SEMICOLON_REPLACEMENT} \\ 2" refined_loc "${loc} " )
136117 list (APPEND refined_sketch "${refined_loc} \n " )
137-
138118 endif ()
139119
140120 endforeach ()
0 commit comments