@@ -138,27 +138,13 @@ func printCommand(cmd string, args ...string) {
138138}
139139
140140// Build compiles and links the given package and writes it to outpath.
141- func Build (pkgName , outpath string , options * compileopts.Options ) error {
142- config , err := builder .NewConfig (options )
143- if err != nil {
144- return err
145- }
146-
147- if options .PrintJSON {
148- b , err := json .MarshalIndent (config , "" , " " )
149- if err != nil {
150- handleCompilerError (err )
151- }
152- fmt .Printf ("%s\n " , string (b ))
153- return nil
154- }
155-
141+ func Build (pkgName , outpath string , config * compileopts.Config ) error {
156142 // Create a temporary directory for intermediary files.
157143 tmpdir , err := os .MkdirTemp ("" , "tinygo" )
158144 if err != nil {
159145 return err
160146 }
161- if ! options .Work {
147+ if ! config . Options .Work {
162148 defer os .RemoveAll (tmpdir )
163149 }
164150
@@ -1396,6 +1382,61 @@ func usage(command string) {
13961382
13971383}
13981384
1385+ // Print diagnostics very similar to the -json flag in Go.
1386+ func printBuildOutput (err error , jsonDiagnostics bool ) {
1387+ if err == nil {
1388+ return // nothing to report
1389+ }
1390+
1391+ if jsonDiagnostics {
1392+ workingDir , getwdErr := os .Getwd ()
1393+ if getwdErr != nil {
1394+ workingDir = ""
1395+ }
1396+
1397+ type jsonDiagnosticOutput struct {
1398+ ImportPath string
1399+ Action string
1400+ Output string `json:",omitempty"`
1401+ }
1402+
1403+ for _ , diags := range diagnostics .CreateDiagnostics (err ) {
1404+ if diags .ImportPath != "" {
1405+ output , _ := json .Marshal (jsonDiagnosticOutput {
1406+ ImportPath : diags .ImportPath ,
1407+ Action : "build-output" ,
1408+ Output : "# " + diags .ImportPath + "\n " ,
1409+ })
1410+ os .Stdout .Write (output )
1411+ os .Stdout .Write ([]byte {'\n' })
1412+ }
1413+ for _ , diag := range diags .Diagnostics {
1414+ w := & bytes.Buffer {}
1415+ diag .WriteTo (w , workingDir )
1416+ output , _ := json .Marshal (jsonDiagnosticOutput {
1417+ ImportPath : diags .ImportPath ,
1418+ Action : "build-output" ,
1419+ Output : w .String (),
1420+ })
1421+ os .Stdout .Write (output )
1422+ os .Stdout .Write ([]byte {'\n' })
1423+ }
1424+
1425+ // Emit the "Action":"build-fail" JSON.
1426+ output , _ := json .Marshal (jsonDiagnosticOutput {
1427+ ImportPath : diags .ImportPath ,
1428+ Action : "build-fail" ,
1429+ })
1430+ os .Stdout .Write (output )
1431+ os .Stdout .Write ([]byte {'\n' })
1432+ }
1433+ os .Exit (1 )
1434+ }
1435+
1436+ // Regular diagnostic handling.
1437+ handleCompilerError (err )
1438+ }
1439+
13991440func handleCompilerError (err error ) {
14001441 if err != nil {
14011442 wd , getwdErr := os .Getwd ()
@@ -1512,6 +1553,7 @@ func main() {
15121553 printStacks := flag .Bool ("print-stacks" , false , "print stack sizes of goroutines" )
15131554 printAllocsString := flag .String ("print-allocs" , "" , "regular expression of functions for which heap allocations should be printed" )
15141555 printCommands := flag .Bool ("x" , false , "Print commands" )
1556+ flagJSON := flag .Bool ("json" , false , "print output in JSON format" )
15151557 parallelism := flag .Int ("p" , runtime .GOMAXPROCS (0 ), "the number of build jobs that can run in parallel" )
15161558 nodebug := flag .Bool ("no-debug" , false , "strip debug information" )
15171559 nobounds := flag .Bool ("nobounds" , false , "do not emit bounds checks" )
@@ -1537,10 +1579,7 @@ func main() {
15371579 // development it can be useful to not emit debug information at all.
15381580 skipDwarf := flag .Bool ("internal-nodwarf" , false , "internal flag, use -no-debug instead" )
15391581
1540- var flagJSON , flagDeps , flagTest bool
1541- if command == "help" || command == "list" || command == "info" || command == "build" {
1542- flag .BoolVar (& flagJSON , "json" , false , "print data in JSON format" )
1543- }
1582+ var flagDeps , flagTest bool
15441583 if command == "help" || command == "list" {
15451584 flag .BoolVar (& flagDeps , "deps" , false , "supply -deps flag to go list" )
15461585 flag .BoolVar (& flagTest , "test" , false , "supply -test flag to go list" )
@@ -1636,7 +1675,6 @@ func main() {
16361675 Programmer : * programmer ,
16371676 OpenOCDCommands : ocdCommands ,
16381677 LLVMFeatures : * llvmFeatures ,
1639- PrintJSON : flagJSON ,
16401678 Monitor : * monitor ,
16411679 BaudRate : * baudrate ,
16421680 Timeout : * timeout ,
@@ -1691,21 +1729,23 @@ func main() {
16911729 os .Exit (1 )
16921730 }
16931731
1694- err := Build ( pkgName , outpath , options )
1732+ config , err := builder . NewConfig ( options )
16951733 handleCompilerError (err )
1734+ err = Build (pkgName , outpath , config )
1735+ printBuildOutput (err , * flagJSON )
16961736 case "flash" , "gdb" , "lldb" :
16971737 pkgName := filepath .ToSlash (flag .Arg (0 ))
16981738 if command == "flash" {
16991739 err := Flash (pkgName , * port , options )
1700- handleCompilerError (err )
1740+ printBuildOutput (err , * flagJSON )
17011741 } else {
17021742 if ! options .Debug {
17031743 fmt .Fprintln (os .Stderr , "Debug disabled while running debugger?" )
17041744 usage (command )
17051745 os .Exit (1 )
17061746 }
17071747 err := Debug (command , pkgName , * ocdOutput , options )
1708- handleCompilerError (err )
1748+ printBuildOutput (err , * flagJSON )
17091749 }
17101750 case "run" :
17111751 if flag .NArg () < 1 {
@@ -1715,7 +1755,7 @@ func main() {
17151755 }
17161756 pkgName := filepath .ToSlash (flag .Arg (0 ))
17171757 err := Run (pkgName , options , flag .Args ()[1 :])
1718- handleCompilerError (err )
1758+ printBuildOutput (err , * flagJSON )
17191759 case "test" :
17201760 var pkgNames []string
17211761 for i := 0 ; i < flag .NArg (); i ++ {
@@ -1849,7 +1889,7 @@ func main() {
18491889 fmt .Fprintln (os .Stderr , err )
18501890 os .Exit (1 )
18511891 }
1852- if flagJSON {
1892+ if * flagJSON {
18531893 json , _ := json .MarshalIndent (struct {
18541894 Target * compileopts.TargetSpec `json:"target"`
18551895 GOROOT string `json:"goroot"`
@@ -1891,7 +1931,7 @@ func main() {
18911931 os .Exit (1 )
18921932 }
18931933 var extraArgs []string
1894- if flagJSON {
1934+ if * flagJSON {
18951935 extraArgs = append (extraArgs , "-json" )
18961936 }
18971937 if flagDeps {
0 commit comments