Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | f5a3eac | 2021-08-23 17:05:17 +0000 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 15 | package bp2build |
| 16 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0da7ce6 | 2021-08-23 17:04:20 +0000 | [diff] [blame] | 17 | /* |
| 18 | For shareable/common bp2build testing functionality and dumping ground for |
| 19 | specific-but-shared functionality among tests in package |
| 20 | */ |
| 21 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 22 | import ( |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 23 | "fmt" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 24 | "strings" |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 25 | "testing" |
| 26 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 27 | "android/soong/android" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 28 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 31 | var ( |
| 32 | // A default configuration for tests to not have to specify bp2build_available on top level targets. |
| 33 | bp2buildConfig = android.Bp2BuildConfig{ |
| 34 | android.BP2BUILD_TOPLEVEL: android.Bp2BuildDefaultTrueRecursively, |
| 35 | } |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 36 | |
| 37 | buildDir string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 38 | ) |
| 39 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 40 | func checkError(t *testing.T, errs []error, expectedErr error) bool { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 41 | t.Helper() |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 42 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 43 | if len(errs) != 1 { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 44 | return false |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 45 | } |
| 46 | if errs[0].Error() == expectedErr.Error() { |
| 47 | return true |
| 48 | } |
| 49 | |
| 50 | return false |
| 51 | } |
| 52 | |
| 53 | func errored(t *testing.T, tc bp2buildTestCase, errs []error) bool { |
| 54 | t.Helper() |
| 55 | if tc.expectedErr != nil { |
| 56 | // Rely on checkErrors, as this test case is expected to have an error. |
| 57 | return false |
| 58 | } |
| 59 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 60 | if len(errs) > 0 { |
| 61 | for _, err := range errs { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 62 | t.Errorf("%s: %s", tc.description, err) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 63 | } |
| 64 | return true |
| 65 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 66 | |
| 67 | // All good, continue execution. |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 68 | return false |
| 69 | } |
| 70 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ce0a07e | 2021-08-23 16:17:32 +0000 | [diff] [blame] | 71 | func runBp2BuildTestCaseSimple(t *testing.T, tc bp2buildTestCase) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 72 | t.Helper() |
| 73 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) |
| 74 | } |
| 75 | |
| 76 | type bp2buildTestCase struct { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 77 | description string |
| 78 | moduleTypeUnderTest string |
| 79 | moduleTypeUnderTestFactory android.ModuleFactory |
| 80 | blueprint string |
| 81 | expectedBazelTargets []string |
| 82 | filesystem map[string]string |
| 83 | dir string |
| 84 | expectedErr error |
| 85 | unconvertedDepsMode unconvertedDepsMode |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) { |
| 89 | t.Helper() |
| 90 | dir := "." |
| 91 | filesystem := make(map[string][]byte) |
| 92 | toParse := []string{ |
| 93 | "Android.bp", |
| 94 | } |
| 95 | for f, content := range tc.filesystem { |
| 96 | if strings.HasSuffix(f, "Android.bp") { |
| 97 | toParse = append(toParse, f) |
| 98 | } |
| 99 | filesystem[f] = []byte(content) |
| 100 | } |
| 101 | config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem) |
| 102 | ctx := android.NewTestContext(config) |
| 103 | |
| 104 | registerModuleTypes(ctx) |
| 105 | ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory) |
| 106 | ctx.RegisterBp2BuildConfig(bp2buildConfig) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 107 | ctx.RegisterForBazelConversion() |
| 108 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 109 | _, parseErrs := ctx.ParseFileList(dir, toParse) |
| 110 | if errored(t, tc, parseErrs) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 111 | return |
| 112 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 113 | _, resolveDepsErrs := ctx.ResolveDependencies(config) |
| 114 | if errored(t, tc, resolveDepsErrs) { |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | errs := append(parseErrs, resolveDepsErrs...) |
| 119 | if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 120 | return |
| 121 | } |
| 122 | |
| 123 | checkDir := dir |
| 124 | if tc.dir != "" { |
| 125 | checkDir = tc.dir |
| 126 | } |
| 127 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 128 | codegenCtx.unconvertedDepMode = tc.unconvertedDepsMode |
| 129 | bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir) |
| 130 | if tc.expectedErr != nil && checkError(t, errs, tc.expectedErr) { |
| 131 | return |
| 132 | } else { |
| 133 | android.FailIfErrored(t, errs) |
| 134 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 135 | if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 136 | t.Errorf("%s: Expected %d bazel target, got `%d``", |
| 137 | tc.description, expectedCount, actualCount) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 138 | } else { |
| 139 | for i, target := range bazelTargets { |
| 140 | if w, g := tc.expectedBazelTargets[i], target.content; w != g { |
| 141 | t.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 142 | "%s: Expected generated Bazel target to be `%s`, got `%s`", |
| 143 | tc.description, w, g) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 149 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 150 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 153 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 154 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 158 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 161 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 162 | EmbeddedProps |
| 163 | *OtherEmbeddedProps |
| 164 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 165 | Bool_prop bool |
| 166 | Bool_ptr_prop *bool |
| 167 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 168 | Int_prop int `blueprint:"mutated"` |
| 169 | Int64_ptr_prop *int64 |
| 170 | String_prop string |
| 171 | String_ptr_prop *string |
| 172 | String_list_prop []string |
| 173 | |
| 174 | Nested_props nestedProps |
| 175 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 176 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 177 | Arch_paths []string `android:"path,arch_variant"` |
| 178 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 179 | |
| 180 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 181 | One_to_many_prop *bool |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | type customModule struct { |
| 185 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 186 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 187 | |
| 188 | props customProps |
| 189 | } |
| 190 | |
| 191 | // OutputFiles is needed because some instances of this module use dist with a |
| 192 | // tag property which requires the module implements OutputFileProducer. |
| 193 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 194 | return android.PathsForTesting("path" + tag), nil |
| 195 | } |
| 196 | |
| 197 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 198 | // nothing for now. |
| 199 | } |
| 200 | |
| 201 | func customModuleFactoryBase() android.Module { |
| 202 | module := &customModule{} |
| 203 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 204 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 205 | return module |
| 206 | } |
| 207 | |
| 208 | func customModuleFactory() android.Module { |
| 209 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 210 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 211 | return m |
| 212 | } |
| 213 | |
| 214 | type testProps struct { |
| 215 | Test_prop struct { |
| 216 | Test_string_prop string |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | type customTestModule struct { |
| 221 | android.ModuleBase |
| 222 | |
| 223 | props customProps |
| 224 | test_props testProps |
| 225 | } |
| 226 | |
| 227 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 228 | // nothing for now. |
| 229 | } |
| 230 | |
| 231 | func customTestModuleFactoryBase() android.Module { |
| 232 | m := &customTestModule{} |
| 233 | m.AddProperties(&m.props) |
| 234 | m.AddProperties(&m.test_props) |
| 235 | return m |
| 236 | } |
| 237 | |
| 238 | func customTestModuleFactory() android.Module { |
| 239 | m := customTestModuleFactoryBase() |
| 240 | android.InitAndroidModule(m) |
| 241 | return m |
| 242 | } |
| 243 | |
| 244 | type customDefaultsModule struct { |
| 245 | android.ModuleBase |
| 246 | android.DefaultsModuleBase |
| 247 | } |
| 248 | |
| 249 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 250 | module := &customDefaultsModule{} |
| 251 | module.AddProperties(&customProps{}) |
| 252 | return module |
| 253 | } |
| 254 | |
| 255 | func customDefaultsModuleFactoryBasic() android.Module { |
| 256 | return customDefaultsModuleFactoryBase() |
| 257 | } |
| 258 | |
| 259 | func customDefaultsModuleFactory() android.Module { |
| 260 | m := customDefaultsModuleFactoryBase() |
| 261 | android.InitDefaultsModule(m) |
| 262 | return m |
| 263 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 264 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 265 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 266 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 270 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 271 | } |
| 272 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 273 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 274 | EmbeddedAttr |
| 275 | *OtherEmbeddedAttr |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 276 | String_ptr_prop *string |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 277 | String_list_prop []string |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 278 | Arch_paths bazel.LabelListAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 281 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 282 | paths := bazel.LabelListAttribute{} |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 283 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 284 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 285 | customBp2buildOneToMany(ctx, m) |
| 286 | return |
| 287 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 288 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 289 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 290 | for config, props := range configToProps { |
| 291 | if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil { |
| 292 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude)) |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 295 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 296 | |
| 297 | paths.ResolveExcludes() |
| 298 | |
| 299 | attrs := &customBazelModuleAttributes{ |
| 300 | String_ptr_prop: m.props.String_ptr_prop, |
| 301 | String_list_prop: m.props.String_list_prop, |
| 302 | Arch_paths: paths, |
| 303 | } |
| 304 | attrs.Embedded_attr = m.props.Embedded_prop |
| 305 | if m.props.OtherEmbeddedProps != nil { |
| 306 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 307 | } |
| 308 | |
| 309 | props := bazel.BazelTargetModuleProperties{ |
| 310 | Rule_class: "custom", |
| 311 | } |
| 312 | |
| 313 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 314 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 315 | |
| 316 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 317 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 318 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 319 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 320 | baseName := m.Name() |
| 321 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 322 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 323 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 324 | Rule_class: "my_library", |
| 325 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 326 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 327 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 328 | |
| 329 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 330 | Rule_class: "proto_library", |
| 331 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 332 | } |
| 333 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 334 | |
| 335 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 336 | Rule_class: "my_proto_library", |
| 337 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 338 | } |
| 339 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 340 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 341 | |
| 342 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 343 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 344 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 345 | res, err := GenerateBazelTargets(codegenCtx, false) |
| 346 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 347 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 348 | |
| 349 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
| 350 | ctx.RegisterModuleType("custom", customModuleFactory) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 351 | ctx.RegisterForBazelConversion() |
| 352 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 353 | |
| 354 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 355 | return fmt.Sprintf(` |
| 356 | %s { |
| 357 | name: "%s", |
| 358 | bazel_module: { bp2build_available: false }, |
| 359 | }`, typ, name) |
| 360 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 361 | |
| 362 | type attrNameToString map[string]string |
| 363 | |
| 364 | func makeBazelTarget(typ, name string, attrs attrNameToString) string { |
| 365 | attrStrings := make([]string, 0, len(attrs)+1) |
| 366 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 367 | for _, k := range android.SortedStringKeys(attrs) { |
| 368 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 369 | } |
| 370 | return fmt.Sprintf(`%s( |
| 371 | %s |
| 372 | )`, typ, strings.Join(attrStrings, "\n")) |
| 373 | } |