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