blob: f157ab7d92e319a99adaee201803383eb038b030 [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
174func ExampleRuleBuilderCommand_FlagWithArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800175 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800176 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800177 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800178 FlagWithArg("--sort=", "time"))
179 // Output:
180 // ls --sort=time
181}
182
Colin Crossc7ed0042019-02-11 14:11:09 -0800183func ExampleRuleBuilderCommand_FlagForEachArg() {
Colin Cross69f59a32019-02-15 10:39:37 -0800184 ctx := pathContext()
Colin Crossc7ed0042019-02-11 14:11:09 -0800185 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800186 Tool(PathForSource(ctx, "ls")).
Colin Crossc7ed0042019-02-11 14:11:09 -0800187 FlagForEachArg("--sort=", []string{"time", "size"}))
188 // Output:
189 // ls --sort=time --sort=size
190}
191
Colin Cross758290d2019-02-01 16:42:32 -0800192func ExampleRuleBuilderCommand_FlagForEachInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800193 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800194 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800195 Tool(PathForSource(ctx, "turbine")).
196 FlagForEachInput("--classpath ", PathsForTesting("a.jar", "b.jar")))
Colin Cross758290d2019-02-01 16:42:32 -0800197 // Output:
198 // turbine --classpath a.jar --classpath b.jar
199}
200
201func ExampleRuleBuilderCommand_FlagWithInputList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800202 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800203 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800204 Tool(PathForSource(ctx, "java")).
205 FlagWithInputList("-classpath=", PathsForTesting("a.jar", "b.jar"), ":"))
Colin Cross758290d2019-02-01 16:42:32 -0800206 // Output:
207 // java -classpath=a.jar:b.jar
208}
209
210func ExampleRuleBuilderCommand_FlagWithInput() {
Colin Cross69f59a32019-02-15 10:39:37 -0800211 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800212 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800213 Tool(PathForSource(ctx, "java")).
214 FlagWithInput("-classpath=", PathForSource(ctx, "a")))
Colin Cross758290d2019-02-01 16:42:32 -0800215 // Output:
216 // java -classpath=a
217}
218
219func ExampleRuleBuilderCommand_FlagWithList() {
Colin Cross69f59a32019-02-15 10:39:37 -0800220 ctx := pathContext()
Colin Cross758290d2019-02-01 16:42:32 -0800221 fmt.Println(NewRuleBuilder().Command().
Colin Cross69f59a32019-02-15 10:39:37 -0800222 Tool(PathForSource(ctx, "ls")).
Colin Cross758290d2019-02-01 16:42:32 -0800223 FlagWithList("--sort=", []string{"time", "size"}, ","))
224 // Output:
225 // ls --sort=time,size
226}
227
Colin Crossfeec25b2019-01-30 17:32:39 -0800228func TestRuleBuilder(t *testing.T) {
Colin Cross758290d2019-02-01 16:42:32 -0800229 rule := NewRuleBuilder()
Colin Crossfeec25b2019-01-30 17:32:39 -0800230
Colin Cross69f59a32019-02-15 10:39:37 -0800231 fs := map[string][]byte{
232 "input": nil,
233 "Implicit": nil,
234 "Input": nil,
235 "Tool": nil,
236 "input2": nil,
237 "tool2": nil,
238 "input3": nil,
239 }
240
241 ctx := PathContextForTesting(TestConfig("out", nil), fs)
242
Colin Crossfeec25b2019-01-30 17:32:39 -0800243 cmd := rule.Command().
244 Flag("Flag").
245 FlagWithArg("FlagWithArg=", "arg").
Colin Cross69f59a32019-02-15 10:39:37 -0800246 FlagWithInput("FlagWithInput=", PathForSource(ctx, "input")).
247 FlagWithOutput("FlagWithOutput=", PathForOutput(ctx, "output")).
248 Implicit(PathForSource(ctx, "Implicit")).
249 ImplicitOutput(PathForOutput(ctx, "ImplicitOutput")).
250 Input(PathForSource(ctx, "Input")).
251 Output(PathForOutput(ctx, "Output")).
Colin Crossfeec25b2019-01-30 17:32:39 -0800252 Text("Text").
Colin Cross69f59a32019-02-15 10:39:37 -0800253 Tool(PathForSource(ctx, "Tool"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800254
255 rule.Command().
256 Text("command2").
Colin Cross69f59a32019-02-15 10:39:37 -0800257 Input(PathForSource(ctx, "input2")).
258 Output(PathForOutput(ctx, "output2")).
259 Tool(PathForSource(ctx, "tool2"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800260
261 // Test updates to the first command after the second command has been started
262 cmd.Text("after command2")
263 // Test updating a command when the previous update did not replace the cmd variable
264 cmd.Text("old cmd")
265
266 // Test a command that uses the output of a previous command as an input
267 rule.Command().
268 Text("command3").
Colin Cross69f59a32019-02-15 10:39:37 -0800269 Input(PathForSource(ctx, "input3")).
270 Input(PathForOutput(ctx, "output2")).
271 Output(PathForOutput(ctx, "output3"))
Colin Crossfeec25b2019-01-30 17:32:39 -0800272
273 wantCommands := []string{
Colin Cross69f59a32019-02-15 10:39:37 -0800274 "Flag FlagWithArg=arg FlagWithInput=input FlagWithOutput=out/output Input out/Output Text Tool after command2 old cmd",
275 "command2 input2 out/output2 tool2",
276 "command3 input3 out/output2 out/output3",
Colin Crossfeec25b2019-01-30 17:32:39 -0800277 }
Colin Cross69f59a32019-02-15 10:39:37 -0800278 wantInputs := PathsForSource(ctx, []string{"Implicit", "Input", "input", "input2", "input3"})
279 wantOutputs := PathsForOutput(ctx, []string{"ImplicitOutput", "Output", "output", "output2", "output3"})
280 wantTools := PathsForSource(ctx, []string{"Tool", "tool2"})
Colin Crossfeec25b2019-01-30 17:32:39 -0800281
282 if !reflect.DeepEqual(rule.Commands(), wantCommands) {
283 t.Errorf("\nwant rule.Commands() = %#v\n got %#v", wantCommands, rule.Commands())
284 }
285 if !reflect.DeepEqual(rule.Inputs(), wantInputs) {
286 t.Errorf("\nwant rule.Inputs() = %#v\n got %#v", wantInputs, rule.Inputs())
287 }
288 if !reflect.DeepEqual(rule.Outputs(), wantOutputs) {
289 t.Errorf("\nwant rule.Outputs() = %#v\n got %#v", wantOutputs, rule.Outputs())
290 }
291 if !reflect.DeepEqual(rule.Tools(), wantTools) {
292 t.Errorf("\nwant rule.Tools() = %#v\n got %#v", wantTools, rule.Tools())
293 }
294}
295
296func testRuleBuilderFactory() Module {
297 module := &testRuleBuilderModule{}
298 module.AddProperties(&module.properties)
299 InitAndroidModule(module)
300 return module
301}
302
303type testRuleBuilderModule struct {
304 ModuleBase
305 properties struct {
306 Src string
307 }
308}
309
310func (t *testRuleBuilderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800311 in := PathForSource(ctx, t.properties.Src)
312 out := PathForModuleOut(ctx, ctx.ModuleName())
313
Colin Cross786cd6d2019-02-01 16:41:11 -0800314 testRuleBuilder_Build(ctx, in, out)
315}
316
317type testRuleBuilderSingleton struct{}
318
319func testRuleBuilderSingletonFactory() Singleton {
320 return &testRuleBuilderSingleton{}
321}
322
323func (t *testRuleBuilderSingleton) GenerateBuildActions(ctx SingletonContext) {
324 in := PathForSource(ctx, "bar")
325 out := PathForOutput(ctx, "baz")
326 testRuleBuilder_Build(ctx, in, out)
327}
328
329func testRuleBuilder_Build(ctx BuilderContext, in Path, out WritablePath) {
Colin Cross758290d2019-02-01 16:42:32 -0800330 rule := NewRuleBuilder()
Colin Cross786cd6d2019-02-01 16:41:11 -0800331
Colin Cross69f59a32019-02-15 10:39:37 -0800332 rule.Command().Tool(PathForSource(ctx, "cp")).Input(in).Output(out)
Colin Crossfeec25b2019-01-30 17:32:39 -0800333
334 rule.Build(pctx, ctx, "rule", "desc")
335}
336
337func TestRuleBuilder_Build(t *testing.T) {
338 buildDir, err := ioutil.TempDir("", "soong_test_rule_builder")
339 if err != nil {
340 t.Fatal(err)
341 }
342 defer os.RemoveAll(buildDir)
343
344 bp := `
345 rule_builder_test {
346 name: "foo",
347 src: "bar",
348 }
349 `
350
351 config := TestConfig(buildDir, nil)
352 ctx := NewTestContext()
353 ctx.MockFileSystem(map[string][]byte{
354 "Android.bp": []byte(bp),
355 "bar": nil,
356 "cp": nil,
357 })
358 ctx.RegisterModuleType("rule_builder_test", ModuleFactoryAdaptor(testRuleBuilderFactory))
Colin Cross786cd6d2019-02-01 16:41:11 -0800359 ctx.RegisterSingletonType("rule_builder_test", SingletonFactoryAdaptor(testRuleBuilderSingletonFactory))
Colin Crossfeec25b2019-01-30 17:32:39 -0800360 ctx.Register()
361
362 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
363 FailIfErrored(t, errs)
364 _, errs = ctx.PrepareBuildActions(config)
365 FailIfErrored(t, errs)
366
Colin Cross4c83e5c2019-02-25 14:54:28 -0800367 check := func(t *testing.T, params TestingBuildParams, wantOutput string) {
368 if len(params.RuleParams.CommandDeps) != 1 || params.RuleParams.CommandDeps[0] != "cp" {
369 t.Errorf("want RuleParams.CommandDeps = [%q], got %q", "cp", params.RuleParams.CommandDeps)
370 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800371
Colin Cross4c83e5c2019-02-25 14:54:28 -0800372 if len(params.Implicits) != 1 || params.Implicits[0].String() != "bar" {
373 t.Errorf("want Implicits = [%q], got %q", "bar", params.Implicits.Strings())
374 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800375
Colin Cross4c83e5c2019-02-25 14:54:28 -0800376 if len(params.Outputs) != 1 || params.Outputs[0].String() != wantOutput {
377 t.Errorf("want Outputs = [%q], got %q", wantOutput, params.Outputs.Strings())
378 }
Colin Crossfeec25b2019-01-30 17:32:39 -0800379 }
380
Colin Cross4c83e5c2019-02-25 14:54:28 -0800381 t.Run("module", func(t *testing.T) {
382 check(t, ctx.ModuleForTests("foo", "").Rule("rule"),
383 filepath.Join(buildDir, ".intermediates", "foo", "foo"))
384 })
385 t.Run("singleton", func(t *testing.T) {
386 check(t, ctx.SingletonForTests("rule_builder_test").Rule("rule"),
387 filepath.Join(buildDir, "baz"))
388 })
Colin Crossfeec25b2019-01-30 17:32:39 -0800389}