blob: 6f17272f7867d186b60d090f3e5d538e9e134141 [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 {
Ivan Lozanofba2aa22021-11-11 09:29:07 -050035 android.Prebuilt
36
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 *libraryDecorator
38 Properties PrebuiltProperties
39}
40
41var _ compiler = (*prebuiltLibraryDecorator)(nil)
Matthew Maurerbb3add12020-06-25 09:34:12 -070042var _ exportedFlagsProducer = (*prebuiltLibraryDecorator)(nil)
Ivan Lozanoffee3342019-08-27 12:03:00 -070043
Matthew Maurerc761eec2020-06-25 00:47:46 -070044func PrebuiltLibraryFactory() android.Module {
45 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
46 return module.Init()
47}
48
Ivan Lozanoffee3342019-08-27 12:03:00 -070049func PrebuiltDylibFactory() android.Module {
50 module, _ := NewPrebuiltDylib(android.HostAndDeviceSupported)
51 return module.Init()
52}
53
Matthew Maurerc761eec2020-06-25 00:47:46 -070054func PrebuiltRlibFactory() android.Module {
55 module, _ := NewPrebuiltRlib(android.HostAndDeviceSupported)
56 return module.Init()
57}
58
Ivan Lozanofba2aa22021-11-11 09:29:07 -050059func addSrcSupplier(module android.PrebuiltInterface, prebuilt *prebuiltLibraryDecorator) {
60 srcsSupplier := func(_ android.BaseModuleContext, _ android.Module) []string {
61 return prebuilt.prebuiltSrcs()
62 }
63 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
64}
65
Matthew Maurerc761eec2020-06-25 00:47:46 -070066func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
67 module, library := NewRustLibrary(hod)
68 library.BuildOnlyRust()
69 library.setNoStdlibs()
70 prebuilt := &prebuiltLibraryDecorator{
71 libraryDecorator: library,
72 }
73 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -050074
75 addSrcSupplier(module, prebuilt)
76
Matthew Maurerc761eec2020-06-25 00:47:46 -070077 return module, prebuilt
78}
79
Ivan Lozanoffee3342019-08-27 12:03:00 -070080func NewPrebuiltDylib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
81 module, library := NewRustLibrary(hod)
82 library.BuildOnlyDylib()
Matthew Maurer99020b02019-10-31 10:44:40 -070083 library.setNoStdlibs()
Matthew Maurerc761eec2020-06-25 00:47:46 -070084 prebuilt := &prebuiltLibraryDecorator{
85 libraryDecorator: library,
86 }
87 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -050088
89 addSrcSupplier(module, prebuilt)
90
Matthew Maurerc761eec2020-06-25 00:47:46 -070091 return module, prebuilt
92}
93
94func NewPrebuiltRlib(hod android.HostOrDeviceSupported) (*Module, *prebuiltLibraryDecorator) {
95 module, library := NewRustLibrary(hod)
96 library.BuildOnlyRlib()
97 library.setNoStdlibs()
Ivan Lozanoffee3342019-08-27 12:03:00 -070098 prebuilt := &prebuiltLibraryDecorator{
99 libraryDecorator: library,
100 }
101 module.compiler = prebuilt
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500102
103 addSrcSupplier(module, prebuilt)
104
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 return module, prebuilt
106}
107
108func (prebuilt *prebuiltLibraryDecorator) compilerProps() []interface{} {
Matthew Maurer128f53b2020-06-29 13:31:04 -0700109 return append(prebuilt.libraryDecorator.compilerProps(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110 &prebuilt.Properties)
111}
112
113func (prebuilt *prebuiltLibraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700114 prebuilt.flagExporter.exportLinkDirs(android.PathsForModuleSrc(ctx, prebuilt.Properties.Link_dirs).Strings()...)
115 prebuilt.flagExporter.setProvider(ctx)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700116
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700117 srcPath, paths := srcPathFromModuleSrcs(ctx, prebuilt.prebuiltSrcs())
Ivan Lozano43845682020-07-09 21:03:28 -0400118 if len(paths) > 0 {
119 ctx.PropertyErrorf("srcs", "prebuilt libraries can only have one entry in srcs (the prebuilt path)")
120 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400121 prebuilt.baseCompiler.unstrippedOutputFile = srcPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700122 return srcPath
123}
Ivan Lozanof1c84332019-09-20 11:00:37 -0700124
Dan Albert06feee92021-03-19 15:06:02 -0700125func (prebuilt *prebuiltLibraryDecorator) rustdoc(ctx ModuleContext, flags Flags,
126 deps PathDeps) android.OptionalPath {
127
128 return android.OptionalPath{}
129}
130
Ivan Lozanof1c84332019-09-20 11:00:37 -0700131func (prebuilt *prebuiltLibraryDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
132 deps = prebuilt.baseCompiler.compilerDeps(ctx, deps)
133 return deps
134}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400135
136func (prebuilt *prebuiltLibraryDecorator) nativeCoverage() bool {
137 return false
138}
Matthew Maurerc761eec2020-06-25 00:47:46 -0700139
140func (prebuilt *prebuiltLibraryDecorator) prebuiltSrcs() []string {
141 srcs := prebuilt.Properties.Srcs
142 if prebuilt.rlib() {
143 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Rlib.Srcs...)
144 }
145 if prebuilt.dylib() {
146 srcs = append(srcs, prebuilt.libraryDecorator.Properties.Dylib.Srcs...)
147 }
148
149 return srcs
150}
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500151
152func (prebuilt *prebuiltLibraryDecorator) prebuilt() *android.Prebuilt {
153 return &prebuilt.Prebuilt
154}