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