@@ -101,178 +101,3 @@ func generateBuildEnvironment(sketchDir *paths.Path, fqbn string) (*paths.Path,
101101 log .Println ("arduino-cli output:" , cmdOutput )
102102 return res .BuilderResult .BuildPath , nil
103103}
104-
105- func filterErrorsAndWarnings (cppCode []byte ) string {
106- var sb strings.Builder
107- scanner := bufio .NewScanner (bytes .NewReader (cppCode ))
108- for scanner .Scan () {
109- lineStr := scanner .Text ()
110- if ! (strings .HasPrefix (lineStr , "ERROR:" ) || strings .HasPrefix (lineStr , "WARNING:" )) {
111- sb .WriteString (lineStr )
112- sb .WriteRune ('\n' )
113- }
114- }
115- return sb .String ()
116- }
117-
118- func copyIno2Cpp (inoCode string , cppPath string ) (cppCode []byte , err error ) {
119- inoPath := strings .TrimSuffix (cppPath , ".cpp" )
120- filePrefix := "#include <Arduino.h>\n #line 1 \" " + inoPath + "\" \n "
121- cppCode = []byte (filePrefix + inoCode )
122- err = ioutil .WriteFile (cppPath , cppCode , 0600 )
123- if err != nil {
124- err = errors .Wrap (err , "Error while writing target file to temporary directory." )
125- return
126- }
127- if enableLogging {
128- log .Println ("Target file written to" , cppPath )
129- }
130- return
131- }
132-
133- func printCompileFlags (buildProps * properties.Map , printer * Printer , fqbn string ) {
134- if strings .Contains (fqbn , ":avr:" ) {
135- printer .Println ("--target=avr" )
136- } else if strings .Contains (fqbn , ":sam:" ) {
137- printer .Println ("--target=arm-none-eabi" )
138- }
139- cppFlags := buildProps .ExpandPropsInString (buildProps .Get ("compiler.cpp.flags" ))
140- printer .Println (splitFlags (cppFlags ))
141- mcu := buildProps .ExpandPropsInString (buildProps .Get ("build.mcu" ))
142- if strings .Contains (fqbn , ":avr:" ) {
143- printer .Println ("-mmcu=" , mcu )
144- } else if strings .Contains (fqbn , ":sam:" ) {
145- printer .Println ("-mcpu=" , mcu )
146- }
147- fcpu := buildProps .ExpandPropsInString (buildProps .Get ("build.f_cpu" ))
148- printer .Println ("-DF_CPU=" , fcpu )
149- ideVersion := buildProps .ExpandPropsInString (buildProps .Get ("runtime.ide.version" ))
150- printer .Println ("-DARDUINO=" , ideVersion )
151- board := buildProps .ExpandPropsInString (buildProps .Get ("build.board" ))
152- printer .Println ("-DARDUINO_" , board )
153- arch := buildProps .ExpandPropsInString (buildProps .Get ("build.arch" ))
154- printer .Println ("-DARDUINO_ARCH_" , arch )
155- if strings .Contains (fqbn , ":sam:" ) {
156- libSamFlags := buildProps .ExpandPropsInString (buildProps .Get ("compiler.libsam.c.flags" ))
157- printer .Println (splitFlags (libSamFlags ))
158- }
159- extraFlags := buildProps .ExpandPropsInString (buildProps .Get ("build.extra_flags" ))
160- printer .Println (splitFlags (extraFlags ))
161- corePath := buildProps .ExpandPropsInString (buildProps .Get ("build.core.path" ))
162- printer .Println ("-I" , corePath )
163- variantPath := buildProps .ExpandPropsInString (buildProps .Get ("build.variant.path" ))
164- printer .Println ("-I" , variantPath )
165- if strings .Contains (fqbn , ":avr:" ) {
166- avrgccPath := buildProps .ExpandPropsInString (buildProps .Get ("runtime.tools.avr-gcc.path" ))
167- printer .Println ("-I" , filepath .Join (avrgccPath , "avr" , "include" ))
168- }
169-
170- printLibraryPaths (corePath , printer )
171- }
172-
173- func printLibraryPaths (basePath string , printer * Printer ) {
174- parentDir := filepath .Dir (basePath )
175- if strings .HasSuffix (parentDir , string (filepath .Separator )) || strings .HasSuffix (parentDir , "." ) {
176- return
177- }
178- libsDir := filepath .Join (parentDir , "libraries" )
179- if libraries , err := ioutil .ReadDir (libsDir ); err == nil {
180- for _ , libInfo := range libraries {
181- if libInfo .IsDir () {
182- srcDir := filepath .Join (libsDir , libInfo .Name (), "src" )
183- if srcInfo , err := os .Stat (srcDir ); err == nil && srcInfo .IsDir () {
184- printer .Println ("-I" , srcDir )
185- } else {
186- printer .Println ("-I" , filepath .Join (libsDir , libInfo .Name ()))
187- }
188- }
189- }
190- }
191- printLibraryPaths (parentDir , printer )
192- }
193-
194- // Printer prints to a Writer and stores the first error.
195- type Printer struct {
196- Writer * bufio.Writer
197- Err error
198- }
199-
200- // Println prints the given strings followed by a line break.
201- func (printer * Printer ) Println (text ... string ) {
202- totalLen := 0
203- for i := range text {
204- if len (text [i ]) > 0 {
205- _ , err := printer .Writer .WriteString (text [i ])
206- if err != nil && printer .Err == nil {
207- printer .Err = err
208- }
209- totalLen += len (text [i ])
210- }
211- }
212- if totalLen > 0 {
213- _ , err := printer .Writer .WriteString ("\n " )
214- if err != nil && printer .Err == nil {
215- printer .Err = err
216- }
217- }
218- }
219-
220- // Flush flushes the underlying writer.
221- func (printer * Printer ) Flush () {
222- err := printer .Writer .Flush ()
223- if err != nil && printer .Err == nil {
224- printer .Err = err
225- }
226- }
227-
228- func splitFlags (flags string ) string {
229- flagsBytes := []byte (flags )
230- result := make ([]byte , len (flagsBytes ))
231- inSingleQuotes := false
232- inDoubleQuotes := false
233- for i , b := range flagsBytes {
234- if b == '\'' && ! inDoubleQuotes {
235- inSingleQuotes = ! inSingleQuotes
236- }
237- if b == '"' && ! inSingleQuotes {
238- inDoubleQuotes = ! inDoubleQuotes
239- }
240- if b == ' ' && ! inSingleQuotes && ! inDoubleQuotes {
241- result [i ] = '\n'
242- } else {
243- result [i ] = b
244- }
245- }
246- return string (result )
247- }
248-
249- func logCommandErr (command * exec.Cmd , stdout []byte , err error , filter func (string ) string ) error {
250- message := ""
251- log .Println ("Command error:" , command .Args , err )
252- if len (stdout ) > 0 {
253- stdoutStr := string (stdout )
254- log .Println ("------------------------------BEGIN STDOUT\n " , stdoutStr , "------------------------------END STDOUT" )
255- message += filter (stdoutStr )
256- }
257- if exitErr , ok := err .(* exec.ExitError ); ok {
258- stderr := exitErr .Stderr
259- if len (stderr ) > 0 {
260- stderrStr := string (stderr )
261- log .Println ("------------------------------BEGIN STDERR\n " , stderrStr , "------------------------------END STDERR" )
262- message += filter (stderrStr )
263- }
264- }
265- if len (message ) == 0 {
266- return err
267- }
268- return errors .New (message )
269- }
270-
271- func errMsgFilter (tempDir string ) func (string ) string {
272- if ! strings .HasSuffix (tempDir , string (filepath .Separator )) {
273- tempDir += string (filepath .Separator )
274- }
275- return func (s string ) string {
276- return strings .ReplaceAll (s , tempDir , "" )
277- }
278- }
0 commit comments