blob: c2ad94425112e18665aec737c5d3d1e1e78e7acf [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: [
Colin Cross1aa7f262019-04-10 11:07:15 -0700331 {{- range .BpJarDeps}}
332 "{{.}}",
333 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700334 {{- range .BpAarDeps}}
335 "{{.}}",
336 {{- end}}
337 {{- range .BpExtraDeps}}
338 "{{.}}",
339 {{- end}}
340 ],
341 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700342}
343
Colin Cross632987a2018-08-29 16:17:55 -0700344{{.ModuleType}} {
345 name: "{{.BpName}}",
346 {{- if .IsDeviceModule}}
347 sdk_version: "{{.SdkVersion}}",
348 {{- if .IsAar}}
Colin Cross461ba492018-07-10 13:45:30 -0700349 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700350 manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
351 {{- end}}
352 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700353 static_libs: [
Colin Cross632987a2018-08-29 16:17:55 -0700354 "{{.BpName}}-nodeps",
Colin Cross1aa7f262019-04-10 11:07:15 -0700355 {{- range .BpJarDeps}}
Colin Cross632987a2018-08-29 16:17:55 -0700356 "{{.}}",
357 {{- end}}
358 {{- range .BpAarDeps}}
359 "{{.}}",
360 {{- end}}
361 {{- range .BpExtraDeps}}
362 "{{.}}",
363 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700364 ],
365 java_version: "1.7",
366}
367`))
368
369func parse(filename string) (*Pom, error) {
370 data, err := ioutil.ReadFile(filename)
371 if err != nil {
372 return nil, err
373 }
374
375 var pom Pom
376 err = xml.Unmarshal(data, &pom)
377 if err != nil {
378 return nil, err
379 }
380
381 if useVersion != "" && pom.Version != useVersion {
382 return nil, nil
383 }
384
385 if pom.Packaging == "" {
386 pom.Packaging = "jar"
387 }
388
389 pom.PomFile = filename
390 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
391
392 return &pom, nil
393}
394
395func rerunForRegen(filename string) error {
396 buf, err := ioutil.ReadFile(filename)
397 if err != nil {
398 return err
399 }
400
401 scanner := bufio.NewScanner(bytes.NewBuffer(buf))
402
403 // Skip the first line in the file
404 for i := 0; i < 2; i++ {
405 if !scanner.Scan() {
406 if scanner.Err() != nil {
407 return scanner.Err()
408 } else {
409 return fmt.Errorf("unexpected EOF")
410 }
411 }
412 }
413
414 // Extract the old args from the file
415 line := scanner.Text()
416 if strings.HasPrefix(line, "// pom2bp ") {
417 line = strings.TrimPrefix(line, "// pom2bp ")
418 } else if strings.HasPrefix(line, "// pom2mk ") {
419 line = strings.TrimPrefix(line, "// pom2mk ")
420 } else if strings.HasPrefix(line, "# pom2mk ") {
421 line = strings.TrimPrefix(line, "# pom2mk ")
422 } else {
423 return fmt.Errorf("unexpected second line: %q", line)
424 }
425 args := strings.Split(line, " ")
426 lastArg := args[len(args)-1]
427 args = args[:len(args)-1]
428
429 // Append all current command line args except -regen <file> to the ones from the file
430 for i := 1; i < len(os.Args); i++ {
Colin Crosscf53e602018-06-26 15:27:20 -0700431 if os.Args[i] == "-regen" || os.Args[i] == "--regen" {
Colin Cross70dd38f2018-04-16 13:52:10 -0700432 i++
433 } else {
434 args = append(args, os.Args[i])
435 }
436 }
437 args = append(args, lastArg)
438
439 cmd := os.Args[0] + " " + strings.Join(args, " ")
440 // Re-exec pom2bp with the new arguments
441 output, err := exec.Command("/bin/sh", "-c", cmd).Output()
442 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
443 return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
444 } else if err != nil {
445 return err
446 }
447
448 // If the old file was a .mk file, replace it with a .bp file
449 if filepath.Ext(filename) == ".mk" {
450 os.Remove(filename)
451 filename = strings.TrimSuffix(filename, ".mk") + ".bp"
452 }
453
454 return ioutil.WriteFile(filename, output, 0666)
455}
456
457func main() {
458 flag.Usage = func() {
459 fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos
460
461The tool will extract the necessary information from *.pom files to create an Android.bp whose
462aar libraries can be linked against when using AAPT2.
463
464Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>]
465
466 -rewrite <regex>=<replace>
467 rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite
468 option can be specified multiple times. When determining the Android.bp module for a given Maven
469 project, mappings are searched in the order they were specified. The first <regex> matching
470 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
471 the Android.bp module name using <replace>. If no matches are found, <artifactId> is used.
472 -exclude <module>
473 Don't put the specified module in the Android.bp file.
474 -extra-deps <module>=<module>[,<module>]
475 Some Android.bp modules have transitive dependencies that must be specified when they are
476 depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
477 This may be specified multiple times to declare these dependencies.
478 -sdk-version <version>
479 Sets LOCAL_SDK_VERSION := <version> for all modules.
480 -use-version <version>
481 If the maven directory contains multiple versions of artifacts and their pom files,
482 -use-version can be used to only write Android.bp files for a specific version of those artifacts.
483 <dir>
484 The directory to search for *.pom files under.
485 The contents are written to stdout, to be put in the current directory (often as Android.bp)
486 -regen <file>
487 Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it
488 ends with .mk).
489
490`, os.Args[0])
491 }
492
493 var regen string
494
495 flag.Var(&excludes, "exclude", "Exclude module")
496 flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
497 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Jeff Gastond4928532018-08-24 14:30:13 -0400498 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 -0700499 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
500 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
501 flag.Bool("static-deps", false, "Ignored")
502 flag.StringVar(&regen, "regen", "", "Rewrite specified file")
503 flag.Parse()
504
505 if regen != "" {
506 err := rerunForRegen(regen)
507 if err != nil {
508 fmt.Fprintln(os.Stderr, err)
509 os.Exit(1)
510 }
511 os.Exit(0)
512 }
513
514 if flag.NArg() == 0 {
515 fmt.Fprintln(os.Stderr, "Directory argument is required")
516 os.Exit(1)
517 } else if flag.NArg() > 1 {
518 fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
519 os.Exit(1)
520 }
521
522 dir := flag.Arg(0)
523 absDir, err := filepath.Abs(dir)
524 if err != nil {
525 fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
526 os.Exit(1)
527 }
528
529 var filenames []string
530 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
531 if err != nil {
532 return err
533 }
534
535 name := info.Name()
536 if info.IsDir() {
537 if strings.HasPrefix(name, ".") {
538 return filepath.SkipDir
539 }
540 return nil
541 }
542
543 if strings.HasPrefix(name, ".") {
544 return nil
545 }
546
547 if strings.HasSuffix(name, ".pom") {
548 path, err = filepath.Rel(absDir, path)
549 if err != nil {
550 return err
551 }
552 filenames = append(filenames, filepath.Join(dir, path))
553 }
554 return nil
555 })
556 if err != nil {
557 fmt.Fprintln(os.Stderr, "Error walking files:", err)
558 os.Exit(1)
559 }
560
561 if len(filenames) == 0 {
562 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
563 os.Exit(1)
564 }
565
566 sort.Strings(filenames)
567
568 poms := []*Pom{}
569 modules := make(map[string]*Pom)
570 duplicate := false
571 for _, filename := range filenames {
572 pom, err := parse(filename)
573 if err != nil {
574 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
575 os.Exit(1)
576 }
577
578 if pom != nil {
579 key := pom.BpName()
580 if excludes[key] {
581 continue
582 }
583
584 if old, ok := modules[key]; ok {
585 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
586 duplicate = true
587 }
588
589 poms = append(poms, pom)
590 modules[key] = pom
591 }
592 }
593 if duplicate {
594 os.Exit(1)
595 }
596
597 for _, pom := range poms {
Colin Crosscf53e602018-06-26 15:27:20 -0700598 if pom.IsAar() {
599 err := pom.ExtractMinSdkVersion()
600 if err != nil {
Colin Crossfe5a3b72018-07-13 21:25:15 -0700601 fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err)
Colin Crosscf53e602018-06-26 15:27:20 -0700602 os.Exit(1)
603 }
604 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700605 pom.FixDeps(modules)
606 }
607
608 buf := &bytes.Buffer{}
609
610 fmt.Fprintln(buf, "// Automatically generated with:")
Colin Cross0b9f31f2019-02-28 11:00:01 -0800611 fmt.Fprintln(buf, "// pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
Colin Cross70dd38f2018-04-16 13:52:10 -0700612
613 for _, pom := range poms {
614 var err error
615 err = bpTemplate.Execute(buf, pom)
616 if err != nil {
617 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err)
618 os.Exit(1)
619 }
620 }
621
622 out, err := bpfix.Reformat(buf.String())
623 if err != nil {
624 fmt.Fprintln(os.Stderr, "Error formatting output", err)
625 os.Exit(1)
626 }
627
628 os.Stdout.WriteString(out)
629}