blob: d341b8c104d176e824951e96f28a93800e6a5120 [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
Tony Mak81785002019-07-18 21:36:44 +0100127type HostAndDeviceModuleNames map[string]bool
128
129func (n HostAndDeviceModuleNames) IsHostAndDeviceModule(groupId string, artifactId string) bool {
130 _, found := n[groupId+":"+artifactId]
131
132 return found
133}
134
135func (n HostAndDeviceModuleNames) String() string {
136 return ""
137}
138
139func (n HostAndDeviceModuleNames) Set(v string) error {
140 n[v] = true
141 return nil
142}
143
144var hostAndDeviceModuleNames = HostAndDeviceModuleNames{}
145
Colin Cross70dd38f2018-04-16 13:52:10 -0700146var sdkVersion string
147var useVersion string
Dan Willemsen52c90d82019-04-21 21:37:39 -0700148var staticDeps bool
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700149var jetifier bool
Colin Cross70dd38f2018-04-16 13:52:10 -0700150
151func InList(s string, list []string) bool {
152 for _, l := range list {
153 if l == s {
154 return true
155 }
156 }
157
158 return false
159}
160
161type Dependency struct {
162 XMLName xml.Name `xml:"dependency"`
163
164 BpTarget string `xml:"-"`
165
166 GroupId string `xml:"groupId"`
167 ArtifactId string `xml:"artifactId"`
168 Version string `xml:"version"`
169 Type string `xml:"type"`
170 Scope string `xml:"scope"`
171}
172
173func (d Dependency) BpName() string {
174 if d.BpTarget == "" {
175 d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
176 }
177 return d.BpTarget
178}
179
180type Pom struct {
181 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
182
Colin Crosscf53e602018-06-26 15:27:20 -0700183 PomFile string `xml:"-"`
184 ArtifactFile string `xml:"-"`
185 BpTarget string `xml:"-"`
186 MinSdkVersion string `xml:"-"`
Colin Cross70dd38f2018-04-16 13:52:10 -0700187
188 GroupId string `xml:"groupId"`
189 ArtifactId string `xml:"artifactId"`
190 Version string `xml:"version"`
191 Packaging string `xml:"packaging"`
192
193 Dependencies []*Dependency `xml:"dependencies>dependency"`
194}
195
196func (p Pom) IsAar() bool {
197 return p.Packaging == "aar"
198}
199
200func (p Pom) IsJar() bool {
201 return p.Packaging == "jar"
202}
203
Jeff Gastond4928532018-08-24 14:30:13 -0400204func (p Pom) IsHostModule() bool {
205 return hostModuleNames.IsHostModule(p.GroupId, p.ArtifactId)
206}
207
208func (p Pom) IsDeviceModule() bool {
209 return !p.IsHostModule()
210}
211
Tony Mak81785002019-07-18 21:36:44 +0100212func (p Pom) IsHostAndDeviceModule() bool {
213 return hostAndDeviceModuleNames.IsHostAndDeviceModule(p.GroupId, p.ArtifactId)
214}
215
Jooyung Han43d30252020-05-22 03:54:24 +0900216func (p Pom) IsHostOnly() bool {
217 return p.IsHostModule() && !p.IsHostAndDeviceModule()
218}
219
Colin Cross632987a2018-08-29 16:17:55 -0700220func (p Pom) ModuleType() string {
221 if p.IsAar() {
222 return "android_library"
Jooyung Han43d30252020-05-22 03:54:24 +0900223 } else if p.IsHostOnly() {
Colin Cross632987a2018-08-29 16:17:55 -0700224 return "java_library_host"
225 } else {
226 return "java_library_static"
227 }
228}
229
230func (p Pom) ImportModuleType() string {
231 if p.IsAar() {
232 return "android_library_import"
Jooyung Han43d30252020-05-22 03:54:24 +0900233 } else if p.IsHostOnly() {
Colin Cross632987a2018-08-29 16:17:55 -0700234 return "java_import_host"
235 } else {
236 return "java_import"
237 }
238}
239
240func (p Pom) ImportProperty() string {
241 if p.IsAar() {
242 return "aars"
243 } else {
244 return "jars"
245 }
246}
247
Colin Cross70dd38f2018-04-16 13:52:10 -0700248func (p Pom) BpName() string {
249 if p.BpTarget == "" {
250 p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
251 }
252 return p.BpTarget
253}
254
255func (p Pom) BpJarDeps() []string {
256 return p.BpDeps("jar", []string{"compile", "runtime"})
257}
258
259func (p Pom) BpAarDeps() []string {
260 return p.BpDeps("aar", []string{"compile", "runtime"})
261}
262
Paul Duffinbabaf072019-04-16 11:35:20 +0100263func (p Pom) BpExtraStaticLibs() []string {
264 return extraStaticLibs[p.BpName()]
265}
266
267func (p Pom) BpExtraLibs() []string {
268 return extraLibs[p.BpName()]
Colin Cross70dd38f2018-04-16 13:52:10 -0700269}
270
271// BpDeps obtains dependencies filtered by type and scope. The results of this
272// method are formatted as Android.bp targets, e.g. run through MavenToBp rules.
273func (p Pom) BpDeps(typeExt string, scopes []string) []string {
274 var ret []string
275 for _, d := range p.Dependencies {
276 if d.Type != typeExt || !InList(d.Scope, scopes) {
277 continue
278 }
279 name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
280 ret = append(ret, name)
281 }
282 return ret
283}
284
285func (p Pom) SdkVersion() string {
286 return sdkVersion
287}
288
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700289func (p Pom) Jetifier() bool {
290 return jetifier
291}
292
Colin Cross70dd38f2018-04-16 13:52:10 -0700293func (p *Pom) FixDeps(modules map[string]*Pom) {
294 for _, d := range p.Dependencies {
295 if d.Type == "" {
296 if depPom, ok := modules[d.BpName()]; ok {
297 // We've seen the POM for this dependency, use its packaging
298 // as the dependency type rather than Maven spec default.
299 d.Type = depPom.Packaging
300 } else {
301 // Dependency type was not specified and we don't have the POM
302 // for this artifact, use the default from Maven spec.
303 d.Type = "jar"
304 }
305 }
306 if d.Scope == "" {
307 // Scope was not specified, use the default from Maven spec.
308 d.Scope = "compile"
309 }
310 }
311}
312
Colin Crosscf53e602018-06-26 15:27:20 -0700313// ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it
314// to "current" if it is not present.
315func (p *Pom) ExtractMinSdkVersion() error {
316 aar, err := zip.OpenReader(p.ArtifactFile)
317 if err != nil {
318 return err
319 }
320 defer aar.Close()
321
322 var manifest *zip.File
323 for _, f := range aar.File {
324 if f.Name == "AndroidManifest.xml" {
325 manifest = f
326 break
327 }
328 }
329
330 if manifest == nil {
331 return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile)
332 }
333
334 r, err := manifest.Open()
335 if err != nil {
336 return err
337 }
338 defer r.Close()
339
340 decoder := xml.NewDecoder(r)
341
342 manifestData := struct {
343 XMLName xml.Name `xml:"manifest"`
344 Uses_sdk struct {
345 MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"`
346 } `xml:"uses-sdk"`
347 }{}
348
349 err = decoder.Decode(&manifestData)
350 if err != nil {
351 return err
352 }
353
354 p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion
355 if p.MinSdkVersion == "" {
356 p.MinSdkVersion = "current"
357 }
358
359 return nil
360}
361
Colin Cross70dd38f2018-04-16 13:52:10 -0700362var bpTemplate = template.Must(template.New("bp").Parse(`
Colin Cross632987a2018-08-29 16:17:55 -0700363{{.ImportModuleType}} {
Dan Willemsen52c90d82019-04-21 21:37:39 -0700364 name: "{{.BpName}}",
365 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
366 sdk_version: "{{.SdkVersion}}",
367 {{- if .Jetifier}}
368 jetifier: true,
369 {{- end}}
Tony Mak81785002019-07-18 21:36:44 +0100370 {{- if .IsHostAndDeviceModule}}
371 host_supported: true,
372 {{- end}}
Jooyung Han43d30252020-05-22 03:54:24 +0900373 {{- if not .IsHostOnly}}
374 apex_available: [
375 "//apex_available:platform",
376 "//apex_available:anyapex",
377 ],
378 {{- end}}
Dan Willemsen52c90d82019-04-21 21:37:39 -0700379 {{- if .IsAar}}
380 min_sdk_version: "{{.MinSdkVersion}}",
381 static_libs: [
382 {{- range .BpJarDeps}}
383 "{{.}}",
384 {{- end}}
385 {{- range .BpAarDeps}}
386 "{{.}}",
387 {{- end}}
388 {{- range .BpExtraStaticLibs}}
389 "{{.}}",
390 {{- end}}
391 ],
392 {{- if .BpExtraLibs}}
393 libs: [
394 {{- range .BpExtraLibs}}
395 "{{.}}",
396 {{- end}}
397 ],
398 {{- end}}
399 {{- end}}
400}
401`))
402
403var bpDepsTemplate = template.Must(template.New("bp").Parse(`
404{{.ImportModuleType}} {
Colin Cross70dd38f2018-04-16 13:52:10 -0700405 name: "{{.BpName}}-nodeps",
Colin Cross632987a2018-08-29 16:17:55 -0700406 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
407 sdk_version: "{{.SdkVersion}}",
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700408 {{- if .Jetifier}}
409 jetifier: true,
410 {{- end}}
Tony Mak81785002019-07-18 21:36:44 +0100411 {{- if .IsHostAndDeviceModule}}
412 host_supported: true,
413 {{- end}}
Jooyung Han43d30252020-05-22 03:54:24 +0900414 {{- if not .IsHostOnly}}
415 apex_available: [
416 "//apex_available:platform",
417 "//apex_available:anyapex",
418 ],
419 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700420 {{- if .IsAar}}
Colin Crosscf53e602018-06-26 15:27:20 -0700421 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700422 static_libs: [
Colin Cross1aa7f262019-04-10 11:07:15 -0700423 {{- range .BpJarDeps}}
424 "{{.}}",
425 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700426 {{- range .BpAarDeps}}
427 "{{.}}",
428 {{- end}}
Paul Duffinbabaf072019-04-16 11:35:20 +0100429 {{- range .BpExtraStaticLibs}}
Colin Cross632987a2018-08-29 16:17:55 -0700430 "{{.}}",
431 {{- end}}
432 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100433 {{- if .BpExtraLibs}}
434 libs: [
435 {{- range .BpExtraLibs}}
436 "{{.}}",
437 {{- end}}
438 ],
439 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700440 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700441}
442
Colin Cross632987a2018-08-29 16:17:55 -0700443{{.ModuleType}} {
444 name: "{{.BpName}}",
445 {{- if .IsDeviceModule}}
446 sdk_version: "{{.SdkVersion}}",
Tony Mak81785002019-07-18 21:36:44 +0100447 {{- if .IsHostAndDeviceModule}}
448 host_supported: true,
449 {{- end}}
Jooyung Han43d30252020-05-22 03:54:24 +0900450 {{- if not .IsHostOnly}}
451 apex_available: [
452 "//apex_available:platform",
453 "//apex_available:anyapex",
454 ],
455 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700456 {{- if .IsAar}}
Colin Cross461ba492018-07-10 13:45:30 -0700457 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700458 manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
Jooyung Han43d30252020-05-22 03:54:24 +0900459 {{- else if not .IsHostOnly}}
460 min_sdk_version: "24",
Colin Cross632987a2018-08-29 16:17:55 -0700461 {{- end}}
462 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700463 static_libs: [
Colin Cross632987a2018-08-29 16:17:55 -0700464 "{{.BpName}}-nodeps",
Colin Cross1aa7f262019-04-10 11:07:15 -0700465 {{- range .BpJarDeps}}
Colin Cross632987a2018-08-29 16:17:55 -0700466 "{{.}}",
467 {{- end}}
468 {{- range .BpAarDeps}}
469 "{{.}}",
470 {{- end}}
Paul Duffinbabaf072019-04-16 11:35:20 +0100471 {{- range .BpExtraStaticLibs}}
Colin Cross632987a2018-08-29 16:17:55 -0700472 "{{.}}",
473 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700474 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100475 {{- if .BpExtraLibs}}
476 libs: [
477 {{- range .BpExtraLibs}}
478 "{{.}}",
479 {{- end}}
480 ],
481 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700482 java_version: "1.7",
483}
484`))
485
486func parse(filename string) (*Pom, error) {
487 data, err := ioutil.ReadFile(filename)
488 if err != nil {
489 return nil, err
490 }
491
492 var pom Pom
493 err = xml.Unmarshal(data, &pom)
494 if err != nil {
495 return nil, err
496 }
497
498 if useVersion != "" && pom.Version != useVersion {
499 return nil, nil
500 }
501
502 if pom.Packaging == "" {
503 pom.Packaging = "jar"
504 }
505
506 pom.PomFile = filename
507 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
508
509 return &pom, nil
510}
511
512func rerunForRegen(filename string) error {
513 buf, err := ioutil.ReadFile(filename)
514 if err != nil {
515 return err
516 }
517
518 scanner := bufio.NewScanner(bytes.NewBuffer(buf))
519
520 // Skip the first line in the file
521 for i := 0; i < 2; i++ {
522 if !scanner.Scan() {
523 if scanner.Err() != nil {
524 return scanner.Err()
525 } else {
526 return fmt.Errorf("unexpected EOF")
527 }
528 }
529 }
530
531 // Extract the old args from the file
532 line := scanner.Text()
533 if strings.HasPrefix(line, "// pom2bp ") {
534 line = strings.TrimPrefix(line, "// pom2bp ")
535 } else if strings.HasPrefix(line, "// pom2mk ") {
536 line = strings.TrimPrefix(line, "// pom2mk ")
537 } else if strings.HasPrefix(line, "# pom2mk ") {
538 line = strings.TrimPrefix(line, "# pom2mk ")
539 } else {
540 return fmt.Errorf("unexpected second line: %q", line)
541 }
542 args := strings.Split(line, " ")
543 lastArg := args[len(args)-1]
544 args = args[:len(args)-1]
545
546 // Append all current command line args except -regen <file> to the ones from the file
547 for i := 1; i < len(os.Args); i++ {
Colin Crosscf53e602018-06-26 15:27:20 -0700548 if os.Args[i] == "-regen" || os.Args[i] == "--regen" {
Colin Cross70dd38f2018-04-16 13:52:10 -0700549 i++
550 } else {
551 args = append(args, os.Args[i])
552 }
553 }
554 args = append(args, lastArg)
555
556 cmd := os.Args[0] + " " + strings.Join(args, " ")
557 // Re-exec pom2bp with the new arguments
558 output, err := exec.Command("/bin/sh", "-c", cmd).Output()
559 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
560 return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
561 } else if err != nil {
562 return err
563 }
564
565 // If the old file was a .mk file, replace it with a .bp file
566 if filepath.Ext(filename) == ".mk" {
567 os.Remove(filename)
568 filename = strings.TrimSuffix(filename, ".mk") + ".bp"
569 }
570
571 return ioutil.WriteFile(filename, output, 0666)
572}
573
574func main() {
575 flag.Usage = func() {
576 fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos
577
578The tool will extract the necessary information from *.pom files to create an Android.bp whose
579aar libraries can be linked against when using AAPT2.
580
Paul Duffinbabaf072019-04-16 11:35:20 +0100581Usage: %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 -0700582
583 -rewrite <regex>=<replace>
584 rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite
585 option can be specified multiple times. When determining the Android.bp module for a given Maven
586 project, mappings are searched in the order they were specified. The first <regex> matching
587 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
588 the Android.bp module name using <replace>. If no matches are found, <artifactId> is used.
589 -exclude <module>
590 Don't put the specified module in the Android.bp file.
Paul Duffinbabaf072019-04-16 11:35:20 +0100591 -extra-static-libs <module>=<module>[,<module>]
592 Some Android.bp modules have transitive static dependencies that must be specified when they
593 are depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
594 This may be specified multiple times to declare these dependencies.
595 -extra-libs <module>=<module>[,<module>]
596 Some Android.bp modules have transitive runtime dependencies that must be specified when they
597 are depended upon (like androidx.test.rules requires android.test.base).
Colin Cross70dd38f2018-04-16 13:52:10 -0700598 This may be specified multiple times to declare these dependencies.
599 -sdk-version <version>
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700600 Sets sdk_version: "<version>" for all modules.
Colin Cross70dd38f2018-04-16 13:52:10 -0700601 -use-version <version>
602 If the maven directory contains multiple versions of artifacts and their pom files,
603 -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 -0700604 -jetifier
605 Sets jetifier: true for all modules.
Colin Cross70dd38f2018-04-16 13:52:10 -0700606 <dir>
607 The directory to search for *.pom files under.
608 The contents are written to stdout, to be put in the current directory (often as Android.bp)
609 -regen <file>
610 Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it
611 ends with .mk).
612
613`, os.Args[0])
614 }
615
616 var regen string
617
618 flag.Var(&excludes, "exclude", "Exclude module")
Paul Duffinbabaf072019-04-16 11:35:20 +0100619 flag.Var(&extraStaticLibs, "extra-static-libs", "Extra static dependencies needed when depending on a module")
620 flag.Var(&extraLibs, "extra-libs", "Extra runtime dependencies needed when depending on a module")
Colin Cross70dd38f2018-04-16 13:52:10 -0700621 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Jeff Gastond4928532018-08-24 14:30:13 -0400622 flag.Var(&hostModuleNames, "host", "Specifies that the corresponding module (specified in the form 'module.group:module.artifact') is a host module")
Tony Mak81785002019-07-18 21:36:44 +0100623 flag.Var(&hostAndDeviceModuleNames, "host-and-device", "Specifies that the corresponding module (specified in the form 'module.group:module.artifact') is both a host and device module.")
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700624 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to sdk_version")
Colin Cross70dd38f2018-04-16 13:52:10 -0700625 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
Dan Willemsen52c90d82019-04-21 21:37:39 -0700626 flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700627 flag.BoolVar(&jetifier, "jetifier", false, "Sets jetifier: true on all modules")
Colin Cross70dd38f2018-04-16 13:52:10 -0700628 flag.StringVar(&regen, "regen", "", "Rewrite specified file")
629 flag.Parse()
630
631 if regen != "" {
632 err := rerunForRegen(regen)
633 if err != nil {
634 fmt.Fprintln(os.Stderr, err)
635 os.Exit(1)
636 }
637 os.Exit(0)
638 }
639
640 if flag.NArg() == 0 {
641 fmt.Fprintln(os.Stderr, "Directory argument is required")
642 os.Exit(1)
643 } else if flag.NArg() > 1 {
644 fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
645 os.Exit(1)
646 }
647
648 dir := flag.Arg(0)
649 absDir, err := filepath.Abs(dir)
650 if err != nil {
651 fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
652 os.Exit(1)
653 }
654
655 var filenames []string
656 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
657 if err != nil {
658 return err
659 }
660
661 name := info.Name()
662 if info.IsDir() {
663 if strings.HasPrefix(name, ".") {
664 return filepath.SkipDir
665 }
666 return nil
667 }
668
669 if strings.HasPrefix(name, ".") {
670 return nil
671 }
672
673 if strings.HasSuffix(name, ".pom") {
674 path, err = filepath.Rel(absDir, path)
675 if err != nil {
676 return err
677 }
678 filenames = append(filenames, filepath.Join(dir, path))
679 }
680 return nil
681 })
682 if err != nil {
683 fmt.Fprintln(os.Stderr, "Error walking files:", err)
684 os.Exit(1)
685 }
686
687 if len(filenames) == 0 {
688 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
689 os.Exit(1)
690 }
691
692 sort.Strings(filenames)
693
694 poms := []*Pom{}
695 modules := make(map[string]*Pom)
696 duplicate := false
697 for _, filename := range filenames {
698 pom, err := parse(filename)
699 if err != nil {
700 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
701 os.Exit(1)
702 }
703
704 if pom != nil {
705 key := pom.BpName()
706 if excludes[key] {
707 continue
708 }
709
710 if old, ok := modules[key]; ok {
711 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
712 duplicate = true
713 }
714
715 poms = append(poms, pom)
716 modules[key] = pom
717 }
718 }
719 if duplicate {
720 os.Exit(1)
721 }
722
723 for _, pom := range poms {
Colin Crosscf53e602018-06-26 15:27:20 -0700724 if pom.IsAar() {
725 err := pom.ExtractMinSdkVersion()
726 if err != nil {
Colin Crossfe5a3b72018-07-13 21:25:15 -0700727 fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err)
Colin Crosscf53e602018-06-26 15:27:20 -0700728 os.Exit(1)
729 }
730 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700731 pom.FixDeps(modules)
732 }
733
734 buf := &bytes.Buffer{}
735
736 fmt.Fprintln(buf, "// Automatically generated with:")
Colin Cross0b9f31f2019-02-28 11:00:01 -0800737 fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
Colin Cross70dd38f2018-04-16 13:52:10 -0700738
739 for _, pom := range poms {
740 var err error
Dan Willemsen52c90d82019-04-21 21:37:39 -0700741 if staticDeps {
742 err = bpDepsTemplate.Execute(buf, pom)
743 } else {
744 err = bpTemplate.Execute(buf, pom)
745 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700746 if err != nil {
747 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err)
748 os.Exit(1)
749 }
750 }
751
752 out, err := bpfix.Reformat(buf.String())
753 if err != nil {
754 fmt.Fprintln(os.Stderr, "Error formatting output", err)
755 os.Exit(1)
756 }
757
758 os.Stdout.WriteString(out)
759}