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
This commit is contained in:
10
cc/cc.go
10
cc/cc.go
@@ -1202,6 +1202,7 @@ var _ ccObjectProvider = (*ccObject)(nil)
|
|||||||
type CCBinary struct {
|
type CCBinary struct {
|
||||||
CCLinked
|
CCLinked
|
||||||
out string
|
out string
|
||||||
|
installFile string
|
||||||
BinaryProperties struct {
|
BinaryProperties struct {
|
||||||
// static_executable: compile executable with -static
|
// static_executable: compile executable with -static
|
||||||
Static_executable bool
|
Static_executable bool
|
||||||
@@ -1349,7 +1350,14 @@ func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
|
func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
|
||||||
ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
|
c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CCBinary) HostToolPath() string {
|
||||||
|
if c.HostOrDevice().Host() {
|
||||||
|
return c.installFile
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
type CCTest struct {
|
type CCTest struct {
|
||||||
|
@@ -58,6 +58,7 @@ func main() {
|
|||||||
ctx.RegisterModuleType("cc_test_host", cc.CCTestHostFactory)
|
ctx.RegisterModuleType("cc_test_host", cc.CCTestHostFactory)
|
||||||
|
|
||||||
ctx.RegisterModuleType("gensrcs", genrule.GenSrcsFactory)
|
ctx.RegisterModuleType("gensrcs", genrule.GenSrcsFactory)
|
||||||
|
ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
|
||||||
|
|
||||||
ctx.RegisterModuleType("art_cc_library", art.ArtCCLibraryFactory)
|
ctx.RegisterModuleType("art_cc_library", art.ArtCCLibraryFactory)
|
||||||
ctx.RegisterModuleType("art_cc_binary", art.ArtCCBinaryFactory)
|
ctx.RegisterModuleType("art_cc_binary", art.ArtCCBinaryFactory)
|
||||||
|
@@ -29,19 +29,122 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
pctx.VariableConfigMethod("srcDir", common.Config.SrcDir)
|
pctx.VariableConfigMethod("srcDir", common.Config.SrcDir)
|
||||||
|
pctx.VariableConfigMethod("hostBin", common.Config.HostBin)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SourceFileGenerator interface {
|
type SourceFileGenerator interface {
|
||||||
GeneratedSourceFiles() []string
|
GeneratedSourceFiles() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type genSrcsProperties struct {
|
type HostToolProvider interface {
|
||||||
// cmd: command to run on each input file. Available variables for substitution:
|
HostToolPath() string
|
||||||
// $in: an input file
|
}
|
||||||
// $out: the corresponding output file
|
|
||||||
|
type generator struct {
|
||||||
|
common.AndroidModuleBase
|
||||||
|
|
||||||
|
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
|
// $srcDir: the root directory of the source tree
|
||||||
|
// The host bin directory will be in the path
|
||||||
Cmd string
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
type taskFunc func(ctx common.AndroidModuleContext) []generateTask
|
||||||
|
|
||||||
|
type generateTask struct {
|
||||||
|
in []string
|
||||||
|
out string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *generator) GeneratedSourceFiles() []string {
|
||||||
|
return g.outputFiles
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *generator) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
|
||||||
|
if g.properties.Tool != "" {
|
||||||
|
return []string{g.properties.Tool}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, task := range g.tasks(ctx) {
|
||||||
|
g.generateSourceFile(ctx, task)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *generator) generateSourceFile(ctx common.AndroidModuleContext, task generateTask) {
|
||||||
|
|
||||||
|
ctx.Build(pctx, blueprint.BuildParams{
|
||||||
|
Rule: g.rule,
|
||||||
|
Inputs: task.in,
|
||||||
|
Implicits: g.deps,
|
||||||
|
Outputs: []string{task.out},
|
||||||
|
})
|
||||||
|
|
||||||
|
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...)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
return generatorFactory(tasks, properties)
|
||||||
|
}
|
||||||
|
|
||||||
|
type genSrcsProperties struct {
|
||||||
// srcs: list of input files
|
// srcs: list of input files
|
||||||
Srcs []string
|
Srcs []string
|
||||||
|
|
||||||
@@ -49,43 +152,25 @@ type genSrcsProperties struct {
|
|||||||
Output_extension string
|
Output_extension string
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenSrcsFactory() (blueprint.Module, []interface{}) {
|
func GenRuleFactory() (blueprint.Module, []interface{}) {
|
||||||
module := &genSrcs{}
|
properties := &genRuleProperties{}
|
||||||
|
|
||||||
return common.InitAndroidModule(module, &module.properties)
|
tasks := func(ctx common.AndroidModuleContext) []generateTask {
|
||||||
}
|
return []generateTask{
|
||||||
|
{
|
||||||
type genSrcs struct {
|
in: common.ExpandSources(ctx, properties.Srcs),
|
||||||
common.AndroidModuleBase
|
out: filepath.Join(common.ModuleGenDir(ctx), properties.Out),
|
||||||
|
},
|
||||||
properties genSrcsProperties
|
|
||||||
outputFiles []string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *genSrcs) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
|
|
||||||
rule := ctx.Rule(pctx, "genSrcs", blueprint.RuleParams{
|
|
||||||
Command: g.properties.Cmd,
|
|
||||||
})
|
|
||||||
|
|
||||||
srcFiles := common.ExpandSources(ctx, g.properties.Srcs)
|
|
||||||
|
|
||||||
g.outputFiles = make([]string, 0, len(srcFiles))
|
|
||||||
|
|
||||||
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
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return generatorFactory(tasks, properties)
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ SourceFileGenerator = (*genSrcs)(nil)
|
type genRuleProperties struct {
|
||||||
|
// srcs: list of input files
|
||||||
|
Srcs []string
|
||||||
|
|
||||||
func (g *genSrcs) GeneratedSourceFiles() []string {
|
// out: name of the output file that will be generated
|
||||||
return g.outputFiles
|
Out string
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user