blob: 0e8ad05a964e33025b12c6524262e683b0d2af66 [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"
Romain Jobredeaux89cb2242021-09-20 14:14:22 +000027 "path"
Colin Cross70dd38f2018-04-16 13:52:10 -070028 "path/filepath"
29 "regexp"
30 "sort"
31 "strings"
32 "text/template"
33
34 "github.com/google/blueprint/proptools"
35
36 "android/soong/bpfix/bpfix"
37)
38
39type RewriteNames []RewriteName
40type RewriteName struct {
41 regexp *regexp.Regexp
42 repl string
43}
44
45func (r *RewriteNames) String() string {
46 return ""
47}
48
49func (r *RewriteNames) Set(v string) error {
50 split := strings.SplitN(v, "=", 2)
51 if len(split) != 2 {
52 return fmt.Errorf("Must be in the form of <regex>=<replace>")
53 }
54 regex, err := regexp.Compile(split[0])
55 if err != nil {
56 return nil
57 }
58 *r = append(*r, RewriteName{
59 regexp: regex,
60 repl: split[1],
61 })
62 return nil
63}
64
65func (r *RewriteNames) MavenToBp(groupId string, artifactId string) string {
66 for _, r := range *r {
67 if r.regexp.MatchString(groupId + ":" + artifactId) {
68 return r.regexp.ReplaceAllString(groupId+":"+artifactId, r.repl)
69 } else if r.regexp.MatchString(artifactId) {
70 return r.regexp.ReplaceAllString(artifactId, r.repl)
71 }
72 }
73 return artifactId
74}
75
76var rewriteNames = RewriteNames{}
77
78type ExtraDeps map[string][]string
79
80func (d ExtraDeps) String() string {
81 return ""
82}
83
84func (d ExtraDeps) Set(v string) error {
85 split := strings.SplitN(v, "=", 2)
86 if len(split) != 2 {
87 return fmt.Errorf("Must be in the form of <module>=<module>[,<module>]")
88 }
89 d[split[0]] = strings.Split(split[1], ",")
90 return nil
91}
92
Paul Duffinbabaf072019-04-16 11:35:20 +010093var extraStaticLibs = make(ExtraDeps)
94
95var extraLibs = make(ExtraDeps)
Colin Cross70dd38f2018-04-16 13:52:10 -070096
Alan Viverette24658d02021-08-31 20:00:52 +000097var optionalUsesLibs = make(ExtraDeps)
98
Colin Cross70dd38f2018-04-16 13:52:10 -070099type Exclude map[string]bool
100
101func (e Exclude) String() string {
102 return ""
103}
104
105func (e Exclude) Set(v string) error {
106 e[v] = true
107 return nil
108}
109
110var excludes = make(Exclude)
111
Jeff Gastond4928532018-08-24 14:30:13 -0400112type HostModuleNames map[string]bool
113
114func (n HostModuleNames) IsHostModule(groupId string, artifactId string) bool {
Colin Cross86bc9d42018-08-29 15:36:33 -0700115 _, found := n[groupId+":"+artifactId]
Jeff Gastond4928532018-08-24 14:30:13 -0400116 return found
117}
118
119func (n HostModuleNames) String() string {
120 return ""
121}
122
123func (n HostModuleNames) Set(v string) error {
124 n[v] = true
125 return nil
126}
127
128var hostModuleNames = HostModuleNames{}
129
Tony Mak81785002019-07-18 21:36:44 +0100130type HostAndDeviceModuleNames map[string]bool
131
132func (n HostAndDeviceModuleNames) IsHostAndDeviceModule(groupId string, artifactId string) bool {
133 _, found := n[groupId+":"+artifactId]
134
135 return found
136}
137
138func (n HostAndDeviceModuleNames) String() string {
139 return ""
140}
141
142func (n HostAndDeviceModuleNames) Set(v string) error {
143 n[v] = true
144 return nil
145}
146
147var hostAndDeviceModuleNames = HostAndDeviceModuleNames{}
148
Colin Cross70dd38f2018-04-16 13:52:10 -0700149var sdkVersion string
Anton Hanssonc29f0762021-03-29 16:10:06 +0100150var defaultMinSdkVersion string
Colin Cross70dd38f2018-04-16 13:52:10 -0700151var useVersion string
Dan Willemsen52c90d82019-04-21 21:37:39 -0700152var staticDeps bool
Alan Viveretteefab5142022-09-19 16:26:00 +0000153var writeCmd bool
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700154var jetifier bool
Colin Cross70dd38f2018-04-16 13:52:10 -0700155
156func InList(s string, list []string) bool {
157 for _, l := range list {
158 if l == s {
159 return true
160 }
161 }
162
163 return false
164}
165
166type Dependency struct {
167 XMLName xml.Name `xml:"dependency"`
168
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000169 BpTarget string `xml:"-"`
170 BazelTarget string `xml:"-"`
Colin Cross70dd38f2018-04-16 13:52:10 -0700171
172 GroupId string `xml:"groupId"`
173 ArtifactId string `xml:"artifactId"`
174 Version string `xml:"version"`
175 Type string `xml:"type"`
176 Scope string `xml:"scope"`
177}
178
179func (d Dependency) BpName() string {
180 if d.BpTarget == "" {
181 d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
182 }
183 return d.BpTarget
184}
185
186type Pom struct {
187 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
188
Colin Crosscf53e602018-06-26 15:27:20 -0700189 PomFile string `xml:"-"`
190 ArtifactFile string `xml:"-"`
191 BpTarget string `xml:"-"`
192 MinSdkVersion string `xml:"-"`
Colin Cross70dd38f2018-04-16 13:52:10 -0700193
194 GroupId string `xml:"groupId"`
195 ArtifactId string `xml:"artifactId"`
196 Version string `xml:"version"`
197 Packaging string `xml:"packaging"`
198
199 Dependencies []*Dependency `xml:"dependencies>dependency"`
200}
201
202func (p Pom) IsAar() bool {
203 return p.Packaging == "aar"
204}
205
206func (p Pom) IsJar() bool {
207 return p.Packaging == "jar"
208}
209
Jeff Gastond4928532018-08-24 14:30:13 -0400210func (p Pom) IsHostModule() bool {
211 return hostModuleNames.IsHostModule(p.GroupId, p.ArtifactId)
212}
213
214func (p Pom) IsDeviceModule() bool {
215 return !p.IsHostModule()
216}
217
Tony Mak81785002019-07-18 21:36:44 +0100218func (p Pom) IsHostAndDeviceModule() bool {
219 return hostAndDeviceModuleNames.IsHostAndDeviceModule(p.GroupId, p.ArtifactId)
220}
221
Jooyung Han43d30252020-05-22 03:54:24 +0900222func (p Pom) IsHostOnly() bool {
223 return p.IsHostModule() && !p.IsHostAndDeviceModule()
224}
225
Colin Cross632987a2018-08-29 16:17:55 -0700226func (p Pom) ModuleType() string {
227 if p.IsAar() {
228 return "android_library"
Jooyung Han43d30252020-05-22 03:54:24 +0900229 } else if p.IsHostOnly() {
Colin Cross632987a2018-08-29 16:17:55 -0700230 return "java_library_host"
231 } else {
232 return "java_library_static"
233 }
234}
235
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000236func (p Pom) BazelTargetType() string {
237 if p.IsAar() {
238 return "android_library"
239 } else {
240 return "java_library"
241 }
242}
243
Colin Cross632987a2018-08-29 16:17:55 -0700244func (p Pom) ImportModuleType() string {
245 if p.IsAar() {
246 return "android_library_import"
Jooyung Han43d30252020-05-22 03:54:24 +0900247 } else if p.IsHostOnly() {
Colin Cross632987a2018-08-29 16:17:55 -0700248 return "java_import_host"
249 } else {
250 return "java_import"
251 }
252}
253
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000254func (p Pom) BazelImportTargetType() string {
255 if p.IsAar() {
256 return "aar_import"
257 } else {
258 return "java_import"
259 }
260}
261
Colin Cross632987a2018-08-29 16:17:55 -0700262func (p Pom) ImportProperty() string {
263 if p.IsAar() {
264 return "aars"
265 } else {
266 return "jars"
267 }
268}
269
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000270func (p Pom) BazelImportProperty() string {
271 if p.IsAar() {
272 return "aar"
273 } else {
274 return "jars"
275 }
276}
277
Colin Cross70dd38f2018-04-16 13:52:10 -0700278func (p Pom) BpName() string {
279 if p.BpTarget == "" {
280 p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId)
281 }
282 return p.BpTarget
283}
284
285func (p Pom) BpJarDeps() []string {
286 return p.BpDeps("jar", []string{"compile", "runtime"})
287}
288
289func (p Pom) BpAarDeps() []string {
290 return p.BpDeps("aar", []string{"compile", "runtime"})
291}
292
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000293func (p Pom) BazelJarDeps() []string {
294 return p.BazelDeps("jar", []string{"compile", "runtime"})
295}
296
297func (p Pom) BazelAarDeps() []string {
298 return p.BazelDeps("aar", []string{"compile", "runtime"})
299}
300
Paul Duffinbabaf072019-04-16 11:35:20 +0100301func (p Pom) BpExtraStaticLibs() []string {
302 return extraStaticLibs[p.BpName()]
303}
304
305func (p Pom) BpExtraLibs() []string {
306 return extraLibs[p.BpName()]
Colin Cross70dd38f2018-04-16 13:52:10 -0700307}
308
Alan Viverette24658d02021-08-31 20:00:52 +0000309func (p Pom) BpOptionalUsesLibs() []string {
310 return optionalUsesLibs[p.BpName()]
311}
312
Colin Cross70dd38f2018-04-16 13:52:10 -0700313// BpDeps obtains dependencies filtered by type and scope. The results of this
314// method are formatted as Android.bp targets, e.g. run through MavenToBp rules.
315func (p Pom) BpDeps(typeExt string, scopes []string) []string {
316 var ret []string
317 for _, d := range p.Dependencies {
318 if d.Type != typeExt || !InList(d.Scope, scopes) {
319 continue
320 }
321 name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId)
322 ret = append(ret, name)
323 }
324 return ret
325}
326
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000327// BazelDeps obtains dependencies filtered by type and scope. The results of this
328// method are formatted as Bazel BUILD targets.
329func (p Pom) BazelDeps(typeExt string, scopes []string) []string {
330 var ret []string
331 for _, d := range p.Dependencies {
332 if d.Type != typeExt || !InList(d.Scope, scopes) {
333 continue
334 }
335 ret = append(ret, d.BazelTarget)
336 }
337 return ret
338}
339
340func PathModVars() (string, string, string) {
341 cmd := "/bin/bash"
342 androidTop := os.Getenv("ANDROID_BUILD_TOP")
343 envSetupSh := path.Join(androidTop, "build/envsetup.sh")
344 return cmd, androidTop, envSetupSh
345}
346
347func InitRefreshMod(poms []*Pom) error {
348 cmd, _, envSetupSh := PathModVars()
349 // refreshmod is expensive, so if pathmod is already working we can skip it.
350 _, err := exec.Command(cmd, "-c", ". "+envSetupSh+" && pathmod "+poms[0].BpName()).Output()
351 if exitErr, _ := err.(*exec.ExitError); exitErr != nil || err != nil {
352 _, err := exec.Command(cmd, "-c", ". "+envSetupSh+" && refreshmod").Output()
353 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
354 return fmt.Errorf("failed to run %s\n%s\ntry running lunch.", cmd, string(exitErr.Stderr))
355 } else if err != nil {
356 return err
357 }
358 }
359 return nil
360}
361
362func BazelifyExtraDeps(extraDeps ExtraDeps, modules map[string]*Pom) error {
363 for _, deps := range extraDeps {
364 for _, dep := range deps {
365 bazelName, err := BpNameToBazelTarget(dep, modules)
366 if err != nil {
367 return err
368 }
369 dep = bazelName
370 }
371
372 }
373 return nil
374}
375
376func (p *Pom) GetBazelDepNames(modules map[string]*Pom) error {
377 for _, d := range p.Dependencies {
378 bazelName, err := BpNameToBazelTarget(d.BpName(), modules)
379 if err != nil {
380 return err
381 }
382 d.BazelTarget = bazelName
383 }
384 return nil
385}
386
387func BpNameToBazelTarget(bpName string, modules map[string]*Pom) (string, error) {
388 cmd, androidTop, envSetupSh := PathModVars()
389
390 if _, ok := modules[bpName]; ok {
391 // We've seen the POM for this dependency, it will be local to the output BUILD file
392 return ":" + bpName, nil
393 } else {
394 // we don't have the POM for this artifact, find and use the fully qualified target name.
395 output, err := exec.Command(cmd, "-c", ". "+envSetupSh+" && pathmod "+bpName).Output()
396 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
397 return "", fmt.Errorf("failed to run %s %s\n%s", cmd, bpName, string(exitErr.Stderr))
398 } else if err != nil {
399 return "", err
400 }
401 relPath := ""
402 for _, line := range strings.Fields(string(output)) {
403 if strings.Contains(line, androidTop) {
404 relPath = strings.TrimPrefix(line, androidTop)
405 relPath = strings.TrimLeft(relPath, "/")
406 }
407 }
408 return "//" + relPath + ":" + bpName, nil
409 }
410}
411
Colin Cross70dd38f2018-04-16 13:52:10 -0700412func (p Pom) SdkVersion() string {
413 return sdkVersion
414}
415
Anton Hanssonc29f0762021-03-29 16:10:06 +0100416func (p Pom) DefaultMinSdkVersion() string {
417 return defaultMinSdkVersion
418}
419
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700420func (p Pom) Jetifier() bool {
421 return jetifier
422}
423
Colin Cross70dd38f2018-04-16 13:52:10 -0700424func (p *Pom) FixDeps(modules map[string]*Pom) {
425 for _, d := range p.Dependencies {
426 if d.Type == "" {
427 if depPom, ok := modules[d.BpName()]; ok {
428 // We've seen the POM for this dependency, use its packaging
429 // as the dependency type rather than Maven spec default.
430 d.Type = depPom.Packaging
431 } else {
432 // Dependency type was not specified and we don't have the POM
433 // for this artifact, use the default from Maven spec.
434 d.Type = "jar"
435 }
436 }
437 if d.Scope == "" {
438 // Scope was not specified, use the default from Maven spec.
439 d.Scope = "compile"
440 }
441 }
442}
443
Colin Crosscf53e602018-06-26 15:27:20 -0700444// ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it
445// to "current" if it is not present.
446func (p *Pom) ExtractMinSdkVersion() error {
447 aar, err := zip.OpenReader(p.ArtifactFile)
448 if err != nil {
449 return err
450 }
451 defer aar.Close()
452
453 var manifest *zip.File
454 for _, f := range aar.File {
455 if f.Name == "AndroidManifest.xml" {
456 manifest = f
457 break
458 }
459 }
460
461 if manifest == nil {
462 return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile)
463 }
464
465 r, err := manifest.Open()
466 if err != nil {
467 return err
468 }
469 defer r.Close()
470
471 decoder := xml.NewDecoder(r)
472
473 manifestData := struct {
474 XMLName xml.Name `xml:"manifest"`
475 Uses_sdk struct {
476 MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"`
477 } `xml:"uses-sdk"`
478 }{}
479
480 err = decoder.Decode(&manifestData)
481 if err != nil {
482 return err
483 }
484
485 p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion
486 if p.MinSdkVersion == "" {
487 p.MinSdkVersion = "current"
488 }
489
490 return nil
491}
492
Colin Cross70dd38f2018-04-16 13:52:10 -0700493var bpTemplate = template.Must(template.New("bp").Parse(`
Colin Cross632987a2018-08-29 16:17:55 -0700494{{.ImportModuleType}} {
Dan Willemsen52c90d82019-04-21 21:37:39 -0700495 name: "{{.BpName}}",
496 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
497 sdk_version: "{{.SdkVersion}}",
498 {{- if .Jetifier}}
499 jetifier: true,
500 {{- end}}
Tony Mak81785002019-07-18 21:36:44 +0100501 {{- if .IsHostAndDeviceModule}}
502 host_supported: true,
503 {{- end}}
Jooyung Han43d30252020-05-22 03:54:24 +0900504 {{- if not .IsHostOnly}}
505 apex_available: [
506 "//apex_available:platform",
507 "//apex_available:anyapex",
508 ],
509 {{- end}}
Dan Willemsen52c90d82019-04-21 21:37:39 -0700510 {{- if .IsAar}}
511 min_sdk_version: "{{.MinSdkVersion}}",
512 static_libs: [
513 {{- range .BpJarDeps}}
514 "{{.}}",
515 {{- end}}
516 {{- range .BpAarDeps}}
517 "{{.}}",
518 {{- end}}
519 {{- range .BpExtraStaticLibs}}
520 "{{.}}",
521 {{- end}}
522 ],
523 {{- if .BpExtraLibs}}
524 libs: [
525 {{- range .BpExtraLibs}}
526 "{{.}}",
527 {{- end}}
528 ],
Alan Viverettebcbfc5f2021-09-13 17:12:28 +0000529 {{- end}}
Alan Viverette24658d02021-08-31 20:00:52 +0000530 {{- if .BpOptionalUsesLibs}}
531 optional_uses_libs: [
532 {{- range .BpOptionalUsesLibs}}
533 "{{.}}",
534 {{- end}}
535 ],
Dan Willemsen52c90d82019-04-21 21:37:39 -0700536 {{- end}}
Anton Hanssonebfbad22021-04-06 19:21:34 +0000537 {{- else if not .IsHostOnly}}
538 min_sdk_version: "{{.DefaultMinSdkVersion}}",
Dan Willemsen52c90d82019-04-21 21:37:39 -0700539 {{- end}}
540}
541`))
542
543var bpDepsTemplate = template.Must(template.New("bp").Parse(`
544{{.ImportModuleType}} {
Colin Cross70dd38f2018-04-16 13:52:10 -0700545 name: "{{.BpName}}-nodeps",
Colin Cross632987a2018-08-29 16:17:55 -0700546 {{.ImportProperty}}: ["{{.ArtifactFile}}"],
547 sdk_version: "{{.SdkVersion}}",
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700548 {{- if .Jetifier}}
549 jetifier: true,
550 {{- end}}
Tony Mak81785002019-07-18 21:36:44 +0100551 {{- if .IsHostAndDeviceModule}}
552 host_supported: true,
553 {{- end}}
Jooyung Han43d30252020-05-22 03:54:24 +0900554 {{- if not .IsHostOnly}}
555 apex_available: [
556 "//apex_available:platform",
557 "//apex_available:anyapex",
558 ],
559 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700560 {{- if .IsAar}}
Colin Crosscf53e602018-06-26 15:27:20 -0700561 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700562 static_libs: [
Colin Cross1aa7f262019-04-10 11:07:15 -0700563 {{- range .BpJarDeps}}
564 "{{.}}",
565 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700566 {{- range .BpAarDeps}}
567 "{{.}}",
568 {{- end}}
Paul Duffinbabaf072019-04-16 11:35:20 +0100569 {{- range .BpExtraStaticLibs}}
Colin Cross632987a2018-08-29 16:17:55 -0700570 "{{.}}",
571 {{- end}}
572 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100573 {{- if .BpExtraLibs}}
574 libs: [
575 {{- range .BpExtraLibs}}
576 "{{.}}",
577 {{- end}}
578 ],
579 {{- end}}
Anton Hanssonebfbad22021-04-06 19:21:34 +0000580 {{- else if not .IsHostOnly}}
581 min_sdk_version: "{{.DefaultMinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700582 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700583}
584
Colin Cross632987a2018-08-29 16:17:55 -0700585{{.ModuleType}} {
586 name: "{{.BpName}}",
587 {{- if .IsDeviceModule}}
588 sdk_version: "{{.SdkVersion}}",
Tony Mak81785002019-07-18 21:36:44 +0100589 {{- if .IsHostAndDeviceModule}}
590 host_supported: true,
591 {{- end}}
Jooyung Han43d30252020-05-22 03:54:24 +0900592 {{- if not .IsHostOnly}}
593 apex_available: [
594 "//apex_available:platform",
595 "//apex_available:anyapex",
596 ],
597 {{- end}}
Colin Cross632987a2018-08-29 16:17:55 -0700598 {{- if .IsAar}}
Colin Cross461ba492018-07-10 13:45:30 -0700599 min_sdk_version: "{{.MinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700600 manifest: "manifests/{{.BpName}}/AndroidManifest.xml",
Jooyung Han43d30252020-05-22 03:54:24 +0900601 {{- else if not .IsHostOnly}}
Anton Hanssonc29f0762021-03-29 16:10:06 +0100602 min_sdk_version: "{{.DefaultMinSdkVersion}}",
Colin Cross632987a2018-08-29 16:17:55 -0700603 {{- end}}
604 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700605 static_libs: [
Colin Cross632987a2018-08-29 16:17:55 -0700606 "{{.BpName}}-nodeps",
Colin Cross1aa7f262019-04-10 11:07:15 -0700607 {{- range .BpJarDeps}}
Colin Cross632987a2018-08-29 16:17:55 -0700608 "{{.}}",
609 {{- end}}
610 {{- range .BpAarDeps}}
611 "{{.}}",
612 {{- end}}
Paul Duffinbabaf072019-04-16 11:35:20 +0100613 {{- range .BpExtraStaticLibs}}
Colin Cross632987a2018-08-29 16:17:55 -0700614 "{{.}}",
615 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700616 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100617 {{- if .BpExtraLibs}}
618 libs: [
619 {{- range .BpExtraLibs}}
620 "{{.}}",
621 {{- end}}
622 ],
Alan Viverettebcbfc5f2021-09-13 17:12:28 +0000623 {{- end}}
Alan Viverette24658d02021-08-31 20:00:52 +0000624 {{- if .BpOptionalUsesLibs}}
625 optional_uses_libs: [
626 {{- range .BpOptionalUsesLibs}}
627 "{{.}}",
628 {{- end}}
629 ],
Paul Duffinbabaf072019-04-16 11:35:20 +0100630 {{- end}}
Colin Cross70dd38f2018-04-16 13:52:10 -0700631 java_version: "1.7",
632}
633`))
634
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000635var bazelTemplate = template.Must(template.New("bp").Parse(`
636{{.BazelImportTargetType}} (
637 name = "{{.BpName}}",
638 {{.BazelImportProperty}}: {{- if not .IsAar}}[{{- end}}"{{.ArtifactFile}}"{{- if not .IsAar}}]{{- end}},
639 visibility = ["//visibility:public"],
640 {{- if .IsAar}}
641 deps = [
642 {{- range .BazelJarDeps}}
643 "{{.}}",
644 {{- end}}
645 {{- range .BazelAarDeps}}
646 "{{.}}",
647 {{- end}}
648 {{- range .BpExtraStaticLibs}}
649 "{{.}}",
650 {{- end}}
651 {{- range .BpExtraLibs}}
652 "{{.}}",
653 {{- end}}
654 {{- range .BpOptionalUsesLibs}}
655 "{{.}}",
656 {{- end}}
657 ],
658 {{- end}}
659)
660`))
661
662var bazelDepsTemplate = template.Must(template.New("bp").Parse(`
663{{.BazelImportTargetType}} (
664 name = "{{.BpName}}",
665 {{.BazelImportProperty}} = {{- if not .IsAar}}[{{- end}}"{{.ArtifactFile}}"{{- if not .IsAar}}]{{- end}},
666 visibility = ["//visibility:public"],
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000667 exports = [
668 {{- range .BazelJarDeps}}
669 "{{.}}",
670 {{- end}}
671 {{- range .BazelAarDeps}}
672 "{{.}}",
673 {{- end}}
674 {{- range .BpExtraStaticLibs}}
675 "{{.}}",
676 {{- end}}
677 {{- range .BpExtraLibs}}
678 "{{.}}",
679 {{- end}}
680 {{- range .BpOptionalUsesLibs}}
681 "{{.}}",
682 {{- end}}
683 ],
684)
685`))
686
Colin Cross70dd38f2018-04-16 13:52:10 -0700687func parse(filename string) (*Pom, error) {
688 data, err := ioutil.ReadFile(filename)
689 if err != nil {
690 return nil, err
691 }
692
693 var pom Pom
694 err = xml.Unmarshal(data, &pom)
695 if err != nil {
696 return nil, err
697 }
698
699 if useVersion != "" && pom.Version != useVersion {
700 return nil, nil
701 }
702
703 if pom.Packaging == "" {
704 pom.Packaging = "jar"
705 }
706
707 pom.PomFile = filename
708 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
709
710 return &pom, nil
711}
712
713func rerunForRegen(filename string) error {
714 buf, err := ioutil.ReadFile(filename)
715 if err != nil {
716 return err
717 }
718
719 scanner := bufio.NewScanner(bytes.NewBuffer(buf))
720
721 // Skip the first line in the file
722 for i := 0; i < 2; i++ {
723 if !scanner.Scan() {
724 if scanner.Err() != nil {
725 return scanner.Err()
726 } else {
727 return fmt.Errorf("unexpected EOF")
728 }
729 }
730 }
731
732 // Extract the old args from the file
733 line := scanner.Text()
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000734 if strings.HasPrefix(line, "// pom2bp ") { // .bp file
Colin Cross70dd38f2018-04-16 13:52:10 -0700735 line = strings.TrimPrefix(line, "// pom2bp ")
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000736 } else if strings.HasPrefix(line, "// pom2mk ") { // .bp file converted from .mk file
Colin Cross70dd38f2018-04-16 13:52:10 -0700737 line = strings.TrimPrefix(line, "// pom2mk ")
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000738 } else if strings.HasPrefix(line, "# pom2mk ") { // .mk file
Colin Cross70dd38f2018-04-16 13:52:10 -0700739 line = strings.TrimPrefix(line, "# pom2mk ")
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000740 } else if strings.HasPrefix(line, "# pom2bp ") { // Bazel BUILD file
741 line = strings.TrimPrefix(line, "# pom2bp ")
Colin Cross70dd38f2018-04-16 13:52:10 -0700742 } else {
743 return fmt.Errorf("unexpected second line: %q", line)
744 }
745 args := strings.Split(line, " ")
746 lastArg := args[len(args)-1]
747 args = args[:len(args)-1]
748
749 // Append all current command line args except -regen <file> to the ones from the file
750 for i := 1; i < len(os.Args); i++ {
Colin Crosscf53e602018-06-26 15:27:20 -0700751 if os.Args[i] == "-regen" || os.Args[i] == "--regen" {
Colin Cross70dd38f2018-04-16 13:52:10 -0700752 i++
753 } else {
754 args = append(args, os.Args[i])
755 }
756 }
757 args = append(args, lastArg)
758
759 cmd := os.Args[0] + " " + strings.Join(args, " ")
760 // Re-exec pom2bp with the new arguments
761 output, err := exec.Command("/bin/sh", "-c", cmd).Output()
762 if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
763 return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
764 } else if err != nil {
765 return err
766 }
767
768 // If the old file was a .mk file, replace it with a .bp file
769 if filepath.Ext(filename) == ".mk" {
770 os.Remove(filename)
771 filename = strings.TrimSuffix(filename, ".mk") + ".bp"
772 }
773
774 return ioutil.WriteFile(filename, output, 0666)
775}
776
777func main() {
778 flag.Usage = func() {
779 fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos
780
781The tool will extract the necessary information from *.pom files to create an Android.bp whose
782aar libraries can be linked against when using AAPT2.
783
Alan Viverette24658d02021-08-31 20:00:52 +0000784Usage: %s [--rewrite <regex>=<replace>] [--exclude <module>] [--extra-static-libs <module>=<module>[,<module>]] [--extra-libs <module>=<module>[,<module>]] [--optional-uses-libs <module>=<module>[,<module>]] [<dir>] [-regen <file>]
Colin Cross70dd38f2018-04-16 13:52:10 -0700785
786 -rewrite <regex>=<replace>
787 rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite
788 option can be specified multiple times. When determining the Android.bp module for a given Maven
789 project, mappings are searched in the order they were specified. The first <regex> matching
790 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
791 the Android.bp module name using <replace>. If no matches are found, <artifactId> is used.
792 -exclude <module>
793 Don't put the specified module in the Android.bp file.
Paul Duffinbabaf072019-04-16 11:35:20 +0100794 -extra-static-libs <module>=<module>[,<module>]
795 Some Android.bp modules have transitive static dependencies that must be specified when they
796 are depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
797 This may be specified multiple times to declare these dependencies.
798 -extra-libs <module>=<module>[,<module>]
799 Some Android.bp modules have transitive runtime dependencies that must be specified when they
800 are depended upon (like androidx.test.rules requires android.test.base).
Colin Cross70dd38f2018-04-16 13:52:10 -0700801 This may be specified multiple times to declare these dependencies.
Alan Viverette24658d02021-08-31 20:00:52 +0000802 -optional-uses-libs <module>=<module>[,<module>]
803 Some Android.bp modules have optional dependencies (typically specified with <uses-library> in
804 the module's AndroidManifest.xml) that must be specified when they are depended upon (like
805 androidx.window:window optionally requires androidx.window:window-extensions).
806 This may be specified multiple times to declare these dependencies.
Colin Cross70dd38f2018-04-16 13:52:10 -0700807 -sdk-version <version>
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700808 Sets sdk_version: "<version>" for all modules.
Anton Hanssonc29f0762021-03-29 16:10:06 +0100809 -default-min-sdk-version
810 The default min_sdk_version to use for a module if one cannot be mined from AndroidManifest.xml
Colin Cross70dd38f2018-04-16 13:52:10 -0700811 -use-version <version>
812 If the maven directory contains multiple versions of artifacts and their pom files,
813 -use-version can be used to only write Android.bp files for a specific version of those artifacts.
Alan Viveretteefab5142022-09-19 16:26:00 +0000814 -write-cmd
815 Whether to write the command line arguments used to generate the build file as a comment at
816 the top of the build file itself.
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700817 -jetifier
818 Sets jetifier: true for all modules.
Colin Cross70dd38f2018-04-16 13:52:10 -0700819 <dir>
820 The directory to search for *.pom files under.
821 The contents are written to stdout, to be put in the current directory (often as Android.bp)
822 -regen <file>
823 Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it
824 ends with .mk).
825
826`, os.Args[0])
827 }
828
829 var regen string
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000830 var pom2build bool
Alan Viveretted3be6f52022-08-11 10:59:12 -0400831 var prepend string
Colin Cross70dd38f2018-04-16 13:52:10 -0700832
833 flag.Var(&excludes, "exclude", "Exclude module")
Paul Duffinbabaf072019-04-16 11:35:20 +0100834 flag.Var(&extraStaticLibs, "extra-static-libs", "Extra static dependencies needed when depending on a module")
835 flag.Var(&extraLibs, "extra-libs", "Extra runtime dependencies needed when depending on a module")
Alan Viverette24658d02021-08-31 20:00:52 +0000836 flag.Var(&optionalUsesLibs, "optional-uses-libs", "Extra optional dependencies needed when depending on a module")
Colin Cross70dd38f2018-04-16 13:52:10 -0700837 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Jeff Gastond4928532018-08-24 14:30:13 -0400838 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 +0100839 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 -0700840 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to sdk_version")
Anton Hanssonc29f0762021-03-29 16:10:06 +0100841 flag.StringVar(&defaultMinSdkVersion, "default-min-sdk-version", "24", "Default min_sdk_version to use, if one is not available from AndroidManifest.xml. Default: 24")
Colin Cross70dd38f2018-04-16 13:52:10 -0700842 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
Dan Willemsen52c90d82019-04-21 21:37:39 -0700843 flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
Alan Viveretteefab5142022-09-19 16:26:00 +0000844 flag.BoolVar(&writeCmd, "write-cmd", true, "Write command line arguments as a comment")
Dan Willemsen7fdab6e2019-04-20 21:47:14 -0700845 flag.BoolVar(&jetifier, "jetifier", false, "Sets jetifier: true on all modules")
Colin Cross70dd38f2018-04-16 13:52:10 -0700846 flag.StringVar(&regen, "regen", "", "Rewrite specified file")
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000847 flag.BoolVar(&pom2build, "pom2build", false, "If true, will generate a Bazel BUILD file *instead* of a .bp file")
Alan Viveretted3be6f52022-08-11 10:59:12 -0400848 flag.StringVar(&prepend, "prepend", "", "Path to a file containing text to insert at the beginning of the generated build file")
Colin Cross70dd38f2018-04-16 13:52:10 -0700849 flag.Parse()
850
851 if regen != "" {
852 err := rerunForRegen(regen)
853 if err != nil {
854 fmt.Fprintln(os.Stderr, err)
855 os.Exit(1)
856 }
857 os.Exit(0)
858 }
859
860 if flag.NArg() == 0 {
861 fmt.Fprintln(os.Stderr, "Directory argument is required")
862 os.Exit(1)
863 } else if flag.NArg() > 1 {
864 fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
865 os.Exit(1)
866 }
867
868 dir := flag.Arg(0)
869 absDir, err := filepath.Abs(dir)
870 if err != nil {
871 fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
872 os.Exit(1)
873 }
874
875 var filenames []string
876 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
877 if err != nil {
878 return err
879 }
880
881 name := info.Name()
882 if info.IsDir() {
883 if strings.HasPrefix(name, ".") {
884 return filepath.SkipDir
885 }
886 return nil
887 }
888
889 if strings.HasPrefix(name, ".") {
890 return nil
891 }
892
893 if strings.HasSuffix(name, ".pom") {
894 path, err = filepath.Rel(absDir, path)
895 if err != nil {
896 return err
897 }
898 filenames = append(filenames, filepath.Join(dir, path))
899 }
900 return nil
901 })
902 if err != nil {
903 fmt.Fprintln(os.Stderr, "Error walking files:", err)
904 os.Exit(1)
905 }
906
907 if len(filenames) == 0 {
908 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
909 os.Exit(1)
910 }
911
912 sort.Strings(filenames)
913
914 poms := []*Pom{}
915 modules := make(map[string]*Pom)
916 duplicate := false
917 for _, filename := range filenames {
918 pom, err := parse(filename)
919 if err != nil {
920 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
921 os.Exit(1)
922 }
923
924 if pom != nil {
925 key := pom.BpName()
926 if excludes[key] {
927 continue
928 }
929
930 if old, ok := modules[key]; ok {
931 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
932 duplicate = true
933 }
934
935 poms = append(poms, pom)
936 modules[key] = pom
937 }
938 }
939 if duplicate {
940 os.Exit(1)
941 }
942
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000943 if pom2build {
944 if err := InitRefreshMod(poms); err != nil {
945 fmt.Fprintf(os.Stderr, "Error in refreshmod: %s", err)
946 os.Exit(1)
947 }
948 BazelifyExtraDeps(extraStaticLibs, modules)
949 BazelifyExtraDeps(extraLibs, modules)
950 BazelifyExtraDeps(optionalUsesLibs, modules)
951 }
952
Colin Cross70dd38f2018-04-16 13:52:10 -0700953 for _, pom := range poms {
Colin Crosscf53e602018-06-26 15:27:20 -0700954 if pom.IsAar() {
955 err := pom.ExtractMinSdkVersion()
956 if err != nil {
Colin Crossfe5a3b72018-07-13 21:25:15 -0700957 fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err)
Colin Crosscf53e602018-06-26 15:27:20 -0700958 os.Exit(1)
959 }
960 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700961 pom.FixDeps(modules)
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000962 if pom2build {
963 pom.GetBazelDepNames(modules)
964 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700965 }
966
967 buf := &bytes.Buffer{}
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000968 commentString := "//"
969 if pom2build {
970 commentString = "#"
971 }
Alan Viveretteefab5142022-09-19 16:26:00 +0000972
973 fmt.Fprintln(buf, commentString, "This is a generated file. Do not modify directly.")
974
975 if writeCmd {
976 fmt.Fprintln(buf, commentString, "Automatically generated with:")
977 fmt.Fprintln(buf, commentString, "pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
978 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700979
Alan Viveretted3be6f52022-08-11 10:59:12 -0400980 if prepend != "" {
981 contents, err := ioutil.ReadFile(prepend)
982 if err != nil {
983 fmt.Fprintln(os.Stderr, "Error reading", prepend, err)
984 os.Exit(1)
985 }
986 fmt.Fprintln(buf, string(contents))
987 }
988
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000989 depsTemplate := bpDepsTemplate
990 template := bpTemplate
991 if pom2build {
992 depsTemplate = bazelDepsTemplate
993 template = bazelTemplate
994 }
Colin Cross70dd38f2018-04-16 13:52:10 -0700995
996 for _, pom := range poms {
997 var err error
Dan Willemsen52c90d82019-04-21 21:37:39 -0700998 if staticDeps {
Romain Jobredeaux89cb2242021-09-20 14:14:22 +0000999 err = depsTemplate.Execute(buf, pom)
Dan Willemsen52c90d82019-04-21 21:37:39 -07001000 } else {
Romain Jobredeaux89cb2242021-09-20 14:14:22 +00001001 err = template.Execute(buf, pom)
Dan Willemsen52c90d82019-04-21 21:37:39 -07001002 }
Colin Cross70dd38f2018-04-16 13:52:10 -07001003 if err != nil {
1004 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err)
1005 os.Exit(1)
1006 }
1007 }
1008
Romain Jobredeaux89cb2242021-09-20 14:14:22 +00001009 if pom2build {
1010 os.Stdout.WriteString(buf.String())
1011 } else {
1012 out, err := bpfix.Reformat(buf.String())
1013 if err != nil {
1014 fmt.Fprintln(os.Stderr, "Error formatting output", err)
1015 os.Exit(1)
1016 }
1017 os.Stdout.WriteString(out)
Colin Cross70dd38f2018-04-16 13:52:10 -07001018 }
1019
Colin Cross70dd38f2018-04-16 13:52:10 -07001020}