@@ -138,27 +138,13 @@ func printCommand(cmd string, args ...string) {
138
138
}
139
139
140
140
// 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 {
156
142
// Create a temporary directory for intermediary files.
157
143
tmpdir , err := os .MkdirTemp ("" , "tinygo" )
158
144
if err != nil {
159
145
return err
160
146
}
161
- if ! options .Work {
147
+ if ! config . Options .Work {
162
148
defer os .RemoveAll (tmpdir )
163
149
}
164
150
@@ -1396,6 +1382,61 @@ func usage(command string) {
1396
1382
1397
1383
}
1398
1384
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
+
1399
1440
func handleCompilerError (err error ) {
1400
1441
if err != nil {
1401
1442
wd , getwdErr := os .Getwd ()
@@ -1512,6 +1553,7 @@ func main() {
1512
1553
printStacks := flag .Bool ("print-stacks" , false , "print stack sizes of goroutines" )
1513
1554
printAllocsString := flag .String ("print-allocs" , "" , "regular expression of functions for which heap allocations should be printed" )
1514
1555
printCommands := flag .Bool ("x" , false , "Print commands" )
1556
+ flagJSON := flag .Bool ("json" , false , "print output in JSON format" )
1515
1557
parallelism := flag .Int ("p" , runtime .GOMAXPROCS (0 ), "the number of build jobs that can run in parallel" )
1516
1558
nodebug := flag .Bool ("no-debug" , false , "strip debug information" )
1517
1559
nobounds := flag .Bool ("nobounds" , false , "do not emit bounds checks" )
@@ -1537,10 +1579,7 @@ func main() {
1537
1579
// development it can be useful to not emit debug information at all.
1538
1580
skipDwarf := flag .Bool ("internal-nodwarf" , false , "internal flag, use -no-debug instead" )
1539
1581
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
1544
1583
if command == "help" || command == "list" {
1545
1584
flag .BoolVar (& flagDeps , "deps" , false , "supply -deps flag to go list" )
1546
1585
flag .BoolVar (& flagTest , "test" , false , "supply -test flag to go list" )
@@ -1636,7 +1675,6 @@ func main() {
1636
1675
Programmer : * programmer ,
1637
1676
OpenOCDCommands : ocdCommands ,
1638
1677
LLVMFeatures : * llvmFeatures ,
1639
- PrintJSON : flagJSON ,
1640
1678
Monitor : * monitor ,
1641
1679
BaudRate : * baudrate ,
1642
1680
Timeout : * timeout ,
@@ -1691,21 +1729,23 @@ func main() {
1691
1729
os .Exit (1 )
1692
1730
}
1693
1731
1694
- err := Build ( pkgName , outpath , options )
1732
+ config , err := builder . NewConfig ( options )
1695
1733
handleCompilerError (err )
1734
+ err = Build (pkgName , outpath , config )
1735
+ printBuildOutput (err , * flagJSON )
1696
1736
case "flash" , "gdb" , "lldb" :
1697
1737
pkgName := filepath .ToSlash (flag .Arg (0 ))
1698
1738
if command == "flash" {
1699
1739
err := Flash (pkgName , * port , options )
1700
- handleCompilerError (err )
1740
+ printBuildOutput (err , * flagJSON )
1701
1741
} else {
1702
1742
if ! options .Debug {
1703
1743
fmt .Fprintln (os .Stderr , "Debug disabled while running debugger?" )
1704
1744
usage (command )
1705
1745
os .Exit (1 )
1706
1746
}
1707
1747
err := Debug (command , pkgName , * ocdOutput , options )
1708
- handleCompilerError (err )
1748
+ printBuildOutput (err , * flagJSON )
1709
1749
}
1710
1750
case "run" :
1711
1751
if flag .NArg () < 1 {
@@ -1715,7 +1755,7 @@ func main() {
1715
1755
}
1716
1756
pkgName := filepath .ToSlash (flag .Arg (0 ))
1717
1757
err := Run (pkgName , options , flag .Args ()[1 :])
1718
- handleCompilerError (err )
1758
+ printBuildOutput (err , * flagJSON )
1719
1759
case "test" :
1720
1760
var pkgNames []string
1721
1761
for i := 0 ; i < flag .NArg (); i ++ {
@@ -1849,7 +1889,7 @@ func main() {
1849
1889
fmt .Fprintln (os .Stderr , err )
1850
1890
os .Exit (1 )
1851
1891
}
1852
- if flagJSON {
1892
+ if * flagJSON {
1853
1893
json , _ := json .MarshalIndent (struct {
1854
1894
Target * compileopts.TargetSpec `json:"target"`
1855
1895
GOROOT string `json:"goroot"`
@@ -1891,7 +1931,7 @@ func main() {
1891
1931
os .Exit (1 )
1892
1932
}
1893
1933
var extraArgs []string
1894
- if flagJSON {
1934
+ if * flagJSON {
1895
1935
extraArgs = append (extraArgs , "-json" )
1896
1936
}
1897
1937
if flagDeps {
0 commit comments