Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 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 | |
| 15 | package genrule |
| 16 | |
| 17 | import ( |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 18 | "fmt" |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 19 | "os" |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 20 | "regexp" |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 21 | "strconv" |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | |
| 24 | "android/soong/android" |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 29 | func TestMain(m *testing.M) { |
Paul Duffin | e66946b | 2021-03-16 12:38:33 +0000 | [diff] [blame] | 30 | os.Exit(m.Run()) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 33 | var prepareForGenRuleTest = android.GroupFixturePreparers( |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 34 | android.PrepareForTestWithArchMutator, |
| 35 | android.PrepareForTestWithDefaults, |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 36 | android.PrepareForTestWithFilegroup, |
| 37 | PrepareForTestWithGenRuleBuildComponents, |
| 38 | android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) { |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 39 | android.RegisterPrebuiltMutators(ctx) |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 40 | ctx.RegisterModuleType("tool", toolFactory) |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 41 | ctx.RegisterModuleType("prebuilt_tool", prebuiltToolFactory) |
Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 42 | ctx.RegisterModuleType("output", outputProducerFactory) |
Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 43 | ctx.RegisterModuleType("use_source", useSourceFactory) |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 44 | }), |
| 45 | android.FixtureMergeMockFs(android.MockFS{ |
| 46 | "tool": nil, |
| 47 | "tool_file1": nil, |
| 48 | "tool_file2": nil, |
| 49 | "in1": nil, |
| 50 | "in2": nil, |
| 51 | "in1.txt": nil, |
| 52 | "in2.txt": nil, |
| 53 | "in3.txt": nil, |
| 54 | }), |
| 55 | ) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 56 | |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 57 | func testGenruleBp() string { |
| 58 | return ` |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 59 | tool { |
| 60 | name: "tool", |
| 61 | } |
| 62 | |
| 63 | filegroup { |
| 64 | name: "tool_files", |
| 65 | srcs: [ |
| 66 | "tool_file1", |
| 67 | "tool_file2", |
| 68 | ], |
| 69 | } |
| 70 | |
| 71 | filegroup { |
| 72 | name: "1tool_file", |
| 73 | srcs: [ |
| 74 | "tool_file1", |
| 75 | ], |
| 76 | } |
| 77 | |
| 78 | filegroup { |
| 79 | name: "ins", |
| 80 | srcs: [ |
| 81 | "in1", |
| 82 | "in2", |
| 83 | ], |
| 84 | } |
| 85 | |
| 86 | filegroup { |
| 87 | name: "1in", |
| 88 | srcs: [ |
| 89 | "in1", |
| 90 | ], |
| 91 | } |
| 92 | |
| 93 | filegroup { |
| 94 | name: "empty", |
| 95 | } |
| 96 | ` |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func TestGenruleCmd(t *testing.T) { |
| 100 | testcases := []struct { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 101 | name string |
| 102 | moduleName string |
| 103 | prop string |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 104 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 105 | allowMissingDependencies bool |
| 106 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 107 | err string |
| 108 | expect string |
| 109 | }{ |
| 110 | { |
| 111 | name: "empty location tool", |
| 112 | prop: ` |
| 113 | tools: ["tool"], |
| 114 | out: ["out"], |
| 115 | cmd: "$(location) > $(out)", |
| 116 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 117 | expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 118 | }, |
| 119 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 120 | name: "empty location tool2", |
| 121 | prop: ` |
| 122 | tools: [":tool"], |
| 123 | out: ["out"], |
| 124 | cmd: "$(location) > $(out)", |
| 125 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 126 | expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 127 | }, |
| 128 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 129 | name: "empty location tool file", |
| 130 | prop: ` |
| 131 | tool_files: ["tool_file1"], |
| 132 | out: ["out"], |
| 133 | cmd: "$(location) > $(out)", |
| 134 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 135 | expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 136 | }, |
| 137 | { |
| 138 | name: "empty location tool file fg", |
| 139 | prop: ` |
| 140 | tool_files: [":1tool_file"], |
| 141 | out: ["out"], |
| 142 | cmd: "$(location) > $(out)", |
| 143 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 144 | expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 145 | }, |
| 146 | { |
| 147 | name: "empty location tool and tool file", |
| 148 | prop: ` |
| 149 | tools: ["tool"], |
| 150 | tool_files: ["tool_file1"], |
| 151 | out: ["out"], |
| 152 | cmd: "$(location) > $(out)", |
| 153 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 154 | expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 155 | }, |
| 156 | { |
| 157 | name: "tool", |
| 158 | prop: ` |
| 159 | tools: ["tool"], |
| 160 | out: ["out"], |
| 161 | cmd: "$(location tool) > $(out)", |
| 162 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 163 | expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 164 | }, |
| 165 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 166 | name: "tool2", |
| 167 | prop: ` |
| 168 | tools: [":tool"], |
| 169 | out: ["out"], |
| 170 | cmd: "$(location :tool) > $(out)", |
| 171 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 172 | expect: "__SBOX_SANDBOX_DIR__/tools/out/bin/tool > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 173 | }, |
| 174 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 175 | name: "tool file", |
| 176 | prop: ` |
| 177 | tool_files: ["tool_file1"], |
| 178 | out: ["out"], |
| 179 | cmd: "$(location tool_file1) > $(out)", |
| 180 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 181 | expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 182 | }, |
| 183 | { |
| 184 | name: "tool file fg", |
| 185 | prop: ` |
| 186 | tool_files: [":1tool_file"], |
| 187 | out: ["out"], |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 188 | cmd: "$(location :1tool_file) > $(out)", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 189 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 190 | expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 191 | }, |
| 192 | { |
| 193 | name: "tool files", |
| 194 | prop: ` |
| 195 | tool_files: [":tool_files"], |
| 196 | out: ["out"], |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 197 | cmd: "$(locations :tool_files) > $(out)", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 198 | `, |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 199 | expect: "__SBOX_SANDBOX_DIR__/tools/src/tool_file1 __SBOX_SANDBOX_DIR__/tools/src/tool_file2 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 200 | }, |
| 201 | { |
| 202 | name: "in1", |
| 203 | prop: ` |
| 204 | srcs: ["in1"], |
| 205 | out: ["out"], |
| 206 | cmd: "cat $(in) > $(out)", |
| 207 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 208 | expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 209 | }, |
| 210 | { |
| 211 | name: "in1 fg", |
| 212 | prop: ` |
| 213 | srcs: [":1in"], |
| 214 | out: ["out"], |
| 215 | cmd: "cat $(in) > $(out)", |
| 216 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 217 | expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 218 | }, |
| 219 | { |
| 220 | name: "ins", |
| 221 | prop: ` |
| 222 | srcs: ["in1", "in2"], |
| 223 | out: ["out"], |
| 224 | cmd: "cat $(in) > $(out)", |
| 225 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 226 | expect: "cat in1 in2 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 227 | }, |
| 228 | { |
| 229 | name: "ins fg", |
| 230 | prop: ` |
| 231 | srcs: [":ins"], |
| 232 | out: ["out"], |
| 233 | cmd: "cat $(in) > $(out)", |
| 234 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 235 | expect: "cat in1 in2 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 236 | }, |
| 237 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 238 | name: "location in1", |
| 239 | prop: ` |
| 240 | srcs: ["in1"], |
| 241 | out: ["out"], |
| 242 | cmd: "cat $(location in1) > $(out)", |
| 243 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 244 | expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 245 | }, |
| 246 | { |
| 247 | name: "location in1 fg", |
| 248 | prop: ` |
| 249 | srcs: [":1in"], |
| 250 | out: ["out"], |
| 251 | cmd: "cat $(location :1in) > $(out)", |
| 252 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 253 | expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 254 | }, |
| 255 | { |
| 256 | name: "location ins", |
| 257 | prop: ` |
| 258 | srcs: ["in1", "in2"], |
| 259 | out: ["out"], |
| 260 | cmd: "cat $(location in1) > $(out)", |
| 261 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 262 | expect: "cat in1 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 263 | }, |
| 264 | { |
| 265 | name: "location ins fg", |
| 266 | prop: ` |
| 267 | srcs: [":ins"], |
| 268 | out: ["out"], |
| 269 | cmd: "cat $(locations :ins) > $(out)", |
| 270 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 271 | expect: "cat in1 in2 > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 272 | }, |
| 273 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 274 | name: "outs", |
| 275 | prop: ` |
| 276 | out: ["out", "out2"], |
| 277 | cmd: "echo foo > $(out)", |
| 278 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 279 | expect: "echo foo > __SBOX_SANDBOX_DIR__/out/out __SBOX_SANDBOX_DIR__/out/out2", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 280 | }, |
| 281 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 282 | name: "location out", |
| 283 | prop: ` |
| 284 | out: ["out", "out2"], |
| 285 | cmd: "echo foo > $(location out2)", |
| 286 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 287 | expect: "echo foo > __SBOX_SANDBOX_DIR__/out/out2", |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 288 | }, |
| 289 | { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 290 | name: "depfile", |
| 291 | moduleName: "depfile_allowed_for_test", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 292 | prop: ` |
| 293 | out: ["out"], |
| 294 | depfile: true, |
| 295 | cmd: "echo foo > $(out) && touch $(depfile)", |
| 296 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 297 | expect: "echo foo > __SBOX_SANDBOX_DIR__/out/out && touch __SBOX_DEPFILE__", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 298 | }, |
| 299 | { |
| 300 | name: "gendir", |
| 301 | prop: ` |
| 302 | out: ["out"], |
| 303 | cmd: "echo foo > $(genDir)/foo && cp $(genDir)/foo $(out)", |
| 304 | `, |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 305 | expect: "echo foo > __SBOX_SANDBOX_DIR__/out/foo && cp __SBOX_SANDBOX_DIR__/out/foo __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 306 | }, |
Colin Cross | 70c4741 | 2021-03-12 17:48:14 -0800 | [diff] [blame] | 307 | { |
| 308 | name: "$", |
| 309 | prop: ` |
| 310 | out: ["out"], |
| 311 | cmd: "echo $$ > $(out)", |
| 312 | `, |
| 313 | expect: "echo $ > __SBOX_SANDBOX_DIR__/out/out", |
| 314 | }, |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 315 | |
| 316 | { |
| 317 | name: "error empty location", |
| 318 | prop: ` |
| 319 | out: ["out"], |
| 320 | cmd: "$(location) > $(out)", |
| 321 | `, |
| 322 | err: "at least one `tools` or `tool_files` is required if $(location) is used", |
| 323 | }, |
| 324 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 325 | name: "error empty location no files", |
| 326 | prop: ` |
| 327 | tool_files: [":empty"], |
| 328 | out: ["out"], |
| 329 | cmd: "$(location) > $(out)", |
| 330 | `, |
| 331 | err: `default label ":empty" has no files`, |
| 332 | }, |
| 333 | { |
| 334 | name: "error empty location multiple files", |
| 335 | prop: ` |
| 336 | tool_files: [":tool_files"], |
| 337 | out: ["out"], |
| 338 | cmd: "$(location) > $(out)", |
| 339 | `, |
| 340 | err: `default label ":tool_files" has multiple files`, |
| 341 | }, |
| 342 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 343 | name: "error location", |
| 344 | prop: ` |
| 345 | out: ["out"], |
| 346 | cmd: "echo foo > $(location missing)", |
| 347 | `, |
Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 348 | err: `unknown location label "missing" is not in srcs, out, tools or tool_files.`, |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 349 | }, |
| 350 | { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 351 | name: "error locations", |
| 352 | prop: ` |
| 353 | out: ["out"], |
| 354 | cmd: "echo foo > $(locations missing)", |
| 355 | `, |
Anton Hansson | bebf526 | 2022-02-23 11:42:38 +0000 | [diff] [blame] | 356 | err: `unknown locations label "missing" is not in srcs, out, tools or tool_files`, |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 357 | }, |
| 358 | { |
| 359 | name: "error location no files", |
| 360 | prop: ` |
| 361 | out: ["out"], |
| 362 | srcs: [":empty"], |
| 363 | cmd: "echo $(location :empty) > $(out)", |
| 364 | `, |
| 365 | err: `label ":empty" has no files`, |
| 366 | }, |
| 367 | { |
| 368 | name: "error locations no files", |
| 369 | prop: ` |
| 370 | out: ["out"], |
| 371 | srcs: [":empty"], |
| 372 | cmd: "echo $(locations :empty) > $(out)", |
| 373 | `, |
| 374 | err: `label ":empty" has no files`, |
| 375 | }, |
| 376 | { |
| 377 | name: "error location multiple files", |
| 378 | prop: ` |
| 379 | out: ["out"], |
| 380 | srcs: [":ins"], |
| 381 | cmd: "echo $(location :ins) > $(out)", |
| 382 | `, |
| 383 | err: `label ":ins" has multiple files`, |
| 384 | }, |
| 385 | { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 386 | name: "error variable", |
| 387 | prop: ` |
| 388 | out: ["out"], |
| 389 | srcs: ["in1"], |
| 390 | cmd: "echo $(foo) > $(out)", |
| 391 | `, |
| 392 | err: `unknown variable '$(foo)'`, |
| 393 | }, |
| 394 | { |
| 395 | name: "error depfile", |
| 396 | prop: ` |
| 397 | out: ["out"], |
| 398 | cmd: "echo foo > $(out) && touch $(depfile)", |
| 399 | `, |
| 400 | err: "$(depfile) used without depfile property", |
| 401 | }, |
| 402 | { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 403 | name: "error no depfile", |
| 404 | moduleName: "depfile_allowed_for_test", |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 405 | prop: ` |
| 406 | out: ["out"], |
| 407 | depfile: true, |
| 408 | cmd: "echo foo > $(out)", |
| 409 | `, |
| 410 | err: "specified depfile=true but did not include a reference to '${depfile}' in cmd", |
| 411 | }, |
| 412 | { |
| 413 | name: "error no out", |
| 414 | prop: ` |
| 415 | cmd: "echo foo > $(out)", |
| 416 | `, |
| 417 | err: "must have at least one output file", |
| 418 | }, |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 419 | { |
| 420 | name: "srcs allow missing dependencies", |
| 421 | prop: ` |
| 422 | srcs: [":missing"], |
| 423 | out: ["out"], |
| 424 | cmd: "cat $(location :missing) > $(out)", |
| 425 | `, |
| 426 | |
| 427 | allowMissingDependencies: true, |
| 428 | |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 429 | expect: "cat '***missing srcs :missing***' > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 430 | }, |
| 431 | { |
| 432 | name: "tool allow missing dependencies", |
| 433 | prop: ` |
| 434 | tools: [":missing"], |
| 435 | out: ["out"], |
| 436 | cmd: "$(location :missing) > $(out)", |
| 437 | `, |
| 438 | |
| 439 | allowMissingDependencies: true, |
| 440 | |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 441 | expect: "'***missing tool :missing***' > __SBOX_SANDBOX_DIR__/out/out", |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 442 | }, |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | for _, test := range testcases { |
| 446 | t.Run(test.name, func(t *testing.T) { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 447 | moduleName := "gen" |
| 448 | if test.moduleName != "" { |
| 449 | moduleName = test.moduleName |
| 450 | } |
| 451 | bp := fmt.Sprintf(` |
| 452 | genrule { |
| 453 | name: "%s", |
| 454 | %s |
| 455 | }`, moduleName, test.prop) |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 456 | var expectedErrors []string |
| 457 | if test.err != "" { |
| 458 | expectedErrors = append(expectedErrors, regexp.QuoteMeta(test.err)) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 459 | } |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 460 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 461 | result := android.GroupFixturePreparers( |
| 462 | prepareForGenRuleTest, |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 463 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 464 | variables.Allow_missing_dependencies = proptools.BoolPtr(test.allowMissingDependencies) |
| 465 | }), |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 466 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 467 | variables.GenruleSandboxing = proptools.BoolPtr(true) |
| 468 | }), |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 469 | android.FixtureModifyContext(func(ctx *android.TestContext) { |
| 470 | ctx.SetAllowMissingDependencies(test.allowMissingDependencies) |
| 471 | }), |
| 472 | ). |
| 473 | ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(expectedErrors)). |
| 474 | RunTestWithBp(t, testGenruleBp()+bp) |
| 475 | |
| 476 | if expectedErrors != nil { |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 477 | return |
| 478 | } |
| 479 | |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 480 | gen := result.Module(moduleName, "").(*Module) |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 481 | android.AssertStringEquals(t, "raw commands", test.expect, gen.rawCommands[0]) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 482 | }) |
| 483 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 486 | func TestGenruleHashInputs(t *testing.T) { |
| 487 | |
| 488 | // The basic idea here is to verify that the sbox command (which is |
| 489 | // in the Command field of the generate rule) contains a hash of the |
| 490 | // inputs, but only if $(in) is not referenced in the genrule cmd |
| 491 | // property. |
| 492 | |
| 493 | // By including a hash of the inputs, we cause the rule to re-run if |
| 494 | // the list of inputs changes (because the sbox command changes). |
| 495 | |
| 496 | // However, if the genrule cmd property already contains $(in), then |
| 497 | // the dependency is already expressed, so we don't need to include the |
| 498 | // hash in that case. |
| 499 | |
| 500 | bp := ` |
| 501 | genrule { |
| 502 | name: "hash0", |
| 503 | srcs: ["in1.txt", "in2.txt"], |
| 504 | out: ["out"], |
| 505 | cmd: "echo foo > $(out)", |
| 506 | } |
| 507 | genrule { |
| 508 | name: "hash1", |
| 509 | srcs: ["*.txt"], |
| 510 | out: ["out"], |
| 511 | cmd: "echo bar > $(out)", |
| 512 | } |
| 513 | genrule { |
| 514 | name: "hash2", |
| 515 | srcs: ["*.txt"], |
| 516 | out: ["out"], |
| 517 | cmd: "echo $(in) > $(out)", |
| 518 | } |
| 519 | ` |
| 520 | testcases := []struct { |
| 521 | name string |
| 522 | expectedHash string |
| 523 | }{ |
| 524 | { |
| 525 | name: "hash0", |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 526 | // sha256 value obtained from: echo -en 'in1.txt\nin2.txt' | sha256sum |
| 527 | expectedHash: "18da75b9b1cc74b09e365b4ca2e321b5d618f438cc632b387ad9dc2ab4b20e9d", |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 528 | }, |
| 529 | { |
| 530 | name: "hash1", |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 531 | // sha256 value obtained from: echo -en 'in1.txt\nin2.txt\nin3.txt' | sha256sum |
| 532 | expectedHash: "a38d432a4b19df93140e1f1fe26c97ff0387dae01fe506412b47208f0595fb45", |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 533 | }, |
| 534 | { |
| 535 | name: "hash2", |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 536 | // sha256 value obtained from: echo -en 'in1.txt\nin2.txt\nin3.txt' | sha256sum |
| 537 | expectedHash: "a38d432a4b19df93140e1f1fe26c97ff0387dae01fe506412b47208f0595fb45", |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 538 | }, |
| 539 | } |
| 540 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 541 | result := prepareForGenRuleTest.RunTestWithBp(t, testGenruleBp()+bp) |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 542 | |
| 543 | for _, test := range testcases { |
| 544 | t.Run(test.name, func(t *testing.T) { |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 545 | gen := result.ModuleForTests(test.name, "") |
Colin Cross | f61d03d | 2023-11-02 16:56:39 -0700 | [diff] [blame^] | 546 | manifest := android.RuleBuilderSboxProtoForTests(t, result.TestContext, gen.Output("genrule.sbox.textproto")) |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 547 | hash := manifest.Commands[0].GetInputHash() |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 548 | |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 549 | android.AssertStringEquals(t, "hash", test.expectedHash, hash) |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 550 | }) |
| 551 | } |
| 552 | } |
| 553 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 554 | func TestGenSrcs(t *testing.T) { |
| 555 | testcases := []struct { |
| 556 | name string |
| 557 | prop string |
| 558 | |
| 559 | allowMissingDependencies bool |
| 560 | |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 561 | err string |
| 562 | cmds []string |
| 563 | deps []string |
| 564 | files []string |
| 565 | shards int |
| 566 | inputs []string |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 567 | }{ |
| 568 | { |
| 569 | name: "gensrcs", |
| 570 | prop: ` |
| 571 | tools: ["tool"], |
| 572 | srcs: ["in1.txt", "in2.txt"], |
| 573 | cmd: "$(location) $(in) > $(out)", |
| 574 | `, |
| 575 | cmds: []string{ |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 576 | "bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in1.txt > __SBOX_SANDBOX_DIR__/out/in1.h' && bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in2.txt > __SBOX_SANDBOX_DIR__/out/in2.h'", |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 577 | }, |
Paul Duffin | e66946b | 2021-03-16 12:38:33 +0000 | [diff] [blame] | 578 | deps: []string{ |
| 579 | "out/soong/.intermediates/gen/gen/gensrcs/in1.h", |
| 580 | "out/soong/.intermediates/gen/gen/gensrcs/in2.h", |
| 581 | }, |
| 582 | files: []string{ |
| 583 | "out/soong/.intermediates/gen/gen/gensrcs/in1.h", |
| 584 | "out/soong/.intermediates/gen/gen/gensrcs/in2.h", |
| 585 | }, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 586 | }, |
| 587 | { |
| 588 | name: "shards", |
| 589 | prop: ` |
| 590 | tools: ["tool"], |
| 591 | srcs: ["in1.txt", "in2.txt", "in3.txt"], |
| 592 | cmd: "$(location) $(in) > $(out)", |
| 593 | shard_size: 2, |
| 594 | `, |
| 595 | cmds: []string{ |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 596 | "bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in1.txt > __SBOX_SANDBOX_DIR__/out/in1.h' && bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in2.txt > __SBOX_SANDBOX_DIR__/out/in2.h'", |
| 597 | "bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in3.txt > __SBOX_SANDBOX_DIR__/out/in3.h'", |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 598 | }, |
Paul Duffin | e66946b | 2021-03-16 12:38:33 +0000 | [diff] [blame] | 599 | deps: []string{ |
| 600 | "out/soong/.intermediates/gen/gen/gensrcs/in1.h", |
| 601 | "out/soong/.intermediates/gen/gen/gensrcs/in2.h", |
| 602 | "out/soong/.intermediates/gen/gen/gensrcs/in3.h", |
| 603 | }, |
| 604 | files: []string{ |
| 605 | "out/soong/.intermediates/gen/gen/gensrcs/in1.h", |
| 606 | "out/soong/.intermediates/gen/gen/gensrcs/in2.h", |
| 607 | "out/soong/.intermediates/gen/gen/gensrcs/in3.h", |
| 608 | }, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 609 | }, |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 610 | { |
| 611 | name: "data", |
| 612 | prop: ` |
| 613 | tools: ["tool"], |
| 614 | srcs: ["in1.txt", "in2.txt", "in3.txt"], |
| 615 | cmd: "$(location) $(in) --extra_input=$(location baz.txt) > $(out)", |
| 616 | data: ["baz.txt"], |
| 617 | shard_size: 2, |
| 618 | `, |
| 619 | cmds: []string{ |
| 620 | "bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in1.txt --extra_input=baz.txt > __SBOX_SANDBOX_DIR__/out/in1.h' && bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in2.txt --extra_input=baz.txt > __SBOX_SANDBOX_DIR__/out/in2.h'", |
| 621 | "bash -c '__SBOX_SANDBOX_DIR__/tools/out/bin/tool in3.txt --extra_input=baz.txt > __SBOX_SANDBOX_DIR__/out/in3.h'", |
| 622 | }, |
| 623 | deps: []string{ |
| 624 | "out/soong/.intermediates/gen/gen/gensrcs/in1.h", |
| 625 | "out/soong/.intermediates/gen/gen/gensrcs/in2.h", |
| 626 | "out/soong/.intermediates/gen/gen/gensrcs/in3.h", |
| 627 | }, |
| 628 | files: []string{ |
| 629 | "out/soong/.intermediates/gen/gen/gensrcs/in1.h", |
| 630 | "out/soong/.intermediates/gen/gen/gensrcs/in2.h", |
| 631 | "out/soong/.intermediates/gen/gen/gensrcs/in3.h", |
| 632 | }, |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 633 | shards: 2, |
| 634 | inputs: []string{ |
| 635 | "baz.txt", |
| 636 | }, |
Liz Kammer | 81fec18 | 2023-06-09 13:33:45 -0400 | [diff] [blame] | 637 | }, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 640 | checkInputs := func(t *testing.T, rule android.TestingBuildParams, inputs []string) { |
| 641 | t.Helper() |
| 642 | if len(inputs) == 0 { |
| 643 | return |
| 644 | } |
| 645 | inputBaseNames := map[string]bool{} |
| 646 | for _, f := range rule.Implicits { |
| 647 | inputBaseNames[f.Base()] = true |
| 648 | } |
| 649 | for _, f := range inputs { |
| 650 | if _, ok := inputBaseNames[f]; !ok { |
| 651 | t.Errorf("Expected to find input file %q for %q, but did not", f, rule.Description) |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 656 | for _, test := range testcases { |
| 657 | t.Run(test.name, func(t *testing.T) { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 658 | bp := "gensrcs {\n" |
| 659 | bp += `name: "gen",` + "\n" |
| 660 | bp += `output_extension: "h",` + "\n" |
| 661 | bp += test.prop |
| 662 | bp += "}\n" |
| 663 | |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 664 | var expectedErrors []string |
| 665 | if test.err != "" { |
| 666 | expectedErrors = append(expectedErrors, regexp.QuoteMeta(test.err)) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 667 | } |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 668 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 669 | result := prepareForGenRuleTest. |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 670 | ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(expectedErrors)). |
| 671 | RunTestWithBp(t, testGenruleBp()+bp) |
| 672 | |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 673 | mod := result.ModuleForTests("gen", "") |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 674 | if expectedErrors != nil { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 675 | return |
| 676 | } |
| 677 | |
Liz Kammer | 796921d | 2023-07-11 08:21:41 -0400 | [diff] [blame] | 678 | if test.shards > 0 { |
| 679 | for i := 0; i < test.shards; i++ { |
| 680 | r := mod.Rule("generator" + strconv.Itoa(i)) |
| 681 | checkInputs(t, r, test.inputs) |
| 682 | } |
| 683 | } else { |
| 684 | r := mod.Rule("generator") |
| 685 | checkInputs(t, r, test.inputs) |
| 686 | } |
| 687 | |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 688 | gen := result.Module("gen", "").(*Module) |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 689 | android.AssertDeepEquals(t, "cmd", test.cmds, gen.rawCommands) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 690 | |
Paul Duffin | e66946b | 2021-03-16 12:38:33 +0000 | [diff] [blame] | 691 | android.AssertPathsRelativeToTopEquals(t, "deps", test.deps, gen.outputDeps) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 692 | |
Paul Duffin | e66946b | 2021-03-16 12:38:33 +0000 | [diff] [blame] | 693 | android.AssertPathsRelativeToTopEquals(t, "files", test.files, gen.outputFiles) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 694 | }) |
| 695 | } |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 698 | func TestGenruleAllowlistingDepfile(t *testing.T) { |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 699 | tests := []struct { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 700 | name string |
| 701 | prop string |
| 702 | err string |
| 703 | moduleName string |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 704 | }{ |
| 705 | { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 706 | name: `error when module is not allowlisted`, |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 707 | prop: ` |
| 708 | depfile: true, |
| 709 | cmd: "cat $(in) > $(out) && cat $(depfile)", |
| 710 | `, |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 711 | err: "depfile: Deprecated to ensure the module type is convertible to Bazel", |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 712 | }, |
| 713 | { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 714 | name: `no error when module is allowlisted`, |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 715 | prop: ` |
| 716 | depfile: true, |
| 717 | cmd: "cat $(in) > $(out) && cat $(depfile)", |
| 718 | `, |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 719 | moduleName: `depfile_allowed_for_test`, |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 720 | }, |
| 721 | } |
| 722 | for _, test := range tests { |
| 723 | t.Run(test.name, func(t *testing.T) { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 724 | moduleName := "foo" |
| 725 | if test.moduleName != "" { |
| 726 | moduleName = test.moduleName |
| 727 | } |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 728 | bp := fmt.Sprintf(` |
| 729 | gensrcs { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 730 | name: "%s", |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 731 | srcs: ["data.txt"], |
| 732 | %s |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 733 | }`, moduleName, test.prop) |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 734 | |
| 735 | var expectedErrors []string |
| 736 | if test.err != "" { |
| 737 | expectedErrors = append(expectedErrors, test.err) |
| 738 | } |
| 739 | android.GroupFixturePreparers( |
| 740 | prepareForGenRuleTest, |
| 741 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
Yu Liu | 6a7940c | 2023-05-09 17:12:22 -0700 | [diff] [blame] | 742 | variables.GenruleSandboxing = proptools.BoolPtr(true) |
Vinh Tran | 140d588 | 2022-06-10 14:23:27 -0400 | [diff] [blame] | 743 | }), |
| 744 | ). |
| 745 | ExtendWithErrorHandler(android.FixtureExpectsAllErrorsToMatchAPattern(expectedErrors)). |
| 746 | RunTestWithBp(t, bp) |
| 747 | }) |
| 748 | |
| 749 | } |
| 750 | } |
| 751 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 752 | func TestGenruleDefaults(t *testing.T) { |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 753 | bp := ` |
| 754 | genrule_defaults { |
| 755 | name: "gen_defaults1", |
| 756 | cmd: "cp $(in) $(out)", |
| 757 | } |
| 758 | |
| 759 | genrule_defaults { |
| 760 | name: "gen_defaults2", |
| 761 | srcs: ["in1"], |
| 762 | } |
| 763 | |
| 764 | genrule { |
| 765 | name: "gen", |
| 766 | out: ["out"], |
| 767 | defaults: ["gen_defaults1", "gen_defaults2"], |
| 768 | } |
| 769 | ` |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 770 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 771 | result := prepareForGenRuleTest.RunTestWithBp(t, testGenruleBp()+bp) |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 772 | |
| 773 | gen := result.Module("gen", "").(*Module) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 774 | |
Colin Cross | e16ce36 | 2020-11-12 08:29:30 -0800 | [diff] [blame] | 775 | expectedCmd := "cp in1 __SBOX_SANDBOX_DIR__/out/out" |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 776 | android.AssertStringEquals(t, "cmd", expectedCmd, gen.rawCommands[0]) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 777 | |
| 778 | expectedSrcs := []string{"in1"} |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 779 | android.AssertDeepEquals(t, "srcs", expectedSrcs, gen.properties.Srcs) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 780 | } |
| 781 | |
Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 782 | func TestGenruleAllowMissingDependencies(t *testing.T) { |
| 783 | bp := ` |
| 784 | output { |
| 785 | name: "disabled", |
| 786 | enabled: false, |
| 787 | } |
| 788 | |
| 789 | genrule { |
| 790 | name: "gen", |
| 791 | srcs: [ |
| 792 | ":disabled", |
| 793 | ], |
| 794 | out: ["out"], |
| 795 | cmd: "cat $(in) > $(out)", |
| 796 | } |
| 797 | ` |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 798 | result := android.GroupFixturePreparers( |
| 799 | prepareForGenRuleTest, |
Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 800 | android.FixtureModifyConfigAndContext( |
| 801 | func(config android.Config, ctx *android.TestContext) { |
| 802 | config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true) |
| 803 | ctx.SetAllowMissingDependencies(true) |
| 804 | })).RunTestWithBp(t, bp) |
| 805 | |
| 806 | gen := result.ModuleForTests("gen", "").Output("out") |
| 807 | if gen.Rule != android.ErrorRule { |
| 808 | t.Errorf("Expected missing dependency error rule for gen, got %q", gen.Rule.String()) |
| 809 | } |
| 810 | } |
| 811 | |
Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 812 | func TestGenruleOutputFiles(t *testing.T) { |
| 813 | bp := ` |
| 814 | genrule { |
| 815 | name: "gen", |
| 816 | out: ["foo", "sub/bar"], |
| 817 | cmd: "echo foo > $(location foo) && echo bar > $(location sub/bar)", |
| 818 | } |
| 819 | use_source { |
| 820 | name: "gen_foo", |
| 821 | srcs: [":gen{foo}"], |
| 822 | } |
| 823 | use_source { |
| 824 | name: "gen_bar", |
| 825 | srcs: [":gen{sub/bar}"], |
| 826 | } |
| 827 | use_source { |
| 828 | name: "gen_all", |
| 829 | srcs: [":gen"], |
| 830 | } |
| 831 | ` |
| 832 | |
| 833 | result := prepareForGenRuleTest.RunTestWithBp(t, testGenruleBp()+bp) |
| 834 | android.AssertPathsRelativeToTopEquals(t, |
| 835 | "genrule.tag with output", |
| 836 | []string{"out/soong/.intermediates/gen/gen/foo"}, |
| 837 | result.ModuleForTests("gen_foo", "").Module().(*useSource).srcs) |
| 838 | android.AssertPathsRelativeToTopEquals(t, |
| 839 | "genrule.tag with output in subdir", |
| 840 | []string{"out/soong/.intermediates/gen/gen/sub/bar"}, |
| 841 | result.ModuleForTests("gen_bar", "").Module().(*useSource).srcs) |
| 842 | android.AssertPathsRelativeToTopEquals(t, |
| 843 | "genrule.tag with all", |
| 844 | []string{"out/soong/.intermediates/gen/gen/foo", "out/soong/.intermediates/gen/gen/sub/bar"}, |
| 845 | result.ModuleForTests("gen_all", "").Module().(*useSource).srcs) |
| 846 | } |
| 847 | |
Liz Kammer | d38c87c | 2023-07-17 09:58:50 -0400 | [diff] [blame] | 848 | func TestGenruleInterface(t *testing.T) { |
| 849 | result := android.GroupFixturePreparers( |
| 850 | prepareForGenRuleTest, |
| 851 | android.FixtureMergeMockFs(android.MockFS{ |
| 852 | "package-dir/Android.bp": []byte(` |
| 853 | genrule { |
| 854 | name: "module-name", |
| 855 | cmd: "mkdir -p $(genDir) && cat $(in) >> $(genDir)/$(out)", |
| 856 | srcs: [ |
| 857 | "src/foo.proto", |
| 858 | ], |
| 859 | out: ["proto.h", "bar/proto.h"], |
| 860 | export_include_dirs: [".", "bar"], |
| 861 | } |
| 862 | `), |
| 863 | }), |
| 864 | ).RunTest(t) |
| 865 | |
| 866 | exportedIncludeDirs := []string{ |
| 867 | "out/soong/.intermediates/package-dir/module-name/gen/package-dir", |
| 868 | "out/soong/.intermediates/package-dir/module-name/gen", |
| 869 | "out/soong/.intermediates/package-dir/module-name/gen/package-dir/bar", |
| 870 | "out/soong/.intermediates/package-dir/module-name/gen/bar", |
| 871 | } |
| 872 | gen := result.Module("module-name", "").(*Module) |
| 873 | |
| 874 | android.AssertPathsRelativeToTopEquals( |
| 875 | t, |
| 876 | "include path", |
| 877 | exportedIncludeDirs, |
| 878 | gen.GeneratedHeaderDirs(), |
| 879 | ) |
| 880 | android.AssertPathsRelativeToTopEquals( |
| 881 | t, |
| 882 | "files", |
| 883 | []string{ |
| 884 | "out/soong/.intermediates/package-dir/module-name/gen/proto.h", |
| 885 | "out/soong/.intermediates/package-dir/module-name/gen/bar/proto.h", |
| 886 | }, |
| 887 | gen.GeneratedSourceFiles(), |
| 888 | ) |
| 889 | } |
| 890 | |
Vinh Tran | 370e08c | 2022-09-23 18:09:01 -0400 | [diff] [blame] | 891 | func TestGenSrcsWithNonRootAndroidBpOutputFiles(t *testing.T) { |
| 892 | result := android.GroupFixturePreparers( |
| 893 | prepareForGenRuleTest, |
| 894 | android.FixtureMergeMockFs(android.MockFS{ |
| 895 | "external-protos/path/Android.bp": []byte(` |
| 896 | filegroup { |
| 897 | name: "external-protos", |
| 898 | srcs: ["baz/baz.proto", "bar.proto"], |
| 899 | } |
| 900 | `), |
| 901 | "package-dir/Android.bp": []byte(` |
| 902 | gensrcs { |
| 903 | name: "module-name", |
| 904 | cmd: "mkdir -p $(genDir) && cat $(in) >> $(genDir)/$(out)", |
| 905 | srcs: [ |
| 906 | "src/foo.proto", |
| 907 | ":external-protos", |
| 908 | ], |
| 909 | output_extension: "proto.h", |
| 910 | } |
| 911 | `), |
| 912 | }), |
| 913 | ).RunTest(t) |
| 914 | |
| 915 | exportedIncludeDir := "out/soong/.intermediates/package-dir/module-name/gen/gensrcs" |
| 916 | gen := result.Module("module-name", "").(*Module) |
| 917 | |
| 918 | android.AssertPathsRelativeToTopEquals( |
| 919 | t, |
| 920 | "include path", |
| 921 | []string{exportedIncludeDir}, |
| 922 | gen.exportedIncludeDirs, |
| 923 | ) |
| 924 | android.AssertPathsRelativeToTopEquals( |
| 925 | t, |
| 926 | "files", |
| 927 | []string{ |
| 928 | exportedIncludeDir + "/package-dir/src/foo.proto.h", |
| 929 | exportedIncludeDir + "/external-protos/path/baz/baz.proto.h", |
| 930 | exportedIncludeDir + "/external-protos/path/bar.proto.h", |
| 931 | }, |
| 932 | gen.outputFiles, |
| 933 | ) |
| 934 | } |
| 935 | |
| 936 | func TestGenSrcsWithSrcsFromExternalPackage(t *testing.T) { |
| 937 | bp := ` |
| 938 | gensrcs { |
| 939 | name: "module-name", |
| 940 | cmd: "mkdir -p $(genDir) && cat $(in) >> $(genDir)/$(out)", |
| 941 | srcs: [ |
| 942 | ":external-protos", |
| 943 | ], |
| 944 | output_extension: "proto.h", |
| 945 | } |
| 946 | ` |
| 947 | result := android.GroupFixturePreparers( |
| 948 | prepareForGenRuleTest, |
| 949 | android.FixtureMergeMockFs(android.MockFS{ |
| 950 | "external-protos/path/Android.bp": []byte(` |
| 951 | filegroup { |
| 952 | name: "external-protos", |
| 953 | srcs: ["foo/foo.proto", "bar.proto"], |
| 954 | } |
| 955 | `), |
| 956 | }), |
| 957 | ).RunTestWithBp(t, bp) |
| 958 | |
| 959 | exportedIncludeDir := "out/soong/.intermediates/module-name/gen/gensrcs" |
| 960 | gen := result.Module("module-name", "").(*Module) |
| 961 | |
| 962 | android.AssertPathsRelativeToTopEquals( |
| 963 | t, |
| 964 | "include path", |
| 965 | []string{exportedIncludeDir}, |
| 966 | gen.exportedIncludeDirs, |
| 967 | ) |
| 968 | android.AssertPathsRelativeToTopEquals( |
| 969 | t, |
| 970 | "files", |
| 971 | []string{ |
| 972 | exportedIncludeDir + "/external-protos/path/foo/foo.proto.h", |
| 973 | exportedIncludeDir + "/external-protos/path/bar.proto.h", |
| 974 | }, |
| 975 | gen.outputFiles, |
| 976 | ) |
| 977 | } |
| 978 | |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 979 | func TestPrebuiltTool(t *testing.T) { |
| 980 | testcases := []struct { |
| 981 | name string |
| 982 | bp string |
| 983 | expectedToolName string |
| 984 | }{ |
| 985 | { |
| 986 | name: "source only", |
| 987 | bp: ` |
| 988 | tool { name: "tool" } |
| 989 | `, |
| 990 | expectedToolName: "bin/tool", |
| 991 | }, |
| 992 | { |
| 993 | name: "prebuilt only", |
| 994 | bp: ` |
| 995 | prebuilt_tool { name: "tool" } |
| 996 | `, |
| 997 | expectedToolName: "prebuilt_bin/tool", |
| 998 | }, |
| 999 | { |
| 1000 | name: "source preferred", |
| 1001 | bp: ` |
| 1002 | tool { name: "tool" } |
| 1003 | prebuilt_tool { name: "tool" } |
| 1004 | `, |
| 1005 | expectedToolName: "bin/tool", |
| 1006 | }, |
| 1007 | { |
| 1008 | name: "prebuilt preferred", |
| 1009 | bp: ` |
| 1010 | tool { name: "tool" } |
| 1011 | prebuilt_tool { name: "tool", prefer: true } |
| 1012 | `, |
| 1013 | expectedToolName: "prebuilt_bin/prebuilt_tool", |
| 1014 | }, |
| 1015 | { |
| 1016 | name: "source disabled", |
| 1017 | bp: ` |
| 1018 | tool { name: "tool", enabled: false } |
| 1019 | prebuilt_tool { name: "tool" } |
| 1020 | `, |
| 1021 | expectedToolName: "prebuilt_bin/prebuilt_tool", |
| 1022 | }, |
| 1023 | } |
| 1024 | |
| 1025 | for _, test := range testcases { |
| 1026 | t.Run(test.name, func(t *testing.T) { |
| 1027 | result := prepareForGenRuleTest.RunTestWithBp(t, test.bp+` |
| 1028 | genrule { |
| 1029 | name: "gen", |
| 1030 | tools: ["tool"], |
| 1031 | out: ["foo"], |
| 1032 | cmd: "$(location tool)", |
| 1033 | } |
| 1034 | `) |
| 1035 | gen := result.Module("gen", "").(*Module) |
| 1036 | expectedCmd := "__SBOX_SANDBOX_DIR__/tools/out/" + test.expectedToolName |
| 1037 | android.AssertStringEquals(t, "command", expectedCmd, gen.rawCommands[0]) |
| 1038 | }) |
| 1039 | } |
| 1040 | } |
| 1041 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1042 | func TestGenruleWithBazel(t *testing.T) { |
| 1043 | bp := ` |
| 1044 | genrule { |
| 1045 | name: "foo", |
| 1046 | out: ["one.txt", "two.txt"], |
Chris Parsons | aa8be05 | 2020-10-14 16:22:37 -0400 | [diff] [blame] | 1047 | bazel_module: { label: "//foo/bar:bar" }, |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1048 | } |
| 1049 | ` |
| 1050 | |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 1051 | result := android.GroupFixturePreparers( |
| 1052 | prepareForGenRuleTest, android.FixtureModifyConfig(func(config android.Config) { |
| 1053 | config.BazelContext = android.MockBazelContext{ |
Liz Kammer | a92e844 | 2021-04-07 20:25:21 -0400 | [diff] [blame] | 1054 | OutputBaseDir: "outputbase", |
| 1055 | LabelToOutputFiles: map[string][]string{ |
Paul Duffin | 89648f9 | 2021-03-20 00:36:55 +0000 | [diff] [blame] | 1056 | "//foo/bar:bar": []string{"bazelone.txt", "bazeltwo.txt"}}} |
| 1057 | })).RunTestWithBp(t, testGenruleBp()+bp) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1058 | |
Paul Duffin | 672cb9f | 2021-03-03 02:30:37 +0000 | [diff] [blame] | 1059 | gen := result.Module("foo", "").(*Module) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1060 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1061 | expectedOutputFiles := []string{"outputbase/execroot/__main__/bazelone.txt", |
| 1062 | "outputbase/execroot/__main__/bazeltwo.txt"} |
Paul Duffin | e84b133 | 2021-03-12 11:59:43 +0000 | [diff] [blame] | 1063 | android.AssertDeepEquals(t, "output files", expectedOutputFiles, gen.outputFiles.Strings()) |
| 1064 | android.AssertDeepEquals(t, "output deps", expectedOutputFiles, gen.outputDeps.Strings()) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 1065 | } |
| 1066 | |
Jihoon Kang | c170af4 | 2022-08-20 05:26:38 +0000 | [diff] [blame] | 1067 | func TestGenruleWithGlobPaths(t *testing.T) { |
| 1068 | testcases := []struct { |
| 1069 | name string |
| 1070 | bp string |
| 1071 | additionalFiles android.MockFS |
| 1072 | expectedCmd string |
| 1073 | }{ |
| 1074 | { |
| 1075 | name: "single file in directory with $ sign", |
| 1076 | bp: ` |
| 1077 | genrule { |
| 1078 | name: "gen", |
| 1079 | srcs: ["inn*.txt"], |
| 1080 | out: ["out.txt"], |
| 1081 | cmd: "cp $(in) $(out)", |
| 1082 | } |
| 1083 | `, |
| 1084 | additionalFiles: android.MockFS{"inn$1.txt": nil}, |
| 1085 | expectedCmd: "cp 'inn$1.txt' __SBOX_SANDBOX_DIR__/out/out.txt", |
| 1086 | }, |
| 1087 | { |
| 1088 | name: "multiple file in directory with $ sign", |
| 1089 | bp: ` |
| 1090 | genrule { |
| 1091 | name: "gen", |
| 1092 | srcs: ["inn*.txt"], |
| 1093 | out: ["."], |
| 1094 | cmd: "cp $(in) $(out)", |
| 1095 | } |
| 1096 | `, |
| 1097 | additionalFiles: android.MockFS{"inn$1.txt": nil, "inn$2.txt": nil}, |
| 1098 | expectedCmd: "cp 'inn$1.txt' 'inn$2.txt' __SBOX_SANDBOX_DIR__/out", |
| 1099 | }, |
| 1100 | { |
| 1101 | name: "file in directory with other shell unsafe character", |
| 1102 | bp: ` |
| 1103 | genrule { |
| 1104 | name: "gen", |
| 1105 | srcs: ["inn*.txt"], |
| 1106 | out: ["out.txt"], |
| 1107 | cmd: "cp $(in) $(out)", |
| 1108 | } |
| 1109 | `, |
| 1110 | additionalFiles: android.MockFS{"inn@1.txt": nil}, |
| 1111 | expectedCmd: "cp 'inn@1.txt' __SBOX_SANDBOX_DIR__/out/out.txt", |
| 1112 | }, |
| 1113 | { |
| 1114 | name: "glob location param with filepath containing $", |
| 1115 | bp: ` |
| 1116 | genrule { |
| 1117 | name: "gen", |
| 1118 | srcs: ["**/inn*"], |
| 1119 | out: ["."], |
| 1120 | cmd: "cp $(in) $(location **/inn*)", |
| 1121 | } |
| 1122 | `, |
| 1123 | additionalFiles: android.MockFS{"a/inn$1.txt": nil}, |
| 1124 | expectedCmd: "cp 'a/inn$1.txt' 'a/inn$1.txt'", |
| 1125 | }, |
| 1126 | { |
| 1127 | name: "glob locations param with filepath containing $", |
| 1128 | bp: ` |
| 1129 | genrule { |
| 1130 | name: "gen", |
| 1131 | tool_files: ["**/inn*"], |
| 1132 | out: ["out.txt"], |
| 1133 | cmd: "cp $(locations **/inn*) $(out)", |
| 1134 | } |
| 1135 | `, |
| 1136 | additionalFiles: android.MockFS{"a/inn$1.txt": nil}, |
| 1137 | expectedCmd: "cp '__SBOX_SANDBOX_DIR__/tools/src/a/inn$1.txt' __SBOX_SANDBOX_DIR__/out/out.txt", |
| 1138 | }, |
| 1139 | } |
| 1140 | |
| 1141 | for _, test := range testcases { |
| 1142 | t.Run(test.name, func(t *testing.T) { |
| 1143 | result := android.GroupFixturePreparers( |
| 1144 | prepareForGenRuleTest, |
| 1145 | android.FixtureMergeMockFs(test.additionalFiles), |
| 1146 | ).RunTestWithBp(t, test.bp) |
| 1147 | gen := result.Module("gen", "").(*Module) |
| 1148 | android.AssertStringEquals(t, "command", test.expectedCmd, gen.rawCommands[0]) |
| 1149 | }) |
| 1150 | } |
| 1151 | } |
| 1152 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 1153 | type testTool struct { |
| 1154 | android.ModuleBase |
| 1155 | outputFile android.Path |
| 1156 | } |
| 1157 | |
| 1158 | func toolFactory() android.Module { |
| 1159 | module := &testTool{} |
| 1160 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
| 1161 | return module |
| 1162 | } |
| 1163 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 1164 | func (t *testTool) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 1165 | t.outputFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), ctx.ModuleName(), android.PathForOutput(ctx, ctx.ModuleName())) |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | func (t *testTool) HostToolPath() android.OptionalPath { |
| 1169 | return android.OptionalPathForPath(t.outputFile) |
| 1170 | } |
| 1171 | |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 1172 | type prebuiltTestTool struct { |
| 1173 | android.ModuleBase |
| 1174 | prebuilt android.Prebuilt |
| 1175 | testTool |
| 1176 | } |
| 1177 | |
| 1178 | func (p *prebuiltTestTool) Name() string { |
| 1179 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 1180 | } |
| 1181 | |
| 1182 | func (p *prebuiltTestTool) Prebuilt() *android.Prebuilt { |
| 1183 | return &p.prebuilt |
| 1184 | } |
| 1185 | |
| 1186 | func (t *prebuiltTestTool) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1187 | t.outputFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "prebuilt_bin"), ctx.ModuleName(), android.PathForOutput(ctx, ctx.ModuleName())) |
| 1188 | } |
| 1189 | |
| 1190 | func prebuiltToolFactory() android.Module { |
| 1191 | module := &prebuiltTestTool{} |
| 1192 | android.InitPrebuiltModuleWithoutSrcs(module) |
| 1193 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
| 1194 | return module |
| 1195 | } |
| 1196 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 1197 | var _ android.HostToolProvider = (*testTool)(nil) |
Martin Stjernholm | dbd814d | 2022-01-12 23:18:30 +0000 | [diff] [blame] | 1198 | var _ android.HostToolProvider = (*prebuiltTestTool)(nil) |
Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 1199 | |
| 1200 | type testOutputProducer struct { |
| 1201 | android.ModuleBase |
| 1202 | outputFile android.Path |
| 1203 | } |
| 1204 | |
| 1205 | func outputProducerFactory() android.Module { |
| 1206 | module := &testOutputProducer{} |
| 1207 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
| 1208 | return module |
| 1209 | } |
| 1210 | |
| 1211 | func (t *testOutputProducer) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1212 | t.outputFile = ctx.InstallFile(android.PathForModuleInstall(ctx, "bin"), ctx.ModuleName(), android.PathForOutput(ctx, ctx.ModuleName())) |
| 1213 | } |
| 1214 | |
| 1215 | func (t *testOutputProducer) OutputFiles(tag string) (android.Paths, error) { |
| 1216 | return android.Paths{t.outputFile}, nil |
| 1217 | } |
| 1218 | |
| 1219 | var _ android.OutputFileProducer = (*testOutputProducer)(nil) |
Jooyung Han | 8c7e3ed | 2021-06-28 17:35:58 +0900 | [diff] [blame] | 1220 | |
| 1221 | type useSource struct { |
| 1222 | android.ModuleBase |
| 1223 | props struct { |
| 1224 | Srcs []string `android:"path"` |
| 1225 | } |
| 1226 | srcs android.Paths |
| 1227 | } |
| 1228 | |
| 1229 | func (s *useSource) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 1230 | s.srcs = android.PathsForModuleSrc(ctx, s.props.Srcs) |
| 1231 | } |
| 1232 | |
| 1233 | func useSourceFactory() android.Module { |
| 1234 | module := &useSource{} |
| 1235 | module.AddProperties(&module.props) |
| 1236 | android.InitAndroidModule(module) |
| 1237 | return module |
| 1238 | } |