Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 4 | "testing" |
| 5 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 6 | "android/soong/android" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 7 | "android/soong/bazel" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 10 | var ( |
| 11 | // A default configuration for tests to not have to specify bp2build_available on top level targets. |
| 12 | bp2buildConfig = android.Bp2BuildConfig{ |
| 13 | android.BP2BUILD_TOPLEVEL: android.Bp2BuildDefaultTrueRecursively, |
| 14 | } |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 15 | |
| 16 | buildDir string |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 17 | ) |
| 18 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 19 | type nestedProps struct { |
| 20 | Nested_prop string |
| 21 | } |
| 22 | |
| 23 | type customProps struct { |
| 24 | Bool_prop bool |
| 25 | Bool_ptr_prop *bool |
| 26 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 27 | Int_prop int `blueprint:"mutated"` |
| 28 | Int64_ptr_prop *int64 |
| 29 | String_prop string |
| 30 | String_ptr_prop *string |
| 31 | String_list_prop []string |
| 32 | |
| 33 | Nested_props nestedProps |
| 34 | Nested_props_ptr *nestedProps |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 35 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame^] | 36 | Arch_paths []string `android:"path,arch_variant"` |
| 37 | Arch_paths_exclude []string `android:"path,arch_variant"` |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type customModule struct { |
| 41 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 42 | android.BazelModuleBase |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 43 | |
| 44 | props customProps |
| 45 | } |
| 46 | |
Rupert Shuttleworth | 06559d0 | 2021-05-19 09:14:26 -0400 | [diff] [blame] | 47 | func errored(t *testing.T, desc string, errs []error) bool { |
| 48 | t.Helper() |
| 49 | if len(errs) > 0 { |
| 50 | for _, err := range errs { |
| 51 | t.Errorf("%s: %s", desc, err) |
| 52 | } |
| 53 | return true |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 58 | // OutputFiles is needed because some instances of this module use dist with a |
| 59 | // tag property which requires the module implements OutputFileProducer. |
| 60 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 61 | return android.PathsForTesting("path" + tag), nil |
| 62 | } |
| 63 | |
| 64 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 65 | // nothing for now. |
| 66 | } |
| 67 | |
| 68 | func customModuleFactoryBase() android.Module { |
| 69 | module := &customModule{} |
| 70 | module.AddProperties(&module.props) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 71 | android.InitBazelModule(module) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 72 | return module |
| 73 | } |
| 74 | |
| 75 | func customModuleFactory() android.Module { |
| 76 | m := customModuleFactoryBase() |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 77 | android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibBoth) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 78 | return m |
| 79 | } |
| 80 | |
| 81 | type testProps struct { |
| 82 | Test_prop struct { |
| 83 | Test_string_prop string |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | type customTestModule struct { |
| 88 | android.ModuleBase |
| 89 | |
| 90 | props customProps |
| 91 | test_props testProps |
| 92 | } |
| 93 | |
| 94 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 95 | // nothing for now. |
| 96 | } |
| 97 | |
| 98 | func customTestModuleFactoryBase() android.Module { |
| 99 | m := &customTestModule{} |
| 100 | m.AddProperties(&m.props) |
| 101 | m.AddProperties(&m.test_props) |
| 102 | return m |
| 103 | } |
| 104 | |
| 105 | func customTestModuleFactory() android.Module { |
| 106 | m := customTestModuleFactoryBase() |
| 107 | android.InitAndroidModule(m) |
| 108 | return m |
| 109 | } |
| 110 | |
| 111 | type customDefaultsModule struct { |
| 112 | android.ModuleBase |
| 113 | android.DefaultsModuleBase |
| 114 | } |
| 115 | |
| 116 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 117 | module := &customDefaultsModule{} |
| 118 | module.AddProperties(&customProps{}) |
| 119 | return module |
| 120 | } |
| 121 | |
| 122 | func customDefaultsModuleFactoryBasic() android.Module { |
| 123 | return customDefaultsModuleFactoryBase() |
| 124 | } |
| 125 | |
| 126 | func customDefaultsModuleFactory() android.Module { |
| 127 | m := customDefaultsModuleFactoryBase() |
| 128 | android.InitDefaultsModule(m) |
| 129 | return m |
| 130 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 131 | |
| 132 | type customBazelModuleAttributes struct { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 133 | String_prop string |
| 134 | String_list_prop []string |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 135 | Arch_paths bazel.LabelListAttribute |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | type customBazelModule struct { |
| 139 | android.BazelTargetModuleBase |
| 140 | customBazelModuleAttributes |
| 141 | } |
| 142 | |
| 143 | func customBazelModuleFactory() android.Module { |
| 144 | module := &customBazelModule{} |
| 145 | module.AddProperties(&module.customBazelModuleAttributes) |
| 146 | android.InitBazelTargetModule(module) |
| 147 | return module |
| 148 | } |
| 149 | |
| 150 | func (m *customBazelModule) Name() string { return m.BaseModuleName() } |
| 151 | func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 152 | |
| 153 | func customBp2BuildMutator(ctx android.TopDownMutatorContext) { |
| 154 | if m, ok := ctx.Module().(*customModule); ok { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 155 | if !m.ConvertWithBp2build(ctx) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 156 | return |
| 157 | } |
| 158 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame^] | 159 | paths := bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrcExcludes(ctx, m.props.Arch_paths, m.props.Arch_paths_exclude)) |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 160 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 161 | for axis, configToProps := range m.GetArchVariantProperties(ctx, &customProps{}) { |
| 162 | for config, props := range configToProps { |
| 163 | if archProps, ok := props.(*customProps); ok && archProps.Arch_paths != nil { |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame^] | 164 | paths.SetSelectValue(axis, config, android.BazelLabelForModuleSrcExcludes(ctx, archProps.Arch_paths, archProps.Arch_paths_exclude)) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 165 | } |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame^] | 169 | paths.ResolveExcludes() |
| 170 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 171 | attrs := &customBazelModuleAttributes{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 172 | String_prop: m.props.String_prop, |
| 173 | String_list_prop: m.props.String_list_prop, |
Liz Kammer | 4562a3b | 2021-04-21 18:15:34 -0400 | [diff] [blame] | 174 | Arch_paths: paths, |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 177 | props := bazel.BazelTargetModuleProperties{ |
| 178 | Rule_class: "custom", |
| 179 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 180 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 181 | ctx.CreateBazelTargetModule(customBazelModuleFactory, m.Name(), props, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 182 | } |
| 183 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 184 | |
| 185 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 186 | // module to target. |
| 187 | func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) { |
| 188 | if m, ok := ctx.Module().(*customModule); ok { |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 189 | if !m.ConvertWithBp2build(ctx) { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 190 | return |
| 191 | } |
| 192 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 193 | baseName := m.Name() |
| 194 | attrs := &customBazelModuleAttributes{} |
| 195 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 196 | myLibraryProps := bazel.BazelTargetModuleProperties{ |
| 197 | Rule_class: "my_library", |
| 198 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
| 199 | } |
| 200 | ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName, myLibraryProps, attrs) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 201 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 202 | protoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 203 | Rule_class: "proto_library", |
| 204 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 205 | } |
| 206 | ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_proto_library_deps", protoLibraryProps, attrs) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 207 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 208 | myProtoLibraryProps := bazel.BazelTargetModuleProperties{ |
| 209 | Rule_class: "my_proto_library", |
| 210 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 211 | } |
| 212 | ctx.CreateBazelTargetModule(customBazelModuleFactory, baseName+"_my_proto_library_deps", myProtoLibraryProps, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 213 | } |
| 214 | } |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 215 | |
| 216 | // Helper method for tests to easily access the targets in a dir. |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 217 | func generateBazelTargetsForDir(codegenCtx *CodegenContext, dir string) BazelTargets { |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 218 | // TODO: Set generateFilegroups to true and/or remove the generateFilegroups argument completely |
Jingwen Chen | c63677b | 2021-06-17 05:43:19 +0000 | [diff] [blame] | 219 | buildFileToTargets, _, _ := GenerateBazelTargets(codegenCtx, false) |
Jingwen Chen | ba369ad | 2021-02-22 10:19:34 -0500 | [diff] [blame] | 220 | return buildFileToTargets[dir] |
| 221 | } |
Liz Kammer | 32b77cf | 2021-08-04 15:17:02 -0400 | [diff] [blame^] | 222 | |
| 223 | func registerCustomModuleForBp2buildConversion(ctx *android.TestContext) { |
| 224 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 225 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 226 | ctx.RegisterForBazelConversion() |
| 227 | } |