blob: 07de999bc21bba700f88e63429323f71957c7bd5 [file] [log] [blame]
Colin Cross2a076922018-10-04 23:28:25 -07001// Copyright 2018 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 genrule
16
17import (
18 "io/ioutil"
19 "os"
Colin Crossba71a3f2019-03-18 12:12:48 -070020 "reflect"
Colin Cross2a076922018-10-04 23:28:25 -070021 "strings"
22 "testing"
23
24 "android/soong/android"
Colin Crossba71a3f2019-03-18 12:12:48 -070025
26 "github.com/google/blueprint/proptools"
Colin Cross2a076922018-10-04 23:28:25 -070027)
28
29var buildDir string
30
31func setUp() {
32 var err error
Colin Crossef354482018-10-23 11:27:50 -070033 buildDir, err = ioutil.TempDir("", "genrule_test")
Colin Cross2a076922018-10-04 23:28:25 -070034 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
54func testContext(config android.Config, bp string,
55 fs map[string][]byte) *android.TestContext {
56
57 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -080058 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
59 ctx.RegisterModuleType("genrule", GenRuleFactory)
60 ctx.RegisterModuleType("gensrcs", GenSrcsFactory)
61 ctx.RegisterModuleType("genrule_defaults", defaultsFactory)
62 ctx.RegisterModuleType("tool", toolFactory)
Jaewoong Jung98716bd2018-12-10 08:13:18 -080063 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Colin Cross2a076922018-10-04 23:28:25 -070064 ctx.Register()
65
66 bp += `
67 tool {
68 name: "tool",
69 }
70
71 filegroup {
72 name: "tool_files",
73 srcs: [
74 "tool_file1",
75 "tool_file2",
76 ],
77 }
78
79 filegroup {
80 name: "1tool_file",
81 srcs: [
82 "tool_file1",
83 ],
84 }
85
86 filegroup {
87 name: "ins",
88 srcs: [
89 "in1",
90 "in2",
91 ],
92 }
93
94 filegroup {
95 name: "1in",
96 srcs: [
97 "in1",
98 ],
99 }
100
101 filegroup {
102 name: "empty",
103 }
104 `
105
106 mockFS := map[string][]byte{
107 "Android.bp": []byte(bp),
108 "tool": nil,
109 "tool_file1": nil,
110 "tool_file2": nil,
111 "in1": nil,
112 "in2": nil,
Colin Cross1a527682019-09-23 15:55:30 -0700113 "in1.txt": nil,
114 "in2.txt": nil,
115 "in3.txt": nil,
Colin Cross2a076922018-10-04 23:28:25 -0700116 }
117
118 for k, v := range fs {
119 mockFS[k] = v
120 }
121
122 ctx.MockFileSystem(mockFS)
123
124 return ctx
125}
126
127func TestGenruleCmd(t *testing.T) {
128 testcases := []struct {
129 name string
130 prop string
131
Colin Crossba71a3f2019-03-18 12:12:48 -0700132 allowMissingDependencies bool
133
Colin Cross2a076922018-10-04 23:28:25 -0700134 err string
135 expect string
136 }{
137 {
138 name: "empty location tool",
139 prop: `
140 tools: ["tool"],
141 out: ["out"],
142 cmd: "$(location) > $(out)",
143 `,
144 expect: "out/tool > __SBOX_OUT_FILES__",
145 },
146 {
Colin Cross08f15ab2018-10-04 23:29:14 -0700147 name: "empty location tool2",
148 prop: `
149 tools: [":tool"],
150 out: ["out"],
151 cmd: "$(location) > $(out)",
152 `,
153 expect: "out/tool > __SBOX_OUT_FILES__",
154 },
155 {
Colin Cross2a076922018-10-04 23:28:25 -0700156 name: "empty location tool file",
157 prop: `
158 tool_files: ["tool_file1"],
159 out: ["out"],
160 cmd: "$(location) > $(out)",
161 `,
162 expect: "tool_file1 > __SBOX_OUT_FILES__",
163 },
164 {
165 name: "empty location tool file fg",
166 prop: `
167 tool_files: [":1tool_file"],
168 out: ["out"],
169 cmd: "$(location) > $(out)",
170 `,
171 expect: "tool_file1 > __SBOX_OUT_FILES__",
172 },
173 {
174 name: "empty location tool and tool file",
175 prop: `
176 tools: ["tool"],
177 tool_files: ["tool_file1"],
178 out: ["out"],
179 cmd: "$(location) > $(out)",
180 `,
181 expect: "out/tool > __SBOX_OUT_FILES__",
182 },
183 {
184 name: "tool",
185 prop: `
186 tools: ["tool"],
187 out: ["out"],
188 cmd: "$(location tool) > $(out)",
189 `,
190 expect: "out/tool > __SBOX_OUT_FILES__",
191 },
192 {
Colin Cross08f15ab2018-10-04 23:29:14 -0700193 name: "tool2",
194 prop: `
195 tools: [":tool"],
196 out: ["out"],
197 cmd: "$(location :tool) > $(out)",
198 `,
199 expect: "out/tool > __SBOX_OUT_FILES__",
200 },
201 {
Colin Cross2a076922018-10-04 23:28:25 -0700202 name: "tool file",
203 prop: `
204 tool_files: ["tool_file1"],
205 out: ["out"],
206 cmd: "$(location tool_file1) > $(out)",
207 `,
208 expect: "tool_file1 > __SBOX_OUT_FILES__",
209 },
210 {
211 name: "tool file fg",
212 prop: `
213 tool_files: [":1tool_file"],
214 out: ["out"],
Colin Cross08f15ab2018-10-04 23:29:14 -0700215 cmd: "$(location :1tool_file) > $(out)",
Colin Cross2a076922018-10-04 23:28:25 -0700216 `,
217 expect: "tool_file1 > __SBOX_OUT_FILES__",
218 },
219 {
220 name: "tool files",
221 prop: `
222 tool_files: [":tool_files"],
223 out: ["out"],
Colin Cross08f15ab2018-10-04 23:29:14 -0700224 cmd: "$(locations :tool_files) > $(out)",
Colin Cross2a076922018-10-04 23:28:25 -0700225 `,
226 expect: "tool_file1 tool_file2 > __SBOX_OUT_FILES__",
227 },
228 {
229 name: "in1",
230 prop: `
231 srcs: ["in1"],
232 out: ["out"],
233 cmd: "cat $(in) > $(out)",
234 `,
235 expect: "cat ${in} > __SBOX_OUT_FILES__",
236 },
237 {
238 name: "in1 fg",
239 prop: `
240 srcs: [":1in"],
241 out: ["out"],
242 cmd: "cat $(in) > $(out)",
243 `,
244 expect: "cat ${in} > __SBOX_OUT_FILES__",
245 },
246 {
247 name: "ins",
248 prop: `
249 srcs: ["in1", "in2"],
250 out: ["out"],
251 cmd: "cat $(in) > $(out)",
252 `,
253 expect: "cat ${in} > __SBOX_OUT_FILES__",
254 },
255 {
256 name: "ins fg",
257 prop: `
258 srcs: [":ins"],
259 out: ["out"],
260 cmd: "cat $(in) > $(out)",
261 `,
262 expect: "cat ${in} > __SBOX_OUT_FILES__",
263 },
264 {
Colin Cross08f15ab2018-10-04 23:29:14 -0700265 name: "location in1",
266 prop: `
267 srcs: ["in1"],
268 out: ["out"],
269 cmd: "cat $(location in1) > $(out)",
270 `,
271 expect: "cat in1 > __SBOX_OUT_FILES__",
272 },
273 {
274 name: "location in1 fg",
275 prop: `
276 srcs: [":1in"],
277 out: ["out"],
278 cmd: "cat $(location :1in) > $(out)",
279 `,
280 expect: "cat in1 > __SBOX_OUT_FILES__",
281 },
282 {
283 name: "location ins",
284 prop: `
285 srcs: ["in1", "in2"],
286 out: ["out"],
287 cmd: "cat $(location in1) > $(out)",
288 `,
289 expect: "cat in1 > __SBOX_OUT_FILES__",
290 },
291 {
292 name: "location ins fg",
293 prop: `
294 srcs: [":ins"],
295 out: ["out"],
296 cmd: "cat $(locations :ins) > $(out)",
297 `,
298 expect: "cat in1 in2 > __SBOX_OUT_FILES__",
299 },
300 {
Colin Cross2a076922018-10-04 23:28:25 -0700301 name: "outs",
302 prop: `
303 out: ["out", "out2"],
304 cmd: "echo foo > $(out)",
305 `,
306 expect: "echo foo > __SBOX_OUT_FILES__",
307 },
308 {
Colin Cross08f15ab2018-10-04 23:29:14 -0700309 name: "location out",
310 prop: `
311 out: ["out", "out2"],
312 cmd: "echo foo > $(location out2)",
313 `,
314 expect: "echo foo > __SBOX_OUT_DIR__/out2",
315 },
316 {
Colin Cross2a076922018-10-04 23:28:25 -0700317 name: "depfile",
318 prop: `
319 out: ["out"],
320 depfile: true,
321 cmd: "echo foo > $(out) && touch $(depfile)",
322 `,
323 expect: "echo foo > __SBOX_OUT_FILES__ && touch __SBOX_DEPFILE__",
324 },
325 {
326 name: "gendir",
327 prop: `
328 out: ["out"],
329 cmd: "echo foo > $(genDir)/foo && cp $(genDir)/foo $(out)",
330 `,
331 expect: "echo foo > __SBOX_OUT_DIR__/foo && cp __SBOX_OUT_DIR__/foo __SBOX_OUT_FILES__",
332 },
333
334 {
335 name: "error empty location",
336 prop: `
337 out: ["out"],
338 cmd: "$(location) > $(out)",
339 `,
340 err: "at least one `tools` or `tool_files` is required if $(location) is used",
341 },
342 {
Colin Cross08f15ab2018-10-04 23:29:14 -0700343 name: "error empty location no files",
344 prop: `
345 tool_files: [":empty"],
346 out: ["out"],
347 cmd: "$(location) > $(out)",
348 `,
349 err: `default label ":empty" has no files`,
350 },
351 {
352 name: "error empty location multiple files",
353 prop: `
354 tool_files: [":tool_files"],
355 out: ["out"],
356 cmd: "$(location) > $(out)",
357 `,
358 err: `default label ":tool_files" has multiple files`,
359 },
360 {
Colin Cross2a076922018-10-04 23:28:25 -0700361 name: "error location",
362 prop: `
363 out: ["out"],
364 cmd: "echo foo > $(location missing)",
365 `,
366 err: `unknown location label "missing"`,
367 },
368 {
Colin Cross08f15ab2018-10-04 23:29:14 -0700369 name: "error locations",
370 prop: `
371 out: ["out"],
372 cmd: "echo foo > $(locations missing)",
373 `,
374 err: `unknown locations label "missing"`,
375 },
376 {
377 name: "error location no files",
378 prop: `
379 out: ["out"],
380 srcs: [":empty"],
381 cmd: "echo $(location :empty) > $(out)",
382 `,
383 err: `label ":empty" has no files`,
384 },
385 {
386 name: "error locations no files",
387 prop: `
388 out: ["out"],
389 srcs: [":empty"],
390 cmd: "echo $(locations :empty) > $(out)",
391 `,
392 err: `label ":empty" has no files`,
393 },
394 {
395 name: "error location multiple files",
396 prop: `
397 out: ["out"],
398 srcs: [":ins"],
399 cmd: "echo $(location :ins) > $(out)",
400 `,
401 err: `label ":ins" has multiple files`,
402 },
403 {
Colin Cross2a076922018-10-04 23:28:25 -0700404 name: "error variable",
405 prop: `
406 out: ["out"],
407 srcs: ["in1"],
408 cmd: "echo $(foo) > $(out)",
409 `,
410 err: `unknown variable '$(foo)'`,
411 },
412 {
413 name: "error depfile",
414 prop: `
415 out: ["out"],
416 cmd: "echo foo > $(out) && touch $(depfile)",
417 `,
418 err: "$(depfile) used without depfile property",
419 },
420 {
421 name: "error no depfile",
422 prop: `
423 out: ["out"],
424 depfile: true,
425 cmd: "echo foo > $(out)",
426 `,
427 err: "specified depfile=true but did not include a reference to '${depfile}' in cmd",
428 },
429 {
430 name: "error no out",
431 prop: `
432 cmd: "echo foo > $(out)",
433 `,
434 err: "must have at least one output file",
435 },
Colin Crossba71a3f2019-03-18 12:12:48 -0700436 {
437 name: "srcs allow missing dependencies",
438 prop: `
439 srcs: [":missing"],
440 out: ["out"],
441 cmd: "cat $(location :missing) > $(out)",
442 `,
443
444 allowMissingDependencies: true,
445
446 expect: "cat ***missing srcs :missing*** > __SBOX_OUT_FILES__",
447 },
448 {
449 name: "tool allow missing dependencies",
450 prop: `
451 tools: [":missing"],
452 out: ["out"],
453 cmd: "$(location :missing) > $(out)",
454 `,
455
456 allowMissingDependencies: true,
457
458 expect: "***missing tool :missing*** > __SBOX_OUT_FILES__",
459 },
Colin Cross2a076922018-10-04 23:28:25 -0700460 }
461
462 for _, test := range testcases {
463 t.Run(test.name, func(t *testing.T) {
464 config := android.TestArchConfig(buildDir, nil)
465 bp := "genrule {\n"
466 bp += "name: \"gen\",\n"
467 bp += test.prop
468 bp += "}\n"
469
Colin Crossba71a3f2019-03-18 12:12:48 -0700470 config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(test.allowMissingDependencies)
471
Colin Cross2a076922018-10-04 23:28:25 -0700472 ctx := testContext(config, bp, nil)
Colin Crossba71a3f2019-03-18 12:12:48 -0700473 ctx.SetAllowMissingDependencies(test.allowMissingDependencies)
Colin Cross2a076922018-10-04 23:28:25 -0700474
475 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
476 if errs == nil {
477 _, errs = ctx.PrepareBuildActions(config)
478 }
479 if errs == nil && test.err != "" {
480 t.Fatalf("want error %q, got no error", test.err)
481 } else if errs != nil && test.err == "" {
482 android.FailIfErrored(t, errs)
483 } else if test.err != "" {
484 if len(errs) != 1 {
485 t.Errorf("want 1 error, got %d errors:", len(errs))
486 for _, err := range errs {
487 t.Errorf(" %s", err.Error())
488 }
489 t.FailNow()
490 }
491 if !strings.Contains(errs[0].Error(), test.err) {
492 t.Fatalf("want %q, got %q", test.err, errs[0].Error())
493 }
494 return
495 }
496
497 gen := ctx.ModuleForTests("gen", "").Module().(*Module)
Colin Cross1a527682019-09-23 15:55:30 -0700498 if g, w := gen.rawCommands[0], "'"+test.expect+"'"; w != g {
Colin Crossba71a3f2019-03-18 12:12:48 -0700499 t.Errorf("want %q, got %q", w, g)
Colin Cross2a076922018-10-04 23:28:25 -0700500 }
501 })
502 }
Colin Cross1a527682019-09-23 15:55:30 -0700503}
504
505func TestGenSrcs(t *testing.T) {
506 testcases := []struct {
507 name string
508 prop string
509
510 allowMissingDependencies bool
511
512 err string
513 cmds []string
514 deps []string
515 files []string
516 }{
517 {
518 name: "gensrcs",
519 prop: `
520 tools: ["tool"],
521 srcs: ["in1.txt", "in2.txt"],
522 cmd: "$(location) $(in) > $(out)",
523 `,
524 cmds: []string{
525 "'bash -c '\\''out/tool in1.txt > __SBOX_OUT_DIR__/in1.h'\\'' && bash -c '\\''out/tool in2.txt > __SBOX_OUT_DIR__/in2.h'\\'''",
526 },
527 deps: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h"},
528 files: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h"},
529 },
530 {
531 name: "shards",
532 prop: `
533 tools: ["tool"],
534 srcs: ["in1.txt", "in2.txt", "in3.txt"],
535 cmd: "$(location) $(in) > $(out)",
536 shard_size: 2,
537 `,
538 cmds: []string{
539 "'bash -c '\\''out/tool in1.txt > __SBOX_OUT_DIR__/in1.h'\\'' && bash -c '\\''out/tool in2.txt > __SBOX_OUT_DIR__/in2.h'\\'''",
540 "'bash -c '\\''out/tool in3.txt > __SBOX_OUT_DIR__/in3.h'\\'''",
541 },
542 deps: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h", buildDir + "/.intermediates/gen/gen/gensrcs/in3.h"},
543 files: []string{buildDir + "/.intermediates/gen/gen/gensrcs/in1.h", buildDir + "/.intermediates/gen/gen/gensrcs/in2.h", buildDir + "/.intermediates/gen/gen/gensrcs/in3.h"},
544 },
545 }
546
547 for _, test := range testcases {
548 t.Run(test.name, func(t *testing.T) {
549 config := android.TestArchConfig(buildDir, nil)
550 bp := "gensrcs {\n"
551 bp += `name: "gen",` + "\n"
552 bp += `output_extension: "h",` + "\n"
553 bp += test.prop
554 bp += "}\n"
555
556 ctx := testContext(config, bp, nil)
557
558 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
559 if errs == nil {
560 _, errs = ctx.PrepareBuildActions(config)
561 }
562 if errs == nil && test.err != "" {
563 t.Fatalf("want error %q, got no error", test.err)
564 } else if errs != nil && test.err == "" {
565 android.FailIfErrored(t, errs)
566 } else if test.err != "" {
567 if len(errs) != 1 {
568 t.Errorf("want 1 error, got %d errors:", len(errs))
569 for _, err := range errs {
570 t.Errorf(" %s", err.Error())
571 }
572 t.FailNow()
573 }
574 if !strings.Contains(errs[0].Error(), test.err) {
575 t.Fatalf("want %q, got %q", test.err, errs[0].Error())
576 }
577 return
578 }
579
580 gen := ctx.ModuleForTests("gen", "").Module().(*Module)
581 if g, w := gen.rawCommands, test.cmds; !reflect.DeepEqual(w, g) {
582 t.Errorf("want %q, got %q", w, g)
583 }
584
585 if g, w := gen.outputDeps.Strings(), test.deps; !reflect.DeepEqual(w, g) {
586 t.Errorf("want deps %q, got %q", w, g)
587 }
588
589 if g, w := gen.outputFiles.Strings(), test.files; !reflect.DeepEqual(w, g) {
590 t.Errorf("want files %q, got %q", w, g)
591 }
592 })
593 }
Colin Cross2a076922018-10-04 23:28:25 -0700594
595}
596
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800597func TestGenruleDefaults(t *testing.T) {
598 config := android.TestArchConfig(buildDir, nil)
599 bp := `
600 genrule_defaults {
601 name: "gen_defaults1",
602 cmd: "cp $(in) $(out)",
603 }
604
605 genrule_defaults {
606 name: "gen_defaults2",
607 srcs: ["in1"],
608 }
609
610 genrule {
611 name: "gen",
612 out: ["out"],
613 defaults: ["gen_defaults1", "gen_defaults2"],
614 }
615 `
616 ctx := testContext(config, bp, nil)
617 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
618 if errs == nil {
619 _, errs = ctx.PrepareBuildActions(config)
620 }
621 if errs != nil {
622 t.Fatal(errs)
623 }
624 gen := ctx.ModuleForTests("gen", "").Module().(*Module)
625
626 expectedCmd := "'cp ${in} __SBOX_OUT_FILES__'"
Colin Cross1a527682019-09-23 15:55:30 -0700627 if gen.rawCommands[0] != expectedCmd {
628 t.Errorf("Expected cmd: %q, actual: %q", expectedCmd, gen.rawCommands[0])
Jaewoong Jung98716bd2018-12-10 08:13:18 -0800629 }
630
631 expectedSrcs := []string{"in1"}
632 if !reflect.DeepEqual(expectedSrcs, gen.properties.Srcs) {
633 t.Errorf("Expected srcs: %q, actual: %q", expectedSrcs, gen.properties.Srcs)
634 }
635}
636
Colin Cross2a076922018-10-04 23:28:25 -0700637type testTool struct {
638 android.ModuleBase
639 outputFile android.Path
640}
641
642func toolFactory() android.Module {
643 module := &testTool{}
644 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
645 return module
646}
647
Colin Cross2a076922018-10-04 23:28:25 -0700648func (t *testTool) GenerateAndroidBuildActions(ctx android.ModuleContext) {
649 t.outputFile = android.PathForTesting("out", ctx.ModuleName())
650}
651
652func (t *testTool) HostToolPath() android.OptionalPath {
653 return android.OptionalPathForPath(t.outputFile)
654}
655
Colin Crossfe17f6f2019-03-28 19:30:56 -0700656var _ android.HostToolProvider = (*testTool)(nil)