• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.linker = &ndkPrebuiltObjectLinker{
74		objectLinker: objectLinker{
75			baseLinker: NewBaseLinker(nil),
76		},
77	}
78	module.Properties.AlwaysSdk = true
79	module.Properties.Sdk_version = StringPtr("current")
80	module.Properties.HideFromMake = true
81	return module.Init()
82}
83
84func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
85	deps PathDeps, objs Objects) android.Path {
86	// A null build step, but it sets up the output path.
87	if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
88		ctx.ModuleErrorf("NDK prebuilt objects must have an ndk_crt prefixed name")
89	}
90
91	return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
92}
93
94func (*ndkPrebuiltObjectLinker) availableFor(what string) bool {
95	// ndk prebuilt objects are available to everywhere
96	return true
97}
98
99type ndkPrebuiltStlLinker struct {
100	*libraryDecorator
101}
102
103func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} {
104	return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
105}
106
107func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
108	// NDK libraries can't have any dependencies
109	return deps
110}
111
112func (*ndkPrebuiltStlLinker) availableFor(what string) bool {
113	// ndk prebuilt objects are available to everywhere
114	return true
115}
116
117// ndk_prebuilt_shared_stl exports a precompiled ndk shared standard template
118// library (stl) library for linking operation. The soong's module name format
119// is ndk_<NAME>.so where the library is located under
120// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.so.
121func NdkPrebuiltSharedStlFactory() android.Module {
122	module, library := NewLibrary(android.DeviceSupported)
123	library.BuildOnlyShared()
124	module.compiler = nil
125	module.linker = &ndkPrebuiltStlLinker{
126		libraryDecorator: library,
127	}
128	module.installer = nil
129	module.Properties.Sdk_version = StringPtr("minimum")
130	module.Properties.AlwaysSdk = true
131	module.stl.Properties.Stl = StringPtr("none")
132	return module.Init()
133}
134
135// ndk_prebuilt_static_stl exports a precompiled ndk static standard template
136// library (stl) library for linking operation. The soong's module name format
137// is ndk_<NAME>.a where the library is located under
138// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.a.
139func NdkPrebuiltStaticStlFactory() android.Module {
140	module, library := NewLibrary(android.DeviceSupported)
141	library.BuildOnlyStatic()
142	module.compiler = nil
143	module.linker = &ndkPrebuiltStlLinker{
144		libraryDecorator: library,
145	}
146	module.installer = nil
147	module.Properties.Sdk_version = StringPtr("minimum")
148	module.Properties.HideFromMake = true
149	module.Properties.AlwaysSdk = true
150	module.Properties.Sdk_version = StringPtr("current")
151	module.stl.Properties.Stl = StringPtr("none")
152	return module.Init()
153}
154
155func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath {
156	libDir := "prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs"
157	return android.PathForSource(ctx, libDir).Join(ctx, ctx.Arch().Abi[0])
158}
159
160func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
161	deps PathDeps, objs Objects) android.Path {
162	// A null build step, but it sets up the output path.
163	if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
164		ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
165	}
166
167	ndk.libraryDecorator.flagExporter.exportIncludesAsSystem(ctx)
168
169	libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
170	libExt := flags.Toolchain.ShlibSuffix()
171	if ndk.static() {
172		libExt = staticLibraryExtension
173	}
174
175	libDir := getNdkStlLibDir(ctx)
176	lib := libDir.Join(ctx, libName+libExt)
177
178	ndk.libraryDecorator.flagExporter.setProvider(ctx)
179
180	if ndk.static() {
181		depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(lib).Build()
182		ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
183			StaticLibrary: lib,
184
185			TransitiveStaticLibrariesForOrdering: depSet,
186		})
187	} else {
188		ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
189			SharedLibrary: lib,
190			Target:        ctx.Target(),
191		})
192	}
193
194	return lib
195}
196