blob: 04162cdce047695a3b6d0c78d0be30c2771a3816 [file] [log] [blame]
Justin Yun71549282017-11-17 12:10:28 +09001// Copyright 2017 Google Inc. All rights reserved.
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 cc
16
17import (
18 "strings"
19
20 "android/soong/android"
21)
22
23var (
Justin Yun3e2323d2018-06-08 15:08:40 +090024 vndkSuffix = ".vndk."
25 binder32Suffix = ".binder32"
Justin Yun71549282017-11-17 12:10:28 +090026)
27
28// Creates vndk prebuilts that include the VNDK version.
29//
30// Example:
31//
32// vndk_prebuilt_shared {
33// name: "libfoo",
Inseob Kimeec88e12020-01-22 11:11:29 +090034// version: "27",
35// target_arch: "arm64",
Justin Yun71549282017-11-17 12:10:28 +090036// vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +090037// product_available: true,
Justin Yun71549282017-11-17 12:10:28 +090038// vndk: {
39// enabled: true,
40// },
41// export_include_dirs: ["include/external/libfoo/vndk_include"],
42// arch: {
43// arm64: {
44// srcs: ["arm/lib64/libfoo.so"],
45// },
46// arm: {
47// srcs: ["arm/lib/libfoo.so"],
48// },
49// },
50// }
51//
52type vndkPrebuiltProperties struct {
Jae Shin43ef2642017-12-29 16:20:21 +090053 // VNDK snapshot version.
54 Version *string
55
SzuWei Lin31c4dfc2020-11-03 18:27:32 +080056 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
Jae Shin43ef2642017-12-29 16:20:21 +090057 Target_arch *string
Justin Yun71549282017-11-17 12:10:28 +090058
Justin Yun3e2323d2018-06-08 15:08:40 +090059 // If the prebuilt snapshot lib is built with 32 bit binder, this must be set to true.
60 // The lib with 64 bit binder does not need to set this property.
61 Binder32bit *bool
62
Justin Yun71549282017-11-17 12:10:28 +090063 // Prebuilt files for each arch.
64 Srcs []string `android:"arch_variant"`
Logan Chien4fcea3d2018-11-20 11:59:08 +080065
Inseob Kimae553032019-05-14 18:52:49 +090066 // list of flags that will be used for any module that links against this module.
67 Export_flags []string `android:"arch_variant"`
68
Logan Chien4fcea3d2018-11-20 11:59:08 +080069 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined symbols,
70 // etc).
71 Check_elf_files *bool
Justin Yun71549282017-11-17 12:10:28 +090072}
73
74type vndkPrebuiltLibraryDecorator struct {
75 *libraryDecorator
Inseob Kim2b96bf52020-02-17 18:00:39 +090076 properties vndkPrebuiltProperties
77 androidMkSuffix string
Justin Yun71549282017-11-17 12:10:28 +090078}
79
80func (p *vndkPrebuiltLibraryDecorator) Name(name string) string {
Jae Shin43ef2642017-12-29 16:20:21 +090081 return name + p.NameSuffix()
82}
83
84func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string {
Justin Yun3e2323d2018-06-08 15:08:40 +090085 suffix := p.version()
Jae Shin43ef2642017-12-29 16:20:21 +090086 if p.arch() != "" {
Justin Yun3e2323d2018-06-08 15:08:40 +090087 suffix += "." + p.arch()
Jae Shin43ef2642017-12-29 16:20:21 +090088 }
Justin Yun3e2323d2018-06-08 15:08:40 +090089 if Bool(p.properties.Binder32bit) {
90 suffix += binder32Suffix
91 }
92 return vndkSuffix + suffix
Justin Yun71549282017-11-17 12:10:28 +090093}
94
95func (p *vndkPrebuiltLibraryDecorator) version() string {
Jae Shin43ef2642017-12-29 16:20:21 +090096 return String(p.properties.Version)
97}
98
99func (p *vndkPrebuiltLibraryDecorator) arch() string {
100 return String(p.properties.Target_arch)
Justin Yun71549282017-11-17 12:10:28 +0900101}
102
Justin Yun3e2323d2018-06-08 15:08:40 +0900103func (p *vndkPrebuiltLibraryDecorator) binderBit() string {
104 if Bool(p.properties.Binder32bit) {
105 return "32"
106 }
107 return "64"
108}
109
Justin Yun71549282017-11-17 12:10:28 +0900110func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Jae Shin43ef2642017-12-29 16:20:21 +0900111 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
Justin Yun71549282017-11-17 12:10:28 +0900112 return p.libraryDecorator.linkerFlags(ctx, flags)
113}
114
115func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path {
116 if len(p.properties.Srcs) == 0 {
117 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
118 return nil
119 }
120
121 if len(p.properties.Srcs) > 1 {
122 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
123 return nil
124 }
125
126 return android.PathForModuleSrc(ctx, p.properties.Srcs[0])
127}
128
129func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext,
130 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossff6c33d2019-10-02 16:01:35 -0700131
Jooyung Han31c470b2019-10-18 16:26:59 +0900132 if !p.matchesWithDevice(ctx.DeviceConfig()) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800133 ctx.Module().HideFromMake()
Colin Crossff6c33d2019-10-02 16:01:35 -0700134 return nil
135 }
136
Justin Yun71549282017-11-17 12:10:28 +0900137 if len(p.properties.Srcs) > 0 && p.shared() {
Inseob Kimae553032019-05-14 18:52:49 +0900138 p.libraryDecorator.exportIncludes(ctx)
Inseob Kimae553032019-05-14 18:52:49 +0900139 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
Justin Yun71549282017-11-17 12:10:28 +0900140 // current VNDK prebuilts are only shared libs.
Inseob Kimeec88e12020-01-22 11:11:29 +0900141
142 in := p.singleSourcePath(ctx)
143 builderFlags := flagsToBuilderFlags(flags)
144 p.unstrippedOutputFile = in
145 libName := in.Base()
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200146 if p.stripper.NeedsStrip(ctx) {
147 stripFlags := flagsToStripFlags(flags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900148 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200149 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900150 in = stripped
151 }
152
153 // Optimize out relinking against shared libraries whose interface hasn't changed by
154 // depending on a table of contents file instead of the library itself.
155 tocFile := android.PathForModuleOut(ctx, libName+".toc")
156 p.tocFile = android.OptionalPathForPath(tocFile)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500157 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900158
Inseob Kim2b96bf52020-02-17 18:00:39 +0900159 p.androidMkSuffix = p.NameSuffix()
160
161 vndkVersion := ctx.DeviceConfig().VndkVersion()
162 if vndkVersion == p.version() {
163 p.androidMkSuffix = ""
164 }
165
Colin Cross0de8a1e2020-09-18 14:15:30 -0700166 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
167 SharedLibrary: in,
168 UnstrippedSharedLibrary: p.unstrippedOutputFile,
169
170 TableOfContents: p.tocFile,
171 })
172
Inseob Kim70cb3b62020-10-16 16:23:05 +0900173 p.libraryDecorator.flagExporter.setProvider(ctx)
174
Inseob Kimeec88e12020-01-22 11:11:29 +0900175 return in
Justin Yun71549282017-11-17 12:10:28 +0900176 }
Colin Crossff6c33d2019-10-02 16:01:35 -0700177
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800178 ctx.Module().HideFromMake()
Justin Yun71549282017-11-17 12:10:28 +0900179 return nil
180}
181
Jooyung Han31c470b2019-10-18 16:26:59 +0900182func (p *vndkPrebuiltLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
183 arches := config.Arches()
184 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
185 return false
186 }
187 if config.BinderBitness() != p.binderBit() {
188 return false
189 }
190 if len(p.properties.Srcs) == 0 {
191 return false
192 }
193 return true
194}
195
Inseob Kimc9fa4a32019-09-09 10:49:03 +0900196func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool {
197 return false
198}
199
Inseob Kim1042d292020-06-01 23:23:05 +0900200func (p *vndkPrebuiltLibraryDecorator) isSnapshotPrebuilt() bool {
201 return true
202}
203
Justin Yun71549282017-11-17 12:10:28 +0900204func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Jooyung Han261e1582020-10-20 18:54:21 +0900205 // do not install vndk libs
Justin Yun71549282017-11-17 12:10:28 +0900206}
207
208func vndkPrebuiltSharedLibrary() *Module {
209 module, library := NewLibrary(android.DeviceSupported)
210 library.BuildOnlyShared()
211 module.stl = nil
212 module.sanitize = nil
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200213 library.disableStripping()
Justin Yun71549282017-11-17 12:10:28 +0900214
215 prebuilt := &vndkPrebuiltLibraryDecorator{
216 libraryDecorator: library,
217 }
218
Logan Chien4fcea3d2018-11-20 11:59:08 +0800219 prebuilt.properties.Check_elf_files = BoolPtr(false)
Inseob Kim64c43952019-08-26 16:52:35 +0900220 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
221 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
222
223 // Prevent default system libs (libc, libm, and libdl) from being linked
224 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
225 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
226 }
Logan Chien4fcea3d2018-11-20 11:59:08 +0800227
Justin Yun71549282017-11-17 12:10:28 +0900228 module.compiler = nil
229 module.linker = prebuilt
230 module.installer = prebuilt
231
232 module.AddProperties(
233 &prebuilt.properties,
234 )
235
Inseob Kim81235ff2020-10-23 14:36:11 +0900236 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
237 // empty BOARD_VNDK_VERSION implies that the device won't support
238 // system only OTA. In this case, VNDK snapshots aren't needed.
239 if ctx.DeviceConfig().VndkVersion() == "" {
240 ctx.Module().Disable()
241 }
242 })
243
Justin Yun71549282017-11-17 12:10:28 +0900244 return module
245}
246
Patrice Arrudaad031502019-04-01 10:56:49 -0700247// vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot
248// shared libraries for system build. Example:
249//
250// vndk_prebuilt_shared {
251// name: "libfoo",
Inseob Kimeec88e12020-01-22 11:11:29 +0900252// version: "27",
253// target_arch: "arm64",
Patrice Arrudaad031502019-04-01 10:56:49 -0700254// vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900255// product_available: true,
Patrice Arrudaad031502019-04-01 10:56:49 -0700256// vndk: {
257// enabled: true,
258// },
259// export_include_dirs: ["include/external/libfoo/vndk_include"],
260// arch: {
261// arm64: {
262// srcs: ["arm/lib64/libfoo.so"],
263// },
264// arm: {
265// srcs: ["arm/lib/libfoo.so"],
266// },
267// },
268// }
Jooyung Han344d5432019-08-23 11:17:39 +0900269func VndkPrebuiltSharedFactory() android.Module {
Justin Yun71549282017-11-17 12:10:28 +0900270 module := vndkPrebuiltSharedLibrary()
271 return module.Init()
272}
273
274func init() {
Jooyung Han344d5432019-08-23 11:17:39 +0900275 android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
Colin Cross7a6fcbe2017-12-06 13:08:00 -0800276}