Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | func init() { |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 22 | android.RegisterModuleType("rust_prebuilt_library", PrebuiltLibraryFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 23 | android.RegisterModuleType("rust_prebuilt_dylib", PrebuiltDylibFactory) |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 24 | android.RegisterModuleType("rust_prebuilt_rlib", PrebuiltRlibFactory) |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame^] | 25 | android.RegisterModuleType("rust_prebuilt_proc_macro", PrebuiltProcMacroFactory) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type PrebuiltProperties struct { |
| 29 | // path to the prebuilt file |
| 30 | Srcs []string `android:"path,arch_variant"` |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 31 | // directories containing associated rlib dependencies |
| 32 | Link_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type prebuiltLibraryDecorator struct { |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame] | 36 | android.Prebuilt |
| 37 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 38 | *libraryDecorator |
| 39 | Properties PrebuiltProperties |
| 40 | } |
| 41 | |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame^] | 42 | type prebuiltProcMacroDecorator struct { |
| 43 | android.Prebuilt |
| 44 | |
| 45 | *procMacroDecorator |
| 46 | Properties PrebuiltProperties |
| 47 | } |
| 48 | |
| 49 | func PrebuiltProcMacroFactory() android.Module { |
| 50 | module, _ := NewPrebuiltProcMacro(android.HostSupportedNoCross) |
| 51 | return module.Init() |
| 52 | } |
| 53 | |
| 54 | type rustPrebuilt interface { |
| 55 | prebuiltSrcs() []string |
| 56 | prebuilt() *android.Prebuilt |
| 57 | } |
| 58 | |
| 59 | func NewPrebuiltProcMacro(hod android.HostOrDeviceSupported) (*Module, *prebuiltProcMacroDecorator) { |
| 60 | module, library := NewProcMacro(hod) |
| 61 | prebuilt := &prebuiltProcMacroDecorator{ |
| 62 | procMacroDecorator: library, |
| 63 | } |
| 64 | module.compiler = prebuilt |
| 65 | |
| 66 | addSrcSupplier(module, prebuilt) |
| 67 | |
| 68 | return module, prebuilt |
| 69 | } |
| 70 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 71 | var _ compiler = (*prebuiltLibraryDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 72 | var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil) |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame^] | 73 | var _ rustPrebuilt = (*prebuiltLibraryDecorator)(nil) |
| 74 | |
| 75 | var _ compiler = (*prebuiltProcMacroDecorator)(nil) |
| 76 | var _ exportedFlagsProducer = (*prebuiltProcMacroDecorator)(nil) |
| 77 | var _ rustPrebuilt = (*prebuiltProcMacroDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 78 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 79 | func PrebuiltLibraryFactory() android.Module { |
| 80 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 81 | return module.Init() |
| 82 | } |
| 83 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 84 | func PrebuiltDylibFactory() android.Module { |
| 85 | module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported) |
| 86 | return module.Init() |
| 87 | } |
| 88 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 89 | func PrebuiltRlibFactory() android.Module { |
| 90 | module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported) |
| 91 | return module.Init() |
| 92 | } |
| 93 | |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame^] | 94 | func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) { |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame] | 95 | srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string { |
| 96 | return prebuilt.prebuiltSrcs() |
| 97 | } |
| 98 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
| 99 | } |
| 100 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 101 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) { |
| 102 | module, library := NewRustLibrary(hod) |
| 103 | library.BuildOnlyRust() |
| 104 | library.setNoStdlibs() |
| 105 | prebuilt := &prebuiltLibraryDecorator{ |
| 106 | libraryDecorator: library, |
| 107 | } |
| 108 | module.compiler = prebuilt |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame] | 109 | |
| 110 | addSrcSupplier(module, prebuilt) |
| 111 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 112 | return module, prebuilt |
| 113 | } |
| 114 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 115 | func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) { |
| 116 | module, library := NewRustLibrary(hod) |
| 117 | library.BuildOnlyDylib() |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 118 | library.setNoStdlibs() |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 119 | prebuilt := &prebuiltLibraryDecorator{ |
| 120 | libraryDecorator: library, |
| 121 | } |
| 122 | module.compiler = prebuilt |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame] | 123 | |
| 124 | addSrcSupplier(module, prebuilt) |
| 125 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 126 | return module, prebuilt |
| 127 | } |
| 128 | |
| 129 | func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) { |
| 130 | module, library := NewRustLibrary(hod) |
| 131 | library.BuildOnlyRlib() |
| 132 | library.setNoStdlibs() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 133 | prebuilt := &prebuiltLibraryDecorator{ |
| 134 | libraryDecorator: library, |
| 135 | } |
| 136 | module.compiler = prebuilt |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame] | 137 | |
| 138 | addSrcSupplier(module, prebuilt) |
| 139 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 140 | return module, prebuilt |
| 141 | } |
| 142 | |
| 143 | func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} { |
Matthew Maurer | 128f53b | 2020-06-29 13:31:04 -0700 | [diff] [blame] | 144 | return append(prebuilt.libraryDecorator.compilerProps(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 145 | &prebuilt.Properties) |
| 146 | } |
| 147 | |
| 148 | func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 149 | prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...) |
| 150 | prebuilt.flagExporter.setProvider(ctx) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 151 | |
Chih-Hung Hsieh | bbd25ae | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 152 | srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs()) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 153 | if len(paths) > 0 { |
| 154 | ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)") |
| 155 | } |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 156 | prebuilt.baseCompiler.unstrippedOutputFile = srcPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 157 | return srcPath |
| 158 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 159 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 160 | func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, |
| 161 | deps PathDeps) android.OptionalPath { |
| 162 | |
| 163 | return android.OptionalPath{} |
| 164 | } |
| 165 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 166 | func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 167 | deps = prebuilt.baseCompiler.compilerDeps(ctx, deps) |
| 168 | return deps |
| 169 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 170 | |
| 171 | func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool { |
| 172 | return false |
| 173 | } |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 174 | |
| 175 | func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string { |
| 176 | srcs := prebuilt.Properties.Srcs |
| 177 | if prebuilt.rlib() { |
| 178 | srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...) |
| 179 | } |
| 180 | if prebuilt.dylib() { |
| 181 | srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...) |
| 182 | } |
| 183 | |
| 184 | return srcs |
| 185 | } |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame] | 186 | |
| 187 | func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt { |
| 188 | return &prebuilt.Prebuilt |
| 189 | } |
Ivan Lozano | 872d579 | 2022-03-23 17:31:39 -0400 | [diff] [blame^] | 190 | |
| 191 | func (prebuilt *prebuiltProcMacroDecorator) prebuiltSrcs() []string { |
| 192 | srcs := prebuilt.Properties.Srcs |
| 193 | return srcs |
| 194 | } |
| 195 | |
| 196 | func (prebuilt *prebuiltProcMacroDecorator) prebuilt() *android.Prebuilt { |
| 197 | return &prebuilt.Prebuilt |
| 198 | } |
| 199 | |
| 200 | func (prebuilt *prebuiltProcMacroDecorator) compilerProps() []interface{} { |
| 201 | return append(prebuilt.procMacroDecorator.compilerProps(), |
| 202 | &prebuilt.Properties) |
| 203 | } |
| 204 | |
| 205 | func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 206 | prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...) |
| 207 | prebuilt.flagExporter.setProvider(ctx) |
| 208 | |
| 209 | srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs()) |
| 210 | if len(paths) > 0 { |
| 211 | ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)") |
| 212 | } |
| 213 | prebuilt.baseCompiler.unstrippedOutputFile = srcPath |
| 214 | return srcPath |
| 215 | } |
| 216 | |
| 217 | func (prebuilt *prebuiltProcMacroDecorator) rustdoc(ctx ModuleContext, flags Flags, |
| 218 | deps PathDeps) android.OptionalPath { |
| 219 | |
| 220 | return android.OptionalPath{} |
| 221 | } |
| 222 | |
| 223 | func (prebuilt *prebuiltProcMacroDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 224 | deps = prebuilt.baseCompiler.compilerDeps(ctx, deps) |
| 225 | return deps |
| 226 | } |
| 227 | |
| 228 | func (prebuilt *prebuiltProcMacroDecorator) nativeCoverage() bool { |
| 229 | return false |
| 230 | } |