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 "strings" 19 20 "android/soong/android" 21) 22 23func init() { 24 android.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory) 25 android.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory) 26} 27 28// NDK prebuilt libraries. 29// 30// These differ from regular prebuilts in that they aren't stripped and usually aren't installed 31// either (with the exception of the shared STLs, which are installed to the app's directory rather 32// than to the system image). 33 34type ndkPrebuiltStlLinker struct { 35 *libraryDecorator 36} 37 38func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} { 39 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties) 40} 41 42func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { 43 // NDK libraries can't have any dependencies 44 return deps 45} 46 47func (*ndkPrebuiltStlLinker) availableFor(what string) bool { 48 // ndk prebuilt objects are available to everywhere 49 return true 50} 51 52// ndk_prebuilt_shared_stl exports a precompiled ndk shared standard template 53// library (stl) library for linking operation. The soong's module name format 54// is ndk_<NAME>.so where the library is located under 55// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.so. 56func NdkPrebuiltSharedStlFactory() android.Module { 57 module, library := NewLibrary(android.DeviceSupported) 58 library.BuildOnlyShared() 59 module.compiler = nil 60 module.linker = &ndkPrebuiltStlLinker{ 61 libraryDecorator: library, 62 } 63 module.installer = nil 64 module.Properties.Sdk_version = StringPtr("minimum") 65 module.Properties.AlwaysSdk = true 66 module.stl.Properties.Stl = StringPtr("none") 67 return module.Init() 68} 69 70// ndk_prebuilt_static_stl exports a precompiled ndk static standard template 71// library (stl) library for linking operation. The soong's module name format 72// is ndk_<NAME>.a where the library is located under 73// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.a. 74func NdkPrebuiltStaticStlFactory() android.Module { 75 module, library := NewLibrary(android.DeviceSupported) 76 library.BuildOnlyStatic() 77 module.compiler = nil 78 module.linker = &ndkPrebuiltStlLinker{ 79 libraryDecorator: library, 80 } 81 module.installer = nil 82 module.Properties.Sdk_version = StringPtr("minimum") 83 module.Properties.HideFromMake = true 84 module.Properties.AlwaysSdk = true 85 module.Properties.Sdk_version = StringPtr("current") 86 module.stl.Properties.Stl = StringPtr("none") 87 return module.Init() 88} 89 90func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath { 91 libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs" 92 return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0]) 93} 94 95func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags, 96 deps PathDeps, objs Objects) android.Path { 97 // A null build step, but it sets up the output path. 98 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { 99 ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name") 100 } 101 102 ndk.libraryDecorator.flagExporter.exportIncludesAsSystem(ctx) 103 104 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") 105 libExt := flags.Toolchain.ShlibSuffix() 106 if ndk.static() { 107 libExt = staticLibraryExtension 108 } 109 110 libDir := getNdkStlLibDir(ctx) 111 lib := libDir.Join(ctx, libName+libExt) 112 113 ndk.libraryDecorator.flagExporter.setProvider(ctx) 114 115 if ndk.static() { 116 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(lib).Build() 117 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ 118 StaticLibrary: lib, 119 120 TransitiveStaticLibrariesForOrdering: depSet, 121 }) 122 } else { 123 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ 124 SharedLibrary: lib, 125 Target: ctx.Target(), 126 }) 127 } 128 129 return lib 130} 131