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 | } |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame^] | 46 | if strings.Contains(errs[0].Error(), expectedErr.Error()) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 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) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame^] | 130 | if tc.expectedErr != nil { |
| 131 | if checkError(t, errs, tc.expectedErr) { |
| 132 | return |
| 133 | } else { |
| 134 | t.Errorf("Expected error: %q, got: %q", tc.expectedErr, errs) |
| 135 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 136 | } else { |
| 137 | android.FailIfErrored(t, errs) |
| 138 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 139 | if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount { |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 140 | t.Errorf("%s: Expected %d bazel target (%s), got `%d`` (%s)", |
| 141 | tc.description, expectedCount, tc.expectedBazelTargets, actualCount, bazelTargets) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 142 | } else { |
| 143 | for i, target := range bazelTargets { |
| 144 | if w, g := tc.expectedBazelTargets[i], target.content; w != g { |
| 145 | t.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 146 | "%s: Expected generated Bazel target to be `%s`, got `%s`", |
| 147 | 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] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 153 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 154 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 157 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 158 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 162 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 165 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 166 | EmbeddedProps |
| 167 | *OtherEmbeddedProps |
| 168 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 169 | Bool_prop bool |
| 170 | Bool_ptr_prop *bool |
| 171 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 172 | Int_prop int `blueprint:"mutated"` |
| 173 | Int64_ptr_prop *int64 |
| 174 | String_prop string |
| 175 | String_ptr_prop *string |
| 176 | String_list_prop []string |
| 177 | |
| 178 | Nested_props nestedProps |
| 179 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 180 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 181 | Arch_paths []string `android:"path,arch_variant"` |
| 182 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 183 | |
| 184 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 185 | One_to_many_prop *bool |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | type customModule struct { |
| 189 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 190 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 191 | |
| 192 | props customProps |
| 193 | } |
| 194 | |
| 195 | // OutputFiles is needed because some instances of this module use dist with a |
| 196 | // tag property which requires the module implements OutputFileProducer. |
| 197 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 198 | return android.PathsForTesting("path" + tag), nil |
| 199 | } |
| 200 | |
| 201 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 202 | // nothing for now. |
| 203 | } |
| 204 | |
| 205 | func customModuleFactoryBase() android.Module { |
| 206 | module := &customModule{} |
| 207 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 208 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 209 | return module |
| 210 | } |
| 211 | |
| 212 | func customModuleFactory() android.Module { |
| 213 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 214 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 215 | return m |
| 216 | } |
| 217 | |
| 218 | type testProps struct { |
| 219 | Test_prop struct { |
| 220 | Test_string_prop string |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | type customTestModule struct { |
| 225 | android.ModuleBase |
| 226 | |
| 227 | props customProps |
| 228 | test_props testProps |
| 229 | } |
| 230 | |
| 231 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 232 | // nothing for now. |
| 233 | } |
| 234 | |
| 235 | func customTestModuleFactoryBase() android.Module { |
| 236 | m := &customTestModule{} |
| 237 | m.AddProperties(&m.props) |
| 238 | m.AddProperties(&m.test_props) |
| 239 | return m |
| 240 | } |
| 241 | |
| 242 | func customTestModuleFactory() android.Module { |
| 243 | m := customTestModuleFactoryBase() |
| 244 | android.InitAndroidModule(m) |
| 245 | return m |
| 246 | } |
| 247 | |
| 248 | type customDefaultsModule struct { |
| 249 | android.ModuleBase |
| 250 | android.DefaultsModuleBase |
| 251 | } |
| 252 | |
| 253 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 254 | module := &customDefaultsModule{} |
| 255 | module.AddProperties(&customProps{}) |
| 256 | return module |
| 257 | } |
| 258 | |
| 259 | func customDefaultsModuleFactoryBasic() android.Module { |
| 260 | return customDefaultsModuleFactoryBase() |
| 261 | } |
| 262 | |
| 263 | func customDefaultsModuleFactory() android.Module { |
| 264 | m := customDefaultsModuleFactoryBase() |
| 265 | android.InitDefaultsModule(m) |
| 266 | return m |
| 267 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 268 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 269 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 270 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 274 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 277 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 278 | EmbeddedAttr |
| 279 | *OtherEmbeddedAttr |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 280 | String_ptr_prop *string |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 281 | String_list_prop []string |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 282 | Arch_paths bazel.LabelListAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 283 | } |
| 284 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 285 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 286 | paths := bazel.LabelListAttribute{} |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 287 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 288 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 289 | customBp2buildOneToMany(ctx, m) |
| 290 | return |
| 291 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 292 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 293 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 294 | for config, props := range configToProps { |
| 295 | if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil { |
| 296 | 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] | 297 | } |
| 298 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 299 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 300 | |
| 301 | paths.ResolveExcludes() |
| 302 | |
| 303 | attrs := &customBazelModuleAttributes{ |
| 304 | String_ptr_prop: m.props.String_ptr_prop, |
| 305 | String_list_prop: m.props.String_list_prop, |
| 306 | Arch_paths: paths, |
| 307 | } |
| 308 | attrs.Embedded_attr = m.props.Embedded_prop |
| 309 | if m.props.OtherEmbeddedProps != nil { |
| 310 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 311 | } |
| 312 | |
| 313 | props := bazel.BazelTargetModuleProperties{ |
| 314 | Rule_class: "custom", |
| 315 | } |
| 316 | |
| 317 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 318 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 319 | |
| 320 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 321 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 322 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 323 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 324 | baseName := m.Name() |
| 325 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 326 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 327 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 328 | Rule_class: "my_library", |
| 329 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 330 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 331 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 332 | |
| 333 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 334 | Rule_class: "proto_library", |
| 335 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 336 | } |
| 337 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 338 | |
| 339 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 340 | Rule_class: "my_proto_library", |
| 341 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 342 | } |
| 343 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 344 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 345 | |
| 346 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 347 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 348 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 349 | res, err := GenerateBazelTargets(codegenCtx, false) |
| 350 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 351 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 352 | |
| 353 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
| 354 | ctx.RegisterModuleType("custom", customModuleFactory) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 355 | ctx.RegisterForBazelConversion() |
| 356 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 357 | |
| 358 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 359 | return fmt.Sprintf(` |
| 360 | %s { |
| 361 | name: "%s", |
| 362 | bazel_module: { bp2build_available: false }, |
| 363 | }`, typ, name) |
| 364 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 365 | |
| 366 | type attrNameToString map[string]string |
| 367 | |
| 368 | func makeBazelTarget(typ, name string, attrs attrNameToString) string { |
| 369 | attrStrings := make([]string, 0, len(attrs)+1) |
| 370 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 371 | for _, k := range android.SortedStringKeys(attrs) { |
| 372 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 373 | } |
| 374 | return fmt.Sprintf(`%s( |
| 375 | %s |
| 376 | )`, typ, strings.Join(attrStrings, "\n")) |
| 377 | } |