2019-01-31 02:32:39 +01:00
|
|
|
// Copyright 2019 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 android
|
|
|
|
|
|
|
|
import (
|
2021-04-21 20:32:19 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2019-02-02 01:42:32 +01:00
|
|
|
"fmt"
|
2019-01-31 02:32:39 +01:00
|
|
|
"path/filepath"
|
2020-11-14 01:23:53 +01:00
|
|
|
"regexp"
|
2019-02-02 01:42:32 +01:00
|
|
|
"strings"
|
2019-01-31 02:32:39 +01:00
|
|
|
"testing"
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/shared"
|
2019-01-31 02:32:39 +01:00
|
|
|
)
|
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
func builderContext() BuilderContext {
|
|
|
|
return BuilderContextForTesting(TestConfig("out", nil, "", map[string][]byte{
|
2019-12-14 05:41:13 +01:00
|
|
|
"ld": nil,
|
|
|
|
"a.o": nil,
|
|
|
|
"b.o": nil,
|
|
|
|
"cp": nil,
|
|
|
|
"a": nil,
|
|
|
|
"b": nil,
|
|
|
|
"ls": nil,
|
2020-09-23 06:30:02 +02:00
|
|
|
"ln": nil,
|
2019-12-14 05:41:13 +01:00
|
|
|
"turbine": nil,
|
|
|
|
"java": nil,
|
|
|
|
"javac": nil,
|
|
|
|
}))
|
2019-02-15 19:39:37 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
func ExampleRuleBuilder() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
2019-02-02 01:42:32 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "ld")).
|
|
|
|
Inputs(PathsForTesting("a.o", "b.o")).
|
|
|
|
FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
|
2019-02-02 01:42:32 +01:00
|
|
|
rule.Command().Text("echo success")
|
|
|
|
|
|
|
|
// To add the command to the build graph:
|
2020-11-17 02:32:30 +01:00
|
|
|
// rule.Build("link", "link")
|
2019-02-02 01:42:32 +01:00
|
|
|
|
|
|
|
fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
|
|
|
|
fmt.Printf("tools: %q\n", rule.Tools())
|
|
|
|
fmt.Printf("inputs: %q\n", rule.Inputs())
|
|
|
|
fmt.Printf("outputs: %q\n", rule.Outputs())
|
|
|
|
|
|
|
|
// Output:
|
2021-11-09 21:34:39 +01:00
|
|
|
// commands: "ld a.o b.o -o out/soong/linked && echo success"
|
2019-02-02 01:42:32 +01:00
|
|
|
// tools: ["ld"]
|
|
|
|
// inputs: ["a.o" "b.o"]
|
2021-11-09 21:34:39 +01:00
|
|
|
// outputs: ["out/soong/linked"]
|
2019-02-02 01:42:32 +01:00
|
|
|
}
|
|
|
|
|
2020-09-23 06:30:02 +02:00
|
|
|
func ExampleRuleBuilder_SymlinkOutputs() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
2020-09-23 06:30:02 +02:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2020-09-23 06:30:02 +02:00
|
|
|
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "ln")).
|
|
|
|
FlagWithInput("-s ", PathForTesting("a.o")).
|
|
|
|
SymlinkOutput(PathForOutput(ctx, "a"))
|
2021-11-09 21:34:39 +01:00
|
|
|
rule.Command().Text("cp out/soong/a out/soong/b").
|
2020-09-23 06:30:02 +02:00
|
|
|
ImplicitSymlinkOutput(PathForOutput(ctx, "b"))
|
|
|
|
|
|
|
|
fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
|
|
|
|
fmt.Printf("tools: %q\n", rule.Tools())
|
|
|
|
fmt.Printf("inputs: %q\n", rule.Inputs())
|
|
|
|
fmt.Printf("outputs: %q\n", rule.Outputs())
|
|
|
|
fmt.Printf("symlink_outputs: %q\n", rule.SymlinkOutputs())
|
|
|
|
|
|
|
|
// Output:
|
2021-11-09 21:34:39 +01:00
|
|
|
// commands: "ln -s a.o out/soong/a && cp out/soong/a out/soong/b"
|
2020-09-23 06:30:02 +02:00
|
|
|
// tools: ["ln"]
|
|
|
|
// inputs: ["a.o"]
|
2021-11-09 21:34:39 +01:00
|
|
|
// outputs: ["out/soong/a" "out/soong/b"]
|
|
|
|
// symlink_outputs: ["out/soong/a" "out/soong/b"]
|
2020-09-23 06:30:02 +02:00
|
|
|
}
|
|
|
|
|
2019-02-03 06:25:18 +01:00
|
|
|
func ExampleRuleBuilder_Temporary() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
2019-02-03 06:25:18 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "cp")).
|
|
|
|
Input(PathForSource(ctx, "a")).
|
|
|
|
Output(PathForOutput(ctx, "b"))
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "cp")).
|
|
|
|
Input(PathForOutput(ctx, "b")).
|
|
|
|
Output(PathForOutput(ctx, "c"))
|
|
|
|
rule.Temporary(PathForOutput(ctx, "b"))
|
2019-02-03 06:25:18 +01:00
|
|
|
|
|
|
|
fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
|
|
|
|
fmt.Printf("tools: %q\n", rule.Tools())
|
|
|
|
fmt.Printf("inputs: %q\n", rule.Inputs())
|
|
|
|
fmt.Printf("outputs: %q\n", rule.Outputs())
|
|
|
|
|
|
|
|
// Output:
|
2021-11-09 21:34:39 +01:00
|
|
|
// commands: "cp a out/soong/b && cp out/soong/b out/soong/c"
|
2019-02-03 06:25:18 +01:00
|
|
|
// tools: ["cp"]
|
|
|
|
// inputs: ["a"]
|
2021-11-09 21:34:39 +01:00
|
|
|
// outputs: ["out/soong/c"]
|
2019-02-03 06:25:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRuleBuilder_DeleteTemporaryFiles() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
2019-02-03 06:25:18 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "cp")).
|
|
|
|
Input(PathForSource(ctx, "a")).
|
|
|
|
Output(PathForOutput(ctx, "b"))
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "cp")).
|
|
|
|
Input(PathForOutput(ctx, "b")).
|
|
|
|
Output(PathForOutput(ctx, "c"))
|
|
|
|
rule.Temporary(PathForOutput(ctx, "b"))
|
2019-02-03 06:25:18 +01:00
|
|
|
rule.DeleteTemporaryFiles()
|
|
|
|
|
|
|
|
fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
|
|
|
|
fmt.Printf("tools: %q\n", rule.Tools())
|
|
|
|
fmt.Printf("inputs: %q\n", rule.Inputs())
|
|
|
|
fmt.Printf("outputs: %q\n", rule.Outputs())
|
|
|
|
|
|
|
|
// Output:
|
2021-11-09 21:34:39 +01:00
|
|
|
// commands: "cp a out/soong/b && cp out/soong/b out/soong/c && rm -f out/soong/b"
|
2019-02-03 06:25:18 +01:00
|
|
|
// tools: ["cp"]
|
|
|
|
// inputs: ["a"]
|
2021-11-09 21:34:39 +01:00
|
|
|
// outputs: ["out/soong/c"]
|
2019-02-03 06:25:18 +01:00
|
|
|
}
|
|
|
|
|
2019-02-11 23:11:09 +01:00
|
|
|
func ExampleRuleBuilder_Installs() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
2019-02-11 23:11:09 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-02-15 19:39:37 +01:00
|
|
|
|
|
|
|
out := PathForOutput(ctx, "linked")
|
|
|
|
|
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "ld")).
|
|
|
|
Inputs(PathsForTesting("a.o", "b.o")).
|
|
|
|
FlagWithOutput("-o ", out)
|
|
|
|
rule.Install(out, "/bin/linked")
|
|
|
|
rule.Install(out, "/sbin/linked")
|
2019-02-11 23:11:09 +01:00
|
|
|
|
|
|
|
fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String())
|
|
|
|
|
|
|
|
// Output:
|
2021-11-09 21:34:39 +01:00
|
|
|
// rule.Installs().String() = "out/soong/linked:/bin/linked out/soong/linked:/sbin/linked"
|
2019-02-11 23:11:09 +01:00
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
func ExampleRuleBuilderCommand() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
2019-02-02 01:42:32 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-02-15 19:39:37 +01:00
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
// chained
|
2019-02-15 19:39:37 +01:00
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "ld")).
|
|
|
|
Inputs(PathsForTesting("a.o", "b.o")).
|
|
|
|
FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
|
2019-02-02 01:42:32 +01:00
|
|
|
|
|
|
|
// unchained
|
|
|
|
cmd := rule.Command()
|
2019-02-15 19:39:37 +01:00
|
|
|
cmd.Tool(PathForSource(ctx, "ld"))
|
|
|
|
cmd.Inputs(PathsForTesting("a.o", "b.o"))
|
|
|
|
cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
|
2019-02-02 01:42:32 +01:00
|
|
|
|
|
|
|
// mixed:
|
2019-02-15 19:39:37 +01:00
|
|
|
cmd = rule.Command().Tool(PathForSource(ctx, "ld"))
|
|
|
|
cmd.Inputs(PathsForTesting("a.o", "b.o"))
|
|
|
|
cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
|
2019-02-02 01:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRuleBuilderCommand_Flag() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "ls")).Flag("-l"))
|
2019-02-02 01:42:32 +01:00
|
|
|
// Output:
|
|
|
|
// ls -l
|
|
|
|
}
|
|
|
|
|
2019-03-29 23:32:51 +01:00
|
|
|
func ExampleRuleBuilderCommand_Flags() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-03-29 23:32:51 +01:00
|
|
|
Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"}))
|
|
|
|
// Output:
|
|
|
|
// ls -l -a
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
func ExampleRuleBuilderCommand_FlagWithArg() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "ls")).
|
2019-02-02 01:42:32 +01:00
|
|
|
FlagWithArg("--sort=", "time"))
|
|
|
|
// Output:
|
|
|
|
// ls --sort=time
|
|
|
|
}
|
|
|
|
|
2019-02-11 23:11:09 +01:00
|
|
|
func ExampleRuleBuilderCommand_FlagForEachArg() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "ls")).
|
2019-02-11 23:11:09 +01:00
|
|
|
FlagForEachArg("--sort=", []string{"time", "size"}))
|
|
|
|
// Output:
|
|
|
|
// ls --sort=time --sort=size
|
|
|
|
}
|
|
|
|
|
2019-02-02 01:42:32 +01:00
|
|
|
func ExampleRuleBuilderCommand_FlagForEachInput() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "turbine")).
|
|
|
|
FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
|
2019-02-02 01:42:32 +01:00
|
|
|
// Output:
|
|
|
|
// turbine --classpath a.jar --classpath b.jar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRuleBuilderCommand_FlagWithInputList() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "java")).
|
|
|
|
FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
|
2019-02-02 01:42:32 +01:00
|
|
|
// Output:
|
|
|
|
// java -classpath=a.jar:b.jar
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRuleBuilderCommand_FlagWithInput() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "java")).
|
|
|
|
FlagWithInput("-classpath=", PathForSource(ctx, "a")))
|
2019-02-02 01:42:32 +01:00
|
|
|
// Output:
|
|
|
|
// java -classpath=a
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRuleBuilderCommand_FlagWithList() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-02-15 19:39:37 +01:00
|
|
|
Tool(PathForSource(ctx, "ls")).
|
2019-02-02 01:42:32 +01:00
|
|
|
FlagWithList("--sort=", []string{"time", "size"}, ","))
|
|
|
|
// Output:
|
|
|
|
// ls --sort=time,size
|
|
|
|
}
|
|
|
|
|
2019-07-11 19:59:15 +02:00
|
|
|
func ExampleRuleBuilderCommand_FlagWithRspFileInputList() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-07-11 19:59:15 +02:00
|
|
|
Tool(PathForSource(ctx, "javac")).
|
2021-03-13 02:48:14 +01:00
|
|
|
FlagWithRspFileInputList("@", PathForOutput(ctx, "foo.rsp"), PathsForTesting("a.java", "b.java")).
|
|
|
|
String())
|
2019-07-11 19:59:15 +02:00
|
|
|
// Output:
|
2021-11-09 21:34:39 +01:00
|
|
|
// javac @out/soong/foo.rsp
|
2019-07-11 19:59:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleRuleBuilderCommand_String() {
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContext()
|
|
|
|
fmt.Println(NewRuleBuilder(pctx, ctx).Command().
|
2019-07-11 19:59:15 +02:00
|
|
|
Text("FOO=foo").
|
|
|
|
Text("echo $FOO").
|
|
|
|
String())
|
|
|
|
// Output:
|
|
|
|
// FOO=foo echo $FOO
|
|
|
|
}
|
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
func TestRuleBuilder(t *testing.T) {
|
2019-02-15 19:39:37 +01:00
|
|
|
fs := map[string][]byte{
|
2020-02-22 01:55:19 +01:00
|
|
|
"dep_fixer": nil,
|
|
|
|
"input": nil,
|
|
|
|
"Implicit": nil,
|
|
|
|
"Input": nil,
|
|
|
|
"OrderOnly": nil,
|
|
|
|
"OrderOnlys": nil,
|
|
|
|
"Tool": nil,
|
|
|
|
"input2": nil,
|
|
|
|
"tool2": nil,
|
|
|
|
"input3": nil,
|
2019-02-15 19:39:37 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 02:52:23 +01:00
|
|
|
pathCtx := PathContextForTesting(TestConfig("out_local", nil, "", fs))
|
2020-11-17 02:32:30 +01:00
|
|
|
ctx := builderContextForTests{
|
|
|
|
PathContext: pathCtx,
|
|
|
|
}
|
2019-02-15 19:39:37 +01:00
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
addCommands := func(rule *RuleBuilder) {
|
|
|
|
cmd := rule.Command().
|
2021-03-13 02:52:23 +01:00
|
|
|
DepFile(PathForOutput(ctx, "module/DepFile")).
|
2019-04-12 20:11:38 +02:00
|
|
|
Flag("Flag").
|
|
|
|
FlagWithArg("FlagWithArg=", "arg").
|
2021-03-13 02:52:23 +01:00
|
|
|
FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "module/depfile")).
|
2019-04-12 20:11:38 +02:00
|
|
|
FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
|
2021-03-13 02:52:23 +01:00
|
|
|
FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "module/output")).
|
|
|
|
FlagWithRspFileInputList("FlagWithRspFileInputList=", PathForOutput(ctx, "rsp"),
|
|
|
|
Paths{
|
|
|
|
PathForSource(ctx, "RspInput"),
|
|
|
|
PathForOutput(ctx, "other/RspOutput2"),
|
|
|
|
}).
|
2019-04-12 20:11:38 +02:00
|
|
|
Implicit(PathForSource(ctx, "Implicit")).
|
2021-03-13 02:52:23 +01:00
|
|
|
ImplicitDepFile(PathForOutput(ctx, "module/ImplicitDepFile")).
|
|
|
|
ImplicitOutput(PathForOutput(ctx, "module/ImplicitOutput")).
|
2019-04-12 20:11:38 +02:00
|
|
|
Input(PathForSource(ctx, "Input")).
|
2021-03-13 02:52:23 +01:00
|
|
|
Output(PathForOutput(ctx, "module/Output")).
|
2020-02-22 01:55:19 +01:00
|
|
|
OrderOnly(PathForSource(ctx, "OrderOnly")).
|
2021-04-21 20:45:23 +02:00
|
|
|
Validation(PathForSource(ctx, "Validation")).
|
2021-03-13 02:52:23 +01:00
|
|
|
SymlinkOutput(PathForOutput(ctx, "module/SymlinkOutput")).
|
|
|
|
ImplicitSymlinkOutput(PathForOutput(ctx, "module/ImplicitSymlinkOutput")).
|
2019-04-12 20:11:38 +02:00
|
|
|
Text("Text").
|
|
|
|
Tool(PathForSource(ctx, "Tool"))
|
|
|
|
|
|
|
|
rule.Command().
|
|
|
|
Text("command2").
|
2021-03-13 02:52:23 +01:00
|
|
|
DepFile(PathForOutput(ctx, "module/depfile2")).
|
2019-04-12 20:11:38 +02:00
|
|
|
Input(PathForSource(ctx, "input2")).
|
2021-03-13 02:52:23 +01:00
|
|
|
Output(PathForOutput(ctx, "module/output2")).
|
2020-02-22 01:55:19 +01:00
|
|
|
OrderOnlys(PathsForSource(ctx, []string{"OrderOnlys"})).
|
2021-04-21 20:45:23 +02:00
|
|
|
Validations(PathsForSource(ctx, []string{"Validations"})).
|
2019-04-12 20:11:38 +02:00
|
|
|
Tool(PathForSource(ctx, "tool2"))
|
|
|
|
|
|
|
|
// Test updates to the first command after the second command has been started
|
|
|
|
cmd.Text("after command2")
|
|
|
|
// Test updating a command when the previous update did not replace the cmd variable
|
|
|
|
cmd.Text("old cmd")
|
|
|
|
|
|
|
|
// Test a command that uses the output of a previous command as an input
|
|
|
|
rule.Command().
|
|
|
|
Text("command3").
|
|
|
|
Input(PathForSource(ctx, "input3")).
|
2021-03-13 02:52:23 +01:00
|
|
|
Input(PathForOutput(ctx, "module/output2")).
|
|
|
|
Output(PathForOutput(ctx, "module/output3")).
|
|
|
|
Text(cmd.PathForInput(PathForSource(ctx, "input3"))).
|
|
|
|
Text(cmd.PathForOutput(PathForOutput(ctx, "module/output2")))
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2019-03-29 23:33:06 +01:00
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
|
2021-03-13 02:52:23 +01:00
|
|
|
wantRspFileInputs := Paths{PathForSource(ctx, "RspInput"),
|
|
|
|
PathForOutput(ctx, "other/RspOutput2")}
|
|
|
|
wantOutputs := PathsForOutput(ctx, []string{
|
|
|
|
"module/ImplicitOutput", "module/ImplicitSymlinkOutput", "module/Output", "module/SymlinkOutput",
|
|
|
|
"module/output", "module/output2", "module/output3"})
|
|
|
|
wantDepFiles := PathsForOutput(ctx, []string{
|
|
|
|
"module/DepFile", "module/depfile", "module/ImplicitDepFile", "module/depfile2"})
|
2019-02-15 19:39:37 +01:00
|
|
|
wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
|
2020-02-22 01:55:19 +01:00
|
|
|
wantOrderOnlys := PathsForSource(ctx, []string{"OrderOnly", "OrderOnlys"})
|
2021-04-21 20:45:23 +02:00
|
|
|
wantValidations := PathsForSource(ctx, []string{"Validation", "Validations"})
|
2021-03-13 02:52:23 +01:00
|
|
|
wantSymlinkOutputs := PathsForOutput(ctx, []string{
|
|
|
|
"module/ImplicitSymlinkOutput", "module/SymlinkOutput"})
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
t.Run("normal", func(t *testing.T) {
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-04-12 20:11:38 +02:00
|
|
|
addCommands(rule)
|
2019-03-29 23:33:06 +01:00
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
wantCommands := []string{
|
2021-11-09 21:34:39 +01:00
|
|
|
"out_local/soong/module/DepFile Flag FlagWithArg=arg FlagWithDepFile=out_local/soong/module/depfile " +
|
|
|
|
"FlagWithInput=input FlagWithOutput=out_local/soong/module/output FlagWithRspFileInputList=out_local/soong/rsp " +
|
|
|
|
"Input out_local/soong/module/Output out_local/soong/module/SymlinkOutput Text Tool after command2 old cmd",
|
|
|
|
"command2 out_local/soong/module/depfile2 input2 out_local/soong/module/output2 tool2",
|
|
|
|
"command3 input3 out_local/soong/module/output2 out_local/soong/module/output3 input3 out_local/soong/module/output2",
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
2019-03-29 23:33:06 +01:00
|
|
|
|
2021-11-09 21:34:39 +01:00
|
|
|
wantDepMergerCommand := "out_local/soong/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer " +
|
|
|
|
"out_local/soong/module/DepFile out_local/soong/module/depfile out_local/soong/module/ImplicitDepFile out_local/soong/module/depfile2"
|
2021-03-13 02:52:23 +01:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
|
2021-03-13 02:52:23 +01:00
|
|
|
AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
|
|
|
|
AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
|
|
|
|
AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
|
|
|
|
AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
|
|
|
|
AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
|
2021-04-21 20:45:23 +02:00
|
|
|
AssertDeepEquals(t, "rule.Validations()", wantValidations, rule.Validations())
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
|
2019-04-12 20:11:38 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sbox", func(t *testing.T) {
|
2021-03-13 02:52:23 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
|
2020-11-12 17:29:30 +01:00
|
|
|
PathForOutput(ctx, "sbox.textproto"))
|
2019-04-12 20:11:38 +02:00
|
|
|
addCommands(rule)
|
|
|
|
|
|
|
|
wantCommands := []string{
|
2021-03-13 02:52:23 +01:00
|
|
|
"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
|
|
|
|
"FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
|
2021-11-09 21:34:39 +01:00
|
|
|
"FlagWithRspFileInputList=out_local/soong/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
|
2021-03-13 02:52:23 +01:00
|
|
|
"__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text Tool after command2 old cmd",
|
2020-11-12 17:29:30 +01:00
|
|
|
"command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 tool2",
|
2021-03-13 02:52:23 +01:00
|
|
|
"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-11-09 21:34:39 +01:00
|
|
|
wantDepMergerCommand := "out_local/soong/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
|
2021-03-13 02:52:23 +01:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
|
2021-03-13 02:52:23 +01:00
|
|
|
AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
|
2021-03-13 02:52:23 +01:00
|
|
|
AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
|
|
|
|
AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
|
|
|
|
AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
|
2021-04-21 20:45:23 +02:00
|
|
|
AssertDeepEquals(t, "rule.Validations()", wantValidations, rule.Validations())
|
2020-11-25 01:32:22 +01:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
|
2020-11-25 01:32:22 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sbox tools", func(t *testing.T) {
|
2021-03-13 02:52:23 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
|
2020-11-25 01:32:22 +01:00
|
|
|
PathForOutput(ctx, "sbox.textproto")).SandboxTools()
|
|
|
|
addCommands(rule)
|
|
|
|
|
|
|
|
wantCommands := []string{
|
2021-03-13 02:52:23 +01:00
|
|
|
"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
|
|
|
|
"FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
|
2021-11-09 21:34:39 +01:00
|
|
|
"FlagWithRspFileInputList=out_local/soong/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
|
2021-03-13 02:52:23 +01:00
|
|
|
"__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd",
|
2020-11-25 01:32:22 +01:00
|
|
|
"command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2",
|
2021-03-13 02:52:23 +01:00
|
|
|
"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
|
2020-11-25 01:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
|
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
|
2020-11-25 01:32:22 +01:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
|
2021-03-13 02:52:23 +01:00
|
|
|
AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
|
2021-03-13 02:52:23 +01:00
|
|
|
AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
|
|
|
|
AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
|
|
|
|
AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
|
2021-04-21 20:45:23 +02:00
|
|
|
AssertDeepEquals(t, "rule.Validations()", wantValidations, rule.Validations())
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
|
2019-04-12 20:11:38 +02:00
|
|
|
})
|
2021-03-25 00:38:03 +01:00
|
|
|
|
|
|
|
t.Run("sbox inputs", func(t *testing.T) {
|
|
|
|
rule := NewRuleBuilder(pctx, ctx).Sbox(PathForOutput(ctx, "module"),
|
|
|
|
PathForOutput(ctx, "sbox.textproto")).SandboxInputs()
|
|
|
|
addCommands(rule)
|
|
|
|
|
|
|
|
wantCommands := []string{
|
|
|
|
"__SBOX_SANDBOX_DIR__/out/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_SANDBOX_DIR__/out/depfile " +
|
|
|
|
"FlagWithInput=input FlagWithOutput=__SBOX_SANDBOX_DIR__/out/output " +
|
|
|
|
"FlagWithRspFileInputList=__SBOX_SANDBOX_DIR__/out/rsp Input __SBOX_SANDBOX_DIR__/out/Output " +
|
|
|
|
"__SBOX_SANDBOX_DIR__/out/SymlinkOutput Text __SBOX_SANDBOX_DIR__/tools/src/Tool after command2 old cmd",
|
|
|
|
"command2 __SBOX_SANDBOX_DIR__/out/depfile2 input2 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/tools/src/tool2",
|
|
|
|
"command3 input3 __SBOX_SANDBOX_DIR__/out/output2 __SBOX_SANDBOX_DIR__/out/output3 input3 __SBOX_SANDBOX_DIR__/out/output2",
|
|
|
|
}
|
|
|
|
|
|
|
|
wantDepMergerCommand := "__SBOX_SANDBOX_DIR__/tools/out/bin/dep_fixer __SBOX_SANDBOX_DIR__/out/DepFile __SBOX_SANDBOX_DIR__/out/depfile __SBOX_SANDBOX_DIR__/out/ImplicitDepFile __SBOX_SANDBOX_DIR__/out/depfile2"
|
|
|
|
|
|
|
|
AssertDeepEquals(t, "rule.Commands()", wantCommands, rule.Commands())
|
|
|
|
|
|
|
|
AssertDeepEquals(t, "rule.Inputs()", wantInputs, rule.Inputs())
|
|
|
|
AssertDeepEquals(t, "rule.RspfileInputs()", wantRspFileInputs, rule.RspFileInputs())
|
|
|
|
AssertDeepEquals(t, "rule.Outputs()", wantOutputs, rule.Outputs())
|
|
|
|
AssertDeepEquals(t, "rule.SymlinkOutputs()", wantSymlinkOutputs, rule.SymlinkOutputs())
|
|
|
|
AssertDeepEquals(t, "rule.DepFiles()", wantDepFiles, rule.DepFiles())
|
|
|
|
AssertDeepEquals(t, "rule.Tools()", wantTools, rule.Tools())
|
|
|
|
AssertDeepEquals(t, "rule.OrderOnlys()", wantOrderOnlys, rule.OrderOnlys())
|
2021-04-21 20:45:23 +02:00
|
|
|
AssertDeepEquals(t, "rule.Validations()", wantValidations, rule.Validations())
|
2021-03-25 00:38:03 +01:00
|
|
|
|
|
|
|
AssertSame(t, "rule.depFileMergerCmd()", wantDepMergerCommand, rule.depFileMergerCmd(rule.DepFiles()).String())
|
|
|
|
})
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testRuleBuilderFactory() Module {
|
|
|
|
module := &testRuleBuilderModule{}
|
|
|
|
module.AddProperties(&module.properties)
|
|
|
|
InitAndroidModule(module)
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
type testRuleBuilderModule struct {
|
|
|
|
ModuleBase
|
|
|
|
properties struct {
|
2020-11-14 01:23:53 +01:00
|
|
|
Srcs []string
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
Restat bool
|
|
|
|
Sbox bool
|
|
|
|
Sbox_inputs bool
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
|
2020-11-14 01:23:53 +01:00
|
|
|
in := PathsForSource(ctx, t.properties.Srcs)
|
2021-04-21 20:32:19 +02:00
|
|
|
implicit := PathForSource(ctx, "implicit")
|
|
|
|
orderOnly := PathForSource(ctx, "orderonly")
|
2021-04-21 20:45:23 +02:00
|
|
|
validation := PathForSource(ctx, "validation")
|
2020-11-12 17:29:30 +01:00
|
|
|
out := PathForModuleOut(ctx, "gen", ctx.ModuleName())
|
|
|
|
outDep := PathForModuleOut(ctx, "gen", ctx.ModuleName()+".d")
|
|
|
|
outDir := PathForModuleOut(ctx, "gen")
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFile := PathForModuleOut(ctx, "rsp")
|
|
|
|
rspFile2 := PathForModuleOut(ctx, "rsp2")
|
|
|
|
rspFileContents := PathsForSource(ctx, []string{"rsp_in"})
|
|
|
|
rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"})
|
2020-11-12 17:29:30 +01:00
|
|
|
manifestPath := PathForModuleOut(ctx, "sbox.textproto")
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2021-04-21 20:45:23 +02:00
|
|
|
testRuleBuilder_Build(ctx, in, implicit, orderOnly, validation, out, outDep, outDir,
|
2021-04-21 20:32:19 +02:00
|
|
|
manifestPath, t.properties.Restat, t.properties.Sbox, t.properties.Sbox_inputs,
|
|
|
|
rspFile, rspFileContents, rspFile2, rspFileContents2)
|
2019-02-02 01:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type testRuleBuilderSingleton struct{}
|
|
|
|
|
|
|
|
func testRuleBuilderSingletonFactory() Singleton {
|
|
|
|
return &testRuleBuilderSingleton{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
|
2021-04-21 20:32:19 +02:00
|
|
|
in := PathsForSource(ctx, []string{"in"})
|
|
|
|
implicit := PathForSource(ctx, "implicit")
|
|
|
|
orderOnly := PathForSource(ctx, "orderonly")
|
2021-04-21 20:45:23 +02:00
|
|
|
validation := PathForSource(ctx, "validation")
|
2020-11-12 17:29:30 +01:00
|
|
|
out := PathForOutput(ctx, "singleton/gen/baz")
|
|
|
|
outDep := PathForOutput(ctx, "singleton/gen/baz.d")
|
|
|
|
outDir := PathForOutput(ctx, "singleton/gen")
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFile := PathForOutput(ctx, "singleton/rsp")
|
|
|
|
rspFile2 := PathForOutput(ctx, "singleton/rsp2")
|
|
|
|
rspFileContents := PathsForSource(ctx, []string{"rsp_in"})
|
|
|
|
rspFileContents2 := PathsForSource(ctx, []string{"rsp_in2"})
|
2020-11-12 17:29:30 +01:00
|
|
|
manifestPath := PathForOutput(ctx, "singleton/sbox.textproto")
|
2021-04-21 20:32:19 +02:00
|
|
|
|
2021-04-21 20:45:23 +02:00
|
|
|
testRuleBuilder_Build(ctx, in, implicit, orderOnly, validation, out, outDep, outDir,
|
2021-04-21 20:32:19 +02:00
|
|
|
manifestPath, true, false, false,
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFile, rspFileContents, rspFile2, rspFileContents2)
|
2019-02-02 01:41:11 +01:00
|
|
|
}
|
|
|
|
|
2021-04-21 20:45:23 +02:00
|
|
|
func testRuleBuilder_Build(ctx BuilderContext, in Paths, implicit, orderOnly, validation Path,
|
2021-04-21 20:32:19 +02:00
|
|
|
out, outDep, outDir, manifestPath WritablePath,
|
2021-03-20 00:22:12 +01:00
|
|
|
restat, sbox, sboxInputs bool,
|
|
|
|
rspFile WritablePath, rspFileContents Paths, rspFile2 WritablePath, rspFileContents2 Paths) {
|
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule := NewRuleBuilder(pctx, ctx)
|
2019-02-02 01:41:11 +01:00
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
if sbox {
|
2020-11-12 17:29:30 +01:00
|
|
|
rule.Sbox(outDir, manifestPath)
|
2021-03-20 00:22:12 +01:00
|
|
|
if sboxInputs {
|
|
|
|
rule.SandboxInputs()
|
|
|
|
}
|
2019-04-12 20:11:38 +02:00
|
|
|
}
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
rule.Command().
|
|
|
|
Tool(PathForSource(ctx, "cp")).
|
|
|
|
Inputs(in).
|
2021-04-21 20:32:19 +02:00
|
|
|
Implicit(implicit).
|
|
|
|
OrderOnly(orderOnly).
|
2021-04-21 20:45:23 +02:00
|
|
|
Validation(validation).
|
2021-03-20 00:22:12 +01:00
|
|
|
Output(out).
|
|
|
|
ImplicitDepFile(outDep).
|
|
|
|
FlagWithRspFileInputList("@", rspFile, rspFileContents).
|
|
|
|
FlagWithRspFileInputList("@", rspFile2, rspFileContents2)
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
if restat {
|
|
|
|
rule.Restat()
|
|
|
|
}
|
2019-02-25 23:56:01 +01:00
|
|
|
|
2020-11-17 02:32:30 +01:00
|
|
|
rule.Build("rule", "desc")
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
var prepareForRuleBuilderTest = FixtureRegisterWithContext(func(ctx RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("rule_builder_test", testRuleBuilderFactory)
|
|
|
|
ctx.RegisterSingletonType("rule_builder_test", testRuleBuilderSingletonFactory)
|
|
|
|
})
|
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
func TestRuleBuilder_Build(t *testing.T) {
|
2021-03-16 22:51:29 +01:00
|
|
|
fs := MockFS{
|
2021-04-21 20:32:19 +02:00
|
|
|
"in": nil,
|
|
|
|
"cp": nil,
|
2019-12-14 05:41:13 +01:00
|
|
|
}
|
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
bp := `
|
|
|
|
rule_builder_test {
|
|
|
|
name: "foo",
|
2021-04-21 20:32:19 +02:00
|
|
|
srcs: ["in"],
|
2019-04-12 20:11:38 +02:00
|
|
|
restat: true,
|
|
|
|
}
|
|
|
|
rule_builder_test {
|
|
|
|
name: "foo_sbox",
|
2021-04-21 20:32:19 +02:00
|
|
|
srcs: ["in"],
|
2019-04-12 20:11:38 +02:00
|
|
|
sbox: true,
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2021-03-20 00:22:12 +01:00
|
|
|
rule_builder_test {
|
|
|
|
name: "foo_sbox_inputs",
|
2021-04-21 20:32:19 +02:00
|
|
|
srcs: ["in"],
|
2021-03-20 00:22:12 +01:00
|
|
|
sbox: true,
|
|
|
|
sbox_inputs: true,
|
|
|
|
}
|
2019-01-31 02:32:39 +01:00
|
|
|
`
|
|
|
|
|
2021-03-20 01:36:14 +01:00
|
|
|
result := GroupFixturePreparers(
|
2021-03-16 22:51:29 +01:00
|
|
|
prepareForRuleBuilderTest,
|
|
|
|
FixtureWithRootAndroidBp(bp),
|
|
|
|
fs.AddToFixture(),
|
2021-03-20 01:36:14 +01:00
|
|
|
).RunTest(t)
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
check := func(t *testing.T, params TestingBuildParams, rspFile2Params TestingBuildParams,
|
|
|
|
wantCommand, wantOutput, wantDepfile, wantRspFile, wantRspFile2 string,
|
|
|
|
wantRestat bool, extraImplicits, extraCmdDeps []string) {
|
|
|
|
|
2019-08-29 23:47:40 +02:00
|
|
|
t.Helper()
|
2020-11-14 01:23:53 +01:00
|
|
|
command := params.RuleParams.Command
|
2020-11-12 17:29:30 +01:00
|
|
|
re := regexp.MustCompile(" # hash of input list: [a-z0-9]*$")
|
2020-11-14 01:23:53 +01:00
|
|
|
command = re.ReplaceAllLiteralString(command, "")
|
2021-03-16 22:51:29 +01:00
|
|
|
|
|
|
|
AssertStringEquals(t, "RuleParams.Command", wantCommand, command)
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
wantDeps := append([]string{"cp"}, extraCmdDeps...)
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertArrayString(t, "RuleParams.CommandDeps", wantDeps, params.RuleParams.CommandDeps)
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertBoolEquals(t, "RuleParams.Restat", wantRestat, params.RuleParams.Restat)
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
wantInputs := []string{"rsp_in"}
|
|
|
|
AssertArrayString(t, "Inputs", wantInputs, params.Inputs.Strings())
|
|
|
|
|
2021-04-21 20:32:19 +02:00
|
|
|
wantImplicits := append([]string{"implicit", "in"}, extraImplicits...)
|
2021-03-20 00:22:12 +01:00
|
|
|
// The second rsp file and the files listed in it should be in implicits
|
|
|
|
wantImplicits = append(wantImplicits, "rsp_in2", wantRspFile2)
|
2021-03-22 11:09:02 +01:00
|
|
|
AssertPathsRelativeToTopEquals(t, "Implicits", wantImplicits, params.Implicits)
|
2019-01-31 02:32:39 +01:00
|
|
|
|
2021-04-21 20:32:19 +02:00
|
|
|
wantOrderOnlys := []string{"orderonly"}
|
|
|
|
AssertPathsRelativeToTopEquals(t, "OrderOnly", wantOrderOnlys, params.OrderOnly)
|
|
|
|
|
2021-04-21 20:45:23 +02:00
|
|
|
wantValidations := []string{"validation"}
|
|
|
|
AssertPathsRelativeToTopEquals(t, "Validations", wantValidations, params.Validations)
|
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
wantRspFileContent := "$in"
|
|
|
|
AssertStringEquals(t, "RspfileContent", wantRspFileContent, params.RuleParams.RspfileContent)
|
|
|
|
|
|
|
|
AssertStringEquals(t, "Rspfile", wantRspFile, params.RuleParams.Rspfile)
|
|
|
|
|
2021-03-22 11:09:02 +01:00
|
|
|
AssertPathRelativeToTopEquals(t, "Output", wantOutput, params.Output)
|
2019-02-25 23:56:01 +01:00
|
|
|
|
2019-04-12 20:11:38 +02:00
|
|
|
if len(params.ImplicitOutputs) != 0 {
|
|
|
|
t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings())
|
|
|
|
}
|
|
|
|
|
2021-03-22 11:09:02 +01:00
|
|
|
AssertPathRelativeToTopEquals(t, "Depfile", wantDepfile, params.Depfile)
|
2019-04-12 20:11:38 +02:00
|
|
|
|
|
|
|
if params.Deps != blueprint.DepsGCC {
|
|
|
|
t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps)
|
2019-02-25 23:56:01 +01:00
|
|
|
}
|
2021-03-20 00:22:12 +01:00
|
|
|
|
|
|
|
rspFile2Content := ContentFromFileRuleForTests(t, rspFile2Params)
|
|
|
|
AssertStringEquals(t, "rspFile2 content", "rsp_in2\n", rspFile2Content)
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:54:28 +01:00
|
|
|
t.Run("module", func(t *testing.T) {
|
2021-03-22 11:09:02 +01:00
|
|
|
outFile := "out/soong/.intermediates/foo/gen/foo"
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFile := "out/soong/.intermediates/foo/rsp"
|
|
|
|
rspFile2 := "out/soong/.intermediates/foo/rsp2"
|
|
|
|
module := result.ModuleForTests("foo", "")
|
2021-03-29 01:42:57 +02:00
|
|
|
check(t, module.Rule("rule"), module.Output(rspFile2),
|
2021-04-21 20:32:19 +02:00
|
|
|
"cp in "+outFile+" @"+rspFile+" @"+rspFile2,
|
2021-03-20 00:22:12 +01:00
|
|
|
outFile, outFile+".d", rspFile, rspFile2, true, nil, nil)
|
2019-04-12 20:11:38 +02:00
|
|
|
})
|
|
|
|
t.Run("sbox", func(t *testing.T) {
|
2021-03-22 11:09:02 +01:00
|
|
|
outDir := "out/soong/.intermediates/foo_sbox"
|
2020-11-12 17:29:30 +01:00
|
|
|
outFile := filepath.Join(outDir, "gen/foo_sbox")
|
|
|
|
depFile := filepath.Join(outDir, "gen/foo_sbox.d")
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFile := filepath.Join(outDir, "rsp")
|
|
|
|
rspFile2 := filepath.Join(outDir, "rsp2")
|
|
|
|
manifest := filepath.Join(outDir, "sbox.textproto")
|
|
|
|
sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox")
|
|
|
|
sandboxPath := shared.TempDirForOutDir("out/soong")
|
|
|
|
|
|
|
|
cmd := `rm -rf ` + outDir + `/gen && ` +
|
|
|
|
sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest
|
|
|
|
module := result.ModuleForTests("foo_sbox", "")
|
2021-03-29 01:42:57 +02:00
|
|
|
check(t, module.Output("gen/foo_sbox"), module.Output(rspFile2),
|
2021-03-20 00:22:12 +01:00
|
|
|
cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox})
|
|
|
|
})
|
|
|
|
t.Run("sbox_inputs", func(t *testing.T) {
|
|
|
|
outDir := "out/soong/.intermediates/foo_sbox_inputs"
|
|
|
|
outFile := filepath.Join(outDir, "gen/foo_sbox_inputs")
|
|
|
|
depFile := filepath.Join(outDir, "gen/foo_sbox_inputs.d")
|
|
|
|
rspFile := filepath.Join(outDir, "rsp")
|
|
|
|
rspFile2 := filepath.Join(outDir, "rsp2")
|
2020-11-12 17:29:30 +01:00
|
|
|
manifest := filepath.Join(outDir, "sbox.textproto")
|
2021-03-22 11:09:02 +01:00
|
|
|
sbox := filepath.Join("out", "soong", "host", result.Config.PrebuiltOS(), "bin/sbox")
|
|
|
|
sandboxPath := shared.TempDirForOutDir("out/soong")
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
cmd := `rm -rf ` + outDir + `/gen && ` +
|
|
|
|
sbox + ` --sandbox-path ` + sandboxPath + ` --manifest ` + manifest
|
2019-04-12 20:11:38 +02:00
|
|
|
|
2021-03-20 00:22:12 +01:00
|
|
|
module := result.ModuleForTests("foo_sbox_inputs", "")
|
2021-03-29 01:42:57 +02:00
|
|
|
check(t, module.Output("gen/foo_sbox_inputs"), module.Output(rspFile2),
|
2021-03-20 00:22:12 +01:00
|
|
|
cmd, outFile, depFile, rspFile, rspFile2, false, []string{manifest}, []string{sbox})
|
2019-02-25 23:54:28 +01:00
|
|
|
})
|
|
|
|
t.Run("singleton", func(t *testing.T) {
|
2021-03-22 11:09:02 +01:00
|
|
|
outFile := filepath.Join("out/soong/singleton/gen/baz")
|
2021-03-20 00:22:12 +01:00
|
|
|
rspFile := filepath.Join("out/soong/singleton/rsp")
|
|
|
|
rspFile2 := filepath.Join("out/soong/singleton/rsp2")
|
|
|
|
singleton := result.SingletonForTests("rule_builder_test")
|
2021-03-29 01:42:57 +02:00
|
|
|
check(t, singleton.Rule("rule"), singleton.Output(rspFile2),
|
2021-04-21 20:32:19 +02:00
|
|
|
"cp in "+outFile+" @"+rspFile+" @"+rspFile2,
|
2021-03-20 00:22:12 +01:00
|
|
|
outFile, outFile+".d", rspFile, rspFile2, true, nil, nil)
|
2019-02-25 23:54:28 +01:00
|
|
|
})
|
2019-01-31 02:32:39 +01:00
|
|
|
}
|
2019-07-11 19:59:15 +02:00
|
|
|
|
2020-11-14 01:23:53 +01:00
|
|
|
func TestRuleBuilderHashInputs(t *testing.T) {
|
|
|
|
// The basic idea here is to verify that the command (in the case of a
|
|
|
|
// non-sbox rule) or the sbox textproto manifest contain a hash of the
|
|
|
|
// inputs.
|
|
|
|
|
|
|
|
// By including a hash of the inputs, we cause the rule to re-run if
|
|
|
|
// the list of inputs changes because the command line or a dependency
|
|
|
|
// changes.
|
|
|
|
|
2021-04-21 20:32:19 +02:00
|
|
|
hashOf := func(s string) string {
|
|
|
|
sum := sha256.Sum256([]byte(s))
|
|
|
|
return hex.EncodeToString(sum[:])
|
|
|
|
}
|
|
|
|
|
2020-11-14 01:23:53 +01:00
|
|
|
bp := `
|
|
|
|
rule_builder_test {
|
|
|
|
name: "hash0",
|
|
|
|
srcs: ["in1.txt", "in2.txt"],
|
|
|
|
}
|
|
|
|
rule_builder_test {
|
|
|
|
name: "hash0_sbox",
|
|
|
|
srcs: ["in1.txt", "in2.txt"],
|
|
|
|
sbox: true,
|
|
|
|
}
|
|
|
|
rule_builder_test {
|
|
|
|
name: "hash1",
|
|
|
|
srcs: ["in1.txt", "in2.txt", "in3.txt"],
|
|
|
|
}
|
|
|
|
rule_builder_test {
|
|
|
|
name: "hash1_sbox",
|
|
|
|
srcs: ["in1.txt", "in2.txt", "in3.txt"],
|
|
|
|
sbox: true,
|
|
|
|
}
|
|
|
|
`
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
expectedHash string
|
|
|
|
}{
|
|
|
|
{
|
2021-04-21 20:32:19 +02:00
|
|
|
name: "hash0",
|
|
|
|
expectedHash: hashOf("implicit\nin1.txt\nin2.txt"),
|
2020-11-14 01:23:53 +01:00
|
|
|
},
|
|
|
|
{
|
2021-04-21 20:32:19 +02:00
|
|
|
name: "hash1",
|
|
|
|
expectedHash: hashOf("implicit\nin1.txt\nin2.txt\nin3.txt"),
|
2020-11-14 01:23:53 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:36:14 +01:00
|
|
|
result := GroupFixturePreparers(
|
2021-03-16 22:51:29 +01:00
|
|
|
prepareForRuleBuilderTest,
|
|
|
|
FixtureWithRootAndroidBp(bp),
|
2021-03-20 01:36:14 +01:00
|
|
|
).RunTest(t)
|
2020-11-14 01:23:53 +01:00
|
|
|
|
|
|
|
for _, test := range testcases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Run("sbox", func(t *testing.T) {
|
2021-03-16 22:51:29 +01:00
|
|
|
gen := result.ModuleForTests(test.name+"_sbox", "")
|
2020-11-12 17:29:30 +01:00
|
|
|
manifest := RuleBuilderSboxProtoForTests(t, gen.Output("sbox.textproto"))
|
|
|
|
hash := manifest.Commands[0].GetInputHash()
|
|
|
|
|
2021-03-16 22:51:29 +01:00
|
|
|
AssertStringEquals(t, "hash", test.expectedHash, hash)
|
2020-11-14 01:23:53 +01:00
|
|
|
})
|
|
|
|
t.Run("", func(t *testing.T) {
|
2021-03-16 22:51:29 +01:00
|
|
|
gen := result.ModuleForTests(test.name+"", "")
|
2020-11-12 17:29:30 +01:00
|
|
|
command := gen.Output("gen/" + test.name).RuleParams.Command
|
2020-11-14 01:23:53 +01:00
|
|
|
if g, w := command, " # hash of input list: "+test.expectedHash; !strings.HasSuffix(g, w) {
|
|
|
|
t.Errorf("Expected command line to end with %q, got %q", w, g)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|