Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | // Copyright 2020 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 ( |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 18 | "fmt" |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 19 | "strings" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 20 | "testing" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 21 | |
| 22 | "android/soong/android" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 23 | "android/soong/android/allowlists" |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 24 | "android/soong/bazel" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 25 | "android/soong/python" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 29 | testCases := []struct { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 30 | description string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 31 | bp string |
| 32 | expectedBazelTarget string |
| 33 | }{ |
| 34 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 35 | description: "only name", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 36 | bp: `custom { name: "foo" } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 37 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 38 | expectedBazelTarget: `soong_module( |
| 39 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 40 | soong_module_name = "foo", |
| 41 | soong_module_type = "custom", |
| 42 | soong_module_variant = "", |
| 43 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 44 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 45 | bool_prop = False, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 46 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 47 | )`, |
| 48 | }, |
| 49 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 50 | description: "handles bool", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 51 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 52 | name: "foo", |
| 53 | bool_prop: true, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 54 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 55 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 56 | expectedBazelTarget: `soong_module( |
| 57 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 58 | soong_module_name = "foo", |
| 59 | soong_module_type = "custom", |
| 60 | soong_module_variant = "", |
| 61 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 62 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 63 | bool_prop = True, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 64 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 65 | )`, |
| 66 | }, |
| 67 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 68 | description: "string escaping", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 69 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 70 | name: "foo", |
| 71 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 72 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 73 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 74 | expectedBazelTarget: `soong_module( |
| 75 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 76 | soong_module_name = "foo", |
| 77 | soong_module_type = "custom", |
| 78 | soong_module_variant = "", |
| 79 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 80 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 81 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 82 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 83 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 84 | )`, |
| 85 | }, |
| 86 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 87 | description: "single item string list", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 88 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 89 | name: "foo", |
| 90 | required: ["bar"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 91 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 92 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 93 | expectedBazelTarget: `soong_module( |
| 94 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 95 | soong_module_name = "foo", |
| 96 | soong_module_type = "custom", |
| 97 | soong_module_variant = "", |
| 98 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 99 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 100 | bool_prop = False, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 101 | required = ["bar"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 102 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 103 | )`, |
| 104 | }, |
| 105 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 106 | description: "list of strings", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 107 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 108 | name: "foo", |
| 109 | target_required: ["qux", "bazqux"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 110 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 111 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 112 | expectedBazelTarget: `soong_module( |
| 113 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 114 | soong_module_name = "foo", |
| 115 | soong_module_type = "custom", |
| 116 | soong_module_variant = "", |
| 117 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 118 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 119 | bool_prop = False, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 120 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 121 | target_required = [ |
| 122 | "qux", |
| 123 | "bazqux", |
| 124 | ], |
| 125 | )`, |
| 126 | }, |
| 127 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 128 | description: "dist/dists", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 129 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 130 | name: "foo", |
| 131 | dist: { |
| 132 | targets: ["goal_foo"], |
| 133 | tag: ".foo", |
| 134 | }, |
| 135 | dists: [{ |
| 136 | targets: ["goal_bar"], |
| 137 | tag: ".bar", |
| 138 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 139 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 140 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 141 | expectedBazelTarget: `soong_module( |
| 142 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 143 | soong_module_name = "foo", |
| 144 | soong_module_type = "custom", |
| 145 | soong_module_variant = "", |
| 146 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 147 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 148 | bool_prop = False, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 149 | dist = { |
| 150 | "tag": ".foo", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 151 | "targets": ["goal_foo"], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 152 | }, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 153 | dists = [{ |
| 154 | "tag": ".bar", |
| 155 | "targets": ["goal_bar"], |
| 156 | }], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 157 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 158 | )`, |
| 159 | }, |
| 160 | { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 161 | description: "put it together", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 162 | bp: `custom { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 163 | name: "foo", |
| 164 | required: ["bar"], |
| 165 | target_required: ["qux", "bazqux"], |
| 166 | bool_prop: true, |
| 167 | owner: "custom_owner", |
| 168 | dists: [ |
| 169 | { |
| 170 | tag: ".tag", |
| 171 | targets: ["my_goal"], |
| 172 | }, |
| 173 | ], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 174 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 175 | `, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 176 | expectedBazelTarget: `soong_module( |
| 177 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 178 | soong_module_name = "foo", |
| 179 | soong_module_type = "custom", |
| 180 | soong_module_variant = "", |
| 181 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 182 | ], |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 183 | bool_prop = True, |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 184 | dists = [{ |
| 185 | "tag": ".tag", |
| 186 | "targets": ["my_goal"], |
| 187 | }], |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 188 | owner = "custom_owner", |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 189 | required = ["bar"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 190 | string_prop = "", |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 191 | target_required = [ |
| 192 | "qux", |
| 193 | "bazqux", |
| 194 | ], |
| 195 | )`, |
| 196 | }, |
| 197 | } |
| 198 | |
| 199 | dir := "." |
| 200 | for _, testCase := range testCases { |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 201 | t.Run(testCase.description, func(t *testing.T) { |
| 202 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 203 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 204 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 205 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 206 | ctx.Register() |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 207 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 208 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 209 | android.FailIfErrored(t, errs) |
| 210 | _, errs = ctx.PrepareBuildActions(config) |
| 211 | android.FailIfErrored(t, errs) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 212 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 213 | codegenCtx := NewCodegenContext(config, ctx.Context, QueryView, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 214 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 215 | android.FailIfErrored(t, err) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 216 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 217 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
| 218 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 219 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 220 | actualBazelTarget := bazelTargets[0] |
| 221 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 222 | t.Errorf( |
| 223 | "Expected generated Bazel target to be '%s', got '%s'", |
| 224 | testCase.expectedBazelTarget, |
| 225 | actualBazelTarget.content, |
| 226 | ) |
| 227 | } |
| 228 | }) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | func TestGenerateBazelTargetModules(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 233 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 234 | { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 235 | Description: "string prop empty", |
| 236 | Blueprint: `custom { |
| 237 | name: "foo", |
| 238 | string_literal_prop: "", |
| 239 | bazel_module: { bp2build_available: true }, |
| 240 | }`, |
| 241 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 242 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 243 | "string_literal_prop": `""`, |
| 244 | }), |
| 245 | }, |
| 246 | }, |
| 247 | { |
| 248 | Description: `string prop "PROP"`, |
| 249 | Blueprint: `custom { |
| 250 | name: "foo", |
| 251 | string_literal_prop: "PROP", |
| 252 | bazel_module: { bp2build_available: true }, |
| 253 | }`, |
| 254 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 255 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 256 | "string_literal_prop": `"PROP"`, |
| 257 | }), |
| 258 | }, |
| 259 | }, |
| 260 | { |
| 261 | Description: `string prop arch variant`, |
| 262 | Blueprint: `custom { |
| 263 | name: "foo", |
| 264 | arch: { |
| 265 | arm: { string_literal_prop: "ARM" }, |
| 266 | arm64: { string_literal_prop: "ARM64" }, |
| 267 | }, |
| 268 | bazel_module: { bp2build_available: true }, |
| 269 | }`, |
| 270 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 271 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 272 | "string_literal_prop": `select({ |
| 273 | "//build/bazel/platforms/arch:arm": "ARM", |
| 274 | "//build/bazel/platforms/arch:arm64": "ARM64", |
| 275 | "//conditions:default": None, |
| 276 | })`, |
| 277 | }), |
| 278 | }, |
| 279 | }, |
| 280 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 281 | Description: "string ptr props", |
| 282 | Blueprint: `custom { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 283 | name: "foo", |
| 284 | string_ptr_prop: "", |
| 285 | bazel_module: { bp2build_available: true }, |
| 286 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 287 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 288 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 289 | "string_ptr_prop": `""`, |
| 290 | }), |
| 291 | }, |
| 292 | }, |
| 293 | { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 294 | Description: "string list props", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 295 | Blueprint: `custom { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 296 | name: "foo", |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 297 | string_list_prop: ["a", "b"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 298 | string_ptr_prop: "a", |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 299 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 300 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 301 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 302 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 303 | "string_list_prop": `[ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 304 | "a", |
| 305 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 306 | ]`, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 307 | "string_ptr_prop": `"a"`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 308 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 309 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 310 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 311 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 312 | Description: "control characters", |
| 313 | Blueprint: `custom { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 314 | name: "foo", |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 315 | string_list_prop: ["\t", "\n"], |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 316 | string_ptr_prop: "a\t\n\r", |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 317 | bazel_module: { bp2build_available: true }, |
| 318 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 319 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 320 | MakeBazelTarget("custom", "foo", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 321 | "string_list_prop": `[ |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 322 | "\t", |
| 323 | "\n", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 324 | ]`, |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 325 | "string_ptr_prop": `"a\t\n\r"`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 326 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 327 | }, |
| 328 | }, |
| 329 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 330 | Description: "handles dep", |
| 331 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 332 | name: "has_dep", |
| 333 | arch_paths: [":dep"], |
| 334 | bazel_module: { bp2build_available: true }, |
| 335 | } |
| 336 | |
| 337 | custom { |
| 338 | name: "dep", |
| 339 | arch_paths: ["abc"], |
| 340 | bazel_module: { bp2build_available: true }, |
| 341 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 342 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 343 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 344 | "arch_paths": `["abc"]`, |
| 345 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 346 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 347 | "arch_paths": `[":dep"]`, |
| 348 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 349 | }, |
| 350 | }, |
| 351 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 352 | Description: "non-existent dep", |
| 353 | Blueprint: `custom { |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 354 | name: "has_dep", |
| 355 | arch_paths: [":dep"], |
| 356 | bazel_module: { bp2build_available: true }, |
| 357 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 358 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 359 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 360 | "arch_paths": `[":dep__BP2BUILD__MISSING__DEP"]`, |
| 361 | }), |
| 362 | }, |
| 363 | }, |
| 364 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 365 | Description: "arch-variant srcs", |
| 366 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 367 | name: "arch_paths", |
| 368 | arch: { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 369 | x86: { arch_paths: ["x86.txt"] }, |
| 370 | x86_64: { arch_paths: ["x86_64.txt"] }, |
| 371 | arm: { arch_paths: ["arm.txt"] }, |
| 372 | arm64: { arch_paths: ["arm64.txt"] }, |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 373 | riscv64: { arch_paths: ["riscv64.txt"] }, |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 374 | }, |
| 375 | target: { |
| 376 | linux: { arch_paths: ["linux.txt"] }, |
| 377 | bionic: { arch_paths: ["bionic.txt"] }, |
| 378 | host: { arch_paths: ["host.txt"] }, |
| 379 | not_windows: { arch_paths: ["not_windows.txt"] }, |
| 380 | android: { arch_paths: ["android.txt"] }, |
| 381 | linux_musl: { arch_paths: ["linux_musl.txt"] }, |
| 382 | musl: { arch_paths: ["musl.txt"] }, |
| 383 | linux_glibc: { arch_paths: ["linux_glibc.txt"] }, |
| 384 | glibc: { arch_paths: ["glibc.txt"] }, |
| 385 | linux_bionic: { arch_paths: ["linux_bionic.txt"] }, |
| 386 | darwin: { arch_paths: ["darwin.txt"] }, |
| 387 | windows: { arch_paths: ["windows.txt"] }, |
| 388 | }, |
| 389 | multilib: { |
| 390 | lib32: { arch_paths: ["lib32.txt"] }, |
| 391 | lib64: { arch_paths: ["lib64.txt"] }, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 392 | }, |
| 393 | bazel_module: { bp2build_available: true }, |
| 394 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 395 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 396 | MakeBazelTarget("custom", "arch_paths", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 397 | "arch_paths": `select({ |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 398 | "//build/bazel/platforms/arch:arm": [ |
| 399 | "arm.txt", |
| 400 | "lib32.txt", |
| 401 | ], |
| 402 | "//build/bazel/platforms/arch:arm64": [ |
| 403 | "arm64.txt", |
| 404 | "lib64.txt", |
| 405 | ], |
Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 406 | "//build/bazel/platforms/arch:riscv64": [ |
| 407 | "riscv64.txt", |
| 408 | "lib64.txt", |
| 409 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 410 | "//build/bazel/platforms/arch:x86": [ |
| 411 | "x86.txt", |
| 412 | "lib32.txt", |
| 413 | ], |
| 414 | "//build/bazel/platforms/arch:x86_64": [ |
| 415 | "x86_64.txt", |
| 416 | "lib64.txt", |
| 417 | ], |
| 418 | "//conditions:default": [], |
| 419 | }) + select({ |
| 420 | "//build/bazel/platforms/os:android": [ |
| 421 | "linux.txt", |
| 422 | "bionic.txt", |
| 423 | "android.txt", |
| 424 | ], |
| 425 | "//build/bazel/platforms/os:darwin": [ |
| 426 | "host.txt", |
| 427 | "darwin.txt", |
| 428 | "not_windows.txt", |
| 429 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 430 | "//build/bazel/platforms/os:linux_bionic": [ |
| 431 | "host.txt", |
| 432 | "linux.txt", |
| 433 | "bionic.txt", |
| 434 | "linux_bionic.txt", |
| 435 | "not_windows.txt", |
| 436 | ], |
Colin Cross | 133782e | 2022-12-20 15:29:31 -0800 | [diff] [blame] | 437 | "//build/bazel/platforms/os:linux_glibc": [ |
| 438 | "host.txt", |
| 439 | "linux.txt", |
| 440 | "glibc.txt", |
| 441 | "linux_glibc.txt", |
| 442 | "not_windows.txt", |
| 443 | ], |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 444 | "//build/bazel/platforms/os:linux_musl": [ |
| 445 | "host.txt", |
| 446 | "linux.txt", |
| 447 | "musl.txt", |
| 448 | "linux_musl.txt", |
| 449 | "not_windows.txt", |
| 450 | ], |
| 451 | "//build/bazel/platforms/os:windows": [ |
| 452 | "host.txt", |
| 453 | "windows.txt", |
| 454 | ], |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 455 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 456 | })`, |
| 457 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 458 | }, |
| 459 | }, |
| 460 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 461 | Description: "arch-variant deps", |
| 462 | Blueprint: `custom { |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 463 | name: "has_dep", |
| 464 | arch: { |
| 465 | x86: { |
| 466 | arch_paths: [":dep"], |
| 467 | }, |
| 468 | }, |
| 469 | bazel_module: { bp2build_available: true }, |
| 470 | } |
| 471 | |
| 472 | custom { |
| 473 | name: "dep", |
| 474 | arch_paths: ["abc"], |
| 475 | bazel_module: { bp2build_available: true }, |
| 476 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 477 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 478 | MakeBazelTarget("custom", "dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 479 | "arch_paths": `["abc"]`, |
| 480 | }), |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 481 | MakeBazelTarget("custom", "has_dep", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 482 | "arch_paths": `select({ |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 483 | "//build/bazel/platforms/arch:x86": [":dep"], |
| 484 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 485 | })`, |
| 486 | }), |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 487 | }, |
Jingwen Chen | 58a12b8 | 2021-03-30 13:08:36 +0000 | [diff] [blame] | 488 | }, |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 489 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 490 | Description: "embedded props", |
| 491 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 492 | name: "embedded_props", |
| 493 | embedded_prop: "abc", |
| 494 | bazel_module: { bp2build_available: true }, |
| 495 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 496 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 497 | MakeBazelTarget("custom", "embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 498 | "embedded_attr": `"abc"`, |
| 499 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 500 | }, |
| 501 | }, |
| 502 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 503 | Description: "ptr to embedded props", |
| 504 | Blueprint: `custom { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 505 | name: "ptr_to_embedded_props", |
| 506 | other_embedded_prop: "abc", |
| 507 | bazel_module: { bp2build_available: true }, |
| 508 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 509 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 510 | MakeBazelTarget("custom", "ptr_to_embedded_props", AttrNameToString{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 511 | "other_embedded_attr": `"abc"`, |
| 512 | }), |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 513 | }, |
| 514 | }, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | dir := "." |
| 518 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 519 | t.Run(testCase.Description, func(t *testing.T) { |
| 520 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, nil) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 521 | ctx := android.NewTestContext(config) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 522 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 523 | registerCustomModuleForBp2buildConversion(ctx) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 524 | |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 525 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 526 | if errored(t, testCase, errs) { |
| 527 | return |
| 528 | } |
| 529 | _, errs = ctx.ResolveDependencies(config) |
| 530 | if errored(t, testCase, errs) { |
| 531 | return |
| 532 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 533 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 534 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 535 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 536 | android.FailIfErrored(t, err) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 537 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 538 | if actualCount, expectedCount := len(bazelTargets), len(testCase.ExpectedBazelTargets); actualCount != expectedCount { |
| 539 | t.Errorf("Expected %d bazel target (%s),\ngot %d (%s)", expectedCount, testCase.ExpectedBazelTargets, actualCount, bazelTargets) |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 540 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 541 | for i, expectedBazelTarget := range testCase.ExpectedBazelTargets { |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 542 | actualBazelTarget := bazelTargets[i] |
| 543 | if actualBazelTarget.content != expectedBazelTarget { |
| 544 | t.Errorf( |
| 545 | "Expected generated Bazel target to be '%s', got '%s'", |
| 546 | expectedBazelTarget, |
| 547 | actualBazelTarget.content, |
| 548 | ) |
| 549 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 550 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 551 | } |
Liz Kammer | fdd72e6 | 2021-10-11 15:41:03 -0400 | [diff] [blame] | 552 | }) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 553 | } |
| 554 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 555 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 556 | func TestBp2buildHostAndDevice(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 557 | testCases := []Bp2buildTestCase{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 558 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 559 | Description: "host and device, device only", |
| 560 | ModuleTypeUnderTest: "custom", |
| 561 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 562 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 563 | name: "foo", |
| 564 | bazel_module: { bp2build_available: true }, |
| 565 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 566 | ExpectedBazelTargets: []string{ |
| 567 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 568 | }, |
| 569 | }, |
| 570 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 571 | Description: "host and device, both", |
| 572 | ModuleTypeUnderTest: "custom", |
| 573 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 574 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 575 | name: "foo", |
| 576 | host_supported: true, |
| 577 | bazel_module: { bp2build_available: true }, |
| 578 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 579 | ExpectedBazelTargets: []string{ |
| 580 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 581 | }, |
| 582 | }, |
| 583 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 584 | Description: "host and device, host explicitly disabled", |
| 585 | ModuleTypeUnderTest: "custom", |
| 586 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 587 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 588 | name: "foo", |
| 589 | host_supported: false, |
| 590 | bazel_module: { bp2build_available: true }, |
| 591 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 592 | ExpectedBazelTargets: []string{ |
| 593 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 594 | }, |
| 595 | }, |
| 596 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 597 | Description: "host and device, neither", |
| 598 | ModuleTypeUnderTest: "custom", |
| 599 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 600 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 601 | name: "foo", |
| 602 | host_supported: false, |
| 603 | device_supported: false, |
| 604 | bazel_module: { bp2build_available: true }, |
| 605 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 606 | ExpectedBazelTargets: []string{ |
| 607 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 608 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 609 | }), |
| 610 | }, |
| 611 | }, |
| 612 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 613 | Description: "host and device, neither, cannot override with product_var", |
| 614 | ModuleTypeUnderTest: "custom", |
| 615 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 616 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 617 | name: "foo", |
| 618 | host_supported: false, |
| 619 | device_supported: false, |
| 620 | product_variables: { unbundled_build: { enabled: true } }, |
| 621 | bazel_module: { bp2build_available: true }, |
| 622 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 623 | ExpectedBazelTargets: []string{ |
| 624 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 625 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 626 | }), |
| 627 | }, |
| 628 | }, |
| 629 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 630 | Description: "host and device, both, disabled overrided with product_var", |
| 631 | ModuleTypeUnderTest: "custom", |
| 632 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 633 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 634 | name: "foo", |
| 635 | host_supported: true, |
| 636 | device_supported: true, |
| 637 | enabled: false, |
| 638 | product_variables: { unbundled_build: { enabled: true } }, |
| 639 | bazel_module: { bp2build_available: true }, |
| 640 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 641 | ExpectedBazelTargets: []string{ |
| 642 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 643 | "target_compatible_with": `["//build/bazel/product_variables:unbundled_build"]`, |
| 644 | }), |
| 645 | }, |
| 646 | }, |
| 647 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 648 | Description: "host and device, neither, cannot override with arch enabled", |
| 649 | ModuleTypeUnderTest: "custom", |
| 650 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 651 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 652 | name: "foo", |
| 653 | host_supported: false, |
| 654 | device_supported: false, |
| 655 | arch: { x86: { enabled: true } }, |
| 656 | bazel_module: { bp2build_available: true }, |
| 657 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 658 | ExpectedBazelTargets: []string{ |
| 659 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 660 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 661 | }), |
| 662 | }, |
| 663 | }, |
| 664 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 665 | Description: "host and device, host only", |
| 666 | ModuleTypeUnderTest: "custom", |
| 667 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 668 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 669 | name: "foo", |
| 670 | host_supported: true, |
| 671 | device_supported: false, |
| 672 | bazel_module: { bp2build_available: true }, |
| 673 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 674 | ExpectedBazelTargets: []string{ |
| 675 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 676 | }, |
| 677 | }, |
| 678 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 679 | Description: "host only", |
| 680 | ModuleTypeUnderTest: "custom", |
| 681 | ModuleTypeUnderTestFactory: customModuleFactoryHostSupported, |
| 682 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 683 | name: "foo", |
| 684 | bazel_module: { bp2build_available: true }, |
| 685 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 686 | ExpectedBazelTargets: []string{ |
| 687 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 688 | }, |
| 689 | }, |
| 690 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 691 | Description: "device only", |
| 692 | ModuleTypeUnderTest: "custom", |
| 693 | ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported, |
| 694 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 695 | name: "foo", |
| 696 | bazel_module: { bp2build_available: true }, |
| 697 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 698 | ExpectedBazelTargets: []string{ |
| 699 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 700 | }, |
| 701 | }, |
| 702 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 703 | Description: "host and device default, default", |
| 704 | ModuleTypeUnderTest: "custom", |
| 705 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 706 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 707 | name: "foo", |
| 708 | bazel_module: { bp2build_available: true }, |
| 709 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 710 | ExpectedBazelTargets: []string{ |
| 711 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 712 | }, |
| 713 | }, |
| 714 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 715 | Description: "host and device default, device only", |
| 716 | ModuleTypeUnderTest: "custom", |
| 717 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 718 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 719 | name: "foo", |
| 720 | host_supported: false, |
| 721 | bazel_module: { bp2build_available: true }, |
| 722 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 723 | ExpectedBazelTargets: []string{ |
| 724 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 725 | }, |
| 726 | }, |
| 727 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 728 | Description: "host and device default, host only", |
| 729 | ModuleTypeUnderTest: "custom", |
| 730 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 731 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 732 | name: "foo", |
| 733 | device_supported: false, |
| 734 | bazel_module: { bp2build_available: true }, |
| 735 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 736 | ExpectedBazelTargets: []string{ |
| 737 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 738 | }, |
| 739 | }, |
| 740 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 741 | Description: "host and device default, neither", |
| 742 | ModuleTypeUnderTest: "custom", |
| 743 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 744 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 745 | name: "foo", |
| 746 | host_supported: false, |
| 747 | device_supported: false, |
| 748 | bazel_module: { bp2build_available: true }, |
| 749 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 750 | ExpectedBazelTargets: []string{ |
| 751 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 752 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 753 | }), |
| 754 | }, |
| 755 | }, |
| 756 | } |
| 757 | |
| 758 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 759 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 760 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 761 | }) |
| 762 | } |
| 763 | } |
| 764 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 765 | func TestLoadStatements(t *testing.T) { |
| 766 | testCases := []struct { |
| 767 | bazelTargets BazelTargets |
| 768 | expectedLoadStatements string |
| 769 | }{ |
| 770 | { |
| 771 | bazelTargets: BazelTargets{ |
| 772 | BazelTarget{ |
| 773 | name: "foo", |
| 774 | ruleClass: "cc_library", |
| 775 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 776 | }, |
| 777 | }, |
| 778 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 779 | }, |
| 780 | { |
| 781 | bazelTargets: BazelTargets{ |
| 782 | BazelTarget{ |
| 783 | name: "foo", |
| 784 | ruleClass: "cc_library", |
| 785 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 786 | }, |
| 787 | BazelTarget{ |
| 788 | name: "bar", |
| 789 | ruleClass: "cc_library", |
| 790 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 791 | }, |
| 792 | }, |
| 793 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 794 | }, |
| 795 | { |
| 796 | bazelTargets: BazelTargets{ |
| 797 | BazelTarget{ |
| 798 | name: "foo", |
| 799 | ruleClass: "cc_library", |
| 800 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 801 | }, |
| 802 | BazelTarget{ |
| 803 | name: "bar", |
| 804 | ruleClass: "cc_binary", |
| 805 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 806 | }, |
| 807 | }, |
| 808 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 809 | }, |
| 810 | { |
| 811 | bazelTargets: BazelTargets{ |
| 812 | BazelTarget{ |
| 813 | name: "foo", |
| 814 | ruleClass: "cc_library", |
| 815 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 816 | }, |
| 817 | BazelTarget{ |
| 818 | name: "bar", |
| 819 | ruleClass: "cc_binary", |
| 820 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 821 | }, |
| 822 | BazelTarget{ |
| 823 | name: "baz", |
| 824 | ruleClass: "java_binary", |
| 825 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 826 | }, |
| 827 | }, |
| 828 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 829 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 830 | }, |
| 831 | { |
| 832 | bazelTargets: BazelTargets{ |
| 833 | BazelTarget{ |
| 834 | name: "foo", |
| 835 | ruleClass: "cc_binary", |
| 836 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 837 | }, |
| 838 | BazelTarget{ |
| 839 | name: "bar", |
| 840 | ruleClass: "java_binary", |
| 841 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 842 | }, |
| 843 | BazelTarget{ |
| 844 | name: "baz", |
| 845 | ruleClass: "genrule", |
| 846 | // Note: no bzlLoadLocation for native rules |
| 847 | }, |
| 848 | }, |
| 849 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 850 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 851 | }, |
| 852 | } |
| 853 | |
| 854 | for _, testCase := range testCases { |
| 855 | actual := testCase.bazelTargets.LoadStatements() |
| 856 | expected := testCase.expectedLoadStatements |
| 857 | if actual != expected { |
| 858 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | } |
| 863 | |
| 864 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 865 | testCases := []struct { |
| 866 | bp string |
| 867 | expectedBazelTarget string |
| 868 | expectedBazelTargetCount int |
| 869 | expectedLoadStatements string |
| 870 | }{ |
| 871 | { |
| 872 | bp: `custom { |
| 873 | name: "bar", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 874 | host_supported: true, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 875 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 876 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 877 | }`, |
| 878 | expectedBazelTarget: `my_library( |
| 879 | name = "bar", |
| 880 | ) |
| 881 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 882 | proto_library( |
| 883 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 884 | ) |
| 885 | |
| 886 | my_proto_library( |
| 887 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 888 | )`, |
| 889 | expectedBazelTargetCount: 3, |
| 890 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 891 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 892 | }, |
| 893 | } |
| 894 | |
| 895 | dir := "." |
| 896 | for _, testCase := range testCases { |
| 897 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 898 | ctx := android.NewTestContext(config) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 899 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 900 | ctx.RegisterForBazelConversion() |
| 901 | |
| 902 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 903 | android.FailIfErrored(t, errs) |
| 904 | _, errs = ctx.ResolveDependencies(config) |
| 905 | android.FailIfErrored(t, errs) |
| 906 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 907 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 908 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 909 | android.FailIfErrored(t, err) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 910 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 911 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 912 | } |
| 913 | |
| 914 | actualBazelTargets := bazelTargets.String() |
| 915 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 916 | t.Errorf( |
| 917 | "Expected generated Bazel target to be '%s', got '%s'", |
| 918 | testCase.expectedBazelTarget, |
| 919 | actualBazelTargets, |
| 920 | ) |
| 921 | } |
| 922 | |
| 923 | actualLoadStatements := bazelTargets.LoadStatements() |
| 924 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 925 | t.Errorf( |
| 926 | "Expected generated load statements to be '%s', got '%s'", |
| 927 | testCase.expectedLoadStatements, |
| 928 | actualLoadStatements, |
| 929 | ) |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 934 | func TestModuleTypeBp2Build(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 935 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 936 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 937 | Description: "filegroup with does not specify srcs", |
| 938 | ModuleTypeUnderTest: "filegroup", |
| 939 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 940 | Blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 941 | name: "fg_foo", |
| 942 | bazel_module: { bp2build_available: true }, |
| 943 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 944 | ExpectedBazelTargets: []string{ |
| 945 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 946 | }, |
| 947 | }, |
| 948 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 949 | Description: "filegroup with no srcs", |
| 950 | ModuleTypeUnderTest: "filegroup", |
| 951 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 952 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 953 | name: "fg_foo", |
| 954 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 955 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 956 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 957 | ExpectedBazelTargets: []string{ |
| 958 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 959 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 960 | }, |
| 961 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 962 | Description: "filegroup with srcs", |
| 963 | ModuleTypeUnderTest: "filegroup", |
| 964 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 965 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 966 | name: "fg_foo", |
| 967 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 968 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 969 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 970 | ExpectedBazelTargets: []string{ |
| 971 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 972 | "srcs": `[ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 973 | "a", |
| 974 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 975 | ]`, |
| 976 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 977 | }, |
| 978 | }, |
| 979 | { |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 980 | Description: "filegroup with dot-slash-prefixed srcs", |
| 981 | ModuleTypeUnderTest: "filegroup", |
| 982 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 983 | Blueprint: `filegroup { |
| 984 | name: "fg_foo", |
| 985 | srcs: ["./a", "./b"], |
| 986 | bazel_module: { bp2build_available: true }, |
| 987 | }`, |
| 988 | ExpectedBazelTargets: []string{ |
| 989 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 990 | "srcs": `[ |
| 991 | "a", |
| 992 | "b", |
| 993 | ]`, |
| 994 | }), |
| 995 | }, |
| 996 | }, |
| 997 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 998 | Description: "filegroup with excludes srcs", |
| 999 | ModuleTypeUnderTest: "filegroup", |
| 1000 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1001 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1002 | name: "fg_foo", |
| 1003 | srcs: ["a", "b"], |
| 1004 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1005 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1006 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1007 | ExpectedBazelTargets: []string{ |
| 1008 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1009 | "srcs": `["b"]`, |
| 1010 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1011 | }, |
| 1012 | }, |
| 1013 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1014 | Description: "depends_on_other_dir_module", |
| 1015 | ModuleTypeUnderTest: "filegroup", |
| 1016 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1017 | Blueprint: `filegroup { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1018 | name: "fg_foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1019 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 1020 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1021 | "c", |
| 1022 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1023 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1024 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1025 | Filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1026 | "other/Android.bp": `filegroup { |
| 1027 | name: "foo", |
| 1028 | srcs: ["a", "b"], |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1029 | bazel_module: { bp2build_available: true }, |
| 1030 | }`, |
| 1031 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1032 | ExpectedBazelTargets: []string{ |
| 1033 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1034 | "srcs": `[ |
| 1035 | "//other:foo", |
| 1036 | "c", |
| 1037 | ]`, |
| 1038 | }), |
| 1039 | }, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1040 | }, |
| 1041 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1042 | Description: "depends_on_other_unconverted_module_error", |
| 1043 | ModuleTypeUnderTest: "filegroup", |
| 1044 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1045 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1046 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1047 | name: "foobar", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1048 | srcs: [ |
| 1049 | ":foo", |
| 1050 | "c", |
| 1051 | ], |
| 1052 | bazel_module: { bp2build_available: true }, |
| 1053 | }`, |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 1054 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`), |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1055 | Filesystem: map[string]string{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1056 | "other/Android.bp": `filegroup { |
| 1057 | name: "foo", |
| 1058 | srcs: ["a", "b"], |
| 1059 | }`, |
| 1060 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1061 | }, |
Usta Shrestha | 808bc71 | 2022-09-23 23:18:18 -0400 | [diff] [blame] | 1062 | { |
| 1063 | Description: "depends_on_other_missing_module_error", |
| 1064 | ModuleTypeUnderTest: "filegroup", |
| 1065 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1066 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1067 | Blueprint: `filegroup { |
| 1068 | name: "foobar", |
| 1069 | srcs: [ |
| 1070 | "c", |
| 1071 | "//other:foo", |
| 1072 | "//other:goo", |
| 1073 | ], |
| 1074 | bazel_module: { bp2build_available: true }, |
| 1075 | }`, |
| 1076 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`), |
| 1077 | Filesystem: map[string]string{"other/Android.bp": `filegroup { |
| 1078 | name: "foo", |
| 1079 | srcs: ["a"], |
| 1080 | bazel_module: { bp2build_available: true }, |
| 1081 | } |
| 1082 | `, |
| 1083 | }, |
| 1084 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1085 | } |
| 1086 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1087 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1088 | t.Run(testCase.Description, func(t *testing.T) { |
| 1089 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1090 | }) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1091 | } |
| 1092 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1093 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1094 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1095 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1096 | moduleTypeUnderTest string |
| 1097 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1098 | bp string |
| 1099 | expectedCount int |
| 1100 | description string |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1101 | }{ |
| 1102 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1103 | description: "explicitly unavailable", |
| 1104 | moduleTypeUnderTest: "filegroup", |
| 1105 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1106 | bp: `filegroup { |
| 1107 | name: "foo", |
| 1108 | srcs: ["a", "b"], |
| 1109 | bazel_module: { bp2build_available: false }, |
| 1110 | }`, |
| 1111 | expectedCount: 0, |
| 1112 | }, |
| 1113 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1114 | description: "implicitly unavailable", |
| 1115 | moduleTypeUnderTest: "filegroup", |
| 1116 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1117 | bp: `filegroup { |
| 1118 | name: "foo", |
| 1119 | srcs: ["a", "b"], |
| 1120 | }`, |
| 1121 | expectedCount: 0, |
| 1122 | }, |
| 1123 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1124 | description: "explicitly available", |
| 1125 | moduleTypeUnderTest: "filegroup", |
| 1126 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1127 | bp: `filegroup { |
| 1128 | name: "foo", |
| 1129 | srcs: ["a", "b"], |
| 1130 | bazel_module: { bp2build_available: true }, |
| 1131 | }`, |
| 1132 | expectedCount: 1, |
| 1133 | }, |
| 1134 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1135 | description: "generates more than 1 target if needed", |
| 1136 | moduleTypeUnderTest: "custom", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 1137 | moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1138 | bp: `custom { |
| 1139 | name: "foo", |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1140 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1141 | bazel_module: { bp2build_available: true }, |
| 1142 | }`, |
| 1143 | expectedCount: 3, |
| 1144 | }, |
| 1145 | } |
| 1146 | |
| 1147 | dir := "." |
| 1148 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1149 | t.Run(testCase.description, func(t *testing.T) { |
| 1150 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1151 | ctx := android.NewTestContext(config) |
| 1152 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1153 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1154 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1155 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1156 | android.FailIfErrored(t, errs) |
| 1157 | _, errs = ctx.ResolveDependencies(config) |
| 1158 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1159 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1160 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1161 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 1162 | android.FailIfErrored(t, err) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1163 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1164 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1165 | } |
| 1166 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1167 | } |
| 1168 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1169 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1170 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 1171 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1172 | moduleTypeUnderTest string |
| 1173 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1174 | expectedCount map[string]int |
| 1175 | description string |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1176 | bp2buildConfig allowlists.Bp2BuildConfig |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1177 | checkDir string |
| 1178 | fs map[string]string |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1179 | forceEnabledModules []string |
| 1180 | expectedErrorMessages []string |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1181 | }{ |
| 1182 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1183 | description: "test bp2build config package and subpackages config", |
| 1184 | moduleTypeUnderTest: "filegroup", |
| 1185 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1186 | expectedCount: map[string]int{ |
| 1187 | "migrated": 1, |
| 1188 | "migrated/but_not_really": 0, |
| 1189 | "migrated/but_not_really/but_really": 1, |
| 1190 | "not_migrated": 0, |
| 1191 | "also_not_migrated": 0, |
| 1192 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1193 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1194 | "migrated": allowlists.Bp2BuildDefaultTrueRecursively, |
| 1195 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1196 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1197 | }, |
| 1198 | fs: map[string]string{ |
| 1199 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1200 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 1201 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 1202 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 1203 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 1204 | }, |
| 1205 | }, |
| 1206 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1207 | description: "test bp2build config opt-in and opt-out", |
| 1208 | moduleTypeUnderTest: "filegroup", |
| 1209 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1210 | expectedCount: map[string]int{ |
| 1211 | "package-opt-in": 2, |
| 1212 | "package-opt-in/subpackage": 0, |
| 1213 | "package-opt-out": 1, |
| 1214 | "package-opt-out/subpackage": 0, |
| 1215 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1216 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1217 | "package-opt-in": allowlists.Bp2BuildDefaultFalse, |
| 1218 | "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1219 | }, |
| 1220 | fs: map[string]string{ |
| 1221 | "package-opt-in/Android.bp": ` |
| 1222 | filegroup { name: "opt-in-a" } |
| 1223 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1224 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1225 | `, |
| 1226 | |
| 1227 | "package-opt-in/subpackage/Android.bp": ` |
| 1228 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1229 | `, |
| 1230 | |
| 1231 | "package-opt-out/Android.bp": ` |
| 1232 | filegroup { name: "opt-out-a" } |
| 1233 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1234 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1235 | `, |
| 1236 | |
| 1237 | "package-opt-out/subpackage/Android.bp": ` |
| 1238 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1239 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 1240 | `, |
| 1241 | }, |
| 1242 | }, |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1243 | { |
| 1244 | description: "test force-enabled errors out", |
| 1245 | moduleTypeUnderTest: "filegroup", |
| 1246 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1247 | expectedCount: map[string]int{ |
| 1248 | "migrated": 0, |
| 1249 | "not_migrated": 0, |
| 1250 | }, |
| 1251 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1252 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1253 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
| 1254 | }, |
| 1255 | fs: map[string]string{ |
| 1256 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1257 | }, |
| 1258 | forceEnabledModules: []string{"a"}, |
| 1259 | expectedErrorMessages: []string{"Force Enabled Module a not converted"}, |
| 1260 | }, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | dir := "." |
| 1264 | for _, testCase := range testCases { |
| 1265 | fs := make(map[string][]byte) |
| 1266 | toParse := []string{ |
| 1267 | "Android.bp", |
| 1268 | } |
| 1269 | for f, content := range testCase.fs { |
| 1270 | if strings.HasSuffix(f, "Android.bp") { |
| 1271 | toParse = append(toParse, f) |
| 1272 | } |
| 1273 | fs[f] = []byte(content) |
| 1274 | } |
| 1275 | config := android.TestConfig(buildDir, nil, "", fs) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1276 | config.AddForceEnabledModules(testCase.forceEnabledModules) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1277 | ctx := android.NewTestContext(config) |
| 1278 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1279 | allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig) |
| 1280 | ctx.RegisterBp2BuildConfig(allowlist) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1281 | ctx.RegisterForBazelConversion() |
| 1282 | |
| 1283 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1284 | android.FailIfErrored(t, errs) |
| 1285 | _, errs = ctx.ResolveDependencies(config) |
| 1286 | android.FailIfErrored(t, errs) |
| 1287 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1288 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1289 | |
| 1290 | // For each directory, test that the expected number of generated targets is correct. |
| 1291 | for dir, expectedCount := range testCase.expectedCount { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1292 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1293 | android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1294 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 1295 | t.Fatalf( |
| 1296 | "%s: Expected %d bazel target for %s package, got %d", |
| 1297 | testCase.description, |
| 1298 | expectedCount, |
| 1299 | dir, |
| 1300 | actualCount) |
| 1301 | } |
| 1302 | |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1307 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1308 | testCases := []Bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1309 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1310 | Description: "filegroup bazel_module.label", |
| 1311 | ModuleTypeUnderTest: "filegroup", |
| 1312 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1313 | Blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1314 | name: "fg_foo", |
| 1315 | bazel_module: { label: "//other:fg_foo" }, |
| 1316 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1317 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1318 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1319 | "other/BUILD.bazel": `// BUILD file`, |
| 1320 | }, |
| 1321 | }, |
| 1322 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1323 | Description: "multiple bazel_module.label same BUILD", |
| 1324 | ModuleTypeUnderTest: "filegroup", |
| 1325 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1326 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1327 | name: "fg_foo", |
| 1328 | bazel_module: { label: "//other:fg_foo" }, |
| 1329 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1330 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1331 | filegroup { |
| 1332 | name: "foo", |
| 1333 | bazel_module: { label: "//other:foo" }, |
| 1334 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1335 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1336 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1337 | "other/BUILD.bazel": `// BUILD file`, |
| 1338 | }, |
| 1339 | }, |
| 1340 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1341 | Description: "filegroup bazel_module.label and bp2build in subdir", |
| 1342 | ModuleTypeUnderTest: "filegroup", |
| 1343 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1344 | Dir: "other", |
| 1345 | Blueprint: ``, |
| 1346 | Filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1347 | "other/Android.bp": `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1348 | name: "fg_foo", |
| 1349 | bazel_module: { |
| 1350 | bp2build_available: true, |
| 1351 | }, |
| 1352 | } |
| 1353 | filegroup { |
| 1354 | name: "fg_bar", |
| 1355 | bazel_module: { |
| 1356 | label: "//other:fg_bar" |
| 1357 | }, |
| 1358 | }`, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1359 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 1360 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1361 | ExpectedBazelTargets: []string{ |
| 1362 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1363 | }, |
| 1364 | }, |
| 1365 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1366 | Description: "filegroup bazel_module.label and filegroup bp2build", |
| 1367 | ModuleTypeUnderTest: "filegroup", |
| 1368 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1369 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1370 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1371 | "other/BUILD.bazel": `// BUILD file`, |
| 1372 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1373 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1374 | name: "fg_foo", |
| 1375 | bazel_module: { |
| 1376 | label: "//other:fg_foo", |
| 1377 | }, |
| 1378 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1379 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1380 | filegroup { |
| 1381 | name: "fg_bar", |
| 1382 | bazel_module: { |
| 1383 | bp2build_available: true, |
| 1384 | }, |
| 1385 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1386 | ExpectedBazelTargets: []string{ |
| 1387 | MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1388 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1389 | }, |
| 1390 | } |
| 1391 | |
| 1392 | dir := "." |
| 1393 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1394 | t.Run(testCase.Description, func(t *testing.T) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1395 | fs := make(map[string][]byte) |
| 1396 | toParse := []string{ |
| 1397 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1398 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1399 | for f, content := range testCase.Filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1400 | if strings.HasSuffix(f, "Android.bp") { |
| 1401 | toParse = append(toParse, f) |
| 1402 | } |
| 1403 | fs[f] = []byte(content) |
| 1404 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1405 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1406 | ctx := android.NewTestContext(config) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1407 | ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1408 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1409 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1410 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1411 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1412 | return |
| 1413 | } |
| 1414 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1415 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1416 | return |
| 1417 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1418 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1419 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1420 | if testCase.Dir != "" { |
| 1421 | checkDir = testCase.Dir |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1422 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1423 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1424 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 1425 | android.FailIfErrored(t, err) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1426 | bazelTargets.sort() |
| 1427 | actualCount := len(bazelTargets) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1428 | expectedCount := len(testCase.ExpectedBazelTargets) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1429 | if actualCount != expectedCount { |
| 1430 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1431 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1432 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1433 | actualContent := target.content |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1434 | expectedContent := testCase.ExpectedBazelTargets[i] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1435 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1436 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1437 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1438 | expectedContent, |
| 1439 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1440 | ) |
| 1441 | } |
| 1442 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1443 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1444 | } |
| 1445 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1446 | |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 1447 | func TestGlob(t *testing.T) { |
| 1448 | testCases := []Bp2buildTestCase{ |
| 1449 | { |
| 1450 | Description: "filegroup with glob", |
| 1451 | ModuleTypeUnderTest: "filegroup", |
| 1452 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1453 | Blueprint: `filegroup { |
| 1454 | name: "fg_foo", |
| 1455 | srcs: ["**/*.txt"], |
| 1456 | bazel_module: { bp2build_available: true }, |
| 1457 | }`, |
| 1458 | ExpectedBazelTargets: []string{ |
| 1459 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1460 | "srcs": `[ |
| 1461 | "other/a.txt", |
| 1462 | "other/b.txt", |
| 1463 | "other/subdir/a.txt", |
| 1464 | ]`, |
| 1465 | }), |
| 1466 | }, |
| 1467 | Filesystem: map[string]string{ |
| 1468 | "other/a.txt": "", |
| 1469 | "other/b.txt": "", |
| 1470 | "other/subdir/a.txt": "", |
| 1471 | "other/file": "", |
| 1472 | }, |
| 1473 | }, |
| 1474 | { |
| 1475 | Description: "filegroup with glob in subdir", |
| 1476 | ModuleTypeUnderTest: "filegroup", |
| 1477 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1478 | Dir: "other", |
| 1479 | Filesystem: map[string]string{ |
| 1480 | "other/Android.bp": `filegroup { |
| 1481 | name: "fg_foo", |
| 1482 | srcs: ["**/*.txt"], |
| 1483 | bazel_module: { bp2build_available: true }, |
| 1484 | }`, |
| 1485 | "other/a.txt": "", |
| 1486 | "other/b.txt": "", |
| 1487 | "other/subdir/a.txt": "", |
| 1488 | "other/file": "", |
| 1489 | }, |
| 1490 | ExpectedBazelTargets: []string{ |
| 1491 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1492 | "srcs": `[ |
| 1493 | "a.txt", |
| 1494 | "b.txt", |
| 1495 | "subdir/a.txt", |
| 1496 | ]`, |
| 1497 | }), |
| 1498 | }, |
| 1499 | }, |
| 1500 | { |
| 1501 | Description: "filegroup with glob with no kept BUILD files", |
| 1502 | ModuleTypeUnderTest: "filegroup", |
| 1503 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1504 | KeepBuildFileForDirs: []string{ |
| 1505 | // empty |
| 1506 | }, |
| 1507 | Blueprint: `filegroup { |
| 1508 | name: "fg_foo", |
| 1509 | srcs: ["**/*.txt"], |
| 1510 | bazel_module: { bp2build_available: true }, |
| 1511 | }`, |
| 1512 | Filesystem: map[string]string{ |
| 1513 | "a.txt": "", |
| 1514 | "b.txt": "", |
| 1515 | "foo/BUILD": "", |
| 1516 | "foo/a.txt": "", |
| 1517 | "foo/bar/BUILD": "", |
| 1518 | "foo/bar/b.txt": "", |
| 1519 | }, |
| 1520 | ExpectedBazelTargets: []string{ |
| 1521 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1522 | "srcs": `[ |
| 1523 | "a.txt", |
| 1524 | "b.txt", |
| 1525 | "foo/a.txt", |
| 1526 | "foo/bar/b.txt", |
| 1527 | ]`, |
| 1528 | }), |
| 1529 | }, |
| 1530 | }, |
| 1531 | { |
| 1532 | Description: "filegroup with glob with kept BUILD file", |
| 1533 | ModuleTypeUnderTest: "filegroup", |
| 1534 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1535 | KeepBuildFileForDirs: []string{ |
| 1536 | "foo", |
| 1537 | }, |
| 1538 | Blueprint: `filegroup { |
| 1539 | name: "fg_foo", |
| 1540 | srcs: ["**/*.txt"], |
| 1541 | bazel_module: { bp2build_available: true }, |
| 1542 | }`, |
| 1543 | Filesystem: map[string]string{ |
| 1544 | "a.txt": "", |
| 1545 | "b.txt": "", |
| 1546 | "foo/BUILD": "", |
| 1547 | "foo/a.txt": "", |
| 1548 | "foo/bar/BUILD": "", |
| 1549 | "foo/bar/b.txt": "", |
| 1550 | }, |
| 1551 | ExpectedBazelTargets: []string{ |
| 1552 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1553 | "srcs": `[ |
| 1554 | "a.txt", |
| 1555 | "b.txt", |
| 1556 | "//foo:a.txt", |
| 1557 | "//foo:bar/b.txt", |
| 1558 | ]`, |
| 1559 | }), |
| 1560 | }, |
| 1561 | }, |
| 1562 | { |
| 1563 | Description: "filegroup with glob with kept BUILD.bazel file", |
| 1564 | ModuleTypeUnderTest: "filegroup", |
| 1565 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1566 | KeepBuildFileForDirs: []string{ |
| 1567 | "foo", |
| 1568 | }, |
| 1569 | Blueprint: `filegroup { |
| 1570 | name: "fg_foo", |
| 1571 | srcs: ["**/*.txt"], |
| 1572 | bazel_module: { bp2build_available: true }, |
| 1573 | }`, |
| 1574 | Filesystem: map[string]string{ |
| 1575 | "a.txt": "", |
| 1576 | "b.txt": "", |
| 1577 | "foo/BUILD.bazel": "", |
| 1578 | "foo/a.txt": "", |
| 1579 | "foo/bar/BUILD.bazel": "", |
| 1580 | "foo/bar/b.txt": "", |
| 1581 | }, |
| 1582 | ExpectedBazelTargets: []string{ |
| 1583 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1584 | "srcs": `[ |
| 1585 | "a.txt", |
| 1586 | "b.txt", |
| 1587 | "//foo:a.txt", |
| 1588 | "//foo:bar/b.txt", |
| 1589 | ]`, |
| 1590 | }), |
| 1591 | }, |
| 1592 | }, |
| 1593 | { |
| 1594 | Description: "filegroup with glob with Android.bp file as boundary", |
| 1595 | ModuleTypeUnderTest: "filegroup", |
| 1596 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1597 | Blueprint: `filegroup { |
| 1598 | name: "fg_foo", |
| 1599 | srcs: ["**/*.txt"], |
| 1600 | bazel_module: { bp2build_available: true }, |
| 1601 | }`, |
| 1602 | Filesystem: map[string]string{ |
| 1603 | "a.txt": "", |
| 1604 | "b.txt": "", |
| 1605 | "foo/Android.bp": "", |
| 1606 | "foo/a.txt": "", |
| 1607 | "foo/bar/Android.bp": "", |
| 1608 | "foo/bar/b.txt": "", |
| 1609 | }, |
| 1610 | ExpectedBazelTargets: []string{ |
| 1611 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1612 | "srcs": `[ |
| 1613 | "a.txt", |
| 1614 | "b.txt", |
| 1615 | "//foo:a.txt", |
| 1616 | "//foo/bar:b.txt", |
| 1617 | ]`, |
| 1618 | }), |
| 1619 | }, |
| 1620 | }, |
| 1621 | { |
| 1622 | Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file", |
| 1623 | ModuleTypeUnderTest: "filegroup", |
| 1624 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1625 | Dir: "other", |
| 1626 | KeepBuildFileForDirs: []string{ |
| 1627 | "other/foo", |
| 1628 | "other/foo/bar", |
| 1629 | // deliberately not other/foo/baz/BUILD. |
| 1630 | }, |
| 1631 | Filesystem: map[string]string{ |
| 1632 | "other/Android.bp": `filegroup { |
| 1633 | name: "fg_foo", |
| 1634 | srcs: ["**/*.txt"], |
| 1635 | bazel_module: { bp2build_available: true }, |
| 1636 | }`, |
| 1637 | "other/a.txt": "", |
| 1638 | "other/b.txt": "", |
| 1639 | "other/foo/BUILD": "", |
| 1640 | "other/foo/a.txt": "", |
| 1641 | "other/foo/bar/BUILD.bazel": "", |
| 1642 | "other/foo/bar/b.txt": "", |
| 1643 | "other/foo/baz/BUILD": "", |
| 1644 | "other/foo/baz/c.txt": "", |
| 1645 | }, |
| 1646 | ExpectedBazelTargets: []string{ |
| 1647 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1648 | "srcs": `[ |
| 1649 | "a.txt", |
| 1650 | "b.txt", |
| 1651 | "//other/foo:a.txt", |
| 1652 | "//other/foo/bar:b.txt", |
| 1653 | "//other/foo:baz/c.txt", |
| 1654 | ]`, |
| 1655 | }), |
| 1656 | }, |
| 1657 | }, |
| 1658 | } |
| 1659 | |
| 1660 | for _, testCase := range testCases { |
| 1661 | t.Run(testCase.Description, func(t *testing.T) { |
| 1662 | RunBp2BuildTestCaseSimple(t, testCase) |
| 1663 | }) |
| 1664 | } |
| 1665 | } |
| 1666 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1667 | func TestGlobExcludeSrcs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1668 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1669 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1670 | Description: "filegroup top level exclude_srcs", |
| 1671 | ModuleTypeUnderTest: "filegroup", |
| 1672 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1673 | Blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1674 | name: "fg_foo", |
| 1675 | srcs: ["**/*.txt"], |
| 1676 | exclude_srcs: ["c.txt"], |
| 1677 | bazel_module: { bp2build_available: true }, |
| 1678 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1679 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1680 | "a.txt": "", |
| 1681 | "b.txt": "", |
| 1682 | "c.txt": "", |
| 1683 | "dir/Android.bp": "", |
| 1684 | "dir/e.txt": "", |
| 1685 | "dir/f.txt": "", |
| 1686 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1687 | ExpectedBazelTargets: []string{ |
| 1688 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1689 | "srcs": `[ |
| 1690 | "a.txt", |
| 1691 | "b.txt", |
| 1692 | "//dir:e.txt", |
| 1693 | "//dir:f.txt", |
| 1694 | ]`, |
| 1695 | }), |
| 1696 | }, |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1697 | }, |
| 1698 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1699 | Description: "filegroup in subdir exclude_srcs", |
| 1700 | ModuleTypeUnderTest: "filegroup", |
| 1701 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1702 | Blueprint: "", |
| 1703 | Dir: "dir", |
| 1704 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1705 | "dir/Android.bp": `filegroup { |
| 1706 | name: "fg_foo", |
| 1707 | srcs: ["**/*.txt"], |
| 1708 | exclude_srcs: ["b.txt"], |
| 1709 | bazel_module: { bp2build_available: true }, |
| 1710 | } |
| 1711 | `, |
| 1712 | "dir/a.txt": "", |
| 1713 | "dir/b.txt": "", |
| 1714 | "dir/subdir/Android.bp": "", |
| 1715 | "dir/subdir/e.txt": "", |
| 1716 | "dir/subdir/f.txt": "", |
| 1717 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1718 | ExpectedBazelTargets: []string{ |
| 1719 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1720 | "srcs": `[ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1721 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1722 | "//dir/subdir:e.txt", |
| 1723 | "//dir/subdir:f.txt", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1724 | ]`, |
| 1725 | }), |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1726 | }, |
| 1727 | }, |
| 1728 | } |
| 1729 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1730 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1731 | t.Run(testCase.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1732 | RunBp2BuildTestCaseSimple(t, testCase) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1733 | }) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1734 | } |
| 1735 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1736 | |
| 1737 | func TestCommonBp2BuildModuleAttrs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1738 | testCases := []Bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1739 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1740 | Description: "Required into data test", |
| 1741 | ModuleTypeUnderTest: "filegroup", |
| 1742 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1743 | Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1744 | filegroup { |
| 1745 | name: "fg_foo", |
| 1746 | required: ["reqd"], |
| 1747 | bazel_module: { bp2build_available: true }, |
| 1748 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1749 | ExpectedBazelTargets: []string{ |
| 1750 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1751 | "data": `[":reqd"]`, |
| 1752 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1753 | }, |
| 1754 | }, |
| 1755 | { |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame] | 1756 | Description: "Required into data test, cyclic self reference is filtered out", |
| 1757 | ModuleTypeUnderTest: "filegroup", |
| 1758 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1759 | Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` |
| 1760 | filegroup { |
| 1761 | name: "fg_foo", |
| 1762 | required: ["reqd", "fg_foo"], |
| 1763 | bazel_module: { bp2build_available: true }, |
| 1764 | }`, |
| 1765 | ExpectedBazelTargets: []string{ |
| 1766 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1767 | "data": `[":reqd"]`, |
| 1768 | }), |
| 1769 | }, |
| 1770 | }, |
| 1771 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1772 | Description: "Required via arch into data test", |
| 1773 | ModuleTypeUnderTest: "python_library", |
| 1774 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1775 | Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") + |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1776 | simpleModuleDoNotConvertBp2build("python_library", "reqdarm") + ` |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1777 | python_library { |
| 1778 | name: "fg_foo", |
| 1779 | arch: { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1780 | arm: { |
| 1781 | required: ["reqdarm"], |
| 1782 | }, |
| 1783 | x86: { |
| 1784 | required: ["reqdx86"], |
| 1785 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1786 | }, |
| 1787 | bazel_module: { bp2build_available: true }, |
| 1788 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1789 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1790 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1791 | "data": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1792 | "//build/bazel/platforms/arch:arm": [":reqdarm"], |
| 1793 | "//build/bazel/platforms/arch:x86": [":reqdx86"], |
| 1794 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1795 | })`, |
| 1796 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1797 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1798 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1799 | }, |
| 1800 | }, |
| 1801 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1802 | Description: "Required appended to data test", |
| 1803 | ModuleTypeUnderTest: "python_library", |
| 1804 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1805 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1806 | "data.bin": "", |
| 1807 | "src.py": "", |
| 1808 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1809 | Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqd") + ` |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1810 | python_library { |
| 1811 | name: "fg_foo", |
| 1812 | data: ["data.bin"], |
| 1813 | required: ["reqd"], |
| 1814 | bazel_module: { bp2build_available: true }, |
| 1815 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1816 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1817 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1818 | "data": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1819 | "data.bin", |
| 1820 | ":reqd", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1821 | ]`, |
| 1822 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1823 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1824 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1825 | }, |
| 1826 | }, |
| 1827 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1828 | Description: "All props-to-attrs at once together test", |
| 1829 | ModuleTypeUnderTest: "filegroup", |
| 1830 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1831 | Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1832 | filegroup { |
| 1833 | name: "fg_foo", |
| 1834 | required: ["reqd"], |
| 1835 | bazel_module: { bp2build_available: true }, |
| 1836 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1837 | ExpectedBazelTargets: []string{ |
| 1838 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1839 | "data": `[":reqd"]`, |
| 1840 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1841 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1842 | }, |
| 1843 | } |
| 1844 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1845 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1846 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1847 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1848 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1849 | } |
| 1850 | } |
Sasha Smundak | 05b0ba6 | 2022-09-26 18:15:45 -0700 | [diff] [blame] | 1851 | |
| 1852 | func TestLicensesAttrConversion(t *testing.T) { |
| 1853 | RunBp2BuildTestCase(t, |
| 1854 | func(ctx android.RegistrationContext) { |
| 1855 | ctx.RegisterModuleType("license", android.LicenseFactory) |
| 1856 | }, |
| 1857 | Bp2buildTestCase{ |
| 1858 | Description: "Test that licenses: attribute is converted", |
| 1859 | ModuleTypeUnderTest: "filegroup", |
| 1860 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1861 | Blueprint: ` |
| 1862 | license { |
| 1863 | name: "my_license", |
| 1864 | } |
| 1865 | filegroup { |
| 1866 | name: "my_filegroup", |
| 1867 | licenses: ["my_license"], |
| 1868 | } |
| 1869 | `, |
| 1870 | ExpectedBazelTargets: []string{ |
| 1871 | MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{ |
| 1872 | "applicable_licenses": `[":my_license"]`, |
| 1873 | }), |
| 1874 | MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}), |
| 1875 | }, |
| 1876 | }) |
| 1877 | } |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 1878 | |
| 1879 | func TestGenerateApiBazelTargets(t *testing.T) { |
| 1880 | bp := ` |
| 1881 | custom { |
| 1882 | name: "foo", |
| 1883 | api: "foo.txt", |
| 1884 | } |
| 1885 | ` |
| 1886 | expectedBazelTarget := MakeBazelTarget( |
| 1887 | "custom_api_contribution", |
| 1888 | "foo", |
| 1889 | AttrNameToString{ |
| 1890 | "api": `"foo.txt"`, |
| 1891 | }, |
| 1892 | ) |
| 1893 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1894 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1895 | } |
| 1896 | RunApiBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1897 | Blueprint: bp, |
| 1898 | ExpectedBazelTargets: []string{expectedBazelTarget}, |
| 1899 | Description: "Generating API contribution Bazel targets for custom module", |
| 1900 | }) |
| 1901 | } |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 1902 | |
| 1903 | func TestGenerateConfigSetting(t *testing.T) { |
| 1904 | bp := ` |
| 1905 | custom { |
| 1906 | name: "foo", |
| 1907 | test_config_setting: true, |
| 1908 | } |
| 1909 | ` |
| 1910 | expectedBazelTargets := []string{ |
| 1911 | MakeBazelTargetNoRestrictions( |
| 1912 | "config_setting", |
| 1913 | "foo_config_setting", |
| 1914 | AttrNameToString{ |
| 1915 | "flag_values": `{ |
| 1916 | "//build/bazel/rules/my_string_setting": "foo", |
| 1917 | }`, |
| 1918 | }, |
| 1919 | ), |
| 1920 | MakeBazelTarget( |
| 1921 | "custom", |
| 1922 | "foo", |
| 1923 | AttrNameToString{}, |
| 1924 | ), |
| 1925 | } |
| 1926 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1927 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1928 | } |
| 1929 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1930 | Blueprint: bp, |
| 1931 | ExpectedBazelTargets: expectedBazelTargets, |
| 1932 | Description: "Generating API contribution Bazel targets for custom module", |
| 1933 | }) |
| 1934 | } |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1935 | |
| 1936 | // If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value |
| 1937 | func TestPrettyPrintSelectMapEqualValues(t *testing.T) { |
| 1938 | lla := bazel.LabelListAttribute{ |
| 1939 | Value: bazel.LabelList{}, |
| 1940 | } |
| 1941 | libFooImplLabel := bazel.Label{ |
| 1942 | Label: ":libfoo.impl", |
| 1943 | } |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1944 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1945 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
| 1946 | actual, _ := prettyPrintAttribute(lla, 0) |
| 1947 | android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual) |
| 1948 | } |