blob: cfbc2abeea219fd0fcab0fad54c3d90b56aeef43 [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 "path/filepath"
20 "reflect"
Colin Cross758290d2019-02-01 16:42:32 -080021 "strings"
Colin Crossfeec25b2019-01-30 17:32:39 -080022 "testing"
Dan Willemsen633c5022019-04-12 11:11:38 -070023
24 "github.com/google/blueprint"
25
26 "android/soong/shared"
Colin Crossfeec25b2019-01-30 17:32:39 -080027)
28
Colin Cross69f59a32019-02-15 10:39:37 -080029func pathContext() PathContext {
30 return PathContextForTesting(TestConfig("out", nil),
31 map[string][]byte{
32 "ld": nil,
33 "a.o": nil,
34 "b.o": nil,
35 "cp": nil,
36 "a": nil,
37 "b": nil,
38 "ls": nil,
39 "turbine": nil,
40 "java": nil,
41 })
42}
43
Colin Cross758290d2019-02-01 16:42:32 -080044func ExampleRuleBuilder() {
45 rule := NewRuleBuilder()
46
Colin Cross69f59a32019-02-15 10:39:37 -080047 ctx := pathContext()
48
49 rule.Command().
50 Tool(PathForSource(ctx, "ld")).
51 Inputs(PathsForTesting("a.o", "b.o")).
52 FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -080053 rule.Command().Text("echo success")
54
55 // To add the command to the build graph:
56 // rule.Build(pctx, ctx, "link", "link")
57
58 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
59 fmt.Printf("tools: %q\n", rule.Tools())
60 fmt.Printf("inputs: %q\n", rule.Inputs())
61 fmt.Printf("outputs: %q\n", rule.Outputs())
62
63 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -080064 // commands: "ld a.o b.o -o out/linked && echo success"
Colin Cross758290d2019-02-01 16:42:32 -080065 // tools: ["ld"]
66 // inputs: ["a.o" "b.o"]
Colin Cross69f59a32019-02-15 10:39:37 -080067 // outputs: ["out/linked"]
Colin Cross758290d2019-02-01 16:42:32 -080068}
69
Colin Cross5cb5b092019-02-02 21:25:18 -080070func ExampleRuleBuilder_Temporary() {
71 rule := NewRuleBuilder()
72
Colin Cross69f59a32019-02-15 10:39:37 -080073 ctx := pathContext()
74
75 rule.Command().
76 Tool(PathForSource(ctx, "cp")).
77 Input(PathForSource(ctx, "a")).
78 Output(PathForOutput(ctx, "b"))
79 rule.Command().
80 Tool(PathForSource(ctx, "cp")).
81 Input(PathForOutput(ctx, "b")).
82 Output(PathForOutput(ctx, "c"))
83 rule.Temporary(PathForOutput(ctx, "b"))
Colin Cross5cb5b092019-02-02 21:25:18 -080084
85 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
86 fmt.Printf("tools: %q\n", rule.Tools())
87 fmt.Printf("inputs: %q\n", rule.Inputs())
88 fmt.Printf("outputs: %q\n", rule.Outputs())
89
90 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -080091 // commands: "cp a out/b && cp out/b out/c"
Colin Cross5cb5b092019-02-02 21:25:18 -080092 // tools: ["cp"]
93 // inputs: ["a"]
Colin Cross69f59a32019-02-15 10:39:37 -080094 // outputs: ["out/c"]
Colin Cross5cb5b092019-02-02 21:25:18 -080095}
96
97func ExampleRuleBuilder_DeleteTemporaryFiles() {
98 rule := NewRuleBuilder()
99
Colin Cross69f59a32019-02-15 10:39:37 -0800100 ctx := pathContext()
101
102 rule.Command().
103 Tool(PathForSource(ctx, "cp")).
104 Input(PathForSource(ctx, "a")).
105 Output(PathForOutput(ctx, "b"))
106 rule.Command().
107 Tool(PathForSource(ctx, "cp")).
108 Input(PathForOutput(ctx, "b")).
109 Output(PathForOutput(ctx, "c"))
110 rule.Temporary(PathForOutput(ctx, "b"))
Colin Cross5cb5b092019-02-02 21:25:18 -0800111 rule.DeleteTemporaryFiles()
112
113 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
114 fmt.Printf("tools: %q\n", rule.Tools())
115 fmt.Printf("inputs: %q\n", rule.Inputs())
116 fmt.Printf("outputs: %q\n", rule.Outputs())
117
118 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -0800119 // commands: "cp a out/b && cp out/b out/c && rm -f out/b"
Colin Cross5cb5b092019-02-02 21:25:18 -0800120 // tools: ["cp"]
121 // inputs: ["a"]
Colin Cross69f59a32019-02-15 10:39:37 -0800122 // outputs: ["out/c"]
Colin Cross5cb5b092019-02-02 21:25:18 -0800123}
124
Colin Crossdeabb942019-02-11 14:11:09 -0800125func ExampleRuleBuilder_Installs() {
126 rule := NewRuleBuilder()
127
Colin Cross69f59a32019-02-15 10:39:37 -0800128 ctx := pathContext()
129
130 out := PathForOutput(ctx, "linked")
131
132 rule.Command().
133 Tool(PathForSource(ctx, "ld")).
134 Inputs(PathsForTesting("a.o", "b.o")).
135 FlagWithOutput("-o ", out)
136 rule.Install(out, "/bin/linked")
137 rule.Install(out, "/sbin/linked")
Colin Crossdeabb942019-02-11 14:11:09 -0800138
139 fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String())
140
141 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -0800142 // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked"
Colin Crossdeabb942019-02-11 14:11:09 -0800143}
144
Colin Cross758290d2019-02-01 16:42:32 -0800145func ExampleRuleBuilderCommand() {
146 rule := NewRuleBuilder()
147
Colin Cross69f59a32019-02-15 10:39:37 -0800148 ctx := pathContext()
149
Colin Cross758290d2019-02-01 16:42:32 -0800150 // chained
Colin Cross69f59a32019-02-15 10:39:37 -0800151 rule.Command().
152 Tool(PathForSource(ctx, "ld")).
153 Inputs(PathsForTesting("a.o", "b.o")).
154 FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800155
156 // unchained
157 cmd := rule.Command()
Colin Cross69f59a32019-02-15 10:39:37 -0800158 cmd.Tool(PathForSource(ctx, "ld"))
159 cmd.Inputs(PathsForTesting("a.o", "b.o"))
160 cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800161
162 // mixed:
Colin Cross69f59a32019-02-15 10:39:37 -0800163 cmd = rule.Command().Tool(PathForSource(ctx, "ld"))
164 cmd.Inputs(PathsForTesting("a.o", "b.o"))
165 cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800166}
167
168func ExampleRuleBuilderCommand_Flag() {
Colin Cross69f59a32019-02-15 10:39:37 -0800169 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800170 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800171 Tool(PathForSource(ctx, "ls")).Flag("-l"))
Colin Cross758290d2019-02-01 16:42:32 -0800172 // Output:
173 // ls -l
174}
175
Colin Cross92b7d582019-03-29 15:32:51 -0700176func ExampleRuleBuilderCommand_Flags() {
177 ctx := pathContext()
178 fmt.Println(NewRuleBuilder().Command().
179 Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"}))
180 // Output:
181 // ls -l -a
182}
183
Colin Cross758290d2019-02-01 16:42:32 -0800184func ExampleRuleBuilderCommand_FlagWithArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800185 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800186 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800187 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800188 FlagWithArg("--sort=", "time"))
189 // Output:
190 // ls --sort=time
191}
192
Colin Crossc7ed0042019-02-11 14:11:09 -0800193func ExampleRuleBuilderCommand_FlagForEachArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800194 ctx := pathContext()
Colin Crossc7ed0042019-02-11 14:11:09 -0800195 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800196 Tool(PathForSource(ctx, "ls")).
Colin Crossc7ed0042019-02-11 14:11:09 -0800197 FlagForEachArg("--sort=", []string{"time", "size"}))
198 // Output:
199 // ls --sort=time --sort=size
200}
201
Colin Cross758290d2019-02-01 16:42:32 -0800202func ExampleRuleBuilderCommand_FlagForEachInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800203 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800204 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800205 Tool(PathForSource(ctx, "turbine")).
206 FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
Colin Cross758290d2019-02-01 16:42:32 -0800207 // Output:
208 // turbine --classpath a.jar --classpath b.jar
209}
210
211func ExampleRuleBuilderCommand_FlagWithInputList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800212 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800213 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800214 Tool(PathForSource(ctx, "java")).
215 FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
Colin Cross758290d2019-02-01 16:42:32 -0800216 // Output:
217 // java -classpath=a.jar:b.jar
218}
219
220func ExampleRuleBuilderCommand_FlagWithInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800221 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800222 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800223 Tool(PathForSource(ctx, "java")).
224 FlagWithInput("-classpath=", PathForSource(ctx, "a")))
Colin Cross758290d2019-02-01 16:42:32 -0800225 // Output:
226 // java -classpath=a
227}
228
229func ExampleRuleBuilderCommand_FlagWithList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800230 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800231 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800232 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800233 FlagWithList("--sort=", []string{"time", "size"}, ","))
234 // Output:
235 // ls --sort=time,size
236}
237
Colin Crossfeec25b2019-01-30 17:32:39 -0800238func TestRuleBuilder(t *testing.T) {
Colin Cross69f59a32019-02-15 10:39:37 -0800239 fs := map[string][]byte{
Colin Cross1d2cf042019-03-29 15:33:06 -0700240 "dep_fixer": nil,
241 "input": nil,
242 "Implicit": nil,
243 "Input": nil,
244 "Tool": nil,
245 "input2": nil,
246 "tool2": nil,
247 "input3": nil,
Colin Cross69f59a32019-02-15 10:39:37 -0800248 }
249
250 ctx := PathContextForTesting(TestConfig("out", nil), fs)
251
Dan Willemsen633c5022019-04-12 11:11:38 -0700252 addCommands := func(rule *RuleBuilder) {
253 cmd := rule.Command().
254 DepFile(PathForOutput(ctx, "DepFile")).
255 Flag("Flag").
256 FlagWithArg("FlagWithArg=", "arg").
257 FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "depfile")).
258 FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
259 FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
260 Implicit(PathForSource(ctx, "Implicit")).
261 ImplicitDepFile(PathForOutput(ctx, "ImplicitDepFile")).
262 ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
263 Input(PathForSource(ctx, "Input")).
264 Output(PathForOutput(ctx, "Output")).
265 Text("Text").
266 Tool(PathForSource(ctx, "Tool"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800267
Dan Willemsen633c5022019-04-12 11:11:38 -0700268 rule.Command().
269 Text("command2").
270 DepFile(PathForOutput(ctx, "depfile2")).
271 Input(PathForSource(ctx, "input2")).
272 Output(PathForOutput(ctx, "output2")).
273 Tool(PathForSource(ctx, "tool2"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800274
Dan Willemsen633c5022019-04-12 11:11:38 -0700275 // Test updates to the first command after the second command has been started
276 cmd.Text("after command2")
277 // Test updating a command when the previous update did not replace the cmd variable
278 cmd.Text("old cmd")
Colin Crossfeec25b2019-01-30 17:32:39 -0800279
Dan Willemsen633c5022019-04-12 11:11:38 -0700280 // Test a command that uses the output of a previous command as an input
281 rule.Command().
282 Text("command3").
283 Input(PathForSource(ctx, "input3")).
284 Input(PathForOutput(ctx, "output2")).
285 Output(PathForOutput(ctx, "output3"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800286 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700287
Colin Cross69f59a32019-02-15 10:39:37 -0800288 wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
289 wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"})
Colin Cross1d2cf042019-03-29 15:33:06 -0700290 wantDepFiles := PathsForOutput(ctx, []string{"DepFile", "depfile", "ImplicitDepFile", "depfile2"})
Colin Cross69f59a32019-02-15 10:39:37 -0800291 wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
Colin Crossfeec25b2019-01-30 17:32:39 -0800292
Dan Willemsen633c5022019-04-12 11:11:38 -0700293 t.Run("normal", func(t *testing.T) {
294 rule := NewRuleBuilder()
295 addCommands(rule)
Colin Cross1d2cf042019-03-29 15:33:06 -0700296
Dan Willemsen633c5022019-04-12 11:11:38 -0700297 wantCommands := []string{
298 "out/DepFile Flag FlagWithArg=arg FlagWithDepFile=out/depfile FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd",
299 "command2 out/depfile2 input2 out/output2 tool2",
300 "command3 input3 out/output2 out/output3",
301 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700302
Dan Willemsen633c5022019-04-12 11:11:38 -0700303 wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer out/DepFile out/depfile out/ImplicitDepFile out/depfile2"
304
305 if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) {
306 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g)
307 }
308
309 if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) {
310 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g)
311 }
312 if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) {
313 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g)
314 }
315 if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) {
316 t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g)
317 }
318 if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) {
319 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g)
320 }
321
322 if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w {
323 t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g)
324 }
325 })
326
327 t.Run("sbox", func(t *testing.T) {
328 rule := NewRuleBuilder().Sbox(PathForOutput(ctx))
329 addCommands(rule)
330
331 wantCommands := []string{
332 "__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",
333 "command2 __SBOX_OUT_DIR__/depfile2 input2 __SBOX_OUT_DIR__/output2 tool2",
334 "command3 input3 __SBOX_OUT_DIR__/output2 __SBOX_OUT_DIR__/output3",
335 }
336
337 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"
338
339 if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) {
340 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g)
341 }
342
343 if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) {
344 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g)
345 }
346 if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) {
347 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g)
348 }
349 if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) {
350 t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g)
351 }
352 if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) {
353 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g)
354 }
355
356 if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w {
357 t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g)
358 }
359 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800360}
361
362func testRuleBuilderFactory() Module {
363 module := &testRuleBuilderModule{}
364 module.AddProperties(&module.properties)
365 InitAndroidModule(module)
366 return module
367}
368
369type testRuleBuilderModule struct {
370 ModuleBase
371 properties struct {
372 Src string
Dan Willemsen633c5022019-04-12 11:11:38 -0700373
374 Restat bool
375 Sbox bool
Colin Crossfeec25b2019-01-30 17:32:39 -0800376 }
377}
378
379func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800380 in := PathForSource(ctx, t.properties.Src)
381 out := PathForModuleOut(ctx, ctx.ModuleName())
Dan Willemsen633c5022019-04-12 11:11:38 -0700382 outDep := PathForModuleOut(ctx, ctx.ModuleName()+".d")
383 outDir := PathForModuleOut(ctx)
Colin Crossfeec25b2019-01-30 17:32:39 -0800384
Dan Willemsen633c5022019-04-12 11:11:38 -0700385 testRuleBuilder_Build(ctx, in, out, outDep, outDir, t.properties.Restat, t.properties.Sbox)
Colin Cross786cd6d2019-02-01 16:41:11 -0800386}
387
388type testRuleBuilderSingleton struct{}
389
390func testRuleBuilderSingletonFactory() Singleton {
391 return &testRuleBuilderSingleton{}
392}
393
394func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
395 in := PathForSource(ctx, "bar")
396 out := PathForOutput(ctx, "baz")
Dan Willemsen633c5022019-04-12 11:11:38 -0700397 outDep := PathForOutput(ctx, "baz.d")
398 outDir := PathForOutput(ctx)
399 testRuleBuilder_Build(ctx, in, out, outDep, outDir, true, false)
Colin Cross786cd6d2019-02-01 16:41:11 -0800400}
401
Dan Willemsen633c5022019-04-12 11:11:38 -0700402func testRuleBuilder_Build(ctx BuilderContext, in Path, out, outDep, outDir WritablePath, restat, sbox bool) {
Colin Cross758290d2019-02-01 16:42:32 -0800403 rule := NewRuleBuilder()
Colin Cross786cd6d2019-02-01 16:41:11 -0800404
Dan Willemsen633c5022019-04-12 11:11:38 -0700405 if sbox {
406 rule.Sbox(outDir)
407 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800408
Dan Willemsen633c5022019-04-12 11:11:38 -0700409 rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out).ImplicitDepFile(outDep)
410
411 if restat {
412 rule.Restat()
413 }
Colin Crossbaa676f2019-02-25 14:56:01 -0800414
Colin Crossfeec25b2019-01-30 17:32:39 -0800415 rule.Build(pctx, ctx, "rule", "desc")
416}
417
418func TestRuleBuilder_Build(t *testing.T) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800419 bp := `
420 rule_builder_test {
421 name: "foo",
422 src: "bar",
Dan Willemsen633c5022019-04-12 11:11:38 -0700423 restat: true,
424 }
425 rule_builder_test {
426 name: "foo_sbox",
427 src: "bar",
428 sbox: true,
Colin Crossfeec25b2019-01-30 17:32:39 -0800429 }
430 `
431
432 config := TestConfig(buildDir, nil)
433 ctx := NewTestContext()
434 ctx.MockFileSystem(map[string][]byte{
435 "Android.bp": []byte(bp),
436 "bar": nil,
437 "cp": nil,
438 })
439 ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory))
Colin Cross786cd6d2019-02-01 16:41:11 -0800440 ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory))
Colin Crossfeec25b2019-01-30 17:32:39 -0800441 ctx.Register()
442
443 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
444 FailIfErrored(t, errs)
445 _, errs = ctx.PrepareBuildActions(config)
446 FailIfErrored(t, errs)
447
Dan Willemsen633c5022019-04-12 11:11:38 -0700448 check := func(t *testing.T, params TestingBuildParams, wantCommand, wantOutput, wantDepfile string, wantRestat bool, extraCmdDeps []string) {
449 if params.RuleParams.Command != wantCommand {
450 t.Errorf("\nwant RuleParams.Command = %q\n got %q", wantCommand, params.RuleParams.Command)
451 }
452
453 wantDeps := append([]string{"cp"}, extraCmdDeps...)
454 if !reflect.DeepEqual(params.RuleParams.CommandDeps, wantDeps) {
455 t.Errorf("\nwant RuleParams.CommandDeps = %q\n got %q", wantDeps, params.RuleParams.CommandDeps)
456 }
457
458 if params.RuleParams.Restat != wantRestat {
459 t.Errorf("want RuleParams.Restat = %v, got %v", wantRestat, params.RuleParams.Restat)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800460 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800461
Colin Cross4c83e5c2019-02-25 14:54:28 -0800462 if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" {
463 t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings())
464 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800465
Colin Cross1d2cf042019-03-29 15:33:06 -0700466 if params.Output.String() != wantOutput {
467 t.Errorf("want Output = %q, got %q", wantOutput, params.Output)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800468 }
Colin Crossbaa676f2019-02-25 14:56:01 -0800469
Dan Willemsen633c5022019-04-12 11:11:38 -0700470 if len(params.ImplicitOutputs) != 0 {
471 t.Errorf("want ImplicitOutputs = [], got %q", params.ImplicitOutputs.Strings())
472 }
473
474 if params.Depfile.String() != wantDepfile {
475 t.Errorf("want Depfile = %q, got %q", wantDepfile, params.Depfile)
476 }
477
478 if params.Deps != blueprint.DepsGCC {
479 t.Errorf("want Deps = %q, got %q", blueprint.DepsGCC, params.Deps)
Colin Crossbaa676f2019-02-25 14:56:01 -0800480 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800481 }
482
Colin Cross4c83e5c2019-02-25 14:54:28 -0800483 t.Run("module", func(t *testing.T) {
Dan Willemsen633c5022019-04-12 11:11:38 -0700484 outFile := filepath.Join(buildDir, ".intermediates", "foo", "foo")
Colin Cross4c83e5c2019-02-25 14:54:28 -0800485 check(t, ctx.ModuleForTests("foo", "").Rule("rule"),
Dan Willemsen633c5022019-04-12 11:11:38 -0700486 "cp bar "+outFile,
487 outFile, outFile+".d", true, nil)
488 })
489 t.Run("sbox", func(t *testing.T) {
490 outDir := filepath.Join(buildDir, ".intermediates", "foo_sbox")
491 outFile := filepath.Join(outDir, "foo_sbox")
492 sbox := filepath.Join(buildDir, "host", config.PrebuiltOS(), "bin/sbox")
493 sandboxPath := shared.TempDirForOutDir(buildDir)
494
495 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"
496
497 check(t, ctx.ModuleForTests("foo_sbox", "").Rule("rule"),
498 cmd, outFile, outFile+".d", false, []string{sbox})
Colin Cross4c83e5c2019-02-25 14:54:28 -0800499 })
500 t.Run("singleton", func(t *testing.T) {
Dan Willemsen633c5022019-04-12 11:11:38 -0700501 outFile := filepath.Join(buildDir, "baz")
Colin Cross4c83e5c2019-02-25 14:54:28 -0800502 check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"),
Dan Willemsen633c5022019-04-12 11:11:38 -0700503 "cp bar "+outFile, outFile, outFile+".d", true, nil)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800504 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800505}