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