blob: 50bc32592f8941473d04be5daa46a59a8ca5340e [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,
37// vndk: {
38// enabled: true,
39// },
40// export_include_dirs: ["include/external/libfoo/vndk_include"],
41// arch: {
42// arm64: {
43// srcs: ["arm/lib64/libfoo.so"],
44// },
45// arm: {
46// srcs: ["arm/lib/libfoo.so"],
47// },
48// },
49// }
50//
51type vndkPrebuiltProperties struct {
Jae Shin43ef2642017-12-29 16:20:21 +090052 // VNDK snapshot version.
53 Version *string
54
55 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64_ab')
56 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
75 properties vndkPrebuiltProperties
76}
77
78func (p *vndkPrebuiltLibraryDecorator) Name(name string) string {
Jae Shin43ef2642017-12-29 16:20:21 +090079 return name + p.NameSuffix()
80}
81
82func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string {
Justin Yun3e2323d2018-06-08 15:08:40 +090083 suffix := p.version()
Jae Shin43ef2642017-12-29 16:20:21 +090084 if p.arch() != "" {
Justin Yun3e2323d2018-06-08 15:08:40 +090085 suffix += "." + p.arch()
Jae Shin43ef2642017-12-29 16:20:21 +090086 }
Justin Yun3e2323d2018-06-08 15:08:40 +090087 if Bool(p.properties.Binder32bit) {
88 suffix += binder32Suffix
89 }
90 return vndkSuffix + suffix
Justin Yun71549282017-11-17 12:10:28 +090091}
92
93func (p *vndkPrebuiltLibraryDecorator) version() string {
Jae Shin43ef2642017-12-29 16:20:21 +090094 return String(p.properties.Version)
95}
96
97func (p *vndkPrebuiltLibraryDecorator) arch() string {
98 return String(p.properties.Target_arch)
Justin Yun71549282017-11-17 12:10:28 +090099}
100
Justin Yun3e2323d2018-06-08 15:08:40 +0900101func (p *vndkPrebuiltLibraryDecorator) binderBit() string {
102 if Bool(p.properties.Binder32bit) {
103 return "32"
104 }
105 return "64"
106}
107
Justin Yun71549282017-11-17 12:10:28 +0900108func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Jae Shin43ef2642017-12-29 16:20:21 +0900109 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix())
Justin Yun71549282017-11-17 12:10:28 +0900110 return p.libraryDecorator.linkerFlags(ctx, flags)
111}
112
113func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path {
114 if len(p.properties.Srcs) == 0 {
115 ctx.PropertyErrorf("srcs", "missing prebuilt source file")
116 return nil
117 }
118
119 if len(p.properties.Srcs) > 1 {
120 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
121 return nil
122 }
123
124 return android.PathForModuleSrc(ctx, p.properties.Srcs[0])
125}
126
127func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext,
128 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossff6c33d2019-10-02 16:01:35 -0700129
Jooyung Han31c470b2019-10-18 16:26:59 +0900130 if !p.matchesWithDevice(ctx.DeviceConfig()) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700131 ctx.Module().SkipInstall()
132 return nil
133 }
134
Justin Yun71549282017-11-17 12:10:28 +0900135 if len(p.properties.Srcs) > 0 && p.shared() {
Inseob Kimae553032019-05-14 18:52:49 +0900136 p.libraryDecorator.exportIncludes(ctx)
Inseob Kimae553032019-05-14 18:52:49 +0900137 p.libraryDecorator.reexportFlags(p.properties.Export_flags...)
Justin Yun71549282017-11-17 12:10:28 +0900138 // current VNDK prebuilts are only shared libs.
Inseob Kimeec88e12020-01-22 11:11:29 +0900139
140 in := p.singleSourcePath(ctx)
141 builderFlags := flagsToBuilderFlags(flags)
142 p.unstrippedOutputFile = in
143 libName := in.Base()
144 if p.needsStrip(ctx) {
145 stripped := android.PathForModuleOut(ctx, "stripped", libName)
146 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
147 in = stripped
148 }
149
150 // Optimize out relinking against shared libraries whose interface hasn't changed by
151 // depending on a table of contents file instead of the library itself.
152 tocFile := android.PathForModuleOut(ctx, libName+".toc")
153 p.tocFile = android.OptionalPathForPath(tocFile)
154 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags)
155
156 return in
Justin Yun71549282017-11-17 12:10:28 +0900157 }
Colin Crossff6c33d2019-10-02 16:01:35 -0700158
159 ctx.Module().SkipInstall()
Justin Yun71549282017-11-17 12:10:28 +0900160 return nil
161}
162
Jooyung Han31c470b2019-10-18 16:26:59 +0900163func (p *vndkPrebuiltLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
164 arches := config.Arches()
165 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
166 return false
167 }
168 if config.BinderBitness() != p.binderBit() {
169 return false
170 }
171 if len(p.properties.Srcs) == 0 {
172 return false
173 }
174 return true
175}
176
Inseob Kimc9fa4a32019-09-09 10:49:03 +0900177func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool {
178 return false
179}
180
Justin Yun71549282017-11-17 12:10:28 +0900181func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) {
Justin Yun312ccb92018-01-23 12:07:46 +0900182 arches := ctx.DeviceConfig().Arches()
183 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() {
184 return
185 }
Justin Yun3e2323d2018-06-08 15:08:40 +0900186 if ctx.DeviceConfig().BinderBitness() != p.binderBit() {
187 return
188 }
Justin Yun71549282017-11-17 12:10:28 +0900189 if p.shared() {
Justin Yun3e15b962018-01-10 18:28:48 +0900190 if ctx.isVndkSp() {
191 p.baseInstaller.subDir = "vndk-sp-" + p.version()
192 } else if ctx.isVndk() {
193 p.baseInstaller.subDir = "vndk-" + p.version()
Justin Yun71549282017-11-17 12:10:28 +0900194 }
195 p.baseInstaller.install(ctx, file)
196 }
197}
198
199func vndkPrebuiltSharedLibrary() *Module {
200 module, library := NewLibrary(android.DeviceSupported)
201 library.BuildOnlyShared()
202 module.stl = nil
203 module.sanitize = nil
204 library.StripProperties.Strip.None = BoolPtr(true)
205
206 prebuilt := &vndkPrebuiltLibraryDecorator{
207 libraryDecorator: library,
208 }
209
Logan Chien4fcea3d2018-11-20 11:59:08 +0800210 prebuilt.properties.Check_elf_files = BoolPtr(false)
Inseob Kim64c43952019-08-26 16:52:35 +0900211 prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true)
212 prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true)
213
214 // Prevent default system libs (libc, libm, and libdl) from being linked
215 if prebuilt.baseLinker.Properties.System_shared_libs == nil {
216 prebuilt.baseLinker.Properties.System_shared_libs = []string{}
217 }
Logan Chien4fcea3d2018-11-20 11:59:08 +0800218
Justin Yun71549282017-11-17 12:10:28 +0900219 module.compiler = nil
220 module.linker = prebuilt
221 module.installer = prebuilt
222
223 module.AddProperties(
224 &prebuilt.properties,
225 )
226
Inseob Kimeec88e12020-01-22 11:11:29 +0900227 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
228 // Only vndk snapshots of BOARD_VNDK_VERSION will be used when building.
229 if prebuilt.version() != ctx.DeviceConfig().VndkVersion() {
230 module.SkipInstall()
231 module.Properties.HideFromMake = true
232 return
233 }
234 })
235
Justin Yun71549282017-11-17 12:10:28 +0900236 return module
237}
238
Patrice Arrudaad031502019-04-01 10:56:49 -0700239// vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot
240// shared libraries for system build. Example:
241//
242// vndk_prebuilt_shared {
243// name: "libfoo",
Inseob Kimeec88e12020-01-22 11:11:29 +0900244// version: "27",
245// target_arch: "arm64",
Patrice Arrudaad031502019-04-01 10:56:49 -0700246// vendor_available: true,
247// vndk: {
248// enabled: true,
249// },
250// export_include_dirs: ["include/external/libfoo/vndk_include"],
251// arch: {
252// arm64: {
253// srcs: ["arm/lib64/libfoo.so"],
254// },
255// arm: {
256// srcs: ["arm/lib/libfoo.so"],
257// },
258// },
259// }
Jooyung Han344d5432019-08-23 11:17:39 +0900260func VndkPrebuiltSharedFactory() android.Module {
Justin Yun71549282017-11-17 12:10:28 +0900261 module := vndkPrebuiltSharedLibrary()
262 return module.Init()
263}
264
265func init() {
Jooyung Han344d5432019-08-23 11:17:39 +0900266 android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory)
Colin Cross7a6fcbe2017-12-06 13:08:00 -0800267}