2015-03-19 04:28:46 +08:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package genrule
|
|
|
|
|
|
|
|
import (
|
2016-11-05 06:32:58 +08:00
|
|
|
"fmt"
|
2019-03-19 13:15:32 +08:00
|
|
|
"io"
|
2019-09-24 06:55:30 +08:00
|
|
|
"strconv"
|
2016-11-05 06:32:58 +08:00
|
|
|
"strings"
|
2016-09-29 07:19:10 +08:00
|
|
|
|
2015-03-24 03:57:34 +08:00
|
|
|
"github.com/google/blueprint"
|
2017-09-14 07:07:44 +08:00
|
|
|
"github.com/google/blueprint/bootstrap"
|
2017-11-09 13:20:04 +08:00
|
|
|
"github.com/google/blueprint/proptools"
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
"android/soong/android"
|
2017-03-30 08:29:06 +08:00
|
|
|
"android/soong/shared"
|
|
|
|
"path/filepath"
|
2015-03-19 04:28:46 +08:00
|
|
|
)
|
|
|
|
|
2015-06-18 05:20:06 +08:00
|
|
|
func init() {
|
2018-12-11 00:13:18 +08:00
|
|
|
android.RegisterModuleType("genrule_defaults", defaultsFactory)
|
|
|
|
|
2017-10-10 06:34:10 +08:00
|
|
|
android.RegisterModuleType("gensrcs", GenSrcsFactory)
|
|
|
|
android.RegisterModuleType("genrule", GenRuleFactory)
|
2015-06-18 05:20:06 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 04:28:46 +08:00
|
|
|
var (
|
2016-05-19 06:37:25 +08:00
|
|
|
pctx = android.NewPackageContext("android/soong/genrule")
|
2019-09-24 06:55:30 +08:00
|
|
|
|
|
|
|
gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{
|
|
|
|
Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}",
|
|
|
|
CommandDeps: []string{"${soongZip}", "${zipSync}"},
|
|
|
|
Rspfile: "${tmpZip}.rsp",
|
|
|
|
RspfileContent: "${zipArgs}",
|
|
|
|
}, "tmpZip", "genDir", "zipArgs")
|
2015-03-19 04:28:46 +08:00
|
|
|
)
|
|
|
|
|
2017-03-30 08:29:06 +08:00
|
|
|
func init() {
|
Expose all deps on genrules
For remote builds, we actually need to know that we depend on all
outputs from a genrule, not just the first one. But having every user
depend on every genrule output increases the ninja file size by >40%.
So to work around the increase in file size, use a ninja phony rule to
produce a single alias to all of the files. Unlike make, where phony
rules are always dirty, ninja does not attempt to stat phony rules as
long as they have inputs, so they act as an alias. (If they don't have
inputs, it will stat the file an only consider it dirty if it doesn't
exist.)
My remote build tooling can then see that it's a phony rule with inputs,
and collapse all of these inputs onto the actual rule.
This only applies to genrules that have >6 outputs, in order to keep the
graph simpler and easier to read for the common case. That's ~10% of the
genrules in AOSP, and it increases the ninja file size by ~1.3%.
Test: manual ninja file inspection
Test: treehugger
Change-Id: I1a26a02fe983f0c92ce726ada3133707223e662e
2019-08-10 07:21:29 +08:00
|
|
|
pctx.Import("android/soong/android")
|
2017-03-30 08:29:06 +08:00
|
|
|
pctx.HostBinToolVariable("sboxCmd", "sbox")
|
2019-09-24 06:55:30 +08:00
|
|
|
|
|
|
|
pctx.HostBinToolVariable("soongZip", "soong_zip")
|
|
|
|
pctx.HostBinToolVariable("zipSync", "zipsync")
|
2017-03-30 08:29:06 +08:00
|
|
|
}
|
|
|
|
|
2015-03-19 04:28:46 +08:00
|
|
|
type SourceFileGenerator interface {
|
2016-05-19 06:37:25 +08:00
|
|
|
GeneratedSourceFiles() android.Paths
|
2016-11-23 04:55:55 +08:00
|
|
|
GeneratedHeaderDirs() android.Paths
|
2018-02-22 10:28:18 +08:00
|
|
|
GeneratedDeps() android.Paths
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
|
|
|
|
2019-03-29 10:30:56 +08:00
|
|
|
// Alias for android.HostToolProvider
|
|
|
|
// Deprecated: use android.HostToolProvider instead.
|
2015-04-29 04:25:36 +08:00
|
|
|
type HostToolProvider interface {
|
2019-03-29 10:30:56 +08:00
|
|
|
android.HostToolProvider
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2017-09-14 06:46:47 +08:00
|
|
|
type hostToolDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
2018-10-05 14:29:14 +08:00
|
|
|
label string
|
2017-09-14 06:46:47 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 04:39:40 +08:00
|
|
|
type generatorProperties struct {
|
2017-03-30 08:29:06 +08:00
|
|
|
// The command to run on one or more input files. Cmd supports substitution of a few variables
|
|
|
|
// (the actual substitution is implemented in GenerateAndroidBuildActions below)
|
|
|
|
//
|
|
|
|
// Available variables for substitution:
|
|
|
|
//
|
2017-10-18 12:38:14 +08:00
|
|
|
// $(location): the path to the first entry in tools or tool_files
|
2018-10-05 14:29:14 +08:00
|
|
|
// $(location <label>): the path to the tool, tool_file, input or output with name <label>
|
2017-10-18 12:38:14 +08:00
|
|
|
// $(in): one or more input files
|
|
|
|
// $(out): a single output file
|
|
|
|
// $(depfile): a file to which dependencies will be written, if the depfile property is set to true
|
|
|
|
// $(genDir): the sandbox directory for this tool; contains $(out)
|
|
|
|
// $$: a literal $
|
2016-11-05 06:32:58 +08:00
|
|
|
//
|
2017-03-30 08:29:06 +08:00
|
|
|
// All files used must be declared as inputs (to ensure proper up-to-date checks).
|
|
|
|
// Use "$(in)" directly in Cmd to ensure that all inputs used are declared.
|
2017-11-09 13:20:04 +08:00
|
|
|
Cmd *string
|
2015-05-12 04:39:40 +08:00
|
|
|
|
2016-11-22 09:23:08 +08:00
|
|
|
// Enable reading a file containing dependencies in gcc format after the command completes
|
2017-11-09 13:20:04 +08:00
|
|
|
Depfile *bool
|
2016-11-22 09:23:08 +08:00
|
|
|
|
2016-11-05 06:32:58 +08:00
|
|
|
// name of the modules (if any) that produces the host executable. Leave empty for
|
2015-05-12 04:39:40 +08:00
|
|
|
// prebuilts or scripts that do not need a module to build them.
|
2016-11-05 06:32:58 +08:00
|
|
|
Tools []string
|
2016-04-21 05:54:32 +08:00
|
|
|
|
|
|
|
// Local file that is used as the tool
|
2019-03-05 14:35:41 +08:00
|
|
|
Tool_files []string `android:"path"`
|
2016-11-23 04:55:55 +08:00
|
|
|
|
|
|
|
// List of directories to export generated headers from
|
|
|
|
Export_include_dirs []string
|
2017-01-14 10:05:49 +08:00
|
|
|
|
|
|
|
// list of input files
|
2019-03-05 14:35:41 +08:00
|
|
|
Srcs []string `android:"path,arch_variant"`
|
2018-11-18 06:01:18 +08:00
|
|
|
|
|
|
|
// input files to exclude
|
2019-03-05 14:35:41 +08:00
|
|
|
Exclude_srcs []string `android:"path,arch_variant"`
|
2015-05-12 04:39:40 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
type Module struct {
|
2016-05-19 06:37:25 +08:00
|
|
|
android.ModuleBase
|
2018-12-11 00:13:18 +08:00
|
|
|
android.DefaultableModuleBase
|
2019-06-12 12:27:29 +08:00
|
|
|
android.ApexModuleBase
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
// For other packages to make their own genrules with extra
|
|
|
|
// properties
|
|
|
|
Extra interface{}
|
2019-11-19 08:00:16 +08:00
|
|
|
android.ImageInterface
|
2017-09-14 09:37:08 +08:00
|
|
|
|
2015-05-12 04:39:40 +08:00
|
|
|
properties generatorProperties
|
2015-04-29 04:25:36 +08:00
|
|
|
|
2017-11-09 04:38:00 +08:00
|
|
|
taskGenerator taskFunc
|
2015-04-29 04:25:36 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
deps android.Paths
|
|
|
|
rule blueprint.Rule
|
|
|
|
rawCommands []string
|
2015-04-29 04:25:36 +08:00
|
|
|
|
2016-11-23 04:55:55 +08:00
|
|
|
exportedIncludeDirs android.Paths
|
2016-04-21 05:21:14 +08:00
|
|
|
|
2016-05-19 06:37:25 +08:00
|
|
|
outputFiles android.Paths
|
2018-02-22 10:28:18 +08:00
|
|
|
outputDeps android.Paths
|
2019-03-19 13:15:32 +08:00
|
|
|
|
|
|
|
subName string
|
2019-09-24 06:55:30 +08:00
|
|
|
subDir string
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2015-04-29 04:25:36 +08:00
|
|
|
type generateTask struct {
|
2018-02-22 06:07:48 +08:00
|
|
|
in android.Paths
|
|
|
|
out android.WritablePaths
|
2019-09-24 06:55:30 +08:00
|
|
|
copyTo android.WritablePaths
|
|
|
|
genDir android.WritablePath
|
2018-02-22 06:07:48 +08:00
|
|
|
sandboxOuts []string
|
|
|
|
cmd string
|
2019-09-24 06:55:30 +08:00
|
|
|
shard int
|
|
|
|
shards int
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func (g *Module) GeneratedSourceFiles() android.Paths {
|
2015-04-29 04:25:36 +08:00
|
|
|
return g.outputFiles
|
|
|
|
}
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func (g *Module) Srcs() android.Paths {
|
2018-03-28 07:19:42 +08:00
|
|
|
return append(android.Paths{}, g.outputFiles...)
|
2016-12-14 07:23:47 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func (g *Module) GeneratedHeaderDirs() android.Paths {
|
2016-11-23 04:55:55 +08:00
|
|
|
return g.exportedIncludeDirs
|
2016-04-21 05:21:14 +08:00
|
|
|
}
|
|
|
|
|
2018-02-22 10:28:18 +08:00
|
|
|
func (g *Module) GeneratedDeps() android.Paths {
|
|
|
|
return g.outputDeps
|
|
|
|
}
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
|
|
|
|
if g, ok := ctx.Module().(*Module); ok {
|
2018-10-05 14:29:14 +08:00
|
|
|
for _, tool := range g.properties.Tools {
|
|
|
|
tag := hostToolDependencyTag{label: tool}
|
|
|
|
if m := android.SrcIsModule(tool); m != "" {
|
|
|
|
tool = m
|
|
|
|
}
|
2019-10-17 02:03:10 +08:00
|
|
|
ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool)
|
2015-10-30 06:25:03 +08:00
|
|
|
}
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2019-03-19 13:15:32 +08:00
|
|
|
g.subName = ctx.ModuleSubDir()
|
|
|
|
|
2016-11-23 04:55:55 +08:00
|
|
|
if len(g.properties.Export_include_dirs) > 0 {
|
|
|
|
for _, dir := range g.properties.Export_include_dirs {
|
|
|
|
g.exportedIncludeDirs = append(g.exportedIncludeDirs,
|
2019-09-24 06:55:30 +08:00
|
|
|
android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir))
|
2016-11-23 04:55:55 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-24 06:55:30 +08:00
|
|
|
g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir))
|
2016-11-23 04:55:55 +08:00
|
|
|
}
|
2016-09-29 07:19:10 +08:00
|
|
|
|
2018-10-05 14:29:14 +08:00
|
|
|
locationLabels := map[string][]string{}
|
|
|
|
firstLabel := ""
|
|
|
|
|
|
|
|
addLocationLabel := func(label string, paths []string) {
|
|
|
|
if firstLabel == "" {
|
|
|
|
firstLabel = label
|
|
|
|
}
|
|
|
|
if _, exists := locationLabels[label]; !exists {
|
|
|
|
locationLabels[label] = paths
|
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("multiple labels for %q, %q and %q",
|
|
|
|
label, strings.Join(locationLabels[label], " "), strings.Join(paths, " "))
|
|
|
|
}
|
|
|
|
}
|
2016-09-29 07:19:10 +08:00
|
|
|
|
2016-11-05 06:32:58 +08:00
|
|
|
if len(g.properties.Tools) > 0 {
|
2019-03-19 03:12:48 +08:00
|
|
|
seenTools := make(map[string]bool)
|
|
|
|
|
2017-11-16 16:11:20 +08:00
|
|
|
ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
|
2018-10-05 14:29:14 +08:00
|
|
|
switch tag := ctx.OtherModuleDependencyTag(module).(type) {
|
|
|
|
case hostToolDependencyTag:
|
2017-09-14 06:46:47 +08:00
|
|
|
tool := ctx.OtherModuleName(module)
|
2017-09-14 07:07:44 +08:00
|
|
|
var path android.OptionalPath
|
2017-09-14 06:46:47 +08:00
|
|
|
|
2019-03-29 10:30:56 +08:00
|
|
|
if t, ok := module.(android.HostToolProvider); ok {
|
2017-11-16 16:11:20 +08:00
|
|
|
if !t.(android.Module).Enabled() {
|
2017-11-29 16:27:14 +08:00
|
|
|
if ctx.Config().AllowMissingDependencies() {
|
2017-11-16 16:11:20 +08:00
|
|
|
ctx.AddMissingDependencies([]string{tool})
|
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("depends on disabled module %q", tool)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2017-09-14 07:07:44 +08:00
|
|
|
path = t.HostToolPath()
|
|
|
|
} else if t, ok := module.(bootstrap.GoBinaryTool); ok {
|
|
|
|
if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil {
|
|
|
|
path = android.OptionalPathForPath(android.PathForOutput(ctx, s))
|
2016-11-05 06:32:58 +08:00
|
|
|
} else {
|
2017-09-14 07:07:44 +08:00
|
|
|
ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
|
|
|
|
break
|
2016-11-05 06:32:58 +08:00
|
|
|
}
|
2016-04-21 05:54:32 +08:00
|
|
|
} else {
|
2017-09-14 06:46:47 +08:00
|
|
|
ctx.ModuleErrorf("%q is not a host tool provider", tool)
|
2017-09-14 07:07:44 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.Valid() {
|
|
|
|
g.deps = append(g.deps, path.Path())
|
2018-10-05 14:29:14 +08:00
|
|
|
addLocationLabel(tag.label, []string{path.Path().String()})
|
2019-03-19 03:12:48 +08:00
|
|
|
seenTools[tag.label] = true
|
2017-09-14 07:07:44 +08:00
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("host tool %q missing output file", tool)
|
2016-04-21 05:54:32 +08:00
|
|
|
}
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
2016-04-21 05:54:32 +08:00
|
|
|
})
|
2019-03-19 03:12:48 +08:00
|
|
|
|
|
|
|
// If AllowMissingDependencies is enabled, the build will not have stopped when
|
|
|
|
// AddFarVariationDependencies was called on a missing tool, which will result in nonsensical
|
|
|
|
// "cmd: unknown location label ..." errors later. Add a dummy file to the local label. The
|
|
|
|
// command that uses this dummy file will never be executed because the rule will be replaced with
|
|
|
|
// an android.Error rule reporting the missing dependencies.
|
|
|
|
if ctx.Config().AllowMissingDependencies() {
|
|
|
|
for _, tool := range g.properties.Tools {
|
|
|
|
if !seenTools[tool] {
|
|
|
|
addLocationLabel(tool, []string{"***missing tool " + tool + "***"})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 05:54:32 +08:00
|
|
|
}
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2017-09-14 06:46:47 +08:00
|
|
|
if ctx.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:29:14 +08:00
|
|
|
for _, toolFile := range g.properties.Tool_files {
|
2019-03-06 14:25:09 +08:00
|
|
|
paths := android.PathsForModuleSrc(ctx, []string{toolFile})
|
2018-10-05 14:29:14 +08:00
|
|
|
g.deps = append(g.deps, paths...)
|
|
|
|
addLocationLabel(toolFile, paths.Strings())
|
2016-11-05 06:32:58 +08:00
|
|
|
}
|
|
|
|
|
2018-10-05 14:29:14 +08:00
|
|
|
var srcFiles android.Paths
|
|
|
|
for _, in := range g.properties.Srcs {
|
2019-03-19 03:12:48 +08:00
|
|
|
paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs)
|
|
|
|
if len(missingDeps) > 0 {
|
|
|
|
if !ctx.Config().AllowMissingDependencies() {
|
|
|
|
panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator",
|
|
|
|
missingDeps))
|
|
|
|
}
|
|
|
|
|
|
|
|
// If AllowMissingDependencies is enabled, the build will not have stopped when
|
|
|
|
// the dependency was added on a missing SourceFileProducer module, which will result in nonsensical
|
|
|
|
// "cmd: label ":..." has no files" errors later. Add a dummy file to the local label. The
|
|
|
|
// command that uses this dummy file will never be executed because the rule will be replaced with
|
|
|
|
// an android.Error rule reporting the missing dependencies.
|
|
|
|
ctx.AddMissingDependencies(missingDeps)
|
|
|
|
addLocationLabel(in, []string{"***missing srcs " + in + "***"})
|
|
|
|
} else {
|
|
|
|
srcFiles = append(srcFiles, paths...)
|
|
|
|
addLocationLabel(in, paths.Strings())
|
|
|
|
}
|
2018-10-05 14:29:14 +08:00
|
|
|
}
|
2017-10-28 05:59:27 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
var copyFrom android.Paths
|
|
|
|
var outputFiles android.WritablePaths
|
|
|
|
var zipArgs strings.Builder
|
2018-10-05 14:29:14 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) {
|
|
|
|
for _, out := range task.out {
|
|
|
|
addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
|
2018-07-10 00:45:06 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
referencedDepfile := false
|
|
|
|
|
|
|
|
rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
|
|
|
|
// report the error directly without returning an error to android.Expand to catch multiple errors in a
|
|
|
|
// single run
|
|
|
|
reportError := func(fmt string, args ...interface{}) (string, bool, error) {
|
|
|
|
ctx.PropertyErrorf("cmd", fmt, args...)
|
|
|
|
return "SOONG_ERROR", false, nil
|
2016-11-22 09:23:08 +08:00
|
|
|
}
|
2019-09-24 06:55:30 +08:00
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "location":
|
|
|
|
if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
|
|
|
|
return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
|
|
|
|
}
|
|
|
|
paths := locationLabels[firstLabel]
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return reportError("default label %q has no files", firstLabel)
|
|
|
|
} else if len(paths) > 1 {
|
|
|
|
return reportError("default label %q has multiple files, use $(locations %s) to reference it",
|
|
|
|
firstLabel, firstLabel)
|
|
|
|
}
|
|
|
|
return locationLabels[firstLabel][0], false, nil
|
|
|
|
case "in":
|
|
|
|
return "${in}", true, nil
|
|
|
|
case "out":
|
|
|
|
return "__SBOX_OUT_FILES__", false, nil
|
|
|
|
case "depfile":
|
|
|
|
referencedDepfile = true
|
|
|
|
if !Bool(g.properties.Depfile) {
|
|
|
|
return reportError("$(depfile) used without depfile property")
|
2016-11-05 06:32:58 +08:00
|
|
|
}
|
2019-09-24 06:55:30 +08:00
|
|
|
return "__SBOX_DEPFILE__", false, nil
|
|
|
|
case "genDir":
|
|
|
|
return "__SBOX_OUT_DIR__", false, nil
|
|
|
|
default:
|
|
|
|
if strings.HasPrefix(name, "location ") {
|
|
|
|
label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
|
|
|
|
if paths, ok := locationLabels[label]; ok {
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return reportError("label %q has no files", label)
|
|
|
|
} else if len(paths) > 1 {
|
|
|
|
return reportError("label %q has multiple files, use $(locations %s) to reference it",
|
|
|
|
label, label)
|
|
|
|
}
|
|
|
|
return paths[0], false, nil
|
|
|
|
} else {
|
|
|
|
return reportError("unknown location label %q", label)
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(name, "locations ") {
|
|
|
|
label := strings.TrimSpace(strings.TrimPrefix(name, "locations "))
|
|
|
|
if paths, ok := locationLabels[label]; ok {
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return reportError("label %q has no files", label)
|
|
|
|
}
|
|
|
|
return strings.Join(paths, " "), false, nil
|
|
|
|
} else {
|
|
|
|
return reportError("unknown locations label %q", label)
|
2018-10-05 14:29:14 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-09-24 06:55:30 +08:00
|
|
|
return reportError("unknown variable '$(%s)'", name)
|
2018-10-05 14:29:14 +08:00
|
|
|
}
|
2016-11-05 06:32:58 +08:00
|
|
|
}
|
2019-09-24 06:55:30 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("cmd", "%s", err.Error())
|
|
|
|
return
|
2016-11-05 06:32:58 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
if Bool(g.properties.Depfile) && !referencedDepfile {
|
|
|
|
ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
|
|
|
|
return
|
|
|
|
}
|
2016-11-05 06:32:58 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
// tell the sbox command which directory to use as its sandbox root
|
|
|
|
buildDir := android.PathForOutput(ctx).String()
|
|
|
|
sandboxPath := shared.TempDirForOutDir(buildDir)
|
2018-07-10 00:45:06 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
// recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
|
|
|
|
// to be replaced later by ninja_strings.go
|
|
|
|
depfilePlaceholder := ""
|
|
|
|
if Bool(g.properties.Depfile) {
|
|
|
|
depfilePlaceholder = "$depfileArgs"
|
|
|
|
}
|
2017-03-30 08:29:06 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
// Escape the command for the shell
|
|
|
|
rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
|
|
|
|
g.rawCommands = append(g.rawCommands, rawCommand)
|
|
|
|
sandboxCommand := fmt.Sprintf("rm -rf %s && $sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
|
|
|
|
task.genDir, sandboxPath, task.genDir, rawCommand, depfilePlaceholder)
|
|
|
|
|
|
|
|
ruleParams := blueprint.RuleParams{
|
|
|
|
Command: sandboxCommand,
|
|
|
|
CommandDeps: []string{"$sboxCmd"},
|
|
|
|
}
|
|
|
|
args := []string{"allouts"}
|
|
|
|
if Bool(g.properties.Depfile) {
|
|
|
|
ruleParams.Deps = blueprint.DepsGCC
|
|
|
|
args = append(args, "depfileArgs")
|
|
|
|
}
|
|
|
|
name := "generator"
|
|
|
|
if task.shards > 1 {
|
|
|
|
name += strconv.Itoa(task.shard)
|
|
|
|
}
|
|
|
|
rule := ctx.Rule(pctx, name, ruleParams, args...)
|
2017-11-07 06:15:16 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
g.generateSourceFile(ctx, task, rule)
|
2017-03-30 08:29:06 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
if len(task.copyTo) > 0 {
|
|
|
|
outputFiles = append(outputFiles, task.copyTo...)
|
|
|
|
copyFrom = append(copyFrom, task.out.Paths()...)
|
|
|
|
zipArgs.WriteString(" -C " + task.genDir.String())
|
|
|
|
zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f "))
|
|
|
|
} else {
|
|
|
|
outputFiles = append(outputFiles, task.out...)
|
|
|
|
}
|
2016-11-22 09:23:08 +08:00
|
|
|
}
|
2019-09-24 06:55:30 +08:00
|
|
|
|
|
|
|
if len(copyFrom) > 0 {
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: gensrcsMerge,
|
|
|
|
Implicits: copyFrom,
|
|
|
|
Outputs: outputFiles,
|
|
|
|
Args: map[string]string{
|
|
|
|
"zipArgs": zipArgs.String(),
|
|
|
|
"tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(),
|
|
|
|
"genDir": android.PathForModuleGen(ctx, g.subDir).String(),
|
|
|
|
},
|
|
|
|
})
|
2016-11-22 09:23:08 +08:00
|
|
|
}
|
2016-11-05 06:32:58 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
g.outputFiles = outputFiles.Paths()
|
|
|
|
|
|
|
|
// For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of
|
|
|
|
// the genrules on AOSP. That will make things simpler to look at the graph in the common
|
|
|
|
// case. For larger sets of outputs, inject a phony target in between to limit ninja file
|
|
|
|
// growth.
|
|
|
|
if len(g.outputFiles) <= 6 {
|
|
|
|
g.outputDeps = g.outputFiles
|
|
|
|
} else {
|
|
|
|
phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: blueprint.Phony,
|
|
|
|
Output: phonyFile,
|
|
|
|
Inputs: g.outputFiles,
|
|
|
|
})
|
|
|
|
|
|
|
|
g.outputDeps = android.Paths{phonyFile}
|
|
|
|
}
|
2017-11-09 04:38:00 +08:00
|
|
|
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask, rule blueprint.Rule) {
|
2017-05-10 04:45:28 +08:00
|
|
|
desc := "generate"
|
2017-10-21 06:07:08 +08:00
|
|
|
if len(task.out) == 0 {
|
|
|
|
ctx.ModuleErrorf("must have at least one output file")
|
|
|
|
return
|
|
|
|
}
|
2017-05-10 04:45:28 +08:00
|
|
|
if len(task.out) == 1 {
|
|
|
|
desc += " " + task.out[0].Base()
|
|
|
|
}
|
|
|
|
|
2017-10-28 05:59:27 +08:00
|
|
|
var depFile android.ModuleGenPath
|
2017-11-09 13:20:04 +08:00
|
|
|
if Bool(g.properties.Depfile) {
|
2017-10-28 05:59:27 +08:00
|
|
|
depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
|
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
if task.shards > 1 {
|
|
|
|
desc += " " + strconv.Itoa(task.shard)
|
|
|
|
}
|
|
|
|
|
2017-10-24 08:16:14 +08:00
|
|
|
params := android.BuildParams{
|
2019-09-24 06:55:30 +08:00
|
|
|
Rule: rule,
|
|
|
|
Description: desc,
|
2017-10-21 06:07:08 +08:00
|
|
|
Output: task.out[0],
|
|
|
|
ImplicitOutputs: task.out[1:],
|
|
|
|
Inputs: task.in,
|
|
|
|
Implicits: g.deps,
|
|
|
|
Args: map[string]string{
|
2018-02-22 06:07:48 +08:00
|
|
|
"allouts": strings.Join(task.sandboxOuts, " "),
|
2017-10-21 06:07:08 +08:00
|
|
|
},
|
2016-11-22 09:23:08 +08:00
|
|
|
}
|
2017-11-09 13:20:04 +08:00
|
|
|
if Bool(g.properties.Depfile) {
|
2017-10-28 05:59:27 +08:00
|
|
|
params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
|
|
|
|
params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
|
2016-11-22 09:23:08 +08:00
|
|
|
}
|
2017-10-28 05:59:27 +08:00
|
|
|
|
2017-10-24 08:16:14 +08:00
|
|
|
ctx.Build(pctx, params)
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
|
|
|
|
2018-08-16 06:35:38 +08:00
|
|
|
// Collect information for opening IDE project files in java/jdeps.go.
|
|
|
|
func (g *Module) IDEInfo(dpInfo *android.IdeInfo) {
|
|
|
|
dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...)
|
|
|
|
for _, src := range g.properties.Srcs {
|
|
|
|
if strings.HasPrefix(src, ":") {
|
|
|
|
src = strings.Trim(src, ":")
|
|
|
|
dpInfo.Deps = append(dpInfo.Deps, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 13:15:32 +08:00
|
|
|
func (g *Module) AndroidMk() android.AndroidMkData {
|
|
|
|
return android.AndroidMkData{
|
|
|
|
Include: "$(BUILD_PHONY_PACKAGE)",
|
|
|
|
Class: "FAKE",
|
|
|
|
OutputFile: android.OptionalPathForPath(g.outputFiles[0]),
|
|
|
|
SubName: g.subName,
|
|
|
|
Extra: []android.AndroidMkExtraFunc{
|
|
|
|
func(w io.Writer, outputFile android.Path) {
|
2019-09-24 06:55:30 +08:00
|
|
|
fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputDeps.Strings(), " "))
|
2019-03-19 13:15:32 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
|
|
|
|
android.WriteAndroidMkData(w, data)
|
|
|
|
if data.SubName != "" {
|
|
|
|
fmt.Fprintln(w, ".PHONY:", name)
|
|
|
|
fmt.Fprintln(w, name, ":", name+g.subName)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 04:38:00 +08:00
|
|
|
func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
|
2017-09-14 09:37:08 +08:00
|
|
|
module := &Module{
|
2017-11-09 04:38:00 +08:00
|
|
|
taskGenerator: taskGenerator,
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
2015-04-29 04:25:36 +08:00
|
|
|
|
2017-06-24 06:06:31 +08:00
|
|
|
module.AddProperties(props...)
|
|
|
|
module.AddProperties(&module.properties)
|
2015-04-29 04:25:36 +08:00
|
|
|
|
2019-11-19 08:00:16 +08:00
|
|
|
module.ImageInterface = noopImageInterface{}
|
|
|
|
|
2017-06-24 06:06:31 +08:00
|
|
|
return module
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
|
|
|
|
2019-11-19 08:00:16 +08:00
|
|
|
type noopImageInterface struct{}
|
|
|
|
|
|
|
|
func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {}
|
|
|
|
func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false }
|
|
|
|
func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false }
|
|
|
|
func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil }
|
|
|
|
func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
|
|
|
|
}
|
|
|
|
|
2018-02-22 06:07:48 +08:00
|
|
|
// replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>"
|
|
|
|
func pathToSandboxOut(path android.Path, genDir android.Path) string {
|
|
|
|
relOut, err := filepath.Rel(genDir.String(), path.String())
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Could not make ${out} relative: %v", err))
|
|
|
|
}
|
|
|
|
return filepath.Join("__SBOX_OUT_DIR__", relOut)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func NewGenSrcs() *Module {
|
2015-04-29 04:25:36 +08:00
|
|
|
properties := &genSrcsProperties{}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
|
|
|
|
genDir := android.PathForModuleGen(ctx, "gensrcs")
|
|
|
|
shardSize := defaultShardSize
|
|
|
|
if s := properties.Shard_size; s != nil {
|
|
|
|
shardSize = int(*s)
|
|
|
|
}
|
|
|
|
|
|
|
|
shards := android.ShardPaths(srcFiles, shardSize)
|
|
|
|
var generateTasks []generateTask
|
|
|
|
|
|
|
|
for i, shard := range shards {
|
|
|
|
var commands []string
|
|
|
|
var outFiles android.WritablePaths
|
|
|
|
var copyTo android.WritablePaths
|
|
|
|
var shardDir android.WritablePath
|
|
|
|
var sandboxOuts []string
|
|
|
|
|
|
|
|
if len(shards) > 1 {
|
|
|
|
shardDir = android.PathForModuleGen(ctx, strconv.Itoa(i))
|
|
|
|
} else {
|
|
|
|
shardDir = genDir
|
2017-11-09 04:38:00 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
for _, in := range shard {
|
|
|
|
outFile := android.GenPathWithExt(ctx, "gensrcs", in, String(properties.Output_extension))
|
|
|
|
sandboxOutfile := pathToSandboxOut(outFile, genDir)
|
2017-11-09 04:38:00 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
if len(shards) > 1 {
|
|
|
|
shardFile := android.GenPathWithExt(ctx, strconv.Itoa(i), in, String(properties.Output_extension))
|
|
|
|
copyTo = append(copyTo, outFile)
|
|
|
|
outFile = shardFile
|
|
|
|
}
|
|
|
|
|
|
|
|
outFiles = append(outFiles, outFile)
|
|
|
|
sandboxOuts = append(sandboxOuts, sandboxOutfile)
|
|
|
|
|
|
|
|
command, err := android.Expand(rawCommand, func(name string) (string, error) {
|
|
|
|
switch name {
|
|
|
|
case "in":
|
|
|
|
return in.String(), nil
|
|
|
|
case "out":
|
|
|
|
return sandboxOutfile, nil
|
|
|
|
default:
|
|
|
|
return "$(" + name + ")", nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("cmd", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// escape the command in case for example it contains '#', an odd number of '"', etc
|
|
|
|
command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
|
|
|
|
commands = append(commands, command)
|
|
|
|
}
|
|
|
|
fullCommand := strings.Join(commands, " && ")
|
|
|
|
|
|
|
|
generateTasks = append(generateTasks, generateTask{
|
|
|
|
in: shard,
|
|
|
|
out: outFiles,
|
|
|
|
copyTo: copyTo,
|
|
|
|
genDir: shardDir,
|
|
|
|
sandboxOuts: sandboxOuts,
|
|
|
|
cmd: fullCommand,
|
|
|
|
shard: i,
|
|
|
|
shards: len(shards),
|
|
|
|
})
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
2019-09-24 06:55:30 +08:00
|
|
|
|
|
|
|
return generateTasks
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
2015-03-19 04:28:46 +08:00
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
g := generatorFactory(taskGenerator, properties)
|
|
|
|
g.subDir = "gensrcs"
|
|
|
|
return g
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
|
|
|
|
2017-10-10 06:34:10 +08:00
|
|
|
func GenSrcsFactory() android.Module {
|
2017-09-14 09:37:08 +08:00
|
|
|
m := NewGenSrcs()
|
|
|
|
android.InitAndroidModule(m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-04-29 04:25:36 +08:00
|
|
|
type genSrcsProperties struct {
|
2015-05-12 04:39:40 +08:00
|
|
|
// extension that will be substituted for each output file
|
2017-11-10 14:42:32 +08:00
|
|
|
Output_extension *string
|
2019-09-24 06:55:30 +08:00
|
|
|
|
|
|
|
// maximum number of files that will be passed on a single command line.
|
|
|
|
Shard_size *int64
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
const defaultShardSize = 100
|
|
|
|
|
2017-09-14 09:37:08 +08:00
|
|
|
func NewGenRule() *Module {
|
2015-04-29 04:25:36 +08:00
|
|
|
properties := &genRuleProperties{}
|
|
|
|
|
2019-09-24 06:55:30 +08:00
|
|
|
taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask {
|
2016-09-29 07:21:00 +08:00
|
|
|
outs := make(android.WritablePaths, len(properties.Out))
|
2018-02-22 06:07:48 +08:00
|
|
|
sandboxOuts := make([]string, len(properties.Out))
|
|
|
|
genDir := android.PathForModuleGen(ctx)
|
2016-09-29 07:21:00 +08:00
|
|
|
for i, out := range properties.Out {
|
|
|
|
outs[i] = android.PathForModuleGen(ctx, out)
|
2018-02-22 06:07:48 +08:00
|
|
|
sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
|
2016-09-29 07:21:00 +08:00
|
|
|
}
|
2019-09-24 06:55:30 +08:00
|
|
|
return []generateTask{{
|
2018-02-22 06:07:48 +08:00
|
|
|
in: srcFiles,
|
|
|
|
out: outs,
|
2019-09-24 06:55:30 +08:00
|
|
|
genDir: android.PathForModuleGen(ctx),
|
2018-02-22 06:07:48 +08:00
|
|
|
sandboxOuts: sandboxOuts,
|
|
|
|
cmd: rawCommand,
|
2019-09-24 06:55:30 +08:00
|
|
|
}}
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 04:38:00 +08:00
|
|
|
return generatorFactory(taskGenerator, properties)
|
2015-04-29 04:25:36 +08:00
|
|
|
}
|
|
|
|
|
2017-10-10 06:34:10 +08:00
|
|
|
func GenRuleFactory() android.Module {
|
2017-09-14 09:37:08 +08:00
|
|
|
m := NewGenRule()
|
|
|
|
android.InitAndroidModule(m)
|
2018-12-11 00:13:18 +08:00
|
|
|
android.InitDefaultableModule(m)
|
2017-09-14 09:37:08 +08:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-04-29 04:25:36 +08:00
|
|
|
type genRuleProperties struct {
|
2016-09-29 07:21:00 +08:00
|
|
|
// names of the output files that will be generated
|
2018-10-24 02:27:50 +08:00
|
|
|
Out []string `android:"arch_variant"`
|
2015-03-19 04:28:46 +08:00
|
|
|
}
|
2017-11-09 13:20:04 +08:00
|
|
|
|
|
|
|
var Bool = proptools.Bool
|
|
|
|
var String = proptools.String
|
2018-12-11 00:13:18 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Defaults
|
|
|
|
//
|
|
|
|
type Defaults struct {
|
|
|
|
android.ModuleBase
|
|
|
|
android.DefaultsModuleBase
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultsFactory() android.Module {
|
|
|
|
return DefaultsFactory()
|
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultsFactory(props ...interface{}) android.Module {
|
|
|
|
module := &Defaults{}
|
|
|
|
|
|
|
|
module.AddProperties(props...)
|
|
|
|
module.AddProperties(
|
|
|
|
&generatorProperties{},
|
|
|
|
&genRuleProperties{},
|
|
|
|
)
|
|
|
|
|
|
|
|
android.InitDefaultsModule(module)
|
|
|
|
|
|
|
|
return module
|
|
|
|
}
|