• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	Force_use_prebuilt *bool `android:"arch_variant"`
35}
36
37type prebuiltLibraryDecorator struct {
38	android.Prebuilt
39
40	*libraryDecorator
41	Properties PrebuiltProperties
42}
43
44type 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
73var _ compiler = (*prebuiltLibraryDecorator)(nil)
74var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
75var _ rustPrebuilt = (*prebuiltLibraryDecorator)(nil)
76
77var _ compiler = (*prebuiltProcMacroDecorator)(nil)
78var _ exportedFlagsProducer = (*prebuiltProcMacroDecorator)(nil)
79var _ rustPrebuilt = (*prebuiltProcMacroDecorator)(nil)
80
81func 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
92func PrebuiltLibraryFactory() android.Module {
93	module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
94	return module.Init()
95}
96
97func PrebuiltDylibFactory() android.Module {
98	module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
99	return module.Init()
100}
101
102func PrebuiltRlibFactory() android.Module {
103	module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
104	return module.Init()
105}
106
107func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) {
108	srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
109		return prebuilt.prebuiltSrcs()
110	}
111	android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
112}
113
114func 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
122
123	addSrcSupplier(module, prebuilt)
124
125	return module, prebuilt
126}
127
128func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
129	module, library := NewRustLibrary(hod)
130	library.BuildOnlyDylib()
131	library.setNoStdlibs()
132	prebuilt := &prebuiltLibraryDecorator{
133		libraryDecorator: library,
134	}
135	module.compiler = prebuilt
136
137	addSrcSupplier(module, prebuilt)
138
139	return module, prebuilt
140}
141
142func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
143	module, library := NewRustLibrary(hod)
144	library.BuildOnlyRlib()
145	library.setNoStdlibs()
146	prebuilt := &prebuiltLibraryDecorator{
147		libraryDecorator: library,
148	}
149	module.compiler = prebuilt
150
151	addSrcSupplier(module, prebuilt)
152
153	return module, prebuilt
154}
155
156func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
157	return append(prebuilt.libraryDecorator.compilerProps(),
158		&prebuilt.Properties)
159}
160
161func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
162	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
163	prebuilt.flagExporter.setRustProvider(ctx)
164	srcPath := prebuiltPath(ctx, prebuilt)
165	prebuilt.baseCompiler.unstrippedOutputFile = srcPath
166	return buildOutput{outputFile: srcPath}
167}
168
169func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
170	deps PathDeps) android.OptionalPath {
171
172	return android.OptionalPath{}
173}
174
175func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
176	deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
177	return deps
178}
179
180func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
181	return false
182}
183
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}
195
196func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
197	return &prebuilt.Prebuilt
198}
199
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
214func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
215	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
216	prebuilt.flagExporter.setRustProvider(ctx)
217	srcPath := prebuiltPath(ctx, prebuilt)
218	prebuilt.baseCompiler.unstrippedOutputFile = srcPath
219	return buildOutput{outputFile: srcPath}
220}
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}
236