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