blob: df0f25640691932e9699fb9215446a159cefe2de [file] [log] [blame]
Colin Crossfeec25b2019-01-30 17:32:39 -08001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package android
16
17import (
Colin Cross758290d2019-02-01 16:42:32 -080018 "fmt"
Colin Crossfeec25b2019-01-30 17:32:39 -080019 "io/ioutil"
20 "os"
21 "path/filepath"
22 "reflect"
Colin Cross758290d2019-02-01 16:42:32 -080023 "strings"
Colin Crossfeec25b2019-01-30 17:32:39 -080024 "testing"
Dan Willemsen633c5022019-04-12 11:11:38 -070025
26 "github.com/google/blueprint"
27
28 "android/soong/shared"
Colin Crossfeec25b2019-01-30 17:32:39 -080029)
30
Colin Cross69f59a32019-02-15 10:39:37 -080031func pathContext() PathContext {
32 return PathContextForTesting(TestConfig("out", nil),
33 map[string][]byte{
34 "ld": nil,
35 "a.o": nil,
36 "b.o": nil,
37 "cp": nil,
38 "a": nil,
39 "b": nil,
40 "ls": nil,
41 "turbine": nil,
42 "java": nil,
43 })
44}
45
Colin Cross758290d2019-02-01 16:42:32 -080046func ExampleRuleBuilder() {
47 rule := NewRuleBuilder()
48
Colin Cross69f59a32019-02-15 10:39:37 -080049 ctx := pathContext()
50
51 rule.Command().
52 Tool(PathForSource(ctx, "ld")).
53 Inputs(PathsForTesting("a.o", "b.o")).
54 FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -080055 rule.Command().Text("echo success")
56
57 // To add the command to the build graph:
58 // rule.Build(pctx, ctx, "link", "link")
59
60 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
61 fmt.Printf("tools: %q\n", rule.Tools())
62 fmt.Printf("inputs: %q\n", rule.Inputs())
63 fmt.Printf("outputs: %q\n", rule.Outputs())
64
65 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -080066 // commands: "ld a.o b.o -o out/linked && echo success"
Colin Cross758290d2019-02-01 16:42:32 -080067 // tools: ["ld"]
68 // inputs: ["a.o" "b.o"]
Colin Cross69f59a32019-02-15 10:39:37 -080069 // outputs: ["out/linked"]
Colin Cross758290d2019-02-01 16:42:32 -080070}
71
Colin Cross5cb5b092019-02-02 21:25:18 -080072func ExampleRuleBuilder_Temporary() {
73 rule := NewRuleBuilder()
74
Colin Cross69f59a32019-02-15 10:39:37 -080075 ctx := pathContext()
76
77 rule.Command().
78 Tool(PathForSource(ctx, "cp")).
79 Input(PathForSource(ctx, "a")).
80 Output(PathForOutput(ctx, "b"))
81 rule.Command().
82 Tool(PathForSource(ctx, "cp")).
83 Input(PathForOutput(ctx, "b")).
84 Output(PathForOutput(ctx, "c"))
85 rule.Temporary(PathForOutput(ctx, "b"))
Colin Cross5cb5b092019-02-02 21:25:18 -080086
87 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
88 fmt.Printf("tools: %q\n", rule.Tools())
89 fmt.Printf("inputs: %q\n", rule.Inputs())
90 fmt.Printf("outputs: %q\n", rule.Outputs())
91
92 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -080093 // commands: "cp a out/b && cp out/b out/c"
Colin Cross5cb5b092019-02-02 21:25:18 -080094 // tools: ["cp"]
95 // inputs: ["a"]
Colin Cross69f59a32019-02-15 10:39:37 -080096 // outputs: ["out/c"]
Colin Cross5cb5b092019-02-02 21:25:18 -080097}
98
99func ExampleRuleBuilder_DeleteTemporaryFiles() {
100 rule := NewRuleBuilder()
101
Colin Cross69f59a32019-02-15 10:39:37 -0800102 ctx := pathContext()
103
104 rule.Command().
105 Tool(PathForSource(ctx, "cp")).
106 Input(PathForSource(ctx, "a")).
107 Output(PathForOutput(ctx, "b"))
108 rule.Command().
109 Tool(PathForSource(ctx, "cp")).
110 Input(PathForOutput(ctx, "b")).
111 Output(PathForOutput(ctx, "c"))
112 rule.Temporary(PathForOutput(ctx, "b"))
Colin Cross5cb5b092019-02-02 21:25:18 -0800113 rule.DeleteTemporaryFiles()
114
115 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
116 fmt.Printf("tools: %q\n", rule.Tools())
117 fmt.Printf("inputs: %q\n", rule.Inputs())
118 fmt.Printf("outputs: %q\n", rule.Outputs())
119
120 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -0800121 // commands: "cp a out/b && cp out/b out/c && rm -f out/b"
Colin Cross5cb5b092019-02-02 21:25:18 -0800122 // tools: ["cp"]
123 // inputs: ["a"]
Colin Cross69f59a32019-02-15 10:39:37 -0800124 // outputs: ["out/c"]
Colin Cross5cb5b092019-02-02 21:25:18 -0800125}
126
Colin Crossdeabb942019-02-11 14:11:09 -0800127func ExampleRuleBuilder_Installs() {
128 rule := NewRuleBuilder()
129
Colin Cross69f59a32019-02-15 10:39:37 -0800130 ctx := pathContext()
131
132 out := PathForOutput(ctx, "linked")
133
134 rule.Command().
135 Tool(PathForSource(ctx, "ld")).
136 Inputs(PathsForTesting("a.o", "b.o")).
137 FlagWithOutput("-o ", out)
138 rule.Install(out, "/bin/linked")
139 rule.Install(out, "/sbin/linked")
Colin Crossdeabb942019-02-11 14:11:09 -0800140
141 fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String())
142
143 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -0800144 // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked"
Colin Crossdeabb942019-02-11 14:11:09 -0800145}
146
Colin Cross758290d2019-02-01 16:42:32 -0800147func ExampleRuleBuilderCommand() {
148 rule := NewRuleBuilder()
149
Colin Cross69f59a32019-02-15 10:39:37 -0800150 ctx := pathContext()
151
Colin Cross758290d2019-02-01 16:42:32 -0800152 // chained
Colin Cross69f59a32019-02-15 10:39:37 -0800153 rule.Command().
154 Tool(PathForSource(ctx, "ld")).
155 Inputs(PathsForTesting("a.o", "b.o")).
156 FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800157
158 // unchained
159 cmd := rule.Command()
Colin Cross69f59a32019-02-15 10:39:37 -0800160 cmd.Tool(PathForSource(ctx, "ld"))
161 cmd.Inputs(PathsForTesting("a.o", "b.o"))
162 cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800163
164 // mixed:
Colin Cross69f59a32019-02-15 10:39:37 -0800165 cmd = rule.Command().Tool(PathForSource(ctx, "ld"))
166 cmd.Inputs(PathsForTesting("a.o", "b.o"))
167 cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800168}
169
170func ExampleRuleBuilderCommand_Flag() {
Colin Cross69f59a32019-02-15 10:39:37 -0800171 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800172 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800173 Tool(PathForSource(ctx, "ls")).Flag("-l"))
Colin Cross758290d2019-02-01 16:42:32 -0800174 // Output:
175 // ls -l
176}
177
Colin Cross92b7d582019-03-29 15:32:51 -0700178func ExampleRuleBuilderCommand_Flags() {
179 ctx := pathContext()
180 fmt.Println(NewRuleBuilder().Command().
181 Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"}))
182 // Output:
183 // ls -l -a
184}
185
Colin Cross758290d2019-02-01 16:42:32 -0800186func ExampleRuleBuilderCommand_FlagWithArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800187 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800188 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800189 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800190 FlagWithArg("--sort=", "time"))
191 // Output:
192 // ls --sort=time
193}
194
Colin Crossc7ed0042019-02-11 14:11:09 -0800195func ExampleRuleBuilderCommand_FlagForEachArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800196 ctx := pathContext()
Colin Crossc7ed0042019-02-11 14:11:09 -0800197 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800198 Tool(PathForSource(ctx, "ls")).
Colin Crossc7ed0042019-02-11 14:11:09 -0800199 FlagForEachArg("--sort=", []string{"time", "size"}))
200 // Output:
201 // ls --sort=time --sort=size
202}
203
Colin Cross758290d2019-02-01 16:42:32 -0800204func ExampleRuleBuilderCommand_FlagForEachInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800205 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800206 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800207 Tool(PathForSource(ctx, "turbine")).
208 FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
Colin Cross758290d2019-02-01 16:42:32 -0800209 // Output:
210 // turbine --classpath a.jar --classpath b.jar
211}
212
213func ExampleRuleBuilderCommand_FlagWithInputList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800214 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800215 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800216 Tool(PathForSource(ctx, "java")).
217 FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
Colin Cross758290d2019-02-01 16:42:32 -0800218 // Output:
219 // java -classpath=a.jar:b.jar
220}
221
222func ExampleRuleBuilderCommand_FlagWithInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800223 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800224 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800225 Tool(PathForSource(ctx, "java")).
226 FlagWithInput("-classpath=", PathForSource(ctx, "a")))
Colin Cross758290d2019-02-01 16:42:32 -0800227 // Output:
228 // java -classpath=a
229}
230
231func ExampleRuleBuilderCommand_FlagWithList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800232 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800233 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800234 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800235 FlagWithList("--sort=", []string{"time", "size"}, ","))
236 // Output:
237 // ls --sort=time,size
238}
239
Colin Crossfeec25b2019-01-30 17:32:39 -0800240func TestRuleBuilder(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800241 fs := map[string][]byte{
Colin Cross1d2cf042019-03-29 15:33:06 -0700242 "dep_fixer": nil,
243 "input": nil,
244 "Implicit": nil,
245 "Input": nil,
246 "Tool": nil,
247 "input2": nil,
248 "tool2": nil,
249 "input3": nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800250 }
251
252 ctx := PathContextForTesting(TestConfig("out", nil), fs)
253
Dan Willemsen633c5022019-04-12 11:11:38 -0700254 addCommands := func(rule *RuleBuilder) {
255 cmd := rule.Command().
256 DepFile(PathForOutput(ctx, "DepFile")).
257 Flag("Flag").
258 FlagWithArg("FlagWithArg=", "arg").
259 FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "depfile")).
260 FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
261 FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
262 Implicit(PathForSource(ctx, "Implicit")).
263 ImplicitDepFile(PathForOutput(ctx, "ImplicitDepFile")).
264 ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
265 Input(PathForSource(ctx, "Input")).
266 Output(PathForOutput(ctx, "Output")).
267 Text("Text").
268 Tool(PathForSource(ctx, "Tool"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800269
Dan Willemsen633c5022019-04-12 11:11:38 -0700270 rule.Command().
271 Text("command2").
272 DepFile(PathForOutput(ctx, "depfile2")).
273 Input(PathForSource(ctx, "input2")).
274 Output(PathForOutput(ctx, "output2")).
275 Tool(PathForSource(ctx, "tool2"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800276
Dan Willemsen633c5022019-04-12 11:11:38 -0700277 // Test updates to the first command after the second command has been started
278 cmd.Text("after command2")
279 // Test updating a command when the previous update did not replace the cmd variable
280 cmd.Text("old cmd")
Colin Crossfeec25b2019-01-30 17:32:39 -0800281
Dan Willemsen633c5022019-04-12 11:11:38 -0700282 // Test a command that uses the output of a previous command as an input
283 rule.Command().
284 Text("command3").
285 Input(PathForSource(ctx, "input3")).
286 Input(PathForOutput(ctx, "output2")).
287 Output(PathForOutput(ctx, "output3"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800288 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700289
Colin Cross69f59a32019-02-15 10:39:37 -0800290 wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
291 wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"})
Colin Cross1d2cf042019-03-29 15:33:06 -0700292 wantDepFiles := PathsForOutput(ctx, []string{"DepFile", "depfile", "ImplicitDepFile", "depfile2"})
Colin Cross69f59a32019-02-15 10:39:37 -0800293 wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
Colin Crossfeec25b2019-01-30 17:32:39 -0800294
Dan Willemsen633c5022019-04-12 11:11:38 -0700295 t.Run("normal", func(t *testing.T) {
296 rule := NewRuleBuilder()
297 addCommands(rule)
Colin Cross1d2cf042019-03-29 15:33:06 -0700298
Dan Willemsen633c5022019-04-12 11:11:38 -0700299 wantCommands := []string{
300 "out/DepFile Flag FlagWithArg=arg FlagWithDepFile=out/depfile FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd",
301 "command2 out/depfile2 input2 out/output2 tool2",
302 "command3 input3 out/output2 out/output3",
303 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700304
Dan Willemsen633c5022019-04-12 11:11:38 -0700305 wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer out/DepFile out/depfile out/ImplicitDepFile out/depfile2"
306
307 if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) {
308 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g)
309 }
310
311 if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) {
312 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g)
313 }
314 if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) {
315 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g)
316 }
317 if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) {
318 t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g)
319 }
320 if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) {
321 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g)
322 }
323
324 if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w {
325 t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g)
326 }
327 })
328
329 t.Run("sbox", func(t *testing.T) {
330 rule := NewRuleBuilder().Sbox(PathForOutput(ctx))
331 addCommands(rule)
332
333 wantCommands := []string{
334 "__SBOX_OUT_DIR__/DepFile Flag FlagWithArg=arg FlagWithDepFile=__SBOX_OUT_DIR__/depfile FlagWithInput=input FlagWithOutput=__SBOX_OUT_DIR__/output Input __SBOX_OUT_DIR__/Output Text Tool after command2 old cmd",
335 "command2 __SBOX_OUT_DIR__/depfile2 input2 __SBOX_OUT_DIR__/output2 tool2",
336 "command3 input3 __SBOX_OUT_DIR__/output2 __SBOX_OUT_DIR__/output3",
337 }
338
339 wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer __SBOX_OUT_DIR__/DepFile __SBOX_OUT_DIR__/depfile __SBOX_OUT_DIR__/ImplicitDepFile __SBOX_OUT_DIR__/depfile2"
340
341 if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) {
342 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g)
343 }
344
345 if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) {
346 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g)
347 }
348 if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) {
349 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g)
350 }
351 if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) {
352 t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g)
353 }
354 if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) {
355 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g)
356 }
357
358 if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w {
359 t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g)
360 }
361 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800362}
363
364func testRuleBuilderFactory() Module {
365 module := &testRuleBuilderModule{}
366 module.AddProperties(&module.properties)
367 InitAndroidModule(module)
368 return module
369}
370
371type testRuleBuilderModule struct {
372 ModuleBase
373 properties struct {
374 Src string
Dan Willemsen633c5022019-04-12 11:11:38 -0700375
376 Restat bool
377 Sbox bool
Colin Crossfeec25b2019-01-30 17:32:39 -0800378 }
379}
380
381func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800382 in := PathForSource(ctx, t.properties.Src)
383 out := PathForModuleOut(ctx, ctx.ModuleName())
Dan Willemsen633c5022019-04-12 11:11:38 -0700384 outDep := PathForModuleOut(ctx, ctx.ModuleName()+".d")
385 outDir := PathForModuleOut(ctx)
Colin Crossfeec25b2019-01-30 17:32:39 -0800386
Dan Willemsen633c5022019-04-12 11:11:38 -0700387 testRuleBuilder_Build(ctx, in, out, outDep, outDir, t.properties.Restat, t.properties.Sbox)
Colin Cross786cd6d2019-02-01 16:41:11 -0800388}
389
390type testRuleBuilderSingleton struct{}
391
392func testRuleBuilderSingletonFactory() Singleton {
393 return &testRuleBuilderSingleton{}
394}
395
396func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
397 in := PathForSource(ctx, "bar")
398 out := PathForOutput(ctx, "baz")
Dan Willemsen633c5022019-04-12 11:11:38 -0700399 outDep := PathForOutput(ctx, "baz.d")
400 outDir := PathForOutput(ctx)
401 testRuleBuilder_Build(ctx, in, out, outDep, outDir, true, false)
Colin Cross786cd6d2019-02-01 16:41:11 -0800402}
403
Dan Willemsen633c5022019-04-12 11:11:38 -0700404func testRuleBuilder_Build(ctx BuilderContext, in Path, out, outDep, outDir WritablePath, restat, sbox bool) {
Colin Cross758290d2019-02-01 16:42:32 -0800405 rule := NewRuleBuilder()
Colin Cross786cd6d2019-02-01 16:41:11 -0800406
Dan Willemsen633c5022019-04-12 11:11:38 -0700407 if sbox {
408 rule.Sbox(outDir)
409 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800410
Dan Willemsen633c5022019-04-12 11:11:38 -0700411 rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out).ImplicitDepFile(outDep)
412
413 if restat {
414 rule.Restat()
415 }
Colin Crossbaa676f2019-02-25 14:56:01 -0800416
Colin Crossfeec25b2019-01-30 17:32:39 -0800417 rule.Build(pctx, ctx, "rule", "desc")
418}
419
420func TestRuleBuilder_Build(t *testing.T) {
421 buildDir, err := ioutil.TempDir("", "soong_test_rule_builder")
422 if err != nil {
423 t.Fatal(err)
424 }
425 defer os.RemoveAll(buildDir)
426
427 bp := `
428 rule_builder_test {
429 name: "foo",
430 src: "bar",
Dan Willemsen633c5022019-04-12 11:11:38 -0700431 restat: true,
432 }
433 rule_builder_test {
434 name: "foo_sbox",
435 src: "bar",
436 sbox: true,
Colin Crossfeec25b2019-01-30 17:32:39 -0800437 }
438 `
439
440 config := TestConfig(buildDir, nil)
441 ctx := NewTestContext()
442 ctx.MockFileSystem(map[string][]byte{
443 "Android.bp": []byte(bp),
444 "bar": nil,
445 "cp": nil,
446 })
447 ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory))
Colin Cross786cd6d2019-02-01 16:41:11 -0800448 ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory))
Colin Crossfeec25b2019-01-30 17:32:39 -0800449 ctx.Register()
450
451 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
452 FailIfErrored(t, errs)
453 _, errs = ctx.PrepareBuildActions(config)
454 FailIfErrored(t, errs)
455
Dan Willemsen633c5022019-04-12 11:11:38 -0700456 check := func(t *testing.T, params TestingBuildParams, wantCommand, wantOutput, wantDepfile string, wantRestat bool, extraCmdDeps []string) {
457 if params.RuleParams.Command != wantCommand {
458 t.Errorf("\nwant RuleParams.Command = %q\n got %q", wantCommand, params.RuleParams.Command)
459 }
460
461 wantDeps := append([]string{"cp"}, extraCmdDeps...)
462 if !reflect.DeepEqual(params.RuleParams.CommandDeps, wantDeps) {
463 t.Errorf("\nwant RuleParams.CommandDeps = %q\n got %q", wantDeps, params.RuleParams.CommandDeps)
464 }
465
466 if params.RuleParams.Restat != wantRestat {
467 t.Errorf("want RuleParams.Restat = %v, got %v", wantRestat, params.RuleParams.Restat)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800468 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800469
Colin Cross4c83e5c2019-02-25 14:54:28 -0800470 if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" {
471 t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings())
472 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800473
Colin Cross1d2cf042019-03-29 15:33:06 -0700474 if params.Output.String() != wantOutput {
475 t.Errorf("want Output = %q, got %q", wantOutput, params.Output)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800476 }
Colin Crossbaa676f2019-02-25 14:56:01 -0800477
Dan Willemsen633c5022019-04-12 11:11:38 -0700478 if len(params.ImplicitOutputs) != 0 {
479 t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings())
480 }
481
482 if params.Depfile.String() != wantDepfile {
483 t.Errorf("want Depfile = %q, got %q", wantDepfile, params.Depfile)
484 }
485
486 if params.Deps != blueprint.DepsGCC {
487 t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps)
Colin Crossbaa676f2019-02-25 14:56:01 -0800488 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800489 }
490
Colin Cross4c83e5c2019-02-25 14:54:28 -0800491 t.Run("module", func(t *testing.T) {
Dan Willemsen633c5022019-04-12 11:11:38 -0700492 outFile := filepath.Join(buildDir, ".intermediates", "foo", "foo")
Colin Cross4c83e5c2019-02-25 14:54:28 -0800493 check(t, ctx.ModuleForTests("foo", "").Rule("rule"),
Dan Willemsen633c5022019-04-12 11:11:38 -0700494 "cp bar "+outFile,
495 outFile, outFile+".d", true, nil)
496 })
497 t.Run("sbox", func(t *testing.T) {
498 outDir := filepath.Join(buildDir, ".intermediates", "foo_sbox")
499 outFile := filepath.Join(outDir, "foo_sbox")
500 sbox := filepath.Join(buildDir, "host", config.PrebuiltOS(), "bin/sbox")
501 sandboxPath := shared.TempDirForOutDir(buildDir)
502
503 cmd := sbox + ` -c 'cp bar __SBOX_OUT_DIR__/foo_sbox' --sandbox-path ` + sandboxPath + " --output-root " + outDir + " __SBOX_OUT_DIR__/foo_sbox __SBOX_OUT_DIR__/foo_sbox.d"
504
505 check(t, ctx.ModuleForTests("foo_sbox", "").Rule("rule"),
506 cmd, outFile, outFile+".d", false, []string{sbox})
Colin Cross4c83e5c2019-02-25 14:54:28 -0800507 })
508 t.Run("singleton", func(t *testing.T) {
Dan Willemsen633c5022019-04-12 11:11:38 -0700509 outFile := filepath.Join(buildDir, "baz")
Colin Cross4c83e5c2019-02-25 14:54:28 -0800510 check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"),
Dan Willemsen633c5022019-04-12 11:11:38 -0700511 "cp bar "+outFile, outFile, outFile+".d", true, nil)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800512 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800513}