blob: e8c60baeaac2fe742e6ce1f91ac3a1129b8e0c6e [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"
Chris Parsonsf874e462022-05-10 13:50:12 -040023 "android/soong/bazel/cquery"
Colin Crossce75d2c2016-10-06 16:12:58 -070024)
25
26func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000027 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000031 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000032 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
33 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040034 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000035 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000036 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
39type prebuiltLinkerInterface interface {
40 Name(string) string
41 prebuilt() *android.Prebuilt
42}
43
Patrice Arruda3554a982019-03-27 19:09:10 -070044type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070045 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
46 Srcs []string `android:"path,arch_variant"`
47
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070048 Sanitized Sanitized `android:"arch_variant"`
49
Patrice Arruda3554a982019-03-27 19:09:10 -070050 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
51 // symbols, etc), default true.
52 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080053
54 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
55 // This is needed only if this library is linked by other modules in build time.
56 // Only makes sense for the Windows target.
57 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070058}
59
Colin Crossde89fb82017-03-17 13:28:06 -070060type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070061 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080062
Patrice Arruda3554a982019-03-27 19:09:10 -070063 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070064}
65
Colin Crossde89fb82017-03-17 13:28:06 -070066func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070067 return &p.Prebuilt
68}
69
Colin Cross74d73e22017-08-02 11:05:49 -070070func (p *prebuiltLinker) PrebuiltSrcs() []string {
71 return p.properties.Srcs
72}
73
Colin Cross33b2fb72019-05-14 14:07:01 -070074type prebuiltLibraryInterface interface {
75 libraryInterface
76 prebuiltLinkerInterface
77 disablePrebuilt()
78}
79
Colin Crossde89fb82017-03-17 13:28:06 -070080type prebuiltLibraryLinker struct {
81 *libraryDecorator
82 prebuiltLinker
83}
84
85var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070086var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070087
Yi Kong00981662018-08-13 16:02:49 -070088func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
89
90func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080091 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070092}
93
94func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070095 return flags
Yi Kong00981662018-08-13 16:02:49 -070096}
97
98func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
99 return p.libraryDecorator.linkerProps()
100}
101
Colin Crossce75d2c2016-10-06 16:12:58 -0700102func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700103 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000104
Colin Cross0de8a1e2020-09-18 14:15:30 -0700105 p.libraryDecorator.flagExporter.exportIncludes(ctx)
106 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
107 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
108 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
109 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
110 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
111
112 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000113
Colin Crossce75d2c2016-10-06 16:12:58 -0700114 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700115 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000116 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000117 if len(srcs) > 1 {
118 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
119 return nil
120 }
121
Jiyong Park892a98f2020-12-14 09:20:00 +0900122 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
123
Paul Duffinbce90da2020-03-12 20:17:14 +0000124 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700125
Yo Chianga3ad9b22020-03-18 14:19:07 +0800126 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700127 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
128 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
129 StaticLibrary: in,
130
131 TransitiveStaticLibrariesForOrdering: depSet,
132 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800133 return in
134 }
135
Colin Cross88f6fef2018-09-05 14:20:03 -0700136 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700137 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700138 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800139 outputFile := android.PathForModuleOut(ctx, libName)
140 var implicits android.Paths
141
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200142 if p.stripper.NeedsStrip(ctx) {
143 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700144 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200145 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700146 in = stripped
147 }
148
Colin Crossb60190a2018-09-04 16:28:17 -0700149 // Optimize out relinking against shared libraries whose interface hasn't changed by
150 // depending on a table of contents file instead of the library itself.
151 tocFile := android.PathForModuleOut(ctx, libName+".toc")
152 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400153 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700154
Yo Chianga3ad9b22020-03-18 14:19:07 +0800155 if ctx.Windows() && p.properties.Windows_import_lib != nil {
156 // Consumers of this library actually links to the import library in build
157 // time and dynamically links to the DLL in run time. i.e.
158 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
159 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
160 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
161 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
162 implicits = append(implicits, importLibOutputFile)
163
164 ctx.Build(pctx, android.BuildParams{
165 Rule: android.Cp,
166 Description: "prebuilt import library",
167 Input: importLibSrc,
168 Output: importLibOutputFile,
169 Args: map[string]string{
170 "cpFlags": "-L",
171 },
172 })
173 }
174
175 ctx.Build(pctx, android.BuildParams{
176 Rule: android.Cp,
177 Description: "prebuilt shared library",
178 Implicits: implicits,
179 Input: in,
180 Output: outputFile,
181 Args: map[string]string{
182 "cpFlags": "-L",
183 },
184 })
185
Colin Cross0de8a1e2020-09-18 14:15:30 -0700186 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400187 SharedLibrary: outputFile,
188 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700189
190 TableOfContents: p.tocFile,
191 })
192
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000193 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
194 // library as their source and must not be installed, but libclang_rt.* libraries
195 // have stubs because they are LLNDK libraries, but use an implementation library
196 // as their source and need to be installed. This discrepancy should be resolved
197 // without the prefix hack below.
198 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() &&
199 !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") {
200 ctx.Module().MakeUninstallable()
201 }
202
Yo Chianga3ad9b22020-03-18 14:19:07 +0800203 return outputFile
204 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700205 }
206
Colin Cross649d8172020-12-10 12:30:21 -0800207 if p.header() {
208 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
209
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000210 // Need to return an output path so that the AndroidMk logic doesn't skip
211 // the prebuilt header. For compatibility, in case Android.mk files use a
212 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
213 // placeholder, just like non-prebuilt header modules do in linkStatic().
214 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
215 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
216 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800217 }
218
Colin Crossce75d2c2016-10-06 16:12:58 -0700219 return nil
220}
221
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700222func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
223 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000224 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700225 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000226 if p.static() {
227 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700228 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000229 }
230 if p.shared() {
231 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700232 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000233 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000234 return srcs
235}
236
Jiyong Park379de2f2018-12-19 02:47:14 +0900237func (p *prebuiltLibraryLinker) shared() bool {
238 return p.libraryDecorator.shared()
239}
240
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700241func (p *prebuiltLibraryLinker) nativeCoverage() bool {
242 return false
243}
244
Colin Cross33b2fb72019-05-14 14:07:01 -0700245func (p *prebuiltLibraryLinker) disablePrebuilt() {
246 p.properties.Srcs = nil
247}
248
Jiyong Park892a98f2020-12-14 09:20:00 +0900249// Implements versionedInterface
250func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100251 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900252}
253
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000254func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700255 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700256 module.compiler = nil
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000257 module.bazelable = true
Colin Crossce75d2c2016-10-06 16:12:58 -0700258
259 prebuilt := &prebuiltLibraryLinker{
260 libraryDecorator: library,
261 }
262 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700263 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700264
Colin Cross74d73e22017-08-02 11:05:49 -0700265 module.AddProperties(&prebuilt.properties)
266
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000267 if srcsProperty == "" {
268 android.InitPrebuiltModuleWithoutSrcs(module)
269 } else {
270 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
271 return prebuilt.prebuiltSrcs(ctx)
272 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000273
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000274 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
275 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900276
Paul Duffinac6e6082019-12-11 15:22:32 +0000277 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900278 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000279 return module, library
280}
281
Paul Duffinbce90da2020-03-12 20:17:14 +0000282// cc_prebuilt_library installs a precompiled shared library that are
283// listed in the srcs property in the device's directory.
284func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000285 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000286
287 // Prebuilt shared libraries can be included in APEXes
288 android.InitApexModule(module)
289
290 return module.Init()
291}
292
Paul Duffinac6e6082019-12-11 15:22:32 +0000293// cc_prebuilt_library_shared installs a precompiled shared library that are
294// listed in the srcs property in the device's directory.
295func PrebuiltSharedLibraryFactory() android.Module {
296 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
297 return module.Init()
298}
299
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400300// cc_prebuilt_test_library_shared installs a precompiled shared library
301// to be used as a data dependency of a test-related module (such as cc_test, or
302// cc_test_library).
303func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000304 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400305 library.BuildOnlyShared()
306 library.baseInstaller = NewTestInstaller()
307 return module.Init()
308}
309
Paul Duffinac6e6082019-12-11 15:22:32 +0000310func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000311 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000312 library.BuildOnlyShared()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400313 module.bazelable = true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400314 module.bazelHandler = &prebuiltSharedLibraryBazelHandler{module: module, library: library}
Paul Duffinac6e6082019-12-11 15:22:32 +0000315
316 // Prebuilt shared libraries can be included in APEXes
317 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900318
Leo Li74f7b972017-05-17 11:30:45 -0700319 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700320}
321
Patrice Arruda3554a982019-03-27 19:09:10 -0700322// cc_prebuilt_library_static installs a precompiled static library that are
323// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900324func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700325 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
326 return module.Init()
327}
328
329func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000330 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700331 library.BuildOnlyStatic()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400332 module.bazelable = true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400333 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700334 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700335}
336
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400337type bazelPrebuiltLibraryStaticAttributes struct {
338 Static_library bazel.LabelAttribute
339 Export_includes bazel.StringListAttribute
340 Export_system_includes bazel.StringListAttribute
341}
342
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000343// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
344// Implements bp2build for cc_prebuilt_library modules. This will generate:
345// * Only a prebuilt_library_static if the shared.enabled property is set to false across all variants.
346// * Only a prebuilt_library_shared if the static.enabled property is set to false across all variants
347// * Both a prebuilt_library_static and prebuilt_library_shared if the aforementioned properties are not false across
348// all variants
349//
350// In all cases, prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
351func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
352 prebuiltLibraryStaticBp2Build(ctx, module, true)
353 prebuiltLibrarySharedBp2Build(ctx, module)
354}
355
356func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
357 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400358 exportedIncludes := Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx, module)
359
360 attrs := &bazelPrebuiltLibraryStaticAttributes{
361 Static_library: prebuiltAttrs.Src,
362 Export_includes: exportedIncludes.Includes,
363 Export_system_includes: exportedIncludes.SystemIncludes,
364 }
365
366 props := bazel.BazelTargetModuleProperties{
367 Rule_class: "prebuilt_library_static",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500368 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400369 }
370
371 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000372 if fullBuild {
373 name += "_bp2build_cc_library_static"
374 }
375 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400376}
377
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400378type bazelPrebuiltLibrarySharedAttributes struct {
379 Shared_library bazel.LabelAttribute
380}
381
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400382func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000383 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400384
385 attrs := &bazelPrebuiltLibrarySharedAttributes{
386 Shared_library: prebuiltAttrs.Src,
387 }
388
389 props := bazel.BazelTargetModuleProperties{
390 Rule_class: "prebuilt_library_shared",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500391 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 -0400392 }
393
394 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000395 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 -0400396}
397
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000398type prebuiltObjectProperties struct {
399 Srcs []string `android:"path,arch_variant"`
400}
401
402type prebuiltObjectLinker struct {
403 android.Prebuilt
404 objectLinker
405
406 properties prebuiltObjectProperties
407}
408
Liz Kammer3f9e1552021-04-02 18:47:09 -0400409type prebuiltStaticLibraryBazelHandler struct {
Chris Parsonsf874e462022-05-10 13:50:12 -0400410 BazelHandler
Liz Kammer3f9e1552021-04-02 18:47:09 -0400411
412 module *Module
413 library *libraryDecorator
414}
415
Chris Parsonsf874e462022-05-10 13:50:12 -0400416func (h *prebuiltStaticLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400417 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400418 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
419}
420
421func (h *prebuiltStaticLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
422 bazelCtx := ctx.Config().BazelContext
423 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400424 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400425 ctx.ModuleErrorf(err.Error())
426 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400427 }
Liz Kammerfe23bf32021-04-09 16:17:05 -0400428 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400429 if len(staticLibs) > 1 {
430 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
Chris Parsonsf874e462022-05-10 13:50:12 -0400431 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400432 }
433
434 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
435
436 // TODO(eakammer):Add stub-related flags if this library is a stub library.
437 // h.library.exportVersioningMacroIfNeeded(ctx)
438
439 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
440 // validation will fail. For now, set this to an empty list.
441 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
442 h.library.collectedSnapshotHeaders = android.Paths{}
443
444 if len(staticLibs) == 0 {
445 h.module.outputFile = android.OptionalPath{}
Chris Parsonsf874e462022-05-10 13:50:12 -0400446 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400447 }
448
449 out := android.PathForBazelOut(ctx, staticLibs[0])
450 h.module.outputFile = android.OptionalPathForPath(out)
451
452 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
453 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
454 StaticLibrary: out,
455
456 TransitiveStaticLibrariesForOrdering: depSet,
457 })
Liz Kammer3f9e1552021-04-02 18:47:09 -0400458}
459
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400460type prebuiltSharedLibraryBazelHandler struct {
Chris Parsonsf874e462022-05-10 13:50:12 -0400461 BazelHandler
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400462
463 module *Module
464 library *libraryDecorator
465}
466
Chris Parsonsf874e462022-05-10 13:50:12 -0400467func (h *prebuiltSharedLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400468 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400469 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
470}
471
472func (h *prebuiltSharedLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
473 bazelCtx := ctx.Config().BazelContext
474 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400475 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400476 ctx.ModuleErrorf(err.Error())
477 return
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400478 }
479 sharedLibs := ccInfo.CcSharedLibraryFiles
480 if len(sharedLibs) != 1 {
481 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Chris Parsonsf874e462022-05-10 13:50:12 -0400482 return
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400483 }
484
485 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
486
487 // TODO(eakammer):Add stub-related flags if this library is a stub library.
488 // h.library.exportVersioningMacroIfNeeded(ctx)
489
490 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
491 // validation will fail. For now, set this to an empty list.
492 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
493 h.library.collectedSnapshotHeaders = android.Paths{}
494
495 if len(sharedLibs) == 0 {
496 h.module.outputFile = android.OptionalPath{}
Chris Parsonsf874e462022-05-10 13:50:12 -0400497 return
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400498 }
499
500 out := android.PathForBazelOut(ctx, sharedLibs[0])
501 h.module.outputFile = android.OptionalPathForPath(out)
502
503 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
504 h.library.unstrippedOutputFile = out
505
506 var toc android.Path
507 if len(ccInfo.TocFile) > 0 {
508 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
509 } else {
510 toc = out // Just reuse `out` so ninja still gets an input but won't matter
511 }
512
513 info := SharedLibraryInfo{
514 SharedLibrary: out,
515 TableOfContents: android.OptionalPathForPath(toc),
516 Target: ctx.Target(),
517 }
518 ctx.SetProvider(SharedLibraryInfoProvider, info)
519
520 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
521 h.module.maybeUnhideFromMake()
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400522}
523
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000524func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
525 return &p.Prebuilt
526}
527
528var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
529
530func (p *prebuiltObjectLinker) link(ctx ModuleContext,
531 flags Flags, deps PathDeps, objs Objects) android.Path {
532 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700533 // Copy objects to a name matching the final installed name
534 in := p.Prebuilt.SingleSourcePath(ctx)
535 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
536 ctx.Build(pctx, android.BuildParams{
537 Rule: android.CpExecutable,
538 Description: "prebuilt",
539 Output: outputFile,
540 Input: in,
541 })
542 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000543 }
544 return nil
545}
546
Inseob Kim1042d292020-06-01 23:23:05 +0900547func (p *prebuiltObjectLinker) object() bool {
548 return true
549}
550
Colin Cross7cabd422021-06-25 14:21:04 -0700551func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
552 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000553 prebuilt := &prebuiltObjectLinker{
554 objectLinker: objectLinker{
555 baseLinker: NewBaseLinker(nil),
556 },
557 }
558 module.linker = prebuilt
559 module.AddProperties(&prebuilt.properties)
560 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
561 android.InitSdkAwareModule(module)
562 return module
563}
564
565func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700566 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000567 return module.Init()
568}
569
Colin Crossde89fb82017-03-17 13:28:06 -0700570type prebuiltBinaryLinker struct {
571 *binaryDecorator
572 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100573
574 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700575}
576
577var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
578
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100579func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
580 return p.toolPath
581}
582
Colin Crossde89fb82017-03-17 13:28:06 -0700583func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
584 flags Flags, deps PathDeps, objs Objects) android.Path {
585 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700586 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700587 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
588 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100589 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700590 p.unstrippedOutputFile = in
591
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100592 if ctx.Host() {
593 // Host binaries are symlinked to their prebuilt source locations. That
594 // way they are executed directly from there so the linker resolves their
595 // shared library dependencies relative to that location (using
596 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
597 // repository can supply the expected versions of the shared libraries
598 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700599
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100600 // These shared lib paths may point to copies of the libs in
601 // .intermediates, which isn't where the binary will load them from, but
602 // it's fine for dependency tracking. If a library dependency is updated,
603 // the symlink will get a new timestamp, along with any installed symlinks
604 // handled in make.
605 sharedLibPaths := deps.EarlySharedLibs
606 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
607 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
608
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100609 var fromPath = in.String()
610 if !filepath.IsAbs(fromPath) {
611 fromPath = "$$PWD/" + fromPath
612 }
613
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100614 ctx.Build(pctx, android.BuildParams{
615 Rule: android.Symlink,
616 Output: outputFile,
617 Input: in,
618 Implicits: sharedLibPaths,
619 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100620 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100621 },
622 })
623
624 p.toolPath = android.OptionalPathForPath(outputFile)
625 } else {
626 if p.stripper.NeedsStrip(ctx) {
627 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
628 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
629 in = stripped
630 }
631
632 // Copy binaries to a name matching the final installed name
633 ctx.Build(pctx, android.BuildParams{
634 Rule: android.CpExecutable,
635 Description: "prebuilt",
636 Output: outputFile,
637 Input: in,
638 })
639 }
Colin Cross94921e72017-08-08 16:20:15 -0700640
641 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700642 }
643
644 return nil
645}
646
Inseob Kim7f283f42020-06-01 21:53:49 +0900647func (p *prebuiltBinaryLinker) binary() bool {
648 return true
649}
650
Patrice Arruda3554a982019-03-27 19:09:10 -0700651// cc_prebuilt_binary installs a precompiled executable in srcs property in the
652// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700653func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700654 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
655 return module.Init()
656}
657
658func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbdc922f2021-12-14 14:21:21 -0500659 module, binary := newBinary(hod, false)
Colin Crossde89fb82017-03-17 13:28:06 -0700660 module.compiler = nil
661
662 prebuilt := &prebuiltBinaryLinker{
663 binaryDecorator: binary,
664 }
665 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100666 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700667
Colin Cross74d73e22017-08-02 11:05:49 -0700668 module.AddProperties(&prebuilt.properties)
669
670 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700671 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700672}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700673
674type Sanitized struct {
675 None struct {
676 Srcs []string `android:"path,arch_variant"`
677 } `android:"arch_variant"`
678 Address struct {
679 Srcs []string `android:"path,arch_variant"`
680 } `android:"arch_variant"`
681 Hwaddress struct {
682 Srcs []string `android:"path,arch_variant"`
683 } `android:"arch_variant"`
684}
685
686func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
687 if sanitize == nil {
688 return nil
689 }
690 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
691 return sanitized.Address.Srcs
692 }
693 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
694 return sanitized.Hwaddress.Srcs
695 }
696 return sanitized.None.Srcs
697}