diff options
author | Colin Cross <ccross@android.com> | 2018-03-28 14:58:31 -0700 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2018-04-16 15:24:47 -0700 |
commit | a97c5d3f08c1df08c73cee4808feb3c1e015bdc8 (patch) | |
tree | e5e21a8264e93af5c588f4780ca4dee874245572 /java/java_resources.go | |
parent | ad6cbf18da303f1bd498a4ecf7856a23eeccfca5 (diff) |
Add support for android_library modules
Add support for compiling android_library modules into AARs,
and refactor app support on top of it.
Bug: 73724997
Test: app_test.go
Change-Id: I1dfac5fffe577c6680bc4709147b2061eb7d819c
Diffstat (limited to 'java/java_resources.go')
-rw-r--r-- | java/java_resources.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/java/java_resources.go b/java/java_resources.go new file mode 100644 index 000000000..e02709dc0 --- /dev/null +++ b/java/java_resources.go @@ -0,0 +1,113 @@ +// Copyright 2015 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package java + +import ( + "fmt" + "path/filepath" + "strings" + + "android/soong/android" +) + +var resourceExcludes = []string{ + "**/*.java", + "**/package.html", + "**/overview.html", + "**/.*.swp", + "**/.DS_Store", + "**/*~", +} + +func ResourceDirsToJarArgs(ctx android.ModuleContext, + resourceDirs, excludeResourceDirs []string) (args []string, deps android.Paths) { + var excludeDirs []string + var excludeFiles []string + + for _, exclude := range excludeResourceDirs { + dirs := ctx.Glob(android.PathForModuleSrc(ctx).Join(ctx, exclude).String(), nil) + for _, dir := range dirs { + excludeDirs = append(excludeDirs, dir.String()) + excludeFiles = append(excludeFiles, dir.(android.ModuleSrcPath).Join(ctx, "**/*").String()) + } + } + + excludeFiles = append(excludeFiles, resourceExcludes...) + + for _, resourceDir := range resourceDirs { + // resourceDir may be a glob, resolve it first + dirs := ctx.Glob(android.PathForModuleSrc(ctx).Join(ctx, resourceDir).String(), excludeDirs) + for _, dir := range dirs { + files := ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), excludeFiles) + + deps = append(deps, files...) + + if len(files) > 0 { + args = append(args, "-C", dir.String()) + + for _, f := range files { + path := f.String() + if !strings.HasPrefix(path, dir.String()) { + panic(fmt.Errorf("path %q does not start with %q", path, dir)) + } + args = append(args, "-f", path) + } + } + } + } + + return args, deps +} + +// Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns +// that should not be treated as resources (including *.java). +func ResourceFilesToJarArgs(ctx android.ModuleContext, + res, exclude []string) (args []string, deps android.Paths) { + + exclude = append([]string(nil), exclude...) + exclude = append(exclude, resourceExcludes...) + return resourceFilesToJarArgs(ctx, res, exclude) +} + +// Convert java_resources properties to arguments to soong_zip -jar, keeping files that should +// normally not used as resources like *.java +func SourceFilesToJarArgs(ctx android.ModuleContext, + res, exclude []string) (args []string, deps android.Paths) { + + return resourceFilesToJarArgs(ctx, res, exclude) +} + +func resourceFilesToJarArgs(ctx android.ModuleContext, + res, exclude []string) (args []string, deps android.Paths) { + + files := ctx.ExpandSources(res, exclude) + + lastDir := "" + for i, f := range files { + rel := f.Rel() + path := f.String() + if !strings.HasSuffix(path, rel) { + panic(fmt.Errorf("path %q does not end with %q", path, rel)) + } + dir := filepath.Clean(strings.TrimSuffix(path, rel)) + if i == 0 || dir != lastDir { + args = append(args, "-C", dir) + } + args = append(args, "-f", path) + lastDir = dir + } + + return args, files +} |