blob: a635586ee8af8fc37013a6f52370ab79eb598d0d [file] [log] [blame]
Dan Willemsen2902fa72017-04-27 21:16:35 -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 (
18 "encoding/xml"
19 "flag"
20 "fmt"
Dan Willemsen2902fa72017-04-27 21:16:35 -070021 "io/ioutil"
22 "os"
23 "path/filepath"
24 "regexp"
25 "sort"
26 "strings"
27 "text/template"
28
29 "github.com/google/blueprint/proptools"
30)
31
32type RewriteNames []RewriteName
33type RewriteName struct {
34 regexp *regexp.Regexp
35 repl string
36}
37
38func (r *RewriteNames) String() string {
39 return ""
40}
41
42func (r *RewriteNames) Set(v string) error {
43 split := strings.SplitN(v, "=", 2)
44 if len(split) != 2 {
45 return fmt.Errorf("Must be in the form of <regex>=<replace>")
46 }
47 regex, err := regexp.Compile(split[0])
48 if err != nil {
49 return nil
50 }
51 *r = append(*r, RewriteName{
52 regexp: regex,
53 repl: split[1],
54 })
55 return nil
56}
57
Alan Viverette75b95f82017-12-04 16:24:07 -050058func (r *RewriteNames) MavenToMk(groupId string, artifactId string) string {
Dan Willemsen2902fa72017-04-27 21:16:35 -070059 for _, r := range *r {
Alan Viverette75b95f82017-12-04 16:24:07 -050060 if r.regexp.MatchString(groupId + ":" + artifactId) {
61 return r.regexp.ReplaceAllString(groupId+":"+artifactId, r.repl)
62 } else if r.regexp.MatchString(artifactId) {
63 return r.regexp.ReplaceAllString(artifactId, r.repl)
Dan Willemsen2902fa72017-04-27 21:16:35 -070064 }
65 }
Alan Viverette75b95f82017-12-04 16:24:07 -050066 return artifactId
Dan Willemsen2902fa72017-04-27 21:16:35 -070067}
68
69var rewriteNames = RewriteNames{}
70
71type ExtraDeps map[string][]string
72
73func (d ExtraDeps) String() string {
74 return ""
75}
76
77func (d ExtraDeps) Set(v string) error {
78 split := strings.SplitN(v, "=", 2)
79 if len(split) != 2 {
80 return fmt.Errorf("Must be in the form of <module>=<module>[,<module>]")
81 }
82 d[split[0]] = strings.Split(split[1], ",")
83 return nil
84}
85
86var extraDeps = make(ExtraDeps)
87
Dan Willemsen15a8e792017-11-10 14:02:44 -080088var sdkVersion string
Dan Willemsen47e44a42017-10-05 13:28:16 -070089var useVersion string
Alan Viverette1593d3d2017-12-11 17:14:26 -050090var staticDeps bool
Dan Willemsen47e44a42017-10-05 13:28:16 -070091
Jeff Gaston8f084822018-03-29 14:58:50 -040092func InList(s string, list []string) bool {
93 for _, l := range list {
94 if l == s {
95 return true
96 }
97 }
98
99 return false
100}
101
Dan Willemsen2902fa72017-04-27 21:16:35 -0700102type Dependency struct {
103 XMLName xml.Name `xml:"dependency"`
104
Alan Viverette1593d3d2017-12-11 17:14:26 -0500105 MakeTarget string `xml:"-"`
106
Dan Willemsen2902fa72017-04-27 21:16:35 -0700107 GroupId string `xml:"groupId"`
108 ArtifactId string `xml:"artifactId"`
109 Version string `xml:"version"`
110 Type string `xml:"type"`
Alan Viverette6bd35eb2018-02-13 13:25:24 -0500111 Scope string `xml:"scope"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700112}
113
Alan Viverette1593d3d2017-12-11 17:14:26 -0500114func (d Dependency) MkName() string {
115 if d.MakeTarget == "" {
116 d.MakeTarget = rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
117 }
118 return d.MakeTarget
119}
120
Dan Willemsen2902fa72017-04-27 21:16:35 -0700121type Pom struct {
122 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
123
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700124 PomFile string `xml:"-"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700125 ArtifactFile string `xml:"-"`
Alan Viverette75b95f82017-12-04 16:24:07 -0500126 MakeTarget string `xml:"-"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700127
128 GroupId string `xml:"groupId"`
129 ArtifactId string `xml:"artifactId"`
130 Version string `xml:"version"`
131 Packaging string `xml:"packaging"`
132
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700133 Dependencies []*Dependency `xml:"dependencies>dependency"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700134}
135
Alan Viverette1593d3d2017-12-11 17:14:26 -0500136func (p Pom) IsAar() bool {
137 return p.Packaging == "aar"
138}
139
140func (p Pom) IsJar() bool {
141 return p.Packaging == "jar"
142}
143
Dan Willemsen2902fa72017-04-27 21:16:35 -0700144func (p Pom) MkName() string {
Alan Viverette75b95f82017-12-04 16:24:07 -0500145 if p.MakeTarget == "" {
146 p.MakeTarget = rewriteNames.MavenToMk(p.GroupId, p.ArtifactId)
147 }
148 return p.MakeTarget
Dan Willemsen2902fa72017-04-27 21:16:35 -0700149}
150
Alan Viverette1593d3d2017-12-11 17:14:26 -0500151func (p Pom) MkJarDeps() []string {
Jeff Gaston8f084822018-03-29 14:58:50 -0400152 return p.MkDeps("jar", []string{"compile", "runtime"})
Alan Viverette1593d3d2017-12-11 17:14:26 -0500153}
154
155func (p Pom) MkAarDeps() []string {
Jeff Gaston8f084822018-03-29 14:58:50 -0400156 return p.MkDeps("aar", []string{"compile", "runtime"})
Alan Viverette1593d3d2017-12-11 17:14:26 -0500157}
158
Alan Viverette6bd35eb2018-02-13 13:25:24 -0500159// MkDeps obtains dependencies filtered by type and scope. The results of this
160// method are formatted as Make targets, e.g. run through MavenToMk rules.
Jeff Gaston8f084822018-03-29 14:58:50 -0400161func (p Pom) MkDeps(typeExt string, scopes []string) []string {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700162 var ret []string
Jeff Gaston358f25e2018-04-23 15:32:19 -0400163 if typeExt == "jar" {
164 // all top-level extra deps are assumed to be of type "jar" until we add syntax to specify other types
165 ret = append(ret, extraDeps[p.MkName()]...)
166 }
Dan Willemsen2902fa72017-04-27 21:16:35 -0700167 for _, d := range p.Dependencies {
Jeff Gaston8f084822018-03-29 14:58:50 -0400168 if d.Type != typeExt || !InList(d.Scope, scopes) {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700169 continue
170 }
Alan Viverette75b95f82017-12-04 16:24:07 -0500171 name := rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700172 ret = append(ret, name)
173 ret = append(ret, extraDeps[name]...)
174 }
175 return ret
176}
177
Dan Willemsen15a8e792017-11-10 14:02:44 -0800178func (p Pom) SdkVersion() string {
179 return sdkVersion
180}
181
Alan Viverette6bd35eb2018-02-13 13:25:24 -0500182func (p *Pom) FixDeps(modules map[string]*Pom) {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700183 for _, d := range p.Dependencies {
Alan Viverette6bd35eb2018-02-13 13:25:24 -0500184 if d.Type == "" {
185 if depPom, ok := modules[d.MkName()]; ok {
186 // We've seen the POM for this dependency, use its packaging
187 // as the dependency type rather than Maven spec default.
188 d.Type = depPom.Packaging
189 } else {
190 // Dependency type was not specified and we don't have the POM
191 // for this artifact, use the default from Maven spec.
192 d.Type = "jar"
193 }
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700194 }
Alan Viverette6bd35eb2018-02-13 13:25:24 -0500195 if d.Scope == "" {
196 // Scope was not specified, use the default from Maven spec.
197 d.Scope = "compile"
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700198 }
199 }
200}
201
Dan Willemsen2902fa72017-04-27 21:16:35 -0700202var mkTemplate = template.Must(template.New("mk").Parse(`
203include $(CLEAR_VARS)
204LOCAL_MODULE := {{.MkName}}
205LOCAL_MODULE_CLASS := JAVA_LIBRARIES
206LOCAL_UNINSTALLABLE_MODULE := true
207LOCAL_SRC_FILES := {{.ArtifactFile}}
208LOCAL_BUILT_MODULE_STEM := javalib.jar
209LOCAL_MODULE_SUFFIX := .{{.Packaging}}
210LOCAL_USE_AAPT2 := true
Dan Willemsen15a8e792017-11-10 14:02:44 -0800211LOCAL_SDK_VERSION := {{.SdkVersion}}
Alan Viverette2b53a0c2018-03-28 10:32:10 -0400212LOCAL_STATIC_JAVA_LIBRARIES :={{range .MkJarDeps}} \
213 {{.}}{{end}}
214LOCAL_STATIC_ANDROID_LIBRARIES :={{range .MkAarDeps}} \
215 {{.}}{{end}}
Dan Willemsen2902fa72017-04-27 21:16:35 -0700216include $(BUILD_PREBUILT)
217`))
218
Alan Viverette1593d3d2017-12-11 17:14:26 -0500219var mkDepsTemplate = template.Must(template.New("mk").Parse(`
220include $(CLEAR_VARS)
221LOCAL_MODULE := {{.MkName}}-nodeps
222LOCAL_MODULE_CLASS := JAVA_LIBRARIES
223LOCAL_UNINSTALLABLE_MODULE := true
224LOCAL_SRC_FILES := {{.ArtifactFile}}
225LOCAL_BUILT_MODULE_STEM := javalib.jar
226LOCAL_MODULE_SUFFIX := .{{.Packaging}}
227LOCAL_USE_AAPT2 := true
228LOCAL_SDK_VERSION := {{.SdkVersion}}
229LOCAL_STATIC_ANDROID_LIBRARIES :={{range .MkAarDeps}} \
230 {{.}}{{end}}
231include $(BUILD_PREBUILT)
232include $(CLEAR_VARS)
233LOCAL_MODULE := {{.MkName}}
234LOCAL_SDK_VERSION := {{.SdkVersion}}{{if .IsAar}}
235LOCAL_MANIFEST_FILE := manifests/{{.MkName}}/AndroidManifest.xml{{end}}
236LOCAL_STATIC_JAVA_LIBRARIES :={{if .IsJar}} \
237 {{.MkName}}-nodeps{{end}}{{range .MkJarDeps}} \
238 {{.}}{{end}}
239LOCAL_STATIC_ANDROID_LIBRARIES :={{if .IsAar}} \
240 {{.MkName}}-nodeps{{end}}{{range .MkAarDeps}} \
241 {{.}}{{end}}
242LOCAL_JAR_EXCLUDE_FILES := none
243LOCAL_JAVA_LANGUAGE_VERSION := 1.7
244LOCAL_USE_AAPT2 := true
245include $(BUILD_STATIC_JAVA_LIBRARY)
246`))
247
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700248func parse(filename string) (*Pom, error) {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700249 data, err := ioutil.ReadFile(filename)
250 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700251 return nil, err
Dan Willemsen2902fa72017-04-27 21:16:35 -0700252 }
253
254 var pom Pom
255 err = xml.Unmarshal(data, &pom)
256 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700257 return nil, err
Dan Willemsen2902fa72017-04-27 21:16:35 -0700258 }
259
Dan Willemsen47e44a42017-10-05 13:28:16 -0700260 if useVersion != "" && pom.Version != useVersion {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700261 return nil, nil
Dan Willemsen47e44a42017-10-05 13:28:16 -0700262 }
263
Dan Willemsen2902fa72017-04-27 21:16:35 -0700264 if pom.Packaging == "" {
265 pom.Packaging = "jar"
266 }
267
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700268 pom.PomFile = filename
Dan Willemsen2902fa72017-04-27 21:16:35 -0700269 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
270
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700271 return &pom, nil
Dan Willemsen2902fa72017-04-27 21:16:35 -0700272}
273
274func main() {
275 flag.Usage = func() {
276 fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos
277
278The tool will extract the necessary information from *.pom files to create an Android.mk whose
279aar libraries can be linked against when using AAPT2.
280
281Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module>]] <dir>
282
283 -rewrite <regex>=<replace>
Alan Viverette75b95f82017-12-04 16:24:07 -0500284 rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite
285 option can be specified multiple times. When determining the Make module for a given Maven
286 project, mappings are searched in the order they were specified. The first <regex> matching
287 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
288 the Make module name using <replace>. If no matches are found, <artifactId> is used.
Dan Willemsen2902fa72017-04-27 21:16:35 -0700289 -extra-deps <module>=<module>[,<module>]
290 Some Android.mk modules have transitive dependencies that must be specified when they are
291 depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
292 This may be specified multiple times to declare these dependencies.
Dan Willemsen15a8e792017-11-10 14:02:44 -0800293 -sdk-version <version>
294 Sets LOCAL_SDK_VERSION := <version> for all modules.
Dan Willemsen47e44a42017-10-05 13:28:16 -0700295 -use-version <version>
296 If the maven directory contains multiple versions of artifacts and their pom files,
297 -use-version can be used to only write makefiles for a specific version of those artifacts.
Alan Viverette1593d3d2017-12-11 17:14:26 -0500298 -static-deps
299 Whether to statically include direct dependencies.
Dan Willemsen2902fa72017-04-27 21:16:35 -0700300 <dir>
301 The directory to search for *.pom files under.
302
303The makefile is written to stdout, to be put in the current directory (often as Android.mk)
304`, os.Args[0])
305 }
306
307 flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
308 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Dan Willemsen15a8e792017-11-10 14:02:44 -0800309 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
Dan Willemsen47e44a42017-10-05 13:28:16 -0700310 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
Alan Viverette1593d3d2017-12-11 17:14:26 -0500311 flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
Dan Willemsen2902fa72017-04-27 21:16:35 -0700312 flag.Parse()
313
314 if flag.NArg() != 1 {
315 flag.Usage()
316 os.Exit(1)
317 }
318
319 dir := flag.Arg(0)
320 absDir, err := filepath.Abs(dir)
321 if err != nil {
Colin Crossf46e37f2018-03-21 16:25:58 -0700322 fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700323 os.Exit(1)
324 }
325
326 var filenames []string
327 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
328 if err != nil {
329 return err
330 }
331
332 name := info.Name()
333 if info.IsDir() {
334 if strings.HasPrefix(name, ".") {
335 return filepath.SkipDir
336 }
337 return nil
338 }
339
340 if strings.HasPrefix(name, ".") {
341 return nil
342 }
343
344 if strings.HasSuffix(name, ".pom") {
345 path, err = filepath.Rel(absDir, path)
346 if err != nil {
347 return err
348 }
349 filenames = append(filenames, filepath.Join(dir, path))
350 }
351 return nil
352 })
353 if err != nil {
354 fmt.Fprintln(os.Stderr, "Error walking files:", err)
355 os.Exit(1)
356 }
357
358 if len(filenames) == 0 {
359 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
360 os.Exit(1)
361 }
362
363 sort.Strings(filenames)
364
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700365 poms := []*Pom{}
366 modules := make(map[string]*Pom)
Jeff Gastonaf2191e2018-03-29 14:59:01 -0400367 duplicate := false
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700368 for _, filename := range filenames {
369 pom, err := parse(filename)
370 if err != nil {
371 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
372 os.Exit(1)
373 }
374
375 if pom != nil {
376 poms = append(poms, pom)
Alan Viverette75b95f82017-12-04 16:24:07 -0500377 key := pom.MkName()
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700378
Alan Viverette75b95f82017-12-04 16:24:07 -0500379 if old, ok := modules[key]; ok {
380 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
Jeff Gastonaf2191e2018-03-29 14:59:01 -0400381 duplicate = true
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700382 }
383
Alan Viverette75b95f82017-12-04 16:24:07 -0500384 modules[key] = pom
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700385 }
386 }
Jeff Gastonaf2191e2018-03-29 14:59:01 -0400387 if duplicate {
388 os.Exit(1)
389 }
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700390
391 for _, pom := range poms {
Alan Viverette6bd35eb2018-02-13 13:25:24 -0500392 pom.FixDeps(modules)
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700393 }
394
Dan Willemsen2902fa72017-04-27 21:16:35 -0700395 fmt.Println("# Automatically generated with:")
396 fmt.Println("# pom2mk", strings.Join(proptools.ShellEscape(os.Args[1:]), " "))
397 fmt.Println("LOCAL_PATH := $(call my-dir)")
398
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700399 for _, pom := range poms {
Alan Viverette1593d3d2017-12-11 17:14:26 -0500400 var err error
401 if staticDeps {
402 err = mkDepsTemplate.Execute(os.Stdout, pom)
403 } else {
404 err = mkTemplate.Execute(os.Stdout, pom)
405 }
Dan Willemsen2902fa72017-04-27 21:16:35 -0700406 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700407 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.MkName(), err)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700408 os.Exit(1)
409 }
410 }
411}