Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | var ( |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 24 | vndkSuffix = ".vndk." |
| 25 | binder32Suffix = ".binder32" |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // Creates vndk prebuilts that include the VNDK version. |
| 29 | // |
| 30 | // Example: |
| 31 | // |
| 32 | // vndk_prebuilt_shared { |
| 33 | // name: "libfoo", |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 34 | // version: "27", |
| 35 | // target_arch: "arm64", |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 36 | // 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 | // |
| 51 | type vndkPrebuiltProperties struct { |
Jae Shin | 43ef264 | 2017-12-29 16:20:21 +0900 | [diff] [blame] | 52 | // 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 Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 57 | |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 58 | // 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 Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 62 | // Prebuilt files for each arch. |
| 63 | Srcs []string `android:"arch_variant"` |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 64 | |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 65 | // list of flags that will be used for any module that links against this module. |
| 66 | Export_flags []string `android:"arch_variant"` |
| 67 | |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 68 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined symbols, |
| 69 | // etc). |
| 70 | Check_elf_files *bool |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type vndkPrebuiltLibraryDecorator struct { |
| 74 | *libraryDecorator |
Inseob Kim | 2b96bf5 | 2020-02-17 18:00:39 +0900 | [diff] [blame] | 75 | properties vndkPrebuiltProperties |
| 76 | androidMkSuffix string |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func (p *vndkPrebuiltLibraryDecorator) Name(name string) string { |
Jae Shin | 43ef264 | 2017-12-29 16:20:21 +0900 | [diff] [blame] | 80 | return name + p.NameSuffix() |
| 81 | } |
| 82 | |
| 83 | func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string { |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 84 | suffix := p.version() |
Jae Shin | 43ef264 | 2017-12-29 16:20:21 +0900 | [diff] [blame] | 85 | if p.arch() != "" { |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 86 | suffix += "." + p.arch() |
Jae Shin | 43ef264 | 2017-12-29 16:20:21 +0900 | [diff] [blame] | 87 | } |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 88 | if Bool(p.properties.Binder32bit) { |
| 89 | suffix += binder32Suffix |
| 90 | } |
| 91 | return vndkSuffix + suffix |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (p *vndkPrebuiltLibraryDecorator) version() string { |
Jae Shin | 43ef264 | 2017-12-29 16:20:21 +0900 | [diff] [blame] | 95 | return String(p.properties.Version) |
| 96 | } |
| 97 | |
| 98 | func (p *vndkPrebuiltLibraryDecorator) arch() string { |
| 99 | return String(p.properties.Target_arch) |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 102 | func (p *vndkPrebuiltLibraryDecorator) binderBit() string { |
| 103 | if Bool(p.properties.Binder32bit) { |
| 104 | return "32" |
| 105 | } |
| 106 | return "64" |
| 107 | } |
| 108 | |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 109 | func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Jae Shin | 43ef264 | 2017-12-29 16:20:21 +0900 | [diff] [blame] | 110 | p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix()) |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 111 | return p.libraryDecorator.linkerFlags(ctx, flags) |
| 112 | } |
| 113 | |
| 114 | func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path { |
| 115 | if len(p.properties.Srcs) == 0 { |
| 116 | ctx.PropertyErrorf("srcs", "missing prebuilt source file") |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | if len(p.properties.Srcs) > 1 { |
| 121 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 122 | return nil |
| 123 | } |
| 124 | |
| 125 | return android.PathForModuleSrc(ctx, p.properties.Srcs[0]) |
| 126 | } |
| 127 | |
| 128 | func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext, |
| 129 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 130 | |
Jooyung Han | 31c470b | 2019-10-18 16:26:59 +0900 | [diff] [blame] | 131 | if !p.matchesWithDevice(ctx.DeviceConfig()) { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 132 | ctx.Module().SkipInstall() |
| 133 | return nil |
| 134 | } |
| 135 | |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 136 | if len(p.properties.Srcs) > 0 && p.shared() { |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 137 | p.libraryDecorator.exportIncludes(ctx) |
Inseob Kim | ae55303 | 2019-05-14 18:52:49 +0900 | [diff] [blame] | 138 | p.libraryDecorator.reexportFlags(p.properties.Export_flags...) |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 139 | // current VNDK prebuilts are only shared libs. |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 140 | |
| 141 | in := p.singleSourcePath(ctx) |
| 142 | builderFlags := flagsToBuilderFlags(flags) |
| 143 | p.unstrippedOutputFile = in |
| 144 | libName := in.Base() |
| 145 | if p.needsStrip(ctx) { |
| 146 | stripped := android.PathForModuleOut(ctx, "stripped", libName) |
| 147 | p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags) |
| 148 | in = stripped |
| 149 | } |
| 150 | |
| 151 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 152 | // depending on a table of contents file instead of the library itself. |
| 153 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 154 | p.tocFile = android.OptionalPathForPath(tocFile) |
| 155 | TransformSharedObjectToToc(ctx, in, tocFile, builderFlags) |
| 156 | |
Inseob Kim | 2b96bf5 | 2020-02-17 18:00:39 +0900 | [diff] [blame] | 157 | p.androidMkSuffix = p.NameSuffix() |
| 158 | |
| 159 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 160 | if vndkVersion == p.version() { |
| 161 | p.androidMkSuffix = "" |
| 162 | } |
| 163 | |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 164 | return in |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 165 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 166 | |
| 167 | ctx.Module().SkipInstall() |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 168 | return nil |
| 169 | } |
| 170 | |
Jooyung Han | 31c470b | 2019-10-18 16:26:59 +0900 | [diff] [blame] | 171 | func (p *vndkPrebuiltLibraryDecorator) matchesWithDevice(config android.DeviceConfig) bool { |
| 172 | arches := config.Arches() |
| 173 | if len(arches) == 0 || arches[0].ArchType.String() != p.arch() { |
| 174 | return false |
| 175 | } |
| 176 | if config.BinderBitness() != p.binderBit() { |
| 177 | return false |
| 178 | } |
| 179 | if len(p.properties.Srcs) == 0 { |
| 180 | return false |
| 181 | } |
| 182 | return true |
| 183 | } |
| 184 | |
Inseob Kim | c9fa4a3 | 2019-09-09 10:49:03 +0900 | [diff] [blame] | 185 | func (p *vndkPrebuiltLibraryDecorator) nativeCoverage() bool { |
| 186 | return false |
| 187 | } |
| 188 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame^] | 189 | func (p *vndkPrebuiltLibraryDecorator) isSnapshotPrebuilt() bool { |
| 190 | return true |
| 191 | } |
| 192 | |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 193 | func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) { |
Justin Yun | 312ccb9 | 2018-01-23 12:07:46 +0900 | [diff] [blame] | 194 | arches := ctx.DeviceConfig().Arches() |
| 195 | if len(arches) == 0 || arches[0].ArchType.String() != p.arch() { |
| 196 | return |
| 197 | } |
Justin Yun | 3e2323d | 2018-06-08 15:08:40 +0900 | [diff] [blame] | 198 | if ctx.DeviceConfig().BinderBitness() != p.binderBit() { |
| 199 | return |
| 200 | } |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 201 | if p.shared() { |
Justin Yun | 3e15b96 | 2018-01-10 18:28:48 +0900 | [diff] [blame] | 202 | if ctx.isVndkSp() { |
| 203 | p.baseInstaller.subDir = "vndk-sp-" + p.version() |
| 204 | } else if ctx.isVndk() { |
| 205 | p.baseInstaller.subDir = "vndk-" + p.version() |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 206 | } |
| 207 | p.baseInstaller.install(ctx, file) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func vndkPrebuiltSharedLibrary() *Module { |
| 212 | module, library := NewLibrary(android.DeviceSupported) |
| 213 | library.BuildOnlyShared() |
| 214 | module.stl = nil |
| 215 | module.sanitize = nil |
| 216 | library.StripProperties.Strip.None = BoolPtr(true) |
| 217 | |
| 218 | prebuilt := &vndkPrebuiltLibraryDecorator{ |
| 219 | libraryDecorator: library, |
| 220 | } |
| 221 | |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 222 | prebuilt.properties.Check_elf_files = BoolPtr(false) |
Inseob Kim | 64c4395 | 2019-08-26 16:52:35 +0900 | [diff] [blame] | 223 | prebuilt.baseLinker.Properties.No_libcrt = BoolPtr(true) |
| 224 | prebuilt.baseLinker.Properties.Nocrt = BoolPtr(true) |
| 225 | |
| 226 | // Prevent default system libs (libc, libm, and libdl) from being linked |
| 227 | if prebuilt.baseLinker.Properties.System_shared_libs == nil { |
| 228 | prebuilt.baseLinker.Properties.System_shared_libs = []string{} |
| 229 | } |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 230 | |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 231 | module.compiler = nil |
| 232 | module.linker = prebuilt |
| 233 | module.installer = prebuilt |
| 234 | |
| 235 | module.AddProperties( |
| 236 | &prebuilt.properties, |
| 237 | ) |
| 238 | |
| 239 | return module |
| 240 | } |
| 241 | |
Patrice Arruda | ad03150 | 2019-04-01 10:56:49 -0700 | [diff] [blame] | 242 | // vndk_prebuilt_shared installs Vendor Native Development kit (VNDK) snapshot |
| 243 | // shared libraries for system build. Example: |
| 244 | // |
| 245 | // vndk_prebuilt_shared { |
| 246 | // name: "libfoo", |
Inseob Kim | eec88e1 | 2020-01-22 11:11:29 +0900 | [diff] [blame] | 247 | // version: "27", |
| 248 | // target_arch: "arm64", |
Patrice Arruda | ad03150 | 2019-04-01 10:56:49 -0700 | [diff] [blame] | 249 | // vendor_available: true, |
| 250 | // vndk: { |
| 251 | // enabled: true, |
| 252 | // }, |
| 253 | // export_include_dirs: ["include/external/libfoo/vndk_include"], |
| 254 | // arch: { |
| 255 | // arm64: { |
| 256 | // srcs: ["arm/lib64/libfoo.so"], |
| 257 | // }, |
| 258 | // arm: { |
| 259 | // srcs: ["arm/lib/libfoo.so"], |
| 260 | // }, |
| 261 | // }, |
| 262 | // } |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 263 | func VndkPrebuiltSharedFactory() android.Module { |
Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 264 | module := vndkPrebuiltSharedLibrary() |
| 265 | return module.Init() |
| 266 | } |
| 267 | |
| 268 | func init() { |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 269 | android.RegisterModuleType("vndk_prebuilt_shared", VndkPrebuiltSharedFactory) |
Colin Cross | 7a6fcbe | 2017-12-06 13:08:00 -0800 | [diff] [blame] | 270 | } |