blob: eb1790f58077a31194d9eb6da342eb6e155a702b [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//
Colin Crossd079e0b2022-08-16 10:27:33 -070032// vndk_prebuilt_shared {
33// name: "libfoo",
34// version: "27",
35// target_arch: "arm64",
36// vendor_available: true,
37// product_available: true,
38// 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// }
Justin Yun71549282017-11-17 12:10:28 +090051type vndkPrebuiltProperties struct {
Jae Shin43ef2642017-12-29 16:20:21 +090052 // VNDK snapshot version.
53 Version *string
54
SzuWei Lin31c4dfc2020-11-03 18:27:32 +080055 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
Jae Shin43ef2642017-12-29 16:20:21 +090056 Target_arch *string
Justin Yun71549282017-11-17 12:10:28 +090057
Justin Yun3e2323d2018-06-08 15:08:40 +090058 // If the prebuilt snapshot lib is built with 32 bit binder, this must be set to true.
59 // The lib with 64 bit binder does not need to set this property.
60 Binder32bit *bool
61
Justin Yun71549282017-11-17 12:10:28 +090062 // Prebuilt files for each arch.
63 Srcs []string `android:"arch_variant"`
Logan Chien4fcea3d2018-11-20 11:59:08 +080064
Inseob Kimae553032019-05-14 18:52:49 +090065 // list of flags that will be used for any module that links against this module.
66 Export_flags []string `android:"arch_variant"`
67
Logan Chien4fcea3d2018-11-20 11:59:08 +080068 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined symbols,
69 // etc).
70 Check_elf_files *bool
Justin Yun71549282017-11-17 12:10:28 +090071}
72
73type vndkPrebuiltLibraryDecorator struct {
74 *libraryDecorator
Inseob Kim2b96bf52020-02-17 18:00:39 +090075 properties vndkPrebuiltProperties
76 androidMkSuffix string
Justin Yun71549282017-11-17 12:10:28 +090077}
78
79func (p *vndkPrebuiltLibraryDecorator) Name(name string) string {
Jae Shin43ef2642017-12-29 16:20:21 +090080 return name + p.NameSuffix()
81}
82
83func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string {
Ivan Lozanod1dec542021-05-26 15:33:11 -040084 suffix := p.Version()
Jae Shin43ef2642017-12-29 16:20:21 +090085 if p.arch() != "" {
Justin Yun3e2323d2018-06-08 15:08:40 +090086 suffix += "." + p.arch()
Jae Shin43ef2642017-12-29 16:20:21 +090087 }
Justin Yun3e2323d2018-06-08 15:08:40 +090088 if Bool(p.properties.Binder32bit) {
89 suffix += binder32Suffix
90 }
91 return vndkSuffix + suffix
Justin Yun71549282017-11-17 12:10:28 +090092}
93
Ivan Lozanod1dec542021-05-26 15:33:11 -040094func (p *vndkPrebuiltLibraryDecorator) Version() string {
Jae Shin43ef2642017-12-29 16:20:21 +090095 return String(p.properties.Version)
96}
97
98func (p *vndkPrebuiltLibraryDecorator) arch() string {
99 return String(p.properties.Target_arch)
Justin Yun71549282017-11-17 12:10:28 +0900100}
101
Justin Yun3e2323d2018-06-08 15:08:40 +0900102func (p *vndkPrebuiltLibraryDecorator) binderBit() string {
103 if Bool(p.properties.Binder32bit) {
104 return "32"
105 }
106 return "64"
107}
108
Ivan Lozanod1dec542021-05-26 15:33:11 -0400109func (p *vndkPrebuiltLibraryDecorator) SnapshotAndroidMkSuffix() string {
Colin Crossa8890802021-01-22 14:06:33 -0800110 return ".vendor"
111}
112
Justin Yun71549282017-11-17 12:10:28 +0900113func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Jae Shin43ef2642017-12-29 16:20:21 +0900114 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
Justin Yun71549282017-11-17 12:10:28 +0900115 return p.libraryDecorator.linkerFlags(ctx, flags)
116}
117
118func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path {
119 if len(p.properties.Srcs) == 0 {
120 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
121 return nil
122 }
123
124 if len(p.properties.Srcs) > 1 {
125 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
126 return nil
127 }
128
129 return android.PathForModuleSrc(ctx, p.properties.Srcs[0])
130}
131
132func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext,
133 flags Flags, deps PathDeps, objs Objects) android.Path {
Justin Yunf14beaf2023-08-18 17:51:14 +0900134 platformVndkApiLevel := android.ApiLevelOrPanic(ctx, ctx.DeviceConfig().PlatformVndkVersion())
135 if platformVndkApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(ctx, p.Version())) {
136 // This prebuilt VNDK module is not required for the current build
137 ctx.Module().HideFromMake()
138 return nil
139 }
Colin Crossff6c33d2019-10-02 16:01:35 -0700140
Ivan Lozanod1dec542021-05-26 15:33:11 -0400141 if !p.MatchesWithDevice(ctx.DeviceConfig()) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800142 ctx.Module().HideFromMake()
Colin Crossff6c33d2019-10-02 16:01:35 -0700143 return nil
144 }
145
Justin Yun71549282017-11-17 12:10:28 +0900146 if len(p.properties.Srcs) > 0 && p.shared() {
Inseob Kimae553032019-05-14 18:52:49 +0900147 p.libraryDecorator.exportIncludes(ctx)
Inseob Kimae553032019-05-14 18:52:49 +0900148 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
Justin Yun71549282017-11-17 12:10:28 +0900149 // current VNDK prebuilts are only shared libs.
Inseob Kimeec88e12020-01-22 11:11:29 +0900150
151 in := p.singleSourcePath(ctx)
Inseob Kimeec88e12020-01-22 11:11:29 +0900152 p.unstrippedOutputFile = in
153 libName := in.Base()
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200154 if p.stripper.NeedsStrip(ctx) {
155 stripFlags := flagsToStripFlags(flags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900156 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200157 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900158 in = stripped
159 }
160
161 // Optimize out relinking against shared libraries whose interface hasn't changed by
162 // depending on a table of contents file instead of the library itself.
163 tocFile := android.PathForModuleOut(ctx, libName+".toc")
164 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400165 TransformSharedObjectToToc(ctx, in, tocFile)
Inseob Kimeec88e12020-01-22 11:11:29 +0900166
Inseob Kim2b96bf52020-02-17 18:00:39 +0900167 p.androidMkSuffix = p.NameSuffix()
168
169 vndkVersion := ctx.DeviceConfig().VndkVersion()
Ivan Lozanod1dec542021-05-26 15:33:11 -0400170 if vndkVersion == p.Version() {
Inseob Kim2b96bf52020-02-17 18:00:39 +0900171 p.androidMkSuffix = ""
172 }
173
Colin Cross40213022023-12-13 15:19:49 -0800174 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400175 SharedLibrary: in,
176 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700177
178 TableOfContents: p.tocFile,
179 })
180
Inseob Kim70cb3b62020-10-16 16:23:05 +0900181 p.libraryDecorator.flagExporter.setProvider(ctx)
182
Inseob Kimeec88e12020-01-22 11:11:29 +0900183 return in
Justin Yun71549282017-11-17 12:10:28 +0900184 }
Colin Crossff6c33d2019-10-02 16:01:35 -0700185
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800186 ctx.Module().HideFromMake()
Justin Yun71549282017-11-17 12:10:28 +0900187 return nil
188}
189
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800190func (p *vndkPrebuiltLibraryDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
191 p.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
192 moduleInfoJSON.SubName += p.androidMkSuffix
193}
194
Ivan Lozanod1dec542021-05-26 15:33:11 -0400195func (p *vndkPrebuiltLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
Jooyung Han31c470b2019-10-18 16:26:59 +0900196 arches := config.Arches()
197 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
198 return false
199 }
200 if config.BinderBitness() != p.binderBit() {
201 return false
202 }
203 if len(p.properties.Srcs) == 0 {
204 return false
205 }
206 return true
207}
208
Inseob Kimc9fa4a32019-09-09 10:49:03 +0900209func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool {
210 return false
211}
212
Ivan Lozanod1dec542021-05-26 15:33:11 -0400213func (p *vndkPrebuiltLibraryDecorator) IsSnapshotPrebuilt() bool {
Inseob Kim1042d292020-06-01 23:23:05 +0900214 return true
215}
216
Justin Yun71549282017-11-17 12:10:28 +0900217func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Jooyung Han261e1582020-10-20 18:54:21 +0900218 // do not install vndk libs
Justin Yun71549282017-11-17 12:10:28 +0900219}
220
221func vndkPrebuiltSharedLibrary() *Module {
222 module, library := NewLibrary(android.DeviceSupported)
223 library.BuildOnlyShared()
224 module.stl = nil
225 module.sanitize = nil
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200226 library.disableStripping()
Justin Yun71549282017-11-17 12:10:28 +0900227
228 prebuilt := &vndkPrebuiltLibraryDecorator{
229 libraryDecorator: library,
230 }
231
Logan Chien4fcea3d2018-11-20 11:59:08 +0800232 prebuilt.properties.Check_elf_files = BoolPtr(false)
Inseob Kim64c43952019-08-26 16:52:35 +0900233 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
234 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
235
236 // Prevent default system libs (libc, libm, and libdl) from being linked
237 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
238 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
239 }
Logan Chien4fcea3d2018-11-20 11:59:08 +0800240
Justin Yun71549282017-11-17 12:10:28 +0900241 module.compiler = nil
242 module.linker = prebuilt
243 module.installer = prebuilt
244
245 module.AddProperties(
246 &prebuilt.properties,
247 )
248
Inseob Kim81235ff2020-10-23 14:36:11 +0900249 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
250 // empty BOARD_VNDK_VERSION implies that the device won't support
251 // system only OTA. In this case, VNDK snapshots aren't needed.
252 if ctx.DeviceConfig().VndkVersion() == "" {
253 ctx.Module().Disable()
254 }
255 })
256
Justin Yun71549282017-11-17 12:10:28 +0900257 return module
258}
259
Patrice Arrudaad031502019-04-01 10:56:49 -0700260// vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot
261// shared libraries for system build. Example:
262//
Colin Crossd079e0b2022-08-16 10:27:33 -0700263// vndk_prebuilt_shared {
264// name: "libfoo",
265// version: "27",
266// target_arch: "arm64",
267// vendor_available: true,
268// product_available: true,
269// vndk: {
270// enabled: true,
271// },
272// export_include_dirs: ["include/external/libfoo/vndk_include"],
273// arch: {
274// arm64: {
275// srcs: ["arm/lib64/libfoo.so"],
276// },
277// arm: {
278// srcs: ["arm/lib/libfoo.so"],
279// },
280// },
281// }
Jooyung Han344d5432019-08-23 11:17:39 +0900282func VndkPrebuiltSharedFactory() android.Module {
Justin Yun71549282017-11-17 12:10:28 +0900283 module := vndkPrebuiltSharedLibrary()
284 return module.Init()
285}
286
287func init() {
Jooyung Han344d5432019-08-23 11:17:39 +0900288 android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
Colin Cross7a6fcbe2017-12-06 13:08:00 -0800289}