blob: 33d7ff912f9faa6f9688373105d5c9f4e0192427 [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
90
Dan Willemsen2902fa72017-04-27 21:16:35 -070091type Dependency struct {
92 XMLName xml.Name `xml:"dependency"`
93
94 GroupId string `xml:"groupId"`
95 ArtifactId string `xml:"artifactId"`
96 Version string `xml:"version"`
97 Type string `xml:"type"`
98
99 Scope string `xml:"scope"`
100}
101
102type Pom struct {
103 XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
104
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700105 PomFile string `xml:"-"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700106 ArtifactFile string `xml:"-"`
Alan Viverette75b95f82017-12-04 16:24:07 -0500107 MakeTarget string `xml:"-"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700108
109 GroupId string `xml:"groupId"`
110 ArtifactId string `xml:"artifactId"`
111 Version string `xml:"version"`
112 Packaging string `xml:"packaging"`
113
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700114 Dependencies []*Dependency `xml:"dependencies>dependency"`
Dan Willemsen2902fa72017-04-27 21:16:35 -0700115}
116
117func (p Pom) MkName() string {
Alan Viverette75b95f82017-12-04 16:24:07 -0500118 if p.MakeTarget == "" {
119 p.MakeTarget = rewriteNames.MavenToMk(p.GroupId, p.ArtifactId)
120 }
121 return p.MakeTarget
Dan Willemsen2902fa72017-04-27 21:16:35 -0700122}
123
124func (p Pom) MkDeps() []string {
125 var ret []string
126 for _, d := range p.Dependencies {
127 if d.Type != "aar" {
128 continue
129 }
Alan Viverette75b95f82017-12-04 16:24:07 -0500130 name := rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700131 ret = append(ret, name)
132 ret = append(ret, extraDeps[name]...)
133 }
134 return ret
135}
136
Dan Willemsen15a8e792017-11-10 14:02:44 -0800137func (p Pom) SdkVersion() string {
138 return sdkVersion
139}
140
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700141func (p *Pom) FixDepTypes(modules map[string]*Pom) {
142 for _, d := range p.Dependencies {
143 if d.Type != "" {
144 continue
145 }
Alan Viverette75b95f82017-12-04 16:24:07 -0500146 if depPom, ok := modules[p.MkName()]; ok {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700147 d.Type = depPom.Packaging
148 }
149 }
150}
151
Dan Willemsen2902fa72017-04-27 21:16:35 -0700152var mkTemplate = template.Must(template.New("mk").Parse(`
153include $(CLEAR_VARS)
154LOCAL_MODULE := {{.MkName}}
155LOCAL_MODULE_CLASS := JAVA_LIBRARIES
156LOCAL_UNINSTALLABLE_MODULE := true
157LOCAL_SRC_FILES := {{.ArtifactFile}}
158LOCAL_BUILT_MODULE_STEM := javalib.jar
159LOCAL_MODULE_SUFFIX := .{{.Packaging}}
160LOCAL_USE_AAPT2 := true
Dan Willemsen15a8e792017-11-10 14:02:44 -0800161LOCAL_SDK_VERSION := {{.SdkVersion}}
Dan Willemsen2902fa72017-04-27 21:16:35 -0700162LOCAL_STATIC_ANDROID_LIBRARIES := \
163{{range .MkDeps}} {{.}} \
164{{end}}
165include $(BUILD_PREBUILT)
166`))
167
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700168func parse(filename string) (*Pom, error) {
Dan Willemsen2902fa72017-04-27 21:16:35 -0700169 data, err := ioutil.ReadFile(filename)
170 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700171 return nil, err
Dan Willemsen2902fa72017-04-27 21:16:35 -0700172 }
173
174 var pom Pom
175 err = xml.Unmarshal(data, &pom)
176 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700177 return nil, err
Dan Willemsen2902fa72017-04-27 21:16:35 -0700178 }
179
Dan Willemsen47e44a42017-10-05 13:28:16 -0700180 if useVersion != "" && pom.Version != useVersion {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700181 return nil, nil
Dan Willemsen47e44a42017-10-05 13:28:16 -0700182 }
183
Dan Willemsen2902fa72017-04-27 21:16:35 -0700184 if pom.Packaging == "" {
185 pom.Packaging = "jar"
186 }
187
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700188 pom.PomFile = filename
Dan Willemsen2902fa72017-04-27 21:16:35 -0700189 pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
190
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700191 return &pom, nil
Dan Willemsen2902fa72017-04-27 21:16:35 -0700192}
193
194func main() {
195 flag.Usage = func() {
196 fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos
197
198The tool will extract the necessary information from *.pom files to create an Android.mk whose
199aar libraries can be linked against when using AAPT2.
200
201Usage: %s [--rewrite <regex>=<replace>] [--extra-deps <module>=<module>[,<module>]] <dir>
202
203 -rewrite <regex>=<replace>
Alan Viverette75b95f82017-12-04 16:24:07 -0500204 rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite
205 option can be specified multiple times. When determining the Make module for a given Maven
206 project, mappings are searched in the order they were specified. The first <regex> matching
207 either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
208 the Make module name using <replace>. If no matches are found, <artifactId> is used.
Dan Willemsen2902fa72017-04-27 21:16:35 -0700209 -extra-deps <module>=<module>[,<module>]
210 Some Android.mk modules have transitive dependencies that must be specified when they are
211 depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
212 This may be specified multiple times to declare these dependencies.
Dan Willemsen15a8e792017-11-10 14:02:44 -0800213 -sdk-version <version>
214 Sets LOCAL_SDK_VERSION := <version> for all modules.
Dan Willemsen47e44a42017-10-05 13:28:16 -0700215 -use-version <version>
216 If the maven directory contains multiple versions of artifacts and their pom files,
217 -use-version can be used to only write makefiles for a specific version of those artifacts.
Dan Willemsen2902fa72017-04-27 21:16:35 -0700218 <dir>
219 The directory to search for *.pom files under.
220
221The makefile is written to stdout, to be put in the current directory (often as Android.mk)
222`, os.Args[0])
223 }
224
225 flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
226 flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
Dan Willemsen15a8e792017-11-10 14:02:44 -0800227 flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
Dan Willemsen47e44a42017-10-05 13:28:16 -0700228 flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
Dan Willemsen2902fa72017-04-27 21:16:35 -0700229 flag.Parse()
230
231 if flag.NArg() != 1 {
232 flag.Usage()
233 os.Exit(1)
234 }
235
236 dir := flag.Arg(0)
237 absDir, err := filepath.Abs(dir)
238 if err != nil {
239 fmt.Println(os.Stderr, "Failed to get absolute directory:", err)
240 os.Exit(1)
241 }
242
243 var filenames []string
244 err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
245 if err != nil {
246 return err
247 }
248
249 name := info.Name()
250 if info.IsDir() {
251 if strings.HasPrefix(name, ".") {
252 return filepath.SkipDir
253 }
254 return nil
255 }
256
257 if strings.HasPrefix(name, ".") {
258 return nil
259 }
260
261 if strings.HasSuffix(name, ".pom") {
262 path, err = filepath.Rel(absDir, path)
263 if err != nil {
264 return err
265 }
266 filenames = append(filenames, filepath.Join(dir, path))
267 }
268 return nil
269 })
270 if err != nil {
271 fmt.Fprintln(os.Stderr, "Error walking files:", err)
272 os.Exit(1)
273 }
274
275 if len(filenames) == 0 {
276 fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
277 os.Exit(1)
278 }
279
280 sort.Strings(filenames)
281
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700282 poms := []*Pom{}
283 modules := make(map[string]*Pom)
284 for _, filename := range filenames {
285 pom, err := parse(filename)
286 if err != nil {
287 fmt.Fprintln(os.Stderr, "Error converting", filename, err)
288 os.Exit(1)
289 }
290
291 if pom != nil {
292 poms = append(poms, pom)
Alan Viverette75b95f82017-12-04 16:24:07 -0500293 key := pom.MkName()
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700294
Alan Viverette75b95f82017-12-04 16:24:07 -0500295 if old, ok := modules[key]; ok {
296 fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700297 os.Exit(1)
298 }
299
Alan Viverette75b95f82017-12-04 16:24:07 -0500300 modules[key] = pom
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700301 }
302 }
303
304 for _, pom := range poms {
305 pom.FixDepTypes(modules)
306 }
307
Dan Willemsen2902fa72017-04-27 21:16:35 -0700308 fmt.Println("# Automatically generated with:")
309 fmt.Println("# pom2mk", strings.Join(proptools.ShellEscape(os.Args[1:]), " "))
310 fmt.Println("LOCAL_PATH := $(call my-dir)")
311
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700312 for _, pom := range poms {
313 err := mkTemplate.Execute(os.Stdout, pom)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700314 if err != nil {
Dan Willemsen5f9d8a62017-10-05 14:01:31 -0700315 fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.MkName(), err)
Dan Willemsen2902fa72017-04-27 21:16:35 -0700316 os.Exit(1)
317 }
318 }
319}