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" |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 28 | "android/soong/android/allowlists" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 29 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 32 | var ( |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 33 | buildDir string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 34 | ) |
| 35 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 36 | 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] | 37 | t.Helper() |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 38 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 39 | if len(errs) != 1 { |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 40 | return false |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 41 | } |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 42 | if strings.Contains(errs[0].Error(), expectedErr.Error()) { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 43 | return true |
| 44 | } |
| 45 | |
| 46 | return false |
| 47 | } |
| 48 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 49 | func errored(t *testing.T, tc Bp2buildTestCase, errs []error) bool { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 50 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 51 | if tc.ExpectedErr != nil { |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 52 | // Rely on checkErrors, as this test case is expected to have an error. |
| 53 | return false |
| 54 | } |
| 55 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 56 | if len(errs) > 0 { |
| 57 | for _, err := range errs { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 58 | 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] | 59 | } |
| 60 | return true |
| 61 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 62 | |
| 63 | // 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] | 64 | return false |
| 65 | } |
| 66 | |
Trevor Radcliffe | 1b4b2d9 | 2022-09-01 18:57:01 +0000 | [diff] [blame] | 67 | 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] | 68 | t.Helper() |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 69 | RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, tc) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 72 | type Bp2buildTestCase struct { |
| 73 | Description string |
| 74 | ModuleTypeUnderTest string |
| 75 | ModuleTypeUnderTestFactory android.ModuleFactory |
| 76 | Blueprint string |
| 77 | ExpectedBazelTargets []string |
| 78 | Filesystem map[string]string |
| 79 | Dir string |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 80 | // An error with a string contained within the string of the expected error |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 81 | ExpectedErr error |
| 82 | UnconvertedDepsMode unconvertedDepsMode |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 83 | |
| 84 | // For every directory listed here, the BUILD file for that directory will |
| 85 | // be merged with the generated BUILD file. This allows custom BUILD targets |
| 86 | // to be used in tests, or use BUILD files to draw package boundaries. |
| 87 | KeepBuildFileForDirs []string |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 90 | func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 91 | t.Helper() |
| 92 | dir := "." |
| 93 | filesystem := make(map[string][]byte) |
| 94 | toParse := []string{ |
| 95 | "Android.bp", |
| 96 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 97 | for f, content := range tc.Filesystem { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 98 | if strings.HasSuffix(f, "Android.bp") { |
| 99 | toParse = append(toParse, f) |
| 100 | } |
| 101 | filesystem[f] = []byte(content) |
| 102 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 103 | config := android.TestConfig(buildDir, nil, tc.Blueprint, filesystem) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 104 | ctx := android.NewTestContext(config) |
| 105 | |
| 106 | registerModuleTypes(ctx) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 107 | ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory) |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 108 | |
| 109 | // A default configuration for tests to not have to specify bp2build_available on top level targets. |
| 110 | bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig( |
| 111 | allowlists.Bp2BuildConfig{ |
| 112 | android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively, |
| 113 | }, |
| 114 | ) |
| 115 | for _, f := range tc.KeepBuildFileForDirs { |
| 116 | bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{ |
| 117 | f: /*recursive=*/ false, |
| 118 | }) |
| 119 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 120 | 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] | 121 | ctx.RegisterForBazelConversion() |
| 122 | |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 123 | _, parseErrs := ctx.ParseFileList(dir, toParse) |
| 124 | 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] | 125 | return |
| 126 | } |
Jingwen Chen | 5146ac0 | 2021-09-02 11:44:42 +0000 | [diff] [blame] | 127 | _, resolveDepsErrs := ctx.ResolveDependencies(config) |
| 128 | if errored(t, tc, resolveDepsErrs) { |
| 129 | return |
| 130 | } |
| 131 | |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 132 | parseAndResolveErrs := append(parseErrs, resolveDepsErrs...) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 133 | if tc.ExpectedErr != nil && checkError(t, parseAndResolveErrs, tc.ExpectedErr) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 134 | return |
| 135 | } |
| 136 | |
| 137 | checkDir := dir |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 138 | if tc.Dir != "" { |
| 139 | checkDir = tc.Dir |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 140 | } |
| 141 | codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 142 | codegenCtx.unconvertedDepMode = tc.UnconvertedDepsMode |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 143 | bazelTargets, errs := generateBazelTargetsForDir(codegenCtx, checkDir) |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 144 | if tc.ExpectedErr != nil { |
| 145 | if checkError(t, errs, tc.ExpectedErr) { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 146 | return |
| 147 | } else { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 148 | t.Errorf("Expected error: %q, got: %q and %q", tc.ExpectedErr, errs, parseAndResolveErrs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 149 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 150 | } else { |
| 151 | android.FailIfErrored(t, errs) |
| 152 | } |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 153 | if actualCount, expectedCount := len(bazelTargets), len(tc.ExpectedBazelTargets); actualCount != expectedCount { |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 154 | t.Errorf("%s: Expected %d bazel target (%s), got %d (%s)", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 155 | 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] | 156 | } else { |
| 157 | for i, target := range bazelTargets { |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 158 | if w, g := tc.ExpectedBazelTargets[i], target.content; w != g { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 1c92aef | 2021-08-23 16:10:00 +0000 | [diff] [blame] | 159 | t.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 160 | "%s: Expected generated Bazel target to be `%s`, got `%s`", |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 161 | 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] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 167 | type nestedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 168 | Nested_prop *string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 171 | type EmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 172 | Embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | type OtherEmbeddedProps struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 176 | Other_embedded_prop *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 179 | type customProps struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 180 | EmbeddedProps |
| 181 | *OtherEmbeddedProps |
| 182 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 183 | Bool_prop bool |
| 184 | Bool_ptr_prop *bool |
| 185 | // Ensure that properties tagged `blueprint:mutated` are omitted |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 186 | Int_prop int `blueprint:"mutated"` |
| 187 | Int64_ptr_prop *int64 |
| 188 | String_prop string |
| 189 | String_literal_prop *string `android:"arch_variant"` |
| 190 | String_ptr_prop *string |
| 191 | String_list_prop []string |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 192 | |
| 193 | Nested_props nestedProps |
| 194 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 195 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 196 | Arch_paths []string `android:"path,arch_variant"` |
| 197 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 198 | |
| 199 | // Prop used to indicate this conversion should be 1 module -> multiple targets |
| 200 | One_to_many_prop *bool |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | type customModule struct { |
| 204 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 205 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 206 | |
| 207 | props customProps |
| 208 | } |
| 209 | |
| 210 | // OutputFiles is needed because some instances of this module use dist with a |
| 211 | // tag property which requires the module implements OutputFileProducer. |
| 212 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 213 | return android.PathsForTesting("path" + tag), nil |
| 214 | } |
| 215 | |
| 216 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 217 | // nothing for now. |
| 218 | } |
| 219 | |
| 220 | func customModuleFactoryBase() android.Module { |
| 221 | module := &customModule{} |
| 222 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 223 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 224 | return module |
| 225 | } |
| 226 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 227 | func customModuleFactoryHostAndDevice() android.Module { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 228 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 229 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 230 | return m |
| 231 | } |
| 232 | |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 233 | func customModuleFactoryDeviceSupported() android.Module { |
| 234 | m := customModuleFactoryBase() |
| 235 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibBoth) |
| 236 | return m |
| 237 | } |
| 238 | |
| 239 | func customModuleFactoryHostSupported() android.Module { |
| 240 | m := customModuleFactoryBase() |
| 241 | android.InitAndroidArchModule(m, android.HostSupported, android.MultilibBoth) |
| 242 | return m |
| 243 | } |
| 244 | |
| 245 | func customModuleFactoryHostAndDeviceDefault() android.Module { |
| 246 | m := customModuleFactoryBase() |
| 247 | android.InitAndroidArchModule(m, android.HostAndDeviceDefault, android.MultilibBoth) |
| 248 | return m |
| 249 | } |
| 250 | |
| 251 | func customModuleFactoryNeitherHostNorDeviceSupported() android.Module { |
| 252 | m := customModuleFactoryBase() |
| 253 | android.InitAndroidArchModule(m, android.NeitherHostNorDeviceSupported, android.MultilibBoth) |
| 254 | return m |
| 255 | } |
| 256 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 257 | type testProps struct { |
| 258 | Test_prop struct { |
| 259 | Test_string_prop string |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | type customTestModule struct { |
| 264 | android.ModuleBase |
| 265 | |
| 266 | props customProps |
| 267 | test_props testProps |
| 268 | } |
| 269 | |
| 270 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 271 | // nothing for now. |
| 272 | } |
| 273 | |
| 274 | func customTestModuleFactoryBase() android.Module { |
| 275 | m := &customTestModule{} |
| 276 | m.AddProperties(&m.props) |
| 277 | m.AddProperties(&m.test_props) |
| 278 | return m |
| 279 | } |
| 280 | |
| 281 | func customTestModuleFactory() android.Module { |
| 282 | m := customTestModuleFactoryBase() |
| 283 | android.InitAndroidModule(m) |
| 284 | return m |
| 285 | } |
| 286 | |
| 287 | type customDefaultsModule struct { |
| 288 | android.ModuleBase |
| 289 | android.DefaultsModuleBase |
| 290 | } |
| 291 | |
| 292 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 293 | module := &customDefaultsModule{} |
| 294 | module.AddProperties(&customProps{}) |
| 295 | return module |
| 296 | } |
| 297 | |
| 298 | func customDefaultsModuleFactoryBasic() android.Module { |
| 299 | return customDefaultsModuleFactoryBase() |
| 300 | } |
| 301 | |
| 302 | func customDefaultsModuleFactory() android.Module { |
| 303 | m := customDefaultsModuleFactoryBase() |
| 304 | android.InitDefaultsModule(m) |
| 305 | return m |
| 306 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 307 | |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 308 | type EmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 309 | Embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | type OtherEmbeddedAttr struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 313 | Other_embedded_attr *string |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 314 | } |
| 315 | |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 316 | type customBazelModuleAttributes struct { |
Liz Kammer | 32a0339 | 2021-09-14 11:17:21 -0400 | [diff] [blame] | 317 | EmbeddedAttr |
| 318 | *OtherEmbeddedAttr |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 319 | String_literal_prop bazel.StringAttribute |
| 320 | String_ptr_prop *string |
| 321 | String_list_prop []string |
| 322 | Arch_paths bazel.LabelListAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 325 | func (m *customModule) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 326 | if p := m.props.One_to_many_prop; p != nil && *p { |
| 327 | customBp2buildOneToMany(ctx, m) |
| 328 | return |
| 329 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 330 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 331 | paths := bazel.LabelListAttribute{} |
| 332 | strAttr := bazel.StringAttribute{} |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 333 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 334 | for config, props := range configToProps { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 335 | if custProps, ok := props.(*customProps); ok { |
| 336 | if custProps.Arch_paths != nil { |
| 337 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, custProps.Arch_paths, custProps.Arch_paths_exclude)) |
| 338 | } |
| 339 | if custProps.String_literal_prop != nil { |
| 340 | strAttr.SetSelectValue(axis, config, custProps.String_literal_prop) |
| 341 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 342 | } |
| 343 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 344 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 345 | |
| 346 | paths.ResolveExcludes() |
| 347 | |
| 348 | attrs := &customBazelModuleAttributes{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 349 | String_literal_prop: strAttr, |
| 350 | String_ptr_prop: m.props.String_ptr_prop, |
| 351 | String_list_prop: m.props.String_list_prop, |
| 352 | Arch_paths: paths, |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 353 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 354 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 355 | attrs.Embedded_attr = m.props.Embedded_prop |
| 356 | if m.props.OtherEmbeddedProps != nil { |
| 357 | attrs.OtherEmbeddedAttr = &OtherEmbeddedAttr{Other_embedded_attr: m.props.OtherEmbeddedProps.Other_embedded_prop} |
| 358 | } |
| 359 | |
| 360 | props := bazel.BazelTargetModuleProperties{ |
| 361 | Rule_class: "custom", |
| 362 | } |
| 363 | |
| 364 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 365 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 366 | |
| 367 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 368 | // module to target. |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 369 | func customBp2buildOneToMany(ctx android.TopDownMutatorContext, m *customModule) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 370 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 371 | baseName := m.Name() |
| 372 | attrs := &customBazelModuleAttributes{} |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 373 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 374 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 375 | Rule_class: "my_library", |
| 376 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 377 | } |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 378 | ctx.CreateBazelTargetModule(myLibraryProps, android.CommonAttributes{Name: baseName}, attrs) |
| 379 | |
| 380 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 381 | Rule_class: "proto_library", |
| 382 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 383 | } |
| 384 | ctx.CreateBazelTargetModule(protoLibraryProps, android.CommonAttributes{Name: baseName + "_proto_library_deps"}, attrs) |
| 385 | |
| 386 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 387 | Rule_class: "my_proto_library", |
| 388 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 389 | } |
| 390 | ctx.CreateBazelTargetModule(myProtoLibraryProps, android.CommonAttributes{Name: baseName + "_my_proto_library_deps"}, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 391 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 392 | |
| 393 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 394 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) (BazelTargets, []error) { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 395 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 396 | res, err := GenerateBazelTargets(codegenCtx, false) |
Alix | 94e2603 | 2022-08-16 20:37:33 +0000 | [diff] [blame] | 397 | if err != nil { |
| 398 | return BazelTargets{}, err |
| 399 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 400 | return res.buildFileToTargets[dir], err |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 401 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 402 | |
| 403 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 404 | ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice) |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame] | 405 | ctx.RegisterForBazelConversion() |
| 406 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 407 | |
| 408 | func simpleModuleDoNotConvertBp2build(typ, name string) string { |
| 409 | return fmt.Sprintf(` |
| 410 | %s { |
| 411 | name: "%s", |
| 412 | bazel_module: { bp2build_available: false }, |
| 413 | }`, typ, name) |
| 414 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 415 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 416 | type AttrNameToString map[string]string |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 417 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 418 | func (a AttrNameToString) clone() AttrNameToString { |
| 419 | newAttrs := make(AttrNameToString, len(a)) |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 420 | for k, v := range a { |
| 421 | newAttrs[k] = v |
| 422 | } |
| 423 | return newAttrs |
| 424 | } |
| 425 | |
| 426 | // makeBazelTargetNoRestrictions returns bazel target build file definition that can be host or |
| 427 | // device specific, or independent of host/device. |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 428 | func makeBazelTargetHostOrDevice(typ, name string, attrs AttrNameToString, hod android.HostOrDeviceSupported) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 429 | if _, ok := attrs["target_compatible_with"]; !ok { |
| 430 | switch hod { |
| 431 | case android.HostSupported: |
| 432 | attrs["target_compatible_with"] = `select({ |
| 433 | "//build/bazel/platforms/os:android": ["@platforms//:incompatible"], |
| 434 | "//conditions:default": [], |
| 435 | })` |
| 436 | case android.DeviceSupported: |
| 437 | attrs["target_compatible_with"] = `["//build/bazel/platforms/os:android"]` |
| 438 | } |
| 439 | } |
| 440 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 441 | attrStrings := make([]string, 0, len(attrs)+1) |
Sasha Smundak | fb58949 | 2022-08-04 11:13:27 -0700 | [diff] [blame] | 442 | if name != "" { |
| 443 | attrStrings = append(attrStrings, fmt.Sprintf(` name = "%s",`, name)) |
| 444 | } |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 445 | for _, k := range android.SortedStringKeys(attrs) { |
| 446 | attrStrings = append(attrStrings, fmt.Sprintf(" %s = %s,", k, attrs[k])) |
| 447 | } |
| 448 | return fmt.Sprintf(`%s( |
| 449 | %s |
| 450 | )`, typ, strings.Join(attrStrings, "\n")) |
| 451 | } |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 452 | |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 453 | // MakeBazelTargetNoRestrictions returns bazel target build file definition that does not add a |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 454 | // target_compatible_with. This is useful for module types like filegroup and genrule that arch not |
| 455 | // arch variant |
Sam Delmerico | 3177a6e | 2022-06-21 19:28:33 +0000 | [diff] [blame] | 456 | func MakeBazelTargetNoRestrictions(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 457 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.HostAndDeviceDefault) |
| 458 | } |
| 459 | |
| 460 | // makeBazelTargetNoRestrictions returns bazel target build file definition that is device specific |
| 461 | // as this is the most common default in Soong. |
Alix | e06d75b | 2022-08-31 18:28:19 +0000 | [diff] [blame] | 462 | func MakeBazelTarget(typ, name string, attrs AttrNameToString) string { |
Liz Kammer | dfeb120 | 2022-05-13 17:20:20 -0400 | [diff] [blame] | 463 | return makeBazelTargetHostOrDevice(typ, name, attrs, android.DeviceSupported) |
| 464 | } |
Sasha Smundak | 9d2f174 | 2022-08-04 13:28:38 -0700 | [diff] [blame] | 465 | |
| 466 | type ExpectedRuleTarget struct { |
| 467 | Rule string |
| 468 | Name string |
| 469 | Attrs AttrNameToString |
| 470 | Hod android.HostOrDeviceSupported |
| 471 | } |
| 472 | |
| 473 | func (ebr ExpectedRuleTarget) String() string { |
| 474 | return makeBazelTargetHostOrDevice(ebr.Rule, ebr.Name, ebr.Attrs, ebr.Hod) |
| 475 | } |
Trevor Radcliffe | 087af54 | 2022-09-16 15:36:10 +0000 | [diff] [blame] | 476 | |
| 477 | func makeCcStubSuiteTargets(name string, attrs AttrNameToString) string { |
| 478 | if _, hasStubs := attrs["stubs_symbol_file"]; !hasStubs { |
| 479 | return "" |
| 480 | } |
| 481 | STUB_SUITE_ATTRS := map[string]string{ |
| 482 | "stubs_symbol_file": "symbol_file", |
| 483 | "stubs_versions": "versions", |
| 484 | "soname": "soname", |
| 485 | "source_library": "source_library", |
| 486 | } |
| 487 | |
| 488 | stubSuiteAttrs := AttrNameToString{} |
| 489 | for key, _ := range attrs { |
| 490 | if _, stubSuiteAttr := STUB_SUITE_ATTRS[key]; stubSuiteAttr { |
| 491 | stubSuiteAttrs[STUB_SUITE_ATTRS[key]] = attrs[key] |
| 492 | } |
| 493 | } |
| 494 | return MakeBazelTarget("cc_stub_suite", name+"_stub_libs", stubSuiteAttrs) |
| 495 | } |