1// Copyright 2016 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 "android/soong/android" 19) 20 21func init() { 22 RegisterPrebuiltBuildComponents(android.InitRegistrationContext) 23} 24 25func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) { 26 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory) 27 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory) 28 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory) 29 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory) 30 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory) 31} 32 33type prebuiltLinkerInterface interface { 34 Name(string) string 35 prebuilt() *android.Prebuilt 36} 37 38type prebuiltLinkerProperties struct { 39 40 // a prebuilt library or binary. Can reference a genrule module that generates an executable file. 41 Srcs []string `android:"path,arch_variant"` 42 43 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined 44 // symbols, etc), default true. 45 Check_elf_files *bool 46} 47 48type prebuiltLinker struct { 49 android.Prebuilt 50 51 properties prebuiltLinkerProperties 52} 53 54func (p *prebuiltLinker) prebuilt() *android.Prebuilt { 55 return &p.Prebuilt 56} 57 58func (p *prebuiltLinker) PrebuiltSrcs() []string { 59 return p.properties.Srcs 60} 61 62type prebuiltLibraryInterface interface { 63 libraryInterface 64 prebuiltLinkerInterface 65 disablePrebuilt() 66} 67 68type prebuiltLibraryLinker struct { 69 *libraryDecorator 70 prebuiltLinker 71} 72 73var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) 74var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil) 75 76func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} 77 78func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { 79 return p.libraryDecorator.linkerDeps(ctx, deps) 80} 81 82func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { 83 return flags 84} 85 86func (p *prebuiltLibraryLinker) linkerProps() []interface{} { 87 return p.libraryDecorator.linkerProps() 88} 89 90func (p *prebuiltLibraryLinker) link(ctx ModuleContext, 91 flags Flags, deps PathDeps, objs Objects) android.Path { 92 93 p.libraryDecorator.exportIncludes(ctx) 94 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...) 95 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...) 96 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...) 97 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...) 98 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) 99 100 // TODO(ccross): verify shared library dependencies 101 srcs := p.prebuiltSrcs() 102 if len(srcs) > 0 { 103 builderFlags := flagsToBuilderFlags(flags) 104 105 if len(srcs) > 1 { 106 ctx.PropertyErrorf("srcs", "multiple prebuilt source files") 107 return nil 108 } 109 110 in := android.PathForModuleSrc(ctx, srcs[0]) 111 112 if p.shared() { 113 p.unstrippedOutputFile = in 114 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix() 115 if p.needsStrip(ctx) { 116 stripped := android.PathForModuleOut(ctx, "stripped", libName) 117 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags) 118 in = stripped 119 } 120 121 // Optimize out relinking against shared libraries whose interface hasn't changed by 122 // depending on a table of contents file instead of the library itself. 123 tocFile := android.PathForModuleOut(ctx, libName+".toc") 124 p.tocFile = android.OptionalPathForPath(tocFile) 125 TransformSharedObjectToToc(ctx, in, tocFile, builderFlags) 126 } 127 128 return in 129 } 130 131 return nil 132} 133 134func (p *prebuiltLibraryLinker) prebuiltSrcs() []string { 135 srcs := p.properties.Srcs 136 if p.static() { 137 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...) 138 } 139 if p.shared() { 140 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...) 141 } 142 143 return srcs 144} 145 146func (p *prebuiltLibraryLinker) shared() bool { 147 return p.libraryDecorator.shared() 148} 149 150func (p *prebuiltLibraryLinker) nativeCoverage() bool { 151 return false 152} 153 154func (p *prebuiltLibraryLinker) disablePrebuilt() { 155 p.properties.Srcs = nil 156} 157 158func (p *prebuiltLibraryLinker) skipInstall(mod *Module) { 159 mod.ModuleBase.SkipInstall() 160} 161 162func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { 163 module, library := NewLibrary(hod) 164 module.compiler = nil 165 166 prebuilt := &prebuiltLibraryLinker{ 167 libraryDecorator: library, 168 } 169 module.linker = prebuilt 170 module.installer = prebuilt 171 172 module.AddProperties(&prebuilt.properties) 173 174 srcsSupplier := func() []string { 175 return prebuilt.prebuiltSrcs() 176 } 177 178 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") 179 180 // Prebuilt libraries can be used in SDKs. 181 android.InitSdkAwareModule(module) 182 return module, library 183} 184 185// cc_prebuilt_library installs a precompiled shared library that are 186// listed in the srcs property in the device's directory. 187func PrebuiltLibraryFactory() android.Module { 188 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported) 189 190 // Prebuilt shared libraries can be included in APEXes 191 android.InitApexModule(module) 192 193 return module.Init() 194} 195 196// cc_prebuilt_library_shared installs a precompiled shared library that are 197// listed in the srcs property in the device's directory. 198func PrebuiltSharedLibraryFactory() android.Module { 199 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) 200 return module.Init() 201} 202 203func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { 204 module, library := NewPrebuiltLibrary(hod) 205 library.BuildOnlyShared() 206 207 // Prebuilt shared libraries can be included in APEXes 208 android.InitApexModule(module) 209 210 return module, library 211} 212 213// cc_prebuilt_library_static installs a precompiled static library that are 214// listed in the srcs property in the device's directory. 215func PrebuiltStaticLibraryFactory() android.Module { 216 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) 217 return module.Init() 218} 219 220func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { 221 module, library := NewPrebuiltLibrary(hod) 222 library.BuildOnlyStatic() 223 return module, library 224} 225 226type prebuiltObjectProperties struct { 227 Srcs []string `android:"path,arch_variant"` 228} 229 230type prebuiltObjectLinker struct { 231 android.Prebuilt 232 objectLinker 233 234 properties prebuiltObjectProperties 235} 236 237func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { 238 return &p.Prebuilt 239} 240 241var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) 242 243func (p *prebuiltObjectLinker) link(ctx ModuleContext, 244 flags Flags, deps PathDeps, objs Objects) android.Path { 245 if len(p.properties.Srcs) > 0 { 246 return p.Prebuilt.SingleSourcePath(ctx) 247 } 248 return nil 249} 250 251func (p *prebuiltObjectLinker) object() bool { 252 return true 253} 254 255func newPrebuiltObject() *Module { 256 module := newObject() 257 prebuilt := &prebuiltObjectLinker{ 258 objectLinker: objectLinker{ 259 baseLinker: NewBaseLinker(nil), 260 }, 261 } 262 module.linker = prebuilt 263 module.AddProperties(&prebuilt.properties) 264 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) 265 android.InitSdkAwareModule(module) 266 return module 267} 268 269func prebuiltObjectFactory() android.Module { 270 module := newPrebuiltObject() 271 return module.Init() 272} 273 274type prebuiltBinaryLinker struct { 275 *binaryDecorator 276 prebuiltLinker 277} 278 279var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) 280 281func (p *prebuiltBinaryLinker) link(ctx ModuleContext, 282 flags Flags, deps PathDeps, objs Objects) android.Path { 283 // TODO(ccross): verify shared library dependencies 284 if len(p.properties.Srcs) > 0 { 285 builderFlags := flagsToBuilderFlags(flags) 286 287 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() 288 in := p.Prebuilt.SingleSourcePath(ctx) 289 290 p.unstrippedOutputFile = in 291 292 if p.needsStrip(ctx) { 293 stripped := android.PathForModuleOut(ctx, "stripped", fileName) 294 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags) 295 in = stripped 296 } 297 298 // Copy binaries to a name matching the final installed name 299 outputFile := android.PathForModuleOut(ctx, fileName) 300 ctx.Build(pctx, android.BuildParams{ 301 Rule: android.CpExecutable, 302 Description: "prebuilt", 303 Output: outputFile, 304 Input: in, 305 }) 306 307 return outputFile 308 } 309 310 return nil 311} 312 313func (p *prebuiltBinaryLinker) binary() bool { 314 return true 315} 316 317// cc_prebuilt_binary installs a precompiled executable in srcs property in the 318// device's directory. 319func prebuiltBinaryFactory() android.Module { 320 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) 321 return module.Init() 322} 323 324func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { 325 module, binary := NewBinary(hod) 326 module.compiler = nil 327 328 prebuilt := &prebuiltBinaryLinker{ 329 binaryDecorator: binary, 330 } 331 module.linker = prebuilt 332 333 module.AddProperties(&prebuilt.properties) 334 335 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) 336 return module, binary 337} 338