Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 1 | // Copyright 2020 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | ) |
| 20 | |
| 21 | type SourceProviderProperties struct { |
Ivan Lozano | ff3a5b3 | 2020-08-05 13:28:32 -0400 | [diff] [blame^] | 22 | // filename for the generated source file (<source_stem>.rs). This field is required. |
| 23 | // The inherited "stem" property sets the output filename for the generated library variants only. |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 24 | Source_stem *string `android:"arch_variant"` |
| 25 | |
| 26 | // crate name, used for the library variant of this source provider. See additional details in rust_library. |
| 27 | Crate_name string `android:"arch_variant"` |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | type baseSourceProvider struct { |
| 31 | Properties SourceProviderProperties |
| 32 | |
| 33 | outputFile android.Path |
| 34 | subAndroidMkOnce map[subAndroidMkProvider]bool |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 35 | subName string |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var _ SourceProvider = (*baseSourceProvider)(nil) |
| 39 | |
| 40 | type SourceProvider interface { |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 41 | generateSource(ctx android.ModuleContext, deps PathDeps) android.Path |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 42 | Srcs() android.Paths |
| 43 | sourceProviderProps() []interface{} |
| 44 | sourceProviderDeps(ctx DepsContext, deps Deps) Deps |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 45 | setSubName(subName string) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func (sp *baseSourceProvider) Srcs() android.Paths { |
| 49 | return android.Paths{sp.outputFile} |
| 50 | } |
| 51 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 52 | func (sp *baseSourceProvider) generateSource(ctx android.ModuleContext, deps PathDeps) android.Path { |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 53 | panic("baseSourceProviderModule does not implement generateSource()") |
| 54 | } |
| 55 | |
| 56 | func (sp *baseSourceProvider) sourceProviderProps() []interface{} { |
| 57 | return []interface{}{&sp.Properties} |
| 58 | } |
| 59 | |
| 60 | func NewSourceProvider() *baseSourceProvider { |
| 61 | return &baseSourceProvider{ |
| 62 | Properties: SourceProviderProperties{}, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func (sp *baseSourceProvider) getStem(ctx android.ModuleContext) string { |
Ivan Lozano | ff3a5b3 | 2020-08-05 13:28:32 -0400 | [diff] [blame^] | 67 | if String(sp.Properties.Source_stem) == "" { |
| 68 | ctx.PropertyErrorf("source_stem", |
| 69 | "source_stem property is undefined but required for rust_bindgen modules") |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 70 | } |
Ivan Lozano | ff3a5b3 | 2020-08-05 13:28:32 -0400 | [diff] [blame^] | 71 | return String(sp.Properties.Source_stem) |
Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func (sp *baseSourceProvider) sourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 75 | return deps |
| 76 | } |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 77 | |
| 78 | func (sp *baseSourceProvider) setSubName(subName string) { |
| 79 | sp.subName = subName |
| 80 | } |