blob: e35e510daa887ba78036ad9adbe44de405d95a22 [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 Maurer1d8e20d2023-11-20 21:18:12 +000079func 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 Maurerc761eec2020-06-25 00:47:46 -070090func PrebuiltLibraryFactory() android.Module {
91 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
92 return module.Init()
93}
94
Ivan Lozanoffee3342019-08-27 12:03:00 -070095func PrebuiltDylibFactory() android.Module {
96 module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
97 return module.Init()
98}
99
Matthew Maurerc761eec2020-06-25 00:47:46 -0700100func PrebuiltRlibFactory() android.Module {
101 module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
102 return module.Init()
103}
104
Ivan Lozano872d5792022-03-23 17:31:39 -0400105func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500106 srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
107 return prebuilt.prebuiltSrcs()
108 }
109 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
110}
111
Matthew Maurerc761eec2020-06-25 00:47:46 -0700112func 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 Lozanofba2aa22021-11-11 09:29:07 -0500120
121 addSrcSupplier(module, prebuilt)
122
Matthew Maurerc761eec2020-06-25 00:47:46 -0700123 return module, prebuilt
124}
125
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
127 module, library := NewRustLibrary(hod)
128 library.BuildOnlyDylib()
Matthew Maurer99020b02019-10-31 10:44:40 -0700129 library.setNoStdlibs()
Matthew Maurerc761eec2020-06-25 00:47:46 -0700130 prebuilt := &prebuiltLibraryDecorator{
131 libraryDecorator: library,
132 }
133 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500134
135 addSrcSupplier(module, prebuilt)
136
Matthew Maurerc761eec2020-06-25 00:47:46 -0700137 return module, prebuilt
138}
139
140func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
141 module, library := NewRustLibrary(hod)
142 library.BuildOnlyRlib()
143 library.setNoStdlibs()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 prebuilt := &prebuiltLibraryDecorator{
145 libraryDecorator: library,
146 }
147 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500148
149 addSrcSupplier(module, prebuilt)
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 return module, prebuilt
152}
153
154func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
Matthew Maurer128f53b2020-06-29 13:31:04 -0700155 return append(prebuilt.libraryDecorator.compilerProps(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 &prebuilt.Properties)
157}
158
Sasha Smundaka76acba2022-04-18 20:12:56 -0700159func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000160 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700161 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000162 srcPath := prebuiltPath(ctx, prebuilt)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400163 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700164 return buildOutput{outputFile: srcPath}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700166
Dan Albert06feee92021-03-19 15:06:02 -0700167func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
168 deps PathDeps) android.OptionalPath {
169
170 return android.OptionalPath{}
171}
172
Ivan Lozanof1c84332019-09-20 11:00:37 -0700173func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
174 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
175 return deps
176}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400177
178func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
179 return false
180}
Matthew Maurerc761eec2020-06-25 00:47:46 -0700181
182func (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 Lozanofba2aa22021-11-11 09:29:07 -0500193
194func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
195 return &prebuilt.Prebuilt
196}
Ivan Lozano872d5792022-03-23 17:31:39 -0400197
198func (prebuilt *prebuiltProcMacroDecorator) prebuiltSrcs() []string {
199 srcs := prebuilt.Properties.Srcs
200 return srcs
201}
202
203func (prebuilt *prebuiltProcMacroDecorator) prebuilt() *android.Prebuilt {
204 return &prebuilt.Prebuilt
205}
206
207func (prebuilt *prebuiltProcMacroDecorator) compilerProps() []interface{} {
208 return append(prebuilt.procMacroDecorator.compilerProps(),
209 &prebuilt.Properties)
210}
211
Sasha Smundaka76acba2022-04-18 20:12:56 -0700212func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000213 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
Ivan Lozano872d5792022-03-23 17:31:39 -0400214 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000215 srcPath := prebuiltPath(ctx, prebuilt)
Ivan Lozano872d5792022-03-23 17:31:39 -0400216 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}