Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame^] | 18 | "fmt" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 19 | "path/filepath" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame^] | 20 | "strings" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 21 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/bootstrap" |
| 23 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 24 | "android/soong/android" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var resourceExcludes = []string{ |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 28 | "**/*.java", |
| 29 | "**/package.html", |
| 30 | "**/overview.html", |
| 31 | "**/.*.swp", |
| 32 | "**/.DS_Store", |
| 33 | "**/*~", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 36 | func 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 Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 45 | func ResourceDirsToJarArgs(ctx android.ModuleContext, |
| 46 | resourceDirs, excludeDirs []string) (args []string, deps android.Paths) { |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 47 | var excludes []string |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 48 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 49 | for _, exclude := range excludeDirs { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 50 | excludes = append(excludes, android.PathForModuleSrc(ctx, exclude, "**/*").String()) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | excludes = append(excludes, resourceExcludes...) |
| 54 | |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 55 | for _, resourceDir := range resourceDirs { |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 56 | if isStringInSlice(resourceDir, excludeDirs) { |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 57 | continue |
| 58 | } |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 59 | resourceDir := android.PathForModuleSrc(ctx, resourceDir) |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 60 | dirs := ctx.Glob(resourceDir.String(), nil) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 61 | for _, dir := range dirs { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 62 | fileListFile := android.ResPathWithName(ctx, dir, "resources.list") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 63 | depFile := fileListFile.String() + ".d" |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 64 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 65 | pattern := filepath.Join(dir.String(), "**/*") |
| 66 | bootstrap.GlobFile(ctx, pattern, excludes, fileListFile.String(), depFile) |
Colin Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 67 | args = append(args, |
| 68 | "-C", dir.String(), |
| 69 | "-l", fileListFile.String()) |
| 70 | deps = append(deps, fileListFile) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 71 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Colin Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 74 | return args, deps |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 75 | } |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame^] | 76 | |
| 77 | func 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 | } |