blob: 40cd2072c8488ad164d1c7d81328a8c5c0c609ad [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
66func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
67 // NDK objects can't have any dependencies
68 return deps
69}
70
71func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
72 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
73 module.linker = &ndkPrebuiltObjectLinker{}
74 module.Properties.HideFromMake = true
75 return module.Init()
76}
77
78func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
79 deps PathDeps, objFiles android.Paths) android.Path {
80 // A null build step, but it sets up the output path.
81 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
82 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
83 }
84
85 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
86}
87
88type ndkPrebuiltLibraryLinker struct {
89 libraryLinker
90}
91
92var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
93var _ exportedFlagsProducer = (*libraryLinker)(nil)
94
95func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
96 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
97}
98
99func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
100 // NDK libraries can't have any dependencies
101 return deps
102}
103
104func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
105 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
106 linker := &ndkPrebuiltLibraryLinker{}
107 linker.dynamicProperties.BuildShared = true
108 module.linker = linker
109 module.Properties.HideFromMake = true
110 return module.Init()
111}
112
113func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
114 deps PathDeps, objFiles android.Paths) android.Path {
115 // A null build step, but it sets up the output path.
116 ndk.exportIncludes(ctx, "-isystem")
117
118 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
119 ctx.sdkVersion())
120}
121
122// The NDK STLs are slightly different from the prebuilt system libraries:
123// * Are not specific to each platform version.
124// * The libraries are not in a predictable location for each STL.
125
126type ndkPrebuiltStlLinker struct {
127 ndkPrebuiltLibraryLinker
128}
129
130func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
131 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
132 linker := &ndkPrebuiltStlLinker{}
133 linker.dynamicProperties.BuildShared = true
134 module.linker = linker
135 module.Properties.HideFromMake = true
136 return module.Init()
137}
138
139func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
140 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
141 linker := &ndkPrebuiltStlLinker{}
142 linker.dynamicProperties.BuildStatic = true
143 module.linker = linker
144 module.Properties.HideFromMake = true
145 return module.Init()
146}
147
Colin Crossb98c8b02016-07-29 13:44:28 -0700148func getNdkStlLibDir(ctx android.ModuleContext, toolchain config.Toolchain, stl string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149 gccVersion := toolchain.GccVersion()
150 var libDir string
151 switch stl {
152 case "libstlport":
153 libDir = "cxx-stl/stlport/libs"
154 case "libc++":
155 libDir = "cxx-stl/llvm-libc++/libs"
156 case "libgnustl":
157 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
158 }
159
160 if libDir != "" {
161 ndkSrcRoot := "prebuilts/ndk/current/sources"
162 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
163 }
164
165 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
166 return android.PathForSource(ctx, "")
167}
168
169func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
170 deps PathDeps, objFiles android.Paths) android.Path {
171 // A null build step, but it sets up the output path.
172 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
173 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
174 }
175
176 ndk.exportIncludes(ctx, "-I")
177
178 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
179 libExt := flags.Toolchain.ShlibSuffix()
180 if ndk.dynamicProperties.BuildStatic {
181 libExt = staticLibraryExtension
182 }
183
184 stlName := strings.TrimSuffix(libName, "_shared")
185 stlName = strings.TrimSuffix(stlName, "_static")
186 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
187 return libDir.Join(ctx, libName+libExt)
188}
189
190func linkageMutator(mctx android.BottomUpMutatorContext) {
191 if m, ok := mctx.Module().(*Module); ok {
192 if m.linker != nil {
193 if linker, ok := m.linker.(baseLinkerInterface); ok {
194 var modules []blueprint.Module
195 if linker.buildStatic() && linker.buildShared() {
196 modules = mctx.CreateLocalVariations("static", "shared")
197 static := modules[0].(*Module)
198 shared := modules[1].(*Module)
199
200 static.linker.(baseLinkerInterface).setStatic(true)
201 shared.linker.(baseLinkerInterface).setStatic(false)
202
203 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
204 sharedCompiler := shared.compiler.(*libraryCompiler)
205 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
206 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
207 // Optimize out compiling common .o files twice for static+shared libraries
208 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
209 sharedCompiler.baseCompiler.Properties.Srcs = nil
210 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
211 }
212 }
213 } else if linker.buildStatic() {
214 modules = mctx.CreateLocalVariations("static")
215 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
216 } else if linker.buildShared() {
217 modules = mctx.CreateLocalVariations("shared")
218 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
219 } else {
220 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
221 }
222 }
223 }
224 }
225}