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}
26
27type PrebuiltProperties struct {
28	// path to the prebuilt file
29	Srcs []string `android:"path,arch_variant"`
30	// directories containing associated rlib dependencies
31	Link_dirs []string `android:"path,arch_variant"`
32}
33
34type prebuiltLibraryDecorator struct {
35	*libraryDecorator
36	Properties PrebuiltProperties
37}
38
39var _ compiler = (*prebuiltLibraryDecorator)(nil)
40var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
41
42func PrebuiltLibraryFactory() android.Module {
43	module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
44	return module.Init()
45}
46
47func PrebuiltDylibFactory() android.Module {
48	module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
49	return module.Init()
50}
51
52func PrebuiltRlibFactory() android.Module {
53	module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
54	return module.Init()
55}
56
57func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
58	module, library := NewRustLibrary(hod)
59	library.BuildOnlyRust()
60	library.setNoStdlibs()
61	prebuilt := &prebuiltLibraryDecorator{
62		libraryDecorator: library,
63	}
64	module.compiler = prebuilt
65	return module, prebuilt
66}
67
68func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
69	module, library := NewRustLibrary(hod)
70	library.BuildOnlyDylib()
71	library.setNoStdlibs()
72	prebuilt := &prebuiltLibraryDecorator{
73		libraryDecorator: library,
74	}
75	module.compiler = prebuilt
76	return module, prebuilt
77}
78
79func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
80	module, library := NewRustLibrary(hod)
81	library.BuildOnlyRlib()
82	library.setNoStdlibs()
83	prebuilt := &prebuiltLibraryDecorator{
84		libraryDecorator: library,
85	}
86	module.compiler = prebuilt
87	return module, prebuilt
88}
89
90func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
91	return append(prebuilt.libraryDecorator.compilerProps(),
92		&prebuilt.Properties)
93}
94
95func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
96	prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
97	prebuilt.flagExporter.setProvider(ctx)
98
99	srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
100	if len(paths) > 0 {
101		ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
102	}
103	return srcPath
104}
105
106func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
107	deps PathDeps) android.OptionalPath {
108
109	return android.OptionalPath{}
110}
111
112func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
113	deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
114	return deps
115}
116
117func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
118	return false
119}
120
121func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string {
122	srcs := prebuilt.Properties.Srcs
123	if prebuilt.rlib() {
124		srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...)
125	}
126	if prebuilt.dylib() {
127		srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...)
128	}
129
130	return srcs
131}
132