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