blob: 106c9b51f64866b501e2624aab70ed7b8775ffc6 [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,
82 deps PathDeps, objFiles android.Paths) android.Path {
83 // 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 Crossb916a382016-07-29 17:28:03 -0700105 module, library := NewLibrary(android.DeviceSupported, true, false)
106 linker := &ndkPrebuiltLibraryLinker{
107 libraryDecorator: library,
108 }
109 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700111 module.installer = nil
112 module.stl = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 module.Properties.HideFromMake = true
114 return module.Init()
115}
116
117func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
118 deps PathDeps, objFiles android.Paths) android.Path {
119 // A null build step, but it sets up the output path.
120 ndk.exportIncludes(ctx, "-isystem")
121
122 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
123 ctx.sdkVersion())
124}
125
126// The NDK STLs are slightly different from the prebuilt system libraries:
127// * Are not specific to each platform version.
128// * The libraries are not in a predictable location for each STL.
129
130type ndkPrebuiltStlLinker struct {
131 ndkPrebuiltLibraryLinker
132}
133
134func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700135 module, library := NewLibrary(android.DeviceSupported, true, false)
136 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
148func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700149 module, library := NewLibrary(android.DeviceSupported, false, true)
150 linker := &ndkPrebuiltStlLinker{
151 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
152 libraryDecorator: library,
153 },
154 }
155 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700157 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700158 module.Properties.HideFromMake = true
159 return module.Init()
160}
161
Colin Crossb98c8b02016-07-29 13:44:28 -0700162func getNdkStlLibDir(ctx android.ModuleContext, toolchain config.Toolchain, stl string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 gccVersion := toolchain.GccVersion()
164 var libDir string
165 switch stl {
166 case "libstlport":
167 libDir = "cxx-stl/stlport/libs"
168 case "libc++":
169 libDir = "cxx-stl/llvm-libc++/libs"
170 case "libgnustl":
171 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
172 }
173
174 if libDir != "" {
175 ndkSrcRoot := "prebuilts/ndk/current/sources"
176 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
177 }
178
179 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
180 return android.PathForSource(ctx, "")
181}
182
183func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
184 deps PathDeps, objFiles android.Paths) android.Path {
185 // A null build step, but it sets up the output path.
186 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
187 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
188 }
189
190 ndk.exportIncludes(ctx, "-I")
191
192 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
193 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossb916a382016-07-29 17:28:03 -0700194 if ndk.Properties.BuildStatic {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195 libExt = staticLibraryExtension
196 }
197
198 stlName := strings.TrimSuffix(libName, "_shared")
199 stlName = strings.TrimSuffix(stlName, "_static")
200 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
201 return libDir.Join(ctx, libName+libExt)
202}