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