blob: a596fd7e65fcf7bfe3ce279fe96ce13c9c521934 [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,
35 resourceDirs, excludeDirs []string) (args []string, deps android.Paths) {
Colin Cross3d7678f2015-04-24 15:16:39 -070036 var excludes []string
Colin Cross2fe66872015-03-30 17:20:39 -070037
Dan Willemsen2ef08f42015-06-30 18:15:24 -070038 for _, exclude := range excludeDirs {
Colin Cross0532fb02017-10-02 16:57:40 -070039 excludes = append(excludes,
40 filepath.Join(android.PathForModuleSrc(ctx, exclude).String(), "**/*"))
Colin Cross3d7678f2015-04-24 15:16:39 -070041 }
42
43 excludes = append(excludes, resourceExcludes...)
44
Colin Crossaf9c55b2017-10-03 14:50:08 -070045 for _, dir := range resourceDirs {
46 dir := android.PathForModuleSrc(ctx, dir).String()
47 files := ctx.Glob(filepath.Join(dir, "**/*"), excludes)
Colin Cross3d7678f2015-04-24 15:16:39 -070048
Colin Crossaf9c55b2017-10-03 14:50:08 -070049 deps = append(deps, files...)
50
51 if len(files) > 0 {
52 args = append(args, "-C", dir)
53
54 for _, f := range files {
55 path := f.String()
56 if !strings.HasPrefix(path, dir) {
57 panic(fmt.Errorf("path %q does not start with %q", path, dir))
58 }
59 args = append(args, "-f", path)
60 }
Colin Cross3d7678f2015-04-24 15:16:39 -070061 }
Colin Cross2fe66872015-03-30 17:20:39 -070062 }
63
Colin Cross40a36712017-09-27 17:41:35 -070064 return args, deps
Colin Cross2fe66872015-03-30 17:20:39 -070065}
Colin Cross0f37af02017-09-27 17:42:05 -070066
Colin Cross23729232017-10-03 13:14:07 -070067// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
68// that should not be treated as resources (including *.java).
Colin Cross0f37af02017-09-27 17:42:05 -070069func ResourceFilesToJarArgs(ctx android.ModuleContext,
70 res, exclude []string) (args []string, deps android.Paths) {
Colin Cross23729232017-10-03 13:14:07 -070071
72 exclude = append([]string(nil), exclude...)
73 exclude = append(exclude, resourceExcludes...)
74 return resourceFilesToJarArgs(ctx, res, exclude)
75}
76
77// Convert java_resources properties to arguments to soong_zip -jar, keeping files that should
78// normally not used as resources like *.java
79func SourceFilesToJarArgs(ctx android.ModuleContext,
80 res, exclude []string) (args []string, deps android.Paths) {
81
82 return resourceFilesToJarArgs(ctx, res, exclude)
83}
84
85func resourceFilesToJarArgs(ctx android.ModuleContext,
86 res, exclude []string) (args []string, deps android.Paths) {
87
Colin Cross0f37af02017-09-27 17:42:05 -070088 files := ctx.ExpandSources(res, exclude)
89
Colin Crossaf9c55b2017-10-03 14:50:08 -070090 lastDir := ""
91 for i, f := range files {
Colin Cross0f37af02017-09-27 17:42:05 -070092 rel := f.Rel()
93 path := f.String()
94 if !strings.HasSuffix(path, rel) {
95 panic(fmt.Errorf("path %q does not end with %q", path, rel))
96 }
Colin Crossaf9c55b2017-10-03 14:50:08 -070097 dir := filepath.Clean(strings.TrimSuffix(path, rel))
98 if i == 0 || dir != lastDir {
99 args = append(args, "-C", dir)
100 }
101 args = append(args, "-f", path)
102 lastDir = dir
Colin Cross0f37af02017-09-27 17:42:05 -0700103 }
104
105 return args, files
106}