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
15package rust
16
17import (
18	"android/soong/android"
19)
20
21func init() {
22	android.RegisterModuleType("rust_prebuilt_library", PrebuiltLibraryFactory)
23	android.RegisterModuleType("rust_prebuilt_dylib", PrebuiltDylibFactory)
24	android.RegisterModuleType("rust_prebuilt_rlib", PrebuiltRlibFactory)
25	android.RegisterModuleType("rust_prebuilt_proc_macro", PrebuiltProcMacroFactory)
26}
27
28type PrebuiltProperties struct {
29	// path to the prebuilt file
30	Srcs []string `android:"path,arch_variant"`
31	// directories containing associated rlib dependencies
32	Link_dirs []string `android:"path,arch_variant"`
33}
34
35type prebuiltLibraryDecorator struct {
36	android.Prebuilt
37
38	*libraryDecorator
39	Properties PrebuiltProperties
40}
41
42type 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
71var _ compiler = (*prebuiltLibraryDecorator)(nil)
72var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
73var _ rustPrebuilt = (*prebuiltLibraryDecorator)(nil)
74
75var _ compiler = (*prebuiltProcMacroDecorator)(nil)
76var _ exportedFlagsProducer = (*prebuiltProcMacroDecorator)(nil)
77var _ rustPrebuilt = (*prebuiltProcMacroDecorator)(nil)
78
79func 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
90func PrebuiltLibraryFactory() android.Module {
91	module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
92	return module.Init()
93}
94
95func PrebuiltDylibFactory() android.Module {
96	module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
97	return module.Init()
98}
99
100func PrebuiltRlibFactory() android.Module {
101	module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
102	return module.Init()
103}
104
105func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) {
106	srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
107		return prebuilt.prebuiltSrcs()
108	}
109	android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
110}
111
112func 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
120
121	addSrcSupplier(module, prebuilt)
122
123	return module, prebuilt
124}
125
126func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
127	module, library := NewRustLibrary(hod)
128	library.BuildOnlyDylib()
129	library.setNoStdlibs()
130	prebuilt := &prebuiltLibraryDecorator{
131		libraryDecorator: library,
132	}
133	module.compiler = prebuilt
134
135	addSrcSupplier(module, prebuilt)
136
137	return module, prebuilt
138}
139
140func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
141	module, library := NewRustLibrary(hod)
142	library.BuildOnlyRlib()
143	library.setNoStdlibs()
144	prebuilt := &prebuiltLibraryDecorator{
145		libraryDecorator: library,
146	}
147	module.compiler = prebuilt
148
149	addSrcSupplier(module, prebuilt)
150
151	return module, prebuilt
152}
153
154func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
155	return append(prebuilt.libraryDecorator.compilerProps(),
156		&prebuilt.Properties)
157}
158
159func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
160	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
161	prebuilt.flagExporter.setProvider(ctx)
162	srcPath := prebuiltPath(ctx, prebuilt)
163	prebuilt.baseCompiler.unstrippedOutputFile = srcPath
164	return buildOutput{outputFile: srcPath}
165}
166
167func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
168	deps PathDeps) android.OptionalPath {
169
170	return android.OptionalPath{}
171}
172
173func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
174	deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
175	return deps
176}
177
178func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
179	return false
180}
181
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}
193
194func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
195	return &prebuilt.Prebuilt
196}
197
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
212func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
213	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
214	prebuilt.flagExporter.setProvider(ctx)
215	srcPath := prebuiltPath(ctx, prebuilt)
216	prebuilt.baseCompiler.unstrippedOutputFile = srcPath
217	return buildOutput{outputFile: srcPath}
218}
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}
234