From 675564daa8ed492a76247fa7086d58de2fcd6e52 Mon Sep 17 00:00:00 2001 From: Usta Shrestha Date: Tue, 9 Aug 2022 18:03:23 -0400 Subject: [PATCH] Cosmetic: More helpful error message Test: various invocations of ./build/soong/soong_ui.bash Bug: N/A Change-Id: I7be5b84330bc7525b5633314a111e5e1fa9fc844 --- cmd/soong_ui/main.go | 51 ++++++++++++++++++++++++++++++-------------- ui/build/build.go | 15 +++++++++++++ 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/cmd/soong_ui/main.go b/cmd/soong_ui/main.go index 3d18849b9..cb8563488 100644 --- a/cmd/soong_ui/main.go +++ b/cmd/soong_ui/main.go @@ -268,6 +268,8 @@ func fixBadDanglingLink(ctx build.Context, name string) { func dumpVar(ctx build.Context, config build.Config, args []string, _ string) { flags := flag.NewFlagSet("dumpvar", flag.ExitOnError) + flags.SetOutput(ctx.Writer) + flags.Usage = func() { fmt.Fprintf(ctx.Writer, "usage: %s --dumpvar-mode [--abs] \n\n", os.Args[0]) fmt.Fprintln(ctx.Writer, "In dumpvar mode, print the value of the legacy make variable VAR to stdout") @@ -318,6 +320,8 @@ func dumpVar(ctx build.Context, config build.Config, args []string, _ string) { func dumpVars(ctx build.Context, config build.Config, args []string, _ string) { flags := flag.NewFlagSet("dumpvars", flag.ExitOnError) + flags.SetOutput(ctx.Writer) + flags.Usage = func() { fmt.Fprintf(ctx.Writer, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0]) fmt.Fprintln(ctx.Writer, "In dumpvars mode, dump the values of one or more legacy make variables, in") @@ -401,6 +405,8 @@ func dumpVarConfig(ctx build.Context, args ...string) build.Config { func buildActionConfig(ctx build.Context, args ...string) build.Config { flags := flag.NewFlagSet("build-mode", flag.ContinueOnError) + flags.SetOutput(ctx.Writer) + flags.Usage = func() { fmt.Fprintf(ctx.Writer, "usage: %s --build-mode --dir= [ ...]\n\n", os.Args[0]) fmt.Fprintln(ctx.Writer, "In build mode, build the set of modules based on the specified build") @@ -453,21 +459,32 @@ func buildActionConfig(ctx build.Context, args ...string) build.Config { const numBuildActionFlags = 2 if len(args) < numBuildActionFlags { flags.Usage() - ctx.Fatalln("Improper build action arguments.") + ctx.Fatalln("Improper build action arguments: too few arguments") } - flags.Parse(args[0:numBuildActionFlags]) + parseError := flags.Parse(args[0:numBuildActionFlags]) // The next block of code is to validate that exactly one build action is set and the dir flag // is specified. - buildActionCount := 0 + buildActionFound := false var buildAction build.BuildAction - for _, flag := range buildActionFlags { - if flag.set { - buildActionCount++ - buildAction = flag.action + for _, f := range buildActionFlags { + if f.set { + if buildActionFound { + if parseError == nil { + //otherwise Parse() already called Usage() + flags.Usage() + } + ctx.Fatalf("Build action already specified, omit: --%s\n", f.name) + } + buildActionFound = true + buildAction = f.action } } - if buildActionCount != 1 { + if !buildActionFound { + if parseError == nil { + //otherwise Parse() already called Usage() + flags.Usage() + } ctx.Fatalln("Build action not defined.") } if *dir == "" { @@ -509,8 +526,16 @@ func runMake(ctx build.Context, config build.Config, _ []string, logsDir string) // getCommand finds the appropriate command based on args[1] flag. args[0] // is the soong_ui filename. func getCommand(args []string) (*command, []string, error) { + listFlags := func() []string { + flags := make([]string, len(commands)) + for i, c := range commands { + flags[i] = c.flag + } + return flags + } + if len(args) < 2 { - return nil, nil, fmt.Errorf("Too few arguments: %q", args) + return nil, nil, fmt.Errorf("Too few arguments: %q\nUse one of these: %q", args, listFlags()) } for _, c := range commands { @@ -518,13 +543,7 @@ func getCommand(args []string) (*command, []string, error) { return &c, args[2:], nil } } - - // command not found - flags := make([]string, len(commands)) - for i, c := range commands { - flags[i] = c.flag - } - return nil, nil, fmt.Errorf("Command not found: %q\nDid you mean one of these: %q", args, flags) + return nil, nil, fmt.Errorf("Command not found: %q\nDid you mean one of these: %q", args[1], listFlags()) } // For Bazel support, this moves files and directories from e.g. out/dist/$f to DIST_DIR/$f if necessary. diff --git a/ui/build/build.go b/ui/build/build.go index 5b80b4df8..f7a2d7b18 100644 --- a/ui/build/build.go +++ b/ui/build/build.go @@ -201,7 +201,20 @@ func Build(ctx Context, config Config) { buildLock := BecomeSingletonOrFail(ctx, config) defer buildLock.Unlock() + logArgsOtherThan := func(specialTargets ...string) { + var ignored []string + for _, a := range config.Arguments() { + if !inList(a, specialTargets) { + ignored = append(ignored, a) + } + } + if len(ignored) > 0 { + ctx.Printf("ignoring arguments %q", ignored) + } + } + if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) { + logArgsOtherThan("clean", "clobber") clean(ctx, config) return } @@ -279,6 +292,7 @@ func Build(ctx Context, config Config) { if inList("installclean", config.Arguments()) || inList("install-clean", config.Arguments()) { + logArgsOtherThan("installclean", "install-clean") installClean(ctx, config) ctx.Println("Deleted images and staging directories.") return @@ -286,6 +300,7 @@ func Build(ctx Context, config Config) { if inList("dataclean", config.Arguments()) || inList("data-clean", config.Arguments()) { + logArgsOtherThan("dataclean", "data-clean") dataClean(ctx, config) ctx.Println("Deleted data files.") return