blob: 13424e3cffd090de6161dbc6e8a72d36a9c9d32e [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070022 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023)
24
25func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070026 android.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
27 android.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
28 android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
29 android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070030}
31
32// NDK prebuilt libraries.
33//
34// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
35// either (with the exception of the shared STLs, which are installed to the app's directory rather
36// than to the system image).
37
Colin Crossb98c8b02016-07-29 13:44:28 -070038func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -070039 suffix := ""
40 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
41 // multilib toolchain and stores the libraries in "lib".
42 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
43 suffix = "64"
44 }
45 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
46 version, toolchain.Name(), suffix))
47}
48
Colin Crossb98c8b02016-07-29 13:44:28 -070049func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain,
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 ext string, version string) android.Path {
51
52 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
53 // We want to translate to just NAME.EXT
54 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
55 dir := getNdkLibDir(ctx, toolchain, version)
56 return dir.Join(ctx, name+ext)
57}
58
59type ndkPrebuiltObjectLinker struct {
60 objectLinker
61}
62
Colin Cross37047f12016-12-13 17:06:13 -080063func (*ndkPrebuiltObjectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070064 // NDK objects can't have any dependencies
65 return deps
66}
67
Colin Cross36242852017-06-23 15:06:31 -070068func ndkPrebuiltObjectFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070069 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossb916a382016-07-29 17:28:03 -070070 module.linker = &ndkPrebuiltObjectLinker{
71 objectLinker: objectLinker{
72 baseLinker: NewBaseLinker(),
73 },
74 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070075 module.Properties.HideFromMake = true
76 return module.Init()
77}
78
79func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070080 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 // A null build step, but it sets up the output path.
82 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
83 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
84 }
85
86 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
87}
88
89type ndkPrebuiltLibraryLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070090 *libraryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -070091}
92
Colin Cross42742b82016-08-01 13:20:05 -070093func (ndk *ndkPrebuiltLibraryLinker) linkerProps() []interface{} {
Colin Crossb916a382016-07-29 17:28:03 -070094 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -070095}
96
Colin Cross37047f12016-12-13 17:06:13 -080097func (*ndkPrebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 // NDK libraries can't have any dependencies
99 return deps
100}
101
Colin Cross36242852017-06-23 15:06:31 -0700102func ndkPrebuiltLibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800103 module, library := NewLibrary(android.DeviceSupported)
104 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700105 linker := &ndkPrebuiltLibraryLinker{
106 libraryDecorator: library,
107 }
108 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700110 module.installer = nil
111 module.stl = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 module.Properties.HideFromMake = true
113 return module.Init()
114}
115
116func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700117 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 // A null build step, but it sets up the output path.
119 ndk.exportIncludes(ctx, "-isystem")
120
121 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
122 ctx.sdkVersion())
123}
124
125// The NDK STLs are slightly different from the prebuilt system libraries:
126// * Are not specific to each platform version.
127// * The libraries are not in a predictable location for each STL.
128
129type ndkPrebuiltStlLinker struct {
130 ndkPrebuiltLibraryLinker
131}
132
Colin Cross36242852017-06-23 15:06:31 -0700133func ndkPrebuiltSharedStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800134 module, library := NewLibrary(android.DeviceSupported)
135 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700136 linker := &ndkPrebuiltStlLinker{
137 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
138 libraryDecorator: library,
139 },
140 }
141 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700142 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700143 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 module.Properties.HideFromMake = true
145 return module.Init()
146}
147
Colin Cross36242852017-06-23 15:06:31 -0700148func ndkPrebuiltStaticStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800149 module, library := NewLibrary(android.DeviceSupported)
150 library.BuildOnlyStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700151 linker := &ndkPrebuiltStlLinker{
152 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
153 libraryDecorator: library,
154 },
155 }
156 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700157 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700158 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 module.Properties.HideFromMake = true
160 return module.Init()
161}
162
Colin Crossb98c8b02016-07-29 13:44:28 -0700163func getNdkStlLibDir(ctx android.ModuleContext, toolchain config.Toolchain, stl string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 gccVersion := toolchain.GccVersion()
165 var libDir string
166 switch stl {
167 case "libstlport":
168 libDir = "cxx-stl/stlport/libs"
169 case "libc++":
170 libDir = "cxx-stl/llvm-libc++/libs"
171 case "libgnustl":
172 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
173 }
174
175 if libDir != "" {
176 ndkSrcRoot := "prebuilts/ndk/current/sources"
177 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
178 }
179
180 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
181 return android.PathForSource(ctx, "")
182}
183
184func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700185 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 // A null build step, but it sets up the output path.
187 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
188 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
189 }
190
Colin Cross03cc1b62017-05-05 13:37:59 -0700191 ndk.exportIncludes(ctx, "-isystem")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192
193 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
194 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossa48ab5b2017-02-14 15:28:44 -0800195 if ndk.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700196 libExt = staticLibraryExtension
197 }
198
199 stlName := strings.TrimSuffix(libName, "_shared")
200 stlName = strings.TrimSuffix(stlName, "_static")
201 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
202 return libDir.Join(ctx, libName+libExt)
203}