blob: f503982cdac7e0144a0413cb6b46f8e1f21eddc0 [file] [log] [blame]
Liana Kazanova08d5d262024-07-29 22:58:39 +00001// 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 "strings"
19
20 "android/soong/android"
21)
22
23func init() {
24 android.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
25 android.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
26}
27
28// NDK prebuilt libraries.
29//
30// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
31// either (with the exception of the shared STLs, which are installed to the app's directory rather
32// than to the system image).
33
34type ndkPrebuiltStlLinker struct {
35 *libraryDecorator
36}
37
38func (ndk *ndkPrebuiltStlLinker) linkerProps() []interface{} {
39 return append(ndk.libraryDecorator.linkerProps(), &ndk.Properties, &ndk.flagExporter.Properties)
40}
41
42func (*ndkPrebuiltStlLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
43 // NDK libraries can't have any dependencies
44 return deps
45}
46
47func (*ndkPrebuiltStlLinker) availableFor(what string) bool {
48 // ndk prebuilt objects are available to everywhere
49 return true
50}
51
52// ndk_prebuilt_shared_stl exports a precompiled ndk shared standard template
53// library (stl) library for linking operation. The soong's module name format
54// is ndk_<NAME>.so where the library is located under
55// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.so.
56func NdkPrebuiltSharedStlFactory() android.Module {
57 module, library := NewLibrary(android.DeviceSupported)
58 library.BuildOnlyShared()
59 module.compiler = nil
60 module.linker = &ndkPrebuiltStlLinker{
61 libraryDecorator: library,
62 }
63 module.installer = nil
64 module.Properties.Sdk_version = StringPtr("minimum")
65 module.Properties.AlwaysSdk = true
66 module.stl.Properties.Stl = StringPtr("none")
67 return module.Init()
68}
69
70// ndk_prebuilt_static_stl exports a precompiled ndk static standard template
71// library (stl) library for linking operation. The soong's module name format
72// is ndk_<NAME>.a where the library is located under
73// ./prebuilts/ndk/current/sources/cxx-stl/llvm-libc++/libs/$(HOST_ARCH)/<NAME>.a.
74func NdkPrebuiltStaticStlFactory() android.Module {
75 module, library := NewLibrary(android.DeviceSupported)
76 library.BuildOnlyStatic()
77 module.compiler = nil
78 module.linker = &ndkPrebuiltStlLinker{
79 libraryDecorator: library,
80 }
81 module.installer = nil
82 module.Properties.Sdk_version = StringPtr("minimum")
83 module.Properties.HideFromMake = true
84 module.Properties.AlwaysSdk = true
85 module.Properties.Sdk_version = StringPtr("current")
86 module.stl.Properties.Stl = StringPtr("none")
87 return module.Init()
88}
89
90const (
91 libDir = "current/sources/cxx-stl/llvm-libc++/libs"
92)
93
94func getNdkStlLibDir(ctx android.ModuleContext) android.SourcePath {
95 return android.PathForSource(ctx, ctx.ModuleDir(), libDir).Join(ctx, ctx.Arch().Abi[0])
96}
97
98func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
99 deps PathDeps, objs Objects) android.Path {
100 // A null build step, but it sets up the output path.
101 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
102 ctx.ModuleErrorf("NDK prebuilt libraries must have an ndk_lib prefixed name")
103 }
104
105 ndk.libraryDecorator.flagExporter.exportIncludesAsSystem(ctx)
106
107 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
108 libExt := flags.Toolchain.ShlibSuffix()
109 if ndk.static() {
110 libExt = staticLibraryExtension
111 }
112
113 libDir := getNdkStlLibDir(ctx)
114 lib := libDir.Join(ctx, libName+libExt)
115
116 ndk.libraryDecorator.flagExporter.setProvider(ctx)
117
118 if ndk.static() {
119 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(lib).Build()
120 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
121 StaticLibrary: lib,
122
123 TransitiveStaticLibrariesForOrdering: depSet,
124 })
125 } else {
126 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
127 SharedLibrary: lib,
128 Target: ctx.Target(),
129 })
130 }
131
132 return lib
133}