blob: 2a7e6573ad094244a684fd2b8464011b4a454e39 [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_object", ndkPrebuiltObjectFactory)
27 android.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
28 android.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -070029}
30
31// NDK prebuilt libraries.
32//
33// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
34// either (with the exception of the shared STLs, which are installed to the app's directory rather
35// than to the system image).
36
Colin Crossb98c8b02016-07-29 13:44:28 -070037func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath {
Colin Cross4d9c2d12016-07-29 12:48:20 -070038 suffix := ""
39 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
40 // multilib toolchain and stores the libraries in "lib".
41 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
42 suffix = "64"
43 }
44 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
45 version, toolchain.Name(), suffix))
46}
47
Colin Crossb98c8b02016-07-29 13:44:28 -070048func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain,
Colin Cross4d9c2d12016-07-29 12:48:20 -070049 ext string, version string) android.Path {
50
51 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
52 // We want to translate to just NAME.EXT
53 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
54 dir := getNdkLibDir(ctx, toolchain, version)
55 return dir.Join(ctx, name+ext)
56}
57
58type ndkPrebuiltObjectLinker struct {
59 objectLinker
60}
61
Colin Cross37047f12016-12-13 17:06:13 -080062func (*ndkPrebuiltObjectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070063 // NDK objects can't have any dependencies
64 return deps
65}
66
Colin Cross36242852017-06-23 15:06:31 -070067func ndkPrebuiltObjectFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070068 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossb916a382016-07-29 17:28:03 -070069 module.linker = &ndkPrebuiltObjectLinker{
70 objectLinker: objectLinker{
Dan Albert61f32122018-07-26 14:00:24 -070071 baseLinker: NewBaseLinker(nil),
Colin Crossb916a382016-07-29 17:28:03 -070072 },
73 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070074 module.Properties.HideFromMake = true
75 return module.Init()
76}
77
78func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070079 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 // A null build step, but it sets up the output path.
81 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
Ryan Prichardb1703652018-03-26 16:30:31 -070082 ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name")
Colin Cross4d9c2d12016-07-29 12:48:20 -070083 }
84
85 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
86}
87
Ryan Prichardb1703652018-03-26 16:30:31 -070088type ndkPrebuiltStlLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070089 *libraryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -070090}
91
Ryan Prichardb1703652018-03-26 16:30:31 -070092func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} {
Colin Crossb916a382016-07-29 17:28:03 -070093 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -070094}
95
Ryan Prichardb1703652018-03-26 16:30:31 -070096func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -070097 // NDK libraries can't have any dependencies
98 return deps
99}
100
Colin Cross36242852017-06-23 15:06:31 -0700101func ndkPrebuiltSharedStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800102 module, library := NewLibrary(android.DeviceSupported)
103 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700104 module.compiler = nil
Ryan Prichardb1703652018-03-26 16:30:31 -0700105 module.linker = &ndkPrebuiltStlLinker{
106 libraryDecorator: library,
107 }
Colin Crossb916a382016-07-29 17:28:03 -0700108 module.installer = nil
Dan Albert8ba131b2017-12-14 13:23:15 -0800109 minVersionString := "minimum"
110 noStlString := "none"
111 module.Properties.Sdk_version = &minVersionString
112 module.stl.Properties.Stl = &noStlString
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 return module.Init()
114}
115
Colin Cross36242852017-06-23 15:06:31 -0700116func ndkPrebuiltStaticStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800117 module, library := NewLibrary(android.DeviceSupported)
118 library.BuildOnlyStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700119 module.compiler = nil
Ryan Prichardb1703652018-03-26 16:30:31 -0700120 module.linker = &ndkPrebuiltStlLinker{
121 libraryDecorator: library,
122 }
Colin Crossb916a382016-07-29 17:28:03 -0700123 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 module.Properties.HideFromMake = true
125 return module.Init()
126}
127
Ryan Prichardb1703652018-03-26 16:30:31 -0700128func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath {
129 libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs"
130 return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131}
132
133func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700134 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700135 // A null build step, but it sets up the output path.
136 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
Ryan Prichardb1703652018-03-26 16:30:31 -0700137 ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 }
139
Colin Cross06080f42018-10-02 11:14:36 -0700140 ndk.exportIncludes(ctx, "-isystem ")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141
142 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
143 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossa48ab5b2017-02-14 15:28:44 -0800144 if ndk.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700145 libExt = staticLibraryExtension
146 }
147
Ryan Prichardb1703652018-03-26 16:30:31 -0700148 libDir := getNdkStlLibDir(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149 return libDir.Join(ctx, libName+libExt)
150}