blob: 41f94238cb0ef430f7061c30ca543eb1110c3ae0 [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"`
Colin Crossd8d8b852024-12-20 16:32:37 -080033
34 Force_use_prebuilt *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070035}
36
37type prebuiltLibraryDecorator struct {
Ivan Lozanofba2aa22021-11-11 09:29:07 -050038 android.Prebuilt
39
Ivan Lozanoffee3342019-08-27 12:03:00 -070040 *libraryDecorator
41 Properties PrebuiltProperties
42}
43
Ivan Lozano872d5792022-03-23 17:31:39 -040044type prebuiltProcMacroDecorator struct {
45 android.Prebuilt
46
47 *procMacroDecorator
48 Properties PrebuiltProperties
49}
50
51func PrebuiltProcMacroFactory() android.Module {
52 module, _ := NewPrebuiltProcMacro(android.HostSupportedNoCross)
53 return module.Init()
54}
55
56type rustPrebuilt interface {
57 prebuiltSrcs() []string
58 prebuilt() *android.Prebuilt
59}
60
61func NewPrebuiltProcMacro(hod android.HostOrDeviceSupported) (*Module, *prebuiltProcMacroDecorator) {
62 module, library := NewProcMacro(hod)
63 prebuilt := &prebuiltProcMacroDecorator{
64 procMacroDecorator: library,
65 }
66 module.compiler = prebuilt
67
68 addSrcSupplier(module, prebuilt)
69
70 return module, prebuilt
71}
72
Ivan Lozanoffee3342019-08-27 12:03:00 -070073var _ compiler = (*prebuiltLibraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070074var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -040075var _ rustPrebuilt = (*prebuiltLibraryDecorator)(nil)
76
77var _ compiler = (*prebuiltProcMacroDecorator)(nil)
78var _ exportedFlagsProducer = (*prebuiltProcMacroDecorator)(nil)
79var _ rustPrebuilt = (*prebuiltProcMacroDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070080
Matthew Maurer1d8e20d2023-11-20 21:18:12 +000081func prebuiltPath(ctx ModuleContext, prebuilt rustPrebuilt) android.Path {
82 srcs := android.PathsForModuleSrc(ctx, prebuilt.prebuiltSrcs())
83 if len(srcs) == 0 {
84 ctx.PropertyErrorf("srcs", "srcs must not be empty")
85 }
86 if len(srcs) > 1 {
87 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
88 }
89 return srcs[0]
90}
91
Matthew Maurerc761eec2020-06-25 00:47:46 -070092func PrebuiltLibraryFactory() android.Module {
93 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
94 return module.Init()
95}
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097func PrebuiltDylibFactory() android.Module {
98 module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
99 return module.Init()
100}
101
Matthew Maurerc761eec2020-06-25 00:47:46 -0700102func PrebuiltRlibFactory() android.Module {
103 module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
104 return module.Init()
105}
106
Ivan Lozano872d5792022-03-23 17:31:39 -0400107func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500108 srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
109 return prebuilt.prebuiltSrcs()
110 }
111 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
112}
113
Matthew Maurerc761eec2020-06-25 00:47:46 -0700114func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
115 module, library := NewRustLibrary(hod)
116 library.BuildOnlyRust()
117 library.setNoStdlibs()
118 prebuilt := &prebuiltLibraryDecorator{
119 libraryDecorator: library,
120 }
121 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500122
123 addSrcSupplier(module, prebuilt)
124
Matthew Maurerc761eec2020-06-25 00:47:46 -0700125 return module, prebuilt
126}
127
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
129 module, library := NewRustLibrary(hod)
130 library.BuildOnlyDylib()
Matthew Maurer99020b02019-10-31 10:44:40 -0700131 library.setNoStdlibs()
Matthew Maurerc761eec2020-06-25 00:47:46 -0700132 prebuilt := &prebuiltLibraryDecorator{
133 libraryDecorator: library,
134 }
135 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500136
137 addSrcSupplier(module, prebuilt)
138
Matthew Maurerc761eec2020-06-25 00:47:46 -0700139 return module, prebuilt
140}
141
142func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
143 module, library := NewRustLibrary(hod)
144 library.BuildOnlyRlib()
145 library.setNoStdlibs()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 prebuilt := &prebuiltLibraryDecorator{
147 libraryDecorator: library,
148 }
149 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500150
151 addSrcSupplier(module, prebuilt)
152
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 return module, prebuilt
154}
155
156func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
Matthew Maurer128f53b2020-06-29 13:31:04 -0700157 return append(prebuilt.libraryDecorator.compilerProps(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158 &prebuilt.Properties)
159}
160
Sasha Smundaka76acba2022-04-18 20:12:56 -0700161func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000162 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -0700163 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000164 srcPath := prebuiltPath(ctx, prebuilt)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400165 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700166 return buildOutput{outputFile: srcPath}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700168
Dan Albert06feee92021-03-19 15:06:02 -0700169func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
170 deps PathDeps) android.OptionalPath {
171
172 return android.OptionalPath{}
173}
174
Ivan Lozanof1c84332019-09-20 11:00:37 -0700175func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
176 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
177 return deps
178}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400179
180func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
181 return false
182}
Matthew Maurerc761eec2020-06-25 00:47:46 -0700183
184func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string {
185 srcs := prebuilt.Properties.Srcs
186 if prebuilt.rlib() {
187 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...)
188 }
189 if prebuilt.dylib() {
190 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...)
191 }
192
193 return srcs
194}
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500195
196func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
197 return &prebuilt.Prebuilt
198}
Ivan Lozano872d5792022-03-23 17:31:39 -0400199
200func (prebuilt *prebuiltProcMacroDecorator) prebuiltSrcs() []string {
201 srcs := prebuilt.Properties.Srcs
202 return srcs
203}
204
205func (prebuilt *prebuiltProcMacroDecorator) prebuilt() *android.Prebuilt {
206 return &prebuilt.Prebuilt
207}
208
209func (prebuilt *prebuiltProcMacroDecorator) compilerProps() []interface{} {
210 return append(prebuilt.procMacroDecorator.compilerProps(),
211 &prebuilt.Properties)
212}
213
Sasha Smundaka76acba2022-04-18 20:12:56 -0700214func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000215 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
Ivan Lozano872d5792022-03-23 17:31:39 -0400216 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000217 srcPath := prebuiltPath(ctx, prebuilt)
Ivan Lozano872d5792022-03-23 17:31:39 -0400218 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Sasha Smundaka76acba2022-04-18 20:12:56 -0700219 return buildOutput{outputFile: srcPath}
Ivan Lozano872d5792022-03-23 17:31:39 -0400220}
221
222func (prebuilt *prebuiltProcMacroDecorator) rustdoc(ctx ModuleContext, flags Flags,
223 deps PathDeps) android.OptionalPath {
224
225 return android.OptionalPath{}
226}
227
228func (prebuilt *prebuiltProcMacroDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
229 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
230 return deps
231}
232
233func (prebuilt *prebuiltProcMacroDecorator) nativeCoverage() bool {
234 return false
235}