• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2015 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 java
16
17import (
18	"fmt"
19	"path/filepath"
20	"strings"
21
22	"github.com/google/blueprint/pathtools"
23
24	"android/soong/android"
25)
26
27var resourceExcludes = []string{
28	"**/*.java",
29	"**/package.html",
30	"**/overview.html",
31	"**/.*.swp",
32	"**/.DS_Store",
33	"**/*~",
34}
35
36func ResourceDirsToJarArgs(ctx android.ModuleContext,
37	resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) {
38	var excludeDirs []string
39	var excludeFiles []string
40
41	for _, exclude := range excludeResourceDirs {
42		dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, exclude).String(), nil)
43		for _, dir := range dirs {
44			excludeDirs = append(excludeDirs, dir.String())
45			excludeFiles = append(excludeFiles, dir.(android.SourcePath).Join(ctx, "**/*").String())
46		}
47	}
48
49	excludeFiles = append(excludeFiles, android.PathsForModuleSrc(ctx, excludeResourceFiles).Strings()...)
50
51	excludeFiles = append(excludeFiles, resourceExcludes...)
52
53	for _, resourceDir := range resourceDirs {
54		// resourceDir may be a glob, resolve it first
55		dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs)
56		for _, dir := range dirs {
57			files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles)
58
59			deps = append(deps, files...)
60
61			if len(files) > 0 {
62				args = append(args, "-C", dir.String())
63
64				for _, f := range files {
65					path := f.String()
66					if !strings.HasPrefix(path, dir.String()) {
67						panic(fmt.Errorf("path %q does not start with %q", path, dir))
68					}
69					args = append(args, "-f", pathtools.MatchEscape(path))
70				}
71			}
72		}
73	}
74
75	return args, deps
76}
77
78// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
79// that should not be treated as resources (including *.java).
80func ResourceFilesToJarArgs(ctx android.ModuleContext,
81	res, exclude []string) (args []string, deps android.Paths) {
82
83	exclude = append([]string(nil), exclude...)
84	exclude = append(exclude, resourceExcludes...)
85	return resourceFilesToJarArgs(ctx, res, exclude)
86}
87
88func resourceFilesToJarArgs(ctx android.ModuleContext,
89	res, exclude []string) (args []string, deps android.Paths) {
90
91	files := android.PathsForModuleSrcExcludes(ctx, res, exclude)
92
93	args = resourcePathsToJarArgs(files)
94
95	return args, files
96}
97
98func resourcePathsToJarArgs(files android.Paths) []string {
99	var args []string
100
101	lastDir := ""
102	for i, f := range files {
103		rel := f.Rel()
104		path := f.String()
105		if !strings.HasSuffix(path, rel) {
106			panic(fmt.Errorf("path %q does not end with %q", path, rel))
107		}
108		dir := filepath.Clean(strings.TrimSuffix(path, rel))
109		if i == 0 || dir != lastDir {
110			args = append(args, "-C", dir)
111		}
112		args = append(args, "-f", pathtools.MatchEscape(path))
113		lastDir = dir
114	}
115
116	return args
117}
118