Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/cc" |
| 22 | "android/soong/genrule" |
| 23 | ) |
| 24 | |
| 25 | var 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 | } |
| 32 | cc_genrule { |
| 33 | name: "other.tool", |
| 34 | out: ["other_tool.out"], |
| 35 | srcs: ["other_tool.in"], |
| 36 | cmd: "cp $(in) $(out)", |
| 37 | }`, |
| 38 | } |
| 39 | |
| 40 | func runCcGenruleTestCase(t *testing.T, tc bp2buildTestCase) { |
| 41 | t.Helper() |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 42 | (&tc).moduleTypeUnderTest = "cc_genrule" |
| 43 | (&tc).moduleTypeUnderTestFactory = cc.GenRuleFactory |
| 44 | (&tc).moduleTypeUnderTestBp2BuildMutator = genrule.CcGenruleBp2Build |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 45 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) |
| 46 | } |
| 47 | |
| 48 | func TestCliVariableReplacement(t *testing.T) { |
| 49 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 50 | description: "cc_genrule with command line variable replacements", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 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 | |
| 59 | cc_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 Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 68 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 79 | }, |
| 80 | }) |
| 81 | } |
| 82 | |
| 83 | func TestUsingLocationsLabel(t *testing.T) { |
| 84 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 85 | description: "cc_genrule using $(locations :label)", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 86 | 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 | |
| 94 | cc_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 Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 102 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 112 | "foo_tool.out", |
| 113 | "foo_tool2.out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 114 | ]`, |
| 115 | "srcs": `["foo_tool.in"]`, |
| 116 | }), |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 117 | }, |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | func TestUsingLocationsAbsoluteLabel(t *testing.T) { |
| 122 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 123 | description: "cc_genrule using $(locations //absolute:label)", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 124 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 132 | filesystem: otherCcGenruleBp, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 133 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 141 | }) |
| 142 | } |
| 143 | |
| 144 | func TestSrcsUsingAbsoluteLabel(t *testing.T) { |
| 145 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 146 | description: "cc_genrule srcs using $(locations //absolute:label)", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 147 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 155 | filesystem: otherCcGenruleBp, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 156 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 164 | }) |
| 165 | } |
| 166 | |
| 167 | func TestLocationsLabelUsesFirstToolFile(t *testing.T) { |
| 168 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 169 | description: "cc_genrule using $(location) label should substitute first tool label automatically", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 170 | 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 Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 178 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 185 | "//other:foo.tool", |
| 186 | "//other:other.tool", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 187 | ]`, |
| 188 | }), |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 189 | }, |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 190 | }) |
| 191 | } |
| 192 | |
| 193 | func TestLocationsLabelUsesFirstTool(t *testing.T) { |
| 194 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 195 | description: "cc_genrule using $(locations) label should substitute first tool label automatically", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 196 | 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 Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 204 | 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 Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 211 | "//other:foo.tool", |
| 212 | "//other:other.tool", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 213 | ]`, |
| 214 | }), |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 215 | }, |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 216 | }) |
| 217 | } |
| 218 | |
| 219 | func TestWithoutToolsOrToolFiles(t *testing.T) { |
| 220 | runCcGenruleTestCase(t, bp2buildTestCase{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 221 | description: "cc_genrule without tools or tool_files can convert successfully", |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 222 | 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 Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 229 | expectedBazelTargets: []string{ |
| 230 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 231 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 232 | "outs": `["foo.out"]`, |
| 233 | "srcs": `["foo.in"]`, |
| 234 | }), |
Wei Li | bcd3994 | 2021-09-16 23:57:28 +0000 | [diff] [blame] | 235 | }, |
| 236 | }) |
| 237 | } |