1// Copyright 2018 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 "sync" 20 21 "android/soong/android" 22) 23 24var ( 25 vendorPublicLibrarySuffix = ".vendorpublic" 26 27 vendorPublicLibraries = []string{} 28 vendorPublicLibrariesLock sync.Mutex 29) 30 31// Creates a stub shared library for a vendor public library. Vendor public libraries 32// are vendor libraries (owned by them and installed to /vendor partition) that are 33// exposed to Android apps via JNI. The libraries are made public by being listed in 34// /vendor/etc/public.libraries.txt. 35// 36// This stub library is a build-time only artifact that provides symbols that are 37// exposed from a vendor public library. 38// 39// Example: 40// 41// vendor_public_library { 42// name: "libfoo", 43// symbol_file: "libfoo.map.txt", 44// export_public_headers: ["libfoo_headers"], 45// } 46// 47// cc_headers { 48// name: "libfoo_headers", 49// export_include_dirs: ["include"], 50// } 51// 52type vendorPublicLibraryProperties struct { 53 // Relative path to the symbol map. 54 Symbol_file *string 55 56 // Whether the system library uses symbol versions. 57 Unversioned *bool 58 59 // list of header libs to re-export include directories from. 60 Export_public_headers []string `android:"arch_variant"` 61} 62 63type vendorPublicLibraryStubDecorator struct { 64 *libraryDecorator 65 66 Properties vendorPublicLibraryProperties 67 68 versionScriptPath android.ModuleGenPath 69} 70 71func (stub *vendorPublicLibraryStubDecorator) Name(name string) string { 72 return name + vendorPublicLibrarySuffix 73} 74 75func (stub *vendorPublicLibraryStubDecorator) compilerInit(ctx BaseModuleContext) { 76 stub.baseCompiler.compilerInit(ctx) 77 78 name := ctx.baseModuleName() 79 if strings.HasSuffix(name, vendorPublicLibrarySuffix) { 80 ctx.PropertyErrorf("name", "Do not append %q manually, just use the base name", vendorPublicLibrarySuffix) 81 } 82 83 vendorPublicLibrariesLock.Lock() 84 defer vendorPublicLibrariesLock.Unlock() 85 for _, lib := range vendorPublicLibraries { 86 if lib == name { 87 return 88 } 89 } 90 vendorPublicLibraries = append(vendorPublicLibraries, name) 91} 92 93func (stub *vendorPublicLibraryStubDecorator) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags { 94 flags = stub.baseCompiler.compilerFlags(ctx, flags, deps) 95 return addStubLibraryCompilerFlags(flags) 96} 97 98func (stub *vendorPublicLibraryStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects { 99 objs, versionScript := compileStubLibrary(ctx, flags, String(stub.Properties.Symbol_file), "current", "") 100 stub.versionScriptPath = versionScript 101 return objs 102} 103 104func (stub *vendorPublicLibraryStubDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps { 105 headers := stub.Properties.Export_public_headers 106 deps.HeaderLibs = append(deps.HeaderLibs, headers...) 107 deps.ReexportHeaderLibHeaders = append(deps.ReexportHeaderLibHeaders, headers...) 108 return deps 109} 110 111func (stub *vendorPublicLibraryStubDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags { 112 stub.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), vendorPublicLibrarySuffix) 113 return stub.libraryDecorator.linkerFlags(ctx, flags) 114} 115 116func (stub *vendorPublicLibraryStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, 117 objs Objects) android.Path { 118 if !Bool(stub.Properties.Unversioned) { 119 linkerScriptFlag := "-Wl,--version-script," + stub.versionScriptPath.String() 120 flags.LdFlags = append(flags.LdFlags, linkerScriptFlag) 121 } 122 return stub.libraryDecorator.link(ctx, flags, deps, objs) 123} 124 125func vendorPublicLibraryFactory() android.Module { 126 module, library := NewLibrary(android.DeviceSupported) 127 library.BuildOnlyShared() 128 module.stl = nil 129 module.sanitize = nil 130 library.StripProperties.Strip.None = BoolPtr(true) 131 132 stub := &vendorPublicLibraryStubDecorator{ 133 libraryDecorator: library, 134 } 135 module.compiler = stub 136 module.linker = stub 137 module.installer = nil 138 139 module.AddProperties( 140 &stub.Properties, 141 &library.MutatedProperties, 142 &library.flagExporter.Properties) 143 144 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth) 145 return module 146} 147 148func init() { 149 android.RegisterModuleType("vendor_public_library", vendorPublicLibraryFactory) 150} 151