blob: b3624ddb0acb0ae7bf5d742a3c4b4d6e738db7fe [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()
Liz Kammer78cfdaa2021-11-08 12:56:31 -050042 (&tc).moduleTypeUnderTest = "cc_genrule"
43 (&tc).moduleTypeUnderTestFactory = cc.GenRuleFactory
44 (&tc).moduleTypeUnderTestBp2BuildMutator = genrule.CcGenruleBp2Build
Wei Libcd39942021-09-16 23:57:28 +000045 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc)
46}
47
48func TestCliVariableReplacement(t *testing.T) {
49 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050050 description: "cc_genrule with command line variable replacements",
Wei Libcd39942021-09-16 23:57:28 +000051 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{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050068 makeBazelTarget("genrule", "foo", attrNameToString{
69 "cmd": `"$(location :foo.tool) --genDir=$(RULEDIR) arg $(SRCS) $(OUTS)"`,
70 "outs": `["foo.out"]`,
71 "srcs": `["foo.in"]`,
72 "tools": `[":foo.tool"]`,
73 }),
74 makeBazelTarget("genrule", "foo.tool", attrNameToString{
75 "cmd": `"cp $(SRCS) $(OUTS)"`,
76 "outs": `["foo_tool.out"]`,
77 "srcs": `["foo_tool.in"]`,
78 }),
Wei Libcd39942021-09-16 23:57:28 +000079 },
80 })
81}
82
83func TestUsingLocationsLabel(t *testing.T) {
84 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050085 description: "cc_genrule using $(locations :label)",
Wei Libcd39942021-09-16 23:57:28 +000086 blueprint: `cc_genrule {
87 name: "foo.tools",
88 out: ["foo_tool.out", "foo_tool2.out"],
89 srcs: ["foo_tool.in"],
90 cmd: "cp $(in) $(out)",
91 bazel_module: { bp2build_available: true },
92}
93
94cc_genrule {
95 name: "foo",
96 out: ["foo.out"],
97 srcs: ["foo.in"],
98 tools: [":foo.tools"],
99 cmd: "$(locations :foo.tools) -s $(out) $(in)",
100 bazel_module: { bp2build_available: true },
101}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500102 expectedBazelTargets: []string{
103 makeBazelTarget("genrule", "foo", attrNameToString{
104 "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
105 "outs": `["foo.out"]`,
106 "srcs": `["foo.in"]`,
107 "tools": `[":foo.tools"]`,
108 }),
109 makeBazelTarget("genrule", "foo.tools", attrNameToString{
110 "cmd": `"cp $(SRCS) $(OUTS)"`,
111 "outs": `[
Wei Libcd39942021-09-16 23:57:28 +0000112 "foo_tool.out",
113 "foo_tool2.out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500114 ]`,
115 "srcs": `["foo_tool.in"]`,
116 }),
Wei Libcd39942021-09-16 23:57:28 +0000117 },
118 })
119}
120
121func TestUsingLocationsAbsoluteLabel(t *testing.T) {
122 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500123 description: "cc_genrule using $(locations //absolute:label)",
Wei Libcd39942021-09-16 23:57:28 +0000124 blueprint: `cc_genrule {
125 name: "foo",
126 out: ["foo.out"],
127 srcs: ["foo.in"],
128 tool_files: [":foo.tool"],
129 cmd: "$(locations :foo.tool) -s $(out) $(in)",
130 bazel_module: { bp2build_available: true },
131}`,
Wei Libcd39942021-09-16 23:57:28 +0000132 filesystem: otherCcGenruleBp,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500133 expectedBazelTargets: []string{
134 makeBazelTarget("genrule", "foo", attrNameToString{
135 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
136 "outs": `["foo.out"]`,
137 "srcs": `["foo.in"]`,
138 "tools": `["//other:foo.tool"]`,
139 }),
140 },
Wei Libcd39942021-09-16 23:57:28 +0000141 })
142}
143
144func TestSrcsUsingAbsoluteLabel(t *testing.T) {
145 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500146 description: "cc_genrule srcs using $(locations //absolute:label)",
Wei Libcd39942021-09-16 23:57:28 +0000147 blueprint: `cc_genrule {
148 name: "foo",
149 out: ["foo.out"],
150 srcs: [":other.tool"],
151 tool_files: [":foo.tool"],
152 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
153 bazel_module: { bp2build_available: true },
154}`,
Wei Libcd39942021-09-16 23:57:28 +0000155 filesystem: otherCcGenruleBp,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500156 expectedBazelTargets: []string{
157 makeBazelTarget("genrule", "foo", attrNameToString{
158 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
159 "outs": `["foo.out"]`,
160 "srcs": `["//other:other.tool"]`,
161 "tools": `["//other:foo.tool"]`,
162 }),
163 },
Wei Libcd39942021-09-16 23:57:28 +0000164 })
165}
166
167func TestLocationsLabelUsesFirstToolFile(t *testing.T) {
168 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500169 description: "cc_genrule using $(location) label should substitute first tool label automatically",
Wei Libcd39942021-09-16 23:57:28 +0000170 blueprint: `cc_genrule {
171 name: "foo",
172 out: ["foo.out"],
173 srcs: ["foo.in"],
174 tool_files: [":foo.tool", ":other.tool"],
175 cmd: "$(location) -s $(out) $(in)",
176 bazel_module: { bp2build_available: true },
177}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500178 filesystem: otherCcGenruleBp,
179 expectedBazelTargets: []string{
180 makeBazelTarget("genrule", "foo", attrNameToString{
181 "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
182 "outs": `["foo.out"]`,
183 "srcs": `["foo.in"]`,
184 "tools": `[
Wei Libcd39942021-09-16 23:57:28 +0000185 "//other:foo.tool",
186 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500187 ]`,
188 }),
Wei Libcd39942021-09-16 23:57:28 +0000189 },
Wei Libcd39942021-09-16 23:57:28 +0000190 })
191}
192
193func TestLocationsLabelUsesFirstTool(t *testing.T) {
194 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500195 description: "cc_genrule using $(locations) label should substitute first tool label automatically",
Wei Libcd39942021-09-16 23:57:28 +0000196 blueprint: `cc_genrule {
197 name: "foo",
198 out: ["foo.out"],
199 srcs: ["foo.in"],
200 tools: [":foo.tool", ":other.tool"],
201 cmd: "$(locations) -s $(out) $(in)",
202 bazel_module: { bp2build_available: true },
203}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500204 filesystem: otherCcGenruleBp,
205 expectedBazelTargets: []string{
206 makeBazelTarget("genrule", "foo", attrNameToString{
207 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
208 "outs": `["foo.out"]`,
209 "srcs": `["foo.in"]`,
210 "tools": `[
Wei Libcd39942021-09-16 23:57:28 +0000211 "//other:foo.tool",
212 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500213 ]`,
214 }),
Wei Libcd39942021-09-16 23:57:28 +0000215 },
Wei Libcd39942021-09-16 23:57:28 +0000216 })
217}
218
219func TestWithoutToolsOrToolFiles(t *testing.T) {
220 runCcGenruleTestCase(t, bp2buildTestCase{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500221 description: "cc_genrule without tools or tool_files can convert successfully",
Wei Libcd39942021-09-16 23:57:28 +0000222 blueprint: `cc_genrule {
223 name: "foo",
224 out: ["foo.out"],
225 srcs: ["foo.in"],
226 cmd: "cp $(in) $(out)",
227 bazel_module: { bp2build_available: true },
228}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500229 expectedBazelTargets: []string{
230 makeBazelTarget("genrule", "foo", attrNameToString{
231 "cmd": `"cp $(SRCS) $(OUTS)"`,
232 "outs": `["foo.out"]`,
233 "srcs": `["foo.in"]`,
234 }),
Wei Libcd39942021-09-16 23:57:28 +0000235 },
236 })
237}