blob: 1012768088f457da42a9cb997ac1a83fdaca8719 [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{
240 "input": nil,
241 "Implicit": nil,
242 "Input": nil,
243 "Tool": nil,
244 "input2": nil,
245 "tool2": nil,
246 "input3": nil,
247 }
248
249 ctx := PathContextForTesting(TestConfig("out", nil), fs)
250
Colin Crossfeec25b2019-01-30 17:32:39 -0800251 cmd := rule.Command().
252 Flag("Flag").
253 FlagWithArg("FlagWithArg=", "arg").
Colin Cross69f59a32019-02-15 10:39:37 -0800254 FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
255 FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
256 Implicit(PathForSource(ctx, "Implicit")).
257 ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
258 Input(PathForSource(ctx, "Input")).
259 Output(PathForOutput(ctx, "Output")).
Colin Crossfeec25b2019-01-30 17:32:39 -0800260 Text("Text").
Colin Cross69f59a32019-02-15 10:39:37 -0800261 Tool(PathForSource(ctx, "Tool"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800262
263 rule.Command().
264 Text("command2").
Colin Cross69f59a32019-02-15 10:39:37 -0800265 Input(PathForSource(ctx, "input2")).
266 Output(PathForOutput(ctx, "output2")).
267 Tool(PathForSource(ctx, "tool2"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800268
269 // Test updates to the first command after the second command has been started
270 cmd.Text("after command2")
271 // Test updating a command when the previous update did not replace the cmd variable
272 cmd.Text("old cmd")
273
274 // Test a command that uses the output of a previous command as an input
275 rule.Command().
276 Text("command3").
Colin Cross69f59a32019-02-15 10:39:37 -0800277 Input(PathForSource(ctx, "input3")).
278 Input(PathForOutput(ctx, "output2")).
279 Output(PathForOutput(ctx, "output3"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800280
281 wantCommands := []string{
Colin Cross69f59a32019-02-15 10:39:37 -0800282 "Flag FlagWithArg=arg FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd",
283 "command2 input2 out/output2 tool2",
284 "command3 input3 out/output2 out/output3",
Colin Crossfeec25b2019-01-30 17:32:39 -0800285 }
Colin Cross69f59a32019-02-15 10:39:37 -0800286 wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
287 wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"})
288 wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
Colin Crossfeec25b2019-01-30 17:32:39 -0800289
290 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
291 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", wantCommands, rule.Commands())
292 }
293 if !reflect.DeepEqual(rule.Inputs(), wantInputs) {
294 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", wantInputs, rule.Inputs())
295 }
296 if !reflect.DeepEqual(rule.Outputs(), wantOutputs) {
297 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", wantOutputs, rule.Outputs())
298 }
299 if !reflect.DeepEqual(rule.Tools(), wantTools) {
300 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", wantTools, rule.Tools())
301 }
302}
303
304func testRuleBuilderFactory() Module {
305 module := &testRuleBuilderModule{}
306 module.AddProperties(&module.properties)
307 InitAndroidModule(module)
308 return module
309}
310
311type testRuleBuilderModule struct {
312 ModuleBase
313 properties struct {
314 Src string
315 }
316}
317
318func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800319 in := PathForSource(ctx, t.properties.Src)
320 out := PathForModuleOut(ctx, ctx.ModuleName())
321
Colin Cross786cd6d2019-02-01 16:41:11 -0800322 testRuleBuilder_Build(ctx, in, out)
323}
324
325type testRuleBuilderSingleton struct{}
326
327func testRuleBuilderSingletonFactory() Singleton {
328 return &testRuleBuilderSingleton{}
329}
330
331func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
332 in := PathForSource(ctx, "bar")
333 out := PathForOutput(ctx, "baz")
334 testRuleBuilder_Build(ctx, in, out)
335}
336
337func testRuleBuilder_Build(ctx BuilderContext, in Path, out WritablePath) {
Colin Cross758290d2019-02-01 16:42:32 -0800338 rule := NewRuleBuilder()
Colin Cross786cd6d2019-02-01 16:41:11 -0800339
Colin Cross69f59a32019-02-15 10:39:37 -0800340 rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out)
Colin Crossfeec25b2019-01-30 17:32:39 -0800341
Colin Crossbaa676f2019-02-25 14:56:01 -0800342 rule.Restat()
343
Colin Crossfeec25b2019-01-30 17:32:39 -0800344 rule.Build(pctx, ctx, "rule", "desc")
345}
346
347func TestRuleBuilder_Build(t *testing.T) {
348 buildDir, err := ioutil.TempDir("", "soong_test_rule_builder")
349 if err != nil {
350 t.Fatal(err)
351 }
352 defer os.RemoveAll(buildDir)
353
354 bp := `
355 rule_builder_test {
356 name: "foo",
357 src: "bar",
358 }
359 `
360
361 config := TestConfig(buildDir, nil)
362 ctx := NewTestContext()
363 ctx.MockFileSystem(map[string][]byte{
364 "Android.bp": []byte(bp),
365 "bar": nil,
366 "cp": nil,
367 })
368 ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory))
Colin Cross786cd6d2019-02-01 16:41:11 -0800369 ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory))
Colin Crossfeec25b2019-01-30 17:32:39 -0800370 ctx.Register()
371
372 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
373 FailIfErrored(t, errs)
374 _, errs = ctx.PrepareBuildActions(config)
375 FailIfErrored(t, errs)
376
Colin Cross4c83e5c2019-02-25 14:54:28 -0800377 check := func(t *testing.T, params TestingBuildParams, wantOutput string) {
378 if len(params.RuleParams.CommandDeps) != 1 || params.RuleParams.CommandDeps[0] != "cp" {
379 t.Errorf("want RuleParams.CommandDeps = [%q], got %q", "cp", params.RuleParams.CommandDeps)
380 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800381
Colin Cross4c83e5c2019-02-25 14:54:28 -0800382 if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" {
383 t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings())
384 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800385
Colin Cross4c83e5c2019-02-25 14:54:28 -0800386 if len(params.Outputs) != 1 || params.Outputs[0].String() != wantOutput {
387 t.Errorf("want Outputs = [%q], got %q", wantOutput, params.Outputs.Strings())
388 }
Colin Crossbaa676f2019-02-25 14:56:01 -0800389
390 if !params.RuleParams.Restat {
391 t.Errorf("want RuleParams.Restat = true, got %v", params.RuleParams.Restat)
392 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800393 }
394
Colin Cross4c83e5c2019-02-25 14:54:28 -0800395 t.Run("module", func(t *testing.T) {
396 check(t, ctx.ModuleForTests("foo", "").Rule("rule"),
397 filepath.Join(buildDir, ".intermediates", "foo", "foo"))
398 })
399 t.Run("singleton", func(t *testing.T) {
400 check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"),
401 filepath.Join(buildDir, "baz"))
402 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800403}