blob: 71e6427c1185ef9073ed648a7bcbbc0343690bc1 [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
Colin Crossa8890802021-01-22 14:06:33 -0800110func (p *vndkPrebuiltLibraryDecorator) snapshotAndroidMkSuffix() string {
111 return ".vendor"
112}
113
Justin Yun71549282017-11-17 12:10:28 +0900114func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Jae Shin43ef2642017-12-29 16:20:21 +0900115 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
Justin Yun71549282017-11-17 12:10:28 +0900116 return p.libraryDecorator.linkerFlags(ctx, flags)
117}
118
119func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path {
120 if len(p.properties.Srcs) == 0 {
121 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
122 return nil
123 }
124
125 if len(p.properties.Srcs) > 1 {
126 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
127 return nil
128 }
129
130 return android.PathForModuleSrc(ctx, p.properties.Srcs[0])
131}
132
133func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext,
134 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossff6c33d2019-10-02 16:01:35 -0700135
Jooyung Han31c470b2019-10-18 16:26:59 +0900136 if !p.matchesWithDevice(ctx.DeviceConfig()) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800137 ctx.Module().HideFromMake()
Colin Crossff6c33d2019-10-02 16:01:35 -0700138 return nil
139 }
140
Justin Yun71549282017-11-17 12:10:28 +0900141 if len(p.properties.Srcs) > 0 && p.shared() {
Inseob Kimae553032019-05-14 18:52:49 +0900142 p.libraryDecorator.exportIncludes(ctx)
Inseob Kimae553032019-05-14 18:52:49 +0900143 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
Justin Yun71549282017-11-17 12:10:28 +0900144 // current VNDK prebuilts are only shared libs.
Inseob Kimeec88e12020-01-22 11:11:29 +0900145
146 in := p.singleSourcePath(ctx)
147 builderFlags := flagsToBuilderFlags(flags)
148 p.unstrippedOutputFile = in
149 libName := in.Base()
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200150 if p.stripper.NeedsStrip(ctx) {
151 stripFlags := flagsToStripFlags(flags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900152 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200153 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900154 in = stripped
155 }
156
157 // Optimize out relinking against shared libraries whose interface hasn't changed by
158 // depending on a table of contents file instead of the library itself.
159 tocFile := android.PathForModuleOut(ctx, libName+".toc")
160 p.tocFile = android.OptionalPathForPath(tocFile)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500161 transformSharedObjectToToc(ctx, in, tocFile, builderFlags)
Inseob Kimeec88e12020-01-22 11:11:29 +0900162
Inseob Kim2b96bf52020-02-17 18:00:39 +0900163 p.androidMkSuffix = p.NameSuffix()
164
165 vndkVersion := ctx.DeviceConfig().VndkVersion()
166 if vndkVersion == p.version() {
167 p.androidMkSuffix = ""
168 }
169
Colin Cross0de8a1e2020-09-18 14:15:30 -0700170 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
171 SharedLibrary: in,
172 UnstrippedSharedLibrary: p.unstrippedOutputFile,
173
174 TableOfContents: p.tocFile,
175 })
176
Inseob Kim70cb3b62020-10-16 16:23:05 +0900177 p.libraryDecorator.flagExporter.setProvider(ctx)
178
Inseob Kimeec88e12020-01-22 11:11:29 +0900179 return in
Justin Yun71549282017-11-17 12:10:28 +0900180 }
Colin Crossff6c33d2019-10-02 16:01:35 -0700181
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800182 ctx.Module().HideFromMake()
Justin Yun71549282017-11-17 12:10:28 +0900183 return nil
184}
185
Jooyung Han31c470b2019-10-18 16:26:59 +0900186func (p *vndkPrebuiltLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
187 arches := config.Arches()
188 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
189 return false
190 }
191 if config.BinderBitness() != p.binderBit() {
192 return false
193 }
194 if len(p.properties.Srcs) == 0 {
195 return false
196 }
197 return true
198}
199
Inseob Kimc9fa4a32019-09-09 10:49:03 +0900200func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool {
201 return false
202}
203
Inseob Kim1042d292020-06-01 23:23:05 +0900204func (p *vndkPrebuiltLibraryDecorator) isSnapshotPrebuilt() bool {
205 return true
206}
207
Justin Yun71549282017-11-17 12:10:28 +0900208func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Jooyung Han261e1582020-10-20 18:54:21 +0900209 // do not install vndk libs
Justin Yun71549282017-11-17 12:10:28 +0900210}
211
212func vndkPrebuiltSharedLibrary() *Module {
213 module, library := NewLibrary(android.DeviceSupported)
214 library.BuildOnlyShared()
215 module.stl = nil
216 module.sanitize = nil
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200217 library.disableStripping()
Justin Yun71549282017-11-17 12:10:28 +0900218
219 prebuilt := &vndkPrebuiltLibraryDecorator{
220 libraryDecorator: library,
221 }
222
Logan Chien4fcea3d2018-11-20 11:59:08 +0800223 prebuilt.properties.Check_elf_files = BoolPtr(false)
Inseob Kim64c43952019-08-26 16:52:35 +0900224 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
225 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
226
227 // Prevent default system libs (libc, libm, and libdl) from being linked
228 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
229 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
230 }
Logan Chien4fcea3d2018-11-20 11:59:08 +0800231
Justin Yun71549282017-11-17 12:10:28 +0900232 module.compiler = nil
233 module.linker = prebuilt
234 module.installer = prebuilt
235
236 module.AddProperties(
237 &prebuilt.properties,
238 )
239
Inseob Kim81235ff2020-10-23 14:36:11 +0900240 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
241 // empty BOARD_VNDK_VERSION implies that the device won't support
242 // system only OTA. In this case, VNDK snapshots aren't needed.
243 if ctx.DeviceConfig().VndkVersion() == "" {
244 ctx.Module().Disable()
245 }
246 })
247
Justin Yun71549282017-11-17 12:10:28 +0900248 return module
249}
250
Patrice Arrudaad031502019-04-01 10:56:49 -0700251// vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot
252// shared libraries for system build. Example:
253//
254// vndk_prebuilt_shared {
255// name: "libfoo",
Inseob Kimeec88e12020-01-22 11:11:29 +0900256// version: "27",
257// target_arch: "arm64",
Patrice Arrudaad031502019-04-01 10:56:49 -0700258// vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900259// product_available: true,
Patrice Arrudaad031502019-04-01 10:56:49 -0700260// vndk: {
261// enabled: true,
262// },
263// export_include_dirs: ["include/external/libfoo/vndk_include"],
264// arch: {
265// arm64: {
266// srcs: ["arm/lib64/libfoo.so"],
267// },
268// arm: {
269// srcs: ["arm/lib/libfoo.so"],
270// },
271// },
272// }
Jooyung Han344d5432019-08-23 11:17:39 +0900273func VndkPrebuiltSharedFactory() android.Module {
Justin Yun71549282017-11-17 12:10:28 +0900274 module := vndkPrebuiltSharedLibrary()
275 return module.Init()
276}
277
278func init() {
Jooyung Han344d5432019-08-23 11:17:39 +0900279 android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
Colin Cross7a6fcbe2017-12-06 13:08:00 -0800280}