blob: f909add434e73709837f69c36983ef87cf10287f [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() {
Jooyung Hanb90e4912019-12-09 18:21:48 +090026 android.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
27 android.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
Jaewoong Jung710756a2019-06-04 11:53:47 -070028 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
Patrice Arruda8c856372019-04-01 17:29:59 -070067// ndk_prebuilt_object exports a precompiled ndk object file for linking
68// operations. Soong's module name format is ndk_<NAME>.o.<sdk_version> where
69// the object is located under
70// ./prebuilts/ndk/current/platforms/android-<sdk_version>/arch-$(HOST_ARCH)/usr/lib/<NAME>.o.
Jooyung Hanb90e4912019-12-09 18:21:48 +090071func NdkPrebuiltObjectFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -070072 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
dimitry03dc3f62019-05-09 14:07:34 +020073 module.ModuleBase.EnableNativeBridgeSupportByDefault()
Colin Crossb916a382016-07-29 17:28:03 -070074 module.linker = &ndkPrebuiltObjectLinker{
75 objectLinker: objectLinker{
Dan Albert61f32122018-07-26 14:00:24 -070076 baseLinker: NewBaseLinker(nil),
Colin Crossb916a382016-07-29 17:28:03 -070077 },
78 }
Colin Crossc511bc52020-04-07 16:50:32 +000079 module.Properties.AlwaysSdk = true
80 module.Properties.Sdk_version = StringPtr("current")
Colin Cross4d9c2d12016-07-29 12:48:20 -070081 module.Properties.HideFromMake = true
82 return module.Init()
83}
84
85func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070086 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -070087 // A null build step, but it sets up the output path.
88 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
Ryan Prichardb1703652018-03-26 16:30:31 -070089 ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name")
Colin Cross4d9c2d12016-07-29 12:48:20 -070090 }
91
92 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
93}
94
Ryan Prichardb1703652018-03-26 16:30:31 -070095type ndkPrebuiltStlLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070096 *libraryDecorator
Colin Cross4d9c2d12016-07-29 12:48:20 -070097}
98
Ryan Prichardb1703652018-03-26 16:30:31 -070099func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} {
Colin Crossb916a382016-07-29 17:28:03 -0700100 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101}
102
Ryan Prichardb1703652018-03-26 16:30:31 -0700103func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700104 // NDK libraries can't have any dependencies
105 return deps
106}
107
Patrice Arruda8c856372019-04-01 17:29:59 -0700108// ndk_prebuilt_shared_stl exports a precompiled ndk shared standard template
109// library (stl) library for linking operation. The soong's module name format
110// is ndk_<NAME>.so where the library is located under
111// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.so.
Jaewoong Jung710756a2019-06-04 11:53:47 -0700112func NdkPrebuiltSharedStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800113 module, library := NewLibrary(android.DeviceSupported)
114 library.BuildOnlyShared()
Colin Crossb916a382016-07-29 17:28:03 -0700115 module.compiler = nil
Ryan Prichardb1703652018-03-26 16:30:31 -0700116 module.linker = &ndkPrebuiltStlLinker{
117 libraryDecorator: library,
118 }
Colin Crossb916a382016-07-29 17:28:03 -0700119 module.installer = nil
Colin Crossc511bc52020-04-07 16:50:32 +0000120 module.Properties.Sdk_version = StringPtr("minimum")
121 module.Properties.AlwaysSdk = true
122 module.stl.Properties.Stl = StringPtr("none")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123 return module.Init()
124}
125
Patrice Arruda8c856372019-04-01 17:29:59 -0700126// ndk_prebuilt_static_stl exports a precompiled ndk static standard template
127// library (stl) library for linking operation. The soong's module name format
128// is ndk_<NAME>.a where the library is located under
129// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.a.
Jooyung Hanb90e4912019-12-09 18:21:48 +0900130func NdkPrebuiltStaticStlFactory() android.Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800131 module, library := NewLibrary(android.DeviceSupported)
132 library.BuildOnlyStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700133 module.compiler = nil
Ryan Prichardb1703652018-03-26 16:30:31 -0700134 module.linker = &ndkPrebuiltStlLinker{
135 libraryDecorator: library,
136 }
Colin Crossb916a382016-07-29 17:28:03 -0700137 module.installer = nil
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 module.Properties.HideFromMake = true
Colin Crossc511bc52020-04-07 16:50:32 +0000139 module.Properties.AlwaysSdk = true
140 module.Properties.Sdk_version = StringPtr("current")
141 module.stl.Properties.Stl = StringPtr("none")
dimitry03dc3f62019-05-09 14:07:34 +0200142 module.ModuleBase.EnableNativeBridgeSupportByDefault()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700143 return module.Init()
144}
145
Ryan Prichardb1703652018-03-26 16:30:31 -0700146func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath {
147 libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs"
148 return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149}
150
151func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700152 deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 // A null build step, but it sets up the output path.
154 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
Ryan Prichardb1703652018-03-26 16:30:31 -0700155 ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156 }
157
Inseob Kim69378442019-06-03 19:10:47 +0900158 ndk.exportIncludesAsSystem(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159
160 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
161 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossa48ab5b2017-02-14 15:28:44 -0800162 if ndk.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700163 libExt = staticLibraryExtension
164 }
165
Ryan Prichardb1703652018-03-26 16:30:31 -0700166 libDir := getNdkStlLibDir(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 return libDir.Join(ctx, libName+libExt)
168}