blob: 6099eec4d805b8c336e14d1d10f943689b4b967a [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 Lozanoffee3342019-08-27 12:03:00 -070025}
26
27type PrebuiltProperties struct {
28 // path to the prebuilt file
29 Srcs []string `android:"path,arch_variant"`
Matthew Maurerbb3add12020-06-25 09:34:12 -070030 // directories containing associated rlib dependencies
31 Link_dirs []string `android:"path,arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070032}
33
34type prebuiltLibraryDecorator struct {
35 *libraryDecorator
36 Properties PrebuiltProperties
37}
38
39var _ compiler = (*prebuiltLibraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070040var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070041
Matthew Maurerc761eec2020-06-25 00:47:46 -070042func PrebuiltLibraryFactory() android.Module {
43 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
44 return module.Init()
45}
46
Ivan Lozanoffee3342019-08-27 12:03:00 -070047func PrebuiltDylibFactory() android.Module {
48 module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
49 return module.Init()
50}
51
Matthew Maurerc761eec2020-06-25 00:47:46 -070052func 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
Ivan Lozanoffee3342019-08-27 12:03:00 -070068func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
69 module, library := NewRustLibrary(hod)
70 library.BuildOnlyDylib()
Matthew Maurer99020b02019-10-31 10:44:40 -070071 library.setNoStdlibs()
Matthew Maurerc761eec2020-06-25 00:47:46 -070072 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()
Ivan Lozanoffee3342019-08-27 12:03:00 -070083 prebuilt := &prebuiltLibraryDecorator{
84 libraryDecorator: library,
85 }
86 module.compiler = prebuilt
Ivan Lozanoffee3342019-08-27 12:03:00 -070087 return module, prebuilt
88}
89
90func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
Matthew Maurer128f53b2020-06-29 13:31:04 -070091 return append(prebuilt.libraryDecorator.compilerProps(),
Ivan Lozanoffee3342019-08-27 12:03:00 -070092 &prebuilt.Properties)
93}
94
95func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Colin Cross0de8a1e2020-09-18 14:15:30 -070096 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
97 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurerbb3add12020-06-25 09:34:12 -070098
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -070099 srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
Ivan Lozano43845682020-07-09 21:03:28 -0400100 if len(paths) > 0 {
101 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
102 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400103 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104 return srcPath
105}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700106
Dan Albert06feee92021-03-19 15:06:02 -0700107func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
108 deps PathDeps) android.OptionalPath {
109
110 return android.OptionalPath{}
111}
112
Ivan Lozanof1c84332019-09-20 11:00:37 -0700113func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
114 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
115 return deps
116}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400117
118func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
119 return false
120}
Matthew Maurerc761eec2020-06-25 00:47:46 -0700121
122func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string {
123 srcs := prebuilt.Properties.Srcs
124 if prebuilt.rlib() {
125 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...)
126 }
127 if prebuilt.dylib() {
128 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...)
129 }
130
131 return srcs
132}