1// 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	"android/soong/android"
22	"android/soong/cc/config"
23)
24
25func init() {
26	android.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
27	android.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
28	android.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
29}
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
37func getNdkLibDir(ctx android.ModuleContext, toolchain config.Toolchain, version string) android.SourcePath {
38	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
48func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain config.Toolchain,
49	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
62func (*ndkPrebuiltObjectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
63	// NDK objects can't have any dependencies
64	return deps
65}
66
67// 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.
71func NdkPrebuiltObjectFactory() android.Module {
72	module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
73	module.ModuleBase.EnableNativeBridgeSupportByDefault()
74	module.linker = &ndkPrebuiltObjectLinker{
75		objectLinker: objectLinker{
76			baseLinker: NewBaseLinker(nil),
77		},
78	}
79	module.Properties.AlwaysSdk = true
80	module.Properties.Sdk_version = StringPtr("current")
81	module.Properties.HideFromMake = true
82	return module.Init()
83}
84
85func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
86	deps PathDeps, objs Objects) android.Path {
87	// A null build step, but it sets up the output path.
88	if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
89		ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name")
90	}
91
92	return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
93}
94
95func (*ndkPrebuiltObjectLinker) availableFor(what string) bool {
96	// ndk prebuilt objects are available to everywhere
97	return true
98}
99
100type ndkPrebuiltStlLinker struct {
101	*libraryDecorator
102}
103
104func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} {
105	return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
106}
107
108func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
109	// NDK libraries can't have any dependencies
110	return deps
111}
112
113func (*ndkPrebuiltStlLinker) availableFor(what string) bool {
114	// ndk prebuilt objects are available to everywhere
115	return true
116}
117
118// ndk_prebuilt_shared_stl exports a precompiled ndk shared standard template
119// library (stl) library for linking operation. The soong's module name format
120// is ndk_<NAME>.so where the library is located under
121// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.so.
122func NdkPrebuiltSharedStlFactory() android.Module {
123	module, library := NewLibrary(android.DeviceSupported)
124	library.BuildOnlyShared()
125	module.compiler = nil
126	module.linker = &ndkPrebuiltStlLinker{
127		libraryDecorator: library,
128	}
129	module.installer = nil
130	module.Properties.Sdk_version = StringPtr("minimum")
131	module.Properties.AlwaysSdk = true
132	module.stl.Properties.Stl = StringPtr("none")
133	return module.Init()
134}
135
136// ndk_prebuilt_static_stl exports a precompiled ndk static standard template
137// library (stl) library for linking operation. The soong's module name format
138// is ndk_<NAME>.a where the library is located under
139// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.a.
140func NdkPrebuiltStaticStlFactory() android.Module {
141	module, library := NewLibrary(android.DeviceSupported)
142	library.BuildOnlyStatic()
143	module.compiler = nil
144	module.linker = &ndkPrebuiltStlLinker{
145		libraryDecorator: library,
146	}
147	module.installer = nil
148	module.Properties.Sdk_version = StringPtr("minimum")
149	module.Properties.HideFromMake = true
150	module.Properties.AlwaysSdk = true
151	module.Properties.Sdk_version = StringPtr("current")
152	module.stl.Properties.Stl = StringPtr("none")
153	module.ModuleBase.EnableNativeBridgeSupportByDefault()
154	return module.Init()
155}
156
157func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath {
158	libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs"
159	return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0])
160}
161
162func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
163	deps PathDeps, objs Objects) android.Path {
164	// A null build step, but it sets up the output path.
165	if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
166		ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
167	}
168
169	ndk.exportIncludesAsSystem(ctx)
170
171	libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
172	libExt := flags.Toolchain.ShlibSuffix()
173	if ndk.static() {
174		libExt = staticLibraryExtension
175	}
176
177	libDir := getNdkStlLibDir(ctx)
178	return libDir.Join(ctx, libName+libExt)
179}
180