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{ |
Cole Faust | 87c0c33 | 2023-07-31 12:10:12 -0700 | [diff] [blame] | 643 | "target_compatible_with": `select({ |
| 644 | "//build/bazel/product_config/config_settings:unbundled_build": [], |
| 645 | "//conditions:default": ["@platforms//:incompatible"], |
| 646 | })`, |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 647 | }), |
| 648 | }, |
| 649 | }, |
| 650 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 651 | Description: "host and device, neither, cannot override with arch enabled", |
| 652 | ModuleTypeUnderTest: "custom", |
| 653 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 654 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 655 | name: "foo", |
| 656 | host_supported: false, |
| 657 | device_supported: false, |
| 658 | arch: { x86: { enabled: true } }, |
| 659 | bazel_module: { bp2build_available: true }, |
| 660 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 661 | ExpectedBazelTargets: []string{ |
| 662 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 663 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 664 | }), |
| 665 | }, |
| 666 | }, |
| 667 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 668 | Description: "host and device, host only", |
| 669 | ModuleTypeUnderTest: "custom", |
| 670 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
| 671 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 672 | name: "foo", |
| 673 | host_supported: true, |
| 674 | device_supported: false, |
| 675 | bazel_module: { bp2build_available: true }, |
| 676 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 677 | ExpectedBazelTargets: []string{ |
| 678 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 679 | }, |
| 680 | }, |
| 681 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 682 | Description: "host only", |
| 683 | ModuleTypeUnderTest: "custom", |
| 684 | ModuleTypeUnderTestFactory: customModuleFactoryHostSupported, |
| 685 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 686 | name: "foo", |
| 687 | bazel_module: { bp2build_available: true }, |
| 688 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 689 | ExpectedBazelTargets: []string{ |
| 690 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 691 | }, |
| 692 | }, |
| 693 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 694 | Description: "device only", |
| 695 | ModuleTypeUnderTest: "custom", |
| 696 | ModuleTypeUnderTestFactory: customModuleFactoryDeviceSupported, |
| 697 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 698 | name: "foo", |
| 699 | bazel_module: { bp2build_available: true }, |
| 700 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 701 | ExpectedBazelTargets: []string{ |
| 702 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 703 | }, |
| 704 | }, |
| 705 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 706 | Description: "host and device default, default", |
| 707 | ModuleTypeUnderTest: "custom", |
| 708 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 709 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 710 | name: "foo", |
| 711 | bazel_module: { bp2build_available: true }, |
| 712 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 713 | ExpectedBazelTargets: []string{ |
| 714 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{}), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 715 | }, |
| 716 | }, |
| 717 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 718 | Description: "host and device default, device only", |
| 719 | ModuleTypeUnderTest: "custom", |
| 720 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 721 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 722 | name: "foo", |
| 723 | host_supported: false, |
| 724 | bazel_module: { bp2build_available: true }, |
| 725 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 726 | ExpectedBazelTargets: []string{ |
| 727 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.DeviceSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 728 | }, |
| 729 | }, |
| 730 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 731 | Description: "host and device default, host only", |
| 732 | ModuleTypeUnderTest: "custom", |
| 733 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 734 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 735 | name: "foo", |
| 736 | device_supported: false, |
| 737 | bazel_module: { bp2build_available: true }, |
| 738 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 739 | ExpectedBazelTargets: []string{ |
| 740 | makeBazelTargetHostOrDevice("custom", "foo", AttrNameToString{}, android.HostSupported), |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 741 | }, |
| 742 | }, |
| 743 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 744 | Description: "host and device default, neither", |
| 745 | ModuleTypeUnderTest: "custom", |
| 746 | ModuleTypeUnderTestFactory: customModuleFactoryHostAndDeviceDefault, |
| 747 | Blueprint: `custom { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 748 | name: "foo", |
| 749 | host_supported: false, |
| 750 | device_supported: false, |
| 751 | bazel_module: { bp2build_available: true }, |
| 752 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 753 | ExpectedBazelTargets: []string{ |
| 754 | MakeBazelTargetNoRestrictions("custom", "foo", AttrNameToString{ |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 755 | "target_compatible_with": `["@platforms//:incompatible"]`, |
| 756 | }), |
| 757 | }, |
| 758 | }, |
| 759 | } |
| 760 | |
| 761 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 762 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 763 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 764 | }) |
| 765 | } |
| 766 | } |
| 767 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 768 | func TestLoadStatements(t *testing.T) { |
| 769 | testCases := []struct { |
| 770 | bazelTargets BazelTargets |
| 771 | expectedLoadStatements string |
| 772 | }{ |
| 773 | { |
| 774 | bazelTargets: BazelTargets{ |
| 775 | BazelTarget{ |
| 776 | name: "foo", |
| 777 | ruleClass: "cc_library", |
| 778 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 779 | }, |
| 780 | }, |
| 781 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 782 | }, |
| 783 | { |
| 784 | bazelTargets: BazelTargets{ |
| 785 | BazelTarget{ |
| 786 | name: "foo", |
| 787 | ruleClass: "cc_library", |
| 788 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 789 | }, |
| 790 | BazelTarget{ |
| 791 | name: "bar", |
| 792 | ruleClass: "cc_library", |
| 793 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 794 | }, |
| 795 | }, |
| 796 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_library")`, |
| 797 | }, |
| 798 | { |
| 799 | bazelTargets: BazelTargets{ |
| 800 | BazelTarget{ |
| 801 | name: "foo", |
| 802 | ruleClass: "cc_library", |
| 803 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 804 | }, |
| 805 | BazelTarget{ |
| 806 | name: "bar", |
| 807 | ruleClass: "cc_binary", |
| 808 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 809 | }, |
| 810 | }, |
| 811 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library")`, |
| 812 | }, |
| 813 | { |
| 814 | bazelTargets: BazelTargets{ |
| 815 | BazelTarget{ |
| 816 | name: "foo", |
| 817 | ruleClass: "cc_library", |
| 818 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 819 | }, |
| 820 | BazelTarget{ |
| 821 | name: "bar", |
| 822 | ruleClass: "cc_binary", |
| 823 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 824 | }, |
| 825 | BazelTarget{ |
| 826 | name: "baz", |
| 827 | ruleClass: "java_binary", |
| 828 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 829 | }, |
| 830 | }, |
| 831 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary", "cc_library") |
| 832 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 833 | }, |
| 834 | { |
| 835 | bazelTargets: BazelTargets{ |
| 836 | BazelTarget{ |
| 837 | name: "foo", |
| 838 | ruleClass: "cc_binary", |
| 839 | bzlLoadLocation: "//build/bazel/rules:cc.bzl", |
| 840 | }, |
| 841 | BazelTarget{ |
| 842 | name: "bar", |
| 843 | ruleClass: "java_binary", |
| 844 | bzlLoadLocation: "//build/bazel/rules:java.bzl", |
| 845 | }, |
| 846 | BazelTarget{ |
| 847 | name: "baz", |
| 848 | ruleClass: "genrule", |
| 849 | // Note: no bzlLoadLocation for native rules |
| 850 | }, |
| 851 | }, |
| 852 | expectedLoadStatements: `load("//build/bazel/rules:cc.bzl", "cc_binary") |
| 853 | load("//build/bazel/rules:java.bzl", "java_binary")`, |
| 854 | }, |
| 855 | } |
| 856 | |
| 857 | for _, testCase := range testCases { |
| 858 | actual := testCase.bazelTargets.LoadStatements() |
| 859 | expected := testCase.expectedLoadStatements |
| 860 | if actual != expected { |
| 861 | t.Fatalf("Expected load statements to be %s, got %s", expected, actual) |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | } |
| 866 | |
| 867 | func TestGenerateBazelTargetModules_OneToMany_LoadedFromStarlark(t *testing.T) { |
| 868 | testCases := []struct { |
| 869 | bp string |
| 870 | expectedBazelTarget string |
| 871 | expectedBazelTargetCount int |
| 872 | expectedLoadStatements string |
| 873 | }{ |
| 874 | { |
| 875 | bp: `custom { |
| 876 | name: "bar", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 877 | host_supported: true, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 878 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 879 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 880 | }`, |
| 881 | expectedBazelTarget: `my_library( |
| 882 | name = "bar", |
| 883 | ) |
| 884 | |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 885 | proto_library( |
| 886 | name = "bar_proto_library_deps", |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 887 | ) |
| 888 | |
| 889 | my_proto_library( |
| 890 | name = "bar_my_proto_library_deps", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 891 | )`, |
| 892 | expectedBazelTargetCount: 3, |
| 893 | expectedLoadStatements: `load("//build/bazel/rules:proto.bzl", "my_proto_library", "proto_library") |
| 894 | load("//build/bazel/rules:rules.bzl", "my_library")`, |
| 895 | }, |
| 896 | } |
| 897 | |
| 898 | dir := "." |
| 899 | for _, testCase := range testCases { |
| 900 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 901 | ctx := android.NewTestContext(config) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 902 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 903 | ctx.RegisterForBazelConversion() |
| 904 | |
| 905 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 906 | android.FailIfErrored(t, errs) |
| 907 | _, errs = ctx.ResolveDependencies(config) |
| 908 | android.FailIfErrored(t, errs) |
| 909 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 910 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 911 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 912 | android.FailIfErrored(t, err) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 913 | if actualCount := len(bazelTargets); actualCount != testCase.expectedBazelTargetCount { |
| 914 | t.Fatalf("Expected %d bazel target, got %d", testCase.expectedBazelTargetCount, actualCount) |
| 915 | } |
| 916 | |
| 917 | actualBazelTargets := bazelTargets.String() |
| 918 | if actualBazelTargets != testCase.expectedBazelTarget { |
| 919 | t.Errorf( |
| 920 | "Expected generated Bazel target to be '%s', got '%s'", |
| 921 | testCase.expectedBazelTarget, |
| 922 | actualBazelTargets, |
| 923 | ) |
| 924 | } |
| 925 | |
| 926 | actualLoadStatements := bazelTargets.LoadStatements() |
| 927 | if actualLoadStatements != testCase.expectedLoadStatements { |
| 928 | t.Errorf( |
| 929 | "Expected generated load statements to be '%s', got '%s'", |
| 930 | testCase.expectedLoadStatements, |
| 931 | actualLoadStatements, |
| 932 | ) |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 937 | func TestModuleTypeBp2Build(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 938 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 939 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 940 | Description: "filegroup with does not specify srcs", |
| 941 | ModuleTypeUnderTest: "filegroup", |
| 942 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 943 | Blueprint: `filegroup { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 944 | name: "fg_foo", |
| 945 | bazel_module: { bp2build_available: true }, |
| 946 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 947 | ExpectedBazelTargets: []string{ |
| 948 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 949 | }, |
| 950 | }, |
| 951 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 952 | Description: "filegroup with no srcs", |
| 953 | ModuleTypeUnderTest: "filegroup", |
| 954 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 955 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 956 | name: "fg_foo", |
| 957 | srcs: [], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 958 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 959 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 960 | ExpectedBazelTargets: []string{ |
| 961 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 962 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 963 | }, |
| 964 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 965 | Description: "filegroup with srcs", |
| 966 | ModuleTypeUnderTest: "filegroup", |
| 967 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 968 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 969 | name: "fg_foo", |
| 970 | srcs: ["a", "b"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 971 | bazel_module: { bp2build_available: true }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 972 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 973 | ExpectedBazelTargets: []string{ |
| 974 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 975 | "srcs": `[ |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 976 | "a", |
| 977 | "b", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 978 | ]`, |
| 979 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 980 | }, |
| 981 | }, |
| 982 | { |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 983 | Description: "filegroup with dot-slash-prefixed srcs", |
| 984 | ModuleTypeUnderTest: "filegroup", |
| 985 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 986 | Blueprint: `filegroup { |
| 987 | name: "fg_foo", |
| 988 | srcs: ["./a", "./b"], |
| 989 | bazel_module: { bp2build_available: true }, |
| 990 | }`, |
| 991 | ExpectedBazelTargets: []string{ |
| 992 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 993 | "srcs": `[ |
| 994 | "a", |
| 995 | "b", |
| 996 | ]`, |
| 997 | }), |
| 998 | }, |
| 999 | }, |
| 1000 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1001 | Description: "filegroup with excludes srcs", |
| 1002 | ModuleTypeUnderTest: "filegroup", |
| 1003 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1004 | Blueprint: `filegroup { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1005 | name: "fg_foo", |
| 1006 | srcs: ["a", "b"], |
| 1007 | exclude_srcs: ["a"], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1008 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1009 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1010 | ExpectedBazelTargets: []string{ |
| 1011 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1012 | "srcs": `["b"]`, |
| 1013 | }), |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1014 | }, |
| 1015 | }, |
| 1016 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1017 | Description: "depends_on_other_dir_module", |
| 1018 | ModuleTypeUnderTest: "filegroup", |
| 1019 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1020 | Blueprint: `filegroup { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1021 | name: "fg_foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1022 | srcs: [ |
Jingwen Chen | b4628eb | 2021-04-08 14:40:57 +0000 | [diff] [blame] | 1023 | ":foo", |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1024 | "c", |
| 1025 | ], |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1026 | bazel_module: { bp2build_available: true }, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1027 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1028 | Filesystem: map[string]string{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 1029 | "other/Android.bp": `filegroup { |
| 1030 | name: "foo", |
| 1031 | srcs: ["a", "b"], |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1032 | bazel_module: { bp2build_available: true }, |
| 1033 | }`, |
| 1034 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1035 | ExpectedBazelTargets: []string{ |
| 1036 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1037 | "srcs": `[ |
| 1038 | "//other:foo", |
| 1039 | "c", |
| 1040 | ]`, |
| 1041 | }), |
| 1042 | }, |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1043 | }, |
| 1044 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1045 | Description: "depends_on_other_unconverted_module_error", |
| 1046 | ModuleTypeUnderTest: "filegroup", |
| 1047 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1048 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1049 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1050 | name: "foobar", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1051 | srcs: [ |
| 1052 | ":foo", |
| 1053 | "c", |
| 1054 | ], |
| 1055 | bazel_module: { bp2build_available: true }, |
| 1056 | }`, |
Sasha Smundak | f2bb26f | 2022-08-04 11:28:15 -0700 | [diff] [blame] | 1057 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on unconverted modules: foo`), |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1058 | Filesystem: map[string]string{ |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1059 | "other/Android.bp": `filegroup { |
| 1060 | name: "foo", |
| 1061 | srcs: ["a", "b"], |
| 1062 | }`, |
| 1063 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1064 | }, |
Usta Shrestha | 808bc71 | 2022-09-23 23:18:18 -0400 | [diff] [blame] | 1065 | { |
| 1066 | Description: "depends_on_other_missing_module_error", |
| 1067 | ModuleTypeUnderTest: "filegroup", |
| 1068 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1069 | UnconvertedDepsMode: errorModulesUnconvertedDeps, |
| 1070 | Blueprint: `filegroup { |
| 1071 | name: "foobar", |
| 1072 | srcs: [ |
| 1073 | "c", |
| 1074 | "//other:foo", |
| 1075 | "//other:goo", |
| 1076 | ], |
| 1077 | bazel_module: { bp2build_available: true }, |
| 1078 | }`, |
| 1079 | ExpectedErr: fmt.Errorf(`filegroup .:foobar depends on missing modules: //other:goo`), |
| 1080 | Filesystem: map[string]string{"other/Android.bp": `filegroup { |
| 1081 | name: "foo", |
| 1082 | srcs: ["a"], |
| 1083 | bazel_module: { bp2build_available: true }, |
| 1084 | } |
| 1085 | `, |
| 1086 | }, |
| 1087 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1088 | } |
| 1089 | |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1090 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1091 | t.Run(testCase.Description, func(t *testing.T) { |
| 1092 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, testCase) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1093 | }) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 1094 | } |
| 1095 | } |
Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 1096 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1097 | func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1098 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1099 | moduleTypeUnderTest string |
| 1100 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1101 | bp string |
| 1102 | expectedCount int |
| 1103 | description string |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1104 | }{ |
| 1105 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1106 | description: "explicitly unavailable", |
| 1107 | moduleTypeUnderTest: "filegroup", |
| 1108 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1109 | bp: `filegroup { |
| 1110 | name: "foo", |
| 1111 | srcs: ["a", "b"], |
| 1112 | bazel_module: { bp2build_available: false }, |
| 1113 | }`, |
| 1114 | expectedCount: 0, |
| 1115 | }, |
| 1116 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1117 | description: "implicitly unavailable", |
| 1118 | moduleTypeUnderTest: "filegroup", |
| 1119 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1120 | bp: `filegroup { |
| 1121 | name: "foo", |
| 1122 | srcs: ["a", "b"], |
| 1123 | }`, |
| 1124 | expectedCount: 0, |
| 1125 | }, |
| 1126 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1127 | description: "explicitly available", |
| 1128 | moduleTypeUnderTest: "filegroup", |
| 1129 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1130 | bp: `filegroup { |
| 1131 | name: "foo", |
| 1132 | srcs: ["a", "b"], |
| 1133 | bazel_module: { bp2build_available: true }, |
| 1134 | }`, |
| 1135 | expectedCount: 1, |
| 1136 | }, |
| 1137 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1138 | description: "generates more than 1 target if needed", |
| 1139 | moduleTypeUnderTest: "custom", |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 1140 | moduleTypeUnderTestFactory: customModuleFactoryHostAndDevice, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1141 | bp: `custom { |
| 1142 | name: "foo", |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1143 | one_to_many_prop: true, |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1144 | bazel_module: { bp2build_available: true }, |
| 1145 | }`, |
| 1146 | expectedCount: 3, |
| 1147 | }, |
| 1148 | } |
| 1149 | |
| 1150 | dir := "." |
| 1151 | for _, testCase := range testCases { |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1152 | t.Run(testCase.description, func(t *testing.T) { |
| 1153 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 1154 | ctx := android.NewTestContext(config) |
| 1155 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1156 | ctx.RegisterForBazelConversion() |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1157 | |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1158 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 1159 | android.FailIfErrored(t, errs) |
| 1160 | _, errs = ctx.ResolveDependencies(config) |
| 1161 | android.FailIfErrored(t, errs) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1162 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1163 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1164 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
| 1165 | android.FailIfErrored(t, err) |
Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 1166 | if actualCount := len(bazelTargets); actualCount != testCase.expectedCount { |
| 1167 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, testCase.expectedCount, actualCount) |
| 1168 | } |
| 1169 | }) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 1170 | } |
| 1171 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1172 | |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1173 | func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) { |
| 1174 | testCases := []struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1175 | moduleTypeUnderTest string |
| 1176 | moduleTypeUnderTestFactory android.ModuleFactory |
| 1177 | expectedCount map[string]int |
| 1178 | description string |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1179 | bp2buildConfig allowlists.Bp2BuildConfig |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1180 | checkDir string |
| 1181 | fs map[string]string |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1182 | forceEnabledModules []string |
| 1183 | expectedErrorMessages []string |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1184 | }{ |
| 1185 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1186 | description: "test bp2build config package and subpackages config", |
| 1187 | moduleTypeUnderTest: "filegroup", |
| 1188 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1189 | expectedCount: map[string]int{ |
| 1190 | "migrated": 1, |
| 1191 | "migrated/but_not_really": 0, |
| 1192 | "migrated/but_not_really/but_really": 1, |
| 1193 | "not_migrated": 0, |
| 1194 | "also_not_migrated": 0, |
| 1195 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1196 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1197 | "migrated": allowlists.Bp2BuildDefaultTrueRecursively, |
| 1198 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1199 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1200 | }, |
| 1201 | fs: map[string]string{ |
| 1202 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1203 | "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`, |
| 1204 | "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`, |
| 1205 | "not_migrated/Android.bp": `filegroup { name: "d" }`, |
| 1206 | "also_not_migrated/Android.bp": `filegroup { name: "e" }`, |
| 1207 | }, |
| 1208 | }, |
| 1209 | { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1210 | description: "test bp2build config opt-in and opt-out", |
| 1211 | moduleTypeUnderTest: "filegroup", |
| 1212 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1213 | expectedCount: map[string]int{ |
| 1214 | "package-opt-in": 2, |
| 1215 | "package-opt-in/subpackage": 0, |
| 1216 | "package-opt-out": 1, |
| 1217 | "package-opt-out/subpackage": 0, |
| 1218 | }, |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1219 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1220 | "package-opt-in": allowlists.Bp2BuildDefaultFalse, |
| 1221 | "package-opt-out": allowlists.Bp2BuildDefaultTrueRecursively, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1222 | }, |
| 1223 | fs: map[string]string{ |
| 1224 | "package-opt-in/Android.bp": ` |
| 1225 | filegroup { name: "opt-in-a" } |
| 1226 | filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } } |
| 1227 | filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } } |
| 1228 | `, |
| 1229 | |
| 1230 | "package-opt-in/subpackage/Android.bp": ` |
| 1231 | filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively |
| 1232 | `, |
| 1233 | |
| 1234 | "package-opt-out/Android.bp": ` |
| 1235 | filegroup { name: "opt-out-a" } |
| 1236 | filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } } |
| 1237 | filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } } |
| 1238 | `, |
| 1239 | |
| 1240 | "package-opt-out/subpackage/Android.bp": ` |
| 1241 | filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } } |
| 1242 | filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } } |
| 1243 | `, |
| 1244 | }, |
| 1245 | }, |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1246 | { |
| 1247 | description: "test force-enabled errors out", |
| 1248 | moduleTypeUnderTest: "filegroup", |
| 1249 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 1250 | expectedCount: map[string]int{ |
| 1251 | "migrated": 0, |
| 1252 | "not_migrated": 0, |
| 1253 | }, |
| 1254 | bp2buildConfig: allowlists.Bp2BuildConfig{ |
| 1255 | "migrated/but_not_really": allowlists.Bp2BuildDefaultFalse, |
| 1256 | "not_migrated": allowlists.Bp2BuildDefaultFalse, |
| 1257 | }, |
| 1258 | fs: map[string]string{ |
| 1259 | "migrated/Android.bp": `filegroup { name: "a" }`, |
| 1260 | }, |
| 1261 | forceEnabledModules: []string{"a"}, |
| 1262 | expectedErrorMessages: []string{"Force Enabled Module a not converted"}, |
| 1263 | }, |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1264 | } |
| 1265 | |
| 1266 | dir := "." |
| 1267 | for _, testCase := range testCases { |
| 1268 | fs := make(map[string][]byte) |
| 1269 | toParse := []string{ |
| 1270 | "Android.bp", |
| 1271 | } |
| 1272 | for f, content := range testCase.fs { |
| 1273 | if strings.HasSuffix(f, "Android.bp") { |
| 1274 | toParse = append(toParse, f) |
| 1275 | } |
| 1276 | fs[f] = []byte(content) |
| 1277 | } |
| 1278 | config := android.TestConfig(buildDir, nil, "", fs) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1279 | config.AddForceEnabledModules(testCase.forceEnabledModules) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1280 | ctx := android.NewTestContext(config) |
| 1281 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 1282 | allowlist := android.NewBp2BuildAllowlist().SetDefaultConfig(testCase.bp2buildConfig) |
| 1283 | ctx.RegisterBp2BuildConfig(allowlist) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1284 | ctx.RegisterForBazelConversion() |
| 1285 | |
| 1286 | _, errs := ctx.ParseFileList(dir, toParse) |
| 1287 | android.FailIfErrored(t, errs) |
| 1288 | _, errs = ctx.ResolveDependencies(config) |
| 1289 | android.FailIfErrored(t, errs) |
| 1290 | |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1291 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1292 | |
| 1293 | // For each directory, test that the expected number of generated targets is correct. |
| 1294 | for dir, expectedCount := range testCase.expectedCount { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1295 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, dir) |
MarkDacek | 9c094ca | 2023-03-16 19:15:19 +0000 | [diff] [blame] | 1296 | android.CheckErrorsAgainstExpectations(t, err, testCase.expectedErrorMessages) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 1297 | if actualCount := len(bazelTargets); actualCount != expectedCount { |
| 1298 | t.Fatalf( |
| 1299 | "%s: Expected %d bazel target for %s package, got %d", |
| 1300 | testCase.description, |
| 1301 | expectedCount, |
| 1302 | dir, |
| 1303 | actualCount) |
| 1304 | } |
| 1305 | |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1310 | func TestCombineBuildFilesBp2buildTargets(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1311 | testCases := []Bp2buildTestCase{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1312 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1313 | Description: "filegroup bazel_module.label", |
| 1314 | ModuleTypeUnderTest: "filegroup", |
| 1315 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1316 | Blueprint: `filegroup { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1317 | name: "fg_foo", |
| 1318 | bazel_module: { label: "//other:fg_foo" }, |
| 1319 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1320 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1321 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1322 | "other/BUILD.bazel": `// BUILD file`, |
| 1323 | }, |
| 1324 | }, |
| 1325 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1326 | Description: "multiple bazel_module.label same BUILD", |
| 1327 | ModuleTypeUnderTest: "filegroup", |
| 1328 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1329 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1330 | name: "fg_foo", |
| 1331 | bazel_module: { label: "//other:fg_foo" }, |
| 1332 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1333 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1334 | filegroup { |
| 1335 | name: "foo", |
| 1336 | bazel_module: { label: "//other:foo" }, |
| 1337 | }`, |
Cole Faust | ea602c5 | 2022-08-31 14:48:26 -0700 | [diff] [blame] | 1338 | ExpectedBazelTargets: []string{}, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1339 | Filesystem: map[string]string{ |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1340 | "other/BUILD.bazel": `// BUILD file`, |
| 1341 | }, |
| 1342 | }, |
| 1343 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1344 | Description: "filegroup bazel_module.label and bp2build in subdir", |
| 1345 | ModuleTypeUnderTest: "filegroup", |
| 1346 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1347 | Dir: "other", |
| 1348 | Blueprint: ``, |
| 1349 | Filesystem: map[string]string{ |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1350 | "other/Android.bp": `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1351 | name: "fg_foo", |
| 1352 | bazel_module: { |
| 1353 | bp2build_available: true, |
| 1354 | }, |
| 1355 | } |
| 1356 | filegroup { |
| 1357 | name: "fg_bar", |
| 1358 | bazel_module: { |
| 1359 | label: "//other:fg_bar" |
| 1360 | }, |
| 1361 | }`, |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 1362 | "other/BUILD.bazel": `// definition for fg_bar`, |
| 1363 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1364 | ExpectedBazelTargets: []string{ |
| 1365 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1366 | }, |
| 1367 | }, |
| 1368 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1369 | Description: "filegroup bazel_module.label and filegroup bp2build", |
| 1370 | ModuleTypeUnderTest: "filegroup", |
| 1371 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1372 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1373 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1374 | "other/BUILD.bazel": `// BUILD file`, |
| 1375 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1376 | Blueprint: `filegroup { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1377 | name: "fg_foo", |
| 1378 | bazel_module: { |
| 1379 | label: "//other:fg_foo", |
| 1380 | }, |
| 1381 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1382 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1383 | filegroup { |
| 1384 | name: "fg_bar", |
| 1385 | bazel_module: { |
| 1386 | bp2build_available: true, |
| 1387 | }, |
| 1388 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1389 | ExpectedBazelTargets: []string{ |
| 1390 | MakeBazelTargetNoRestrictions("filegroup", "fg_bar", map[string]string{}), |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1391 | }, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1392 | }, |
| 1393 | } |
| 1394 | |
| 1395 | dir := "." |
| 1396 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1397 | t.Run(testCase.Description, func(t *testing.T) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1398 | fs := make(map[string][]byte) |
| 1399 | toParse := []string{ |
| 1400 | "Android.bp", |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1401 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1402 | for f, content := range testCase.Filesystem { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1403 | if strings.HasSuffix(f, "Android.bp") { |
| 1404 | toParse = append(toParse, f) |
| 1405 | } |
| 1406 | fs[f] = []byte(content) |
| 1407 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1408 | config := android.TestConfig(buildDir, nil, testCase.Blueprint, fs) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1409 | ctx := android.NewTestContext(config) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1410 | ctx.RegisterModuleType(testCase.ModuleTypeUnderTest, testCase.ModuleTypeUnderTestFactory) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1411 | ctx.RegisterForBazelConversion() |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1412 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1413 | _, errs := ctx.ParseFileList(dir, toParse) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1414 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1415 | return |
| 1416 | } |
| 1417 | _, errs = ctx.ResolveDependencies(config) |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 1418 | if errored(t, testCase, errs) { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1419 | return |
| 1420 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1421 | |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1422 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1423 | if testCase.Dir != "" { |
| 1424 | checkDir = testCase.Dir |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1425 | } |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 1426 | codegenCtx := NewCodegenContext(config, ctx.Context, Bp2Build, "") |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 1427 | bazelTargets, err := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 1428 | android.FailIfErrored(t, err) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1429 | bazelTargets.sort() |
| 1430 | actualCount := len(bazelTargets) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1431 | expectedCount := len(testCase.ExpectedBazelTargets) |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1432 | if actualCount != expectedCount { |
| 1433 | t.Errorf("Expected %d bazel target, got %d\n%s", expectedCount, actualCount, bazelTargets) |
| 1434 | } |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1435 | for i, target := range bazelTargets { |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1436 | actualContent := target.content |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1437 | expectedContent := testCase.ExpectedBazelTargets[i] |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1438 | if expectedContent != actualContent { |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1439 | t.Errorf( |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1440 | "Expected generated Bazel target to be '%s', got '%s'", |
| 1441 | expectedContent, |
| 1442 | actualContent, |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1443 | ) |
| 1444 | } |
| 1445 | } |
Jingwen Chen | 4910976 | 2021-05-25 05:16:48 +0000 | [diff] [blame] | 1446 | }) |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 1447 | } |
| 1448 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1449 | |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 1450 | func TestGlob(t *testing.T) { |
| 1451 | testCases := []Bp2buildTestCase{ |
| 1452 | { |
| 1453 | Description: "filegroup with glob", |
| 1454 | ModuleTypeUnderTest: "filegroup", |
| 1455 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1456 | Blueprint: `filegroup { |
| 1457 | name: "fg_foo", |
| 1458 | srcs: ["**/*.txt"], |
| 1459 | bazel_module: { bp2build_available: true }, |
| 1460 | }`, |
| 1461 | ExpectedBazelTargets: []string{ |
| 1462 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1463 | "srcs": `[ |
| 1464 | "other/a.txt", |
| 1465 | "other/b.txt", |
| 1466 | "other/subdir/a.txt", |
| 1467 | ]`, |
| 1468 | }), |
| 1469 | }, |
| 1470 | Filesystem: map[string]string{ |
| 1471 | "other/a.txt": "", |
| 1472 | "other/b.txt": "", |
| 1473 | "other/subdir/a.txt": "", |
| 1474 | "other/file": "", |
| 1475 | }, |
| 1476 | }, |
| 1477 | { |
| 1478 | Description: "filegroup with glob in subdir", |
| 1479 | ModuleTypeUnderTest: "filegroup", |
| 1480 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1481 | Dir: "other", |
| 1482 | Filesystem: map[string]string{ |
| 1483 | "other/Android.bp": `filegroup { |
| 1484 | name: "fg_foo", |
| 1485 | srcs: ["**/*.txt"], |
| 1486 | bazel_module: { bp2build_available: true }, |
| 1487 | }`, |
| 1488 | "other/a.txt": "", |
| 1489 | "other/b.txt": "", |
| 1490 | "other/subdir/a.txt": "", |
| 1491 | "other/file": "", |
| 1492 | }, |
| 1493 | ExpectedBazelTargets: []string{ |
| 1494 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1495 | "srcs": `[ |
| 1496 | "a.txt", |
| 1497 | "b.txt", |
| 1498 | "subdir/a.txt", |
| 1499 | ]`, |
| 1500 | }), |
| 1501 | }, |
| 1502 | }, |
| 1503 | { |
| 1504 | Description: "filegroup with glob with no kept BUILD files", |
| 1505 | ModuleTypeUnderTest: "filegroup", |
| 1506 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1507 | KeepBuildFileForDirs: []string{ |
| 1508 | // empty |
| 1509 | }, |
| 1510 | Blueprint: `filegroup { |
| 1511 | name: "fg_foo", |
| 1512 | srcs: ["**/*.txt"], |
| 1513 | bazel_module: { bp2build_available: true }, |
| 1514 | }`, |
| 1515 | Filesystem: map[string]string{ |
| 1516 | "a.txt": "", |
| 1517 | "b.txt": "", |
| 1518 | "foo/BUILD": "", |
| 1519 | "foo/a.txt": "", |
| 1520 | "foo/bar/BUILD": "", |
| 1521 | "foo/bar/b.txt": "", |
| 1522 | }, |
| 1523 | ExpectedBazelTargets: []string{ |
| 1524 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1525 | "srcs": `[ |
| 1526 | "a.txt", |
| 1527 | "b.txt", |
| 1528 | "foo/a.txt", |
| 1529 | "foo/bar/b.txt", |
| 1530 | ]`, |
| 1531 | }), |
| 1532 | }, |
| 1533 | }, |
| 1534 | { |
| 1535 | Description: "filegroup with glob with kept BUILD file", |
| 1536 | ModuleTypeUnderTest: "filegroup", |
| 1537 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1538 | KeepBuildFileForDirs: []string{ |
| 1539 | "foo", |
| 1540 | }, |
| 1541 | Blueprint: `filegroup { |
| 1542 | name: "fg_foo", |
| 1543 | srcs: ["**/*.txt"], |
| 1544 | bazel_module: { bp2build_available: true }, |
| 1545 | }`, |
| 1546 | Filesystem: map[string]string{ |
| 1547 | "a.txt": "", |
| 1548 | "b.txt": "", |
| 1549 | "foo/BUILD": "", |
| 1550 | "foo/a.txt": "", |
| 1551 | "foo/bar/BUILD": "", |
| 1552 | "foo/bar/b.txt": "", |
| 1553 | }, |
| 1554 | ExpectedBazelTargets: []string{ |
| 1555 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1556 | "srcs": `[ |
| 1557 | "a.txt", |
| 1558 | "b.txt", |
| 1559 | "//foo:a.txt", |
| 1560 | "//foo:bar/b.txt", |
| 1561 | ]`, |
| 1562 | }), |
| 1563 | }, |
| 1564 | }, |
| 1565 | { |
| 1566 | Description: "filegroup with glob with kept BUILD.bazel file", |
| 1567 | ModuleTypeUnderTest: "filegroup", |
| 1568 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1569 | KeepBuildFileForDirs: []string{ |
| 1570 | "foo", |
| 1571 | }, |
| 1572 | Blueprint: `filegroup { |
| 1573 | name: "fg_foo", |
| 1574 | srcs: ["**/*.txt"], |
| 1575 | bazel_module: { bp2build_available: true }, |
| 1576 | }`, |
| 1577 | Filesystem: map[string]string{ |
| 1578 | "a.txt": "", |
| 1579 | "b.txt": "", |
| 1580 | "foo/BUILD.bazel": "", |
| 1581 | "foo/a.txt": "", |
| 1582 | "foo/bar/BUILD.bazel": "", |
| 1583 | "foo/bar/b.txt": "", |
| 1584 | }, |
| 1585 | ExpectedBazelTargets: []string{ |
| 1586 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1587 | "srcs": `[ |
| 1588 | "a.txt", |
| 1589 | "b.txt", |
| 1590 | "//foo:a.txt", |
| 1591 | "//foo:bar/b.txt", |
| 1592 | ]`, |
| 1593 | }), |
| 1594 | }, |
| 1595 | }, |
| 1596 | { |
| 1597 | Description: "filegroup with glob with Android.bp file as boundary", |
| 1598 | ModuleTypeUnderTest: "filegroup", |
| 1599 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1600 | Blueprint: `filegroup { |
| 1601 | name: "fg_foo", |
| 1602 | srcs: ["**/*.txt"], |
| 1603 | bazel_module: { bp2build_available: true }, |
| 1604 | }`, |
| 1605 | Filesystem: map[string]string{ |
| 1606 | "a.txt": "", |
| 1607 | "b.txt": "", |
| 1608 | "foo/Android.bp": "", |
| 1609 | "foo/a.txt": "", |
| 1610 | "foo/bar/Android.bp": "", |
| 1611 | "foo/bar/b.txt": "", |
| 1612 | }, |
| 1613 | ExpectedBazelTargets: []string{ |
| 1614 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1615 | "srcs": `[ |
| 1616 | "a.txt", |
| 1617 | "b.txt", |
| 1618 | "//foo:a.txt", |
| 1619 | "//foo/bar:b.txt", |
| 1620 | ]`, |
| 1621 | }), |
| 1622 | }, |
| 1623 | }, |
| 1624 | { |
| 1625 | Description: "filegroup with glob in subdir with kept BUILD and BUILD.bazel file", |
| 1626 | ModuleTypeUnderTest: "filegroup", |
| 1627 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1628 | Dir: "other", |
| 1629 | KeepBuildFileForDirs: []string{ |
| 1630 | "other/foo", |
| 1631 | "other/foo/bar", |
| 1632 | // deliberately not other/foo/baz/BUILD. |
| 1633 | }, |
| 1634 | Filesystem: map[string]string{ |
| 1635 | "other/Android.bp": `filegroup { |
| 1636 | name: "fg_foo", |
| 1637 | srcs: ["**/*.txt"], |
| 1638 | bazel_module: { bp2build_available: true }, |
| 1639 | }`, |
| 1640 | "other/a.txt": "", |
| 1641 | "other/b.txt": "", |
| 1642 | "other/foo/BUILD": "", |
| 1643 | "other/foo/a.txt": "", |
| 1644 | "other/foo/bar/BUILD.bazel": "", |
| 1645 | "other/foo/bar/b.txt": "", |
| 1646 | "other/foo/baz/BUILD": "", |
| 1647 | "other/foo/baz/c.txt": "", |
| 1648 | }, |
| 1649 | ExpectedBazelTargets: []string{ |
| 1650 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1651 | "srcs": `[ |
| 1652 | "a.txt", |
| 1653 | "b.txt", |
| 1654 | "//other/foo:a.txt", |
| 1655 | "//other/foo/bar:b.txt", |
| 1656 | "//other/foo:baz/c.txt", |
| 1657 | ]`, |
| 1658 | }), |
| 1659 | }, |
| 1660 | }, |
| 1661 | } |
| 1662 | |
| 1663 | for _, testCase := range testCases { |
| 1664 | t.Run(testCase.Description, func(t *testing.T) { |
| 1665 | RunBp2BuildTestCaseSimple(t, testCase) |
| 1666 | }) |
| 1667 | } |
| 1668 | } |
| 1669 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1670 | func TestGlobExcludeSrcs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1671 | testCases := []Bp2buildTestCase{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1672 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1673 | Description: "filegroup top level exclude_srcs", |
| 1674 | ModuleTypeUnderTest: "filegroup", |
| 1675 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1676 | Blueprint: `filegroup { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1677 | name: "fg_foo", |
| 1678 | srcs: ["**/*.txt"], |
| 1679 | exclude_srcs: ["c.txt"], |
| 1680 | bazel_module: { bp2build_available: true }, |
| 1681 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1682 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1683 | "a.txt": "", |
| 1684 | "b.txt": "", |
| 1685 | "c.txt": "", |
| 1686 | "dir/Android.bp": "", |
| 1687 | "dir/e.txt": "", |
| 1688 | "dir/f.txt": "", |
| 1689 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1690 | ExpectedBazelTargets: []string{ |
| 1691 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1692 | "srcs": `[ |
| 1693 | "a.txt", |
| 1694 | "b.txt", |
| 1695 | "//dir:e.txt", |
| 1696 | "//dir:f.txt", |
| 1697 | ]`, |
| 1698 | }), |
| 1699 | }, |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1700 | }, |
| 1701 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1702 | Description: "filegroup in subdir exclude_srcs", |
| 1703 | ModuleTypeUnderTest: "filegroup", |
| 1704 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1705 | Blueprint: "", |
| 1706 | Dir: "dir", |
| 1707 | Filesystem: map[string]string{ |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1708 | "dir/Android.bp": `filegroup { |
| 1709 | name: "fg_foo", |
| 1710 | srcs: ["**/*.txt"], |
| 1711 | exclude_srcs: ["b.txt"], |
| 1712 | bazel_module: { bp2build_available: true }, |
| 1713 | } |
| 1714 | `, |
| 1715 | "dir/a.txt": "", |
| 1716 | "dir/b.txt": "", |
| 1717 | "dir/subdir/Android.bp": "", |
| 1718 | "dir/subdir/e.txt": "", |
| 1719 | "dir/subdir/f.txt": "", |
| 1720 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1721 | ExpectedBazelTargets: []string{ |
| 1722 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1723 | "srcs": `[ |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 1724 | "a.txt", |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1725 | "//dir/subdir:e.txt", |
| 1726 | "//dir/subdir:f.txt", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1727 | ]`, |
| 1728 | }), |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1729 | }, |
| 1730 | }, |
| 1731 | } |
| 1732 | |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1733 | for _, testCase := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1734 | t.Run(testCase.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1735 | RunBp2BuildTestCaseSimple(t, testCase) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1736 | }) |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1739 | |
| 1740 | func TestCommonBp2BuildModuleAttrs(t *testing.T) { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1741 | testCases := []Bp2buildTestCase{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1742 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1743 | Description: "Required into data test", |
| 1744 | ModuleTypeUnderTest: "filegroup", |
| 1745 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1746 | 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] | 1747 | filegroup { |
| 1748 | name: "fg_foo", |
| 1749 | required: ["reqd"], |
| 1750 | bazel_module: { bp2build_available: true }, |
| 1751 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1752 | ExpectedBazelTargets: []string{ |
| 1753 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1754 | "data": `[":reqd"]`, |
| 1755 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1756 | }, |
| 1757 | }, |
| 1758 | { |
Jingwen Chen | a5ecb37 | 2022-09-21 09:05:37 +0000 | [diff] [blame] | 1759 | Description: "Required into data test, cyclic self reference is filtered out", |
| 1760 | ModuleTypeUnderTest: "filegroup", |
| 1761 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1762 | Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "reqd") + ` |
| 1763 | filegroup { |
| 1764 | name: "fg_foo", |
| 1765 | required: ["reqd", "fg_foo"], |
| 1766 | bazel_module: { bp2build_available: true }, |
| 1767 | }`, |
| 1768 | ExpectedBazelTargets: []string{ |
| 1769 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
| 1770 | "data": `[":reqd"]`, |
| 1771 | }), |
| 1772 | }, |
| 1773 | }, |
| 1774 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1775 | Description: "Required via arch into data test", |
| 1776 | ModuleTypeUnderTest: "python_library", |
| 1777 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1778 | Blueprint: simpleModuleDoNotConvertBp2build("python_library", "reqdx86") + |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1779 | 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] | 1780 | python_library { |
| 1781 | name: "fg_foo", |
| 1782 | arch: { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 1783 | arm: { |
| 1784 | required: ["reqdarm"], |
| 1785 | }, |
| 1786 | x86: { |
| 1787 | required: ["reqdx86"], |
| 1788 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1789 | }, |
| 1790 | bazel_module: { bp2build_available: true }, |
| 1791 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1792 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1793 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1794 | "data": `select({ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1795 | "//build/bazel/platforms/arch:arm": [":reqdarm"], |
| 1796 | "//build/bazel/platforms/arch:x86": [":reqdx86"], |
| 1797 | "//conditions:default": [], |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1798 | })`, |
| 1799 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1800 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1801 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1802 | }, |
| 1803 | }, |
| 1804 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1805 | Description: "Required appended to data test", |
| 1806 | ModuleTypeUnderTest: "python_library", |
| 1807 | ModuleTypeUnderTestFactory: python.PythonLibraryFactory, |
| 1808 | Filesystem: map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1809 | "data.bin": "", |
| 1810 | "src.py": "", |
| 1811 | }, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1812 | 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] | 1813 | python_library { |
| 1814 | name: "fg_foo", |
| 1815 | data: ["data.bin"], |
| 1816 | required: ["reqd"], |
| 1817 | bazel_module: { bp2build_available: true }, |
| 1818 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1819 | ExpectedBazelTargets: []string{ |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 1820 | MakeBazelTarget("py_library", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1821 | "data": `[ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1822 | "data.bin", |
| 1823 | ":reqd", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1824 | ]`, |
| 1825 | "srcs_version": `"PY3"`, |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 1826 | "imports": `["."]`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1827 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1828 | }, |
| 1829 | }, |
| 1830 | { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1831 | Description: "All props-to-attrs at once together test", |
| 1832 | ModuleTypeUnderTest: "filegroup", |
| 1833 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1834 | 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] | 1835 | filegroup { |
| 1836 | name: "fg_foo", |
| 1837 | required: ["reqd"], |
| 1838 | bazel_module: { bp2build_available: true }, |
| 1839 | }`, |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1840 | ExpectedBazelTargets: []string{ |
| 1841 | MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1842 | "data": `[":reqd"]`, |
| 1843 | }), |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1844 | }, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1845 | }, |
| 1846 | } |
| 1847 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1848 | for _, tc := range testCases { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 1849 | t.Run(tc.Description, func(t *testing.T) { |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 1850 | RunBp2BuildTestCaseSimple(t, tc) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 1851 | }) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 1852 | } |
| 1853 | } |
Sasha Smundak | 05b0ba6 | 2022-09-26 18:15:45 -0700 | [diff] [blame] | 1854 | |
| 1855 | func TestLicensesAttrConversion(t *testing.T) { |
| 1856 | RunBp2BuildTestCase(t, |
| 1857 | func(ctx android.RegistrationContext) { |
| 1858 | ctx.RegisterModuleType("license", android.LicenseFactory) |
| 1859 | }, |
| 1860 | Bp2buildTestCase{ |
| 1861 | Description: "Test that licenses: attribute is converted", |
| 1862 | ModuleTypeUnderTest: "filegroup", |
| 1863 | ModuleTypeUnderTestFactory: android.FileGroupFactory, |
| 1864 | Blueprint: ` |
| 1865 | license { |
| 1866 | name: "my_license", |
| 1867 | } |
| 1868 | filegroup { |
| 1869 | name: "my_filegroup", |
| 1870 | licenses: ["my_license"], |
| 1871 | } |
| 1872 | `, |
| 1873 | ExpectedBazelTargets: []string{ |
| 1874 | MakeBazelTargetNoRestrictions("filegroup", "my_filegroup", AttrNameToString{ |
| 1875 | "applicable_licenses": `[":my_license"]`, |
| 1876 | }), |
| 1877 | MakeBazelTargetNoRestrictions("android_license", "my_license", AttrNameToString{}), |
| 1878 | }, |
| 1879 | }) |
| 1880 | } |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 1881 | |
Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 1882 | func TestGenerateConfigSetting(t *testing.T) { |
| 1883 | bp := ` |
| 1884 | custom { |
| 1885 | name: "foo", |
| 1886 | test_config_setting: true, |
| 1887 | } |
| 1888 | ` |
| 1889 | expectedBazelTargets := []string{ |
| 1890 | MakeBazelTargetNoRestrictions( |
| 1891 | "config_setting", |
| 1892 | "foo_config_setting", |
| 1893 | AttrNameToString{ |
| 1894 | "flag_values": `{ |
| 1895 | "//build/bazel/rules/my_string_setting": "foo", |
| 1896 | }`, |
| 1897 | }, |
| 1898 | ), |
| 1899 | MakeBazelTarget( |
| 1900 | "custom", |
| 1901 | "foo", |
| 1902 | AttrNameToString{}, |
| 1903 | ), |
| 1904 | } |
| 1905 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1906 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1907 | } |
| 1908 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1909 | Blueprint: bp, |
| 1910 | ExpectedBazelTargets: expectedBazelTargets, |
| 1911 | Description: "Generating API contribution Bazel targets for custom module", |
| 1912 | }) |
| 1913 | } |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1914 | |
| 1915 | // If values of all keys in an axis are equal to //conditions:default, drop the axis and print the common value |
| 1916 | func TestPrettyPrintSelectMapEqualValues(t *testing.T) { |
| 1917 | lla := bazel.LabelListAttribute{ |
| 1918 | Value: bazel.LabelList{}, |
| 1919 | } |
| 1920 | libFooImplLabel := bazel.Label{ |
| 1921 | Label: ":libfoo.impl", |
| 1922 | } |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1923 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 1924 | lla.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.MakeLabelList([]bazel.Label{libFooImplLabel})) |
| 1925 | actual, _ := prettyPrintAttribute(lla, 0) |
| 1926 | android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual) |
| 1927 | } |
Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 1928 | |
| 1929 | // If CommonAttributes.Dir is set, the bazel target should be created in that dir |
| 1930 | func TestCreateBazelTargetInDifferentDir(t *testing.T) { |
| 1931 | t.Parallel() |
| 1932 | bp := ` |
| 1933 | custom { |
| 1934 | name: "foo", |
| 1935 | dir: "subdir", |
| 1936 | } |
| 1937 | ` |
| 1938 | registerCustomModule := func(ctx android.RegistrationContext) { |
| 1939 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
| 1940 | } |
| 1941 | // Check that foo is not created in root dir |
| 1942 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1943 | Description: "foo is not created in root dir because it sets dir explicitly", |
| 1944 | Blueprint: bp, |
| 1945 | Filesystem: map[string]string{ |
| 1946 | "subdir/Android.bp": "", |
| 1947 | }, |
| 1948 | ExpectedBazelTargets: []string{}, |
| 1949 | }) |
| 1950 | // Check that foo is created in `subdir` |
| 1951 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1952 | Description: "foo is created in `subdir` because it sets dir explicitly", |
| 1953 | Blueprint: bp, |
| 1954 | Filesystem: map[string]string{ |
| 1955 | "subdir/Android.bp": "", |
| 1956 | }, |
| 1957 | Dir: "subdir", |
| 1958 | ExpectedBazelTargets: []string{ |
| 1959 | MakeBazelTarget("custom", "foo", AttrNameToString{}), |
| 1960 | }, |
| 1961 | }) |
| 1962 | // Check that we cannot create target in different dir if it is does not an Android.bp |
| 1963 | RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{ |
| 1964 | Description: "foo cannot be created in `subdir` because it does not contain an Android.bp file", |
| 1965 | Blueprint: bp, |
| 1966 | Dir: "subdir", |
| 1967 | ExpectedErr: fmt.Errorf("Cannot use ca.Dir to create a BazelTarget in dir: subdir since it does not contain an Android.bp file"), |
| 1968 | }) |
| 1969 | |
| 1970 | } |