blob: 721e250ef9985aa1252be95a2c26c2e87c1b0dc1 [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
Dan Willemsen2902fa72017-04-27 21:16:35 -070092type Dependency struct {
93 XMLName xml.Name `xml:"dependency"`
94
Alan Viverette1593d3d2017-12-11 17:14:26 -050095 MakeTarget string `xml:"-"`
96
Dan Willemsen2902fa72017-04-27 21:16:35 -070097 GroupId string `xml:"groupId"`
98 ArtifactId string `xml:"artifactId"`
99 Version string `xml:"version"`
100 Type string `xml:"type"`
101
102 Scope string `xml:"scope"`
103}
104
Alan Viverette1593d3d2017-12-11 17:14:26 -0500105func (d Dependency) MkName() string {
106 if d.MakeTarget == "" {
107 d.MakeTarget = rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
108 }
109 return d.MakeTarget
110}
111
Dan Willemsen2902fa72017-04-27 21:16:35 -0700112type Pom struct {
113 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
114
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700115 PomFile string `xml:"-"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700116 ArtifactFile string `xml:"-"`
Alan Viverette75b95f82017-12-04 16:24:07 -0500117 MakeTarget string `xml:"-"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700118
119 GroupId string `xml:"groupId"`
120 ArtifactId string `xml:"artifactId"`
121 Version string `xml:"version"`
122 Packaging string `xml:"packaging"`
123
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700124 Dependencies []*Dependency `xml:"dependencies>dependency"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700125}
126
Alan Viverette1593d3d2017-12-11 17:14:26 -0500127func (p Pom) IsAar() bool {
128 return p.Packaging == "aar"
129}
130
131func (p Pom) IsJar() bool {
132 return p.Packaging == "jar"
133}
134
Dan Willemsen2902fa72017-04-27 21:16:35 -0700135func (p Pom) MkName() string {
Alan Viverette75b95f82017-12-04 16:24:07 -0500136 if p.MakeTarget == "" {
137 p.MakeTarget = rewriteNames.MavenToMk(p.GroupId, p.ArtifactId)
138 }
139 return p.MakeTarget
Dan Willemsen2902fa72017-04-27 21:16:35 -0700140}
141
Alan Viverette1593d3d2017-12-11 17:14:26 -0500142func (p Pom) MkJarDeps() []string {
143 return p.MkDeps("jar")
144}
145
146func (p Pom) MkAarDeps() []string {
147 return p.MkDeps("aar")
148}
149
150func (p Pom) MkDeps(typeExt string) []string {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700151 var ret []string
152 for _, d := range p.Dependencies {
Alan Viverette1593d3d2017-12-11 17:14:26 -0500153 if d.Type != typeExt {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700154 continue
155 }
Alan Viverette75b95f82017-12-04 16:24:07 -0500156 name := rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700157 ret = append(ret, name)
158 ret = append(ret, extraDeps[name]...)
159 }
160 return ret
161}
162
Dan Willemsen15a8e792017-11-10 14:02:44 -0800163func (p Pom) SdkVersion() string {
164 return sdkVersion
165}
166
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700167func (p *Pom) FixDepTypes(modules map[string]*Pom) {
168 for _, d := range p.Dependencies {
169 if d.Type != "" {
170 continue
171 }
Alan Viverette1593d3d2017-12-11 17:14:26 -0500172 if depPom, ok := modules[d.MkName()]; ok {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700173 d.Type = depPom.Packaging
174 }
175 }
176}
177
Dan Willemsen2902fa72017-04-27 21:16:35 -0700178var mkTemplate = template.Must(template.New("mk").Parse(`
179include $(CLEAR_VARS)
180LOCAL_MODULE := {{.MkName}}
181LOCAL_MODULE_CLASS := JAVA_LIBRARIES
182LOCAL_UNINSTALLABLE_MODULE := true
183LOCAL_SRC_FILES := {{.ArtifactFile}}
184LOCAL_BUILT_MODULE_STEM := javalib.jar
185LOCAL_MODULE_SUFFIX := .{{.Packaging}}
186LOCAL_USE_AAPT2 := true
Dan Willemsen15a8e792017-11-10 14:02:44 -0800187LOCAL_SDK_VERSION := {{.SdkVersion}}
Dan Willemsen2902fa72017-04-27 21:16:35 -0700188LOCAL_STATIC_ANDROID_LIBRARIES := \
Alan Viverette1593d3d2017-12-11 17:14:26 -0500189{{range .MkAarDeps}} {{.}} \
Dan Willemsen2902fa72017-04-27 21:16:35 -0700190{{end}}
191include $(BUILD_PREBUILT)
192`))
193
Alan Viverette1593d3d2017-12-11 17:14:26 -0500194var mkDepsTemplate = template.Must(template.New("mk").Parse(`
195include $(CLEAR_VARS)
196LOCAL_MODULE := {{.MkName}}-nodeps
197LOCAL_MODULE_CLASS := JAVA_LIBRARIES
198LOCAL_UNINSTALLABLE_MODULE := true
199LOCAL_SRC_FILES := {{.ArtifactFile}}
200LOCAL_BUILT_MODULE_STEM := javalib.jar
201LOCAL_MODULE_SUFFIX := .{{.Packaging}}
202LOCAL_USE_AAPT2 := true
203LOCAL_SDK_VERSION := {{.SdkVersion}}
204LOCAL_STATIC_ANDROID_LIBRARIES :={{range .MkAarDeps}} \
205 {{.}}{{end}}
206include $(BUILD_PREBUILT)
207include $(CLEAR_VARS)
208LOCAL_MODULE := {{.MkName}}
209LOCAL_SDK_VERSION := {{.SdkVersion}}{{if .IsAar}}
210LOCAL_MANIFEST_FILE := manifests/{{.MkName}}/AndroidManifest.xml{{end}}
211LOCAL_STATIC_JAVA_LIBRARIES :={{if .IsJar}} \
212 {{.MkName}}-nodeps{{end}}{{range .MkJarDeps}} \
213 {{.}}{{end}}
214LOCAL_STATIC_ANDROID_LIBRARIES :={{if .IsAar}} \
215 {{.MkName}}-nodeps{{end}}{{range .MkAarDeps}} \
216 {{.}}{{end}}
217LOCAL_JAR_EXCLUDE_FILES := none
218LOCAL_JAVA_LANGUAGE_VERSION := 1.7
219LOCAL_USE_AAPT2 := true
220include $(BUILD_STATIC_JAVA_LIBRARY)
221`))
222
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700223func parse(filename string) (*Pom, error) {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700224 data, err := ioutil.ReadFile(filename)
225 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700226 return nil, err
Dan Willemsen2902fa72017-04-27 21:16:35 -0700227 }
228
229 var pom Pom
230 err = xml.Unmarshal(data, &pom)
231 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700232 return nil, err
Dan Willemsen2902fa72017-04-27 21:16:35 -0700233 }
234
Dan Willemsen47e44a42017-10-05 13:28:16 -0700235 if useVersion != "" && pom.Version != useVersion {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700236 return nil, nil
Dan Willemsen47e44a42017-10-05 13:28:16 -0700237 }
238
Dan Willemsen2902fa72017-04-27 21:16:35 -0700239 if pom.Packaging == "" {
240 pom.Packaging = "jar"
241 }
242
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700243 pom.PomFile = filename
Dan Willemsen2902fa72017-04-27 21:16:35 -0700244 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
245
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700246 return &pom, nil
Dan Willemsen2902fa72017-04-27 21:16:35 -0700247}
248
249func main() {
250 flag.Usage = func() {
251 fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos
252
253The tool will extract the necessary information from *.pom files to create an Android.mk whose
254aar libraries can be linked against when using AAPT2.
255
256Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module>]] <dir>
257
258 -rewrite <regex>=<replace>
Alan Viverette75b95f82017-12-04 16:24:07 -0500259 rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite
260 option can be specified multiple times. When determining the Make module for a given Maven
261 project, mappings are searched in the order they were specified. The first <regex> matching
262 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
263 the Make module name using <replace>. If no matches are found, <artifactId> is used.
Dan Willemsen2902fa72017-04-27 21:16:35 -0700264 -extra-deps <module>=<module>[,<module>]
265 Some Android.mk modules have transitive dependencies that must be specified when they are
266 depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
267 This may be specified multiple times to declare these dependencies.
Dan Willemsen15a8e792017-11-10 14:02:44 -0800268 -sdk-version <version>
269 Sets LOCAL_SDK_VERSION := <version> for all modules.
Dan Willemsen47e44a42017-10-05 13:28:16 -0700270 -use-version <version>
271 If the maven directory contains multiple versions of artifacts and their pom files,
272 -use-version can be used to only write makefiles for a specific version of those artifacts.
Alan Viverette1593d3d2017-12-11 17:14:26 -0500273 -static-deps
274 Whether to statically include direct dependencies.
Dan Willemsen2902fa72017-04-27 21:16:35 -0700275 <dir>
276 The directory to search for *.pom files under.
277
278The makefile is written to stdout, to be put in the current directory (often as Android.mk)
279`, os.Args[0])
280 }
281
282 flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
283 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Dan Willemsen15a8e792017-11-10 14:02:44 -0800284 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
Dan Willemsen47e44a42017-10-05 13:28:16 -0700285 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
Alan Viverette1593d3d2017-12-11 17:14:26 -0500286 flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
Dan Willemsen2902fa72017-04-27 21:16:35 -0700287 flag.Parse()
288
289 if flag.NArg() != 1 {
290 flag.Usage()
291 os.Exit(1)
292 }
293
294 dir := flag.Arg(0)
295 absDir, err := filepath.Abs(dir)
296 if err != nil {
297 fmt.Println(os.Stderr, "Failed to get absolute directory:", err)
298 os.Exit(1)
299 }
300
301 var filenames []string
302 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
303 if err != nil {
304 return err
305 }
306
307 name := info.Name()
308 if info.IsDir() {
309 if strings.HasPrefix(name, ".") {
310 return filepath.SkipDir
311 }
312 return nil
313 }
314
315 if strings.HasPrefix(name, ".") {
316 return nil
317 }
318
319 if strings.HasSuffix(name, ".pom") {
320 path, err = filepath.Rel(absDir, path)
321 if err != nil {
322 return err
323 }
324 filenames = append(filenames, filepath.Join(dir, path))
325 }
326 return nil
327 })
328 if err != nil {
329 fmt.Fprintln(os.Stderr, "Error walking files:", err)
330 os.Exit(1)
331 }
332
333 if len(filenames) == 0 {
334 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
335 os.Exit(1)
336 }
337
338 sort.Strings(filenames)
339
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700340 poms := []*Pom{}
341 modules := make(map[string]*Pom)
342 for _, filename := range filenames {
343 pom, err := parse(filename)
344 if err != nil {
345 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
346 os.Exit(1)
347 }
348
349 if pom != nil {
350 poms = append(poms, pom)
Alan Viverette75b95f82017-12-04 16:24:07 -0500351 key := pom.MkName()
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700352
Alan Viverette75b95f82017-12-04 16:24:07 -0500353 if old, ok := modules[key]; ok {
354 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700355 os.Exit(1)
356 }
357
Alan Viverette75b95f82017-12-04 16:24:07 -0500358 modules[key] = pom
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700359 }
360 }
361
362 for _, pom := range poms {
363 pom.FixDepTypes(modules)
364 }
365
Dan Willemsen2902fa72017-04-27 21:16:35 -0700366 fmt.Println("# Automatically generated with:")
367 fmt.Println("# pom2mk", strings.Join(proptools.ShellEscape(os.Args[1:]), " "))
368 fmt.Println("LOCAL_PATH := $(call my-dir)")
369
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700370 for _, pom := range poms {
Alan Viverette1593d3d2017-12-11 17:14:26 -0500371 var err error
372 if staticDeps {
373 err = mkDepsTemplate.Execute(os.Stdout, pom)
374 } else {
375 err = mkTemplate.Execute(os.Stdout, pom)
376 }
Dan Willemsen2902fa72017-04-27 21:16:35 -0700377 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700378 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.MkName(), err)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700379 os.Exit(1)
380 }
381 }
382}