blob: d012680f031a3726521521a42898d593cde93382 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package rust
16
17import (
18 "android/soong/android"
19)
20
21func init() {
Matthew Maurerc761eec2020-06-25 00:47:46 -070022 android.RegisterModuleType("rust_prebuilt_library", PrebuiltLibraryFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070023 android.RegisterModuleType("rust_prebuilt_dylib", PrebuiltDylibFactory)
Matthew Maurerc761eec2020-06-25 00:47:46 -070024 android.RegisterModuleType("rust_prebuilt_rlib", PrebuiltRlibFactory)
Ivan Lozano872d5792022-03-23 17:31:39 -040025 android.RegisterModuleType("rust_prebuilt_proc_macro", PrebuiltProcMacroFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070026}
27
28type PrebuiltProperties struct {
29 // path to the prebuilt file
30 Srcs []string `android:"path,arch_variant"`
Matthew Maurerbb3add12020-06-25 09:34:12 -070031 // directories containing associated rlib dependencies
32 Link_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070033}
34
35type prebuiltLibraryDecorator struct {
Ivan Lozanofba2aa22021-11-11 09:29:07 -050036 android.Prebuilt
37
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 *libraryDecorator
39 Properties PrebuiltProperties
40}
41
Ivan Lozano872d5792022-03-23 17:31:39 -040042type prebuiltProcMacroDecorator struct {
43 android.Prebuilt
44
45 *procMacroDecorator
46 Properties PrebuiltProperties
47}
48
49func PrebuiltProcMacroFactory() android.Module {
50 module, _ := NewPrebuiltProcMacro(android.HostSupportedNoCross)
51 return module.Init()
52}
53
54type rustPrebuilt interface {
55 prebuiltSrcs() []string
56 prebuilt() *android.Prebuilt
57}
58
59func 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 Lozanoffee3342019-08-27 12:03:00 -070071var _ compiler = (*prebuiltLibraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070072var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -040073var _ rustPrebuilt = (*prebuiltLibraryDecorator)(nil)
74
75var _ compiler = (*prebuiltProcMacroDecorator)(nil)
76var _ exportedFlagsProducer = (*prebuiltProcMacroDecorator)(nil)
77var _ rustPrebuilt = (*prebuiltProcMacroDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070078
Matthew Maurerc761eec2020-06-25 00:47:46 -070079func PrebuiltLibraryFactory() android.Module {
80 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
81 return module.Init()
82}
83
Ivan Lozanoffee3342019-08-27 12:03:00 -070084func PrebuiltDylibFactory() android.Module {
85 module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
86 return module.Init()
87}
88
Matthew Maurerc761eec2020-06-25 00:47:46 -070089func PrebuiltRlibFactory() android.Module {
90 module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
91 return module.Init()
92}
93
Ivan Lozano872d5792022-03-23 17:31:39 -040094func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) {
Ivan Lozanofba2aa22021-11-11 09:29:07 -050095 srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
96 return prebuilt.prebuiltSrcs()
97 }
98 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
99}
100
Matthew Maurerc761eec2020-06-25 00:47:46 -0700101func 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 Lozanofba2aa22021-11-11 09:29:07 -0500109
110 addSrcSupplier(module, prebuilt)
111
Matthew Maurerc761eec2020-06-25 00:47:46 -0700112 return module, prebuilt
113}
114
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
116 module, library := NewRustLibrary(hod)
117 library.BuildOnlyDylib()
Matthew Maurer99020b02019-10-31 10:44:40 -0700118 library.setNoStdlibs()
Matthew Maurerc761eec2020-06-25 00:47:46 -0700119 prebuilt := &prebuiltLibraryDecorator{
120 libraryDecorator: library,
121 }
122 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500123
124 addSrcSupplier(module, prebuilt)
125
Matthew Maurerc761eec2020-06-25 00:47:46 -0700126 return module, prebuilt
127}
128
129func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
130 module, library := NewRustLibrary(hod)
131 library.BuildOnlyRlib()
132 library.setNoStdlibs()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133 prebuilt := &prebuiltLibraryDecorator{
134 libraryDecorator: library,
135 }
136 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500137
138 addSrcSupplier(module, prebuilt)
139
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 return module, prebuilt
141}
142
143func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
Matthew Maurer128f53b2020-06-29 13:31:04 -0700144 return append(prebuilt.libraryDecorator.compilerProps(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 &prebuilt.Properties)
146}
147
Sasha Smundaka76acba2022-04-18 20:12:56 -0700148func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Sam Delmericoa588d152023-06-16 10:28:04 -0400149 deps.linkDirs = append(deps.linkDirs, android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs)...)
150 prebuilt.flagExporter.exportLinkDirs(deps.linkDirs...)
151 prebuilt.flagExporter.exportLinkObjects(deps.linkObjects...)
152 prebuilt.flagExporter.exportLibDeps(deps.LibDeps...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700153 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700154
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700155 srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
Ivan Lozano43845682020-07-09 21:03:28 -0400156 if len(paths) > 0 {
157 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
158 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400159 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700160 return buildOutput{outputFile: srcPath}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700162
Dan Albert06feee92021-03-19 15:06:02 -0700163func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
164 deps PathDeps) android.OptionalPath {
165
166 return android.OptionalPath{}
167}
168
Ivan Lozanof1c84332019-09-20 11:00:37 -0700169func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
170 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
171 return deps
172}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400173
174func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
175 return false
176}
Matthew Maurerc761eec2020-06-25 00:47:46 -0700177
178func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string {
179 srcs := prebuilt.Properties.Srcs
180 if prebuilt.rlib() {
181 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...)
182 }
183 if prebuilt.dylib() {
184 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...)
185 }
186
187 return srcs
188}
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500189
190func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
191 return &prebuilt.Prebuilt
192}
Ivan Lozano872d5792022-03-23 17:31:39 -0400193
194func (prebuilt *prebuiltProcMacroDecorator) prebuiltSrcs() []string {
195 srcs := prebuilt.Properties.Srcs
196 return srcs
197}
198
199func (prebuilt *prebuiltProcMacroDecorator) prebuilt() *android.Prebuilt {
200 return &prebuilt.Prebuilt
201}
202
203func (prebuilt *prebuiltProcMacroDecorator) compilerProps() []interface{} {
204 return append(prebuilt.procMacroDecorator.compilerProps(),
205 &prebuilt.Properties)
206}
207
Sasha Smundaka76acba2022-04-18 20:12:56 -0700208func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Sam Delmericoa588d152023-06-16 10:28:04 -0400209 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs)...)
Ivan Lozano872d5792022-03-23 17:31:39 -0400210 prebuilt.flagExporter.setProvider(ctx)
211
212 srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
213 if len(paths) > 0 {
214 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
215 }
216 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700217 return buildOutput{outputFile: srcPath}
Ivan Lozano872d5792022-03-23 17:31:39 -0400218}
219
220func (prebuilt *prebuiltProcMacroDecorator) rustdoc(ctx ModuleContext, flags Flags,
221 deps PathDeps) android.OptionalPath {
222
223 return android.OptionalPath{}
224}
225
226func (prebuilt *prebuiltProcMacroDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
227 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
228 return deps
229}
230
231func (prebuilt *prebuiltProcMacroDecorator) nativeCoverage() bool {
232 return false
233}