Skip to content

Commit 234234a

Browse files
sago35deadprogram
authored andcommitted
build: add JSON output to build command
1 parent 3d68804 commit 234234a

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

compileopts/options.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ type Options struct {
3232
PrintIR bool
3333
DumpSSA bool
3434
VerifyIR bool
35-
PrintCommands func(cmd string, args ...string)
36-
Semaphore chan struct{} // -p flag controls cap
35+
PrintCommands func(cmd string, args ...string) `json:"-"`
36+
Semaphore chan struct{} `json:"-"` // -p flag controls cap
3737
Debug bool
3838
PrintSizes string
3939
PrintAllocs *regexp.Regexp // regexp string
@@ -46,6 +46,7 @@ type Options struct {
4646
OpenOCDCommands []string
4747
LLVMFeatures string
4848
Directory string
49+
PrintJSON bool
4950
}
5051

5152
// Verify performs a validation on the given options, raising an error if options are not valid.

main.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
142142
return err
143143
}
144144

145+
if options.PrintJSON {
146+
b, err := json.MarshalIndent(config, "", " ")
147+
if err != nil {
148+
handleCompilerError(err)
149+
}
150+
fmt.Printf("%s\n", string(b))
151+
return nil
152+
}
153+
145154
return builder.Build(pkgName, outpath, config, func(result builder.BuildResult) error {
146155
if outpath == "" {
147156
if strings.HasSuffix(pkgName, ".go") {
@@ -1213,13 +1222,13 @@ func main() {
12131222
llvmFeatures := flag.String("llvm-features", "", "comma separated LLVM features to enable")
12141223
cpuprofile := flag.String("cpuprofile", "", "cpuprofile output")
12151224

1216-
var flagJSON, flagDeps, flagTest *bool
1217-
if command == "help" || command == "list" || command == "info" {
1218-
flagJSON = flag.Bool("json", false, "print data in JSON format")
1225+
var flagJSON, flagDeps, flagTest bool
1226+
if command == "help" || command == "list" || command == "info" || command == "build" {
1227+
flag.BoolVar(&flagJSON, "json", false, "print data in JSON format")
12191228
}
12201229
if command == "help" || command == "list" {
1221-
flagDeps = flag.Bool("deps", false, "supply -deps flag to go list")
1222-
flagTest = flag.Bool("test", false, "supply -test flag to go list")
1230+
flag.BoolVar(&flagDeps, "deps", false, "supply -deps flag to go list")
1231+
flag.BoolVar(&flagTest, "test", false, "supply -test flag to go list")
12231232
}
12241233
var outpath string
12251234
if command == "help" || command == "build" || command == "build-library" || command == "test" {
@@ -1296,6 +1305,7 @@ func main() {
12961305
Programmer: *programmer,
12971306
OpenOCDCommands: ocdCommands,
12981307
LLVMFeatures: *llvmFeatures,
1308+
PrintJSON: flagJSON,
12991309
}
13001310
if *printCommands {
13011311
options.PrintCommands = printCommand
@@ -1532,7 +1542,7 @@ func main() {
15321542
fmt.Fprintln(os.Stderr, err)
15331543
os.Exit(1)
15341544
}
1535-
if *flagJSON {
1545+
if flagJSON {
15361546
json, _ := json.MarshalIndent(struct {
15371547
GOROOT string `json:"goroot"`
15381548
GOOS string `json:"goos"`
@@ -1571,13 +1581,13 @@ func main() {
15711581
os.Exit(1)
15721582
}
15731583
var extraArgs []string
1574-
if *flagJSON {
1584+
if flagJSON {
15751585
extraArgs = append(extraArgs, "-json")
15761586
}
1577-
if *flagDeps {
1587+
if flagDeps {
15781588
extraArgs = append(extraArgs, "-deps")
15791589
}
1580-
if *flagTest {
1590+
if flagTest {
15811591
extraArgs = append(extraArgs, "-test")
15821592
}
15831593
cmd, err := loader.List(config, extraArgs, flag.Args())

0 commit comments

Comments
 (0)