blob: 407a026aa27d358dc73d7abbb684fa31ea7eaaed [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
23 "android/soong"
24 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070025 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
28func init() {
29 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
30 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
31 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
32 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
33}
34
35// NDK prebuilt libraries.
36//
37// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
38// either (with the exception of the shared STLs, which are installed to the app's directory rather
39// than to the system image).
40
Colin Crossb98c8b02016-07-29 13:44:28 -070041func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -070042 suffix := ""
43 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
44 // multilib toolchain and stores the libraries in "lib".
45 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
46 suffix = "64"
47 }
48 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
49 version, toolchain.Name(), suffix))
50}
51
Colin Crossb98c8b02016-07-29 13:44:28 -070052func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain,
Colin Cross4d9c2d12016-07-29 12:48:20 -070053 ext string, version string) android.Path {
54
55 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
56 // We want to translate to just NAME.EXT
57 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
58 dir := getNdkLibDir(ctx, toolchain, version)
59 return dir.Join(ctx, name+ext)
60}
61
62type ndkPrebuiltObjectLinker struct {
63 objectLinker
64}
65
Colin Cross42742b82016-08-01 13:20:05 -070066func (*ndkPrebuiltObjectLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070067 // NDK objects can't have any dependencies
68 return deps
69}
70
71func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
72 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossb916a382016-07-29 17:28:03 -070073 module.linker = &ndkPrebuiltObjectLinker{
74 objectLinker: objectLinker{
75 baseLinker: NewBaseLinker(),
76 },
77 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070078 module.Properties.HideFromMake = true
79 return module.Init()
80}
81
82func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
83 deps PathDeps, objFiles android.Paths) android.Path {
84 // A null build step, but it sets up the output path.
85 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
86 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
87 }
88
89 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
90}
91
92type ndkPrebuiltLibraryLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070093 *libraryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -070094}
95
Colin Cross42742b82016-08-01 13:20:05 -070096func (ndk *ndkPrebuiltLibraryLinker) linkerProps() []interface{} {
Colin Crossb916a382016-07-29 17:28:03 -070097 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -070098}
99
Colin Cross42742b82016-08-01 13:20:05 -0700100func (*ndkPrebuiltLibraryLinker) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101 // NDK libraries can't have any dependencies
102 return deps
103}
104
105func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700106 module, library := NewLibrary(android.DeviceSupported, true, false)
107 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,
119 deps PathDeps, objFiles android.Paths) android.Path {
120 // 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 Crossb916a382016-07-29 17:28:03 -0700136 module, library := NewLibrary(android.DeviceSupported, true, false)
137 linker := &ndkPrebuiltStlLinker{
138 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
139 libraryDecorator: library,
140 },
141 }
142 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700144 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 module.Properties.HideFromMake = true
146 return module.Init()
147}
148
149func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Crossb916a382016-07-29 17:28:03 -0700150 module, library := NewLibrary(android.DeviceSupported, false, true)
151 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,
185 deps PathDeps, objFiles android.Paths) android.Path {
186 // 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
191 ndk.exportIncludes(ctx, "-I")
192
193 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
194 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossb916a382016-07-29 17:28:03 -0700195 if ndk.Properties.BuildStatic {
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}