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 15package java 16 17import ( 18 "fmt" 19 "path/filepath" 20 "strings" 21 22 "github.com/google/blueprint/pathtools" 23 24 "android/soong/android" 25) 26 27var resourceExcludes = []string{ 28 "**/*.java", 29 "**/package.html", 30 "**/overview.html", 31 "**/.*.swp", 32 "**/.DS_Store", 33 "**/*~", 34} 35 36type resourceDeps struct { 37 dir android.Path 38 files android.Paths 39} 40 41func ResourceDirsToFiles(ctx android.BaseModuleContext, 42 resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (deps []resourceDeps) { 43 var excludeDirs []string 44 var excludeFiles []string 45 46 for _, exclude := range excludeResourceDirs { 47 dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, exclude).String(), nil) 48 for _, dir := range dirs { 49 excludeDirs = append(excludeDirs, dir.String()) 50 excludeFiles = append(excludeFiles, dir.(android.SourcePath).Join(ctx, "**/*").String()) 51 } 52 } 53 54 excludeFiles = append(excludeFiles, android.PathsForModuleSrc(ctx, excludeResourceFiles).Strings()...) 55 56 excludeFiles = append(excludeFiles, resourceExcludes...) 57 58 for _, resourceDir := range resourceDirs { 59 // resourceDir may be a glob, resolve it first 60 dirs := ctx.Glob(android.PathForSource(ctx, ctx.ModuleDir()).Join(ctx, resourceDir).String(), excludeDirs) 61 for _, dir := range dirs { 62 files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles) 63 deps = append(deps, resourceDeps{ 64 dir: dir, 65 files: files, 66 }) 67 } 68 } 69 70 return deps 71} 72 73func ResourceDirsToJarArgs(ctx android.ModuleContext, 74 resourceDirs, excludeResourceDirs, excludeResourceFiles []string) (args []string, deps android.Paths) { 75 resDeps := ResourceDirsToFiles(ctx, resourceDirs, excludeResourceDirs, excludeResourceFiles) 76 77 for _, resDep := range resDeps { 78 dir, files := resDep.dir, resDep.files 79 80 if len(files) > 0 { 81 args = append(args, "-C", dir.String()) 82 deps = append(deps, files...) 83 84 for _, f := range files { 85 path := f.String() 86 if !strings.HasPrefix(path, dir.String()) { 87 panic(fmt.Errorf("path %q does not start with %q", path, dir)) 88 } 89 args = append(args, "-f", pathtools.MatchEscape(path)) 90 } 91 } 92 93 } 94 95 return args, deps 96} 97 98// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns 99// that should not be treated as resources (including *.java). 100func ResourceFilesToJarArgs(ctx android.ModuleContext, 101 res, exclude []string) (args []string, deps android.Paths) { 102 103 exclude = append([]string(nil), exclude...) 104 exclude = append(exclude, resourceExcludes...) 105 return resourceFilesToJarArgs(ctx, res, exclude) 106} 107 108func resourceFilesToJarArgs(ctx android.ModuleContext, 109 res, exclude []string) (args []string, deps android.Paths) { 110 111 files := android.PathsForModuleSrcExcludes(ctx, res, exclude) 112 113 args = resourcePathsToJarArgs(files) 114 115 return args, files 116} 117 118func resourcePathsToJarArgs(files android.Paths) []string { 119 var args []string 120 121 lastDir := "" 122 for i, f := range files { 123 rel := f.Rel() 124 path := f.String() 125 if !strings.HasSuffix(path, rel) { 126 panic(fmt.Errorf("path %q does not end with %q", path, rel)) 127 } 128 dir := filepath.Clean(strings.TrimSuffix(path, rel)) 129 if i == 0 || dir != lastDir { 130 args = append(args, "-C", dir) 131 } 132 args = append(args, "-f", pathtools.MatchEscape(path)) 133 lastDir = dir 134 } 135 136 return args 137} 138