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