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 | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 22 | "android/soong/android" |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | var resourceExcludes = []string{ |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 26 | "**/*.java", |
| 27 | "**/package.html", |
| 28 | "**/overview.html", |
| 29 | "**/.*.swp", |
| 30 | "**/.DS_Store", |
| 31 | "**/*~", |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Colin Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 34 | func ResourceDirsToJarArgs(ctx android.ModuleContext, |
| 35 | resourceDirs, excludeDirs []string) (args []string, deps android.Paths) { |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 36 | var excludes []string |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 37 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 38 | for _, exclude := range excludeDirs { |
Colin Cross | 0532fb0 | 2017-10-02 16:57:40 -0700 | [diff] [blame] | 39 | excludes = append(excludes, |
| 40 | filepath.Join(android.PathForModuleSrc(ctx, exclude).String(), "**/*")) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | excludes = append(excludes, resourceExcludes...) |
| 44 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 45 | for _, dir := range resourceDirs { |
| 46 | dir := android.PathForModuleSrc(ctx, dir).String() |
| 47 | files := ctx.Glob(filepath.Join(dir, "**/*"), excludes) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 48 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 49 | 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 Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 61 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Colin Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 64 | return args, deps |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 65 | } |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 66 | |
Colin Cross | 2372923 | 2017-10-03 13:14:07 -0700 | [diff] [blame] | 67 | // Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns |
| 68 | // that should not be treated as resources (including *.java). |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 69 | func ResourceFilesToJarArgs(ctx android.ModuleContext, |
| 70 | res, exclude []string) (args []string, deps android.Paths) { |
Colin Cross | 2372923 | 2017-10-03 13:14:07 -0700 | [diff] [blame] | 71 | |
| 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 |
| 79 | func SourceFilesToJarArgs(ctx android.ModuleContext, |
| 80 | res, exclude []string) (args []string, deps android.Paths) { |
| 81 | |
| 82 | return resourceFilesToJarArgs(ctx, res, exclude) |
| 83 | } |
| 84 | |
| 85 | func resourceFilesToJarArgs(ctx android.ModuleContext, |
| 86 | res, exclude []string) (args []string, deps android.Paths) { |
| 87 | |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 88 | files := ctx.ExpandSources(res, exclude) |
| 89 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 90 | lastDir := "" |
| 91 | for i, f := range files { |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 92 | 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 Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 97 | 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 Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return args, files |
| 106 | } |