2018-10-05 08:28:25 +02:00
|
|
|
// Copyright 2018 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 (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-03-18 20:12:48 +01:00
|
|
|
"reflect"
|
2018-10-05 08:28:25 +02:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"android/soong/android"
|
2019-03-18 20:12:48 +01:00
|
|
|
|
|
|
|
"github.com/google/blueprint/proptools"
|
2018-10-05 08:28:25 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var buildDir string
|
|
|
|
|
|
|
|
func setUp() {
|
|
|
|
var err error
|
2018-10-23 20:27:50 +02:00
|
|
|
buildDir, err = ioutil.TempDir("", "genrule_test")
|
2018-10-05 08:28:25 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tearDown() {
|
|
|
|
os.RemoveAll(buildDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
run := func() int {
|
|
|
|
setUp()
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
return m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(run())
|
|
|
|
}
|
|
|
|
|
2019-12-14 05:41:13 +01:00
|
|
|
func testContext(config android.Config) *android.TestContext {
|
2018-10-05 08:28:25 +02:00
|
|
|
|
2020-10-30 01:09:13 +01:00
|
|
|
ctx := android.NewTestArchContext(config)
|
2019-11-23 00:25:03 +01:00
|
|
|
ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
|
|
|
|
ctx.RegisterModuleType("tool", toolFactory)
|
2020-01-16 16:12:04 +01:00
|
|
|
|
2020-11-11 03:12:15 +01:00
|
|
|
RegisterGenruleBuildComponents(ctx)
|
2020-01-16 16:12:04 +01:00
|
|
|
|
2018-12-10 17:13:18 +01:00
|
|
|
ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
|
2020-10-30 01:09:13 +01:00
|
|
|
ctx.Register()
|
2018-10-05 08:28:25 +02:00
|
|
|
|
2019-12-14 05:41:13 +01:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig(bp string, fs map[string][]byte) android.Config {
|
2018-10-05 08:28:25 +02:00
|
|
|
bp += `
|
|
|
|
tool {
|
|
|
|
name: "tool",
|
|
|
|
}
|
|
|
|
|
|
|
|
filegroup {
|
|
|
|
name: "tool_files",
|
|
|
|
srcs: [
|
|
|
|
"tool_file1",
|
|
|
|
"tool_file2",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
filegroup {
|
|
|
|
name: "1tool_file",
|
|
|
|
srcs: [
|
|
|
|
"tool_file1",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
filegroup {
|
|
|
|
name: "ins",
|
|
|
|
srcs: [
|
|
|
|
"in1",
|
|
|
|
"in2",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
filegroup {
|
|
|
|
name: "1in",
|
|
|
|
srcs: [
|
|
|
|
"in1",
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
filegroup {
|
|
|
|
name: "empty",
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
mockFS := map[string][]byte{
|
|
|
|
"tool": nil,
|
|
|
|
"tool_file1": nil,
|
|
|
|
"tool_file2": nil,
|
|
|
|
"in1": nil,
|
|
|
|
"in2": nil,
|
2019-09-24 00:55:30 +02:00
|
|
|
"in1.txt": nil,
|
|
|
|
"in2.txt": nil,
|
|
|
|
"in3.txt": nil,
|
2018-10-05 08:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range fs {
|
|
|
|
mockFS[k] = v
|
|
|
|
}
|
|
|
|
|
2019-12-14 05:41:13 +01:00
|
|
|
return android.TestArchConfig(buildDir, nil, bp, mockFS)
|
2018-10-05 08:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenruleCmd(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
prop string
|
|
|
|
|
2019-03-18 20:12:48 +01:00
|
|
|
allowMissingDependencies bool
|
|
|
|
|
2018-10-05 08:28:25 +02:00
|
|
|
err string
|
|
|
|
expect string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty location tool",
|
|
|
|
prop: `
|
|
|
|
tools: ["tool"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
2018-10-05 08:29:14 +02:00
|
|
|
{
|
|
|
|
name: "empty location tool2",
|
|
|
|
prop: `
|
|
|
|
tools: [":tool"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
{
|
|
|
|
name: "empty location tool file",
|
|
|
|
prop: `
|
|
|
|
tool_files: ["tool_file1"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty location tool file fg",
|
|
|
|
prop: `
|
|
|
|
tool_files: [":1tool_file"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty location tool and tool file",
|
|
|
|
prop: `
|
|
|
|
tools: ["tool"],
|
|
|
|
tool_files: ["tool_file1"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tool",
|
|
|
|
prop: `
|
|
|
|
tools: ["tool"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location tool) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
2018-10-05 08:29:14 +02:00
|
|
|
{
|
|
|
|
name: "tool2",
|
|
|
|
prop: `
|
|
|
|
tools: [":tool"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location :tool) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
{
|
|
|
|
name: "tool file",
|
|
|
|
prop: `
|
|
|
|
tool_files: ["tool_file1"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location tool_file1) > $(out)",
|
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tool file fg",
|
|
|
|
prop: `
|
|
|
|
tool_files: [":1tool_file"],
|
|
|
|
out: ["out"],
|
2018-10-05 08:29:14 +02:00
|
|
|
cmd: "$(location :1tool_file) > $(out)",
|
2018-10-05 08:28:25 +02:00
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tool files",
|
|
|
|
prop: `
|
|
|
|
tool_files: [":tool_files"],
|
|
|
|
out: ["out"],
|
2018-10-05 08:29:14 +02:00
|
|
|
cmd: "$(locations :tool_files) > $(out)",
|
2018-10-05 08:28:25 +02:00
|
|
|
`,
|
2020-11-25 01:32:22 +01:00
|
|
|
expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 __SBOX_SANDBOX_DIR__/tools/src/tool_file2 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "in1",
|
|
|
|
prop: `
|
|
|
|
srcs: ["in1"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(in) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "in1 fg",
|
|
|
|
prop: `
|
|
|
|
srcs: [":1in"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(in) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ins",
|
|
|
|
prop: `
|
|
|
|
srcs: ["in1", "in2"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(in) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 in2 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ins fg",
|
|
|
|
prop: `
|
|
|
|
srcs: [":ins"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(in) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 in2 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
2018-10-05 08:29:14 +02:00
|
|
|
{
|
|
|
|
name: "location in1",
|
|
|
|
prop: `
|
|
|
|
srcs: ["in1"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(location in1) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "location in1 fg",
|
|
|
|
prop: `
|
|
|
|
srcs: [":1in"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(location :1in) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "location ins",
|
|
|
|
prop: `
|
|
|
|
srcs: ["in1", "in2"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(location in1) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "location ins fg",
|
|
|
|
prop: `
|
|
|
|
srcs: [":ins"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(locations :ins) > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat in1 in2 > __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
{
|
|
|
|
name: "outs",
|
|
|
|
prop: `
|
|
|
|
out: ["out", "out2"],
|
|
|
|
cmd: "echo foo > $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "echo foo > __SBOX_SANDBOX_DIR__/out/out __SBOX_SANDBOX_DIR__/out/out2",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
2018-10-05 08:29:14 +02:00
|
|
|
{
|
|
|
|
name: "location out",
|
|
|
|
prop: `
|
|
|
|
out: ["out", "out2"],
|
|
|
|
cmd: "echo foo > $(location out2)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "echo foo > __SBOX_SANDBOX_DIR__/out/out2",
|
2018-10-05 08:29:14 +02:00
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
{
|
|
|
|
name: "depfile",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
depfile: true,
|
|
|
|
cmd: "echo foo > $(out) && touch $(depfile)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "echo foo > __SBOX_SANDBOX_DIR__/out/out && touch __SBOX_DEPFILE__",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gendir",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo foo > $(genDir)/foo && cp $(genDir)/foo $(out)",
|
|
|
|
`,
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "echo foo > __SBOX_SANDBOX_DIR__/out/foo && cp __SBOX_SANDBOX_DIR__/out/foo __SBOX_SANDBOX_DIR__/out/out",
|
2018-10-05 08:28:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "error empty location",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
|
|
|
err: "at least one `tools` or `tool_files` is required if $(location) is used",
|
|
|
|
},
|
2018-10-05 08:29:14 +02:00
|
|
|
{
|
|
|
|
name: "error empty location no files",
|
|
|
|
prop: `
|
|
|
|
tool_files: [":empty"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
|
|
|
err: `default label ":empty" has no files`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error empty location multiple files",
|
|
|
|
prop: `
|
|
|
|
tool_files: [":tool_files"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location) > $(out)",
|
|
|
|
`,
|
|
|
|
err: `default label ":tool_files" has multiple files`,
|
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
{
|
|
|
|
name: "error location",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo foo > $(location missing)",
|
|
|
|
`,
|
|
|
|
err: `unknown location label "missing"`,
|
|
|
|
},
|
2018-10-05 08:29:14 +02:00
|
|
|
{
|
|
|
|
name: "error locations",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo foo > $(locations missing)",
|
|
|
|
`,
|
|
|
|
err: `unknown locations label "missing"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error location no files",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
srcs: [":empty"],
|
|
|
|
cmd: "echo $(location :empty) > $(out)",
|
|
|
|
`,
|
|
|
|
err: `label ":empty" has no files`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error locations no files",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
srcs: [":empty"],
|
|
|
|
cmd: "echo $(locations :empty) > $(out)",
|
|
|
|
`,
|
|
|
|
err: `label ":empty" has no files`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error location multiple files",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
srcs: [":ins"],
|
|
|
|
cmd: "echo $(location :ins) > $(out)",
|
|
|
|
`,
|
|
|
|
err: `label ":ins" has multiple files`,
|
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
{
|
|
|
|
name: "error variable",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
srcs: ["in1"],
|
|
|
|
cmd: "echo $(foo) > $(out)",
|
|
|
|
`,
|
|
|
|
err: `unknown variable '$(foo)'`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error depfile",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo foo > $(out) && touch $(depfile)",
|
|
|
|
`,
|
|
|
|
err: "$(depfile) used without depfile property",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error no depfile",
|
|
|
|
prop: `
|
|
|
|
out: ["out"],
|
|
|
|
depfile: true,
|
|
|
|
cmd: "echo foo > $(out)",
|
|
|
|
`,
|
|
|
|
err: "specified depfile=true but did not include a reference to '${depfile}' in cmd",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error no out",
|
|
|
|
prop: `
|
|
|
|
cmd: "echo foo > $(out)",
|
|
|
|
`,
|
|
|
|
err: "must have at least one output file",
|
|
|
|
},
|
2019-03-18 20:12:48 +01:00
|
|
|
{
|
|
|
|
name: "srcs allow missing dependencies",
|
|
|
|
prop: `
|
|
|
|
srcs: [":missing"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "cat $(location :missing) > $(out)",
|
|
|
|
`,
|
|
|
|
|
|
|
|
allowMissingDependencies: true,
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "cat ***missing srcs :missing*** > __SBOX_SANDBOX_DIR__/out/out",
|
2019-03-18 20:12:48 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "tool allow missing dependencies",
|
|
|
|
prop: `
|
|
|
|
tools: [":missing"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "$(location :missing) > $(out)",
|
|
|
|
`,
|
|
|
|
|
|
|
|
allowMissingDependencies: true,
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
expect: "***missing tool :missing*** > __SBOX_SANDBOX_DIR__/out/out",
|
2019-03-18 20:12:48 +01:00
|
|
|
},
|
2018-10-05 08:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testcases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
bp := "genrule {\n"
|
|
|
|
bp += "name: \"gen\",\n"
|
|
|
|
bp += test.prop
|
|
|
|
bp += "}\n"
|
|
|
|
|
2019-12-14 05:41:13 +01:00
|
|
|
config := testConfig(bp, nil)
|
2019-03-18 20:12:48 +01:00
|
|
|
config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(test.allowMissingDependencies)
|
|
|
|
|
2019-12-14 05:41:13 +01:00
|
|
|
ctx := testContext(config)
|
2019-03-18 20:12:48 +01:00
|
|
|
ctx.SetAllowMissingDependencies(test.allowMissingDependencies)
|
2018-10-05 08:28:25 +02:00
|
|
|
|
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
|
|
if errs == nil {
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
}
|
|
|
|
if errs == nil && test.err != "" {
|
|
|
|
t.Fatalf("want error %q, got no error", test.err)
|
|
|
|
} else if errs != nil && test.err == "" {
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
} else if test.err != "" {
|
|
|
|
if len(errs) != 1 {
|
|
|
|
t.Errorf("want 1 error, got %d errors:", len(errs))
|
|
|
|
for _, err := range errs {
|
|
|
|
t.Errorf(" %s", err.Error())
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if !strings.Contains(errs[0].Error(), test.err) {
|
|
|
|
t.Fatalf("want %q, got %q", test.err, errs[0].Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gen := ctx.ModuleForTests("gen", "").Module().(*Module)
|
2020-11-14 01:23:53 +01:00
|
|
|
if g, w := gen.rawCommands[0], test.expect; w != g {
|
2019-03-18 20:12:48 +01:00
|
|
|
t.Errorf("want %q, got %q", w, g)
|
2018-10-05 08:28:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-09-24 00:55:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 00:55:10 +01:00
|
|
|
func TestGenruleHashInputs(t *testing.T) {
|
|
|
|
|
|
|
|
// The basic idea here is to verify that the sbox command (which is
|
|
|
|
// in the Command field of the generate rule) contains a hash of the
|
|
|
|
// inputs, but only if $(in) is not referenced in the genrule cmd
|
|
|
|
// property.
|
|
|
|
|
|
|
|
// By including a hash of the inputs, we cause the rule to re-run if
|
|
|
|
// the list of inputs changes (because the sbox command changes).
|
|
|
|
|
|
|
|
// However, if the genrule cmd property already contains $(in), then
|
|
|
|
// the dependency is already expressed, so we don't need to include the
|
|
|
|
// hash in that case.
|
|
|
|
|
|
|
|
bp := `
|
|
|
|
genrule {
|
|
|
|
name: "hash0",
|
|
|
|
srcs: ["in1.txt", "in2.txt"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo foo > $(out)",
|
|
|
|
}
|
|
|
|
genrule {
|
|
|
|
name: "hash1",
|
|
|
|
srcs: ["*.txt"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo bar > $(out)",
|
|
|
|
}
|
|
|
|
genrule {
|
|
|
|
name: "hash2",
|
|
|
|
srcs: ["*.txt"],
|
|
|
|
out: ["out"],
|
|
|
|
cmd: "echo $(in) > $(out)",
|
|
|
|
}
|
|
|
|
`
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
expectedHash string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "hash0",
|
2020-11-14 01:23:53 +01:00
|
|
|
// sha256 value obtained from: echo -en 'in1.txt\nin2.txt' | sha256sum
|
|
|
|
expectedHash: "18da75b9b1cc74b09e365b4ca2e321b5d618f438cc632b387ad9dc2ab4b20e9d",
|
2020-02-14 00:55:10 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hash1",
|
2020-11-14 01:23:53 +01:00
|
|
|
// sha256 value obtained from: echo -en 'in1.txt\nin2.txt\nin3.txt' | sha256sum
|
|
|
|
expectedHash: "a38d432a4b19df93140e1f1fe26c97ff0387dae01fe506412b47208f0595fb45",
|
2020-02-14 00:55:10 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hash2",
|
2020-11-14 01:23:53 +01:00
|
|
|
// sha256 value obtained from: echo -en 'in1.txt\nin2.txt\nin3.txt' | sha256sum
|
|
|
|
expectedHash: "a38d432a4b19df93140e1f1fe26c97ff0387dae01fe506412b47208f0595fb45",
|
2020-02-14 00:55:10 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testConfig(bp, nil)
|
|
|
|
ctx := testContext(config)
|
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
|
|
if errs == nil {
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
}
|
|
|
|
if errs != nil {
|
|
|
|
t.Fatal(errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testcases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
gen := ctx.ModuleForTests(test.name, "")
|
2020-11-12 17:29:30 +01:00
|
|
|
manifest := android.RuleBuilderSboxProtoForTests(t, gen.Output("genrule.sbox.textproto"))
|
|
|
|
hash := manifest.Commands[0].GetInputHash()
|
2020-02-14 00:55:10 +01:00
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
if g, w := hash, test.expectedHash; g != w {
|
|
|
|
t.Errorf("Expected has %q, got %q", w, g)
|
2020-02-14 00:55:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 00:55:30 +02:00
|
|
|
func TestGenSrcs(t *testing.T) {
|
|
|
|
testcases := []struct {
|
|
|
|
name string
|
|
|
|
prop string
|
|
|
|
|
|
|
|
allowMissingDependencies bool
|
|
|
|
|
|
|
|
err string
|
|
|
|
cmds []string
|
|
|
|
deps []string
|
|
|
|
files []string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "gensrcs",
|
|
|
|
prop: `
|
|
|
|
tools: ["tool"],
|
|
|
|
srcs: ["in1.txt", "in2.txt"],
|
|
|
|
cmd: "$(location) $(in) > $(out)",
|
|
|
|
`,
|
|
|
|
cmds: []string{
|
2020-11-25 01:32:22 +01:00
|
|
|
"bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in1.txt > __SBOX_SANDBOX_DIR__/out/in1.h' && bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in2.txt > __SBOX_SANDBOX_DIR__/out/in2.h'",
|
2019-09-24 00:55:30 +02:00
|
|
|
},
|
|
|
|
deps: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h"},
|
|
|
|
files: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "shards",
|
|
|
|
prop: `
|
|
|
|
tools: ["tool"],
|
|
|
|
srcs: ["in1.txt", "in2.txt", "in3.txt"],
|
|
|
|
cmd: "$(location) $(in) > $(out)",
|
|
|
|
shard_size: 2,
|
|
|
|
`,
|
|
|
|
cmds: []string{
|
2020-11-25 01:32:22 +01:00
|
|
|
"bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in1.txt > __SBOX_SANDBOX_DIR__/out/in1.h' && bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in2.txt > __SBOX_SANDBOX_DIR__/out/in2.h'",
|
|
|
|
"bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in3.txt > __SBOX_SANDBOX_DIR__/out/in3.h'",
|
2019-09-24 00:55:30 +02:00
|
|
|
},
|
|
|
|
deps: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h", buildDir + "/.intermediates/gen/gen/gensrcs/in3.h"},
|
|
|
|
files: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h", buildDir + "/.intermediates/gen/gen/gensrcs/in3.h"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testcases {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
bp := "gensrcs {\n"
|
|
|
|
bp += `name: "gen",` + "\n"
|
|
|
|
bp += `output_extension: "h",` + "\n"
|
|
|
|
bp += test.prop
|
|
|
|
bp += "}\n"
|
|
|
|
|
2019-12-14 05:41:13 +01:00
|
|
|
config := testConfig(bp, nil)
|
|
|
|
ctx := testContext(config)
|
2019-09-24 00:55:30 +02:00
|
|
|
|
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
|
|
if errs == nil {
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
}
|
|
|
|
if errs == nil && test.err != "" {
|
|
|
|
t.Fatalf("want error %q, got no error", test.err)
|
|
|
|
} else if errs != nil && test.err == "" {
|
|
|
|
android.FailIfErrored(t, errs)
|
|
|
|
} else if test.err != "" {
|
|
|
|
if len(errs) != 1 {
|
|
|
|
t.Errorf("want 1 error, got %d errors:", len(errs))
|
|
|
|
for _, err := range errs {
|
|
|
|
t.Errorf(" %s", err.Error())
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
if !strings.Contains(errs[0].Error(), test.err) {
|
|
|
|
t.Fatalf("want %q, got %q", test.err, errs[0].Error())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
gen := ctx.ModuleForTests("gen", "").Module().(*Module)
|
|
|
|
if g, w := gen.rawCommands, test.cmds; !reflect.DeepEqual(w, g) {
|
|
|
|
t.Errorf("want %q, got %q", w, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g, w := gen.outputDeps.Strings(), test.deps; !reflect.DeepEqual(w, g) {
|
|
|
|
t.Errorf("want deps %q, got %q", w, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
if g, w := gen.outputFiles.Strings(), test.files; !reflect.DeepEqual(w, g) {
|
|
|
|
t.Errorf("want files %q, got %q", w, g)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-10-05 08:28:25 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:13:18 +01:00
|
|
|
func TestGenruleDefaults(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
genrule_defaults {
|
|
|
|
name: "gen_defaults1",
|
|
|
|
cmd: "cp $(in) $(out)",
|
|
|
|
}
|
|
|
|
|
|
|
|
genrule_defaults {
|
|
|
|
name: "gen_defaults2",
|
|
|
|
srcs: ["in1"],
|
|
|
|
}
|
|
|
|
|
|
|
|
genrule {
|
|
|
|
name: "gen",
|
|
|
|
out: ["out"],
|
|
|
|
defaults: ["gen_defaults1", "gen_defaults2"],
|
|
|
|
}
|
|
|
|
`
|
2019-12-14 05:41:13 +01:00
|
|
|
config := testConfig(bp, nil)
|
|
|
|
ctx := testContext(config)
|
2018-12-10 17:13:18 +01:00
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
|
|
if errs == nil {
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
}
|
|
|
|
if errs != nil {
|
|
|
|
t.Fatal(errs)
|
|
|
|
}
|
|
|
|
gen := ctx.ModuleForTests("gen", "").Module().(*Module)
|
|
|
|
|
2020-11-12 17:29:30 +01:00
|
|
|
expectedCmd := "cp in1 __SBOX_SANDBOX_DIR__/out/out"
|
2019-09-24 00:55:30 +02:00
|
|
|
if gen.rawCommands[0] != expectedCmd {
|
|
|
|
t.Errorf("Expected cmd: %q, actual: %q", expectedCmd, gen.rawCommands[0])
|
2018-12-10 17:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedSrcs := []string{"in1"}
|
|
|
|
if !reflect.DeepEqual(expectedSrcs, gen.properties.Srcs) {
|
|
|
|
t.Errorf("Expected srcs: %q, actual: %q", expectedSrcs, gen.properties.Srcs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 08:23:17 +02:00
|
|
|
func TestGenruleWithBazel(t *testing.T) {
|
|
|
|
bp := `
|
|
|
|
genrule {
|
|
|
|
name: "foo",
|
|
|
|
out: ["one.txt", "two.txt"],
|
2020-10-14 22:22:37 +02:00
|
|
|
bazel_module: { label: "//foo/bar:bar" },
|
2020-09-29 08:23:17 +02:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
config := testConfig(bp, nil)
|
|
|
|
config.BazelContext = android.MockBazelContext{
|
|
|
|
AllFiles: map[string][]string{
|
|
|
|
"//foo/bar:bar": []string{"bazelone.txt", "bazeltwo.txt"}}}
|
|
|
|
|
|
|
|
ctx := testContext(config)
|
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
|
|
if errs == nil {
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
|
|
}
|
|
|
|
if errs != nil {
|
|
|
|
t.Fatal(errs)
|
|
|
|
}
|
|
|
|
gen := ctx.ModuleForTests("foo", "").Module().(*Module)
|
|
|
|
|
2020-12-10 23:19:18 +01:00
|
|
|
expectedOutputFiles := []string{"outputbase/execroot/__main__/bazelone.txt",
|
|
|
|
"outputbase/execroot/__main__/bazeltwo.txt"}
|
2020-09-29 08:23:17 +02:00
|
|
|
if !reflect.DeepEqual(gen.outputFiles.Strings(), expectedOutputFiles) {
|
|
|
|
t.Errorf("Expected output files: %q, actual: %q", expectedOutputFiles, gen.outputFiles)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gen.outputDeps.Strings(), expectedOutputFiles) {
|
|
|
|
t.Errorf("Expected output deps: %q, actual: %q", expectedOutputFiles, gen.outputDeps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 08:28:25 +02:00
|
|
|
type testTool struct {
|
|
|
|
android.ModuleBase
|
|
|
|
outputFile android.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func toolFactory() android.Module {
|
|
|
|
module := &testTool{}
|
|
|
|
android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testTool) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2020-11-25 01:32:22 +01:00
|
|
|
t.outputFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), ctx.ModuleName(), android.PathForOutput(ctx, ctx.ModuleName()))
|
2018-10-05 08:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testTool) HostToolPath() android.OptionalPath {
|
|
|
|
return android.OptionalPathForPath(t.outputFile)
|
|
|
|
}
|
|
|
|
|
2019-03-29 03:30:56 +01:00
|
|
|
var _ android.HostToolProvider = (*testTool)(nil)
|