blob: 1b023303d78a248f74dfded5631917cca73751a7 [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...)
109 d.libraryDecorator.flagExporter.setProvider(ctx)
110
Spandan Dasf0beebc2022-10-18 18:23:28 +0000111 if d.properties.Src == nil {
112 ctx.PropertyErrorf("src", "src is a required property")
113 }
114 // Skip the existence check of the stub prebuilt file.
115 // The file is not guaranteed to exist during Soong analysis.
116 // Build orchestrator will be responsible for creating a connected ninja graph.
Spandan Dasc6c10fa2022-10-21 21:52:13 +0000117 in := android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), *d.properties.Src)
Kiyoung Kim487689e2022-07-26 09:48:22 +0900118
119 d.unstrippedOutputFile = in
120 libName := d.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
121
122 tocFile := android.PathForModuleOut(ctx, libName+".toc")
123 d.tocFile = android.OptionalPathForPath(tocFile)
124 TransformSharedObjectToToc(ctx, in, tocFile)
125
126 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
127 SharedLibrary: in,
128 Target: ctx.Target(),
129
130 TableOfContents: d.tocFile,
131 })
132
133 return in
134}
135
136func (d *apiLibraryDecorator) availableFor(what string) bool {
137 // Stub from API surface should be available for any APEX.
138 return true
139}
140
Kiyoung Kim51279d32022-08-24 14:10:46 +0900141// 'cc_api_headers' is similar with 'cc_api_library', but which replaces
142// header libraries. The module will replace any dependencies to existing
143// original header libraries.
144type apiHeadersDecorator struct {
145 *libraryDecorator
146}
147
148func CcApiHeadersFactory() android.Module {
149 module, decorator := NewLibrary(android.DeviceSupported)
150 apiHeadersDecorator := &apiHeadersDecorator{
151 libraryDecorator: decorator,
152 }
153 apiHeadersDecorator.HeaderOnly()
154
155 module.stl = nil
156 module.sanitize = nil
157 decorator.disableStripping()
158
159 module.compiler = nil
160 module.linker = apiHeadersDecorator
161 module.installer = nil
162
163 // Mark module as stub, so APEX would not include this stub in the package.
164 module.library.setBuildStubs(true)
165
166 // Prevent default system libs (libc, libm, and libdl) from being linked
167 if apiHeadersDecorator.baseLinker.Properties.System_shared_libs == nil {
168 apiHeadersDecorator.baseLinker.Properties.System_shared_libs = []string{}
169 }
170
171 apiHeadersDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true)
172 apiHeadersDecorator.baseLinker.Properties.Nocrt = BoolPtr(true)
173
174 module.Init()
175
176 return module
177}
178
179func (d *apiHeadersDecorator) Name(basename string) string {
180 return basename + multitree.GetApiImportSuffix()
181}
182
183func (d *apiHeadersDecorator) availableFor(what string) bool {
184 // Stub from API surface should be available for any APEX.
185 return true
Kiyoung Kim487689e2022-07-26 09:48:22 +0900186}