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 "fmt" 19 "strings" 20 21 "android/soong/android" 22 "android/soong/cc/config" 23) 24 25func init() { 26 android.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory) 27 android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory) 28 android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory) 29} 30 31// NDK prebuilt libraries. 32// 33// These differ from regular prebuilts in that they aren't stripped and usually aren't installed 34// either (with the exception of the shared STLs, which are installed to the app's directory rather 35// than to the system image). 36 37func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath { 38 suffix := "" 39 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a 40 // multilib toolchain and stores the libraries in "lib". 41 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 { 42 suffix = "64" 43 } 44 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s", 45 version, toolchain.Name(), suffix)) 46} 47 48func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain, 49 ext string, version string) android.Path { 50 51 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION. 52 // We want to translate to just NAME.EXT 53 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0] 54 dir := getNdkLibDir(ctx, toolchain, version) 55 return dir.Join(ctx, name+ext) 56} 57 58type ndkPrebuiltObjectLinker struct { 59 objectLinker 60} 61 62func (*ndkPrebuiltObjectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { 63 // NDK objects can't have any dependencies 64 return deps 65} 66 67func ndkPrebuiltObjectFactory() android.Module { 68 module := newBaseModule(android.DeviceSupported, android.MultilibBoth) 69 module.linker = &ndkPrebuiltObjectLinker{ 70 objectLinker: objectLinker{ 71 baseLinker: NewBaseLinker(nil), 72 }, 73 } 74 module.Properties.HideFromMake = true 75 return module.Init() 76} 77 78func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags, 79 deps PathDeps, objs Objects) android.Path { 80 // A null build step, but it sets up the output path. 81 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") { 82 ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name") 83 } 84 85 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion()) 86} 87 88type ndkPrebuiltStlLinker struct { 89 *libraryDecorator 90} 91 92func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} { 93 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties) 94} 95 96func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { 97 // NDK libraries can't have any dependencies 98 return deps 99} 100 101func ndkPrebuiltSharedStlFactory() android.Module { 102 module, library := NewLibrary(android.DeviceSupported) 103 library.BuildOnlyShared() 104 module.compiler = nil 105 module.linker = &ndkPrebuiltStlLinker{ 106 libraryDecorator: library, 107 } 108 module.installer = nil 109 minVersionString := "minimum" 110 noStlString := "none" 111 module.Properties.Sdk_version = &minVersionString 112 module.stl.Properties.Stl = &noStlString 113 return module.Init() 114} 115 116func ndkPrebuiltStaticStlFactory() android.Module { 117 module, library := NewLibrary(android.DeviceSupported) 118 library.BuildOnlyStatic() 119 module.compiler = nil 120 module.linker = &ndkPrebuiltStlLinker{ 121 libraryDecorator: library, 122 } 123 module.installer = nil 124 module.Properties.HideFromMake = true 125 return module.Init() 126} 127 128func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath { 129 libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs" 130 return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0]) 131} 132 133func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags, 134 deps PathDeps, objs Objects) android.Path { 135 // A null build step, but it sets up the output path. 136 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { 137 ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name") 138 } 139 140 ndk.exportIncludes(ctx, "-isystem ") 141 142 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") 143 libExt := flags.Toolchain.ShlibSuffix() 144 if ndk.static() { 145 libExt = staticLibraryExtension 146 } 147 148 libDir := getNdkStlLibDir(ctx) 149 return libDir.Join(ctx, libName+libExt) 150} 151