blob: fdc15906391bf52fd38c3d03d5d5cfe95f70912b [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 Cross635c3b02016-05-18 15:37:25 -070022 "android/soong/android"
Colin Cross2fe66872015-03-30 17:20:39 -070023)
24
25var resourceExcludes = []string{
Colin Cross3d7678f2015-04-24 15:16:39 -070026 "**/*.java",
27 "**/package.html",
28 "**/overview.html",
29 "**/.*.swp",
30 "**/.DS_Store",
31 "**/*~",
Colin Cross2fe66872015-03-30 17:20:39 -070032}
33
Colin Cross40a36712017-09-27 17:41:35 -070034func ResourceDirsToJarArgs(ctx android.ModuleContext,
Colin Crosscedd4762018-09-13 11:26:19 -070035 resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) {
Colin Cross0ead1d72018-04-10 13:07:42 -070036 var excludeDirs []string
37 var excludeFiles []string
Colin Cross2fe66872015-03-30 17:20:39 -070038
Colin Cross0ead1d72018-04-10 13:07:42 -070039 for _, exclude := range excludeResourceDirs {
40 dirs := ctx.Glob(android.PathForModuleSrc(ctx).Join(ctx, exclude).String(), nil)
41 for _, dir := range dirs {
42 excludeDirs = append(excludeDirs, dir.String())
43 excludeFiles = append(excludeFiles, dir.(android.ModuleSrcPath).Join(ctx, "**/*").String())
44 }
Colin Cross3d7678f2015-04-24 15:16:39 -070045 }
46
Colin Crosscedd4762018-09-13 11:26:19 -070047 excludeFiles = append(excludeFiles, ctx.ExpandSources(excludeResourceFiles, nil).Strings()...)
48
Colin Cross0ead1d72018-04-10 13:07:42 -070049 excludeFiles = append(excludeFiles, resourceExcludes...)
Colin Cross3d7678f2015-04-24 15:16:39 -070050
Colin Cross0ead1d72018-04-10 13:07:42 -070051 for _, resourceDir := range resourceDirs {
52 // resourceDir may be a glob, resolve it first
53 dirs := ctx.Glob(android.PathForModuleSrc(ctx).Join(ctx, resourceDir).String(), excludeDirs)
54 for _, dir := range dirs {
55 files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles)
Colin Cross3d7678f2015-04-24 15:16:39 -070056
Colin Cross0ead1d72018-04-10 13:07:42 -070057 deps = append(deps, files...)
Colin Crossaf9c55b2017-10-03 14:50:08 -070058
Colin Cross0ead1d72018-04-10 13:07:42 -070059 if len(files) > 0 {
60 args = append(args, "-C", dir.String())
Colin Crossaf9c55b2017-10-03 14:50:08 -070061
Colin Cross0ead1d72018-04-10 13:07:42 -070062 for _, f := range files {
63 path := f.String()
64 if !strings.HasPrefix(path, dir.String()) {
65 panic(fmt.Errorf("path %q does not start with %q", path, dir))
66 }
67 args = append(args, "-f", path)
Colin Crossaf9c55b2017-10-03 14:50:08 -070068 }
Colin Crossaf9c55b2017-10-03 14:50:08 -070069 }
Colin Cross3d7678f2015-04-24 15:16:39 -070070 }
Colin Cross2fe66872015-03-30 17:20:39 -070071 }
72
Colin Cross40a36712017-09-27 17:41:35 -070073 return args, deps
Colin Cross2fe66872015-03-30 17:20:39 -070074}
Colin Cross0f37af02017-09-27 17:42:05 -070075
Colin Cross23729232017-10-03 13:14:07 -070076// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
77// that should not be treated as resources (including *.java).
Colin Cross0f37af02017-09-27 17:42:05 -070078func ResourceFilesToJarArgs(ctx android.ModuleContext,
79 res, exclude []string) (args []string, deps android.Paths) {
Colin Cross23729232017-10-03 13:14:07 -070080
81 exclude = append([]string(nil), exclude...)
82 exclude = append(exclude, resourceExcludes...)
83 return resourceFilesToJarArgs(ctx, res, exclude)
84}
85
86// Convert java_resources properties to arguments to soong_zip -jar, keeping files that should
87// normally not used as resources like *.java
88func SourceFilesToJarArgs(ctx android.ModuleContext,
89 res, exclude []string) (args []string, deps android.Paths) {
90
91 return resourceFilesToJarArgs(ctx, res, exclude)
92}
93
94func resourceFilesToJarArgs(ctx android.ModuleContext,
95 res, exclude []string) (args []string, deps android.Paths) {
96
Colin Cross0f37af02017-09-27 17:42:05 -070097 files := ctx.ExpandSources(res, exclude)
98
Colin Crossaf9c55b2017-10-03 14:50:08 -070099 lastDir := ""
100 for i, f := range files {
Colin Cross0f37af02017-09-27 17:42:05 -0700101 rel := f.Rel()
102 path := f.String()
103 if !strings.HasSuffix(path, rel) {
104 panic(fmt.Errorf("path %q does not end with %q", path, rel))
105 }
Colin Crossaf9c55b2017-10-03 14:50:08 -0700106 dir := filepath.Clean(strings.TrimSuffix(path, rel))
107 if i == 0 || dir != lastDir {
108 args = append(args, "-C", dir)
109 }
110 args = append(args, "-f", path)
111 lastDir = dir
Colin Cross0f37af02017-09-27 17:42:05 -0700112 }
113
114 return args, files
115}