@@ -102,31 +102,11 @@ func generateCompileFlags(tempDir, inoPath, fqbn string) (string, error) {
102
102
return flagsPath , errors .Wrap (err , "Error while creating output file for compile flags." )
103
103
}
104
104
defer outFile .Close ()
105
- writer := bufio .NewWriter (outFile )
106
105
107
- // TODO support other architectures
108
- writer .WriteString ("--target=avr\n " )
109
- cppFlags := expandProperty (properties , "compiler.cpp.flags" )
110
- writer .WriteString (strings .ReplaceAll (cppFlags , " " , "\n " ) + "\n " )
111
- mcu := expandProperty (properties , "build.mcu" )
112
- writer .WriteString ("-mmcu=" + mcu + "\n " )
113
- fcpu := expandProperty (properties , "build.f_cpu" )
114
- writer .WriteString ("-DF_CPU=" + fcpu + "\n " )
115
- ideVersion := expandProperty (properties , "runtime.ide.version" )
116
- writer .WriteString ("-DARDUINO=" + ideVersion + "\n " )
117
- board := expandProperty (properties , "build.board" )
118
- writer .WriteString ("-DARDUINO_" + board + "\n " )
119
- arch := expandProperty (properties , "build.arch" )
120
- writer .WriteString ("-DARDUINO_ARCH_" + arch + "\n " )
121
- corePath := expandProperty (properties , "build.core.path" )
122
- writer .WriteString ("-I" + corePath + "\n " )
123
- variantPath := expandProperty (properties , "build.variant.path" )
124
- writer .WriteString ("-I" + variantPath + "\n " )
125
- avrgccPath := expandProperty (properties , "runtime.tools.avr-gcc.path" )
126
- writer .WriteString ("-I" + filepath .Join (avrgccPath , "avr" , "include" ) + "\n " )
127
-
128
- writer .Flush ()
129
- return flagsPath , nil
106
+ printer := Printer {Writer : bufio .NewWriter (outFile )}
107
+ printCompileFlags (properties , & printer , fqbn )
108
+ printer .Flush ()
109
+ return flagsPath , printer .Err
130
110
}
131
111
132
112
func generateTargetFile (tempDir , inoPath , cppPath , fqbn string ) (cppCode []byte , err error ) {
@@ -152,8 +132,10 @@ func generateTargetFile(tempDir, inoPath, cppPath, fqbn string) (cppCode []byte,
152
132
return
153
133
}
154
134
155
- func copyIno2Cpp (inoCode []byte , cppPath string ) (cppCode []byte , err error ) {
156
- cppCode = inoCode
135
+ func copyIno2Cpp (inoCode string , cppPath string ) (cppCode []byte , err error ) {
136
+ inoPath := strings .TrimSuffix (cppPath , ".cpp" )
137
+ filePrefix := "#include <Arduino.h>\n #line 1 \" " + inoPath + "\" \n "
138
+ cppCode = []byte (filePrefix + inoCode )
157
139
err = ioutil .WriteFile (cppPath , cppCode , 0600 )
158
140
if err != nil {
159
141
err = errors .Wrap (err , "Error while writing target file to temporary directory." )
@@ -165,6 +147,66 @@ func copyIno2Cpp(inoCode []byte, cppPath string) (cppCode []byte, err error) {
165
147
return
166
148
}
167
149
150
+ func printCompileFlags (properties map [string ]string , printer * Printer , fqbn string ) {
151
+ if strings .Contains (fqbn , ":avr:" ) {
152
+ printer .Println ("--target=avr" )
153
+ } else if strings .Contains (fqbn , ":sam:" ) {
154
+ printer .Println ("--target=arm-none-eabi" )
155
+ }
156
+ cppFlags := expandProperty (properties , "compiler.cpp.flags" )
157
+ printer .Println (strings .ReplaceAll (cppFlags , " " , "\n " ))
158
+ mcu := expandProperty (properties , "build.mcu" )
159
+ if strings .Contains (fqbn , ":avr:" ) {
160
+ printer .Println ("-mmcu=" + mcu )
161
+ } else if strings .Contains (fqbn , ":sam:" ) {
162
+ printer .Println ("-mcpu=" + mcu )
163
+ }
164
+ fcpu := expandProperty (properties , "build.f_cpu" )
165
+ printer .Println ("-DF_CPU=" + fcpu )
166
+ ideVersion := expandProperty (properties , "runtime.ide.version" )
167
+ printer .Println ("-DARDUINO=" + ideVersion )
168
+ board := expandProperty (properties , "build.board" )
169
+ printer .Println ("-DARDUINO_" + board )
170
+ arch := expandProperty (properties , "build.arch" )
171
+ printer .Println ("-DARDUINO_ARCH_" + arch )
172
+ if strings .Contains (fqbn , ":sam:" ) {
173
+ libSamFlags := expandProperty (properties , "compiler.libsam.c.flags" )
174
+ printer .Println (strings .ReplaceAll (libSamFlags , " " , "\n " ))
175
+ }
176
+ extraFlags := expandProperty (properties , "build.extra_flags" )
177
+ printer .Println (strings .ReplaceAll (extraFlags , " " , "\n " ))
178
+ corePath := expandProperty (properties , "build.core.path" )
179
+ printer .Println ("-I" + corePath )
180
+ variantPath := expandProperty (properties , "build.variant.path" )
181
+ printer .Println ("-I" + variantPath )
182
+ avrgccPath := expandProperty (properties , "runtime.tools.avr-gcc.path" )
183
+ printer .Println ("-I" + filepath .Join (avrgccPath , "avr" , "include" ))
184
+ }
185
+
186
+ // Printer prints to a Writer and stores the first error.
187
+ type Printer struct {
188
+ Writer * bufio.Writer
189
+ Err error
190
+ }
191
+
192
+ // Println prints the given text followed by a line break.
193
+ func (printer * Printer ) Println (text string ) {
194
+ if len (text ) > 0 {
195
+ _ , err := printer .Writer .WriteString (text + "\n " )
196
+ if err != nil && printer .Err == nil {
197
+ printer .Err = err
198
+ }
199
+ }
200
+ }
201
+
202
+ // Flush flushes the underlying writer.
203
+ func (printer * Printer ) Flush () {
204
+ err := printer .Writer .Flush ()
205
+ if err != nil && printer .Err == nil {
206
+ printer .Err = err
207
+ }
208
+ }
209
+
168
210
func logCommandErr (command string , stdout []byte , err error , filter func (string ) string ) error {
169
211
message := ""
170
212
log .Println ("Command error:" , command , err )
0 commit comments