• 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
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 PrebuiltLibraryFactory() android.Module {
80	module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
81	return module.Init()
82}
83
84func PrebuiltDylibFactory() android.Module {
85	module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
86	return module.Init()
87}
88
89func PrebuiltRlibFactory() android.Module {
90	module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
91	return module.Init()
92}
93
94func addSrcSupplier(module android.PrebuiltInterface, prebuilt rustPrebuilt) {
95	srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
96		return prebuilt.prebuiltSrcs()
97	}
98	android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
99}
100
101func 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
109
110	addSrcSupplier(module, prebuilt)
111
112	return module, prebuilt
113}
114
115func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
116	module, library := NewRustLibrary(hod)
117	library.BuildOnlyDylib()
118	library.setNoStdlibs()
119	prebuilt := &prebuiltLibraryDecorator{
120		libraryDecorator: library,
121	}
122	module.compiler = prebuilt
123
124	addSrcSupplier(module, prebuilt)
125
126	return module, prebuilt
127}
128
129func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
130	module, library := NewRustLibrary(hod)
131	library.BuildOnlyRlib()
132	library.setNoStdlibs()
133	prebuilt := &prebuiltLibraryDecorator{
134		libraryDecorator: library,
135	}
136	module.compiler = prebuilt
137
138	addSrcSupplier(module, prebuilt)
139
140	return module, prebuilt
141}
142
143func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
144	return append(prebuilt.libraryDecorator.compilerProps(),
145		&prebuilt.Properties)
146}
147
148func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
149	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
150	prebuilt.flagExporter.setProvider(ctx)
151
152	srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
153	if len(paths) > 0 {
154		ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
155	}
156	prebuilt.baseCompiler.unstrippedOutputFile = srcPath
157	return srcPath
158}
159
160func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
161	deps PathDeps) android.OptionalPath {
162
163	return android.OptionalPath{}
164}
165
166func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
167	deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
168	return deps
169}
170
171func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
172	return false
173}
174
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}
186
187func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
188	return &prebuilt.Prebuilt
189}
190
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
205func (prebuilt *prebuiltProcMacroDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
206	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
207	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
214	return srcPath
215}
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}
231