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