blob: 760d36ae9c6fb99ab749a8a594f2b8b4f38ef179 [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)
Inseob Kim5eb7ee92022-04-27 10:30:34 +090029}
30
Kiyoung Kim487689e2022-07-26 09:48:22 +090031// 'cc_api_library' is a module type which is from the exported API surface
32// with C shared library type. The module will replace original module, and
33// offer a link to the module that generates shared library object from the
34// map file.
35type apiLibraryProperties struct {
36 Src *string `android:"arch_variant"`
37}
38
39type apiLibraryDecorator struct {
40 *libraryDecorator
41 properties apiLibraryProperties
42}
43
44func CcApiLibraryFactory() android.Module {
45 module, decorator := NewLibrary(android.DeviceSupported)
46 apiLibraryDecorator := &apiLibraryDecorator{
47 libraryDecorator: decorator,
48 }
49 apiLibraryDecorator.BuildOnlyShared()
50
51 module.stl = nil
52 module.sanitize = nil
53 decorator.disableStripping()
54
55 module.compiler = nil
56 module.linker = apiLibraryDecorator
57 module.installer = nil
58 module.AddProperties(&module.Properties, &apiLibraryDecorator.properties)
59
60 // Mark module as stub, so APEX would not include this stub in the package.
61 module.library.setBuildStubs(true)
62
63 // Prevent default system libs (libc, libm, and libdl) from being linked
64 if apiLibraryDecorator.baseLinker.Properties.System_shared_libs == nil {
65 apiLibraryDecorator.baseLinker.Properties.System_shared_libs = []string{}
66 }
67
Kiyoung Kim835c5892022-08-17 16:40:16 +090068 apiLibraryDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true)
69 apiLibraryDecorator.baseLinker.Properties.Nocrt = BoolPtr(true)
70
Kiyoung Kim487689e2022-07-26 09:48:22 +090071 module.Init()
72
73 return module
74}
75
76func (d *apiLibraryDecorator) Name(basename string) string {
77 return basename + multitree.GetApiImportSuffix()
78}
79
Spandan Dasf0beebc2022-10-18 18:23:28 +000080// Export include dirs without checking for existence.
81// The directories are not guaranteed to exist during Soong analysis.
82func (d *apiLibraryDecorator) exportIncludes(ctx ModuleContext) {
83 exporterProps := d.flagExporter.Properties
84 for _, dir := range exporterProps.Export_include_dirs {
Spandan Dasc6c10fa2022-10-21 21:52:13 +000085 d.dirs = append(d.dirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir))
Spandan Dasf0beebc2022-10-18 18:23:28 +000086 }
87 // system headers
88 for _, dir := range exporterProps.Export_system_include_dirs {
Spandan Dasc6c10fa2022-10-21 21:52:13 +000089 d.systemDirs = append(d.systemDirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir))
Spandan Dasf0beebc2022-10-18 18:23:28 +000090 }
91}
92
Kiyoung Kim487689e2022-07-26 09:48:22 +090093func (d *apiLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path {
Kiyoung Kim51279d32022-08-24 14:10:46 +090094 // Export headers as system include dirs if specified. Mostly for libc
95 if Bool(d.libraryDecorator.Properties.Llndk.Export_headers_as_system) {
96 d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs = append(
97 d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs,
98 d.libraryDecorator.flagExporter.Properties.Export_include_dirs...)
99 d.libraryDecorator.flagExporter.Properties.Export_include_dirs = nil
100 }
101
Kiyoung Kim487689e2022-07-26 09:48:22 +0900102 // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
Spandan Dasf0beebc2022-10-18 18:23:28 +0000103 d.exportIncludes(ctx)
Kiyoung Kim487689e2022-07-26 09:48:22 +0900104 d.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
105 d.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
106 d.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
107 d.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
108 d.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
Kiyoung Kim487689e2022-07-26 09:48:22 +0900109
Spandan Dasf0beebc2022-10-18 18:23:28 +0000110 if d.properties.Src == nil {
111 ctx.PropertyErrorf("src", "src is a required property")
112 }
113 // Skip the existence check of the stub prebuilt file.
114 // The file is not guaranteed to exist during Soong analysis.
115 // Build orchestrator will be responsible for creating a connected ninja graph.
Spandan Dasc6c10fa2022-10-21 21:52:13 +0000116 in := android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), *d.properties.Src)
Kiyoung Kim487689e2022-07-26 09:48:22 +0900117
Spandan Dasa3c8a172022-10-26 20:54:32 +0000118 // Make the _compilation_ of rdeps have an order-only dep on cc_api_library.src (an .so file)
119 // The .so file itself has an order-only dependency on the headers contributed by this library.
120 // Creating this dependency ensures that the headers are assembled before compilation of rdeps begins.
121 d.libraryDecorator.reexportDeps(in)
122 d.libraryDecorator.flagExporter.setProvider(ctx)
123
Kiyoung Kim487689e2022-07-26 09:48:22 +0900124 d.unstrippedOutputFile = in
125 libName := d.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
126
127 tocFile := android.PathForModuleOut(ctx, libName+".toc")
128 d.tocFile = android.OptionalPathForPath(tocFile)
129 TransformSharedObjectToToc(ctx, in, tocFile)
130
131 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
132 SharedLibrary: in,
133 Target: ctx.Target(),
134
135 TableOfContents: d.tocFile,
136 })
137
138 return in
139}
140
141func (d *apiLibraryDecorator) availableFor(what string) bool {
142 // Stub from API surface should be available for any APEX.
143 return true
144}
145
Kiyoung Kim51279d32022-08-24 14:10:46 +0900146// 'cc_api_headers' is similar with 'cc_api_library', but which replaces
147// header libraries. The module will replace any dependencies to existing
148// original header libraries.
149type apiHeadersDecorator struct {
150 *libraryDecorator
151}
152
153func CcApiHeadersFactory() android.Module {
154 module, decorator := NewLibrary(android.DeviceSupported)
155 apiHeadersDecorator := &apiHeadersDecorator{
156 libraryDecorator: decorator,
157 }
158 apiHeadersDecorator.HeaderOnly()
159
160 module.stl = nil
161 module.sanitize = nil
162 decorator.disableStripping()
163
164 module.compiler = nil
165 module.linker = apiHeadersDecorator
166 module.installer = nil
167
168 // Mark module as stub, so APEX would not include this stub in the package.
169 module.library.setBuildStubs(true)
170
171 // Prevent default system libs (libc, libm, and libdl) from being linked
172 if apiHeadersDecorator.baseLinker.Properties.System_shared_libs == nil {
173 apiHeadersDecorator.baseLinker.Properties.System_shared_libs = []string{}
174 }
175
176 apiHeadersDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true)
177 apiHeadersDecorator.baseLinker.Properties.Nocrt = BoolPtr(true)
178
179 module.Init()
180
181 return module
182}
183
184func (d *apiHeadersDecorator) Name(basename string) string {
185 return basename + multitree.GetApiImportSuffix()
186}
187
188func (d *apiHeadersDecorator) availableFor(what string) bool {
189 // Stub from API surface should be available for any APEX.
190 return true
Kiyoung Kim487689e2022-07-26 09:48:22 +0900191}