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