blob: 787d74a0dc17683cb4e22e807fda719aceb79194 [file] [log] [blame]
Colin Cross2fe66872015-03-30 17:20:39 -07001// 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 (
Colin Cross0f37af02017-09-27 17:42:05 -070018 "fmt"
Colin Cross2fe66872015-03-30 17:20:39 -070019 "path/filepath"
Colin Cross0f37af02017-09-27 17:42:05 -070020 "strings"
Colin Cross2fe66872015-03-30 17:20:39 -070021
Colin Cross1d98ee22018-09-18 17:05:15 -070022 "github.com/google/blueprint/pathtools"
23
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Colin Cross2fe66872015-03-30 17:20:39 -070025)
26
27var resourceExcludes = []string{
Colin Cross3d7678f2015-04-24 15:16:39 -070028 "**/*.java",
29 "**/package.html",
30 "**/overview.html",
31 "**/.*.swp",
32 "**/.DS_Store",
33 "**/*~",
Colin Cross2fe66872015-03-30 17:20:39 -070034}
35
Colin Cross40a36712017-09-27 17:41:35 -070036func ResourceDirsToJarArgs(ctx android.ModuleContext,
Colin Crosscedd4762018-09-13 11:26:19 -070037 resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) {
Colin Cross0ead1d72018-04-10 13:07:42 -070038 var excludeDirs []string
39 var excludeFiles []string
Colin Cross2fe66872015-03-30 17:20:39 -070040
Colin Cross0ead1d72018-04-10 13:07:42 -070041 for _, exclude := range excludeResourceDirs {
Colin Cross07e51612019-03-05 12:46:40 -080042 dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, exclude).String(), nil)
Colin Cross0ead1d72018-04-10 13:07:42 -070043 for _, dir := range dirs {
44 excludeDirs = append(excludeDirs, dir.String())
Colin Cross07e51612019-03-05 12:46:40 -080045 excludeFiles = append(excludeFiles, dir.(android.SourcePath).Join(ctx, "**/*").String())
Colin Cross0ead1d72018-04-10 13:07:42 -070046 }
Colin Cross3d7678f2015-04-24 15:16:39 -070047 }
48
Colin Cross8a497952019-03-05 22:25:09 -080049 excludeFiles = append(excludeFiles, android.PathsForModuleSrc(ctx, excludeResourceFiles).Strings()...)
Colin Crosscedd4762018-09-13 11:26:19 -070050
Colin Cross0ead1d72018-04-10 13:07:42 -070051 excludeFiles = append(excludeFiles, resourceExcludes...)
Colin Cross3d7678f2015-04-24 15:16:39 -070052
Colin Cross0ead1d72018-04-10 13:07:42 -070053 for _, resourceDir := range resourceDirs {
54 // resourceDir may be a glob, resolve it first
Colin Cross07e51612019-03-05 12:46:40 -080055 dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs)
Colin Cross0ead1d72018-04-10 13:07:42 -070056 for _, dir := range dirs {
57 files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles)
Colin Cross3d7678f2015-04-24 15:16:39 -070058
Colin Cross0ead1d72018-04-10 13:07:42 -070059 deps = append(deps, files...)
Colin Crossaf9c55b2017-10-03 14:50:08 -070060
Colin Cross0ead1d72018-04-10 13:07:42 -070061 if len(files) > 0 {
62 args = append(args, "-C", dir.String())
Colin Crossaf9c55b2017-10-03 14:50:08 -070063
Colin Cross0ead1d72018-04-10 13:07:42 -070064 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 }
Colin Cross1d98ee22018-09-18 17:05:15 -070069 args = append(args, "-f", pathtools.MatchEscape(path))
Colin Crossaf9c55b2017-10-03 14:50:08 -070070 }
Colin Crossaf9c55b2017-10-03 14:50:08 -070071 }
Colin Cross3d7678f2015-04-24 15:16:39 -070072 }
Colin Cross2fe66872015-03-30 17:20:39 -070073 }
74
Colin Cross40a36712017-09-27 17:41:35 -070075 return args, deps
Colin Cross2fe66872015-03-30 17:20:39 -070076}
Colin Cross0f37af02017-09-27 17:42:05 -070077
Colin Cross23729232017-10-03 13:14:07 -070078// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
79// that should not be treated as resources (including *.java).
Colin Cross0f37af02017-09-27 17:42:05 -070080func ResourceFilesToJarArgs(ctx android.ModuleContext,
81 res, exclude []string) (args []string, deps android.Paths) {
Colin Cross23729232017-10-03 13:14:07 -070082
83 exclude = append([]string(nil), exclude...)
84 exclude = append(exclude, resourceExcludes...)
85 return resourceFilesToJarArgs(ctx, res, exclude)
86}
87
Colin Cross23729232017-10-03 13:14:07 -070088func resourceFilesToJarArgs(ctx android.ModuleContext,
89 res, exclude []string) (args []string, deps android.Paths) {
90
Colin Cross8a497952019-03-05 22:25:09 -080091 files := android.PathsForModuleSrcExcludes(ctx, res, exclude)
Colin Cross0f37af02017-09-27 17:42:05 -070092
Colin Cross988708c2019-05-06 14:04:11 -070093 args = resourcePathsToJarArgs(files)
94
95 return args, files
96}
97
98func resourcePathsToJarArgs(files android.Paths) []string {
99 var args []string
100
Colin Crossaf9c55b2017-10-03 14:50:08 -0700101 lastDir := ""
102 for i, f := range files {
Colin Cross0f37af02017-09-27 17:42:05 -0700103 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 }
Colin Crossaf9c55b2017-10-03 14:50:08 -0700108 dir := filepath.Clean(strings.TrimSuffix(path, rel))
109 if i == 0 || dir != lastDir {
110 args = append(args, "-C", dir)
111 }
Colin Cross1d98ee22018-09-18 17:05:15 -0700112 args = append(args, "-f", pathtools.MatchEscape(path))
Colin Crossaf9c55b2017-10-03 14:50:08 -0700113 lastDir = dir
Colin Cross0f37af02017-09-27 17:42:05 -0700114 }
115
Colin Cross988708c2019-05-06 14:04:11 -0700116 return args
Colin Cross0f37af02017-09-27 17:42:05 -0700117}