blob: 63f8921019364dd56a09d904b8916855e9089dc1 [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
21 "github.com/google/blueprint"
22
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070024 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
26
27func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070028 android.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
29 android.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
30 android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
31 android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070032}
33
34// NDK prebuilt libraries.
35//
36// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
37// either (with the exception of the shared STLs, which are installed to the app's directory rather
38// than to the system image).
39
Colin Crossb98c8b02016-07-29 13:44:28 -070040func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -070041 suffix := ""
42 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
43 // multilib toolchain and stores the libraries in "lib".
44 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
45 suffix = "64"
46 }
47 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
48 version, toolchain.Name(), suffix))
49}
50
Colin Crossb98c8b02016-07-29 13:44:28 -070051func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain,
Colin Cross4d9c2d12016-07-29 12:48:20 -070052 ext string, version string) android.Path {
53
54 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
55 // We want to translate to just NAME.EXT
56 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
57 dir := getNdkLibDir(ctx, toolchain, version)
58 return dir.Join(ctx, name+ext)
59}
60
61type ndkPrebuiltObjectLinker struct {
62 objectLinker
63}
64
Colin Cross42742b82016-08-01 13:20:05 -070065func (*ndkPrebuiltObjectLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070066 // NDK objects can't have any dependencies
67 return deps
68}
69
70func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
71 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossb916a382016-07-29 17:28:03 -070072 module.linker = &ndkPrebuiltObjectLinker{
73 objectLinker: objectLinker{
74 baseLinker: NewBaseLinker(),
75 },
76 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070077 module.Properties.HideFromMake = true
78 return module.Init()
79}
80
81func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070082 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 // A null build step, but it sets up the output path.
84 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
85 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
86 }
87
88 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
89}
90
91type ndkPrebuiltLibraryLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070092 *libraryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -070093}
94
Colin Cross42742b82016-08-01 13:20:05 -070095func (ndk *ndkPrebuiltLibraryLinker) linkerProps() []interface{} {
Colin Crossb916a382016-07-29 17:28:03 -070096 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -070097}
98
Colin Cross42742b82016-08-01 13:20:05 -070099func (*ndkPrebuiltLibraryLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700100 // NDK libraries can't have any dependencies
101 return deps
102}
103
104func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800105 module, library := NewLibrary(android.DeviceSupported)
106 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700107 linker := &ndkPrebuiltLibraryLinker{
108 libraryDecorator: library,
109 }
110 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700112 module.installer = nil
113 module.stl = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700114 module.Properties.HideFromMake = true
115 return module.Init()
116}
117
118func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700119 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 // A null build step, but it sets up the output path.
121 ndk.exportIncludes(ctx, "-isystem")
122
123 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
124 ctx.sdkVersion())
125}
126
127// The NDK STLs are slightly different from the prebuilt system libraries:
128// * Are not specific to each platform version.
129// * The libraries are not in a predictable location for each STL.
130
131type ndkPrebuiltStlLinker struct {
132 ndkPrebuiltLibraryLinker
133}
134
135func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800136 module, library := NewLibrary(android.DeviceSupported)
137 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700138 linker := &ndkPrebuiltStlLinker{
139 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
140 libraryDecorator: library,
141 },
142 }
143 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700145 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146 module.Properties.HideFromMake = true
147 return module.Init()
148}
149
150func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800151 module, library := NewLibrary(android.DeviceSupported)
152 library.BuildOnlyStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700153 linker := &ndkPrebuiltStlLinker{
154 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
155 libraryDecorator: library,
156 },
157 }
158 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700160 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 module.Properties.HideFromMake = true
162 return module.Init()
163}
164
Colin Crossb98c8b02016-07-29 13:44:28 -0700165func getNdkStlLibDir(ctx android.ModuleContext, toolchain config.Toolchain, stl string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 gccVersion := toolchain.GccVersion()
167 var libDir string
168 switch stl {
169 case "libstlport":
170 libDir = "cxx-stl/stlport/libs"
171 case "libc++":
172 libDir = "cxx-stl/llvm-libc++/libs"
173 case "libgnustl":
174 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
175 }
176
177 if libDir != "" {
178 ndkSrcRoot := "prebuilts/ndk/current/sources"
179 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
180 }
181
182 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
183 return android.PathForSource(ctx, "")
184}
185
186func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700187 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700188 // A null build step, but it sets up the output path.
189 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
190 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
191 }
192
193 ndk.exportIncludes(ctx, "-I")
194
195 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
196 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossb916a382016-07-29 17:28:03 -0700197 if ndk.Properties.BuildStatic {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700198 libExt = staticLibraryExtension
199 }
200
201 stlName := strings.TrimSuffix(libName, "_shared")
202 stlName = strings.TrimSuffix(stlName, "_static")
203 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
204 return libDir.Join(ctx, libName+libExt)
205}