blob: 589758d8ba03ede4a1e8366371e6c6eb0c252e8d [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
70 srcFiles := g.properties.Srcs
71 srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx))
72 srcFiles = common.ExpandGlobs(ctx, srcFiles)
73
74 g.outputFiles = make([]string, 0, len(srcFiles))
75
76 for _, in := range srcFiles {
77 out := pathtools.ReplaceExtension(in, g.properties.Output_extension)
78 out = filepath.Join(common.ModuleGenDir(ctx), out)
79 g.outputFiles = append(g.outputFiles, out)
80 ctx.Build(pctx, blueprint.BuildParams{
81 Rule: rule,
82 Inputs: []string{in},
83 Outputs: []string{out},
84 // TODO: visit dependencies to add implicit dependencies on required tools
85 })
86 }
87}
88
89var _ SourceFileGenerator = (*genSrcs)(nil)
90
91func (g *genSrcs) GeneratedSourceFiles() []string {
92 return g.outputFiles
93}