blob: 9f5e5e0cecf69e967c8e94052882d63cd89d2192 [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 Cross7f19f372016-11-01 11:10:25 -070022 "github.com/google/blueprint/bootstrap"
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
Dan Willemsen2ef08f42015-06-30 18:15:24 -070036func isStringInSlice(str string, slice []string) bool {
37 for _, s := range slice {
38 if s == str {
39 return true
40 }
41 }
42 return false
43}
44
Colin Cross40a36712017-09-27 17:41:35 -070045func ResourceDirsToJarArgs(ctx android.ModuleContext,
46 resourceDirs, excludeDirs []string) (args []string, deps android.Paths) {
Colin Cross3d7678f2015-04-24 15:16:39 -070047 var excludes []string
Colin Cross2fe66872015-03-30 17:20:39 -070048
Dan Willemsen2ef08f42015-06-30 18:15:24 -070049 for _, exclude := range excludeDirs {
Colin Cross635c3b02016-05-18 15:37:25 -070050 excludes = append(excludes, android.PathForModuleSrc(ctx, exclude, "**/*").String())
Colin Cross3d7678f2015-04-24 15:16:39 -070051 }
52
53 excludes = append(excludes, resourceExcludes...)
54
Colin Cross3d7678f2015-04-24 15:16:39 -070055 for _, resourceDir := range resourceDirs {
Dan Willemsen2ef08f42015-06-30 18:15:24 -070056 if isStringInSlice(resourceDir, excludeDirs) {
Colin Cross3d7678f2015-04-24 15:16:39 -070057 continue
58 }
Colin Cross635c3b02016-05-18 15:37:25 -070059 resourceDir := android.PathForModuleSrc(ctx, resourceDir)
Colin Cross7f19f372016-11-01 11:10:25 -070060 dirs := ctx.Glob(resourceDir.String(), nil)
Colin Cross3d7678f2015-04-24 15:16:39 -070061 for _, dir := range dirs {
Colin Cross635c3b02016-05-18 15:37:25 -070062 fileListFile := android.ResPathWithName(ctx, dir, "resources.list")
Dan Willemsen34cc69e2015-09-23 15:26:20 -070063 depFile := fileListFile.String() + ".d"
Colin Cross3d7678f2015-04-24 15:16:39 -070064
Colin Cross7f19f372016-11-01 11:10:25 -070065 pattern := filepath.Join(dir.String(), "**/*")
66 bootstrap.GlobFile(ctx, pattern, excludes, fileListFile.String(), depFile)
Colin Cross40a36712017-09-27 17:41:35 -070067 args = append(args,
68 "-C", dir.String(),
69 "-l", fileListFile.String())
70 deps = append(deps, fileListFile)
Colin Cross3d7678f2015-04-24 15:16:39 -070071 }
Colin Cross2fe66872015-03-30 17:20:39 -070072 }
73
Colin Cross40a36712017-09-27 17:41:35 -070074 return args, deps
Colin Cross2fe66872015-03-30 17:20:39 -070075}
Colin Cross0f37af02017-09-27 17:42:05 -070076
77func ResourceFilesToJarArgs(ctx android.ModuleContext,
78 res, exclude []string) (args []string, deps android.Paths) {
79 files := ctx.ExpandSources(res, exclude)
80
81 for _, f := range files {
82 rel := f.Rel()
83 path := f.String()
84 if !strings.HasSuffix(path, rel) {
85 panic(fmt.Errorf("path %q does not end with %q", path, rel))
86 }
87 path = filepath.Clean(strings.TrimSuffix(path, rel))
88 args = append(args, "-C", filepath.Clean(path), "-f", f.String())
89 }
90
91 return args, files
92}