blob: 598031914a9562e51f09f300ec4422e503d780c7 [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// Copyright 2016 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 (
Martin Stjernholm14ee8322020-09-21 21:45:49 +010018 "path/filepath"
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +000019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
21 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -040022 "android/soong/bazel"
Colin Crossce75d2c2016-10-06 16:12:58 -070023)
24
25func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000026 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
27}
28
29func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000030 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000031 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
32 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040033 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000034 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000035 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070036}
37
38type prebuiltLinkerInterface interface {
39 Name(string) string
40 prebuilt() *android.Prebuilt
41}
42
Patrice Arruda3554a982019-03-27 19:09:10 -070043type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070044 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
45 Srcs []string `android:"path,arch_variant"`
46
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070047 Sanitized Sanitized `android:"arch_variant"`
48
Patrice Arruda3554a982019-03-27 19:09:10 -070049 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
50 // symbols, etc), default true.
51 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080052
53 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
54 // This is needed only if this library is linked by other modules in build time.
55 // Only makes sense for the Windows target.
56 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070057}
58
Colin Crossde89fb82017-03-17 13:28:06 -070059type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070060 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080061
Patrice Arruda3554a982019-03-27 19:09:10 -070062 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070063}
64
Colin Crossde89fb82017-03-17 13:28:06 -070065func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070066 return &p.Prebuilt
67}
68
Colin Cross74d73e22017-08-02 11:05:49 -070069func (p *prebuiltLinker) PrebuiltSrcs() []string {
70 return p.properties.Srcs
71}
72
Colin Cross33b2fb72019-05-14 14:07:01 -070073type prebuiltLibraryInterface interface {
74 libraryInterface
75 prebuiltLinkerInterface
76 disablePrebuilt()
77}
78
Colin Crossde89fb82017-03-17 13:28:06 -070079type prebuiltLibraryLinker struct {
80 *libraryDecorator
81 prebuiltLinker
82}
83
84var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070085var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070086
Yi Kong00981662018-08-13 16:02:49 -070087func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
88
89func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080090 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070091}
92
93func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070094 return flags
Yi Kong00981662018-08-13 16:02:49 -070095}
96
97func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
98 return p.libraryDecorator.linkerProps()
99}
100
Colin Crossce75d2c2016-10-06 16:12:58 -0700101func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700102 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000103
Colin Cross0de8a1e2020-09-18 14:15:30 -0700104 p.libraryDecorator.flagExporter.exportIncludes(ctx)
105 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
106 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
107 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
108 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
109 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
110
111 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000112
Colin Crossce75d2c2016-10-06 16:12:58 -0700113 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700114 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000115 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000116 if len(srcs) > 1 {
117 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
118 return nil
119 }
120
Jiyong Park892a98f2020-12-14 09:20:00 +0900121 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
122
Paul Duffinbce90da2020-03-12 20:17:14 +0000123 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700124
Yo Chianga3ad9b22020-03-18 14:19:07 +0800125 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700126 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
127 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
128 StaticLibrary: in,
129
130 TransitiveStaticLibrariesForOrdering: depSet,
131 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800132 return in
133 }
134
Colin Cross88f6fef2018-09-05 14:20:03 -0700135 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700136 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700137 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800138 outputFile := android.PathForModuleOut(ctx, libName)
139 var implicits android.Paths
140
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200141 if p.stripper.NeedsStrip(ctx) {
142 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700143 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200144 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700145 in = stripped
146 }
147
Colin Crossb60190a2018-09-04 16:28:17 -0700148 // Optimize out relinking against shared libraries whose interface hasn't changed by
149 // depending on a table of contents file instead of the library itself.
150 tocFile := android.PathForModuleOut(ctx, libName+".toc")
151 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400152 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700153
Yo Chianga3ad9b22020-03-18 14:19:07 +0800154 if ctx.Windows() && p.properties.Windows_import_lib != nil {
155 // Consumers of this library actually links to the import library in build
156 // time and dynamically links to the DLL in run time. i.e.
157 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
158 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
159 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
160 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
161 implicits = append(implicits, importLibOutputFile)
162
163 ctx.Build(pctx, android.BuildParams{
164 Rule: android.Cp,
165 Description: "prebuilt import library",
166 Input: importLibSrc,
167 Output: importLibOutputFile,
168 Args: map[string]string{
169 "cpFlags": "-L",
170 },
171 })
172 }
173
174 ctx.Build(pctx, android.BuildParams{
175 Rule: android.Cp,
176 Description: "prebuilt shared library",
177 Implicits: implicits,
178 Input: in,
179 Output: outputFile,
180 Args: map[string]string{
181 "cpFlags": "-L",
182 },
183 })
184
Colin Cross0de8a1e2020-09-18 14:15:30 -0700185 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400186 SharedLibrary: outputFile,
187 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700188
189 TableOfContents: p.tocFile,
190 })
191
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000192 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
193 // library as their source and must not be installed, but libclang_rt.* libraries
194 // have stubs because they are LLNDK libraries, but use an implementation library
195 // as their source and need to be installed. This discrepancy should be resolved
196 // without the prefix hack below.
197 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() &&
198 !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") {
199 ctx.Module().MakeUninstallable()
200 }
201
Yo Chianga3ad9b22020-03-18 14:19:07 +0800202 return outputFile
203 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700204 }
205
Colin Cross649d8172020-12-10 12:30:21 -0800206 if p.header() {
207 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
208
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000209 // Need to return an output path so that the AndroidMk logic doesn't skip
210 // the prebuilt header. For compatibility, in case Android.mk files use a
211 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
212 // placeholder, just like non-prebuilt header modules do in linkStatic().
213 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
214 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
215 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800216 }
217
Colin Crossce75d2c2016-10-06 16:12:58 -0700218 return nil
219}
220
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700221func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
222 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000223 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700224 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000225 if p.static() {
226 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700227 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000228 }
229 if p.shared() {
230 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700231 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000232 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000233 return srcs
234}
235
Jiyong Park379de2f2018-12-19 02:47:14 +0900236func (p *prebuiltLibraryLinker) shared() bool {
237 return p.libraryDecorator.shared()
238}
239
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700240func (p *prebuiltLibraryLinker) nativeCoverage() bool {
241 return false
242}
243
Colin Cross33b2fb72019-05-14 14:07:01 -0700244func (p *prebuiltLibraryLinker) disablePrebuilt() {
245 p.properties.Srcs = nil
246}
247
Jiyong Park892a98f2020-12-14 09:20:00 +0900248// Implements versionedInterface
249func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100250 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900251}
252
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000253func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700254 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700255 module.compiler = nil
256
257 prebuilt := &prebuiltLibraryLinker{
258 libraryDecorator: library,
259 }
260 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700261 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700262
Colin Cross74d73e22017-08-02 11:05:49 -0700263 module.AddProperties(&prebuilt.properties)
264
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000265 if srcsProperty == "" {
266 android.InitPrebuiltModuleWithoutSrcs(module)
267 } else {
268 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
269 return prebuilt.prebuiltSrcs(ctx)
270 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000271
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000272 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
273 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900274
Paul Duffinac6e6082019-12-11 15:22:32 +0000275 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900276 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000277 return module, library
278}
279
Paul Duffinbce90da2020-03-12 20:17:14 +0000280// cc_prebuilt_library installs a precompiled shared library that are
281// listed in the srcs property in the device's directory.
282func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000283 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000284
285 // Prebuilt shared libraries can be included in APEXes
286 android.InitApexModule(module)
287
288 return module.Init()
289}
290
Paul Duffinac6e6082019-12-11 15:22:32 +0000291// cc_prebuilt_library_shared installs a precompiled shared library that are
292// listed in the srcs property in the device's directory.
293func PrebuiltSharedLibraryFactory() android.Module {
294 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
295 return module.Init()
296}
297
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400298// cc_prebuilt_test_library_shared installs a precompiled shared library
299// to be used as a data dependency of a test-related module (such as cc_test, or
300// cc_test_library).
301func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000302 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400303 library.BuildOnlyShared()
304 library.baseInstaller = NewTestInstaller()
305 return module.Init()
306}
307
Paul Duffinac6e6082019-12-11 15:22:32 +0000308func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000309 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000310 library.BuildOnlyShared()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400311 module.bazelable = true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400312 module.bazelHandler = &prebuiltSharedLibraryBazelHandler{module: module, library: library}
Paul Duffinac6e6082019-12-11 15:22:32 +0000313
314 // Prebuilt shared libraries can be included in APEXes
315 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900316
Leo Li74f7b972017-05-17 11:30:45 -0700317 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700318}
319
Patrice Arruda3554a982019-03-27 19:09:10 -0700320// cc_prebuilt_library_static installs a precompiled static library that are
321// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900322func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700323 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
324 return module.Init()
325}
326
327func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000328 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700329 library.BuildOnlyStatic()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400330 module.bazelable = true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400331 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700332 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700333}
334
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400335type bazelPrebuiltLibraryStaticAttributes struct {
336 Static_library bazel.LabelAttribute
337 Export_includes bazel.StringListAttribute
338 Export_system_includes bazel.StringListAttribute
339}
340
341func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module) {
342 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module)
343 exportedIncludes := Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx, module)
344
345 attrs := &bazelPrebuiltLibraryStaticAttributes{
346 Static_library: prebuiltAttrs.Src,
347 Export_includes: exportedIncludes.Includes,
348 Export_system_includes: exportedIncludes.SystemIncludes,
349 }
350
351 props := bazel.BazelTargetModuleProperties{
352 Rule_class: "prebuilt_library_static",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500353 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400354 }
355
356 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
357 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
358}
359
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400360type bazelPrebuiltLibrarySharedAttributes struct {
361 Shared_library bazel.LabelAttribute
362}
363
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400364func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400365 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module)
366
367 attrs := &bazelPrebuiltLibrarySharedAttributes{
368 Shared_library: prebuiltAttrs.Src,
369 }
370
371 props := bazel.BazelTargetModuleProperties{
372 Rule_class: "prebuilt_library_shared",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500373 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_shared.bzl",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400374 }
375
376 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
377 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
378}
379
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000380type prebuiltObjectProperties struct {
381 Srcs []string `android:"path,arch_variant"`
382}
383
384type prebuiltObjectLinker struct {
385 android.Prebuilt
386 objectLinker
387
388 properties prebuiltObjectProperties
389}
390
Liz Kammer3f9e1552021-04-02 18:47:09 -0400391type prebuiltStaticLibraryBazelHandler struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000392 android.BazelHandler
Liz Kammer3f9e1552021-04-02 18:47:09 -0400393
394 module *Module
395 library *libraryDecorator
396}
397
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000398func (h *prebuiltStaticLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400399 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400400 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400401 if err != nil {
402 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
403 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400404 if !ok {
405 return false
406 }
Liz Kammerfe23bf32021-04-09 16:17:05 -0400407 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400408 if len(staticLibs) > 1 {
409 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
410 return false
411 }
412
413 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
414
415 // TODO(eakammer):Add stub-related flags if this library is a stub library.
416 // h.library.exportVersioningMacroIfNeeded(ctx)
417
418 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
419 // validation will fail. For now, set this to an empty list.
420 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
421 h.library.collectedSnapshotHeaders = android.Paths{}
422
423 if len(staticLibs) == 0 {
424 h.module.outputFile = android.OptionalPath{}
425 return true
426 }
427
428 out := android.PathForBazelOut(ctx, staticLibs[0])
429 h.module.outputFile = android.OptionalPathForPath(out)
430
431 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
432 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
433 StaticLibrary: out,
434
435 TransitiveStaticLibrariesForOrdering: depSet,
436 })
437
438 return true
439}
440
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400441type prebuiltSharedLibraryBazelHandler struct {
442 android.BazelHandler
443
444 module *Module
445 library *libraryDecorator
446}
447
448func (h *prebuiltSharedLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
449 bazelCtx := ctx.Config().BazelContext
450 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
451 if err != nil {
452 ctx.ModuleErrorf("Error getting Bazel CcInfo for %s: %s", label, err)
453 }
454 if !ok {
455 return false
456 }
457 sharedLibs := ccInfo.CcSharedLibraryFiles
458 if len(sharedLibs) != 1 {
459 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
460 return false
461 }
462
463 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
464
465 // TODO(eakammer):Add stub-related flags if this library is a stub library.
466 // h.library.exportVersioningMacroIfNeeded(ctx)
467
468 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
469 // validation will fail. For now, set this to an empty list.
470 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
471 h.library.collectedSnapshotHeaders = android.Paths{}
472
473 if len(sharedLibs) == 0 {
474 h.module.outputFile = android.OptionalPath{}
475 return true
476 }
477
478 out := android.PathForBazelOut(ctx, sharedLibs[0])
479 h.module.outputFile = android.OptionalPathForPath(out)
480
481 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
482 h.library.unstrippedOutputFile = out
483
484 var toc android.Path
485 if len(ccInfo.TocFile) > 0 {
486 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
487 } else {
488 toc = out // Just reuse `out` so ninja still gets an input but won't matter
489 }
490
491 info := SharedLibraryInfo{
492 SharedLibrary: out,
493 TableOfContents: android.OptionalPathForPath(toc),
494 Target: ctx.Target(),
495 }
496 ctx.SetProvider(SharedLibraryInfoProvider, info)
497
498 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
499 h.module.maybeUnhideFromMake()
500
501 return true
502}
503
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000504func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
505 return &p.Prebuilt
506}
507
508var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
509
510func (p *prebuiltObjectLinker) link(ctx ModuleContext,
511 flags Flags, deps PathDeps, objs Objects) android.Path {
512 if len(p.properties.Srcs) > 0 {
513 return p.Prebuilt.SingleSourcePath(ctx)
514 }
515 return nil
516}
517
Inseob Kim1042d292020-06-01 23:23:05 +0900518func (p *prebuiltObjectLinker) object() bool {
519 return true
520}
521
Colin Cross7cabd422021-06-25 14:21:04 -0700522func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
523 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000524 prebuilt := &prebuiltObjectLinker{
525 objectLinker: objectLinker{
526 baseLinker: NewBaseLinker(nil),
527 },
528 }
529 module.linker = prebuilt
530 module.AddProperties(&prebuilt.properties)
531 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
532 android.InitSdkAwareModule(module)
533 return module
534}
535
536func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700537 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000538 return module.Init()
539}
540
Colin Crossde89fb82017-03-17 13:28:06 -0700541type prebuiltBinaryLinker struct {
542 *binaryDecorator
543 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100544
545 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700546}
547
548var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
549
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100550func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
551 return p.toolPath
552}
553
Colin Crossde89fb82017-03-17 13:28:06 -0700554func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
555 flags Flags, deps PathDeps, objs Objects) android.Path {
556 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700557 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700558 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
559 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100560 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700561 p.unstrippedOutputFile = in
562
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100563 if ctx.Host() {
564 // Host binaries are symlinked to their prebuilt source locations. That
565 // way they are executed directly from there so the linker resolves their
566 // shared library dependencies relative to that location (using
567 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
568 // repository can supply the expected versions of the shared libraries
569 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700570
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100571 // These shared lib paths may point to copies of the libs in
572 // .intermediates, which isn't where the binary will load them from, but
573 // it's fine for dependency tracking. If a library dependency is updated,
574 // the symlink will get a new timestamp, along with any installed symlinks
575 // handled in make.
576 sharedLibPaths := deps.EarlySharedLibs
577 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
578 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
579
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100580 var fromPath = in.String()
581 if !filepath.IsAbs(fromPath) {
582 fromPath = "$$PWD/" + fromPath
583 }
584
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100585 ctx.Build(pctx, android.BuildParams{
586 Rule: android.Symlink,
587 Output: outputFile,
588 Input: in,
589 Implicits: sharedLibPaths,
590 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100591 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100592 },
593 })
594
595 p.toolPath = android.OptionalPathForPath(outputFile)
596 } else {
597 if p.stripper.NeedsStrip(ctx) {
598 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
599 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
600 in = stripped
601 }
602
603 // Copy binaries to a name matching the final installed name
604 ctx.Build(pctx, android.BuildParams{
605 Rule: android.CpExecutable,
606 Description: "prebuilt",
607 Output: outputFile,
608 Input: in,
609 })
610 }
Colin Cross94921e72017-08-08 16:20:15 -0700611
612 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700613 }
614
615 return nil
616}
617
Inseob Kim7f283f42020-06-01 21:53:49 +0900618func (p *prebuiltBinaryLinker) binary() bool {
619 return true
620}
621
Patrice Arruda3554a982019-03-27 19:09:10 -0700622// cc_prebuilt_binary installs a precompiled executable in srcs property in the
623// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700624func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700625 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
626 return module.Init()
627}
628
629func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbdc922f2021-12-14 14:21:21 -0500630 module, binary := newBinary(hod, false)
Colin Crossde89fb82017-03-17 13:28:06 -0700631 module.compiler = nil
632
633 prebuilt := &prebuiltBinaryLinker{
634 binaryDecorator: binary,
635 }
636 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100637 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700638
Colin Cross74d73e22017-08-02 11:05:49 -0700639 module.AddProperties(&prebuilt.properties)
640
641 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700642 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700643}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700644
645type Sanitized struct {
646 None struct {
647 Srcs []string `android:"path,arch_variant"`
648 } `android:"arch_variant"`
649 Address struct {
650 Srcs []string `android:"path,arch_variant"`
651 } `android:"arch_variant"`
652 Hwaddress struct {
653 Srcs []string `android:"path,arch_variant"`
654 } `android:"arch_variant"`
655}
656
657func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
658 if sanitize == nil {
659 return nil
660 }
661 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
662 return sanitized.Address.Srcs
663 }
664 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
665 return sanitized.Hwaddress.Srcs
666 }
667 return sanitized.None.Srcs
668}