Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 21 | "android/soong/android" |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 22 | "android/soong/cc/config" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 26 | android.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory) |
| 27 | android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory) |
| 28 | android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 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 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 37 | func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 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 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 48 | func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 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 | |
| 58 | type ndkPrebuiltObjectLinker struct { |
| 59 | objectLinker |
| 60 | } |
| 61 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 62 | func (*ndkPrebuiltObjectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 63 | // NDK objects can't have any dependencies |
| 64 | return deps |
| 65 | } |
| 66 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 67 | func ndkPrebuiltObjectFactory() android.Module { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 68 | module := newBaseModule(android.DeviceSupported, android.MultilibBoth) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 69 | module.linker = &ndkPrebuiltObjectLinker{ |
| 70 | objectLinker: objectLinker{ |
Dan Albert | 61f3212 | 2018-07-26 14:00:24 -0700 | [diff] [blame] | 71 | baseLinker: NewBaseLinker(nil), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 72 | }, |
| 73 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 74 | module.Properties.HideFromMake = true |
| 75 | return module.Init() |
| 76 | } |
| 77 | |
| 78 | func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 79 | deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 80 | // A null build step, but it sets up the output path. |
| 81 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") { |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 82 | ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion()) |
| 86 | } |
| 87 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 88 | type ndkPrebuiltStlLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 89 | *libraryDecorator |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 92 | func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 93 | return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 96 | func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 97 | // NDK libraries can't have any dependencies |
| 98 | return deps |
| 99 | } |
| 100 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 101 | func ndkPrebuiltSharedStlFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 102 | module, library := NewLibrary(android.DeviceSupported) |
| 103 | library.BuildOnlyShared() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 104 | module.compiler = nil |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 105 | module.linker = &ndkPrebuiltStlLinker{ |
| 106 | libraryDecorator: library, |
| 107 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 108 | module.installer = nil |
Dan Albert | 8ba131b | 2017-12-14 13:23:15 -0800 | [diff] [blame] | 109 | minVersionString := "minimum" |
| 110 | noStlString := "none" |
| 111 | module.Properties.Sdk_version = &minVersionString |
| 112 | module.stl.Properties.Stl = &noStlString |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 113 | return module.Init() |
| 114 | } |
| 115 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 116 | func ndkPrebuiltStaticStlFactory() android.Module { |
Colin Cross | ab3b732 | 2016-12-09 14:46:15 -0800 | [diff] [blame] | 117 | module, library := NewLibrary(android.DeviceSupported) |
| 118 | library.BuildOnlyStatic() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 119 | module.compiler = nil |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 120 | module.linker = &ndkPrebuiltStlLinker{ |
| 121 | libraryDecorator: library, |
| 122 | } |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 123 | module.installer = nil |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 124 | module.Properties.HideFromMake = true |
| 125 | return module.Init() |
| 126 | } |
| 127 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 128 | func 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]) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 134 | deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 135 | // A null build step, but it sets up the output path. |
| 136 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 137 | ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Colin Cross | 06080f4 | 2018-10-02 11:14:36 -0700 | [diff] [blame] | 140 | ndk.exportIncludes(ctx, "-isystem ") |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 141 | |
| 142 | libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") |
| 143 | libExt := flags.Toolchain.ShlibSuffix() |
Colin Cross | a48ab5b | 2017-02-14 15:28:44 -0800 | [diff] [blame] | 144 | if ndk.static() { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 145 | libExt = staticLibraryExtension |
| 146 | } |
| 147 | |
Ryan Prichard | b170365 | 2018-03-26 16:30:31 -0700 | [diff] [blame] | 148 | libDir := getNdkStlLibDir(ctx) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 149 | return libDir.Join(ctx, libName+libExt) |
| 150 | } |