blob: 1283d76e64c859121d9fa0d628a6c8b55ab65968 [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
80func (d *apiLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path {
Kiyoung Kim51279d32022-08-24 14:10:46 +090081 // Export headers as system include dirs if specified. Mostly for libc
82 if Bool(d.libraryDecorator.Properties.Llndk.Export_headers_as_system) {
83 d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs = append(
84 d.libraryDecorator.flagExporter.Properties.Export_system_include_dirs,
85 d.libraryDecorator.flagExporter.Properties.Export_include_dirs...)
86 d.libraryDecorator.flagExporter.Properties.Export_include_dirs = nil
87 }
88
Kiyoung Kim487689e2022-07-26 09:48:22 +090089 // Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
90 d.libraryDecorator.flagExporter.exportIncludes(ctx)
91 d.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
92 d.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
93 d.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
94 d.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
95 d.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
96 d.libraryDecorator.flagExporter.setProvider(ctx)
97
98 in := android.PathForModuleSrc(ctx, *d.properties.Src)
99
100 d.unstrippedOutputFile = in
101 libName := d.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
102
103 tocFile := android.PathForModuleOut(ctx, libName+".toc")
104 d.tocFile = android.OptionalPathForPath(tocFile)
105 TransformSharedObjectToToc(ctx, in, tocFile)
106
107 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
108 SharedLibrary: in,
109 Target: ctx.Target(),
110
111 TableOfContents: d.tocFile,
112 })
113
114 return in
115}
116
117func (d *apiLibraryDecorator) availableFor(what string) bool {
118 // Stub from API surface should be available for any APEX.
119 return true
120}
121
Kiyoung Kim51279d32022-08-24 14:10:46 +0900122// 'cc_api_headers' is similar with 'cc_api_library', but which replaces
123// header libraries. The module will replace any dependencies to existing
124// original header libraries.
125type apiHeadersDecorator struct {
126 *libraryDecorator
127}
128
129func CcApiHeadersFactory() android.Module {
130 module, decorator := NewLibrary(android.DeviceSupported)
131 apiHeadersDecorator := &apiHeadersDecorator{
132 libraryDecorator: decorator,
133 }
134 apiHeadersDecorator.HeaderOnly()
135
136 module.stl = nil
137 module.sanitize = nil
138 decorator.disableStripping()
139
140 module.compiler = nil
141 module.linker = apiHeadersDecorator
142 module.installer = nil
143
144 // Mark module as stub, so APEX would not include this stub in the package.
145 module.library.setBuildStubs(true)
146
147 // Prevent default system libs (libc, libm, and libdl) from being linked
148 if apiHeadersDecorator.baseLinker.Properties.System_shared_libs == nil {
149 apiHeadersDecorator.baseLinker.Properties.System_shared_libs = []string{}
150 }
151
152 apiHeadersDecorator.baseLinker.Properties.No_libcrt = BoolPtr(true)
153 apiHeadersDecorator.baseLinker.Properties.Nocrt = BoolPtr(true)
154
155 module.Init()
156
157 return module
158}
159
160func (d *apiHeadersDecorator) Name(basename string) string {
161 return basename + multitree.GetApiImportSuffix()
162}
163
164func (d *apiHeadersDecorator) availableFor(what string) bool {
165 // Stub from API surface should be available for any APEX.
166 return true
Kiyoung Kim487689e2022-07-26 09:48:22 +0900167}