2015-03-18 21:28:46 +01: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-04 23:32:58 +01:00
|
|
|
"fmt"
|
2019-03-19 06:15:32 +01:00
|
|
|
"io"
|
2016-11-04 23:32:58 +01:00
|
|
|
"strings"
|
2016-09-29 01:19:10 +02:00
|
|
|
|
2015-03-23 20:57:34 +01:00
|
|
|
"github.com/google/blueprint"
|
2017-09-14 01:07:44 +02:00
|
|
|
"github.com/google/blueprint/bootstrap"
|
2017-11-09 06:20:04 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
"android/soong/android"
|
2017-03-30 02:29:06 +02:00
|
|
|
"android/soong/shared"
|
|
|
|
"path/filepath"
|
2015-03-18 21:28:46 +01:00
|
|
|
)
|
|
|
|
|
2015-06-17 23:20:06 +02:00
|
|
|
func init() {
|
2018-12-10 17:13:18 +01:00
|
|
|
android.RegisterModuleType("genrule_defaults", defaultsFactory)
|
|
|
|
|
2017-10-10 00:34:10 +02:00
|
|
|
android.RegisterModuleType("gensrcs", GenSrcsFactory)
|
|
|
|
android.RegisterModuleType("genrule", GenRuleFactory)
|
2015-06-17 23:20:06 +02:00
|
|
|
}
|
|
|
|
|
2015-03-18 21:28:46 +01:00
|
|
|
var (
|
2016-05-19 00:37:25 +02:00
|
|
|
pctx = android.NewPackageContext("android/soong/genrule")
|
2015-03-18 21:28:46 +01:00
|
|
|
)
|
|
|
|
|
2017-03-30 02:29:06 +02: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 01:21:29 +02:00
|
|
|
pctx.Import("android/soong/android")
|
2017-03-30 02:29:06 +02:00
|
|
|
pctx.HostBinToolVariable("sboxCmd", "sbox")
|
|
|
|
}
|
|
|
|
|
2015-03-18 21:28:46 +01:00
|
|
|
type SourceFileGenerator interface {
|
2016-05-19 00:37:25 +02:00
|
|
|
GeneratedSourceFiles() android.Paths
|
2016-11-22 21:55:55 +01:00
|
|
|
GeneratedHeaderDirs() android.Paths
|
2018-02-22 03:28:18 +01:00
|
|
|
GeneratedDeps() android.Paths
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 03:30:56 +01:00
|
|
|
// Alias for android.HostToolProvider
|
|
|
|
// Deprecated: use android.HostToolProvider instead.
|
2015-04-28 22:25:36 +02:00
|
|
|
type HostToolProvider interface {
|
2019-03-29 03:30:56 +01:00
|
|
|
android.HostToolProvider
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2017-09-14 00:46:47 +02:00
|
|
|
type hostToolDependencyTag struct {
|
|
|
|
blueprint.BaseDependencyTag
|
2018-10-05 08:29:14 +02:00
|
|
|
label string
|
2017-09-14 00:46:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 22:39:40 +02:00
|
|
|
type generatorProperties struct {
|
2017-03-30 02:29:06 +02: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 06:38:14 +02:00
|
|
|
// $(location): the path to the first entry in tools or tool_files
|
2018-10-05 08:29:14 +02:00
|
|
|
// $(location <label>): the path to the tool, tool_file, input or output with name <label>
|
2017-10-18 06:38:14 +02: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-04 23:32:58 +01:00
|
|
|
//
|
2017-03-30 02:29:06 +02: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 06:20:04 +01:00
|
|
|
Cmd *string
|
2015-05-11 22:39:40 +02:00
|
|
|
|
2016-11-22 02:23:08 +01:00
|
|
|
// Enable reading a file containing dependencies in gcc format after the command completes
|
2017-11-09 06:20:04 +01:00
|
|
|
Depfile *bool
|
2016-11-22 02:23:08 +01:00
|
|
|
|
2016-11-04 23:32:58 +01:00
|
|
|
// name of the modules (if any) that produces the host executable. Leave empty for
|
2015-05-11 22:39:40 +02:00
|
|
|
// prebuilts or scripts that do not need a module to build them.
|
2016-11-04 23:32:58 +01:00
|
|
|
Tools []string
|
2016-04-20 23:54:32 +02:00
|
|
|
|
|
|
|
// Local file that is used as the tool
|
2019-03-05 07:35:41 +01:00
|
|
|
Tool_files []string `android:"path"`
|
2016-11-22 21:55:55 +01:00
|
|
|
|
|
|
|
// List of directories to export generated headers from
|
|
|
|
Export_include_dirs []string
|
2017-01-14 03:05:49 +01:00
|
|
|
|
|
|
|
// list of input files
|
2019-03-05 07:35:41 +01:00
|
|
|
Srcs []string `android:"path,arch_variant"`
|
2018-11-17 23:01:18 +01:00
|
|
|
|
|
|
|
// input files to exclude
|
2019-03-05 07:35:41 +01:00
|
|
|
Exclude_srcs []string `android:"path,arch_variant"`
|
2015-05-11 22:39:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
type Module struct {
|
2016-05-19 00:37:25 +02:00
|
|
|
android.ModuleBase
|
2018-12-10 17:13:18 +01:00
|
|
|
android.DefaultableModuleBase
|
2019-06-12 06:27:29 +02:00
|
|
|
android.ApexModuleBase
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
// For other packages to make their own genrules with extra
|
|
|
|
// properties
|
|
|
|
Extra interface{}
|
|
|
|
|
2015-05-11 22:39:40 +02:00
|
|
|
properties generatorProperties
|
2015-04-28 22:25:36 +02:00
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
taskGenerator taskFunc
|
2015-04-28 22:25:36 +02:00
|
|
|
|
2018-10-05 08:28:25 +02:00
|
|
|
deps android.Paths
|
|
|
|
rule blueprint.Rule
|
|
|
|
rawCommand string
|
2015-04-28 22:25:36 +02:00
|
|
|
|
2016-11-22 21:55:55 +01:00
|
|
|
exportedIncludeDirs android.Paths
|
2016-04-20 23:21:14 +02:00
|
|
|
|
2016-05-19 00:37:25 +02:00
|
|
|
outputFiles android.Paths
|
2018-02-22 03:28:18 +01:00
|
|
|
outputDeps android.Paths
|
2019-03-19 06:15:32 +01:00
|
|
|
|
|
|
|
subName string
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2015-04-28 22:25:36 +02:00
|
|
|
type generateTask struct {
|
2018-02-21 23:07:48 +01:00
|
|
|
in android.Paths
|
|
|
|
out android.WritablePaths
|
|
|
|
sandboxOuts []string
|
|
|
|
cmd string
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func (g *Module) GeneratedSourceFiles() android.Paths {
|
2015-04-28 22:25:36 +02:00
|
|
|
return g.outputFiles
|
|
|
|
}
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func (g *Module) Srcs() android.Paths {
|
2018-03-28 01:19:42 +02:00
|
|
|
return append(android.Paths{}, g.outputFiles...)
|
2016-12-14 00:23:47 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func (g *Module) GeneratedHeaderDirs() android.Paths {
|
2016-11-22 21:55:55 +01:00
|
|
|
return g.exportedIncludeDirs
|
2016-04-20 23:21:14 +02:00
|
|
|
}
|
|
|
|
|
2018-02-22 03:28:18 +01:00
|
|
|
func (g *Module) GeneratedDeps() android.Paths {
|
|
|
|
return g.outputDeps
|
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) {
|
|
|
|
if g, ok := ctx.Module().(*Module); ok {
|
2018-10-05 08:29:14 +02:00
|
|
|
for _, tool := range g.properties.Tools {
|
|
|
|
tag := hostToolDependencyTag{label: tool}
|
|
|
|
if m := android.SrcIsModule(tool); m != "" {
|
|
|
|
tool = m
|
|
|
|
}
|
2015-11-25 02:53:15 +01:00
|
|
|
ctx.AddFarVariationDependencies([]blueprint.Variation{
|
2018-07-23 06:18:45 +02:00
|
|
|
{Mutator: "arch", Variation: ctx.Config().BuildOsVariant},
|
2018-10-05 08:29:14 +02:00
|
|
|
}, tag, tool)
|
2015-10-29 23:25:03 +01:00
|
|
|
}
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2019-03-19 06:15:32 +01:00
|
|
|
g.subName = ctx.ModuleSubDir()
|
|
|
|
|
2016-11-22 21:55:55 +01:00
|
|
|
if len(g.properties.Export_include_dirs) > 0 {
|
|
|
|
for _, dir := range g.properties.Export_include_dirs {
|
|
|
|
g.exportedIncludeDirs = append(g.exportedIncludeDirs,
|
|
|
|
android.PathForModuleGen(ctx, ctx.ModuleDir(), dir))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, ""))
|
|
|
|
}
|
2016-09-29 01:19:10 +02:00
|
|
|
|
2018-10-05 08:29:14 +02: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 01:19:10 +02:00
|
|
|
|
2016-11-04 23:32:58 +01:00
|
|
|
if len(g.properties.Tools) > 0 {
|
2019-03-18 20:12:48 +01:00
|
|
|
seenTools := make(map[string]bool)
|
|
|
|
|
2017-11-16 09:11:20 +01:00
|
|
|
ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
|
2018-10-05 08:29:14 +02:00
|
|
|
switch tag := ctx.OtherModuleDependencyTag(module).(type) {
|
|
|
|
case hostToolDependencyTag:
|
2017-09-14 00:46:47 +02:00
|
|
|
tool := ctx.OtherModuleName(module)
|
2017-09-14 01:07:44 +02:00
|
|
|
var path android.OptionalPath
|
2017-09-14 00:46:47 +02:00
|
|
|
|
2019-03-29 03:30:56 +01:00
|
|
|
if t, ok := module.(android.HostToolProvider); ok {
|
2017-11-16 09:11:20 +01:00
|
|
|
if !t.(android.Module).Enabled() {
|
2017-11-29 09:27:14 +01:00
|
|
|
if ctx.Config().AllowMissingDependencies() {
|
2017-11-16 09:11:20 +01:00
|
|
|
ctx.AddMissingDependencies([]string{tool})
|
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("depends on disabled module %q", tool)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2017-09-14 01:07:44 +02: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-04 23:32:58 +01:00
|
|
|
} else {
|
2017-09-14 01:07:44 +02:00
|
|
|
ctx.ModuleErrorf("cannot find path for %q: %v", tool, err)
|
|
|
|
break
|
2016-11-04 23:32:58 +01:00
|
|
|
}
|
2016-04-20 23:54:32 +02:00
|
|
|
} else {
|
2017-09-14 00:46:47 +02:00
|
|
|
ctx.ModuleErrorf("%q is not a host tool provider", tool)
|
2017-09-14 01:07:44 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if path.Valid() {
|
|
|
|
g.deps = append(g.deps, path.Path())
|
2018-10-05 08:29:14 +02:00
|
|
|
addLocationLabel(tag.label, []string{path.Path().String()})
|
2019-03-18 20:12:48 +01:00
|
|
|
seenTools[tag.label] = true
|
2017-09-14 01:07:44 +02:00
|
|
|
} else {
|
|
|
|
ctx.ModuleErrorf("host tool %q missing output file", tool)
|
2016-04-20 23:54:32 +02:00
|
|
|
}
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
2016-04-20 23:54:32 +02:00
|
|
|
})
|
2019-03-18 20:12:48 +01: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-20 23:54:32 +02:00
|
|
|
}
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2017-09-14 00:46:47 +02:00
|
|
|
if ctx.Failed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-05 08:29:14 +02:00
|
|
|
for _, toolFile := range g.properties.Tool_files {
|
2019-03-06 07:25:09 +01:00
|
|
|
paths := android.PathsForModuleSrc(ctx, []string{toolFile})
|
2018-10-05 08:29:14 +02:00
|
|
|
g.deps = append(g.deps, paths...)
|
|
|
|
addLocationLabel(toolFile, paths.Strings())
|
2016-11-04 23:32:58 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 08:29:14 +02:00
|
|
|
var srcFiles android.Paths
|
|
|
|
for _, in := range g.properties.Srcs {
|
2019-03-18 20:12:48 +01: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 08:29:14 +02:00
|
|
|
}
|
2017-10-27 23:59:27 +02:00
|
|
|
|
2017-11-09 06:20:04 +01:00
|
|
|
task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles)
|
2017-11-06 23:15:16 +01:00
|
|
|
|
2018-10-05 08:29:14 +02:00
|
|
|
for _, out := range task.out {
|
|
|
|
addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())})
|
|
|
|
}
|
|
|
|
|
|
|
|
referencedDepfile := false
|
|
|
|
|
2019-07-11 19:58:17 +02:00
|
|
|
rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) {
|
2018-07-09 18:45:06 +02:00
|
|
|
// report the error directly without returning an error to android.Expand to catch multiple errors in a
|
|
|
|
// single run
|
2019-07-11 19:58:17 +02:00
|
|
|
reportError := func(fmt string, args ...interface{}) (string, bool, error) {
|
2018-07-09 18:45:06 +02:00
|
|
|
ctx.PropertyErrorf("cmd", fmt, args...)
|
2019-07-11 19:58:17 +02:00
|
|
|
return "SOONG_ERROR", false, nil
|
2018-07-09 18:45:06 +02:00
|
|
|
}
|
|
|
|
|
2016-11-04 23:32:58 +01:00
|
|
|
switch name {
|
|
|
|
case "location":
|
2018-10-05 08:29:14 +02:00
|
|
|
if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 {
|
2018-07-09 18:45:06 +02:00
|
|
|
return reportError("at least one `tools` or `tool_files` is required if $(location) is used")
|
2016-11-04 23:32:58 +01:00
|
|
|
}
|
2018-10-05 08:29:14 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-07-11 19:58:17 +02:00
|
|
|
return locationLabels[firstLabel][0], false, nil
|
2016-11-04 23:32:58 +01:00
|
|
|
case "in":
|
2019-07-11 19:58:17 +02:00
|
|
|
return "${in}", true, nil
|
2016-11-04 23:32:58 +01:00
|
|
|
case "out":
|
2019-07-11 19:58:17 +02:00
|
|
|
return "__SBOX_OUT_FILES__", false, nil
|
2016-11-22 02:23:08 +01:00
|
|
|
case "depfile":
|
2017-10-27 23:59:27 +02:00
|
|
|
referencedDepfile = true
|
2017-11-09 06:20:04 +01:00
|
|
|
if !Bool(g.properties.Depfile) {
|
2018-07-09 18:45:06 +02:00
|
|
|
return reportError("$(depfile) used without depfile property")
|
2016-11-22 02:23:08 +01:00
|
|
|
}
|
2019-07-11 19:58:17 +02:00
|
|
|
return "__SBOX_DEPFILE__", false, nil
|
2016-11-04 23:32:58 +01:00
|
|
|
case "genDir":
|
2019-07-11 19:58:17 +02:00
|
|
|
return "__SBOX_OUT_DIR__", false, nil
|
2016-11-04 23:32:58 +01:00
|
|
|
default:
|
|
|
|
if strings.HasPrefix(name, "location ") {
|
|
|
|
label := strings.TrimSpace(strings.TrimPrefix(name, "location "))
|
2018-10-05 08:29:14 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-07-11 19:58:17 +02:00
|
|
|
return paths[0], false, nil
|
2016-11-04 23:32:58 +01:00
|
|
|
} else {
|
2018-07-09 18:45:06 +02:00
|
|
|
return reportError("unknown location label %q", label)
|
2016-11-04 23:32:58 +01:00
|
|
|
}
|
2018-10-05 08:29:14 +02:00
|
|
|
} 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)
|
|
|
|
}
|
2019-07-11 19:58:17 +02:00
|
|
|
return strings.Join(paths, " "), false, nil
|
2018-10-05 08:29:14 +02:00
|
|
|
} else {
|
|
|
|
return reportError("unknown locations label %q", label)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return reportError("unknown variable '$(%s)'", name)
|
2016-11-04 23:32:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("cmd", "%s", err.Error())
|
2017-04-19 23:23:26 +02:00
|
|
|
return
|
2016-11-04 23:32:58 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 18:45:06 +02:00
|
|
|
if Bool(g.properties.Depfile) && !referencedDepfile {
|
|
|
|
ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd")
|
|
|
|
}
|
|
|
|
|
2017-03-30 02:29:06 +02:00
|
|
|
// tell the sbox command which directory to use as its sandbox root
|
2017-06-13 00:00:12 +02:00
|
|
|
buildDir := android.PathForOutput(ctx).String()
|
|
|
|
sandboxPath := shared.TempDirForOutDir(buildDir)
|
2017-03-30 02:29:06 +02:00
|
|
|
|
|
|
|
// recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written,
|
|
|
|
// to be replaced later by ninja_strings.go
|
2017-10-27 23:59:27 +02:00
|
|
|
depfilePlaceholder := ""
|
2017-11-09 06:20:04 +01:00
|
|
|
if Bool(g.properties.Depfile) {
|
2017-10-27 23:59:27 +02:00
|
|
|
depfilePlaceholder = "$depfileArgs"
|
|
|
|
}
|
2017-11-06 23:15:16 +01:00
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
genDir := android.PathForModuleGen(ctx)
|
2017-12-08 21:45:35 +01:00
|
|
|
// Escape the command for the shell
|
|
|
|
rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'"
|
2018-10-05 08:28:25 +02:00
|
|
|
g.rawCommand = rawCommand
|
2017-12-08 21:45:35 +01:00
|
|
|
sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts",
|
|
|
|
sandboxPath, genDir, rawCommand, depfilePlaceholder)
|
2017-03-30 02:29:06 +02:00
|
|
|
|
2016-11-22 02:23:08 +01:00
|
|
|
ruleParams := blueprint.RuleParams{
|
2017-03-30 02:29:06 +02:00
|
|
|
Command: sandboxCommand,
|
|
|
|
CommandDeps: []string{"$sboxCmd"},
|
2016-11-22 02:23:08 +01:00
|
|
|
}
|
2017-10-21 00:07:08 +02:00
|
|
|
args := []string{"allouts"}
|
2017-11-09 06:20:04 +01:00
|
|
|
if Bool(g.properties.Depfile) {
|
2016-11-22 02:23:08 +01:00
|
|
|
ruleParams.Deps = blueprint.DepsGCC
|
2017-10-27 23:59:27 +02:00
|
|
|
args = append(args, "depfileArgs")
|
2016-11-22 02:23:08 +01:00
|
|
|
}
|
|
|
|
g.rule = ctx.Rule(pctx, "generator", ruleParams, args...)
|
2016-11-04 23:32:58 +01:00
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
g.generateSourceFile(ctx, task)
|
|
|
|
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) {
|
2017-05-09 22:45:28 +02:00
|
|
|
desc := "generate"
|
2017-10-21 00:07:08 +02:00
|
|
|
if len(task.out) == 0 {
|
|
|
|
ctx.ModuleErrorf("must have at least one output file")
|
|
|
|
return
|
|
|
|
}
|
2017-05-09 22:45:28 +02:00
|
|
|
if len(task.out) == 1 {
|
|
|
|
desc += " " + task.out[0].Base()
|
|
|
|
}
|
|
|
|
|
2017-10-27 23:59:27 +02:00
|
|
|
var depFile android.ModuleGenPath
|
2017-11-09 06:20:04 +01:00
|
|
|
if Bool(g.properties.Depfile) {
|
2017-10-27 23:59:27 +02:00
|
|
|
depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
|
|
|
|
}
|
|
|
|
|
2017-10-24 02:16:14 +02:00
|
|
|
params := android.BuildParams{
|
2017-10-21 00:07:08 +02:00
|
|
|
Rule: g.rule,
|
|
|
|
Description: "generate",
|
|
|
|
Output: task.out[0],
|
|
|
|
ImplicitOutputs: task.out[1:],
|
|
|
|
Inputs: task.in,
|
|
|
|
Implicits: g.deps,
|
|
|
|
Args: map[string]string{
|
2018-02-21 23:07:48 +01:00
|
|
|
"allouts": strings.Join(task.sandboxOuts, " "),
|
2017-10-21 00:07:08 +02:00
|
|
|
},
|
2016-11-22 02:23:08 +01:00
|
|
|
}
|
2017-11-09 06:20:04 +01:00
|
|
|
if Bool(g.properties.Depfile) {
|
2017-10-27 23:59:27 +02:00
|
|
|
params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d")
|
|
|
|
params.Args["depfileArgs"] = "--depfile-out " + depFile.String()
|
2016-11-22 02:23:08 +01:00
|
|
|
}
|
2017-10-27 23:59:27 +02:00
|
|
|
|
2017-10-24 02:16:14 +02:00
|
|
|
ctx.Build(pctx, params)
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2016-09-29 01:21:00 +02:00
|
|
|
for _, outputFile := range task.out {
|
|
|
|
g.outputFiles = append(g.outputFiles, outputFile)
|
|
|
|
}
|
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 01:21:29 +02:00
|
|
|
|
|
|
|
// 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(task.out) <= 6 {
|
|
|
|
g.outputDeps = g.outputFiles
|
|
|
|
} else {
|
|
|
|
phonyFile := android.PathForModuleGen(ctx, "genrule-phony")
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2019-09-20 19:55:10 +02:00
|
|
|
Rule: blueprint.Phony,
|
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 01:21:29 +02:00
|
|
|
Output: phonyFile,
|
|
|
|
Inputs: g.outputFiles,
|
|
|
|
})
|
|
|
|
|
|
|
|
g.outputDeps = android.Paths{phonyFile}
|
|
|
|
}
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 00:35:38 +02: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 06:15:32 +01: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) {
|
|
|
|
fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputFiles.Strings(), " "))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-08 21:38:00 +01:00
|
|
|
func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module {
|
2017-09-14 03:37:08 +02:00
|
|
|
module := &Module{
|
2017-11-08 21:38:00 +01:00
|
|
|
taskGenerator: taskGenerator,
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
2015-04-28 22:25:36 +02:00
|
|
|
|
2017-06-24 00:06:31 +02:00
|
|
|
module.AddProperties(props...)
|
|
|
|
module.AddProperties(&module.properties)
|
2015-04-28 22:25:36 +02:00
|
|
|
|
2017-06-24 00:06:31 +02:00
|
|
|
return module
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 23:07:48 +01: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 03:37:08 +02:00
|
|
|
func NewGenSrcs() *Module {
|
2015-04-28 22:25:36 +02:00
|
|
|
properties := &genSrcsProperties{}
|
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
|
|
|
|
commands := []string{}
|
|
|
|
outFiles := android.WritablePaths{}
|
2018-02-21 23:07:48 +01:00
|
|
|
genDir := android.PathForModuleGen(ctx)
|
|
|
|
sandboxOuts := []string{}
|
2015-04-28 22:25:36 +02:00
|
|
|
for _, in := range srcFiles {
|
2017-11-10 07:42:32 +01:00
|
|
|
outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension))
|
2017-11-08 21:38:00 +01:00
|
|
|
outFiles = append(outFiles, outFile)
|
|
|
|
|
2018-02-21 23:07:48 +01:00
|
|
|
sandboxOutfile := pathToSandboxOut(outFile, genDir)
|
|
|
|
sandboxOuts = append(sandboxOuts, sandboxOutfile)
|
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
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
|
|
|
|
}
|
2015-09-24 00:26:20 +02:00
|
|
|
})
|
2017-11-08 21:38:00 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.PropertyErrorf("cmd", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// escape the command in case for example it contains '#', an odd number of '"', etc
|
2019-02-28 20:00:01 +01:00
|
|
|
command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command))
|
2017-11-08 21:38:00 +01:00
|
|
|
commands = append(commands, command)
|
|
|
|
}
|
|
|
|
fullCommand := strings.Join(commands, " && ")
|
|
|
|
|
|
|
|
return generateTask{
|
2018-02-21 23:07:48 +01:00
|
|
|
in: srcFiles,
|
|
|
|
out: outFiles,
|
|
|
|
sandboxOuts: sandboxOuts,
|
|
|
|
cmd: fullCommand,
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 21:28:46 +01:00
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
return generatorFactory(taskGenerator, properties)
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 00:34:10 +02:00
|
|
|
func GenSrcsFactory() android.Module {
|
2017-09-14 03:37:08 +02:00
|
|
|
m := NewGenSrcs()
|
|
|
|
android.InitAndroidModule(m)
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:25:36 +02:00
|
|
|
type genSrcsProperties struct {
|
2015-05-11 22:39:40 +02:00
|
|
|
// extension that will be substituted for each output file
|
2017-11-10 07:42:32 +01:00
|
|
|
Output_extension *string
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 03:37:08 +02:00
|
|
|
func NewGenRule() *Module {
|
2015-04-28 22:25:36 +02:00
|
|
|
properties := &genRuleProperties{}
|
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask {
|
2016-09-29 01:21:00 +02:00
|
|
|
outs := make(android.WritablePaths, len(properties.Out))
|
2018-02-21 23:07:48 +01:00
|
|
|
sandboxOuts := make([]string, len(properties.Out))
|
|
|
|
genDir := android.PathForModuleGen(ctx)
|
2016-09-29 01:21:00 +02:00
|
|
|
for i, out := range properties.Out {
|
|
|
|
outs[i] = android.PathForModuleGen(ctx, out)
|
2018-02-21 23:07:48 +01:00
|
|
|
sandboxOuts[i] = pathToSandboxOut(outs[i], genDir)
|
2016-09-29 01:21:00 +02:00
|
|
|
}
|
2017-11-08 21:38:00 +01:00
|
|
|
return generateTask{
|
2018-02-21 23:07:48 +01:00
|
|
|
in: srcFiles,
|
|
|
|
out: outs,
|
|
|
|
sandboxOuts: sandboxOuts,
|
|
|
|
cmd: rawCommand,
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 21:38:00 +01:00
|
|
|
return generatorFactory(taskGenerator, properties)
|
2015-04-28 22:25:36 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 00:34:10 +02:00
|
|
|
func GenRuleFactory() android.Module {
|
2017-09-14 03:37:08 +02:00
|
|
|
m := NewGenRule()
|
|
|
|
android.InitAndroidModule(m)
|
2018-12-10 17:13:18 +01:00
|
|
|
android.InitDefaultableModule(m)
|
2017-09-14 03:37:08 +02:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-04-28 22:25:36 +02:00
|
|
|
type genRuleProperties struct {
|
2016-09-29 01:21:00 +02:00
|
|
|
// names of the output files that will be generated
|
2018-10-23 20:27:50 +02:00
|
|
|
Out []string `android:"arch_variant"`
|
2015-03-18 21:28:46 +01:00
|
|
|
}
|
2017-11-09 06:20:04 +01:00
|
|
|
|
|
|
|
var Bool = proptools.Bool
|
|
|
|
var String = proptools.String
|
2018-12-10 17:13:18 +01: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
|
|
|
|
}
|