blob: f54c6f8d656d1b2b92d6914410cd0f5ca7c17981 [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
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000256 module.bazelable = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700257
258 prebuilt := &prebuiltLibraryLinker{
259 libraryDecorator: library,
260 }
261 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700262 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700263
Colin Cross74d73e22017-08-02 11:05:49 -0700264 module.AddProperties(&prebuilt.properties)
265
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000266 if srcsProperty == "" {
267 android.InitPrebuiltModuleWithoutSrcs(module)
268 } else {
269 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
270 return prebuilt.prebuiltSrcs(ctx)
271 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000272
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000273 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
274 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900275
Paul Duffinac6e6082019-12-11 15:22:32 +0000276 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900277 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000278 return module, library
279}
280
Paul Duffinbce90da2020-03-12 20:17:14 +0000281// cc_prebuilt_library installs a precompiled shared library that are
282// listed in the srcs property in the device's directory.
283func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000284 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000285
286 // Prebuilt shared libraries can be included in APEXes
287 android.InitApexModule(module)
288
289 return module.Init()
290}
291
Paul Duffinac6e6082019-12-11 15:22:32 +0000292// cc_prebuilt_library_shared installs a precompiled shared library that are
293// listed in the srcs property in the device's directory.
294func PrebuiltSharedLibraryFactory() android.Module {
295 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
296 return module.Init()
297}
298
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400299// cc_prebuilt_test_library_shared installs a precompiled shared library
300// to be used as a data dependency of a test-related module (such as cc_test, or
301// cc_test_library).
302func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000303 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400304 library.BuildOnlyShared()
305 library.baseInstaller = NewTestInstaller()
306 return module.Init()
307}
308
Paul Duffinac6e6082019-12-11 15:22:32 +0000309func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000310 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000311 library.BuildOnlyShared()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400312 module.bazelable = true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400313 module.bazelHandler = &prebuiltSharedLibraryBazelHandler{module: module, library: library}
Paul Duffinac6e6082019-12-11 15:22:32 +0000314
315 // Prebuilt shared libraries can be included in APEXes
316 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900317
Leo Li74f7b972017-05-17 11:30:45 -0700318 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700319}
320
Patrice Arruda3554a982019-03-27 19:09:10 -0700321// cc_prebuilt_library_static installs a precompiled static library that are
322// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900323func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700324 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
325 return module.Init()
326}
327
328func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000329 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700330 library.BuildOnlyStatic()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400331 module.bazelable = true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400332 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700333 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700334}
335
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400336type bazelPrebuiltLibraryStaticAttributes struct {
337 Static_library bazel.LabelAttribute
338 Export_includes bazel.StringListAttribute
339 Export_system_includes bazel.StringListAttribute
340}
341
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000342// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
343// Implements bp2build for cc_prebuilt_library modules. This will generate:
344// * Only a prebuilt_library_static if the shared.enabled property is set to false across all variants.
345// * Only a prebuilt_library_shared if the static.enabled property is set to false across all variants
346// * Both a prebuilt_library_static and prebuilt_library_shared if the aforementioned properties are not false across
347// all variants
348//
349// In all cases, prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
350func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
351 prebuiltLibraryStaticBp2Build(ctx, module, true)
352 prebuiltLibrarySharedBp2Build(ctx, module)
353}
354
355func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
356 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400357 exportedIncludes := Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx, module)
358
359 attrs := &bazelPrebuiltLibraryStaticAttributes{
360 Static_library: prebuiltAttrs.Src,
361 Export_includes: exportedIncludes.Includes,
362 Export_system_includes: exportedIncludes.SystemIncludes,
363 }
364
365 props := bazel.BazelTargetModuleProperties{
366 Rule_class: "prebuilt_library_static",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500367 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400368 }
369
370 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000371 if fullBuild {
372 name += "_bp2build_cc_library_static"
373 }
374 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400375}
376
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400377type bazelPrebuiltLibrarySharedAttributes struct {
378 Shared_library bazel.LabelAttribute
379}
380
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400381func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000382 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400383
384 attrs := &bazelPrebuiltLibrarySharedAttributes{
385 Shared_library: prebuiltAttrs.Src,
386 }
387
388 props := bazel.BazelTargetModuleProperties{
389 Rule_class: "prebuilt_library_shared",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500390 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 -0400391 }
392
393 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000394 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400395}
396
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000397type prebuiltObjectProperties struct {
398 Srcs []string `android:"path,arch_variant"`
399}
400
401type prebuiltObjectLinker struct {
402 android.Prebuilt
403 objectLinker
404
405 properties prebuiltObjectProperties
406}
407
Liz Kammer3f9e1552021-04-02 18:47:09 -0400408type prebuiltStaticLibraryBazelHandler struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000409 android.BazelHandler
Liz Kammer3f9e1552021-04-02 18:47:09 -0400410
411 module *Module
412 library *libraryDecorator
413}
414
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000415func (h *prebuiltStaticLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400416 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400417 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400418 if err != nil {
419 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
420 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400421 if !ok {
422 return false
423 }
Liz Kammerfe23bf32021-04-09 16:17:05 -0400424 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400425 if len(staticLibs) > 1 {
426 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
427 return false
428 }
429
430 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
431
432 // TODO(eakammer):Add stub-related flags if this library is a stub library.
433 // h.library.exportVersioningMacroIfNeeded(ctx)
434
435 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
436 // validation will fail. For now, set this to an empty list.
437 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
438 h.library.collectedSnapshotHeaders = android.Paths{}
439
440 if len(staticLibs) == 0 {
441 h.module.outputFile = android.OptionalPath{}
442 return true
443 }
444
445 out := android.PathForBazelOut(ctx, staticLibs[0])
446 h.module.outputFile = android.OptionalPathForPath(out)
447
448 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
449 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
450 StaticLibrary: out,
451
452 TransitiveStaticLibrariesForOrdering: depSet,
453 })
454
455 return true
456}
457
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400458type prebuiltSharedLibraryBazelHandler struct {
459 android.BazelHandler
460
461 module *Module
462 library *libraryDecorator
463}
464
465func (h *prebuiltSharedLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
466 bazelCtx := ctx.Config().BazelContext
467 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
468 if err != nil {
469 ctx.ModuleErrorf("Error getting Bazel CcInfo for %s: %s", label, err)
470 }
471 if !ok {
472 return false
473 }
474 sharedLibs := ccInfo.CcSharedLibraryFiles
475 if len(sharedLibs) != 1 {
476 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
477 return false
478 }
479
480 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
481
482 // TODO(eakammer):Add stub-related flags if this library is a stub library.
483 // h.library.exportVersioningMacroIfNeeded(ctx)
484
485 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
486 // validation will fail. For now, set this to an empty list.
487 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
488 h.library.collectedSnapshotHeaders = android.Paths{}
489
490 if len(sharedLibs) == 0 {
491 h.module.outputFile = android.OptionalPath{}
492 return true
493 }
494
495 out := android.PathForBazelOut(ctx, sharedLibs[0])
496 h.module.outputFile = android.OptionalPathForPath(out)
497
498 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
499 h.library.unstrippedOutputFile = out
500
501 var toc android.Path
502 if len(ccInfo.TocFile) > 0 {
503 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
504 } else {
505 toc = out // Just reuse `out` so ninja still gets an input but won't matter
506 }
507
508 info := SharedLibraryInfo{
509 SharedLibrary: out,
510 TableOfContents: android.OptionalPathForPath(toc),
511 Target: ctx.Target(),
512 }
513 ctx.SetProvider(SharedLibraryInfoProvider, info)
514
515 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
516 h.module.maybeUnhideFromMake()
517
518 return true
519}
520
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000521func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
522 return &p.Prebuilt
523}
524
525var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
526
527func (p *prebuiltObjectLinker) link(ctx ModuleContext,
528 flags Flags, deps PathDeps, objs Objects) android.Path {
529 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700530 // Copy objects to a name matching the final installed name
531 in := p.Prebuilt.SingleSourcePath(ctx)
532 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
533 ctx.Build(pctx, android.BuildParams{
534 Rule: android.CpExecutable,
535 Description: "prebuilt",
536 Output: outputFile,
537 Input: in,
538 })
539 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000540 }
541 return nil
542}
543
Inseob Kim1042d292020-06-01 23:23:05 +0900544func (p *prebuiltObjectLinker) object() bool {
545 return true
546}
547
Colin Cross7cabd422021-06-25 14:21:04 -0700548func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
549 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000550 prebuilt := &prebuiltObjectLinker{
551 objectLinker: objectLinker{
552 baseLinker: NewBaseLinker(nil),
553 },
554 }
555 module.linker = prebuilt
556 module.AddProperties(&prebuilt.properties)
557 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
558 android.InitSdkAwareModule(module)
559 return module
560}
561
562func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700563 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000564 return module.Init()
565}
566
Colin Crossde89fb82017-03-17 13:28:06 -0700567type prebuiltBinaryLinker struct {
568 *binaryDecorator
569 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100570
571 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700572}
573
574var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
575
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100576func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
577 return p.toolPath
578}
579
Colin Crossde89fb82017-03-17 13:28:06 -0700580func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
581 flags Flags, deps PathDeps, objs Objects) android.Path {
582 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700583 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700584 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
585 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100586 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700587 p.unstrippedOutputFile = in
588
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100589 if ctx.Host() {
590 // Host binaries are symlinked to their prebuilt source locations. That
591 // way they are executed directly from there so the linker resolves their
592 // shared library dependencies relative to that location (using
593 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
594 // repository can supply the expected versions of the shared libraries
595 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700596
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100597 // These shared lib paths may point to copies of the libs in
598 // .intermediates, which isn't where the binary will load them from, but
599 // it's fine for dependency tracking. If a library dependency is updated,
600 // the symlink will get a new timestamp, along with any installed symlinks
601 // handled in make.
602 sharedLibPaths := deps.EarlySharedLibs
603 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
604 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
605
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100606 var fromPath = in.String()
607 if !filepath.IsAbs(fromPath) {
608 fromPath = "$$PWD/" + fromPath
609 }
610
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100611 ctx.Build(pctx, android.BuildParams{
612 Rule: android.Symlink,
613 Output: outputFile,
614 Input: in,
615 Implicits: sharedLibPaths,
616 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100617 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100618 },
619 })
620
621 p.toolPath = android.OptionalPathForPath(outputFile)
622 } else {
623 if p.stripper.NeedsStrip(ctx) {
624 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
625 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
626 in = stripped
627 }
628
629 // Copy binaries to a name matching the final installed name
630 ctx.Build(pctx, android.BuildParams{
631 Rule: android.CpExecutable,
632 Description: "prebuilt",
633 Output: outputFile,
634 Input: in,
635 })
636 }
Colin Cross94921e72017-08-08 16:20:15 -0700637
638 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700639 }
640
641 return nil
642}
643
Inseob Kim7f283f42020-06-01 21:53:49 +0900644func (p *prebuiltBinaryLinker) binary() bool {
645 return true
646}
647
Patrice Arruda3554a982019-03-27 19:09:10 -0700648// cc_prebuilt_binary installs a precompiled executable in srcs property in the
649// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700650func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700651 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
652 return module.Init()
653}
654
655func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbdc922f2021-12-14 14:21:21 -0500656 module, binary := newBinary(hod, false)
Colin Crossde89fb82017-03-17 13:28:06 -0700657 module.compiler = nil
658
659 prebuilt := &prebuiltBinaryLinker{
660 binaryDecorator: binary,
661 }
662 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100663 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700664
Colin Cross74d73e22017-08-02 11:05:49 -0700665 module.AddProperties(&prebuilt.properties)
666
667 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700668 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700669}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700670
671type Sanitized struct {
672 None struct {
673 Srcs []string `android:"path,arch_variant"`
674 } `android:"arch_variant"`
675 Address struct {
676 Srcs []string `android:"path,arch_variant"`
677 } `android:"arch_variant"`
678 Hwaddress struct {
679 Srcs []string `android:"path,arch_variant"`
680 } `android:"arch_variant"`
681}
682
683func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
684 if sanitize == nil {
685 return nil
686 }
687 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
688 return sanitized.Address.Srcs
689 }
690 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
691 return sanitized.Hwaddress.Srcs
692 }
693 return sanitized.None.Srcs
694}