Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | package bp2build |
| 2 | |
| 3 | import ( |
| 4 | "android/soong/android" |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 5 | "android/soong/bazel" |
| 6 | |
| 7 | "github.com/google/blueprint/proptools" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | type nestedProps struct { |
| 11 | Nested_prop string |
| 12 | } |
| 13 | |
| 14 | type customProps struct { |
| 15 | Bool_prop bool |
| 16 | Bool_ptr_prop *bool |
| 17 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 18 | Int_prop int `blueprint:"mutated"` |
| 19 | Int64_ptr_prop *int64 |
| 20 | String_prop string |
| 21 | String_ptr_prop *string |
| 22 | String_list_prop []string |
| 23 | |
| 24 | Nested_props nestedProps |
| 25 | Nested_props_ptr *nestedProps |
| 26 | } |
| 27 | |
| 28 | type customModule struct { |
| 29 | android.ModuleBase |
| 30 | |
| 31 | props customProps |
| 32 | } |
| 33 | |
| 34 | // OutputFiles is needed because some instances of this module use dist with a |
| 35 | // tag property which requires the module implements OutputFileProducer. |
| 36 | func (m *customModule) OutputFiles(tag string) (android.Paths, error) { |
| 37 | return android.PathsForTesting("path" + tag), nil |
| 38 | } |
| 39 | |
| 40 | func (m *customModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 41 | // nothing for now. |
| 42 | } |
| 43 | |
| 44 | func customModuleFactoryBase() android.Module { |
| 45 | module := &customModule{} |
| 46 | module.AddProperties(&module.props) |
| 47 | return module |
| 48 | } |
| 49 | |
| 50 | func customModuleFactory() android.Module { |
| 51 | m := customModuleFactoryBase() |
| 52 | android.InitAndroidModule(m) |
| 53 | return m |
| 54 | } |
| 55 | |
| 56 | type testProps struct { |
| 57 | Test_prop struct { |
| 58 | Test_string_prop string |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | type customTestModule struct { |
| 63 | android.ModuleBase |
| 64 | |
| 65 | props customProps |
| 66 | test_props testProps |
| 67 | } |
| 68 | |
| 69 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 70 | // nothing for now. |
| 71 | } |
| 72 | |
| 73 | func customTestModuleFactoryBase() android.Module { |
| 74 | m := &customTestModule{} |
| 75 | m.AddProperties(&m.props) |
| 76 | m.AddProperties(&m.test_props) |
| 77 | return m |
| 78 | } |
| 79 | |
| 80 | func customTestModuleFactory() android.Module { |
| 81 | m := customTestModuleFactoryBase() |
| 82 | android.InitAndroidModule(m) |
| 83 | return m |
| 84 | } |
| 85 | |
| 86 | type customDefaultsModule struct { |
| 87 | android.ModuleBase |
| 88 | android.DefaultsModuleBase |
| 89 | } |
| 90 | |
| 91 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 92 | module := &customDefaultsModule{} |
| 93 | module.AddProperties(&customProps{}) |
| 94 | return module |
| 95 | } |
| 96 | |
| 97 | func customDefaultsModuleFactoryBasic() android.Module { |
| 98 | return customDefaultsModuleFactoryBase() |
| 99 | } |
| 100 | |
| 101 | func customDefaultsModuleFactory() android.Module { |
| 102 | m := customDefaultsModuleFactoryBase() |
| 103 | android.InitDefaultsModule(m) |
| 104 | return m |
| 105 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 106 | |
| 107 | type customBazelModuleAttributes struct { |
| 108 | Name *string |
| 109 | String_prop string |
| 110 | String_list_prop []string |
| 111 | } |
| 112 | |
| 113 | type customBazelModule struct { |
| 114 | android.BazelTargetModuleBase |
| 115 | customBazelModuleAttributes |
| 116 | } |
| 117 | |
| 118 | func customBazelModuleFactory() android.Module { |
| 119 | module := &customBazelModule{} |
| 120 | module.AddProperties(&module.customBazelModuleAttributes) |
| 121 | android.InitBazelTargetModule(module) |
| 122 | return module |
| 123 | } |
| 124 | |
| 125 | func (m *customBazelModule) Name() string { return m.BaseModuleName() } |
| 126 | func (m *customBazelModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 127 | |
| 128 | func customBp2BuildMutator(ctx android.TopDownMutatorContext) { |
| 129 | if m, ok := ctx.Module().(*customModule); ok { |
| 130 | name := "__bp2build__" + m.Name() |
| 131 | ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{ |
| 132 | Name: proptools.StringPtr(name), |
| 133 | String_prop: m.props.String_prop, |
| 134 | String_list_prop: m.props.String_list_prop, |
| 135 | }, &bazel.BazelTargetModuleProperties{ |
| 136 | Rule_class: "custom", |
| 137 | }) |
| 138 | } |
| 139 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame^] | 140 | |
| 141 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 142 | // module to target. |
| 143 | func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) { |
| 144 | if m, ok := ctx.Module().(*customModule); ok { |
| 145 | baseName := "__bp2build__" + m.Name() |
| 146 | ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{ |
| 147 | Name: proptools.StringPtr(baseName), |
| 148 | }, &bazel.BazelTargetModuleProperties{ |
| 149 | Rule_class: "my_library", |
| 150 | Bzl_load_location: "//build/bazel/rules:rules.bzl", |
| 151 | }) |
| 152 | ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{ |
| 153 | Name: proptools.StringPtr(baseName + "_proto_library_deps"), |
| 154 | }, &bazel.BazelTargetModuleProperties{ |
| 155 | Rule_class: "proto_library", |
| 156 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 157 | }) |
| 158 | ctx.CreateModule(customBazelModuleFactory, &customBazelModuleAttributes{ |
| 159 | Name: proptools.StringPtr(baseName + "_my_proto_library_deps"), |
| 160 | }, &bazel.BazelTargetModuleProperties{ |
| 161 | Rule_class: "my_proto_library", |
| 162 | Bzl_load_location: "//build/bazel/rules:proto.bzl", |
| 163 | }) |
| 164 | } |
| 165 | } |