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