blob: 1361631a15cfa540954da6ba1ae601c50e49efc4 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// 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
15package genrule
16
17import (
18 "path/filepath"
19
Colin Cross70b40592015-03-23 12:57:34 -070020 "github.com/google/blueprint"
21 "github.com/google/blueprint/pathtools"
Colin Cross5049f022015-03-18 13:28:46 -070022
23 "android/soong/common"
24)
25
Colin Cross5049f022015-03-18 13:28:46 -070026var (
27 pctx = blueprint.NewPackageContext("android/soong/genrule")
28)
29
30func init() {
Colin Cross1332b002015-04-07 17:11:30 -070031 pctx.VariableConfigMethod("srcDir", common.Config.SrcDir)
Colin Cross5049f022015-03-18 13:28:46 -070032}
33
34type SourceFileGenerator interface {
35 GeneratedSourceFiles() []string
36}
37
38type 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 Cross97ba0732015-03-23 17:50:24 -070052func GenSrcsFactory() (blueprint.Module, []interface{}) {
Colin Cross5049f022015-03-18 13:28:46 -070053 module := &genSrcs{}
54
55 return common.InitAndroidModule(module, &module.properties)
56}
57
58type genSrcs struct {
59 common.AndroidModuleBase
60
61 properties genSrcsProperties
62 outputFiles []string
63}
64
65func (g *genSrcs) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
66 rule := ctx.Rule(pctx, "genSrcs", blueprint.RuleParams{
67 Command: g.properties.Cmd,
68 })
69
Colin Crossfce53272015-04-08 11:21:40 -070070 srcFiles := common.ExpandSources(ctx, g.properties.Srcs)
Colin Cross5049f022015-03-18 13:28:46 -070071
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
87var _ SourceFileGenerator = (*genSrcs)(nil)
88
89func (g *genSrcs) GeneratedSourceFiles() []string {
90 return g.outputFiles
91}