Home | History | Annotate | Download | only in java
      1 // Copyright 2018 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 	"path/filepath"
     19 	"strings"
     20 
     21 	"android/soong/android"
     22 )
     23 
     24 func init() {
     25 	android.RegisterPreSingletonType("overlay", OverlaySingletonFactory)
     26 }
     27 
     28 var androidResourceIgnoreFilenames = []string{
     29 	".svn",
     30 	".git",
     31 	".ds_store",
     32 	"*.scc",
     33 	".*",
     34 	"CVS",
     35 	"thumbs.db",
     36 	"picasa.ini",
     37 	"*~",
     38 }
     39 
     40 func androidResourceGlob(ctx android.ModuleContext, dir android.Path) android.Paths {
     41 	return ctx.GlobFiles(filepath.Join(dir.String(), "**/*"), androidResourceIgnoreFilenames)
     42 }
     43 
     44 type overlayGlobResult struct {
     45 	dir   string
     46 	paths android.DirectorySortedPaths
     47 
     48 	// Set to true of the product has selected that values in this overlay should not be moved to
     49 	// Runtime Resource Overlay (RRO) packages.
     50 	excludeFromRRO bool
     51 }
     52 
     53 const overlayDataKey = "overlayDataKey"
     54 
     55 type globbedResourceDir struct {
     56 	dir   android.Path
     57 	files android.Paths
     58 }
     59 
     60 func overlayResourceGlob(ctx android.ModuleContext, dir android.Path) (res []globbedResourceDir,
     61 	rroDirs android.Paths) {
     62 
     63 	overlayData := ctx.Config().Get(overlayDataKey).([]overlayGlobResult)
     64 
     65 	// Runtime resource overlays (RRO) may be turned on by the product config for some modules
     66 	rroEnabled := ctx.Config().EnforceRROForModule(ctx.ModuleName())
     67 
     68 	for _, data := range overlayData {
     69 		files := data.paths.PathsInDirectory(filepath.Join(data.dir, dir.String()))
     70 		if len(files) > 0 {
     71 			overlayModuleDir := android.PathForSource(ctx, data.dir, dir.String())
     72 			// If enforce RRO is enabled for this module and this overlay is not in the
     73 			// exclusion list, ignore the overlay.  The list of ignored overlays will be
     74 			// passed to Make to be turned into an RRO package.
     75 			if rroEnabled && !data.excludeFromRRO {
     76 				rroDirs = append(rroDirs, overlayModuleDir)
     77 			} else {
     78 				res = append(res, globbedResourceDir{
     79 					dir:   overlayModuleDir,
     80 					files: files,
     81 				})
     82 			}
     83 		}
     84 	}
     85 
     86 	return res, rroDirs
     87 }
     88 
     89 func OverlaySingletonFactory() android.Singleton {
     90 	return overlaySingleton{}
     91 }
     92 
     93 type overlaySingleton struct{}
     94 
     95 func (overlaySingleton) GenerateBuildActions(ctx android.SingletonContext) {
     96 	var overlayData []overlayGlobResult
     97 	overlayDirs := ctx.Config().ResourceOverlays()
     98 	for i := range overlayDirs {
     99 		// Iterate backwards through the list of overlay directories so that the later, lower-priority
    100 		// directories in the list show up earlier in the command line to aapt2.
    101 		overlay := overlayDirs[len(overlayDirs)-1-i]
    102 		var result overlayGlobResult
    103 		result.dir = overlay
    104 
    105 		// Mark overlays that will not have Runtime Resource Overlays enforced on them
    106 		// based on the product config
    107 		result.excludeFromRRO = ctx.Config().EnforceRROExcludedOverlay(overlay)
    108 
    109 		files, err := ctx.GlobWithDeps(filepath.Join(overlay, "**/*"), androidResourceIgnoreFilenames)
    110 		if err != nil {
    111 			ctx.Errorf("failed to glob resource dir %q: %s", overlay, err.Error())
    112 			continue
    113 		}
    114 		var paths android.Paths
    115 		for _, f := range files {
    116 			if !strings.HasSuffix(f, "/") {
    117 				paths = append(paths, android.PathForSource(ctx, f))
    118 			}
    119 		}
    120 		result.paths = android.PathsToDirectorySortedPaths(paths)
    121 		overlayData = append(overlayData, result)
    122 	}
    123 
    124 	ctx.Config().Once(overlayDataKey, func() interface{} {
    125 		return overlayData
    126 	})
    127 }
    128