blob: 14d1b963abd78380c7b9981b1b4d6a50ada177d4 [file] [log] [blame]
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001// Copyright 2021 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 "android/soong/android"
19 "android/soong/multitree"
Kiyoung Kim487689e2022-07-26 09:48:22 +090020 "strings"
Inseob Kim5eb7ee92022-04-27 10:30:34 +090021)
22
23func init() {
24 RegisterLibraryStubBuildComponents(android.InitRegistrationContext)
25}
26
27func RegisterLibraryStubBuildComponents(ctx android.RegistrationContext) {
28 // cc_api_stub_library shares a lot of ndk_library, and this will be refactored later
Kiyoung Kim487689e2022-07-26 09:48:22 +090029 ctx.RegisterModuleType("cc_api_library", CcApiLibraryFactory)
Inseob Kim5eb7ee92022-04-27 10:30:34 +090030 ctx.RegisterModuleType("cc_api_stub_library", CcApiStubLibraryFactory)
31 ctx.RegisterModuleType("cc_api_contribution", CcApiContributionFactory)
32}
33
Kiyoung Kim487689e2022-07-26 09:48:22 +090034// 'cc_api_library' is a module type which is from the exported API surface
35// with C shared library type. The module will replace original module, and
36// offer a link to the module that generates shared library object from the
37// map file.
38type apiLibraryProperties struct {
39 Src *string `android:"arch_variant"`
40}
41
42type apiLibraryDecorator struct {
43 *libraryDecorator
44 properties apiLibraryProperties
45}
46
47func CcApiLibraryFactory() android.Module {
48 module, decorator := NewLibrary(android.DeviceSupported)
49 apiLibraryDecorator := &apiLibraryDecorator{
50 libraryDecorator: decorator,
51 }
52 apiLibraryDecorator.BuildOnlyShared()
53
54 module.stl = nil
55 module.sanitize = nil
56 decorator.disableStripping()
57
58 module.compiler = nil
59 module.linker = apiLibraryDecorator
60 module.installer = nil
61 module.AddProperties(&module.Properties, &apiLibraryDecorator.properties)
62
63 // Mark module as stub, so APEX would not include this stub in the package.
64 module.library.setBuildStubs(true)
65
66 // Prevent default system libs (libc, libm, and libdl) from being linked
67 if apiLibraryDecorator.baseLinker.Properties.System_shared_libs == nil {
68 apiLibraryDecorator.baseLinker.Properties.System_shared_libs = []string{}
69 }
70
71 module.Init()
72
73 return module
74}
75
76func (d *apiLibraryDecorator) Name(basename string) string {
77 return basename + multitree.GetApiImportSuffix()
78}
79
80func (d *apiLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path {
81 // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
82 d.libraryDecorator.flagExporter.exportIncludes(ctx)
83 d.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
84 d.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
85 d.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
86 d.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
87 d.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
88 d.libraryDecorator.flagExporter.setProvider(ctx)
89
90 in := android.PathForModuleSrc(ctx, *d.properties.Src)
91
92 d.unstrippedOutputFile = in
93 libName := d.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
94
95 tocFile := android.PathForModuleOut(ctx, libName+".toc")
96 d.tocFile = android.OptionalPathForPath(tocFile)
97 TransformSharedObjectToToc(ctx, in, tocFile)
98
99 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
100 SharedLibrary: in,
101 Target: ctx.Target(),
102
103 TableOfContents: d.tocFile,
104 })
105
106 return in
107}
108
109func (d *apiLibraryDecorator) availableFor(what string) bool {
110 // Stub from API surface should be available for any APEX.
111 return true
112}
113
114func (d *apiLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
115 d.libraryDecorator.libName = strings.TrimSuffix(ctx.ModuleName(), multitree.GetApiImportSuffix())
116 return d.libraryDecorator.linkerFlags(ctx, flags)
117}
118
Inseob Kim5eb7ee92022-04-27 10:30:34 +0900119func CcApiStubLibraryFactory() android.Module {
120 module, decorator := NewLibrary(android.DeviceSupported)
121 apiStubDecorator := &apiStubDecorator{
122 libraryDecorator: decorator,
123 }
124 apiStubDecorator.BuildOnlyShared()
125
126 module.compiler = apiStubDecorator
127 module.linker = apiStubDecorator
128 module.installer = nil
129 module.library = apiStubDecorator
130 module.Properties.HideFromMake = true // TODO: remove
131
132 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibBoth)
133 module.AddProperties(&module.Properties,
134 &apiStubDecorator.properties,
135 &apiStubDecorator.MutatedProperties,
136 &apiStubDecorator.apiStubLibraryProperties)
137 return module
138}
139
140type apiStubLiraryProperties struct {
141 Imported_includes []string `android:"path"`
142}
143
144type apiStubDecorator struct {
145 *libraryDecorator
146 properties libraryProperties
147 apiStubLibraryProperties apiStubLiraryProperties
148}
149
150func (compiler *apiStubDecorator) stubsVersions(ctx android.BaseMutatorContext) []string {
151 firstVersion := String(compiler.properties.First_version)
152 return ndkLibraryVersions(ctx, android.ApiLevelOrPanic(ctx, firstVersion))
153}
154
155func (decorator *apiStubDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
156 if decorator.stubsVersion() == "" {
157 decorator.setStubsVersion("current")
158 } // TODO: fix
159 symbolFile := String(decorator.properties.Symbol_file)
160 nativeAbiResult := parseNativeAbiDefinition(ctx, symbolFile,
161 android.ApiLevelOrPanic(ctx, decorator.stubsVersion()),
162 "")
163 return compileStubLibrary(ctx, flags, nativeAbiResult.stubSrc)
164}
165
166func (decorator *apiStubDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path {
167 decorator.reexportDirs(android.PathsForModuleSrc(ctx, decorator.apiStubLibraryProperties.Imported_includes)...)
168 return decorator.libraryDecorator.link(ctx, flags, deps, objects)
169}
170
171func init() {
172 pctx.HostBinToolVariable("gen_api_surface_build_files", "gen_api_surface_build_files")
173}
174
175type CcApiContribution struct {
176 android.ModuleBase
177 properties ccApiContributionProperties
178}
179
180type ccApiContributionProperties struct {
181 Symbol_file *string `android:"path"`
182 First_version *string
183 Export_include_dir *string
184}
185
186func CcApiContributionFactory() android.Module {
187 module := &CcApiContribution{}
188 module.AddProperties(&module.properties)
189 android.InitAndroidModule(module)
190 return module
191}
192
193// Do some simple validations
194// Majority of the build rules will be created in the ctx of the api surface this module contributes to
195func (contrib *CcApiContribution) GenerateAndroidBuildActions(ctx android.ModuleContext) {
196 if contrib.properties.Symbol_file == nil {
197 ctx.PropertyErrorf("symbol_file", "%v does not have symbol file", ctx.ModuleName())
198 }
199 if contrib.properties.First_version == nil {
200 ctx.PropertyErrorf("first_version", "%v does not have first_version for stub variants", ctx.ModuleName())
201 }
202}
203
204// Path is out/soong/.export/ but will be different in final multi-tree layout
205func outPathApiSurface(ctx android.ModuleContext, myModuleName string, pathComponent string) android.OutputPath {
206 return android.PathForOutput(ctx, ".export", ctx.ModuleName(), myModuleName, pathComponent)
207}
208
209func (contrib *CcApiContribution) CopyFilesWithTag(apiSurfaceContext android.ModuleContext) map[string]android.Paths {
210 // copy map.txt for now
211 // hardlinks cannot be created since nsjail creates a different mountpoint for out/
212 myDir := apiSurfaceContext.OtherModuleDir(contrib)
213 genMapTxt := outPathApiSurface(apiSurfaceContext, contrib.Name(), String(contrib.properties.Symbol_file))
214 apiSurfaceContext.Build(pctx, android.BuildParams{
215 Rule: android.Cp,
216 Description: "import map.txt file",
217 Input: android.PathForSource(apiSurfaceContext, myDir, String(contrib.properties.Symbol_file)),
218 Output: genMapTxt,
219 })
220
221 outputs := make(map[string]android.Paths)
222 outputs["map"] = []android.Path{genMapTxt}
223
224 if contrib.properties.Export_include_dir != nil {
225 includeDir := android.PathForSource(apiSurfaceContext, myDir, String(contrib.properties.Export_include_dir))
226 outputs["export_include_dir"] = []android.Path{includeDir}
227 }
228 return outputs
229}
230
231var _ multitree.ApiContribution = (*CcApiContribution)(nil)
232
233/*
234func (contrib *CcApiContribution) GenerateBuildFiles(apiSurfaceContext android.ModuleContext) android.Paths {
235 genAndroidBp := outPathApiSurface(apiSurfaceContext, contrib.Name(), "Android.bp")
236
237 // generate Android.bp
238 apiSurfaceContext.Build(pctx, android.BuildParams{
239 Rule: genApiSurfaceBuildFiles,
240 Description: "generate API surface build files",
241 Outputs: []android.WritablePath{genAndroidBp},
242 Args: map[string]string{
243 "name": contrib.Name() + "." + apiSurfaceContext.ModuleName(), //e.g. liblog.ndk
244 "symbol_file": String(contrib.properties.Symbol_file),
245 "first_version": String(contrib.properties.First_version),
246 },
247 })
248 return []android.Path{genAndroidBp}
249}
250*/