blob: fe9d0b5ddb18fb4bff8a0ef8e8ff87d5766a187b [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 {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000149 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700150 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700151
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700152 srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
Ivan Lozano43845682020-07-09 21:03:28 -0400153 if len(paths) > 0 {
154 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
155 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400156 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700157 return buildOutput{outputFile: srcPath}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700159
Dan Albert06feee92021-03-19 15:06:02 -0700160func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
161 deps PathDeps) android.OptionalPath {
162
163 return android.OptionalPath{}
164}
165
Ivan Lozanof1c84332019-09-20 11:00:37 -0700166func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
167 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
168 return deps
169}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400170
171func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
172 return false
173}
Matthew Maurerc761eec2020-06-25 00:47:46 -0700174
175func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string {
176 srcs := prebuilt.Properties.Srcs
177 if prebuilt.rlib() {
178 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...)
179 }
180 if prebuilt.dylib() {
181 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...)
182 }
183
184 return srcs
185}
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500186
187func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
188 return &prebuilt.Prebuilt
189}
Ivan Lozano872d5792022-03-23 17:31:39 -0400190
191func (prebuilt *prebuiltProcMacroDecorator) prebuiltSrcs() []string {
192 srcs := prebuilt.Properties.Srcs
193 return srcs
194}
195
196func (prebuilt *prebuiltProcMacroDecorator) prebuilt() *android.Prebuilt {
197 return &prebuilt.Prebuilt
198}
199
200func (prebuilt *prebuiltProcMacroDecorator) compilerProps() []interface{} {
201 return append(prebuilt.procMacroDecorator.compilerProps(),
202 &prebuilt.Properties)
203}
204
Sasha Smundaka76acba2022-04-18 20:12:56 -0700205func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000206 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
Ivan Lozano872d5792022-03-23 17:31:39 -0400207 prebuilt.flagExporter.setProvider(ctx)
208
209 srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
210 if len(paths) > 0 {
211 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
212 }
213 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700214 return buildOutput{outputFile: srcPath}
Ivan Lozano872d5792022-03-23 17:31:39 -0400215}
216
217func (prebuilt *prebuiltProcMacroDecorator) rustdoc(ctx ModuleContext, flags Flags,
218 deps PathDeps) android.OptionalPath {
219
220 return android.OptionalPath{}
221}
222
223func (prebuilt *prebuiltProcMacroDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
224 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
225 return deps
226}
227
228func (prebuilt *prebuiltProcMacroDecorator) nativeCoverage() bool {
229 return false
230}