blob: a7e9cb21accd3c30bbf85d5eda2f919fc5bf4f6f [file] [log] [blame]
Wei Libcd39942021-09-16 23:57:28 +00001// Copyright 2021 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 bp2build
16
17import (
18 "testing"
19
20 "android/soong/android"
21 "android/soong/cc"
22 "android/soong/genrule"
23)
24
25var otherCcGenruleBp = map[string]string{
26 "other/Android.bp": `cc_genrule {
27 name: "foo.tool",
28 out: ["foo_tool.out"],
29 srcs: ["foo_tool.in"],
30 cmd: "cp $(in) $(out)",
31}
32cc_genrule {
33 name: "other.tool",
34 out: ["other_tool.out"],
35 srcs: ["other_tool.in"],
36 cmd: "cp $(in) $(out)",
37}`,
38}
39
40func runCcGenruleTestCase(t *testing.T, tc bp2buildTestCase) {
41 t.Helper()
42 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
43}
44
45func TestCliVariableReplacement(t *testing.T) {
46 runCcGenruleTestCase(t, bp2buildTestCase{
47 description: "cc_genrule with command line variable replacements",
48 moduleTypeUnderTest: "cc_genrule",
49 moduleTypeUnderTestFactory: cc.GenRuleFactory,
50 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
51 blueprint: `cc_genrule {
52 name: "foo.tool",
53 out: ["foo_tool.out"],
54 srcs: ["foo_tool.in"],
55 cmd: "cp $(in) $(out)",
56 bazel_module: { bp2build_available: true },
57}
58
59cc_genrule {
60 name: "foo",
61 out: ["foo.out"],
62 srcs: ["foo.in"],
63 tools: [":foo.tool"],
64 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
65 bazel_module: { bp2build_available: true },
66}`,
67 expectedBazelTargets: []string{
68 `genrule(
69 name = "foo",
70 cmd = "$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)",
71 outs = ["foo.out"],
72 srcs = ["foo.in"],
73 tools = [":foo.tool"],
74)`,
75 `genrule(
76 name = "foo.tool",
77 cmd = "cp $(SRCS) $(OUTS)",
78 outs = ["foo_tool.out"],
79 srcs = ["foo_tool.in"],
80)`,
81 },
82 })
83}
84
85func TestUsingLocationsLabel(t *testing.T) {
86 runCcGenruleTestCase(t, bp2buildTestCase{
87 description: "cc_genrule using $(locations :label)",
88 moduleTypeUnderTest: "cc_genrule",
89 moduleTypeUnderTestFactory: cc.GenRuleFactory,
90 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
91 blueprint: `cc_genrule {
92 name: "foo.tools",
93 out: ["foo_tool.out", "foo_tool2.out"],
94 srcs: ["foo_tool.in"],
95 cmd: "cp $(in) $(out)",
96 bazel_module: { bp2build_available: true },
97}
98
99cc_genrule {
100 name: "foo",
101 out: ["foo.out"],
102 srcs: ["foo.in"],
103 tools: [":foo.tools"],
104 cmd: "$(locations :foo.tools) -s $(out) $(in)",
105 bazel_module: { bp2build_available: true },
106}`,
107 expectedBazelTargets: []string{`genrule(
108 name = "foo",
109 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
110 outs = ["foo.out"],
111 srcs = ["foo.in"],
112 tools = [":foo.tools"],
113)`,
114 `genrule(
115 name = "foo.tools",
116 cmd = "cp $(SRCS) $(OUTS)",
117 outs = [
118 "foo_tool.out",
119 "foo_tool2.out",
120 ],
121 srcs = ["foo_tool.in"],
122)`,
123 },
124 })
125}
126
127func TestUsingLocationsAbsoluteLabel(t *testing.T) {
128 runCcGenruleTestCase(t, bp2buildTestCase{
129 description: "cc_genrule using $(locations //absolute:label)",
130 moduleTypeUnderTest: "cc_genrule",
131 moduleTypeUnderTestFactory: cc.GenRuleFactory,
132 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
133 blueprint: `cc_genrule {
134 name: "foo",
135 out: ["foo.out"],
136 srcs: ["foo.in"],
137 tool_files: [":foo.tool"],
138 cmd: "$(locations :foo.tool) -s $(out) $(in)",
139 bazel_module: { bp2build_available: true },
140}`,
141 expectedBazelTargets: []string{`genrule(
142 name = "foo",
143 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
144 outs = ["foo.out"],
145 srcs = ["foo.in"],
146 tools = ["//other:foo.tool"],
147)`,
148 },
149 filesystem: otherCcGenruleBp,
150 })
151}
152
153func TestSrcsUsingAbsoluteLabel(t *testing.T) {
154 runCcGenruleTestCase(t, bp2buildTestCase{
155 description: "cc_genrule srcs using $(locations //absolute:label)",
156 moduleTypeUnderTest: "cc_genrule",
157 moduleTypeUnderTestFactory: cc.GenRuleFactory,
158 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
159 blueprint: `cc_genrule {
160 name: "foo",
161 out: ["foo.out"],
162 srcs: [":other.tool"],
163 tool_files: [":foo.tool"],
164 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
165 bazel_module: { bp2build_available: true },
166}`,
167 expectedBazelTargets: []string{`genrule(
168 name = "foo",
169 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)",
170 outs = ["foo.out"],
171 srcs = ["//other:other.tool"],
172 tools = ["//other:foo.tool"],
173)`,
174 },
175 filesystem: otherCcGenruleBp,
176 })
177}
178
179func TestLocationsLabelUsesFirstToolFile(t *testing.T) {
180 runCcGenruleTestCase(t, bp2buildTestCase{
181 description: "cc_genrule using $(location) label should substitute first tool label automatically",
182 moduleTypeUnderTest: "cc_genrule",
183 moduleTypeUnderTestFactory: cc.GenRuleFactory,
184 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
185 blueprint: `cc_genrule {
186 name: "foo",
187 out: ["foo.out"],
188 srcs: ["foo.in"],
189 tool_files: [":foo.tool", ":other.tool"],
190 cmd: "$(location) -s $(out) $(in)",
191 bazel_module: { bp2build_available: true },
192}`,
193 expectedBazelTargets: []string{`genrule(
194 name = "foo",
195 cmd = "$(location //other:foo.tool) -s $(OUTS) $(SRCS)",
196 outs = ["foo.out"],
197 srcs = ["foo.in"],
198 tools = [
199 "//other:foo.tool",
200 "//other:other.tool",
201 ],
202)`,
203 },
204 filesystem: otherCcGenruleBp,
205 })
206}
207
208func TestLocationsLabelUsesFirstTool(t *testing.T) {
209 runCcGenruleTestCase(t, bp2buildTestCase{
210 description: "cc_genrule using $(locations) label should substitute first tool label automatically",
211 moduleTypeUnderTest: "cc_genrule",
212 moduleTypeUnderTestFactory: cc.GenRuleFactory,
213 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
214 blueprint: `cc_genrule {
215 name: "foo",
216 out: ["foo.out"],
217 srcs: ["foo.in"],
218 tools: [":foo.tool", ":other.tool"],
219 cmd: "$(locations) -s $(out) $(in)",
220 bazel_module: { bp2build_available: true },
221}`,
222 expectedBazelTargets: []string{`genrule(
223 name = "foo",
224 cmd = "$(locations //other:foo.tool) -s $(OUTS) $(SRCS)",
225 outs = ["foo.out"],
226 srcs = ["foo.in"],
227 tools = [
228 "//other:foo.tool",
229 "//other:other.tool",
230 ],
231)`,
232 },
233 filesystem: otherCcGenruleBp,
234 })
235}
236
237func TestWithoutToolsOrToolFiles(t *testing.T) {
238 runCcGenruleTestCase(t, bp2buildTestCase{
239 description: "cc_genrule without tools or tool_files can convert successfully",
240 moduleTypeUnderTest: "cc_genrule",
241 moduleTypeUnderTestFactory: cc.GenRuleFactory,
242 moduleTypeUnderTestBp2BuildMutator: genrule.CcGenruleBp2Build,
243 blueprint: `cc_genrule {
244 name: "foo",
245 out: ["foo.out"],
246 srcs: ["foo.in"],
247 cmd: "cp $(in) $(out)",
248 bazel_module: { bp2build_available: true },
249}`,
250 expectedBazelTargets: []string{`genrule(
251 name = "foo",
252 cmd = "cp $(SRCS) $(OUTS)",
253 outs = ["foo.out"],
254 srcs = ["foo.in"],
255)`,
256 },
257 })
258}