blob: a399b28345b8c817b103b26e55d820b056950728 [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
92var extraDeps = make(ExtraDeps)
93
94type Exclude map[string]bool
95
96func (e Exclude) String() string {
97 return ""
98}
99
100func (e Exclude) Set(v string) error {
101 e[v] = true
102 return nil
103}
104
105var excludes = make(Exclude)
106
Jeff Gastond4928532018-08-24 14:30:13 -0400107type HostModuleNames map[string]bool
108
109func (n HostModuleNames) IsHostModule(groupId string, artifactId string) bool {
Colin Cross86bc9d42018-08-29 15:36:33 -0700110 _, found := n[groupId+":"+artifactId]
Jeff Gastond4928532018-08-24 14:30:13 -0400111 return found
112}
113
114func (n HostModuleNames) String() string {
115 return ""
116}
117
118func (n HostModuleNames) Set(v string) error {
119 n[v] = true
120 return nil
121}
122
123var hostModuleNames = HostModuleNames{}
124
Colin Cross70dd38f2018-04-16 13:52:10 -0700125var sdkVersion string
126var useVersion string
127
128func InList(s string, list []string) bool {
129 for _, l := range list {
130 if l == s {
131 return true
132 }
133 }
134
135 return false
136}
137
138type Dependency struct {
139 XMLName xml.Name `xml:"dependency"`
140
141 BpTarget string `xml:"-"`
142
143 GroupId string `xml:"groupId"`
144 ArtifactId string `xml:"artifactId"`
145 Version string `xml:"version"`
146 Type string `xml:"type"`
147 Scope string `xml:"scope"`
148}
149
150func (d Dependency) BpName() string {
151 if d.BpTarget == "" {
152 d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
153 }
154 return d.BpTarget
155}
156
157type Pom struct {
158 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
159
Colin Crosscf53e602018-06-26 15:27:20 -0700160 PomFile string `xml:"-"`
161 ArtifactFile string `xml:"-"`
162 BpTarget string `xml:"-"`
163 MinSdkVersion string `xml:"-"`
Colin Cross70dd38f2018-04-16 13:52:10 -0700164
165 GroupId string `xml:"groupId"`
166 ArtifactId string `xml:"artifactId"`
167 Version string `xml:"version"`
168 Packaging string `xml:"packaging"`
169
170 Dependencies []*Dependency `xml:"dependencies>dependency"`
171}
172
173func (p Pom) IsAar() bool {
174 return p.Packaging == "aar"
175}
176
177func (p Pom) IsJar() bool {
178 return p.Packaging == "jar"
179}
180
Jeff Gastond4928532018-08-24 14:30:13 -0400181func (p Pom) IsHostModule() bool {
182 return hostModuleNames.IsHostModule(p.GroupId, p.ArtifactId)
183}
184
185func (p Pom) IsDeviceModule() bool {
186 return !p.IsHostModule()
187}
188
Colin Cross632987a2018-08-29 16:17:55 -0700189func (p Pom) ModuleType() string {
190 if p.IsAar() {
191 return "android_library"
192 } else if p.IsHostModule() {
193 return "java_library_host"
194 } else {
195 return "java_library_static"
196 }
197}
198
199func (p Pom) ImportModuleType() string {
200 if p.IsAar() {
201 return "android_library_import"
202 } else if p.IsHostModule() {
203 return "java_import_host"
204 } else {
205 return "java_import"
206 }
207}
208
209func (p Pom) ImportProperty() string {
210 if p.IsAar() {
211 return "aars"
212 } else {
213 return "jars"
214 }
215}
216
Colin Cross70dd38f2018-04-16 13:52:10 -0700217func (p Pom) BpName() string {
218 if p.BpTarget == "" {
219 p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
220 }
221 return p.BpTarget
222}
223
224func (p Pom) BpJarDeps() []string {
225 return p.BpDeps("jar", []string{"compile", "runtime"})
226}
227
228func (p Pom) BpAarDeps() []string {
229 return p.BpDeps("aar", []string{"compile", "runtime"})
230}
231
232func (p Pom) BpExtraDeps() []string {
233 return extraDeps[p.BpName()]
234}
235
236// BpDeps obtains dependencies filtered by type and scope. The results of this
237// method are formatted as Android.bp targets, e.g. run through MavenToBp rules.
238func (p Pom) BpDeps(typeExt string, scopes []string) []string {
239 var ret []string
240 for _, d := range p.Dependencies {
241 if d.Type != typeExt || !InList(d.Scope, scopes) {
242 continue
243 }
244 name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
245 ret = append(ret, name)
246 }
247 return ret
248}
249
250func (p Pom) SdkVersion() string {
251 return sdkVersion
252}
253
254func (p *Pom) FixDeps(modules map[string]*Pom) {
255 for _, d := range p.Dependencies {
256 if d.Type == "" {
257 if depPom, ok := modules[d.BpName()]; ok {
258 // We've seen the POM for this dependency, use its packaging
259 // as the dependency type rather than Maven spec default.
260 d.Type = depPom.Packaging
261 } else {
262 // Dependency type was not specified and we don't have the POM
263 // for this artifact, use the default from Maven spec.
264 d.Type = "jar"
265 }
266 }
267 if d.Scope == "" {
268 // Scope was not specified, use the default from Maven spec.
269 d.Scope = "compile"
270 }
271 }
272}
273
Colin Crosscf53e602018-06-26 15:27:20 -0700274// ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it
275// to "current" if it is not present.
276func (p *Pom) ExtractMinSdkVersion() error {
277 aar, err := zip.OpenReader(p.ArtifactFile)
278 if err != nil {
279 return err
280 }
281 defer aar.Close()
282
283 var manifest *zip.File
284 for _, f := range aar.File {
285 if f.Name == "AndroidManifest.xml" {
286 manifest = f
287 break
288 }
289 }
290
291 if manifest == nil {
292 return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile)
293 }
294
295 r, err := manifest.Open()
296 if err != nil {
297 return err
298 }
299 defer r.Close()
300
301 decoder := xml.NewDecoder(r)
302
303 manifestData := struct {
304 XMLName xml.Name `xml:"manifest"`
305 Uses_sdk struct {
306 MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"`
307 } `xml:"uses-sdk"`
308 }{}
309
310 err = decoder.Decode(&manifestData)
311 if err != nil {
312 return err
313 }
314
315 p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion
316 if p.MinSdkVersion == "" {
317 p.MinSdkVersion = "current"
318 }
319
320 return nil
321}
322
Colin Cross70dd38f2018-04-16 13:52:10 -0700323var bpTemplate = template.Must(template.New("bp").Parse(`
Colin Cross632987a2018-08-29 16:17:55 -0700324{{.ImportModuleType}} {
Colin Cross70dd38f2018-04-16 13:52:10 -0700325 name: "{{.BpName}}-nodeps",
Colin Cross632987a2018-08-29 16:17:55 -0700326 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
327 sdk_version: "{{.SdkVersion}}",
328 {{- if .IsAar}}
Colin Crosscf53e602018-06-26 15:27:20 -0700329 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700330 static_libs: [
331 {{- range .BpAarDeps}}
332 "{{.}}",
333 {{- end}}
334 {{- range .BpExtraDeps}}
335 "{{.}}",
336 {{- end}}
337 ],
338 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700339}
340
Colin Cross632987a2018-08-29 16:17:55 -0700341{{.ModuleType}} {
342 name: "{{.BpName}}",
343 {{- if .IsDeviceModule}}
344 sdk_version: "{{.SdkVersion}}",
345 {{- if .IsAar}}
Colin Cross461ba492018-07-10 13:45:30 -0700346 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700347 manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
348 {{- end}}
349 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700350 static_libs: [
Colin Cross632987a2018-08-29 16:17:55 -0700351 "{{.BpName}}-nodeps",
352 {{- range .BpJarDeps}}
353 "{{.}}",
354 {{- end}}
355 {{- range .BpAarDeps}}
356 "{{.}}",
357 {{- end}}
358 {{- range .BpExtraDeps}}
359 "{{.}}",
360 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700361 ],
362 java_version: "1.7",
363}
364`))
365
366func parse(filename string) (*Pom, error) {
367 data, err := ioutil.ReadFile(filename)
368 if err != nil {
369 return nil, err
370 }
371
372 var pom Pom
373 err = xml.Unmarshal(data, &pom)
374 if err != nil {
375 return nil, err
376 }
377
378 if useVersion != "" && pom.Version != useVersion {
379 return nil, nil
380 }
381
382 if pom.Packaging == "" {
383 pom.Packaging = "jar"
384 }
385
386 pom.PomFile = filename
387 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
388
389 return &pom, nil
390}
391
392func rerunForRegen(filename string) error {
393 buf, err := ioutil.ReadFile(filename)
394 if err != nil {
395 return err
396 }
397
398 scanner := bufio.NewScanner(bytes.NewBuffer(buf))
399
400 // Skip the first line in the file
401 for i := 0; i < 2; i++ {
402 if !scanner.Scan() {
403 if scanner.Err() != nil {
404 return scanner.Err()
405 } else {
406 return fmt.Errorf("unexpected EOF")
407 }
408 }
409 }
410
411 // Extract the old args from the file
412 line := scanner.Text()
413 if strings.HasPrefix(line, "// pom2bp ") {
414 line = strings.TrimPrefix(line, "// pom2bp ")
415 } else if strings.HasPrefix(line, "// pom2mk ") {
416 line = strings.TrimPrefix(line, "// pom2mk ")
417 } else if strings.HasPrefix(line, "# pom2mk ") {
418 line = strings.TrimPrefix(line, "# pom2mk ")
419 } else {
420 return fmt.Errorf("unexpected second line: %q", line)
421 }
422 args := strings.Split(line, " ")
423 lastArg := args[len(args)-1]
424 args = args[:len(args)-1]
425
426 // Append all current command line args except -regen <file> to the ones from the file
427 for i := 1; i < len(os.Args); i++ {
Colin Crosscf53e602018-06-26 15:27:20 -0700428 if os.Args[i] == "-regen" || os.Args[i] == "--regen" {
Colin Cross70dd38f2018-04-16 13:52:10 -0700429 i++
430 } else {
431 args = append(args, os.Args[i])
432 }
433 }
434 args = append(args, lastArg)
435
436 cmd := os.Args[0] + " " + strings.Join(args, " ")
437 // Re-exec pom2bp with the new arguments
438 output, err := exec.Command("/bin/sh", "-c", cmd).Output()
439 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
440 return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
441 } else if err != nil {
442 return err
443 }
444
445 // If the old file was a .mk file, replace it with a .bp file
446 if filepath.Ext(filename) == ".mk" {
447 os.Remove(filename)
448 filename = strings.TrimSuffix(filename, ".mk") + ".bp"
449 }
450
451 return ioutil.WriteFile(filename, output, 0666)
452}
453
454func main() {
455 flag.Usage = func() {
456 fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos
457
458The tool will extract the necessary information from *.pom files to create an Android.bp whose
459aar libraries can be linked against when using AAPT2.
460
461Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>]
462
463 -rewrite <regex>=<replace>
464 rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite
465 option can be specified multiple times. When determining the Android.bp module for a given Maven
466 project, mappings are searched in the order they were specified. The first <regex> matching
467 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
468 the Android.bp module name using <replace>. If no matches are found, <artifactId> is used.
469 -exclude <module>
470 Don't put the specified module in the Android.bp file.
471 -extra-deps <module>=<module>[,<module>]
472 Some Android.bp modules have transitive dependencies that must be specified when they are
473 depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
474 This may be specified multiple times to declare these dependencies.
475 -sdk-version <version>
476 Sets LOCAL_SDK_VERSION := <version> for all modules.
477 -use-version <version>
478 If the maven directory contains multiple versions of artifacts and their pom files,
479 -use-version can be used to only write Android.bp files for a specific version of those artifacts.
480 <dir>
481 The directory to search for *.pom files under.
482 The contents are written to stdout, to be put in the current directory (often as Android.bp)
483 -regen <file>
484 Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it
485 ends with .mk).
486
487`, os.Args[0])
488 }
489
490 var regen string
491
492 flag.Var(&excludes, "exclude", "Exclude module")
493 flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
494 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Jeff Gastond4928532018-08-24 14:30:13 -0400495 flag.Var(&hostModuleNames, "host", "Specifies that the corresponding module (specified in the form 'module.group:module.artifact') is a host module")
Colin Cross70dd38f2018-04-16 13:52:10 -0700496 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
497 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
498 flag.Bool("static-deps", false, "Ignored")
499 flag.StringVar(&regen, "regen", "", "Rewrite specified file")
500 flag.Parse()
501
502 if regen != "" {
503 err := rerunForRegen(regen)
504 if err != nil {
505 fmt.Fprintln(os.Stderr, err)
506 os.Exit(1)
507 }
508 os.Exit(0)
509 }
510
511 if flag.NArg() == 0 {
512 fmt.Fprintln(os.Stderr, "Directory argument is required")
513 os.Exit(1)
514 } else if flag.NArg() > 1 {
515 fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
516 os.Exit(1)
517 }
518
519 dir := flag.Arg(0)
520 absDir, err := filepath.Abs(dir)
521 if err != nil {
522 fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
523 os.Exit(1)
524 }
525
526 var filenames []string
527 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
528 if err != nil {
529 return err
530 }
531
532 name := info.Name()
533 if info.IsDir() {
534 if strings.HasPrefix(name, ".") {
535 return filepath.SkipDir
536 }
537 return nil
538 }
539
540 if strings.HasPrefix(name, ".") {
541 return nil
542 }
543
544 if strings.HasSuffix(name, ".pom") {
545 path, err = filepath.Rel(absDir, path)
546 if err != nil {
547 return err
548 }
549 filenames = append(filenames, filepath.Join(dir, path))
550 }
551 return nil
552 })
553 if err != nil {
554 fmt.Fprintln(os.Stderr, "Error walking files:", err)
555 os.Exit(1)
556 }
557
558 if len(filenames) == 0 {
559 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
560 os.Exit(1)
561 }
562
563 sort.Strings(filenames)
564
565 poms := []*Pom{}
566 modules := make(map[string]*Pom)
567 duplicate := false
568 for _, filename := range filenames {
569 pom, err := parse(filename)
570 if err != nil {
571 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
572 os.Exit(1)
573 }
574
575 if pom != nil {
576 key := pom.BpName()
577 if excludes[key] {
578 continue
579 }
580
581 if old, ok := modules[key]; ok {
582 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
583 duplicate = true
584 }
585
586 poms = append(poms, pom)
587 modules[key] = pom
588 }
589 }
590 if duplicate {
591 os.Exit(1)
592 }
593
594 for _, pom := range poms {
Colin Crosscf53e602018-06-26 15:27:20 -0700595 if pom.IsAar() {
596 err := pom.ExtractMinSdkVersion()
597 if err != nil {
Colin Crossfe5a3b72018-07-13 21:25:15 -0700598 fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err)
Colin Crosscf53e602018-06-26 15:27:20 -0700599 os.Exit(1)
600 }
601 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700602 pom.FixDeps(modules)
603 }
604
605 buf := &bytes.Buffer{}
606
607 fmt.Fprintln(buf, "// Automatically generated with:")
Colin Cross0b9f31f2019-02-28 11:00:01 -0800608 fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
Colin Cross70dd38f2018-04-16 13:52:10 -0700609
610 for _, pom := range poms {
611 var err error
612 err = bpTemplate.Execute(buf, pom)
613 if err != nil {
614 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err)
615 os.Exit(1)
616 }
617 }
618
619 out, err := bpfix.Reformat(buf.String())
620 if err != nil {
621 fmt.Fprintln(os.Stderr, "Error formatting output", err)
622 os.Exit(1)
623 }
624
625 os.Stdout.WriteString(out)
626}