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" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | type nestedProps struct { |
| 9 | Nested_prop string |
| 10 | } |
| 11 | |
| 12 | type customProps struct { |
| 13 | Bool_prop bool |
| 14 | Bool_ptr_prop *bool |
| 15 | // Ensure that properties tagged `blueprint:mutated` are omitted |
| 16 | Int_prop int `blueprint:"mutated"` |
| 17 | Int64_ptr_prop *int64 |
| 18 | String_prop string |
| 19 | String_ptr_prop *string |
| 20 | String_list_prop []string |
| 21 | |
| 22 | Nested_props nestedProps |
| 23 | Nested_props_ptr *nestedProps |
| 24 | } |
| 25 | |
| 26 | type customModule struct { |
| 27 | android.ModuleBase |
| 28 | |
| 29 | props customProps |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 30 | |
| 31 | bazelProps bazel.Properties |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 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) |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 47 | module.AddProperties(&module.bazelProps) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 48 | return module |
| 49 | } |
| 50 | |
| 51 | func customModuleFactory() android.Module { |
| 52 | m := customModuleFactoryBase() |
| 53 | android.InitAndroidModule(m) |
| 54 | return m |
| 55 | } |
| 56 | |
| 57 | type testProps struct { |
| 58 | Test_prop struct { |
| 59 | Test_string_prop string |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | type customTestModule struct { |
| 64 | android.ModuleBase |
| 65 | |
| 66 | props customProps |
| 67 | test_props testProps |
| 68 | } |
| 69 | |
| 70 | func (m *customTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 71 | // nothing for now. |
| 72 | } |
| 73 | |
| 74 | func customTestModuleFactoryBase() android.Module { |
| 75 | m := &customTestModule{} |
| 76 | m.AddProperties(&m.props) |
| 77 | m.AddProperties(&m.test_props) |
| 78 | return m |
| 79 | } |
| 80 | |
| 81 | func customTestModuleFactory() android.Module { |
| 82 | m := customTestModuleFactoryBase() |
| 83 | android.InitAndroidModule(m) |
| 84 | return m |
| 85 | } |
| 86 | |
| 87 | type customDefaultsModule struct { |
| 88 | android.ModuleBase |
| 89 | android.DefaultsModuleBase |
| 90 | } |
| 91 | |
| 92 | func customDefaultsModuleFactoryBase() android.DefaultsModule { |
| 93 | module := &customDefaultsModule{} |
| 94 | module.AddProperties(&customProps{}) |
| 95 | return module |
| 96 | } |
| 97 | |
| 98 | func customDefaultsModuleFactoryBasic() android.Module { |
| 99 | return customDefaultsModuleFactoryBase() |
| 100 | } |
| 101 | |
| 102 | func customDefaultsModuleFactory() android.Module { |
| 103 | m := customDefaultsModuleFactoryBase() |
| 104 | android.InitDefaultsModule(m) |
| 105 | return m |
| 106 | } |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 107 | |
| 108 | type customBazelModuleAttributes struct { |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 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 { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 130 | if !m.bazelProps.Bazel_module.Bp2build_available { |
| 131 | return |
| 132 | } |
| 133 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 134 | attrs := &customBazelModuleAttributes{ |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 135 | String_prop: m.props.String_prop, |
| 136 | String_list_prop: m.props.String_list_prop, |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 139 | props := bazel.NewBazelTargetModuleProperties(m.Name(), "custom", "") |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 140 | |
| 141 | ctx.CreateBazelTargetModule(customBazelModuleFactory, props, attrs) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 144 | |
| 145 | // A bp2build mutator that uses load statements and creates a 1:M mapping from |
| 146 | // module to target. |
| 147 | func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) { |
| 148 | if m, ok := ctx.Module().(*customModule); ok { |
Jingwen Chen | 77e8b7b | 2021-02-05 03:03:24 -0500 | [diff] [blame] | 149 | if !m.bazelProps.Bazel_module.Bp2build_available { |
| 150 | return |
| 151 | } |
| 152 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 153 | baseName := m.Name() |
| 154 | attrs := &customBazelModuleAttributes{} |
| 155 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 156 | myLibraryProps := bazel.NewBazelTargetModuleProperties( |
| 157 | baseName, |
| 158 | "my_library", |
| 159 | "//build/bazel/rules:rules.bzl", |
| 160 | ) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 161 | ctx.CreateBazelTargetModule(customBazelModuleFactory, myLibraryProps, attrs) |
| 162 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 163 | protoLibraryProps := bazel.NewBazelTargetModuleProperties( |
| 164 | baseName+"_proto_library_deps", |
| 165 | "proto_library", |
| 166 | "//build/bazel/rules:proto.bzl", |
| 167 | ) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 168 | ctx.CreateBazelTargetModule(customBazelModuleFactory, protoLibraryProps, attrs) |
| 169 | |
Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 170 | myProtoLibraryProps := bazel.NewBazelTargetModuleProperties( |
| 171 | baseName+"_my_proto_library_deps", |
| 172 | "my_proto_library", |
| 173 | "//build/bazel/rules:proto.bzl", |
| 174 | ) |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 175 | ctx.CreateBazelTargetModule(customBazelModuleFactory, myProtoLibraryProps, attrs) |
Jingwen Chen | 40067de | 2021-01-26 21:58:43 -0500 | [diff] [blame] | 176 | } |
| 177 | } |