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 | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | type PrebuiltProperties struct { |
| 28 | // path to the prebuilt file |
| 29 | Srcs []string `android:"path,arch_variant"` |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 30 | // directories containing associated rlib dependencies |
| 31 | Link_dirs []string `android:"path,arch_variant"` |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type prebuiltLibraryDecorator struct { |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame^] | 35 | android.Prebuilt |
| 36 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 37 | *libraryDecorator |
| 38 | Properties PrebuiltProperties |
| 39 | } |
| 40 | |
| 41 | var _ compiler = (*prebuiltLibraryDecorator)(nil) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 42 | var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 43 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 44 | func PrebuiltLibraryFactory() android.Module { |
| 45 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 46 | return module.Init() |
| 47 | } |
| 48 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 | func PrebuiltDylibFactory() android.Module { |
| 50 | module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported) |
| 51 | return module.Init() |
| 52 | } |
| 53 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 54 | func PrebuiltRlibFactory() android.Module { |
| 55 | module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported) |
| 56 | return module.Init() |
| 57 | } |
| 58 | |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame^] | 59 | func addSrcSupplier(module android.PrebuiltInterface, prebuilt *prebuiltLibraryDecorator) { |
| 60 | srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string { |
| 61 | return prebuilt.prebuiltSrcs() |
| 62 | } |
| 63 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
| 64 | } |
| 65 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 66 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) { |
| 67 | module, library := NewRustLibrary(hod) |
| 68 | library.BuildOnlyRust() |
| 69 | library.setNoStdlibs() |
| 70 | prebuilt := &prebuiltLibraryDecorator{ |
| 71 | libraryDecorator: library, |
| 72 | } |
| 73 | module.compiler = prebuilt |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame^] | 74 | |
| 75 | addSrcSupplier(module, prebuilt) |
| 76 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 77 | return module, prebuilt |
| 78 | } |
| 79 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 80 | func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) { |
| 81 | module, library := NewRustLibrary(hod) |
| 82 | library.BuildOnlyDylib() |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 83 | library.setNoStdlibs() |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 84 | prebuilt := &prebuiltLibraryDecorator{ |
| 85 | libraryDecorator: library, |
| 86 | } |
| 87 | module.compiler = prebuilt |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame^] | 88 | |
| 89 | addSrcSupplier(module, prebuilt) |
| 90 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 91 | return module, prebuilt |
| 92 | } |
| 93 | |
| 94 | func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) { |
| 95 | module, library := NewRustLibrary(hod) |
| 96 | library.BuildOnlyRlib() |
| 97 | library.setNoStdlibs() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 98 | prebuilt := &prebuiltLibraryDecorator{ |
| 99 | libraryDecorator: library, |
| 100 | } |
| 101 | module.compiler = prebuilt |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame^] | 102 | |
| 103 | addSrcSupplier(module, prebuilt) |
| 104 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 105 | return module, prebuilt |
| 106 | } |
| 107 | |
| 108 | func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} { |
Matthew Maurer | 128f53b | 2020-06-29 13:31:04 -0700 | [diff] [blame] | 109 | return append(prebuilt.libraryDecorator.compilerProps(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 110 | &prebuilt.Properties) |
| 111 | } |
| 112 | |
| 113 | func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 114 | prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...) |
| 115 | prebuilt.flagExporter.setProvider(ctx) |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 116 | |
Chih-Hung Hsieh | bbd25ae | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 117 | srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs()) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 118 | if len(paths) > 0 { |
| 119 | ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)") |
| 120 | } |
Ivan Lozano | 8d10fc3 | 2021-11-05 16:36:47 -0400 | [diff] [blame] | 121 | prebuilt.baseCompiler.unstrippedOutputFile = srcPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 122 | return srcPath |
| 123 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 124 | |
Dan Albert | 06feee9 | 2021-03-19 15:06:02 -0700 | [diff] [blame] | 125 | func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags, |
| 126 | deps PathDeps) android.OptionalPath { |
| 127 | |
| 128 | return android.OptionalPath{} |
| 129 | } |
| 130 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 131 | func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 132 | deps = prebuilt.baseCompiler.compilerDeps(ctx, deps) |
| 133 | return deps |
| 134 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 135 | |
| 136 | func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool { |
| 137 | return false |
| 138 | } |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 139 | |
| 140 | func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string { |
| 141 | srcs := prebuilt.Properties.Srcs |
| 142 | if prebuilt.rlib() { |
| 143 | srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...) |
| 144 | } |
| 145 | if prebuilt.dylib() { |
| 146 | srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...) |
| 147 | } |
| 148 | |
| 149 | return srcs |
| 150 | } |
Ivan Lozano | fba2aa2 | 2021-11-11 09:29:07 -0500 | [diff] [blame^] | 151 | |
| 152 | func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt { |
| 153 | return &prebuilt.Prebuilt |
| 154 | } |