@@ -25,6 +25,18 @@ var commands = map[string]string{
2525 "clang" : "clang-7" ,
2626}
2727
28+ // commandError is an error type to wrap os/exec.Command errors. This provides
29+ // some more information regarding what went wrong while running a command.
30+ type commandError struct {
31+ Msg string
32+ File string
33+ Err error
34+ }
35+
36+ func (e * commandError ) Error () string {
37+ return e .Msg + " " + e .File + ": " + e .Err .Error ()
38+ }
39+
2840type BuildConfig struct {
2941 opt string
3042 gc string
@@ -200,7 +212,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
200212 cmd .Dir = sourceDir ()
201213 err := cmd .Run ()
202214 if err != nil {
203- return err
215+ return & commandError { "failed to build" , path , err }
204216 }
205217 ldflags = append (ldflags , outpath )
206218 }
@@ -216,7 +228,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
216228 cmd .Dir = sourceDir ()
217229 err := cmd .Run ()
218230 if err != nil {
219- return err
231+ return & commandError { "failed to build" , path , err }
220232 }
221233 ldflags = append (ldflags , outpath )
222234 }
@@ -229,7 +241,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
229241 cmd .Dir = sourceDir ()
230242 err = cmd .Run ()
231243 if err != nil {
232- return err
244+ return & commandError { "failed to link" , executable , err }
233245 }
234246
235247 if config .printSizes == "short" || config .printSizes == "full" {
@@ -263,7 +275,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, config *BuildConfig, act
263275 cmd .Stderr = os .Stderr
264276 err = cmd .Run ()
265277 if err != nil {
266- return err
278+ return & commandError { "failed to extract " + format + " from" , executable , err }
267279 }
268280 }
269281 return action (tmppath )
@@ -325,7 +337,11 @@ func Flash(pkgName, target, port string, config *BuildConfig) error {
325337 cmd .Stdout = os .Stdout
326338 cmd .Stderr = os .Stderr
327339 cmd .Dir = sourceDir ()
328- return cmd .Run ()
340+ err := cmd .Run ()
341+ if err != nil {
342+ return & commandError {"failed to flash" , tmppath , err }
343+ }
344+ return nil
329345 })
330346}
331347
@@ -392,7 +408,11 @@ func FlashGDB(pkgName, target, port string, ocdOutput bool, config *BuildConfig)
392408 cmd .Stdin = os .Stdin
393409 cmd .Stdout = os .Stdout
394410 cmd .Stderr = os .Stderr
395- return cmd .Run ()
411+ err := cmd .Run ()
412+ if err != nil {
413+ return & commandError {"failed to run gdb with" , tmppath , err }
414+ }
415+ return nil
396416 })
397417}
398418
@@ -415,8 +435,9 @@ func Run(pkgName, target string, config *BuildConfig) error {
415435 // Workaround for QEMU which always exits with an error.
416436 return nil
417437 }
438+ return & commandError {"failed to run compiled binary" , tmppath , err }
418439 }
419- return err
440+ return nil
420441 } else {
421442 // Run in an emulator.
422443 args := append (spec .Emulator [1 :], tmppath )
@@ -429,8 +450,9 @@ func Run(pkgName, target string, config *BuildConfig) error {
429450 // Workaround for QEMU which always exits with an error.
430451 return nil
431452 }
453+ return & commandError {"failed to run emulator with" , tmppath , err }
432454 }
433- return err
455+ return nil
434456 }
435457 })
436458}
0 commit comments