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
15package main
16
17import (
18	"bufio"
19	"bytes"
20	"encoding/xml"
21	"flag"
22	"fmt"
23	"io/ioutil"
24	"os"
25	"os/exec"
26	"path/filepath"
27	"regexp"
28	"sort"
29	"strings"
30	"text/template"
31
32	"github.com/google/blueprint/proptools"
33)
34
35type RewriteNames []RewriteName
36type RewriteName struct {
37	regexp *regexp.Regexp
38	repl   string
39}
40
41func (r *RewriteNames) String() string {
42	return ""
43}
44
45func (r *RewriteNames) Set(v string) error {
46	split := strings.SplitN(v, "=", 2)
47	if len(split) != 2 {
48		return fmt.Errorf("Must be in the form of <regex>=<replace>")
49	}
50	regex, err := regexp.Compile(split[0])
51	if err != nil {
52		return nil
53	}
54	*r = append(*r, RewriteName{
55		regexp: regex,
56		repl:   split[1],
57	})
58	return nil
59}
60
61func (r *RewriteNames) MavenToMk(groupId string, artifactId string) string {
62	for _, r := range *r {
63		if r.regexp.MatchString(groupId + ":" + artifactId) {
64			return r.regexp.ReplaceAllString(groupId+":"+artifactId, r.repl)
65		} else if r.regexp.MatchString(artifactId) {
66			return r.regexp.ReplaceAllString(artifactId, r.repl)
67		}
68	}
69	return artifactId
70}
71
72var rewriteNames = RewriteNames{}
73
74type ExtraDeps map[string][]string
75
76func (d ExtraDeps) String() string {
77	return ""
78}
79
80func (d ExtraDeps) Set(v string) error {
81	split := strings.SplitN(v, "=", 2)
82	if len(split) != 2 {
83		return fmt.Errorf("Must be in the form of <module>=<module>[,<module>]")
84	}
85	d[split[0]] = strings.Split(split[1], ",")
86	return nil
87}
88
89var extraDeps = make(ExtraDeps)
90
91type Exclude map[string]bool
92
93func (e Exclude) String() string {
94	return ""
95}
96
97func (e Exclude) Set(v string) error {
98	e[v] = true
99	return nil
100}
101
102var excludes = make(Exclude)
103
104var sdkVersion string
105var useVersion string
106var staticDeps bool
107var jetifier bool
108
109func InList(s string, list []string) bool {
110	for _, l := range list {
111		if l == s {
112			return true
113		}
114	}
115
116	return false
117}
118
119type Dependency struct {
120	XMLName xml.Name `xml:"dependency"`
121
122	MakeTarget string `xml:"-"`
123
124	GroupId    string `xml:"groupId"`
125	ArtifactId string `xml:"artifactId"`
126	Version    string `xml:"version"`
127	Type       string `xml:"type"`
128	Scope      string `xml:"scope"`
129}
130
131func (d Dependency) MkName() string {
132	if d.MakeTarget == "" {
133		d.MakeTarget = rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
134	}
135	return d.MakeTarget
136}
137
138type Pom struct {
139	XMLName xml.Name `xml:"http://maven.apache.org/POM/4.0.0 project"`
140
141	PomFile      string `xml:"-"`
142	ArtifactFile string `xml:"-"`
143	MakeTarget   string `xml:"-"`
144
145	GroupId    string `xml:"groupId"`
146	ArtifactId string `xml:"artifactId"`
147	Version    string `xml:"version"`
148	Packaging  string `xml:"packaging"`
149
150	Dependencies []*Dependency `xml:"dependencies>dependency"`
151}
152
153func (p Pom) IsAar() bool {
154	return p.Packaging == "aar"
155}
156
157func (p Pom) IsJar() bool {
158	return p.Packaging == "jar"
159}
160
161func (p Pom) MkName() string {
162	if p.MakeTarget == "" {
163		p.MakeTarget = rewriteNames.MavenToMk(p.GroupId, p.ArtifactId)
164	}
165	return p.MakeTarget
166}
167
168func (p Pom) MkJarDeps() []string {
169	return p.MkDeps("jar", []string{"compile", "runtime"})
170}
171
172func (p Pom) MkAarDeps() []string {
173	return p.MkDeps("aar", []string{"compile", "runtime"})
174}
175
176// MkDeps obtains dependencies filtered by type and scope. The results of this
177// method are formatted as Make targets, e.g. run through MavenToMk rules.
178func (p Pom) MkDeps(typeExt string, scopes []string) []string {
179	var ret []string
180	if typeExt == "jar" {
181		// all top-level extra deps are assumed to be of type "jar" until we add syntax to specify other types
182		ret = append(ret, extraDeps[p.MkName()]...)
183	}
184	for _, d := range p.Dependencies {
185		if d.Type != typeExt || !InList(d.Scope, scopes) {
186			continue
187		}
188		name := rewriteNames.MavenToMk(d.GroupId, d.ArtifactId)
189		ret = append(ret, name)
190		ret = append(ret, extraDeps[name]...)
191	}
192	return ret
193}
194
195func (p Pom) SdkVersion() string {
196	return sdkVersion
197}
198
199func (p Pom) Jetifier() bool {
200	return jetifier
201}
202
203func (p *Pom) FixDeps(modules map[string]*Pom) {
204	for _, d := range p.Dependencies {
205		if d.Type == "" {
206			if depPom, ok := modules[d.MkName()]; ok {
207				// We've seen the POM for this dependency, use its packaging
208				// as the dependency type rather than Maven spec default.
209				d.Type = depPom.Packaging
210			} else {
211				// Dependency type was not specified and we don't have the POM
212				// for this artifact, use the default from Maven spec.
213				d.Type = "jar"
214			}
215		}
216		if d.Scope == "" {
217			// Scope was not specified, use the default from Maven spec.
218			d.Scope = "compile"
219		}
220	}
221}
222
223var mkTemplate = template.Must(template.New("mk").Parse(`
224include $(CLEAR_VARS)
225LOCAL_MODULE := {{.MkName}}
226LOCAL_MODULE_CLASS := JAVA_LIBRARIES
227LOCAL_UNINSTALLABLE_MODULE := true
228LOCAL_SRC_FILES := {{.ArtifactFile}}
229LOCAL_BUILT_MODULE_STEM := javalib.jar
230LOCAL_MODULE_SUFFIX := .{{.Packaging}}
231LOCAL_USE_AAPT2 := true
232LOCAL_SDK_VERSION := {{.SdkVersion}}
233LOCAL_STATIC_JAVA_LIBRARIES :={{range .MkJarDeps}} \
234  {{.}}{{end}}
235LOCAL_STATIC_ANDROID_LIBRARIES :={{range .MkAarDeps}} \
236  {{.}}{{end}}
237LOCAL_JETIFIER_ENABLED := {{if .Jetifier}}true{{end}}
238include $(BUILD_PREBUILT)
239`))
240
241var mkDepsTemplate = template.Must(template.New("mk").Parse(`
242include $(CLEAR_VARS)
243LOCAL_MODULE := {{.MkName}}-nodeps
244LOCAL_MODULE_CLASS := JAVA_LIBRARIES
245LOCAL_UNINSTALLABLE_MODULE := true
246LOCAL_SRC_FILES := {{.ArtifactFile}}
247LOCAL_BUILT_MODULE_STEM := javalib.jar
248LOCAL_MODULE_SUFFIX := .{{.Packaging}}
249LOCAL_USE_AAPT2 := true
250LOCAL_SDK_VERSION := {{.SdkVersion}}
251LOCAL_STATIC_ANDROID_LIBRARIES :={{range .MkAarDeps}} \
252  {{.}}{{end}}
253include $(BUILD_PREBUILT)
254include $(CLEAR_VARS)
255LOCAL_MODULE := {{.MkName}}
256LOCAL_SDK_VERSION := {{.SdkVersion}}{{if .IsAar}}
257LOCAL_MANIFEST_FILE := manifests/{{.MkName}}/AndroidManifest.xml{{end}}
258LOCAL_STATIC_JAVA_LIBRARIES :={{if .IsJar}} \
259  {{.MkName}}-nodeps{{end}}{{range .MkJarDeps}} \
260  {{.}}{{end}}
261LOCAL_STATIC_ANDROID_LIBRARIES :={{if .IsAar}} \
262  {{.MkName}}-nodeps{{end}}{{range .MkAarDeps}}  \
263  {{.}}{{end}}
264LOCAL_JAR_EXCLUDE_FILES := none
265LOCAL_JAVA_LANGUAGE_VERSION := 1.7
266LOCAL_USE_AAPT2 := true
267include $(BUILD_STATIC_JAVA_LIBRARY)
268`))
269
270func parse(filename string) (*Pom, error) {
271	data, err := ioutil.ReadFile(filename)
272	if err != nil {
273		return nil, err
274	}
275
276	var pom Pom
277	err = xml.Unmarshal(data, &pom)
278	if err != nil {
279		return nil, err
280	}
281
282	if useVersion != "" && pom.Version != useVersion {
283		return nil, nil
284	}
285
286	if pom.Packaging == "" {
287		pom.Packaging = "jar"
288	}
289
290	pom.PomFile = filename
291	pom.ArtifactFile = strings.TrimSuffix(filename, ".pom") + "." + pom.Packaging
292
293	return &pom, nil
294}
295
296func rerunForRegen(filename string) error {
297	buf, err := ioutil.ReadFile(filename)
298	if err != nil {
299		return err
300	}
301
302	scanner := bufio.NewScanner(bytes.NewBuffer(buf))
303
304	// Skip the first line in the file
305	for i := 0; i < 2; i++ {
306		if !scanner.Scan() {
307			if scanner.Err() != nil {
308				return scanner.Err()
309			} else {
310				return fmt.Errorf("unexpected EOF")
311			}
312		}
313	}
314
315	// Extract the old args from the file
316	line := scanner.Text()
317	if strings.HasPrefix(line, "# pom2mk ") {
318		line = strings.TrimPrefix(line, "# pom2mk ")
319	} else {
320		return fmt.Errorf("unexpected second line: %q", line)
321	}
322	args := strings.Split(line, " ")
323	lastArg := args[len(args)-1]
324	args = args[:len(args)-1]
325
326	// Append all current command line args except -regen <file> to the ones from the file
327	for i := 1; i < len(os.Args); i++ {
328		if os.Args[i] == "-regen" {
329			i++
330		} else {
331			args = append(args, os.Args[i])
332		}
333	}
334	args = append(args, lastArg)
335
336	cmd := os.Args[0] + " " + strings.Join(args, " ")
337	// Re-exec pom2mk with the new arguments
338	output, err := exec.Command("/bin/sh", "-c", cmd).Output()
339	if exitErr, _ := err.(*exec.ExitError); exitErr != nil {
340		return fmt.Errorf("failed to run %s\n%s", cmd, string(exitErr.Stderr))
341	} else if err != nil {
342		return err
343	}
344
345	return ioutil.WriteFile(filename, output, 0666)
346}
347
348func main() {
349	flag.Usage = func() {
350		fmt.Fprintf(os.Stderr, `pom2mk, a tool to create Android.mk files from maven repos
351
352The tool will extract the necessary information from *.pom files to create an Android.mk whose
353aar libraries can be linked against when using AAPT2.
354
355Usage: %s [--rewrite <regex>=<replace>] [-exclude <module>] [--extra-deps <module>=<module>[,<module>]] [<dir>] [-regen <file>]
356
357  -rewrite <regex>=<replace>
358     rewrite can be used to specify mappings between Maven projects and Make modules. The -rewrite
359     option can be specified multiple times. When determining the Make module for a given Maven
360     project, mappings are searched in the order they were specified. The first <regex> matching
361     either the Maven project's <groupId>:<artifactId> or <artifactId> will be used to generate
362     the Make module name using <replace>. If no matches are found, <artifactId> is used.
363  -exclude <module>
364     Don't put the specified module in the makefile.
365  -extra-deps <module>=<module>[,<module>]
366     Some Android.mk modules have transitive dependencies that must be specified when they are
367     depended upon (like android-support-v7-mediarouter requires android-support-v7-appcompat).
368     This may be specified multiple times to declare these dependencies.
369  -sdk-version <version>
370     Sets LOCAL_SDK_VERSION := <version> for all modules.
371  -use-version <version>
372     If the maven directory contains multiple versions of artifacts and their pom files,
373     -use-version can be used to only write makefiles for a specific version of those artifacts.
374  -static-deps
375     Whether to statically include direct dependencies.
376  -jetifier
377     Enable jetifier in order to use androidx
378  <dir>
379     The directory to search for *.pom files under.
380     The makefile is written to stdout, to be put in the current directory (often as Android.mk)
381  -regen <file>
382     Read arguments from <file> and overwrite it.
383`, os.Args[0])
384	}
385
386	var regen string
387
388	flag.Var(&excludes, "exclude", "Exclude module")
389	flag.Var(&extraDeps, "extra-deps", "Extra dependencies needed when depending on a module")
390	flag.Var(&rewriteNames, "rewrite", "Regex(es) to rewrite artifact names")
391	flag.StringVar(&sdkVersion, "sdk-version", "", "What to write to LOCAL_SDK_VERSION")
392	flag.StringVar(&useVersion, "use-version", "", "Only read artifacts of a specific version")
393	flag.BoolVar(&staticDeps, "static-deps", false, "Statically include direct dependencies")
394	flag.BoolVar(&jetifier, "jetifier", false, "Enable jetifier in order to use androidx")
395	flag.StringVar(&regen, "regen", "", "Rewrite specified file")
396	flag.Parse()
397
398	if regen != "" {
399		err := rerunForRegen(regen)
400		if err != nil {
401			fmt.Fprintln(os.Stderr, err)
402			os.Exit(1)
403		}
404		os.Exit(0)
405	}
406
407	if flag.NArg() == 0 {
408		fmt.Fprintln(os.Stderr, "Directory argument is required")
409		os.Exit(1)
410	} else if flag.NArg() > 1 {
411		fmt.Fprintln(os.Stderr, "Multiple directories provided:", strings.Join(flag.Args(), " "))
412		os.Exit(1)
413	}
414
415	dir := flag.Arg(0)
416	absDir, err := filepath.Abs(dir)
417	if err != nil {
418		fmt.Fprintln(os.Stderr, "Failed to get absolute directory:", err)
419		os.Exit(1)
420	}
421
422	var filenames []string
423	err = filepath.Walk(absDir, func(path string, info os.FileInfo, err error) error {
424		if err != nil {
425			return err
426		}
427
428		name := info.Name()
429		if info.IsDir() {
430			if strings.HasPrefix(name, ".") {
431				return filepath.SkipDir
432			}
433			return nil
434		}
435
436		if strings.HasPrefix(name, ".") {
437			return nil
438		}
439
440		if strings.HasSuffix(name, ".pom") {
441			path, err = filepath.Rel(absDir, path)
442			if err != nil {
443				return err
444			}
445			filenames = append(filenames, filepath.Join(dir, path))
446		}
447		return nil
448	})
449	if err != nil {
450		fmt.Fprintln(os.Stderr, "Error walking files:", err)
451		os.Exit(1)
452	}
453
454	if len(filenames) == 0 {
455		fmt.Fprintln(os.Stderr, "Error: no *.pom files found under", dir)
456		os.Exit(1)
457	}
458
459	sort.Strings(filenames)
460
461	poms := []*Pom{}
462	modules := make(map[string]*Pom)
463	duplicate := false
464	for _, filename := range filenames {
465		pom, err := parse(filename)
466		if err != nil {
467			fmt.Fprintln(os.Stderr, "Error converting", filename, err)
468			os.Exit(1)
469		}
470
471		if pom != nil {
472			key := pom.MkName()
473			if excludes[key] {
474				continue
475			}
476
477			if old, ok := modules[key]; ok {
478				fmt.Fprintln(os.Stderr, "Module", key, "defined twice:", old.PomFile, pom.PomFile)
479				duplicate = true
480			}
481
482			poms = append(poms, pom)
483			modules[key] = pom
484		}
485	}
486	if duplicate {
487		os.Exit(1)
488	}
489
490	for _, pom := range poms {
491		pom.FixDeps(modules)
492	}
493
494	fmt.Println("# Automatically generated with:")
495	fmt.Println("# pom2mk", strings.Join(proptools.ShellEscapeList(os.Args[1:]), " "))
496	fmt.Println("LOCAL_PATH := $(call my-dir)")
497
498	for _, pom := range poms {
499		var err error
500		if staticDeps {
501			err = mkDepsTemplate.Execute(os.Stdout, pom)
502		} else {
503			err = mkTemplate.Execute(os.Stdout, pom)
504		}
505		if err != nil {
506			fmt.Fprintln(os.Stderr, "Error writing", pom.PomFile, pom.MkName(), err)
507			os.Exit(1)
508		}
509	}
510}
511