blob: 7bad02586b8d3a5249a4bc658106a98101a031e8 [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"
25)
26
Colin Cross69f59a32019-02-15 10:39:37 -080027func pathContext() PathContext {
28 return PathContextForTesting(TestConfig("out", nil),
29 map[string][]byte{
30 "ld": nil,
31 "a.o": nil,
32 "b.o": nil,
33 "cp": nil,
34 "a": nil,
35 "b": nil,
36 "ls": nil,
37 "turbine": nil,
38 "java": nil,
39 })
40}
41
Colin Cross758290d2019-02-01 16:42:32 -080042func ExampleRuleBuilder() {
43 rule := NewRuleBuilder()
44
Colin Cross69f59a32019-02-15 10:39:37 -080045 ctx := pathContext()
46
47 rule.Command().
48 Tool(PathForSource(ctx, "ld")).
49 Inputs(PathsForTesting("a.o", "b.o")).
50 FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -080051 rule.Command().Text("echo success")
52
53 // To add the command to the build graph:
54 // rule.Build(pctx, ctx, "link", "link")
55
56 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
57 fmt.Printf("tools: %q\n", rule.Tools())
58 fmt.Printf("inputs: %q\n", rule.Inputs())
59 fmt.Printf("outputs: %q\n", rule.Outputs())
60
61 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -080062 // commands: "ld a.o b.o -o out/linked && echo success"
Colin Cross758290d2019-02-01 16:42:32 -080063 // tools: ["ld"]
64 // inputs: ["a.o" "b.o"]
Colin Cross69f59a32019-02-15 10:39:37 -080065 // outputs: ["out/linked"]
Colin Cross758290d2019-02-01 16:42:32 -080066}
67
Colin Cross5cb5b092019-02-02 21:25:18 -080068func ExampleRuleBuilder_Temporary() {
69 rule := NewRuleBuilder()
70
Colin Cross69f59a32019-02-15 10:39:37 -080071 ctx := pathContext()
72
73 rule.Command().
74 Tool(PathForSource(ctx, "cp")).
75 Input(PathForSource(ctx, "a")).
76 Output(PathForOutput(ctx, "b"))
77 rule.Command().
78 Tool(PathForSource(ctx, "cp")).
79 Input(PathForOutput(ctx, "b")).
80 Output(PathForOutput(ctx, "c"))
81 rule.Temporary(PathForOutput(ctx, "b"))
Colin Cross5cb5b092019-02-02 21:25:18 -080082
83 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
84 fmt.Printf("tools: %q\n", rule.Tools())
85 fmt.Printf("inputs: %q\n", rule.Inputs())
86 fmt.Printf("outputs: %q\n", rule.Outputs())
87
88 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -080089 // commands: "cp a out/b && cp out/b out/c"
Colin Cross5cb5b092019-02-02 21:25:18 -080090 // tools: ["cp"]
91 // inputs: ["a"]
Colin Cross69f59a32019-02-15 10:39:37 -080092 // outputs: ["out/c"]
Colin Cross5cb5b092019-02-02 21:25:18 -080093}
94
95func ExampleRuleBuilder_DeleteTemporaryFiles() {
96 rule := NewRuleBuilder()
97
Colin Cross69f59a32019-02-15 10:39:37 -080098 ctx := pathContext()
99
100 rule.Command().
101 Tool(PathForSource(ctx, "cp")).
102 Input(PathForSource(ctx, "a")).
103 Output(PathForOutput(ctx, "b"))
104 rule.Command().
105 Tool(PathForSource(ctx, "cp")).
106 Input(PathForOutput(ctx, "b")).
107 Output(PathForOutput(ctx, "c"))
108 rule.Temporary(PathForOutput(ctx, "b"))
Colin Cross5cb5b092019-02-02 21:25:18 -0800109 rule.DeleteTemporaryFiles()
110
111 fmt.Printf("commands: %q\n", strings.Join(rule.Commands(), " && "))
112 fmt.Printf("tools: %q\n", rule.Tools())
113 fmt.Printf("inputs: %q\n", rule.Inputs())
114 fmt.Printf("outputs: %q\n", rule.Outputs())
115
116 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -0800117 // commands: "cp a out/b && cp out/b out/c && rm -f out/b"
Colin Cross5cb5b092019-02-02 21:25:18 -0800118 // tools: ["cp"]
119 // inputs: ["a"]
Colin Cross69f59a32019-02-15 10:39:37 -0800120 // outputs: ["out/c"]
Colin Cross5cb5b092019-02-02 21:25:18 -0800121}
122
Colin Crossdeabb942019-02-11 14:11:09 -0800123func ExampleRuleBuilder_Installs() {
124 rule := NewRuleBuilder()
125
Colin Cross69f59a32019-02-15 10:39:37 -0800126 ctx := pathContext()
127
128 out := PathForOutput(ctx, "linked")
129
130 rule.Command().
131 Tool(PathForSource(ctx, "ld")).
132 Inputs(PathsForTesting("a.o", "b.o")).
133 FlagWithOutput("-o ", out)
134 rule.Install(out, "/bin/linked")
135 rule.Install(out, "/sbin/linked")
Colin Crossdeabb942019-02-11 14:11:09 -0800136
137 fmt.Printf("rule.Installs().String() = %q\n", rule.Installs().String())
138
139 // Output:
Colin Cross69f59a32019-02-15 10:39:37 -0800140 // rule.Installs().String() = "out/linked:/bin/linked out/linked:/sbin/linked"
Colin Crossdeabb942019-02-11 14:11:09 -0800141}
142
Colin Cross758290d2019-02-01 16:42:32 -0800143func ExampleRuleBuilderCommand() {
144 rule := NewRuleBuilder()
145
Colin Cross69f59a32019-02-15 10:39:37 -0800146 ctx := pathContext()
147
Colin Cross758290d2019-02-01 16:42:32 -0800148 // chained
Colin Cross69f59a32019-02-15 10:39:37 -0800149 rule.Command().
150 Tool(PathForSource(ctx, "ld")).
151 Inputs(PathsForTesting("a.o", "b.o")).
152 FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800153
154 // unchained
155 cmd := rule.Command()
Colin Cross69f59a32019-02-15 10:39:37 -0800156 cmd.Tool(PathForSource(ctx, "ld"))
157 cmd.Inputs(PathsForTesting("a.o", "b.o"))
158 cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800159
160 // mixed:
Colin Cross69f59a32019-02-15 10:39:37 -0800161 cmd = rule.Command().Tool(PathForSource(ctx, "ld"))
162 cmd.Inputs(PathsForTesting("a.o", "b.o"))
163 cmd.FlagWithOutput("-o ", PathForOutput(ctx, "linked"))
Colin Cross758290d2019-02-01 16:42:32 -0800164}
165
166func ExampleRuleBuilderCommand_Flag() {
Colin Cross69f59a32019-02-15 10:39:37 -0800167 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800168 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800169 Tool(PathForSource(ctx, "ls")).Flag("-l"))
Colin Cross758290d2019-02-01 16:42:32 -0800170 // Output:
171 // ls -l
172}
173
Colin Cross92b7d582019-03-29 15:32:51 -0700174func ExampleRuleBuilderCommand_Flags() {
175 ctx := pathContext()
176 fmt.Println(NewRuleBuilder().Command().
177 Tool(PathForSource(ctx, "ls")).Flags([]string{"-l", "-a"}))
178 // Output:
179 // ls -l -a
180}
181
Colin Cross758290d2019-02-01 16:42:32 -0800182func ExampleRuleBuilderCommand_FlagWithArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800183 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800184 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800185 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800186 FlagWithArg("--sort=", "time"))
187 // Output:
188 // ls --sort=time
189}
190
Colin Crossc7ed0042019-02-11 14:11:09 -0800191func ExampleRuleBuilderCommand_FlagForEachArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800192 ctx := pathContext()
Colin Crossc7ed0042019-02-11 14:11:09 -0800193 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800194 Tool(PathForSource(ctx, "ls")).
Colin Crossc7ed0042019-02-11 14:11:09 -0800195 FlagForEachArg("--sort=", []string{"time", "size"}))
196 // Output:
197 // ls --sort=time --sort=size
198}
199
Colin Cross758290d2019-02-01 16:42:32 -0800200func ExampleRuleBuilderCommand_FlagForEachInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800201 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800202 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800203 Tool(PathForSource(ctx, "turbine")).
204 FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
Colin Cross758290d2019-02-01 16:42:32 -0800205 // Output:
206 // turbine --classpath a.jar --classpath b.jar
207}
208
209func ExampleRuleBuilderCommand_FlagWithInputList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800210 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800211 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800212 Tool(PathForSource(ctx, "java")).
213 FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
Colin Cross758290d2019-02-01 16:42:32 -0800214 // Output:
215 // java -classpath=a.jar:b.jar
216}
217
218func ExampleRuleBuilderCommand_FlagWithInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800219 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800220 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800221 Tool(PathForSource(ctx, "java")).
222 FlagWithInput("-classpath=", PathForSource(ctx, "a")))
Colin Cross758290d2019-02-01 16:42:32 -0800223 // Output:
224 // java -classpath=a
225}
226
227func ExampleRuleBuilderCommand_FlagWithList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800228 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800229 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800230 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800231 FlagWithList("--sort=", []string{"time", "size"}, ","))
232 // Output:
233 // ls --sort=time,size
234}
235
Colin Crossfeec25b2019-01-30 17:32:39 -0800236func TestRuleBuilder(t *testing.T) {
Colin Cross758290d2019-02-01 16:42:32 -0800237 rule := NewRuleBuilder()
Colin Crossfeec25b2019-01-30 17:32:39 -0800238
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
Colin Crossfeec25b2019-01-30 17:32:39 -0800252 cmd := rule.Command().
Colin Cross1d2cf042019-03-29 15:33:06 -0700253 DepFile(PathForOutput(ctx, "DepFile")).
Colin Crossfeec25b2019-01-30 17:32:39 -0800254 Flag("Flag").
255 FlagWithArg("FlagWithArg=", "arg").
Colin Cross1d2cf042019-03-29 15:33:06 -0700256 FlagWithDepFile("FlagWithDepFile=", PathForOutput(ctx, "depfile")).
Colin Cross69f59a32019-02-15 10:39:37 -0800257 FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
258 FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
259 Implicit(PathForSource(ctx, "Implicit")).
Colin Cross1d2cf042019-03-29 15:33:06 -0700260 ImplicitDepFile(PathForOutput(ctx, "ImplicitDepFile")).
Colin Cross69f59a32019-02-15 10:39:37 -0800261 ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
262 Input(PathForSource(ctx, "Input")).
263 Output(PathForOutput(ctx, "Output")).
Colin Crossfeec25b2019-01-30 17:32:39 -0800264 Text("Text").
Colin Cross69f59a32019-02-15 10:39:37 -0800265 Tool(PathForSource(ctx, "Tool"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800266
267 rule.Command().
268 Text("command2").
Colin Cross1d2cf042019-03-29 15:33:06 -0700269 DepFile(PathForOutput(ctx, "depfile2")).
Colin Cross69f59a32019-02-15 10:39:37 -0800270 Input(PathForSource(ctx, "input2")).
271 Output(PathForOutput(ctx, "output2")).
272 Tool(PathForSource(ctx, "tool2"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800273
274 // Test updates to the first command after the second command has been started
275 cmd.Text("after command2")
276 // Test updating a command when the previous update did not replace the cmd variable
277 cmd.Text("old cmd")
278
279 // Test a command that uses the output of a previous command as an input
280 rule.Command().
281 Text("command3").
Colin Cross69f59a32019-02-15 10:39:37 -0800282 Input(PathForSource(ctx, "input3")).
283 Input(PathForOutput(ctx, "output2")).
284 Output(PathForOutput(ctx, "output3"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800285
286 wantCommands := []string{
Colin Cross1d2cf042019-03-29 15:33:06 -0700287 "out/DepFile Flag FlagWithArg=arg FlagWithDepFile=out/depfile FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd",
288 "command2 out/depfile2 input2 out/output2 tool2",
Colin Cross69f59a32019-02-15 10:39:37 -0800289 "command3 input3 out/output2 out/output3",
Colin Crossfeec25b2019-01-30 17:32:39 -0800290 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700291
292 wantDepMergerCommand := "out/host/" + ctx.Config().PrebuiltOS() + "/bin/dep_fixer out/DepFile out/depfile out/ImplicitDepFile out/depfile2"
293
Colin Cross69f59a32019-02-15 10:39:37 -0800294 wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
295 wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"})
Colin Cross1d2cf042019-03-29 15:33:06 -0700296 wantDepFiles := PathsForOutput(ctx, []string{"DepFile", "depfile", "ImplicitDepFile", "depfile2"})
Colin Cross69f59a32019-02-15 10:39:37 -0800297 wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
Colin Crossfeec25b2019-01-30 17:32:39 -0800298
Colin Cross1d2cf042019-03-29 15:33:06 -0700299 if g, w := rule.Commands(), wantCommands; !reflect.DeepEqual(g, w) {
300 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", w, g)
Colin Crossfeec25b2019-01-30 17:32:39 -0800301 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700302
303 if g, w := rule.depFileMergerCmd(ctx, rule.DepFiles()).String(), wantDepMergerCommand; g != w {
304 t.Errorf("\nwant rule.depFileMergerCmd() = %#v\n got %#v", w, g)
Colin Crossfeec25b2019-01-30 17:32:39 -0800305 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700306
307 if g, w := rule.Inputs(), wantInputs; !reflect.DeepEqual(w, g) {
308 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", w, g)
Colin Crossfeec25b2019-01-30 17:32:39 -0800309 }
Colin Cross1d2cf042019-03-29 15:33:06 -0700310 if g, w := rule.Outputs(), wantOutputs; !reflect.DeepEqual(w, g) {
311 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", w, g)
312 }
313 if g, w := rule.DepFiles(), wantDepFiles; !reflect.DeepEqual(w, g) {
314 t.Errorf("\nwant rule.DepFiles() = %#v\n got %#v", w, g)
315 }
316 if g, w := rule.Tools(), wantTools; !reflect.DeepEqual(w, g) {
317 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", w, g)
Colin Crossfeec25b2019-01-30 17:32:39 -0800318 }
319}
320
321func testRuleBuilderFactory() Module {
322 module := &testRuleBuilderModule{}
323 module.AddProperties(&module.properties)
324 InitAndroidModule(module)
325 return module
326}
327
328type testRuleBuilderModule struct {
329 ModuleBase
330 properties struct {
331 Src string
332 }
333}
334
335func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800336 in := PathForSource(ctx, t.properties.Src)
337 out := PathForModuleOut(ctx, ctx.ModuleName())
338
Colin Cross786cd6d2019-02-01 16:41:11 -0800339 testRuleBuilder_Build(ctx, in, out)
340}
341
342type testRuleBuilderSingleton struct{}
343
344func testRuleBuilderSingletonFactory() Singleton {
345 return &testRuleBuilderSingleton{}
346}
347
348func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
349 in := PathForSource(ctx, "bar")
350 out := PathForOutput(ctx, "baz")
351 testRuleBuilder_Build(ctx, in, out)
352}
353
354func testRuleBuilder_Build(ctx BuilderContext, in Path, out WritablePath) {
Colin Cross758290d2019-02-01 16:42:32 -0800355 rule := NewRuleBuilder()
Colin Cross786cd6d2019-02-01 16:41:11 -0800356
Colin Cross69f59a32019-02-15 10:39:37 -0800357 rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out)
Colin Crossfeec25b2019-01-30 17:32:39 -0800358
Colin Crossbaa676f2019-02-25 14:56:01 -0800359 rule.Restat()
360
Colin Crossfeec25b2019-01-30 17:32:39 -0800361 rule.Build(pctx, ctx, "rule", "desc")
362}
363
364func TestRuleBuilder_Build(t *testing.T) {
365 buildDir, err := ioutil.TempDir("", "soong_test_rule_builder")
366 if err != nil {
367 t.Fatal(err)
368 }
369 defer os.RemoveAll(buildDir)
370
371 bp := `
372 rule_builder_test {
373 name: "foo",
374 src: "bar",
375 }
376 `
377
378 config := TestConfig(buildDir, nil)
379 ctx := NewTestContext()
380 ctx.MockFileSystem(map[string][]byte{
381 "Android.bp": []byte(bp),
382 "bar": nil,
383 "cp": nil,
384 })
385 ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory))
Colin Cross786cd6d2019-02-01 16:41:11 -0800386 ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory))
Colin Crossfeec25b2019-01-30 17:32:39 -0800387 ctx.Register()
388
389 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
390 FailIfErrored(t, errs)
391 _, errs = ctx.PrepareBuildActions(config)
392 FailIfErrored(t, errs)
393
Colin Cross4c83e5c2019-02-25 14:54:28 -0800394 check := func(t *testing.T, params TestingBuildParams, wantOutput string) {
395 if len(params.RuleParams.CommandDeps) != 1 || params.RuleParams.CommandDeps[0] != "cp" {
396 t.Errorf("want RuleParams.CommandDeps = [%q], got %q", "cp", params.RuleParams.CommandDeps)
397 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800398
Colin Cross4c83e5c2019-02-25 14:54:28 -0800399 if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" {
400 t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings())
401 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800402
Colin Cross1d2cf042019-03-29 15:33:06 -0700403 if params.Output.String() != wantOutput {
404 t.Errorf("want Output = %q, got %q", wantOutput, params.Output)
Colin Cross4c83e5c2019-02-25 14:54:28 -0800405 }
Colin Crossbaa676f2019-02-25 14:56:01 -0800406
407 if !params.RuleParams.Restat {
408 t.Errorf("want RuleParams.Restat = true, got %v", params.RuleParams.Restat)
409 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800410 }
411
Colin Cross4c83e5c2019-02-25 14:54:28 -0800412 t.Run("module", func(t *testing.T) {
413 check(t, ctx.ModuleForTests("foo", "").Rule("rule"),
414 filepath.Join(buildDir, ".intermediates", "foo", "foo"))
415 })
416 t.Run("singleton", func(t *testing.T) {
417 check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"),
418 filepath.Join(buildDir, "baz"))
419 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800420}