build: add JSON output to build command

Этот коммит содержится в:
sago35 2022-02-14 12:03:04 +09:00 коммит произвёл Ron Evans
родитель 3d68804702
коммит 234234af15
2 изменённых файлов: 22 добавлений и 11 удалений

Просмотреть файл

@ -32,8 +32,8 @@ type Options struct {
PrintIR bool PrintIR bool
DumpSSA bool DumpSSA bool
VerifyIR bool VerifyIR bool
PrintCommands func(cmd string, args ...string) PrintCommands func(cmd string, args ...string) `json:"-"`
Semaphore chan struct{} // -p flag controls cap Semaphore chan struct{} `json:"-"` // -p flag controls cap
Debug bool Debug bool
PrintSizes string PrintSizes string
PrintAllocs *regexp.Regexp // regexp string PrintAllocs *regexp.Regexp // regexp string
@ -46,6 +46,7 @@ type Options struct {
OpenOCDCommands []string OpenOCDCommands []string
LLVMFeatures string LLVMFeatures string
Directory string Directory string
PrintJSON bool
} }
// Verify performs a validation on the given options, raising an error if options are not valid. // Verify performs a validation on the given options, raising an error if options are not valid.

28
main.go
Просмотреть файл

@ -142,6 +142,15 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
return err return err
} }
if options.PrintJSON {
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
handleCompilerError(err)
}
fmt.Printf("%s\n", string(b))
return nil
}
return builder.Build(pkgName, outpath, config, func(result builder.BuildResult) error { return builder.Build(pkgName, outpath, config, func(result builder.BuildResult) error {
if outpath == "" { if outpath == "" {
if strings.HasSuffix(pkgName, ".go") { if strings.HasSuffix(pkgName, ".go") {
@ -1213,13 +1222,13 @@ func main() {
llvmFeatures := flag.String("llvm-features", "", "comma separated LLVM features to enable") llvmFeatures := flag.String("llvm-features", "", "comma separated LLVM features to enable")
cpuprofile := flag.String("cpuprofile", "", "cpuprofile output") cpuprofile := flag.String("cpuprofile", "", "cpuprofile output")
var flagJSON, flagDeps, flagTest *bool var flagJSON, flagDeps, flagTest bool
if command == "help" || command == "list" || command == "info" { if command == "help" || command == "list" || command == "info" || command == "build" {
flagJSON = flag.Bool("json", false, "print data in JSON format") flag.BoolVar(&flagJSON, "json", false, "print data in JSON format")
} }
if command == "help" || command == "list" { if command == "help" || command == "list" {
flagDeps = flag.Bool("deps", false, "supply -deps flag to go list") flag.BoolVar(&flagDeps, "deps", false, "supply -deps flag to go list")
flagTest = flag.Bool("test", false, "supply -test flag to go list") flag.BoolVar(&flagTest, "test", false, "supply -test flag to go list")
} }
var outpath string var outpath string
if command == "help" || command == "build" || command == "build-library" || command == "test" { if command == "help" || command == "build" || command == "build-library" || command == "test" {
@ -1296,6 +1305,7 @@ func main() {
Programmer: *programmer, Programmer: *programmer,
OpenOCDCommands: ocdCommands, OpenOCDCommands: ocdCommands,
LLVMFeatures: *llvmFeatures, LLVMFeatures: *llvmFeatures,
PrintJSON: flagJSON,
} }
if *printCommands { if *printCommands {
options.PrintCommands = printCommand options.PrintCommands = printCommand
@ -1532,7 +1542,7 @@ func main() {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
} }
if *flagJSON { if flagJSON {
json, _ := json.MarshalIndent(struct { json, _ := json.MarshalIndent(struct {
GOROOT string `json:"goroot"` GOROOT string `json:"goroot"`
GOOS string `json:"goos"` GOOS string `json:"goos"`
@ -1571,13 +1581,13 @@ func main() {
os.Exit(1) os.Exit(1)
} }
var extraArgs []string var extraArgs []string
if *flagJSON { if flagJSON {
extraArgs = append(extraArgs, "-json") extraArgs = append(extraArgs, "-json")
} }
if *flagDeps { if flagDeps {
extraArgs = append(extraArgs, "-deps") extraArgs = append(extraArgs, "-deps")
} }
if *flagTest { if flagTest {
extraArgs = append(extraArgs, "-test") extraArgs = append(extraArgs, "-test")
} }
cmd, err := loader.List(config, extraArgs, flag.Args()) cmd, err := loader.List(config, extraArgs, flag.Args())