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 15package cc 16 17import ( 18 "strings" 19 20 "android/soong/android" 21) 22 23var ( 24 vndkSuffix = ".vndk." 25 binder32Suffix = ".binder32" 26) 27 28// Creates vndk prebuilts that include the VNDK version. 29// 30// Example: 31// 32// vndk_prebuilt_shared { 33// name: "libfoo", 34// version: "27.1.0", 35// vendor_available: true, 36// vndk: { 37// enabled: true, 38// }, 39// export_include_dirs: ["include/external/libfoo/vndk_include"], 40// arch: { 41// arm64: { 42// srcs: ["arm/lib64/libfoo.so"], 43// }, 44// arm: { 45// srcs: ["arm/lib/libfoo.so"], 46// }, 47// }, 48// } 49// 50type vndkPrebuiltProperties struct { 51 // VNDK snapshot version. 52 Version *string 53 54 // Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64_ab') 55 Target_arch *string 56 57 // If the prebuilt snapshot lib is built with 32 bit binder, this must be set to true. 58 // The lib with 64 bit binder does not need to set this property. 59 Binder32bit *bool 60 61 // Prebuilt files for each arch. 62 Srcs []string `android:"arch_variant"` 63 64 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined symbols, 65 // etc). 66 Check_elf_files *bool 67} 68 69type vndkPrebuiltLibraryDecorator struct { 70 *libraryDecorator 71 properties vndkPrebuiltProperties 72} 73 74func (p *vndkPrebuiltLibraryDecorator) Name(name string) string { 75 return name + p.NameSuffix() 76} 77 78func (p *vndkPrebuiltLibraryDecorator) NameSuffix() string { 79 suffix := p.version() 80 if p.arch() != "" { 81 suffix += "." + p.arch() 82 } 83 if Bool(p.properties.Binder32bit) { 84 suffix += binder32Suffix 85 } 86 return vndkSuffix + suffix 87} 88 89func (p *vndkPrebuiltLibraryDecorator) version() string { 90 return String(p.properties.Version) 91} 92 93func (p *vndkPrebuiltLibraryDecorator) arch() string { 94 return String(p.properties.Target_arch) 95} 96 97func (p *vndkPrebuiltLibraryDecorator) binderBit() string { 98 if Bool(p.properties.Binder32bit) { 99 return "32" 100 } 101 return "64" 102} 103 104func (p *vndkPrebuiltLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { 105 p.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), p.NameSuffix()) 106 return p.libraryDecorator.linkerFlags(ctx, flags) 107} 108 109func (p *vndkPrebuiltLibraryDecorator) singleSourcePath(ctx ModuleContext) android.Path { 110 if len(p.properties.Srcs) == 0 { 111 ctx.PropertyErrorf("srcs", "missing prebuilt source file") 112 return nil 113 } 114 115 if len(p.properties.Srcs) > 1 { 116 ctx.PropertyErrorf("srcs", "multiple prebuilt source files") 117 return nil 118 } 119 120 return android.PathForModuleSrc(ctx, p.properties.Srcs[0]) 121} 122 123func (p *vndkPrebuiltLibraryDecorator) link(ctx ModuleContext, 124 flags Flags, deps PathDeps, objs Objects) android.Path { 125 if len(p.properties.Srcs) > 0 && p.shared() { 126 // current VNDK prebuilts are only shared libs. 127 return p.singleSourcePath(ctx) 128 } 129 return nil 130} 131 132func (p *vndkPrebuiltLibraryDecorator) install(ctx ModuleContext, file android.Path) { 133 arches := ctx.DeviceConfig().Arches() 134 if len(arches) == 0 || arches[0].ArchType.String() != p.arch() { 135 return 136 } 137 if ctx.DeviceConfig().BinderBitness() != p.binderBit() { 138 return 139 } 140 if p.shared() { 141 if ctx.isVndkSp() { 142 p.baseInstaller.subDir = "vndk-sp-" + p.version() 143 } else if ctx.isVndk() { 144 p.baseInstaller.subDir = "vndk-" + p.version() 145 } 146 p.baseInstaller.install(ctx, file) 147 } 148} 149 150func vndkPrebuiltSharedLibrary() *Module { 151 module, library := NewLibrary(android.DeviceSupported) 152 library.BuildOnlyShared() 153 module.stl = nil 154 module.sanitize = nil 155 library.StripProperties.Strip.None = BoolPtr(true) 156 module.Properties.UseVndk = true 157 158 prebuilt := &vndkPrebuiltLibraryDecorator{ 159 libraryDecorator: library, 160 } 161 162 prebuilt.properties.Check_elf_files = BoolPtr(false) 163 164 module.compiler = nil 165 module.linker = prebuilt 166 module.installer = prebuilt 167 168 module.AddProperties( 169 &prebuilt.properties, 170 ) 171 172 return module 173} 174 175func vndkPrebuiltSharedFactory() android.Module { 176 module := vndkPrebuiltSharedLibrary() 177 return module.Init() 178} 179 180func init() { 181 android.RegisterModuleType("vndk_prebuilt_shared", vndkPrebuiltSharedFactory) 182} 183