blob: 7e8d989165215cc2869b69ae50e5394c0e829677 [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
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "android/soong/android"
Colin Crossb98c8b02016-07-29 13:44:28 -070022 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023)
24
25func init() {
Colin Cross798bfce2016-10-12 14:28:16 -070026 android.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
27 android.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
28 android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
29 android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070030}
31
32// NDK prebuilt libraries.
33//
34// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
35// either (with the exception of the shared STLs, which are installed to the app's directory rather
36// than to the system image).
37
Colin Crossb98c8b02016-07-29 13:44:28 -070038func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -070039 suffix := ""
40 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
41 // multilib toolchain and stores the libraries in "lib".
42 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
43 suffix = "64"
44 }
45 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
46 version, toolchain.Name(), suffix))
47}
48
Colin Crossb98c8b02016-07-29 13:44:28 -070049func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain,
Colin Cross4d9c2d12016-07-29 12:48:20 -070050 ext string, version string) android.Path {
51
52 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
53 // We want to translate to just NAME.EXT
54 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
55 dir := getNdkLibDir(ctx, toolchain, version)
56 return dir.Join(ctx, name+ext)
57}
58
59type ndkPrebuiltObjectLinker struct {
60 objectLinker
61}
62
Colin Cross37047f12016-12-13 17:06:13 -080063func (*ndkPrebuiltObjectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070064 // NDK objects can't have any dependencies
65 return deps
66}
67
Colin Cross36242852017-06-23 15:06:31 -070068func ndkPrebuiltObjectFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070069 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossb916a382016-07-29 17:28:03 -070070 module.linker = &ndkPrebuiltObjectLinker{
71 objectLinker: objectLinker{
72 baseLinker: NewBaseLinker(),
73 },
74 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070075 module.Properties.HideFromMake = true
76 return module.Init()
77}
78
79func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070080 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 // A null build step, but it sets up the output path.
82 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
83 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
84 }
85
86 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
87}
88
89type ndkPrebuiltLibraryLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070090 *libraryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -070091}
92
Colin Cross42742b82016-08-01 13:20:05 -070093func (ndk *ndkPrebuiltLibraryLinker) linkerProps() []interface{} {
Colin Crossb916a382016-07-29 17:28:03 -070094 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -070095}
96
Colin Cross37047f12016-12-13 17:06:13 -080097func (*ndkPrebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070098 // NDK libraries can't have any dependencies
99 return deps
100}
101
Colin Cross36242852017-06-23 15:06:31 -0700102func ndkPrebuiltLibraryFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800103 module, library := NewLibrary(android.DeviceSupported)
104 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700105 linker := &ndkPrebuiltLibraryLinker{
106 libraryDecorator: library,
107 }
108 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700109 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700110 module.installer = nil
111 module.stl = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112 module.Properties.HideFromMake = true
113 return module.Init()
114}
115
116func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700117 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700118 // A null build step, but it sets up the output path.
119 ndk.exportIncludes(ctx, "-isystem")
120
121 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
122 ctx.sdkVersion())
123}
124
125// The NDK STLs are slightly different from the prebuilt system libraries:
126// * Are not specific to each platform version.
127// * The libraries are not in a predictable location for each STL.
128
129type ndkPrebuiltStlLinker struct {
130 ndkPrebuiltLibraryLinker
131}
132
Colin Cross36242852017-06-23 15:06:31 -0700133func ndkPrebuiltSharedStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800134 module, library := NewLibrary(android.DeviceSupported)
135 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700136 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
Dan Albert8ba131b2017-12-14 13:23:15 -0800144 minVersionString := "minimum"
145 noStlString := "none"
146 module.Properties.Sdk_version = &minVersionString
147 module.stl.Properties.Stl = &noStlString
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 return module.Init()
149}
150
Colin Cross36242852017-06-23 15:06:31 -0700151func ndkPrebuiltStaticStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800152 module, library := NewLibrary(android.DeviceSupported)
153 library.BuildOnlyStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700154 linker := &ndkPrebuiltStlLinker{
155 ndkPrebuiltLibraryLinker: ndkPrebuiltLibraryLinker{
156 libraryDecorator: library,
157 },
158 }
159 module.compiler = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 module.linker = linker
Colin Crossb916a382016-07-29 17:28:03 -0700161 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 module.Properties.HideFromMake = true
163 return module.Init()
164}
165
Dan Albertc9460bb2017-12-20 15:51:30 -0800166func getNdkStlLibDir(ctx android.ModuleContext, stl string) android.SourcePath {
Dan Albert749fc782018-01-04 14:21:14 -0800167 libDir := "cxx-stl/llvm-libc++/libs"
168 ndkSrcRoot := "prebuilts/ndk/current/sources"
169 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170}
171
172func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700173 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 // A null build step, but it sets up the output path.
175 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
176 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
177 }
178
Colin Cross03cc1b62017-05-05 13:37:59 -0700179 ndk.exportIncludes(ctx, "-isystem")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180
181 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
182 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossa48ab5b2017-02-14 15:28:44 -0800183 if ndk.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700184 libExt = staticLibraryExtension
185 }
186
187 stlName := strings.TrimSuffix(libName, "_shared")
188 stlName = strings.TrimSuffix(stlName, "_static")
Dan Albertc9460bb2017-12-20 15:51:30 -0800189 libDir := getNdkStlLibDir(ctx, stlName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190 return libDir.Join(ctx, libName+libExt)
191}