Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame^] | 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 genrule |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | |
| 20 | "blueprint" |
| 21 | "blueprint/pathtools" |
| 22 | |
| 23 | "android/soong/common" |
| 24 | ) |
| 25 | |
| 26 | type Config interface { |
| 27 | SrcDir() string |
| 28 | } |
| 29 | |
| 30 | var ( |
| 31 | pctx = blueprint.NewPackageContext("android/soong/genrule") |
| 32 | ) |
| 33 | |
| 34 | func init() { |
| 35 | pctx.VariableConfigMethod("srcDir", Config.SrcDir) |
| 36 | } |
| 37 | |
| 38 | type SourceFileGenerator interface { |
| 39 | GeneratedSourceFiles() []string |
| 40 | } |
| 41 | |
| 42 | type genSrcsProperties struct { |
| 43 | // cmd: command to run on each input file. Available variables for substitution: |
| 44 | // $in: an input file |
| 45 | // $out: the corresponding output file |
| 46 | // $srcDir: the root directory of the source tree |
| 47 | Cmd string |
| 48 | |
| 49 | // srcs: list of input files |
| 50 | Srcs []string |
| 51 | |
| 52 | // output_extension: extension that will be substituted for each output file |
| 53 | Output_extension string |
| 54 | } |
| 55 | |
| 56 | func NewGenSrcs() (blueprint.Module, []interface{}) { |
| 57 | module := &genSrcs{} |
| 58 | |
| 59 | return common.InitAndroidModule(module, &module.properties) |
| 60 | } |
| 61 | |
| 62 | type genSrcs struct { |
| 63 | common.AndroidModuleBase |
| 64 | |
| 65 | properties genSrcsProperties |
| 66 | outputFiles []string |
| 67 | } |
| 68 | |
| 69 | func (g *genSrcs) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
| 70 | rule := ctx.Rule(pctx, "genSrcs", blueprint.RuleParams{ |
| 71 | Command: g.properties.Cmd, |
| 72 | }) |
| 73 | |
| 74 | srcFiles := g.properties.Srcs |
| 75 | srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx)) |
| 76 | srcFiles = common.ExpandGlobs(ctx, srcFiles) |
| 77 | |
| 78 | g.outputFiles = make([]string, 0, len(srcFiles)) |
| 79 | |
| 80 | for _, in := range srcFiles { |
| 81 | out := pathtools.ReplaceExtension(in, g.properties.Output_extension) |
| 82 | out = filepath.Join(common.ModuleGenDir(ctx), out) |
| 83 | g.outputFiles = append(g.outputFiles, out) |
| 84 | ctx.Build(pctx, blueprint.BuildParams{ |
| 85 | Rule: rule, |
| 86 | Inputs: []string{in}, |
| 87 | Outputs: []string{out}, |
| 88 | // TODO: visit dependencies to add implicit dependencies on required tools |
| 89 | }) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | var _ SourceFileGenerator = (*genSrcs)(nil) |
| 94 | |
| 95 | func (g *genSrcs) GeneratedSourceFiles() []string { |
| 96 | return g.outputFiles |
| 97 | } |