| 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 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 30 | type BaseSourceProvider struct { | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 31 | Properties SourceProviderProperties | 
|  | 32 |  | 
| Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 33 | // The first file in OutputFiles must be the library entry point. | 
| Andrew Walbran | c61652e | 2022-09-21 13:13:35 +0000 | [diff] [blame] | 34 | OutputFiles android.Paths | 
|  | 35 | subName     string | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 38 | var _ SourceProvider = (*BaseSourceProvider)(nil) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 39 |  | 
|  | 40 | type SourceProvider interface { | 
| Thiébaud Weksteen | 31f1bb8 | 2020-08-27 13:37:29 +0200 | [diff] [blame] | 41 | GenerateSource(ctx ModuleContext, deps PathDeps) android.Path | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 42 | Srcs() android.Paths | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 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) | 
| Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 46 | setOutputFiles(outputFiles android.Paths) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 47 | } | 
|  | 48 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 49 | func (sp *BaseSourceProvider) Srcs() android.Paths { | 
| Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 50 | return sp.OutputFiles | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Thiébaud Weksteen | 31f1bb8 | 2020-08-27 13:37:29 +0200 | [diff] [blame] | 53 | func (sp *BaseSourceProvider) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 54 | panic("BaseSourceProviderModule does not implement GenerateSource()") | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 57 | func (sp *BaseSourceProvider) SourceProviderProps() []interface{} { | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 58 | return []interface{}{&sp.Properties} | 
|  | 59 | } | 
|  | 60 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 61 | func NewSourceProvider() *BaseSourceProvider { | 
|  | 62 | return &BaseSourceProvider{ | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 63 | Properties: SourceProviderProperties{}, | 
|  | 64 | } | 
|  | 65 | } | 
|  | 66 |  | 
| Matthew Maurer | e94f3e7 | 2022-08-10 20:25:50 +0000 | [diff] [blame] | 67 | func NewSourceProviderModule(hod android.HostOrDeviceSupported, sourceProvider SourceProvider, enableLints bool, rlibOnly bool) *Module { | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 68 | _, library := NewRustLibrary(hod) | 
|  | 69 | library.BuildOnlyRust() | 
| Matthew Maurer | e94f3e7 | 2022-08-10 20:25:50 +0000 | [diff] [blame] | 70 | if rlibOnly { | 
|  | 71 | library.BuildOnlyRlib() | 
|  | 72 | } | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 73 | library.sourceProvider = sourceProvider | 
|  | 74 |  | 
|  | 75 | module := newModule(hod, android.MultilibBoth) | 
|  | 76 | module.sourceProvider = sourceProvider | 
|  | 77 | module.compiler = library | 
|  | 78 |  | 
|  | 79 | if !enableLints { | 
| Thiébaud Weksteen | 9e8451e | 2020-08-13 12:55:59 +0200 | [diff] [blame] | 80 | library.disableLints() | 
|  | 81 | module.disableClippy() | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | return module | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | func (sp *BaseSourceProvider) getStem(ctx android.ModuleContext) string { | 
| Ivan Lozano | ff3a5b3 | 2020-08-05 13:28:32 -0400 | [diff] [blame] | 88 | if String(sp.Properties.Source_stem) == "" { | 
|  | 89 | ctx.PropertyErrorf("source_stem", | 
|  | 90 | "source_stem property is undefined but required for rust_bindgen modules") | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 91 | } | 
| Ivan Lozano | ff3a5b3 | 2020-08-05 13:28:32 -0400 | [diff] [blame] | 92 | return String(sp.Properties.Source_stem) | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 95 | func (sp *BaseSourceProvider) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { | 
| Ivan Lozano | 4fef93c | 2020-07-08 08:39:44 -0400 | [diff] [blame] | 96 | return deps | 
|  | 97 | } | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 98 |  | 
| Andrei Homescu | c776792 | 2020-08-05 06:36:19 -0700 | [diff] [blame] | 99 | func (sp *BaseSourceProvider) setSubName(subName string) { | 
| Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame] | 100 | sp.subName = subName | 
|  | 101 | } | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 102 |  | 
| Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 103 | func (sp *BaseSourceProvider) setOutputFiles(outputFiles android.Paths) { | 
|  | 104 | sp.OutputFiles = outputFiles | 
| Thiébaud Weksteen | 295c72b | 2020-09-23 18:10:17 +0200 | [diff] [blame] | 105 | } |