diff options
author | Colin Cross <ccross@android.com> | 2015-04-28 13:25:36 -0700 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2015-04-29 14:58:16 -0700 |
commit | d350ecd1023ca0352c71d866b3ab4e5de62e8506 (patch) | |
tree | f3a278fc423a4573955b62811443fefc9990c52f /genrule | |
parent | c3144b155353000907773d058f6498de4cccb6f9 (diff) | |
download | build_soong-d350ecd1023ca0352c71d866b3ab4e5de62e8506.tar.gz build_soong-d350ecd1023ca0352c71d866b3ab4e5de62e8506.tar.bz2 build_soong-d350ecd1023ca0352c71d866b3ab4e5de62e8506.zip |
Add support for genrule
Add genrule, which uses a host executable (possibly built by the
build system) to generate a single source file. This differs slightly
from gensrcs, which takes a list of sources and translates them through
a host executable to individual source files.
Change-Id: I94bda62c4c53fb3f3817def190e6a7561508d297
Diffstat (limited to 'genrule')
-rw-r--r-- | genrule/genrule.go | 155 |
1 files changed, 120 insertions, 35 deletions
diff --git a/genrule/genrule.go b/genrule/genrule.go index 1361631a..0f276d06 100644 --- a/genrule/genrule.go +++ b/genrule/genrule.go @@ -29,63 +29,148 @@ var ( func init() { pctx.VariableConfigMethod("srcDir", common.Config.SrcDir) + pctx.VariableConfigMethod("hostBin", common.Config.HostBin) } type SourceFileGenerator interface { GeneratedSourceFiles() []string } -type genSrcsProperties struct { - // cmd: command to run on each input file. Available variables for substitution: - // $in: an input file - // $out: the corresponding output file - // $srcDir: the root directory of the source tree - Cmd string +type HostToolProvider interface { + HostToolPath() string +} - // srcs: list of input files - Srcs []string +type generator struct { + common.AndroidModuleBase - // output_extension: extension that will be substituted for each output file - Output_extension string + properties struct { + // cmd: command to run on one or more input files. Available variables for substitution: + // $in: one or more input files + // $out: a single output file + // $srcDir: the root directory of the source tree + // The host bin directory will be in the path + Cmd string + + // tool: name of the module (if any) that produces the host executable. Leave empty for + // prebuilts or scripts that do not need a module to build them. + Tool string + } + + tasks taskFunc + + deps []string + rule blueprint.Rule + + outputFiles []string } -func GenSrcsFactory() (blueprint.Module, []interface{}) { - module := &genSrcs{} +type taskFunc func(ctx common.AndroidModuleContext) []generateTask - return common.InitAndroidModule(module, &module.properties) +type generateTask struct { + in []string + out string } -type genSrcs struct { - common.AndroidModuleBase +func (g *generator) GeneratedSourceFiles() []string { + return g.outputFiles +} - properties genSrcsProperties - outputFiles []string +func (g *generator) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { + if g.properties.Tool != "" { + return []string{g.properties.Tool} + } + return nil } -func (g *genSrcs) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { - rule := ctx.Rule(pctx, "genSrcs", blueprint.RuleParams{ - Command: g.properties.Cmd, +func (g *generator) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { + g.rule = ctx.Rule(pctx, "generator", blueprint.RuleParams{ + Command: "PATH=$$PATH:$hostBin " + g.properties.Cmd, + }) + + ctx.VisitDirectDeps(func(module blueprint.Module) { + if t, ok := module.(HostToolProvider); ok { + p := t.HostToolPath() + if p != "" { + g.deps = append(g.deps, p) + } else { + ctx.ModuleErrorf("host tool %q missing output file", ctx.OtherModuleName(module)) + } + } else { + ctx.ModuleErrorf("unknown dependency %q", ctx.OtherModuleName(module)) + } }) - srcFiles := common.ExpandSources(ctx, g.properties.Srcs) + for _, task := range g.tasks(ctx) { + g.generateSourceFile(ctx, task) + } +} + +func (g *generator) generateSourceFile(ctx common.AndroidModuleContext, task generateTask) { - g.outputFiles = make([]string, 0, len(srcFiles)) + ctx.Build(pctx, blueprint.BuildParams{ + Rule: g.rule, + Inputs: task.in, + Implicits: g.deps, + Outputs: []string{task.out}, + }) - for _, in := range srcFiles { - out := pathtools.ReplaceExtension(in, g.properties.Output_extension) - out = filepath.Join(common.ModuleGenDir(ctx), out) - g.outputFiles = append(g.outputFiles, out) - ctx.Build(pctx, blueprint.BuildParams{ - Rule: rule, - Inputs: []string{in}, - Outputs: []string{out}, - // TODO: visit dependencies to add implicit dependencies on required tools - }) + g.outputFiles = append(g.outputFiles, task.out) +} + +func generatorFactory(tasks taskFunc, props ...interface{}) (blueprint.Module, []interface{}) { + module := &generator{ + tasks: tasks, } + + props = append(props, &module.properties) + + return common.InitAndroidModule(module, props...) } -var _ SourceFileGenerator = (*genSrcs)(nil) +func GenSrcsFactory() (blueprint.Module, []interface{}) { + properties := &genSrcsProperties{} + + tasks := func(ctx common.AndroidModuleContext) []generateTask { + srcFiles := common.ExpandSources(ctx, properties.Srcs) + tasks := make([]generateTask, 0, len(srcFiles)) + for _, in := range srcFiles { + out := pathtools.ReplaceExtension(in, properties.Output_extension) + out = filepath.Join(common.ModuleGenDir(ctx), out) + tasks = append(tasks, generateTask{[]string{in}, out}) + } + return tasks + } -func (g *genSrcs) GeneratedSourceFiles() []string { - return g.outputFiles + return generatorFactory(tasks, properties) +} + +type genSrcsProperties struct { + // srcs: list of input files + Srcs []string + + // output_extension: extension that will be substituted for each output file + Output_extension string +} + +func GenRuleFactory() (blueprint.Module, []interface{}) { + properties := &genRuleProperties{} + + tasks := func(ctx common.AndroidModuleContext) []generateTask { + return []generateTask{ + { + in: common.ExpandSources(ctx, properties.Srcs), + out: filepath.Join(common.ModuleGenDir(ctx), properties.Out), + }, + } + } + + return generatorFactory(tasks, properties) +} + +type genRuleProperties struct { + // srcs: list of input files + Srcs []string + + // out: name of the output file that will be generated + Out string } |