Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 18 | "archive/zip" |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 19 | "bufio" |
| 20 | "bytes" |
| 21 | "encoding/xml" |
| 22 | "flag" |
| 23 | "fmt" |
| 24 | "io/ioutil" |
| 25 | "os" |
| 26 | "os/exec" |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 27 | "path" |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 28 | "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 | |
| 39 | type RewriteNames []RewriteName |
| 40 | type RewriteName struct { |
| 41 | regexp *regexp.Regexp |
| 42 | repl string |
| 43 | } |
| 44 | |
| 45 | func (r *RewriteNames) String() string { |
| 46 | return "" |
| 47 | } |
| 48 | |
| 49 | func (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 | |
| 65 | func (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 | |
| 76 | var rewriteNames = RewriteNames{} |
| 77 | |
| 78 | type ExtraDeps map[string][]string |
| 79 | |
| 80 | func (d ExtraDeps) String() string { |
| 81 | return "" |
| 82 | } |
| 83 | |
| 84 | func (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 Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 93 | var extraStaticLibs = make(ExtraDeps) |
| 94 | |
| 95 | var extraLibs = make(ExtraDeps) |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 96 | |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 97 | var optionalUsesLibs = make(ExtraDeps) |
| 98 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 99 | type Exclude map[string]bool |
| 100 | |
| 101 | func (e Exclude) String() string { |
| 102 | return "" |
| 103 | } |
| 104 | |
| 105 | func (e Exclude) Set(v string) error { |
| 106 | e[v] = true |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | var excludes = make(Exclude) |
| 111 | |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame] | 112 | type HostModuleNames map[string]bool |
| 113 | |
| 114 | func (n HostModuleNames) IsHostModule(groupId string, artifactId string) bool { |
Colin Cross | 86bc9d4 | 2018-08-29 15:36:33 -0700 | [diff] [blame] | 115 | _, found := n[groupId+":"+artifactId] |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame] | 116 | return found |
| 117 | } |
| 118 | |
| 119 | func (n HostModuleNames) String() string { |
| 120 | return "" |
| 121 | } |
| 122 | |
| 123 | func (n HostModuleNames) Set(v string) error { |
| 124 | n[v] = true |
| 125 | return nil |
| 126 | } |
| 127 | |
| 128 | var hostModuleNames = HostModuleNames{} |
| 129 | |
Tony Mak | 8178500 | 2019-07-18 21:36:44 +0100 | [diff] [blame] | 130 | type HostAndDeviceModuleNames map[string]bool |
| 131 | |
| 132 | func (n HostAndDeviceModuleNames) IsHostAndDeviceModule(groupId string, artifactId string) bool { |
| 133 | _, found := n[groupId+":"+artifactId] |
| 134 | |
| 135 | return found |
| 136 | } |
| 137 | |
| 138 | func (n HostAndDeviceModuleNames) String() string { |
| 139 | return "" |
| 140 | } |
| 141 | |
| 142 | func (n HostAndDeviceModuleNames) Set(v string) error { |
| 143 | n[v] = true |
| 144 | return nil |
| 145 | } |
| 146 | |
| 147 | var hostAndDeviceModuleNames = HostAndDeviceModuleNames{} |
| 148 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 149 | var sdkVersion string |
Anton Hansson | c29f076 | 2021-03-29 16:10:06 +0100 | [diff] [blame] | 150 | var defaultMinSdkVersion string |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 151 | var useVersion string |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 152 | var staticDeps bool |
Alan Viverette | efab514 | 2022-09-19 16:26:00 +0000 | [diff] [blame] | 153 | var writeCmd bool |
Dan Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 154 | var jetifier bool |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 155 | |
| 156 | func 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 | |
| 166 | type Dependency struct { |
| 167 | XMLName xml.Name `xml:"dependency"` |
| 168 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 169 | BpTarget string `xml:"-"` |
| 170 | BazelTarget string `xml:"-"` |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 171 | |
| 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 | |
| 179 | func (d Dependency) BpName() string { |
| 180 | if d.BpTarget == "" { |
| 181 | d.BpTarget = rewriteNames.MavenToBp(d.GroupId, d.ArtifactId) |
| 182 | } |
| 183 | return d.BpTarget |
| 184 | } |
| 185 | |
| 186 | type Pom struct { |
| 187 | XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"` |
| 188 | |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 189 | PomFile string `xml:"-"` |
| 190 | ArtifactFile string `xml:"-"` |
| 191 | BpTarget string `xml:"-"` |
| 192 | MinSdkVersion string `xml:"-"` |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 193 | |
| 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 | |
| 202 | func (p Pom) IsAar() bool { |
| 203 | return p.Packaging == "aar" |
| 204 | } |
| 205 | |
| 206 | func (p Pom) IsJar() bool { |
| 207 | return p.Packaging == "jar" |
| 208 | } |
| 209 | |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 210 | func (p Pom) IsApk() bool { |
| 211 | return p.Packaging == "apk" |
| 212 | } |
| 213 | |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame] | 214 | func (p Pom) IsHostModule() bool { |
| 215 | return hostModuleNames.IsHostModule(p.GroupId, p.ArtifactId) |
| 216 | } |
| 217 | |
| 218 | func (p Pom) IsDeviceModule() bool { |
| 219 | return !p.IsHostModule() |
| 220 | } |
| 221 | |
Tony Mak | 8178500 | 2019-07-18 21:36:44 +0100 | [diff] [blame] | 222 | func (p Pom) IsHostAndDeviceModule() bool { |
| 223 | return hostAndDeviceModuleNames.IsHostAndDeviceModule(p.GroupId, p.ArtifactId) |
| 224 | } |
| 225 | |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 226 | func (p Pom) IsHostOnly() bool { |
| 227 | return p.IsHostModule() && !p.IsHostAndDeviceModule() |
| 228 | } |
| 229 | |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 230 | func (p Pom) ModuleType() string { |
| 231 | if p.IsAar() { |
| 232 | return "android_library" |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 233 | } else if p.IsHostOnly() { |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 234 | return "java_library_host" |
| 235 | } else { |
| 236 | return "java_library_static" |
| 237 | } |
| 238 | } |
| 239 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 240 | func (p Pom) BazelTargetType() string { |
| 241 | if p.IsAar() { |
| 242 | return "android_library" |
| 243 | } else { |
| 244 | return "java_library" |
| 245 | } |
| 246 | } |
| 247 | |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 248 | func (p Pom) ImportModuleType() string { |
| 249 | if p.IsAar() { |
| 250 | return "android_library_import" |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 251 | } else if p.IsApk() { |
| 252 | return "android_app_import" |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 253 | } else if p.IsHostOnly() { |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 254 | return "java_import_host" |
| 255 | } else { |
| 256 | return "java_import" |
| 257 | } |
| 258 | } |
| 259 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 260 | func (p Pom) BazelImportTargetType() string { |
| 261 | if p.IsAar() { |
| 262 | return "aar_import" |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 263 | } else if p.IsApk() { |
| 264 | return "apk_import" |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 265 | } else { |
| 266 | return "java_import" |
| 267 | } |
| 268 | } |
| 269 | |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 270 | func (p Pom) ImportProperty() string { |
| 271 | if p.IsAar() { |
| 272 | return "aars" |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 273 | } else if p.IsApk() { |
| 274 | return "apk" |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 275 | } else { |
| 276 | return "jars" |
| 277 | } |
| 278 | } |
| 279 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 280 | func (p Pom) BazelImportProperty() string { |
| 281 | if p.IsAar() { |
| 282 | return "aar" |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 283 | } else if p.IsApk() { |
| 284 | return "apk" |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 285 | } else { |
| 286 | return "jars" |
| 287 | } |
| 288 | } |
| 289 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 290 | func (p Pom) BpName() string { |
| 291 | if p.BpTarget == "" { |
| 292 | p.BpTarget = rewriteNames.MavenToBp(p.GroupId, p.ArtifactId) |
| 293 | } |
| 294 | return p.BpTarget |
| 295 | } |
| 296 | |
| 297 | func (p Pom) BpJarDeps() []string { |
| 298 | return p.BpDeps("jar", []string{"compile", "runtime"}) |
| 299 | } |
| 300 | |
| 301 | func (p Pom) BpAarDeps() []string { |
| 302 | return p.BpDeps("aar", []string{"compile", "runtime"}) |
| 303 | } |
| 304 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 305 | func (p Pom) BazelJarDeps() []string { |
| 306 | return p.BazelDeps("jar", []string{"compile", "runtime"}) |
| 307 | } |
| 308 | |
| 309 | func (p Pom) BazelAarDeps() []string { |
| 310 | return p.BazelDeps("aar", []string{"compile", "runtime"}) |
| 311 | } |
| 312 | |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 313 | func (p Pom) BpExtraStaticLibs() []string { |
| 314 | return extraStaticLibs[p.BpName()] |
| 315 | } |
| 316 | |
| 317 | func (p Pom) BpExtraLibs() []string { |
| 318 | return extraLibs[p.BpName()] |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 321 | func (p Pom) BpOptionalUsesLibs() []string { |
| 322 | return optionalUsesLibs[p.BpName()] |
| 323 | } |
| 324 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 325 | // BpDeps obtains dependencies filtered by type and scope. The results of this |
| 326 | // method are formatted as Android.bp targets, e.g. run through MavenToBp rules. |
| 327 | func (p Pom) BpDeps(typeExt string, scopes []string) []string { |
| 328 | var ret []string |
| 329 | for _, d := range p.Dependencies { |
| 330 | if d.Type != typeExt || !InList(d.Scope, scopes) { |
| 331 | continue |
| 332 | } |
| 333 | name := rewriteNames.MavenToBp(d.GroupId, d.ArtifactId) |
| 334 | ret = append(ret, name) |
| 335 | } |
| 336 | return ret |
| 337 | } |
| 338 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 339 | // BazelDeps obtains dependencies filtered by type and scope. The results of this |
| 340 | // method are formatted as Bazel BUILD targets. |
| 341 | func (p Pom) BazelDeps(typeExt string, scopes []string) []string { |
| 342 | var ret []string |
| 343 | for _, d := range p.Dependencies { |
| 344 | if d.Type != typeExt || !InList(d.Scope, scopes) { |
| 345 | continue |
| 346 | } |
| 347 | ret = append(ret, d.BazelTarget) |
| 348 | } |
| 349 | return ret |
| 350 | } |
| 351 | |
| 352 | func PathModVars() (string, string, string) { |
| 353 | cmd := "/bin/bash" |
| 354 | androidTop := os.Getenv("ANDROID_BUILD_TOP") |
| 355 | envSetupSh := path.Join(androidTop, "build/envsetup.sh") |
| 356 | return cmd, androidTop, envSetupSh |
| 357 | } |
| 358 | |
| 359 | func InitRefreshMod(poms []*Pom) error { |
| 360 | cmd, _, envSetupSh := PathModVars() |
| 361 | // refreshmod is expensive, so if pathmod is already working we can skip it. |
| 362 | _, err := exec.Command(cmd, "-c", ". "+envSetupSh+" && pathmod "+poms[0].BpName()).Output() |
| 363 | if exitErr, _ := err.(*exec.ExitError); exitErr != nil || err != nil { |
| 364 | _, err := exec.Command(cmd, "-c", ". "+envSetupSh+" && refreshmod").Output() |
| 365 | if exitErr, _ := err.(*exec.ExitError); exitErr != nil { |
| 366 | return fmt.Errorf("failed to run %s\n%s\ntry running lunch.", cmd, string(exitErr.Stderr)) |
| 367 | } else if err != nil { |
| 368 | return err |
| 369 | } |
| 370 | } |
| 371 | return nil |
| 372 | } |
| 373 | |
| 374 | func BazelifyExtraDeps(extraDeps ExtraDeps, modules map[string]*Pom) error { |
| 375 | for _, deps := range extraDeps { |
| 376 | for _, dep := range deps { |
| 377 | bazelName, err := BpNameToBazelTarget(dep, modules) |
| 378 | if err != nil { |
| 379 | return err |
| 380 | } |
| 381 | dep = bazelName |
| 382 | } |
| 383 | |
| 384 | } |
| 385 | return nil |
| 386 | } |
| 387 | |
| 388 | func (p *Pom) GetBazelDepNames(modules map[string]*Pom) error { |
| 389 | for _, d := range p.Dependencies { |
| 390 | bazelName, err := BpNameToBazelTarget(d.BpName(), modules) |
| 391 | if err != nil { |
| 392 | return err |
| 393 | } |
| 394 | d.BazelTarget = bazelName |
| 395 | } |
| 396 | return nil |
| 397 | } |
| 398 | |
| 399 | func BpNameToBazelTarget(bpName string, modules map[string]*Pom) (string, error) { |
| 400 | cmd, androidTop, envSetupSh := PathModVars() |
| 401 | |
| 402 | if _, ok := modules[bpName]; ok { |
| 403 | // We've seen the POM for this dependency, it will be local to the output BUILD file |
| 404 | return ":" + bpName, nil |
| 405 | } else { |
| 406 | // we don't have the POM for this artifact, find and use the fully qualified target name. |
| 407 | output, err := exec.Command(cmd, "-c", ". "+envSetupSh+" && pathmod "+bpName).Output() |
| 408 | if exitErr, _ := err.(*exec.ExitError); exitErr != nil { |
| 409 | return "", fmt.Errorf("failed to run %s %s\n%s", cmd, bpName, string(exitErr.Stderr)) |
| 410 | } else if err != nil { |
| 411 | return "", err |
| 412 | } |
| 413 | relPath := "" |
| 414 | for _, line := range strings.Fields(string(output)) { |
| 415 | if strings.Contains(line, androidTop) { |
| 416 | relPath = strings.TrimPrefix(line, androidTop) |
| 417 | relPath = strings.TrimLeft(relPath, "/") |
| 418 | } |
| 419 | } |
| 420 | return "//" + relPath + ":" + bpName, nil |
| 421 | } |
| 422 | } |
| 423 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 424 | func (p Pom) SdkVersion() string { |
| 425 | return sdkVersion |
| 426 | } |
| 427 | |
Anton Hansson | c29f076 | 2021-03-29 16:10:06 +0100 | [diff] [blame] | 428 | func (p Pom) DefaultMinSdkVersion() string { |
| 429 | return defaultMinSdkVersion |
| 430 | } |
| 431 | |
Dan Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 432 | func (p Pom) Jetifier() bool { |
| 433 | return jetifier |
| 434 | } |
| 435 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 436 | func (p *Pom) FixDeps(modules map[string]*Pom) { |
| 437 | for _, d := range p.Dependencies { |
| 438 | if d.Type == "" { |
| 439 | if depPom, ok := modules[d.BpName()]; ok { |
| 440 | // We've seen the POM for this dependency, use its packaging |
| 441 | // as the dependency type rather than Maven spec default. |
| 442 | d.Type = depPom.Packaging |
| 443 | } else { |
| 444 | // Dependency type was not specified and we don't have the POM |
| 445 | // for this artifact, use the default from Maven spec. |
| 446 | d.Type = "jar" |
| 447 | } |
| 448 | } |
| 449 | if d.Scope == "" { |
| 450 | // Scope was not specified, use the default from Maven spec. |
| 451 | d.Scope = "compile" |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 456 | // ExtractMinSdkVersion extracts the minSdkVersion from the AndroidManifest.xml file inside an aar file, or sets it |
| 457 | // to "current" if it is not present. |
| 458 | func (p *Pom) ExtractMinSdkVersion() error { |
| 459 | aar, err := zip.OpenReader(p.ArtifactFile) |
| 460 | if err != nil { |
| 461 | return err |
| 462 | } |
| 463 | defer aar.Close() |
| 464 | |
| 465 | var manifest *zip.File |
| 466 | for _, f := range aar.File { |
| 467 | if f.Name == "AndroidManifest.xml" { |
| 468 | manifest = f |
| 469 | break |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if manifest == nil { |
| 474 | return fmt.Errorf("failed to find AndroidManifest.xml in %s", p.ArtifactFile) |
| 475 | } |
| 476 | |
| 477 | r, err := manifest.Open() |
| 478 | if err != nil { |
| 479 | return err |
| 480 | } |
| 481 | defer r.Close() |
| 482 | |
| 483 | decoder := xml.NewDecoder(r) |
| 484 | |
| 485 | manifestData := struct { |
| 486 | XMLName xml.Name `xml:"manifest"` |
| 487 | Uses_sdk struct { |
| 488 | MinSdkVersion string `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"` |
| 489 | } `xml:"uses-sdk"` |
| 490 | }{} |
| 491 | |
| 492 | err = decoder.Decode(&manifestData) |
| 493 | if err != nil { |
| 494 | return err |
| 495 | } |
| 496 | |
| 497 | p.MinSdkVersion = manifestData.Uses_sdk.MinSdkVersion |
| 498 | if p.MinSdkVersion == "" { |
| 499 | p.MinSdkVersion = "current" |
| 500 | } |
| 501 | |
| 502 | return nil |
| 503 | } |
| 504 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 505 | var bpTemplate = template.Must(template.New("bp").Parse(` |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 506 | {{.ImportModuleType}} { |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 507 | name: "{{.BpName}}", |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 508 | {{- if .IsApk}} |
| 509 | {{.ImportProperty}}: "{{.ArtifactFile}}", |
| 510 | {{- else}} |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 511 | {{.ImportProperty}}: ["{{.ArtifactFile}}"], |
| 512 | sdk_version: "{{.SdkVersion}}", |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 513 | {{- end}} |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 514 | {{- if .Jetifier}} |
| 515 | jetifier: true, |
| 516 | {{- end}} |
Tony Mak | 8178500 | 2019-07-18 21:36:44 +0100 | [diff] [blame] | 517 | {{- if .IsHostAndDeviceModule}} |
| 518 | host_supported: true, |
| 519 | {{- end}} |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 520 | {{- if not .IsHostOnly}} |
| 521 | apex_available: [ |
| 522 | "//apex_available:platform", |
| 523 | "//apex_available:anyapex", |
| 524 | ], |
| 525 | {{- end}} |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 526 | {{- if .IsAar}} |
| 527 | min_sdk_version: "{{.MinSdkVersion}}", |
| 528 | static_libs: [ |
| 529 | {{- range .BpJarDeps}} |
| 530 | "{{.}}", |
| 531 | {{- end}} |
| 532 | {{- range .BpAarDeps}} |
| 533 | "{{.}}", |
| 534 | {{- end}} |
| 535 | {{- range .BpExtraStaticLibs}} |
| 536 | "{{.}}", |
| 537 | {{- end}} |
| 538 | ], |
| 539 | {{- if .BpExtraLibs}} |
| 540 | libs: [ |
| 541 | {{- range .BpExtraLibs}} |
| 542 | "{{.}}", |
| 543 | {{- end}} |
| 544 | ], |
Alan Viverette | bcbfc5f | 2021-09-13 17:12:28 +0000 | [diff] [blame] | 545 | {{- end}} |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 546 | {{- if .BpOptionalUsesLibs}} |
| 547 | optional_uses_libs: [ |
| 548 | {{- range .BpOptionalUsesLibs}} |
| 549 | "{{.}}", |
| 550 | {{- end}} |
| 551 | ], |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 552 | {{- end}} |
Anton Hansson | ebfbad2 | 2021-04-06 19:21:34 +0000 | [diff] [blame] | 553 | {{- else if not .IsHostOnly}} |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 554 | {{- if not .IsApk}} |
Anton Hansson | ebfbad2 | 2021-04-06 19:21:34 +0000 | [diff] [blame] | 555 | min_sdk_version: "{{.DefaultMinSdkVersion}}", |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 556 | {{- end}} |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 557 | {{- end}} |
| 558 | {{- if .IsApk}} |
| 559 | presigned: true |
| 560 | {{- end}} |
| 561 | |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 562 | } |
| 563 | `)) |
| 564 | |
| 565 | var bpDepsTemplate = template.Must(template.New("bp").Parse(` |
| 566 | {{.ImportModuleType}} { |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 567 | name: "{{.BpName}}-nodeps", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 568 | {{.ImportProperty}}: ["{{.ArtifactFile}}"], |
| 569 | sdk_version: "{{.SdkVersion}}", |
Dan Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 570 | {{- if .Jetifier}} |
| 571 | jetifier: true, |
| 572 | {{- end}} |
Tony Mak | 8178500 | 2019-07-18 21:36:44 +0100 | [diff] [blame] | 573 | {{- if .IsHostAndDeviceModule}} |
| 574 | host_supported: true, |
| 575 | {{- end}} |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 576 | {{- if not .IsHostOnly}} |
| 577 | apex_available: [ |
| 578 | "//apex_available:platform", |
| 579 | "//apex_available:anyapex", |
| 580 | ], |
| 581 | {{- end}} |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 582 | {{- if .IsAar}} |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 583 | min_sdk_version: "{{.MinSdkVersion}}", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 584 | static_libs: [ |
Colin Cross | 1aa7f26 | 2019-04-10 11:07:15 -0700 | [diff] [blame] | 585 | {{- range .BpJarDeps}} |
| 586 | "{{.}}", |
| 587 | {{- end}} |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 588 | {{- range .BpAarDeps}} |
| 589 | "{{.}}", |
| 590 | {{- end}} |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 591 | {{- range .BpExtraStaticLibs}} |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 592 | "{{.}}", |
| 593 | {{- end}} |
| 594 | ], |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 595 | {{- if .BpExtraLibs}} |
| 596 | libs: [ |
| 597 | {{- range .BpExtraLibs}} |
| 598 | "{{.}}", |
| 599 | {{- end}} |
| 600 | ], |
| 601 | {{- end}} |
Anton Hansson | ebfbad2 | 2021-04-06 19:21:34 +0000 | [diff] [blame] | 602 | {{- else if not .IsHostOnly}} |
| 603 | min_sdk_version: "{{.DefaultMinSdkVersion}}", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 604 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 607 | {{.ModuleType}} { |
| 608 | name: "{{.BpName}}", |
| 609 | {{- if .IsDeviceModule}} |
| 610 | sdk_version: "{{.SdkVersion}}", |
Tony Mak | 8178500 | 2019-07-18 21:36:44 +0100 | [diff] [blame] | 611 | {{- if .IsHostAndDeviceModule}} |
| 612 | host_supported: true, |
| 613 | {{- end}} |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 614 | {{- if not .IsHostOnly}} |
| 615 | apex_available: [ |
| 616 | "//apex_available:platform", |
| 617 | "//apex_available:anyapex", |
| 618 | ], |
| 619 | {{- end}} |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 620 | {{- if .IsAar}} |
Colin Cross | 461ba49 | 2018-07-10 13:45:30 -0700 | [diff] [blame] | 621 | min_sdk_version: "{{.MinSdkVersion}}", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 622 | manifest: "manifests/{{.BpName}}/AndroidManifest.xml", |
Jooyung Han | 43d3025 | 2020-05-22 03:54:24 +0900 | [diff] [blame] | 623 | {{- else if not .IsHostOnly}} |
Anton Hansson | c29f076 | 2021-03-29 16:10:06 +0100 | [diff] [blame] | 624 | min_sdk_version: "{{.DefaultMinSdkVersion}}", |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 625 | {{- end}} |
| 626 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 627 | static_libs: [ |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 628 | "{{.BpName}}-nodeps", |
Colin Cross | 1aa7f26 | 2019-04-10 11:07:15 -0700 | [diff] [blame] | 629 | {{- range .BpJarDeps}} |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 630 | "{{.}}", |
| 631 | {{- end}} |
| 632 | {{- range .BpAarDeps}} |
| 633 | "{{.}}", |
| 634 | {{- end}} |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 635 | {{- range .BpExtraStaticLibs}} |
Colin Cross | 632987a | 2018-08-29 16:17:55 -0700 | [diff] [blame] | 636 | "{{.}}", |
| 637 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 638 | ], |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 639 | {{- if .BpExtraLibs}} |
| 640 | libs: [ |
| 641 | {{- range .BpExtraLibs}} |
| 642 | "{{.}}", |
| 643 | {{- end}} |
| 644 | ], |
Alan Viverette | bcbfc5f | 2021-09-13 17:12:28 +0000 | [diff] [blame] | 645 | {{- end}} |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 646 | {{- if .BpOptionalUsesLibs}} |
| 647 | optional_uses_libs: [ |
| 648 | {{- range .BpOptionalUsesLibs}} |
| 649 | "{{.}}", |
| 650 | {{- end}} |
| 651 | ], |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 652 | {{- end}} |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 653 | java_version: "1.7", |
| 654 | } |
| 655 | `)) |
| 656 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 657 | var bazelTemplate = template.Must(template.New("bp").Parse(` |
| 658 | {{.BazelImportTargetType}} ( |
| 659 | name = "{{.BpName}}", |
| 660 | {{.BazelImportProperty}}: {{- if not .IsAar}}[{{- end}}"{{.ArtifactFile}}"{{- if not .IsAar}}]{{- end}}, |
| 661 | visibility = ["//visibility:public"], |
| 662 | {{- if .IsAar}} |
| 663 | deps = [ |
| 664 | {{- range .BazelJarDeps}} |
| 665 | "{{.}}", |
| 666 | {{- end}} |
| 667 | {{- range .BazelAarDeps}} |
| 668 | "{{.}}", |
| 669 | {{- end}} |
| 670 | {{- range .BpExtraStaticLibs}} |
| 671 | "{{.}}", |
| 672 | {{- end}} |
| 673 | {{- range .BpExtraLibs}} |
| 674 | "{{.}}", |
| 675 | {{- end}} |
| 676 | {{- range .BpOptionalUsesLibs}} |
| 677 | "{{.}}", |
| 678 | {{- end}} |
| 679 | ], |
| 680 | {{- end}} |
| 681 | ) |
| 682 | `)) |
| 683 | |
| 684 | var bazelDepsTemplate = template.Must(template.New("bp").Parse(` |
| 685 | {{.BazelImportTargetType}} ( |
| 686 | name = "{{.BpName}}", |
| 687 | {{.BazelImportProperty}} = {{- if not .IsAar}}[{{- end}}"{{.ArtifactFile}}"{{- if not .IsAar}}]{{- end}}, |
| 688 | visibility = ["//visibility:public"], |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 689 | exports = [ |
| 690 | {{- range .BazelJarDeps}} |
| 691 | "{{.}}", |
| 692 | {{- end}} |
| 693 | {{- range .BazelAarDeps}} |
| 694 | "{{.}}", |
| 695 | {{- end}} |
| 696 | {{- range .BpExtraStaticLibs}} |
| 697 | "{{.}}", |
| 698 | {{- end}} |
| 699 | {{- range .BpExtraLibs}} |
| 700 | "{{.}}", |
| 701 | {{- end}} |
| 702 | {{- range .BpOptionalUsesLibs}} |
| 703 | "{{.}}", |
| 704 | {{- end}} |
| 705 | ], |
| 706 | ) |
| 707 | `)) |
| 708 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 709 | func parse(filename string) (*Pom, error) { |
| 710 | data, err := ioutil.ReadFile(filename) |
| 711 | if err != nil { |
| 712 | return nil, err |
| 713 | } |
| 714 | |
| 715 | var pom Pom |
| 716 | err = xml.Unmarshal(data, &pom) |
| 717 | if err != nil { |
| 718 | return nil, err |
| 719 | } |
| 720 | |
| 721 | if useVersion != "" && pom.Version != useVersion { |
| 722 | return nil, nil |
| 723 | } |
| 724 | |
| 725 | if pom.Packaging == "" { |
| 726 | pom.Packaging = "jar" |
| 727 | } |
| 728 | |
| 729 | pom.PomFile = filename |
| 730 | pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging |
| 731 | |
| 732 | return &pom, nil |
| 733 | } |
| 734 | |
| 735 | func rerunForRegen(filename string) error { |
| 736 | buf, err := ioutil.ReadFile(filename) |
| 737 | if err != nil { |
| 738 | return err |
| 739 | } |
| 740 | |
| 741 | scanner := bufio.NewScanner(bytes.NewBuffer(buf)) |
| 742 | |
| 743 | // Skip the first line in the file |
| 744 | for i := 0; i < 2; i++ { |
| 745 | if !scanner.Scan() { |
| 746 | if scanner.Err() != nil { |
| 747 | return scanner.Err() |
| 748 | } else { |
| 749 | return fmt.Errorf("unexpected EOF") |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // Extract the old args from the file |
| 755 | line := scanner.Text() |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 756 | if strings.HasPrefix(line, "// pom2bp ") { // .bp file |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 757 | line = strings.TrimPrefix(line, "// pom2bp ") |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 758 | } else if strings.HasPrefix(line, "// pom2mk ") { // .bp file converted from .mk file |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 759 | line = strings.TrimPrefix(line, "// pom2mk ") |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 760 | } else if strings.HasPrefix(line, "# pom2mk ") { // .mk file |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 761 | line = strings.TrimPrefix(line, "# pom2mk ") |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 762 | } else if strings.HasPrefix(line, "# pom2bp ") { // Bazel BUILD file |
| 763 | line = strings.TrimPrefix(line, "# pom2bp ") |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 764 | } else { |
| 765 | return fmt.Errorf("unexpected second line: %q", line) |
| 766 | } |
| 767 | args := strings.Split(line, " ") |
| 768 | lastArg := args[len(args)-1] |
| 769 | args = args[:len(args)-1] |
| 770 | |
| 771 | // Append all current command line args except -regen <file> to the ones from the file |
| 772 | for i := 1; i < len(os.Args); i++ { |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 773 | if os.Args[i] == "-regen" || os.Args[i] == "--regen" { |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 774 | i++ |
| 775 | } else { |
| 776 | args = append(args, os.Args[i]) |
| 777 | } |
| 778 | } |
| 779 | args = append(args, lastArg) |
| 780 | |
| 781 | cmd := os.Args[0] + " " + strings.Join(args, " ") |
| 782 | // Re-exec pom2bp with the new arguments |
| 783 | output, err := exec.Command("/bin/sh", "-c", cmd).Output() |
| 784 | if exitErr, _ := err.(*exec.ExitError); exitErr != nil { |
| 785 | return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr)) |
| 786 | } else if err != nil { |
| 787 | return err |
| 788 | } |
| 789 | |
| 790 | // If the old file was a .mk file, replace it with a .bp file |
| 791 | if filepath.Ext(filename) == ".mk" { |
| 792 | os.Remove(filename) |
| 793 | filename = strings.TrimSuffix(filename, ".mk") + ".bp" |
| 794 | } |
| 795 | |
| 796 | return ioutil.WriteFile(filename, output, 0666) |
| 797 | } |
| 798 | |
| 799 | func main() { |
| 800 | flag.Usage = func() { |
| 801 | fmt.Fprintf(os.Stderr, `pom2bp, a tool to create Android.bp files from maven repos |
| 802 | |
| 803 | The tool will extract the necessary information from *.pom files to create an Android.bp whose |
| 804 | aar libraries can be linked against when using AAPT2. |
| 805 | |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 806 | Usage: %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 Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 807 | |
| 808 | -rewrite <regex>=<replace> |
| 809 | rewrite can be used to specify mappings between Maven projects and Android.bp modules. The -rewrite |
| 810 | option can be specified multiple times. When determining the Android.bp module for a given Maven |
| 811 | project, mappings are searched in the order they were specified. The first <regex> matching |
| 812 | either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate |
| 813 | the Android.bp module name using <replace>. If no matches are found, <artifactId> is used. |
| 814 | -exclude <module> |
| 815 | Don't put the specified module in the Android.bp file. |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 816 | -extra-static-libs <module>=<module>[,<module>] |
| 817 | Some Android.bp modules have transitive static dependencies that must be specified when they |
| 818 | are depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat). |
| 819 | This may be specified multiple times to declare these dependencies. |
| 820 | -extra-libs <module>=<module>[,<module>] |
| 821 | Some Android.bp modules have transitive runtime dependencies that must be specified when they |
| 822 | are depended upon (like androidx.test.rules requires android.test.base). |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 823 | This may be specified multiple times to declare these dependencies. |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 824 | -optional-uses-libs <module>=<module>[,<module>] |
| 825 | Some Android.bp modules have optional dependencies (typically specified with <uses-library> in |
| 826 | the module's AndroidManifest.xml) that must be specified when they are depended upon (like |
| 827 | androidx.window:window optionally requires androidx.window:window-extensions). |
| 828 | This may be specified multiple times to declare these dependencies. |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 829 | -sdk-version <version> |
Dan Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 830 | Sets sdk_version: "<version>" for all modules. |
Anton Hansson | c29f076 | 2021-03-29 16:10:06 +0100 | [diff] [blame] | 831 | -default-min-sdk-version |
| 832 | The default min_sdk_version to use for a module if one cannot be mined from AndroidManifest.xml |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 833 | -use-version <version> |
| 834 | If the maven directory contains multiple versions of artifacts and their pom files, |
| 835 | -use-version can be used to only write Android.bp files for a specific version of those artifacts. |
Alan Viverette | efab514 | 2022-09-19 16:26:00 +0000 | [diff] [blame] | 836 | -write-cmd |
| 837 | Whether to write the command line arguments used to generate the build file as a comment at |
| 838 | the top of the build file itself. |
Dan Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 839 | -jetifier |
| 840 | Sets jetifier: true for all modules. |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 841 | <dir> |
| 842 | The directory to search for *.pom files under. |
| 843 | The contents are written to stdout, to be put in the current directory (often as Android.bp) |
| 844 | -regen <file> |
| 845 | Read arguments from <file> and overwrite it (if it ends with .bp) or move it to .bp (if it |
| 846 | ends with .mk). |
| 847 | |
| 848 | `, os.Args[0]) |
| 849 | } |
| 850 | |
| 851 | var regen string |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 852 | var pom2build bool |
Alan Viverette | d3be6f5 | 2022-08-11 10:59:12 -0400 | [diff] [blame] | 853 | var prepend string |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 854 | |
| 855 | flag.Var(&excludes, "exclude", "Exclude module") |
Paul Duffin | babaf07 | 2019-04-16 11:35:20 +0100 | [diff] [blame] | 856 | flag.Var(&extraStaticLibs, "extra-static-libs", "Extra static dependencies needed when depending on a module") |
| 857 | flag.Var(&extraLibs, "extra-libs", "Extra runtime dependencies needed when depending on a module") |
Alan Viverette | 24658d0 | 2021-08-31 20:00:52 +0000 | [diff] [blame] | 858 | flag.Var(&optionalUsesLibs, "optional-uses-libs", "Extra optional dependencies needed when depending on a module") |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 859 | flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names") |
Jeff Gaston | d492853 | 2018-08-24 14:30:13 -0400 | [diff] [blame] | 860 | flag.Var(&hostModuleNames, "host", "Specifies that the corresponding module (specified in the form 'module.group:module.artifact') is a host module") |
Tony Mak | 8178500 | 2019-07-18 21:36:44 +0100 | [diff] [blame] | 861 | 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 Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 862 | flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to sdk_version") |
Anton Hansson | c29f076 | 2021-03-29 16:10:06 +0100 | [diff] [blame] | 863 | 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 Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 864 | flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version") |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 865 | flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies") |
Alan Viverette | efab514 | 2022-09-19 16:26:00 +0000 | [diff] [blame] | 866 | flag.BoolVar(&writeCmd, "write-cmd", true, "Write command line arguments as a comment") |
Dan Willemsen | 7fdab6e | 2019-04-20 21:47:14 -0700 | [diff] [blame] | 867 | flag.BoolVar(&jetifier, "jetifier", false, "Sets jetifier: true on all modules") |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 868 | flag.StringVar(®en, "regen", "", "Rewrite specified file") |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 869 | flag.BoolVar(&pom2build, "pom2build", false, "If true, will generate a Bazel BUILD file *instead* of a .bp file") |
Alan Viverette | d3be6f5 | 2022-08-11 10:59:12 -0400 | [diff] [blame] | 870 | flag.StringVar(&prepend, "prepend", "", "Path to a file containing text to insert at the beginning of the generated build file") |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 871 | flag.Parse() |
| 872 | |
| 873 | if regen != "" { |
| 874 | err := rerunForRegen(regen) |
| 875 | if err != nil { |
| 876 | fmt.Fprintln(os.Stderr, err) |
| 877 | os.Exit(1) |
| 878 | } |
| 879 | os.Exit(0) |
| 880 | } |
| 881 | |
| 882 | if flag.NArg() == 0 { |
| 883 | fmt.Fprintln(os.Stderr, "Directory argument is required") |
| 884 | os.Exit(1) |
| 885 | } else if flag.NArg() > 1 { |
| 886 | fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " ")) |
| 887 | os.Exit(1) |
| 888 | } |
| 889 | |
| 890 | dir := flag.Arg(0) |
| 891 | absDir, err := filepath.Abs(dir) |
| 892 | if err != nil { |
| 893 | fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err) |
| 894 | os.Exit(1) |
| 895 | } |
| 896 | |
| 897 | var filenames []string |
| 898 | err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error { |
| 899 | if err != nil { |
| 900 | return err |
| 901 | } |
| 902 | |
| 903 | name := info.Name() |
| 904 | if info.IsDir() { |
| 905 | if strings.HasPrefix(name, ".") { |
| 906 | return filepath.SkipDir |
| 907 | } |
| 908 | return nil |
| 909 | } |
| 910 | |
| 911 | if strings.HasPrefix(name, ".") { |
| 912 | return nil |
| 913 | } |
| 914 | |
| 915 | if strings.HasSuffix(name, ".pom") { |
| 916 | path, err = filepath.Rel(absDir, path) |
| 917 | if err != nil { |
| 918 | return err |
| 919 | } |
| 920 | filenames = append(filenames, filepath.Join(dir, path)) |
| 921 | } |
| 922 | return nil |
| 923 | }) |
| 924 | if err != nil { |
| 925 | fmt.Fprintln(os.Stderr, "Error walking files:", err) |
| 926 | os.Exit(1) |
| 927 | } |
| 928 | |
| 929 | if len(filenames) == 0 { |
| 930 | fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir) |
| 931 | os.Exit(1) |
| 932 | } |
| 933 | |
| 934 | sort.Strings(filenames) |
| 935 | |
| 936 | poms := []*Pom{} |
| 937 | modules := make(map[string]*Pom) |
| 938 | duplicate := false |
| 939 | for _, filename := range filenames { |
| 940 | pom, err := parse(filename) |
| 941 | if err != nil { |
| 942 | fmt.Fprintln(os.Stderr, "Error converting", filename, err) |
| 943 | os.Exit(1) |
| 944 | } |
| 945 | |
| 946 | if pom != nil { |
| 947 | key := pom.BpName() |
| 948 | if excludes[key] { |
| 949 | continue |
| 950 | } |
| 951 | |
| 952 | if old, ok := modules[key]; ok { |
| 953 | fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile) |
| 954 | duplicate = true |
| 955 | } |
| 956 | |
| 957 | poms = append(poms, pom) |
| 958 | modules[key] = pom |
| 959 | } |
| 960 | } |
| 961 | if duplicate { |
| 962 | os.Exit(1) |
| 963 | } |
| 964 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 965 | if pom2build { |
| 966 | if err := InitRefreshMod(poms); err != nil { |
| 967 | fmt.Fprintf(os.Stderr, "Error in refreshmod: %s", err) |
| 968 | os.Exit(1) |
| 969 | } |
| 970 | BazelifyExtraDeps(extraStaticLibs, modules) |
| 971 | BazelifyExtraDeps(extraLibs, modules) |
| 972 | BazelifyExtraDeps(optionalUsesLibs, modules) |
| 973 | } |
| 974 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 975 | for _, pom := range poms { |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 976 | if pom.IsAar() { |
| 977 | err := pom.ExtractMinSdkVersion() |
| 978 | if err != nil { |
Colin Cross | fe5a3b7 | 2018-07-13 21:25:15 -0700 | [diff] [blame] | 979 | fmt.Fprintf(os.Stderr, "Error reading manifest for %s: %s", pom.ArtifactFile, err) |
Colin Cross | cf53e60 | 2018-06-26 15:27:20 -0700 | [diff] [blame] | 980 | os.Exit(1) |
| 981 | } |
| 982 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 983 | pom.FixDeps(modules) |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 984 | if pom2build { |
| 985 | pom.GetBazelDepNames(modules) |
| 986 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | buf := &bytes.Buffer{} |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 990 | commentString := "//" |
| 991 | if pom2build { |
| 992 | commentString = "#" |
| 993 | } |
Alan Viverette | efab514 | 2022-09-19 16:26:00 +0000 | [diff] [blame] | 994 | |
| 995 | fmt.Fprintln(buf, commentString, "This is a generated file. Do not modify directly.") |
| 996 | |
| 997 | if writeCmd { |
| 998 | fmt.Fprintln(buf, commentString, "Automatically generated with:") |
| 999 | fmt.Fprintln(buf, commentString, "pom2bp", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " ")) |
| 1000 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1001 | |
Alan Viverette | d3be6f5 | 2022-08-11 10:59:12 -0400 | [diff] [blame] | 1002 | if prepend != "" { |
| 1003 | contents, err := ioutil.ReadFile(prepend) |
| 1004 | if err != nil { |
| 1005 | fmt.Fprintln(os.Stderr, "Error reading", prepend, err) |
| 1006 | os.Exit(1) |
| 1007 | } |
| 1008 | fmt.Fprintln(buf, string(contents)) |
| 1009 | } |
| 1010 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 1011 | depsTemplate := bpDepsTemplate |
| 1012 | template := bpTemplate |
| 1013 | if pom2build { |
| 1014 | depsTemplate = bazelDepsTemplate |
| 1015 | template = bazelTemplate |
| 1016 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1017 | |
| 1018 | for _, pom := range poms { |
| 1019 | var err error |
Brett Chabot | 98fb286 | 2022-10-12 12:12:02 -0700 | [diff] [blame] | 1020 | if staticDeps && !pom.IsApk() { |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 1021 | err = depsTemplate.Execute(buf, pom) |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 1022 | } else { |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 1023 | err = template.Execute(buf, pom) |
Dan Willemsen | 52c90d8 | 2019-04-21 21:37:39 -0700 | [diff] [blame] | 1024 | } |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1025 | if err != nil { |
| 1026 | fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.BpName(), err) |
| 1027 | os.Exit(1) |
| 1028 | } |
| 1029 | } |
| 1030 | |
Romain Jobredeaux | 89cb224 | 2021-09-20 14:14:22 +0000 | [diff] [blame] | 1031 | if pom2build { |
| 1032 | os.Stdout.WriteString(buf.String()) |
| 1033 | } else { |
| 1034 | out, err := bpfix.Reformat(buf.String()) |
| 1035 | if err != nil { |
| 1036 | fmt.Fprintln(os.Stderr, "Error formatting output", err) |
| 1037 | os.Exit(1) |
| 1038 | } |
| 1039 | os.Stdout.WriteString(out) |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Colin Cross | 70dd38f | 2018-04-16 13:52:10 -0700 | [diff] [blame] | 1042 | } |