Home | History | Annotate | Download | only in genrule
      1 // Copyright 2016 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 genrule
     16 
     17 import (
     18 	"android/soong/android"
     19 	"io"
     20 	"strings"
     21 	"text/template"
     22 )
     23 
     24 func init() {
     25 	android.RegisterModuleType("filegroup", FileGroupFactory)
     26 }
     27 
     28 type fileGroupProperties struct {
     29 	// srcs lists files that will be included in this filegroup
     30 	Srcs []string
     31 
     32 	Exclude_srcs []string
     33 
     34 	// The base path to the files.  May be used by other modules to determine which portion
     35 	// of the path to use.  For example, when a filegroup is used as data in a cc_test rule,
     36 	// the base path is stripped off the path and the remaining path is used as the
     37 	// installation directory.
     38 	Path *string
     39 
     40 	// Create a make variable with the specified name that contains the list of files in the
     41 	// filegroup, relative to the root of the source tree.
     42 	Export_to_make_var *string
     43 }
     44 
     45 type fileGroup struct {
     46 	android.ModuleBase
     47 	properties fileGroupProperties
     48 	srcs       android.Paths
     49 }
     50 
     51 var _ android.SourceFileProducer = (*fileGroup)(nil)
     52 
     53 // filegroup modules contain a list of files, and can be used to export files across package
     54 // boundaries.  filegroups (and genrules) can be referenced from srcs properties of other modules
     55 // using the syntax ":module".
     56 func FileGroupFactory() android.Module {
     57 	module := &fileGroup{}
     58 	module.AddProperties(&module.properties)
     59 	android.InitAndroidModule(module)
     60 	return module
     61 }
     62 
     63 func (fg *fileGroup) DepsMutator(ctx android.BottomUpMutatorContext) {
     64 	android.ExtractSourcesDeps(ctx, fg.properties.Srcs)
     65 	android.ExtractSourcesDeps(ctx, fg.properties.Exclude_srcs)
     66 }
     67 
     68 func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
     69 	fg.srcs = ctx.ExpandSourcesSubDir(fg.properties.Srcs, fg.properties.Exclude_srcs, String(fg.properties.Path))
     70 }
     71 
     72 func (fg *fileGroup) Srcs() android.Paths {
     73 	return append(android.Paths{}, fg.srcs...)
     74 }
     75 
     76 var androidMkTemplate = template.Must(template.New("filegroup").Parse(`
     77 ifdef {{.makeVar}}
     78   $(error variable {{.makeVar}} set by soong module is already set in make)
     79 endif
     80 {{.makeVar}} := {{.value}}
     81 .KATI_READONLY := {{.makeVar}}
     82 `))
     83 
     84 func (fg *fileGroup) AndroidMk() android.AndroidMkData {
     85 	return android.AndroidMkData{
     86 		Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
     87 			if makeVar := String(fg.properties.Export_to_make_var); makeVar != "" {
     88 				androidMkTemplate.Execute(w, map[string]string{
     89 					"makeVar": makeVar,
     90 					"value":   strings.Join(fg.srcs.Strings(), " "),
     91 				})
     92 			}
     93 		},
     94 	}
     95 }
     96