1// 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 cc
16
17import (
18	"reflect"
19	"testing"
20
21	"android/soong/android"
22)
23
24func testGenruleContext(config android.Config) *android.TestContext {
25	ctx := android.NewTestArchContext(config)
26	ctx.RegisterModuleType("cc_genrule", genRuleFactory)
27	ctx.Register()
28
29	return ctx
30}
31
32func TestArchGenruleCmd(t *testing.T) {
33	fs := map[string][]byte{
34		"tool": nil,
35		"foo":  nil,
36		"bar":  nil,
37	}
38	bp := `
39				cc_genrule {
40					name: "gen",
41					tool_files: ["tool"],
42					cmd: "$(location tool) $(in) $(out)",
43					arch: {
44						arm: {
45							srcs: ["foo"],
46							out: ["out_arm"],
47						},
48						arm64: {
49							srcs: ["bar"],
50							out: ["out_arm64"],
51						},
52					},
53				}
54			`
55	config := android.TestArchConfig(t.TempDir(), nil, bp, fs)
56
57	ctx := testGenruleContext(config)
58
59	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
60	if errs == nil {
61		_, errs = ctx.PrepareBuildActions(config)
62	}
63	if errs != nil {
64		t.Fatal(errs)
65	}
66
67	gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out_arm")
68	expected := []string{"foo"}
69	if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) {
70		t.Errorf(`want arm inputs %v, got %v`, expected, gen.Implicits.Strings())
71	}
72
73	gen = ctx.ModuleForTests("gen", "android_arm64_armv8-a").Output("out_arm64")
74	expected = []string{"bar"}
75	if !reflect.DeepEqual(expected, gen.Implicits.Strings()[:len(expected)]) {
76		t.Errorf(`want arm64 inputs %v, got %v`, expected, gen.Implicits.Strings())
77	}
78}
79
80func TestLibraryGenruleCmd(t *testing.T) {
81	bp := `
82		cc_library {
83			name: "libboth",
84		}
85
86		cc_library_shared {
87			name: "libshared",
88		}
89
90		cc_library_static {
91			name: "libstatic",
92		}
93
94		cc_genrule {
95			name: "gen",
96			tool_files: ["tool"],
97			srcs: [
98				":libboth",
99				":libshared",
100				":libstatic",
101			],
102			cmd: "$(location tool) $(in) $(out)",
103			out: ["out"],
104		}
105		`
106	ctx := testCc(t, bp)
107
108	gen := ctx.ModuleForTests("gen", "android_arm_armv7-a-neon").Output("out")
109	expected := []string{"libboth.so", "libshared.so", "libstatic.a"}
110	var got []string
111	for _, input := range gen.Implicits {
112		got = append(got, input.Base())
113	}
114	if !reflect.DeepEqual(expected, got[:len(expected)]) {
115		t.Errorf(`want inputs %v, got %v`, expected, got)
116	}
117}
118