blob: c858c402cc2a792b0cbf061faa2aa386ae9a923d [file] [log] [blame]
Colin Cross70dd38f2018-04-16 13:52:10 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
Colin Crosscf53e602018-06-26 15:27:20 -070018 "archive/zip"
Colin Cross70dd38f2018-04-16 13:52:10 -070019 "bufio"
20 "bytes"
21 "encoding/xml"
22 "flag"
23 "fmt"
24 "io/ioutil"
25 "os"
26 "os/exec"
27 "path/filepath"
28 "regexp"
29 "sort"
30 "strings"
31 "text/template"
32
33 "github.com/google/blueprint/proptools"
34
35 "android/soong/bpfix/bpfix"
36)
37
38type RewriteNames []RewriteName
39type RewriteName struct {
40 regexp *regexp.Regexp
41 repl string
42}
43
44func (r *RewriteNames) String() string {
45 return ""
46}
47
48func (r *RewriteNames) Set(v string) error {
49 split := strings.SplitN(v, "=", 2)
50 if len(split) != 2 {
51 return fmt.Errorf("Must be in the form of <regex>=<replace>")
52 }
53 regex, err := regexp.Compile(split[0])
54 if err != nil {
55 return nil
56 }
57 *r = append(*r, RewriteName{
58 regexp: regex,
59 repl: split[1],
60 })
61 return nil
62}
63
64func (r *RewriteNames) MavenToBp(groupId string, artifactId string) string {
65 for _, r := range *r {
66 if r.regexp.MatchString(groupId + ":" + artifactId) {
67 return r.regexp.ReplaceAllString(groupId+":"+artifactId, r.repl)
68 } else if r.regexp.MatchString(artifactId) {
69 return r.regexp.ReplaceAllString(artifactId, r.repl)
70 }
71 }
72 return artifactId
73}
74
75var rewriteNames = RewriteNames{}
76
77type ExtraDeps map[string][]string
78
79func (d ExtraDeps) String() string {
80 return ""
81}
82
83func (d ExtraDeps) Set(v string) error {
84 split := strings.SplitN(v, "=", 2)
85 if len(split) != 2 {
86 return fmt.Errorf("Must be in the form of <module>=<module>[,<module>]")
87 }
88 d[split[0]] = strings.Split(split[1], ",")
89 return nil
90}
91
Paul Duffinbabaf072019-04-16 11:35:20 +010092var extraStaticLibs = make(ExtraDeps)
93
94var extraLibs = make(ExtraDeps)
Colin Cross70dd38f2018-04-16 13:52:10 -070095
96type Exclude map[string]bool
97
98func (e Exclude) String() string {
99 return ""
100}
101
102func (e Exclude) Set(v string) error {
103 e[v] = true
104 return nil
105}
106
107var excludes = make(Exclude)
108
Jeff Gastond4928532018-08-24 14:30:13 -0400109type HostModuleNames map[string]bool
110
111func (n HostModuleNames) IsHostModule(groupId string, artifactId string) bool {
Colin Cross86bc9d42018-08-29 15:36:33 -0700112 _, found := n[groupId+":"+artifactId]
Jeff Gastond4928532018-08-24 14:30:13 -0400113 return found
114}
115
116func (n HostModuleNames) String() string {
117 return ""
118}
119
120func (n HostModuleNames) Set(v string) error {
121 n[v] = true
122 return nil
123}
124
125var hostModuleNames = HostModuleNames{}
126
Colin Cross70dd38f2018-04-16 13:52:10 -0700127var sdkVersion string
128var useVersion string
Dan Willemsen52c90d82019-04-21 21:37:39 -0700129var staticDeps bool
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700130var jetifier bool
Colin Cross70dd38f2018-04-16 13:52:10 -0700131
132func InList(s string, list []string) bool {
133 for _, l := range list {
134 if l == s {
135 return true
136 }
137 }
138
139 return false
140}
141
142type Dependency struct {
143 XMLName xml.Name `xml:"dependency"`
144
145 BpTarget string `xml:"-"`
146
147 GroupId string `xml:"groupId"`
148 ArtifactId string `xml:"artifactId"`
149 Version string `xml:"version"`
150 Type string `xml:"type"`
151 Scope string `xml:"scope"`
152}
153
154func (d Dependency) BpName() string {
155 if d.BpTarget == "" {
156 d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
157 }
158 return d.BpTarget
159}
160
161type Pom struct {
162 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
163
Colin Crosscf53e602018-06-26 15:27:20 -0700164 PomFile string `xml:"-"`
165 ArtifactFile string `xml:"-"`
166 BpTarget string `xml:"-"`
167 MinSdkVersion string `xml:"-"`
Colin Cross70dd38f2018-04-16 13:52:10 -0700168
169 GroupId string `xml:"groupId"`
170 ArtifactId string `xml:"artifactId"`
171 Version string `xml:"version"`
172 Packaging string `xml:"packaging"`
173
174 Dependencies []*Dependency `xml:"dependencies>dependency"`
175}
176
177func (p Pom) IsAar() bool {
178 return p.Packaging == "aar"
179}
180
181func (p Pom) IsJar() bool {
182 return p.Packaging == "jar"
183}
184
Jeff Gastond4928532018-08-24 14:30:13 -0400185func (p Pom) IsHostModule() bool {
186 return hostModuleNames.IsHostModule(p.GroupId, p.ArtifactId)
187}
188
189func (p Pom) IsDeviceModule() bool {
190 return !p.IsHostModule()
191}
192
Colin Cross632987a2018-08-29 16:17:55 -0700193func (p Pom) ModuleType() string {
194 if p.IsAar() {
195 return "android_library"
196 } else if p.IsHostModule() {
197 return "java_library_host"
198 } else {
199 return "java_library_static"
200 }
201}
202
203func (p Pom) ImportModuleType() string {
204 if p.IsAar() {
205 return "android_library_import"
206 } else if p.IsHostModule() {
207 return "java_import_host"
208 } else {
209 return "java_import"
210 }
211}
212
213func (p Pom) ImportProperty() string {
214 if p.IsAar() {
215 return "aars"
216 } else {
217 return "jars"
218 }
219}
220
Colin Cross70dd38f2018-04-16 13:52:10 -0700221func (p Pom) BpName() string {
222 if p.BpTarget == "" {
223 p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
224 }
225 return p.BpTarget
226}
227
228func (p Pom) BpJarDeps() []string {
229 return p.BpDeps("jar", []string{"compile", "runtime"})
230}
231
232func (p Pom) BpAarDeps() []string {
233 return p.BpDeps("aar", []string{"compile", "runtime"})
234}
235
Paul Duffinbabaf072019-04-16 11:35:20 +0100236func (p Pom) BpExtraStaticLibs() []string {
237 return extraStaticLibs[p.BpName()]
238}
239
240func (p Pom) BpExtraLibs() []string {
241 return extraLibs[p.BpName()]
Colin Cross70dd38f2018-04-16 13:52:10 -0700242}
243
244// BpDeps obtains dependencies filtered by type and scope. The results of this
245// method are formatted as Android.bp targets, e.g. run through MavenToBp rules.
246func (p Pom) BpDeps(typeExt string, scopes []string) []string {
247 var ret []string
248 for _, d := range p.Dependencies {
249 if d.Type != typeExt || !InList(d.Scope, scopes) {
250 continue
251 }
252 name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
253 ret = append(ret, name)
254 }
255 return ret
256}
257
258func (p Pom) SdkVersion() string {
259 return sdkVersion
260}
261
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700262func (p Pom) Jetifier() bool {
263 return jetifier
264}
265
Colin Cross70dd38f2018-04-16 13:52:10 -0700266func (p *Pom) FixDeps(modules map[string]*Pom) {
267 for _, d := range p.Dependencies {
268 if d.Type == "" {
269 if depPom, ok := modules[d.BpName()]; ok {
270 // We've seen the POM for this dependency, use its packaging
271 // as the dependency type rather than Maven spec default.
272 d.Type = depPom.Packaging
273 } else {
274 // Dependency type was not specified and we don't have the POM
275 // for this artifact, use the default from Maven spec.
276 d.Type = "jar"
277 }
278 }
279 if d.Scope == "" {
280 // Scope was not specified, use the default from Maven spec.
281 d.Scope = "compile"
282 }
283 }
284}
285
Colin Crosscf53e602018-06-26 15:27:20 -0700286// ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it
287// to "current" if it is not present.
288func (p *Pom) ExtractMinSdkVersion() error {
289 aar, err := zip.OpenReader(p.ArtifactFile)
290 if err != nil {
291 return err
292 }
293 defer aar.Close()
294
295 var manifest *zip.File
296 for _, f := range aar.File {
297 if f.Name == "AndroidManifest.xml" {
298 manifest = f
299 break
300 }
301 }
302
303 if manifest == nil {
304 return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile)
305 }
306
307 r, err := manifest.Open()
308 if err != nil {
309 return err
310 }
311 defer r.Close()
312
313 decoder := xml.NewDecoder(r)
314
315 manifestData := struct {
316 XMLName xml.Name `xml:"manifest"`
317 Uses_sdk struct {
318 MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"`
319 } `xml:"uses-sdk"`
320 }{}
321
322 err = decoder.Decode(&manifestData)
323 if err != nil {
324 return err
325 }
326
327 p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion
328 if p.MinSdkVersion == "" {
329 p.MinSdkVersion = "current"
330 }
331
332 return nil
333}
334
Colin Cross70dd38f2018-04-16 13:52:10 -0700335var bpTemplate = template.Must(template.New("bp").Parse(`
Colin Cross632987a2018-08-29 16:17:55 -0700336{{.ImportModuleType}} {
Dan Willemsen52c90d82019-04-21 21:37:39 -0700337 name: "{{.BpName}}",
338 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
339 sdk_version: "{{.SdkVersion}}",
340 {{- if .Jetifier}}
341 jetifier: true,
342 {{- end}}
343 {{- if .IsAar}}
344 min_sdk_version: "{{.MinSdkVersion}}",
345 static_libs: [
346 {{- range .BpJarDeps}}
347 "{{.}}",
348 {{- end}}
349 {{- range .BpAarDeps}}
350 "{{.}}",
351 {{- end}}
352 {{- range .BpExtraStaticLibs}}
353 "{{.}}",
354 {{- end}}
355 ],
356 {{- if .BpExtraLibs}}
357 libs: [
358 {{- range .BpExtraLibs}}
359 "{{.}}",
360 {{- end}}
361 ],
362 {{- end}}
363 {{- end}}
364}
365`))
366
367var bpDepsTemplate = template.Must(template.New("bp").Parse(`
368{{.ImportModuleType}} {
Colin Cross70dd38f2018-04-16 13:52:10 -0700369 name: "{{.BpName}}-nodeps",
Colin Cross632987a2018-08-29 16:17:55 -0700370 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
371 sdk_version: "{{.SdkVersion}}",
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700372 {{- if .Jetifier}}
373 jetifier: true,
374 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700375 {{- if .IsAar}}
Colin Crosscf53e602018-06-26 15:27:20 -0700376 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700377 static_libs: [
Colin Cross1aa7f262019-04-10 11:07:15 -0700378 {{- range .BpJarDeps}}
379 "{{.}}",
380 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700381 {{- range .BpAarDeps}}
382 "{{.}}",
383 {{- end}}
Paul Duffinbabaf072019-04-16 11:35:20 +0100384 {{- range .BpExtraStaticLibs}}
Colin Cross632987a2018-08-29 16:17:55 -0700385 "{{.}}",
386 {{- end}}
387 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100388 {{- if .BpExtraLibs}}
389 libs: [
390 {{- range .BpExtraLibs}}
391 "{{.}}",
392 {{- end}}
393 ],
394 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700395 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700396}
397
Colin Cross632987a2018-08-29 16:17:55 -0700398{{.ModuleType}} {
399 name: "{{.BpName}}",
400 {{- if .IsDeviceModule}}
401 sdk_version: "{{.SdkVersion}}",
402 {{- if .IsAar}}
Colin Cross461ba492018-07-10 13:45:30 -0700403 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700404 manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
405 {{- end}}
406 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700407 static_libs: [
Colin Cross632987a2018-08-29 16:17:55 -0700408 "{{.BpName}}-nodeps",
Colin Cross1aa7f262019-04-10 11:07:15 -0700409 {{- range .BpJarDeps}}
Colin Cross632987a2018-08-29 16:17:55 -0700410 "{{.}}",
411 {{- end}}
412 {{- range .BpAarDeps}}
413 "{{.}}",
414 {{- end}}
Paul Duffinbabaf072019-04-16 11:35:20 +0100415 {{- range .BpExtraStaticLibs}}
Colin Cross632987a2018-08-29 16:17:55 -0700416 "{{.}}",
417 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700418 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100419 {{- if .BpExtraLibs}}
420 libs: [
421 {{- range .BpExtraLibs}}
422 "{{.}}",
423 {{- end}}
424 ],
425 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700426 java_version: "1.7",
427}
428`))
429
430func parse(filename string) (*Pom, error) {
431 data, err := ioutil.ReadFile(filename)
432 if err != nil {
433 return nil, err
434 }
435
436 var pom Pom
437 err = xml.Unmarshal(data, &pom)
438 if err != nil {
439 return nil, err
440 }
441
442 if useVersion != "" && pom.Version != useVersion {
443 return nil, nil
444 }
445
446 if pom.Packaging == "" {
447 pom.Packaging = "jar"
448 }
449
450 pom.PomFile = filename
451 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
452
453 return &pom, nil
454}
455
456func rerunForRegen(filename string) error {
457 buf, err := ioutil.ReadFile(filename)
458 if err != nil {
459 return err
460 }
461
462 scanner := bufio.NewScanner(bytes.NewBuffer(buf))
463
464 // Skip the first line in the file
465 for i := 0; i < 2; i++ {
466 if !scanner.Scan() {
467 if scanner.Err() != nil {
468 return scanner.Err()
469 } else {
470 return fmt.Errorf("unexpected EOF")
471 }
472 }
473 }
474
475 // Extract the old args from the file
476 line := scanner.Text()
477 if strings.HasPrefix(line, "// pom2bp ") {
478 line = strings.TrimPrefix(line, "// pom2bp ")
479 } else if strings.HasPrefix(line, "// pom2mk ") {
480 line = strings.TrimPrefix(line, "// pom2mk ")
481 } else if strings.HasPrefix(line, "# pom2mk ") {
482 line = strings.TrimPrefix(line, "# pom2mk ")
483 } else {
484 return fmt.Errorf("unexpected second line: %q", line)
485 }
486 args := strings.Split(line, " ")
487 lastArg := args[len(args)-1]
488 args = args[:len(args)-1]
489
490 // Append all current command line args except -regen <file> to the ones from the file
491 for i := 1; i < len(os.Args); i++ {
Colin Crosscf53e602018-06-26 15:27:20 -0700492 if os.Args[i] == "-regen" || os.Args[i] == "--regen" {
Colin Cross70dd38f2018-04-16 13:52:10 -0700493 i++
494 } else {
495 args = append(args, os.Args[i])
496 }
497 }
498 args = append(args, lastArg)
499
500 cmd := os.Args[0] + " " + strings.Join(args, " ")
501 // Re-exec pom2bp with the new arguments
502 output, err := exec.Command("/bin/sh", "-c", cmd).Output()
503 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
504 return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
505 } else if err != nil {
506 return err
507 }
508
509 // If the old file was a .mk file, replace it with a .bp file
510 if filepath.Ext(filename) == ".mk" {
511 os.Remove(filename)
512 filename = strings.TrimSuffix(filename, ".mk") + ".bp"
513 }
514
515 return ioutil.WriteFile(filename, output, 0666)
516}
517
518func main() {
519 flag.Usage = func() {
520 fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos
521
522The tool will extract the necessary information from *.pom files to create an Android.bp whose
523aar libraries can be linked against when using AAPT2.
524
Paul Duffinbabaf072019-04-16 11:35:20 +0100525Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-static-libs <module>=<module>[,<module>]] [--extra-libs <module>=<module>[,<module>]] [<dir>] [-regen <file>]
Colin Cross70dd38f2018-04-16 13:52:10 -0700526
527 -rewrite <regex>=<replace>
528 rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite
529 option can be specified multiple times. When determining the Android.bp module for a given Maven
530 project, mappings are searched in the order they were specified. The first <regex> matching
531 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
532 the Android.bp module name using <replace>. If no matches are found, <artifactId> is used.
533 -exclude <module>
534 Don't put the specified module in the Android.bp file.
Paul Duffinbabaf072019-04-16 11:35:20 +0100535 -extra-static-libs <module>=<module>[,<module>]
536 Some Android.bp modules have transitive static dependencies that must be specified when they
537 are depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
538 This may be specified multiple times to declare these dependencies.
539 -extra-libs <module>=<module>[,<module>]
540 Some Android.bp modules have transitive runtime dependencies that must be specified when they
541 are depended upon (like androidx.test.rules requires android.test.base).
Colin Cross70dd38f2018-04-16 13:52:10 -0700542 This may be specified multiple times to declare these dependencies.
543 -sdk-version <version>
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700544 Sets sdk_version: "<version>" for all modules.
Colin Cross70dd38f2018-04-16 13:52:10 -0700545 -use-version <version>
546 If the maven directory contains multiple versions of artifacts and their pom files,
547 -use-version can be used to only write Android.bp files for a specific version of those artifacts.
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700548 -jetifier
549 Sets jetifier: true for all modules.
Colin Cross70dd38f2018-04-16 13:52:10 -0700550 <dir>
551 The directory to search for *.pom files under.
552 The contents are written to stdout, to be put in the current directory (often as Android.bp)
553 -regen <file>
554 Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it
555 ends with .mk).
556
557`, os.Args[0])
558 }
559
560 var regen string
561
562 flag.Var(&excludes, "exclude", "Exclude module")
Paul Duffinbabaf072019-04-16 11:35:20 +0100563 flag.Var(&extraStaticLibs, "extra-static-libs", "Extra static dependencies needed when depending on a module")
564 flag.Var(&extraLibs, "extra-libs", "Extra runtime dependencies needed when depending on a module")
Colin Cross70dd38f2018-04-16 13:52:10 -0700565 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Jeff Gastond4928532018-08-24 14:30:13 -0400566 flag.Var(&hostModuleNames, "host", "Specifies that the corresponding module (specified in the form 'module.group:module.artifact') is a host module")
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700567 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to sdk_version")
Colin Cross70dd38f2018-04-16 13:52:10 -0700568 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
Dan Willemsen52c90d82019-04-21 21:37:39 -0700569 flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700570 flag.BoolVar(&jetifier, "jetifier", false, "Sets jetifier: true on all modules")
Colin Cross70dd38f2018-04-16 13:52:10 -0700571 flag.StringVar(&regen, "regen", "", "Rewrite specified file")
572 flag.Parse()
573
574 if regen != "" {
575 err := rerunForRegen(regen)
576 if err != nil {
577 fmt.Fprintln(os.Stderr, err)
578 os.Exit(1)
579 }
580 os.Exit(0)
581 }
582
583 if flag.NArg() == 0 {
584 fmt.Fprintln(os.Stderr, "Directory argument is required")
585 os.Exit(1)
586 } else if flag.NArg() > 1 {
587 fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
588 os.Exit(1)
589 }
590
591 dir := flag.Arg(0)
592 absDir, err := filepath.Abs(dir)
593 if err != nil {
594 fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
595 os.Exit(1)
596 }
597
598 var filenames []string
599 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
600 if err != nil {
601 return err
602 }
603
604 name := info.Name()
605 if info.IsDir() {
606 if strings.HasPrefix(name, ".") {
607 return filepath.SkipDir
608 }
609 return nil
610 }
611
612 if strings.HasPrefix(name, ".") {
613 return nil
614 }
615
616 if strings.HasSuffix(name, ".pom") {
617 path, err = filepath.Rel(absDir, path)
618 if err != nil {
619 return err
620 }
621 filenames = append(filenames, filepath.Join(dir, path))
622 }
623 return nil
624 })
625 if err != nil {
626 fmt.Fprintln(os.Stderr, "Error walking files:", err)
627 os.Exit(1)
628 }
629
630 if len(filenames) == 0 {
631 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
632 os.Exit(1)
633 }
634
635 sort.Strings(filenames)
636
637 poms := []*Pom{}
638 modules := make(map[string]*Pom)
639 duplicate := false
640 for _, filename := range filenames {
641 pom, err := parse(filename)
642 if err != nil {
643 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
644 os.Exit(1)
645 }
646
647 if pom != nil {
648 key := pom.BpName()
649 if excludes[key] {
650 continue
651 }
652
653 if old, ok := modules[key]; ok {
654 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
655 duplicate = true
656 }
657
658 poms = append(poms, pom)
659 modules[key] = pom
660 }
661 }
662 if duplicate {
663 os.Exit(1)
664 }
665
666 for _, pom := range poms {
Colin Crosscf53e602018-06-26 15:27:20 -0700667 if pom.IsAar() {
668 err := pom.ExtractMinSdkVersion()
669 if err != nil {
Colin Crossfe5a3b72018-07-13 21:25:15 -0700670 fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err)
Colin Crosscf53e602018-06-26 15:27:20 -0700671 os.Exit(1)
672 }
673 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700674 pom.FixDeps(modules)
675 }
676
677 buf := &bytes.Buffer{}
678
679 fmt.Fprintln(buf, "// Automatically generated with:")
Colin Cross0b9f31f2019-02-28 11:00:01 -0800680 fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
Colin Cross70dd38f2018-04-16 13:52:10 -0700681
682 for _, pom := range poms {
683 var err error
Dan Willemsen52c90d82019-04-21 21:37:39 -0700684 if staticDeps {
685 err = bpDepsTemplate.Execute(buf, pom)
686 } else {
687 err = bpTemplate.Execute(buf, pom)
688 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700689 if err != nil {
690 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err)
691 os.Exit(1)
692 }
693 }
694
695 out, err := bpfix.Reformat(buf.String())
696 if err != nil {
697 fmt.Fprintln(os.Stderr, "Error formatting output", err)
698 os.Exit(1)
699 }
700
701 os.Stdout.WriteString(out)
702}