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