From 9f5633d60ce6e25ffc0ee9366b8b7e6252606c11 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 19 Apr 2018 16:52:21 -0700 Subject: [PATCH 1/2] Add pom2mk -regen pom2mk -regen Android.mk will read the previous arguments out of Android.mk, rerun pom2mk with those arguments, and write the result to Android.mk. Bug: 78300023 Test: cd prebuilts/sdk/current/support && pom2mk -regen Android.mk Change-Id: Ie8e0f97ae5b87c8078cf8e753489326d80da1896 --- cmd/pom2mk/pom2mk.go | 81 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/cmd/pom2mk/pom2mk.go b/cmd/pom2mk/pom2mk.go index a635586ee..55ccb7bbe 100644 --- a/cmd/pom2mk/pom2mk.go +++ b/cmd/pom2mk/pom2mk.go @@ -15,11 +15,14 @@ package main import ( + "bufio" + "bytes" "encoding/xml" "flag" "fmt" "io/ioutil" "os" + "os/exec" "path/filepath" "regexp" "sort" @@ -271,6 +274,58 @@ func parse(filename string) (*Pom, error) { return &pom, nil } +func rerunForRegen(filename string) error { + buf, err := ioutil.ReadFile(filename) + if err != nil { + return err + } + + scanner := bufio.NewScanner(bytes.NewBuffer(buf)) + + // Skip the first line in the file + for i := 0; i < 2; i++ { + if !scanner.Scan() { + if scanner.Err() != nil { + return scanner.Err() + } else { + return fmt.Errorf("unexpected EOF") + } + } + } + + // Extract the old args from the file + line := scanner.Text() + if strings.HasPrefix(line, "# pom2mk ") { + line = strings.TrimPrefix(line, "# pom2mk ") + } else { + return fmt.Errorf("unexpected second line: %q", line) + } + args := strings.Split(line, " ") + lastArg := args[len(args)-1] + args = args[:len(args)-1] + + // Append all current command line args except -regen to the ones from the file + for i := 1; i < len(os.Args); i++ { + if os.Args[i] == "-regen" { + i++ + } else { + args = append(args, os.Args[i]) + } + } + args = append(args, lastArg) + + cmd := os.Args[0] + " " + strings.Join(args, " ") + // Re-exec pom2mk with the new arguments + output, err := exec.Command("/bin/sh", "-c", cmd).Output() + if exitErr, _ := err.(*exec.ExitError); exitErr != nil { + return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr)) + } else if err != nil { + return err + } + + return ioutil.WriteFile(filename, output, 0666) +} + func main() { flag.Usage = func() { fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos @@ -278,7 +333,7 @@ func main() { The tool will extract the necessary information from *.pom files to create an Android.mk whose aar libraries can be linked against when using AAPT2. -Usage: %s [--rewrite =] [--extra-deps =[,]] +Usage: %s [--rewrite =] [--extra-deps =[,]] [] [-regen ] -rewrite = rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite @@ -299,20 +354,36 @@ Usage: %s [--rewrite =] [--extra-deps =[, The directory to search for *.pom files under. - -The makefile is written to stdout, to be put in the current directory (often as Android.mk) + The makefile is written to stdout, to be put in the current directory (often as Android.mk) + -regen + Read arguments from and overwrite it. `, os.Args[0]) } + var regen string + flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module") flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names") flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION") flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version") flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies") + flag.StringVar(®en, "regen", "", "Rewrite specified file") flag.Parse() - if flag.NArg() != 1 { - flag.Usage() + if regen != "" { + err := rerunForRegen(regen) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + os.Exit(0) + } + + if flag.NArg() == 0 { + fmt.Fprintln(os.Stderr, "Directory argument is required") + os.Exit(1) + } else if flag.NArg() > 1 { + fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " ")) os.Exit(1) } From 6b382bcd041ed8cf7a1b26607ec14c8e89409954 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 19 Apr 2018 22:44:16 -0700 Subject: [PATCH 2/2] Add pom2mk -exclude Add an option to pom2mk to exclude modules by name. Bug: 78300023 Test: cd prebuilts/sdk/current/support && pom2mk -regen Android.mk -exclude androidx.car_car Change-Id: I083907ef364384aace524ced81820567f5075d76 --- cmd/pom2mk/pom2mk.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/pom2mk/pom2mk.go b/cmd/pom2mk/pom2mk.go index 55ccb7bbe..fc836411a 100644 --- a/cmd/pom2mk/pom2mk.go +++ b/cmd/pom2mk/pom2mk.go @@ -88,6 +88,19 @@ func (d ExtraDeps) Set(v string) error { var extraDeps = make(ExtraDeps) +type Exclude map[string]bool + +func (e Exclude) String() string { + return "" +} + +func (e Exclude) Set(v string) error { + e[v] = true + return nil +} + +var excludes = make(Exclude) + var sdkVersion string var useVersion string var staticDeps bool @@ -333,7 +346,7 @@ func main() { The tool will extract the necessary information from *.pom files to create an Android.mk whose aar libraries can be linked against when using AAPT2. -Usage: %s [--rewrite =] [--extra-deps =[,]] [] [-regen ] +Usage: %s [--rewrite =] [-exclude ] [--extra-deps =[,]] [] [-regen ] -rewrite = rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite @@ -341,6 +354,8 @@ Usage: %s [--rewrite =] [--extra-deps =[, matching either the Maven project's : or will be used to generate the Make module name using . If no matches are found, is used. + -exclude + Don't put the specified module in the makefile. -extra-deps =[,] Some Android.mk modules have transitive dependencies that must be specified when they are depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat). @@ -362,6 +377,7 @@ Usage: %s [--rewrite =] [--extra-deps =[,=] [--extra-deps =[,