blob: 141f4387400ad7d2722ade92d26067dd10d3a912 [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 {
Ivan Lozanod1dec542021-05-26 15:33:11 -040085 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
Ivan Lozanod1dec542021-05-26 15:33:11 -040095func (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
Ivan Lozanod1dec542021-05-26 15:33:11 -0400110func (p *vndkPrebuiltLibraryDecorator) SnapshotAndroidMkSuffix() string {
Colin Crossa8890802021-01-22 14:06:33 -0800111 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
Ivan Lozanod1dec542021-05-26 15:33:11 -0400136 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()
Ivan Lozanod1dec542021-05-26 15:33:11 -0400166 if vndkVersion == p.Version() {
Inseob Kim2b96bf52020-02-17 18:00:39 +0900167 p.androidMkSuffix = ""
168 }
169
Colin Cross0de8a1e2020-09-18 14:15:30 -0700170 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
171 SharedLibrary: in,
172 UnstrippedSharedLibrary: p.unstrippedOutputFile,
Colin Cross2df81772021-01-26 11:00:18 -0800173 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700174
175 TableOfContents: p.tocFile,
176 })
177
Inseob Kim70cb3b62020-10-16 16:23:05 +0900178 p.libraryDecorator.flagExporter.setProvider(ctx)
179
Inseob Kimeec88e12020-01-22 11:11:29 +0900180 return in
Justin Yun71549282017-11-17 12:10:28 +0900181 }
Colin Crossff6c33d2019-10-02 16:01:35 -0700182
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800183 ctx.Module().HideFromMake()
Justin Yun71549282017-11-17 12:10:28 +0900184 return nil
185}
186
Ivan Lozanod1dec542021-05-26 15:33:11 -0400187func (p *vndkPrebuiltLibraryDecorator) MatchesWithDevice(config android.DeviceConfig) bool {
Jooyung Han31c470b2019-10-18 16:26:59 +0900188 arches := config.Arches()
189 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
190 return false
191 }
192 if config.BinderBitness() != p.binderBit() {
193 return false
194 }
195 if len(p.properties.Srcs) == 0 {
196 return false
197 }
198 return true
199}
200
Inseob Kimc9fa4a32019-09-09 10:49:03 +0900201func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool {
202 return false
203}
204
Ivan Lozanod1dec542021-05-26 15:33:11 -0400205func (p *vndkPrebuiltLibraryDecorator) IsSnapshotPrebuilt() bool {
Inseob Kim1042d292020-06-01 23:23:05 +0900206 return true
207}
208
Justin Yun71549282017-11-17 12:10:28 +0900209func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Jooyung Han261e1582020-10-20 18:54:21 +0900210 // do not install vndk libs
Justin Yun71549282017-11-17 12:10:28 +0900211}
212
213func vndkPrebuiltSharedLibrary() *Module {
214 module, library := NewLibrary(android.DeviceSupported)
215 library.BuildOnlyShared()
216 module.stl = nil
217 module.sanitize = nil
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200218 library.disableStripping()
Justin Yun71549282017-11-17 12:10:28 +0900219
220 prebuilt := &vndkPrebuiltLibraryDecorator{
221 libraryDecorator: library,
222 }
223
Logan Chien4fcea3d2018-11-20 11:59:08 +0800224 prebuilt.properties.Check_elf_files = BoolPtr(false)
Inseob Kim64c43952019-08-26 16:52:35 +0900225 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
226 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
227
228 // Prevent default system libs (libc, libm, and libdl) from being linked
229 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
230 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
231 }
Logan Chien4fcea3d2018-11-20 11:59:08 +0800232
Justin Yun71549282017-11-17 12:10:28 +0900233 module.compiler = nil
234 module.linker = prebuilt
235 module.installer = prebuilt
236
237 module.AddProperties(
238 &prebuilt.properties,
239 )
240
Inseob Kim81235ff2020-10-23 14:36:11 +0900241 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
242 // empty BOARD_VNDK_VERSION implies that the device won't support
243 // system only OTA. In this case, VNDK snapshots aren't needed.
244 if ctx.DeviceConfig().VndkVersion() == "" {
245 ctx.Module().Disable()
246 }
247 })
248
Justin Yun71549282017-11-17 12:10:28 +0900249 return module
250}
251
Patrice Arrudaad031502019-04-01 10:56:49 -0700252// vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot
253// shared libraries for system build. Example:
254//
255// vndk_prebuilt_shared {
256// name: "libfoo",
Inseob Kimeec88e12020-01-22 11:11:29 +0900257// version: "27",
258// target_arch: "arm64",
Patrice Arrudaad031502019-04-01 10:56:49 -0700259// vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900260// product_available: true,
Patrice Arrudaad031502019-04-01 10:56:49 -0700261// vndk: {
262// enabled: true,
263// },
264// export_include_dirs: ["include/external/libfoo/vndk_include"],
265// arch: {
266// arm64: {
267// srcs: ["arm/lib64/libfoo.so"],
268// },
269// arm: {
270// srcs: ["arm/lib/libfoo.so"],
271// },
272// },
273// }
Jooyung Han344d5432019-08-23 11:17:39 +0900274func VndkPrebuiltSharedFactory() android.Module {
Justin Yun71549282017-11-17 12:10:28 +0900275 module := vndkPrebuiltSharedLibrary()
276 return module.Init()
277}
278
279func init() {
Jooyung Han344d5432019-08-23 11:17:39 +0900280 android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
Colin Cross7a6fcbe2017-12-06 13:08:00 -0800281}