blob: e02709dc0a562b11df9724d4587ccdee516a86b5 [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 Cross0ead1d72018-04-10 13:07:42 -070035 resourceDirs, excludeResourceDirs []string) (args []string, deps android.Paths) {
36 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 Cross0ead1d72018-04-10 13:07:42 -070047 excludeFiles = append(excludeFiles, resourceExcludes...)
Colin Cross3d7678f2015-04-24 15:16:39 -070048
Colin Cross0ead1d72018-04-10 13:07:42 -070049 for _, resourceDir := range resourceDirs {
50 // resourceDir may be a glob, resolve it first
51 dirs := ctx.Glob(android.PathForModuleSrc(ctx).Join(ctx, resourceDir).String(), excludeDirs)
52 for _, dir := range dirs {
53 files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles)
Colin Cross3d7678f2015-04-24 15:16:39 -070054
Colin Cross0ead1d72018-04-10 13:07:42 -070055 deps = append(deps, files...)
Colin Crossaf9c55b2017-10-03 14:50:08 -070056
Colin Cross0ead1d72018-04-10 13:07:42 -070057 if len(files) > 0 {
58 args = append(args, "-C", dir.String())
Colin Crossaf9c55b2017-10-03 14:50:08 -070059
Colin Cross0ead1d72018-04-10 13:07:42 -070060 for _, f := range files {
61 path := f.String()
62 if !strings.HasPrefix(path, dir.String()) {
63 panic(fmt.Errorf("path %q does not start with %q", path, dir))
64 }
65 args = append(args, "-f", path)
Colin Crossaf9c55b2017-10-03 14:50:08 -070066 }
Colin Crossaf9c55b2017-10-03 14:50:08 -070067 }
Colin Cross3d7678f2015-04-24 15:16:39 -070068 }
Colin Cross2fe66872015-03-30 17:20:39 -070069 }
70
Colin Cross40a36712017-09-27 17:41:35 -070071 return args, deps
Colin Cross2fe66872015-03-30 17:20:39 -070072}
Colin Cross0f37af02017-09-27 17:42:05 -070073
Colin Cross23729232017-10-03 13:14:07 -070074// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
75// that should not be treated as resources (including *.java).
Colin Cross0f37af02017-09-27 17:42:05 -070076func ResourceFilesToJarArgs(ctx android.ModuleContext,
77 res, exclude []string) (args []string, deps android.Paths) {
Colin Cross23729232017-10-03 13:14:07 -070078
79 exclude = append([]string(nil), exclude...)
80 exclude = append(exclude, resourceExcludes...)
81 return resourceFilesToJarArgs(ctx, res, exclude)
82}
83
84// Convert java_resources properties to arguments to soong_zip -jar, keeping files that should
85// normally not used as resources like *.java
86func SourceFilesToJarArgs(ctx android.ModuleContext,
87 res, exclude []string) (args []string, deps android.Paths) {
88
89 return resourceFilesToJarArgs(ctx, res, exclude)
90}
91
92func resourceFilesToJarArgs(ctx android.ModuleContext,
93 res, exclude []string) (args []string, deps android.Paths) {
94
Colin Cross0f37af02017-09-27 17:42:05 -070095 files := ctx.ExpandSources(res, exclude)
96
Colin Crossaf9c55b2017-10-03 14:50:08 -070097 lastDir := ""
98 for i, f := range files {
Colin Cross0f37af02017-09-27 17:42:05 -070099 rel := f.Rel()
100 path := f.String()
101 if !strings.HasSuffix(path, rel) {
102 panic(fmt.Errorf("path %q does not end with %q", path, rel))
103 }
Colin Crossaf9c55b2017-10-03 14:50:08 -0700104 dir := filepath.Clean(strings.TrimSuffix(path, rel))
105 if i == 0 || dir != lastDir {
106 args = append(args, "-C", dir)
107 }
108 args = append(args, "-f", path)
109 lastDir = dir
Colin Cross0f37af02017-09-27 17:42:05 -0700110 }
111
112 return args, files
113}