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 | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint/pathtools" |
| 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 | |
Colin Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 36 | func ResourceDirsToJarArgs(ctx android.ModuleContext, |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 37 | resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) { |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 38 | var excludeDirs []string |
| 39 | var excludeFiles []string |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 40 | |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 41 | for _, exclude := range excludeResourceDirs { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 42 | dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, exclude).String(), nil) |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 43 | for _, dir := range dirs { |
| 44 | excludeDirs = append(excludeDirs, dir.String()) |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 45 | excludeFiles = append(excludeFiles, dir.(android.SourcePath).Join(ctx, "**/*").String()) |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 46 | } |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 49 | excludeFiles = append(excludeFiles, android.PathsForModuleSrc(ctx, excludeResourceFiles).Strings()...) |
Colin Cross | cedd476 | 2018-09-13 11:26:19 -0700 | [diff] [blame] | 50 | |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 51 | excludeFiles = append(excludeFiles, resourceExcludes...) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 52 | |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 53 | for _, resourceDir := range resourceDirs { |
| 54 | // resourceDir may be a glob, resolve it first |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 55 | dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs) |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 56 | for _, dir := range dirs { |
| 57 | files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles) |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 58 | |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 59 | deps = append(deps, files...) |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 60 | |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 61 | if len(files) > 0 { |
| 62 | args = append(args, "-C", dir.String()) |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 63 | |
Colin Cross | 0ead1d7 | 2018-04-10 13:07:42 -0700 | [diff] [blame] | 64 | for _, f := range files { |
| 65 | path := f.String() |
| 66 | if !strings.HasPrefix(path, dir.String()) { |
| 67 | panic(fmt.Errorf("path %q does not start with %q", path, dir)) |
| 68 | } |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 69 | args = append(args, "-f", pathtools.MatchEscape(path)) |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 70 | } |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 71 | } |
Colin Cross | 3d7678f | 2015-04-24 15:16:39 -0700 | [diff] [blame] | 72 | } |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Colin Cross | 40a3671 | 2017-09-27 17:41:35 -0700 | [diff] [blame] | 75 | return args, deps |
Colin Cross | 2fe6687 | 2015-03-30 17:20:39 -0700 | [diff] [blame] | 76 | } |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 77 | |
Colin Cross | 2372923 | 2017-10-03 13:14:07 -0700 | [diff] [blame] | 78 | // Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns |
| 79 | // that should not be treated as resources (including *.java). |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 80 | func ResourceFilesToJarArgs(ctx android.ModuleContext, |
| 81 | res, exclude []string) (args []string, deps android.Paths) { |
Colin Cross | 2372923 | 2017-10-03 13:14:07 -0700 | [diff] [blame] | 82 | |
| 83 | exclude = append([]string(nil), exclude...) |
| 84 | exclude = append(exclude, resourceExcludes...) |
| 85 | return resourceFilesToJarArgs(ctx, res, exclude) |
| 86 | } |
| 87 | |
Colin Cross | 2372923 | 2017-10-03 13:14:07 -0700 | [diff] [blame] | 88 | func resourceFilesToJarArgs(ctx android.ModuleContext, |
| 89 | res, exclude []string) (args []string, deps android.Paths) { |
| 90 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 91 | files := android.PathsForModuleSrcExcludes(ctx, res, exclude) |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 92 | |
Colin Cross | 988708c | 2019-05-06 14:04:11 -0700 | [diff] [blame] | 93 | args = resourcePathsToJarArgs(files) |
| 94 | |
| 95 | return args, files |
| 96 | } |
| 97 | |
| 98 | func resourcePathsToJarArgs(files android.Paths) []string { |
| 99 | var args []string |
| 100 | |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 101 | lastDir := "" |
| 102 | for i, f := range files { |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 103 | rel := f.Rel() |
| 104 | path := f.String() |
| 105 | if !strings.HasSuffix(path, rel) { |
| 106 | panic(fmt.Errorf("path %q does not end with %q", path, rel)) |
| 107 | } |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 108 | dir := filepath.Clean(strings.TrimSuffix(path, rel)) |
| 109 | if i == 0 || dir != lastDir { |
| 110 | args = append(args, "-C", dir) |
| 111 | } |
Colin Cross | 1d98ee2 | 2018-09-18 17:05:15 -0700 | [diff] [blame] | 112 | args = append(args, "-f", pathtools.MatchEscape(path)) |
Colin Cross | af9c55b | 2017-10-03 14:50:08 -0700 | [diff] [blame] | 113 | lastDir = dir |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Colin Cross | 988708c | 2019-05-06 14:04:11 -0700 | [diff] [blame] | 116 | return args |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 117 | } |