Home | History | Annotate | Download | only in java
      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 (
     18 	"fmt"
     19 	"path/filepath"
     20 	"strings"
     21 
     22 	"android/soong/android"
     23 )
     24 
     25 var resourceExcludes = []string{
     26 	"**/*.java",
     27 	"**/package.html",
     28 	"**/overview.html",
     29 	"**/.*.swp",
     30 	"**/.DS_Store",
     31 	"**/*~",
     32 }
     33 
     34 func ResourceDirsToJarArgs(ctx android.ModuleContext,
     35 	resourceDirs, excludeDirs []string) (args []string, deps android.Paths) {
     36 	var excludes []string
     37 
     38 	for _, exclude := range excludeDirs {
     39 		excludes = append(excludes,
     40 			filepath.Join(android.PathForModuleSrc(ctx, exclude).String(), "**/*"))
     41 	}
     42 
     43 	excludes = append(excludes, resourceExcludes...)
     44 
     45 	for _, dir := range resourceDirs {
     46 		dir := android.PathForModuleSrc(ctx, dir).String()
     47 		files := ctx.Glob(filepath.Join(dir, "**/*"), excludes)
     48 
     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 			}
     61 		}
     62 	}
     63 
     64 	return args, deps
     65 }
     66 
     67 // Convert java_resources properties to arguments to soong_zip -jar, ignoring common patterns
     68 // that should not be treated as resources (including *.java).
     69 func ResourceFilesToJarArgs(ctx android.ModuleContext,
     70 	res, exclude []string) (args []string, deps android.Paths) {
     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 
     88 	files := ctx.ExpandSources(res, exclude)
     89 
     90 	lastDir := ""
     91 	for i, f := range files {
     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 		}
     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
    103 	}
    104 
    105 	return args, files
    106 }
    107