blob: 68df8798118aa2ea6e4cc3f2552dcffc0b54db02 [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)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +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
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010054 // if set, add an extra objcopy --prefix-symbols= step
55 Prefix_symbols *string
56
Yo Chianga3ad9b22020-03-18 14:19:07 +080057 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
58 // This is needed only if this library is linked by other modules in build time.
59 // Only makes sense for the Windows target.
60 Windows_import_lib *string `android:"path,arch_variant"`
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +000061
62 // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either
63 // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with
64 // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this
65 // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see
66 // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled.
67 MixedBuildsDisabled bool `blueprint:"mutated"`
Patrice Arruda3554a982019-03-27 19:09:10 -070068}
69
Colin Crossde89fb82017-03-17 13:28:06 -070070type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070071 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080072
Patrice Arruda3554a982019-03-27 19:09:10 -070073 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070074}
75
Colin Crossde89fb82017-03-17 13:28:06 -070076func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070077 return &p.Prebuilt
78}
79
Colin Cross74d73e22017-08-02 11:05:49 -070080func (p *prebuiltLinker) PrebuiltSrcs() []string {
81 return p.properties.Srcs
82}
83
Colin Cross33b2fb72019-05-14 14:07:01 -070084type prebuiltLibraryInterface interface {
85 libraryInterface
86 prebuiltLinkerInterface
87 disablePrebuilt()
88}
89
Colin Crossde89fb82017-03-17 13:28:06 -070090type prebuiltLibraryLinker struct {
91 *libraryDecorator
92 prebuiltLinker
93}
94
95var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070096var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070097
Yi Kong00981662018-08-13 16:02:49 -070098func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
99
100func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +0800101 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -0700102}
103
104func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -0700105 return flags
Yi Kong00981662018-08-13 16:02:49 -0700106}
107
108func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
109 return p.libraryDecorator.linkerProps()
110}
111
Colin Crossce75d2c2016-10-06 16:12:58 -0700112func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700113 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000114
Colin Cross0de8a1e2020-09-18 14:15:30 -0700115 p.libraryDecorator.flagExporter.exportIncludes(ctx)
116 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
117 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
118 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
119 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
120 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
121
122 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000123
Colin Crossce75d2c2016-10-06 16:12:58 -0700124 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700125 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000126 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000127 if len(srcs) > 1 {
128 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
129 return nil
130 }
131
Jiyong Park892a98f2020-12-14 09:20:00 +0900132 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
133
Paul Duffinbce90da2020-03-12 20:17:14 +0000134 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700135
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100136 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
137 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
138 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
139 in, flagsToBuilderFlags(flags), prefixed)
140 in = prefixed
141 }
142
Yo Chianga3ad9b22020-03-18 14:19:07 +0800143 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700144 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
145 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
146 StaticLibrary: in,
147
148 TransitiveStaticLibrariesForOrdering: depSet,
149 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800150 return in
151 }
152
Colin Cross88f6fef2018-09-05 14:20:03 -0700153 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700154 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700155 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800156 outputFile := android.PathForModuleOut(ctx, libName)
157 var implicits android.Paths
158
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200159 if p.stripper.NeedsStrip(ctx) {
160 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700161 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200162 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700163 in = stripped
164 }
165
Colin Crossb60190a2018-09-04 16:28:17 -0700166 // Optimize out relinking against shared libraries whose interface hasn't changed by
167 // depending on a table of contents file instead of the library itself.
168 tocFile := android.PathForModuleOut(ctx, libName+".toc")
169 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400170 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700171
Yo Chianga3ad9b22020-03-18 14:19:07 +0800172 if ctx.Windows() && p.properties.Windows_import_lib != nil {
173 // Consumers of this library actually links to the import library in build
174 // time and dynamically links to the DLL in run time. i.e.
175 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
176 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
177 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
178 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
179 implicits = append(implicits, importLibOutputFile)
180
181 ctx.Build(pctx, android.BuildParams{
182 Rule: android.Cp,
183 Description: "prebuilt import library",
184 Input: importLibSrc,
185 Output: importLibOutputFile,
186 Args: map[string]string{
187 "cpFlags": "-L",
188 },
189 })
190 }
191
192 ctx.Build(pctx, android.BuildParams{
193 Rule: android.Cp,
194 Description: "prebuilt shared library",
195 Implicits: implicits,
196 Input: in,
197 Output: outputFile,
198 Args: map[string]string{
199 "cpFlags": "-L",
200 },
201 })
202
Colin Cross0de8a1e2020-09-18 14:15:30 -0700203 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400204 SharedLibrary: outputFile,
205 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700206
207 TableOfContents: p.tocFile,
208 })
209
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000210 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
211 // library as their source and must not be installed, but libclang_rt.* libraries
212 // have stubs because they are LLNDK libraries, but use an implementation library
213 // as their source and need to be installed. This discrepancy should be resolved
214 // without the prefix hack below.
215 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() &&
216 !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") {
217 ctx.Module().MakeUninstallable()
218 }
219
Yo Chianga3ad9b22020-03-18 14:19:07 +0800220 return outputFile
221 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700222 }
223
Colin Cross649d8172020-12-10 12:30:21 -0800224 if p.header() {
225 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
226
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000227 // Need to return an output path so that the AndroidMk logic doesn't skip
228 // the prebuilt header. For compatibility, in case Android.mk files use a
229 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
230 // placeholder, just like non-prebuilt header modules do in linkStatic().
231 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
232 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
233 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800234 }
235
Colin Crossce75d2c2016-10-06 16:12:58 -0700236 return nil
237}
238
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700239func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
240 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000241 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700242 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000243 if p.static() {
244 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700245 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000246 }
247 if p.shared() {
248 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700249 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000250 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000251 return srcs
252}
253
Jiyong Park379de2f2018-12-19 02:47:14 +0900254func (p *prebuiltLibraryLinker) shared() bool {
255 return p.libraryDecorator.shared()
256}
257
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700258func (p *prebuiltLibraryLinker) nativeCoverage() bool {
259 return false
260}
261
Colin Cross33b2fb72019-05-14 14:07:01 -0700262func (p *prebuiltLibraryLinker) disablePrebuilt() {
263 p.properties.Srcs = nil
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000264 p.properties.MixedBuildsDisabled = true
Colin Cross33b2fb72019-05-14 14:07:01 -0700265}
266
Jiyong Park892a98f2020-12-14 09:20:00 +0900267// Implements versionedInterface
268func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100269 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900270}
271
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000272func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700273 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700274 module.compiler = nil
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000275 module.bazelable = true
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000276 module.bazelHandler = &prebuiltLibraryBazelHandler{module: module, library: library}
Colin Crossce75d2c2016-10-06 16:12:58 -0700277
278 prebuilt := &prebuiltLibraryLinker{
279 libraryDecorator: library,
280 }
281 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700282 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700283
Colin Cross74d73e22017-08-02 11:05:49 -0700284 module.AddProperties(&prebuilt.properties)
285
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000286 if srcsProperty == "" {
287 android.InitPrebuiltModuleWithoutSrcs(module)
288 } else {
289 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
290 return prebuilt.prebuiltSrcs(ctx)
291 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000292
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000293 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
294 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900295
Paul Duffinac6e6082019-12-11 15:22:32 +0000296 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900297 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000298 return module, library
299}
300
Paul Duffinbce90da2020-03-12 20:17:14 +0000301// cc_prebuilt_library installs a precompiled shared library that are
302// listed in the srcs property in the device's directory.
303func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000304 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000305
306 // Prebuilt shared libraries can be included in APEXes
307 android.InitApexModule(module)
308
309 return module.Init()
310}
311
Paul Duffinac6e6082019-12-11 15:22:32 +0000312// cc_prebuilt_library_shared installs a precompiled shared library that are
313// listed in the srcs property in the device's directory.
314func PrebuiltSharedLibraryFactory() android.Module {
315 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
316 return module.Init()
317}
318
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400319// cc_prebuilt_test_library_shared installs a precompiled shared library
320// to be used as a data dependency of a test-related module (such as cc_test, or
321// cc_test_library).
322func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000323 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400324 library.BuildOnlyShared()
325 library.baseInstaller = NewTestInstaller()
326 return module.Init()
327}
328
Paul Duffinac6e6082019-12-11 15:22:32 +0000329func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000330 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000331 library.BuildOnlyShared()
332
333 // Prebuilt shared libraries can be included in APEXes
334 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900335
Leo Li74f7b972017-05-17 11:30:45 -0700336 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700337}
338
Patrice Arruda3554a982019-03-27 19:09:10 -0700339// cc_prebuilt_library_static installs a precompiled static library that are
340// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900341func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700342 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
343 return module.Init()
344}
345
346func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000347 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700348 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000349
Leo Li74f7b972017-05-17 11:30:45 -0700350 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700351}
352
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400353type bazelPrebuiltLibraryStaticAttributes struct {
354 Static_library bazel.LabelAttribute
355 Export_includes bazel.StringListAttribute
356 Export_system_includes bazel.StringListAttribute
357}
358
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000359// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
360// Implements bp2build for cc_prebuilt_library modules. This will generate:
Colin Crossd079e0b2022-08-16 10:27:33 -0700361// - Only a prebuilt_library_static if the shared.enabled property is set to false across all variants.
362// - Only a prebuilt_library_shared if the static.enabled property is set to false across all variants
363// - Both a prebuilt_library_static and prebuilt_library_shared if the aforementioned properties are not false across
364// all variants
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000365//
366// In all cases, prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
367func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
368 prebuiltLibraryStaticBp2Build(ctx, module, true)
369 prebuiltLibrarySharedBp2Build(ctx, module)
370}
371
372func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
373 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammer54549442022-05-11 13:55:06 -0400374 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400375
376 attrs := &bazelPrebuiltLibraryStaticAttributes{
377 Static_library: prebuiltAttrs.Src,
378 Export_includes: exportedIncludes.Includes,
379 Export_system_includes: exportedIncludes.SystemIncludes,
380 }
381
382 props := bazel.BazelTargetModuleProperties{
383 Rule_class: "prebuilt_library_static",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500384 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400385 }
386
387 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000388 if fullBuild {
389 name += "_bp2build_cc_library_static"
390 }
391 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400392}
393
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400394type bazelPrebuiltLibrarySharedAttributes struct {
395 Shared_library bazel.LabelAttribute
396}
397
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400398func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000399 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400400
401 attrs := &bazelPrebuiltLibrarySharedAttributes{
402 Shared_library: prebuiltAttrs.Src,
403 }
404
405 props := bazel.BazelTargetModuleProperties{
406 Rule_class: "prebuilt_library_shared",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500407 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 -0400408 }
409
410 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000411 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 -0400412}
413
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000414type prebuiltObjectProperties struct {
415 Srcs []string `android:"path,arch_variant"`
416}
417
418type prebuiltObjectLinker struct {
419 android.Prebuilt
420 objectLinker
421
422 properties prebuiltObjectProperties
423}
424
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000425type prebuiltLibraryBazelHandler struct {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400426 module *Module
427 library *libraryDecorator
428}
429
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000430var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400431
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000432func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
433 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
434 return
435 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400436 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400437 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
438}
439
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000440func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
441 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
442 return
443 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400444 bazelCtx := ctx.Config().BazelContext
445 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400446 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400447 ctx.ModuleErrorf(err.Error())
448 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400449 }
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000450
451 if h.module.static() {
452 if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
453 return
454 }
455 } else if h.module.Shared() {
456 if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
457 return
458 }
459 } else {
460 return
461 }
462
463 h.module.maybeUnhideFromMake()
464}
465
466func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400467 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400468 if len(staticLibs) > 1 {
469 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000470 return false
Liz Kammer3f9e1552021-04-02 18:47:09 -0400471 }
472
473 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
474
475 // TODO(eakammer):Add stub-related flags if this library is a stub library.
476 // h.library.exportVersioningMacroIfNeeded(ctx)
477
478 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
479 // validation will fail. For now, set this to an empty list.
480 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
481 h.library.collectedSnapshotHeaders = android.Paths{}
482
483 if len(staticLibs) == 0 {
484 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000485 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400486 }
487
488 out := android.PathForBazelOut(ctx, staticLibs[0])
489 h.module.outputFile = android.OptionalPathForPath(out)
490
491 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
492 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
493 StaticLibrary: out,
494
495 TransitiveStaticLibrariesForOrdering: depSet,
496 })
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000497
498 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400499}
500
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000501func (h *prebuiltLibraryBazelHandler) processSharedBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400502 sharedLibs := ccInfo.CcSharedLibraryFiles
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000503 if len(sharedLibs) > 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400504 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000505 return false
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400506 }
507
508 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
509
510 // TODO(eakammer):Add stub-related flags if this library is a stub library.
511 // h.library.exportVersioningMacroIfNeeded(ctx)
512
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400513 if len(sharedLibs) == 0 {
514 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000515 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400516 }
517
518 out := android.PathForBazelOut(ctx, sharedLibs[0])
519 h.module.outputFile = android.OptionalPathForPath(out)
520
521 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
522 h.library.unstrippedOutputFile = out
523
524 var toc android.Path
525 if len(ccInfo.TocFile) > 0 {
526 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
527 } else {
528 toc = out // Just reuse `out` so ninja still gets an input but won't matter
529 }
530
531 info := SharedLibraryInfo{
532 SharedLibrary: out,
533 TableOfContents: android.OptionalPathForPath(toc),
534 Target: ctx.Target(),
535 }
536 ctx.SetProvider(SharedLibraryInfoProvider, info)
537
538 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
539 h.module.maybeUnhideFromMake()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000540 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400541}
542
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000543func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
544 return &p.Prebuilt
545}
546
547var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
548
549func (p *prebuiltObjectLinker) link(ctx ModuleContext,
550 flags Flags, deps PathDeps, objs Objects) android.Path {
551 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700552 // Copy objects to a name matching the final installed name
553 in := p.Prebuilt.SingleSourcePath(ctx)
554 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
555 ctx.Build(pctx, android.BuildParams{
556 Rule: android.CpExecutable,
557 Description: "prebuilt",
558 Output: outputFile,
559 Input: in,
560 })
561 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000562 }
563 return nil
564}
565
Inseob Kim1042d292020-06-01 23:23:05 +0900566func (p *prebuiltObjectLinker) object() bool {
567 return true
568}
569
Colin Cross7cabd422021-06-25 14:21:04 -0700570func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
571 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000572 prebuilt := &prebuiltObjectLinker{
573 objectLinker: objectLinker{
574 baseLinker: NewBaseLinker(nil),
575 },
576 }
577 module.linker = prebuilt
578 module.AddProperties(&prebuilt.properties)
579 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
580 android.InitSdkAwareModule(module)
581 return module
582}
583
584func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700585 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000586 return module.Init()
587}
588
Colin Crossde89fb82017-03-17 13:28:06 -0700589type prebuiltBinaryLinker struct {
590 *binaryDecorator
591 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100592
593 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700594}
595
596var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
597
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100598func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
599 return p.toolPath
600}
601
Colin Crossde89fb82017-03-17 13:28:06 -0700602func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
603 flags Flags, deps PathDeps, objs Objects) android.Path {
604 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700605 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700606 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
607 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100608 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700609 p.unstrippedOutputFile = in
610
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100611 if ctx.Host() {
612 // Host binaries are symlinked to their prebuilt source locations. That
613 // way they are executed directly from there so the linker resolves their
614 // shared library dependencies relative to that location (using
615 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
616 // repository can supply the expected versions of the shared libraries
617 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700618
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100619 // These shared lib paths may point to copies of the libs in
620 // .intermediates, which isn't where the binary will load them from, but
621 // it's fine for dependency tracking. If a library dependency is updated,
622 // the symlink will get a new timestamp, along with any installed symlinks
623 // handled in make.
624 sharedLibPaths := deps.EarlySharedLibs
625 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
626 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
627
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100628 var fromPath = in.String()
629 if !filepath.IsAbs(fromPath) {
630 fromPath = "$$PWD/" + fromPath
631 }
632
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100633 ctx.Build(pctx, android.BuildParams{
634 Rule: android.Symlink,
635 Output: outputFile,
636 Input: in,
637 Implicits: sharedLibPaths,
638 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100639 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100640 },
641 })
642
643 p.toolPath = android.OptionalPathForPath(outputFile)
644 } else {
645 if p.stripper.NeedsStrip(ctx) {
646 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
647 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
648 in = stripped
649 }
650
651 // Copy binaries to a name matching the final installed name
652 ctx.Build(pctx, android.BuildParams{
653 Rule: android.CpExecutable,
654 Description: "prebuilt",
655 Output: outputFile,
656 Input: in,
657 })
658 }
Colin Cross94921e72017-08-08 16:20:15 -0700659
660 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700661 }
662
663 return nil
664}
665
Inseob Kim7f283f42020-06-01 21:53:49 +0900666func (p *prebuiltBinaryLinker) binary() bool {
667 return true
668}
669
Patrice Arruda3554a982019-03-27 19:09:10 -0700670// cc_prebuilt_binary installs a precompiled executable in srcs property in the
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000671// device's directory, for both the host and device
672func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700673 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
674 return module.Init()
675}
676
677func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbdc922f2021-12-14 14:21:21 -0500678 module, binary := newBinary(hod, false)
Colin Crossde89fb82017-03-17 13:28:06 -0700679 module.compiler = nil
680
681 prebuilt := &prebuiltBinaryLinker{
682 binaryDecorator: binary,
683 }
684 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100685 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700686
Colin Cross74d73e22017-08-02 11:05:49 -0700687 module.AddProperties(&prebuilt.properties)
688
689 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700690 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700691}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700692
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000693type bazelPrebuiltBinaryAttributes struct {
694 Src bazel.LabelAttribute
695 Strip stripAttributes
696}
697
698func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
699 prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module)
700
701 var la linkerAttributes
702 la.convertStripProps(ctx, module)
703 attrs := &bazelPrebuiltBinaryAttributes{
704 Src: prebuiltAttrs.Src,
705 Strip: stripAttrsFromLinkerAttrs(&la),
706 }
707
708 props := bazel.BazelTargetModuleProperties{
709 Rule_class: "cc_prebuilt_binary",
710 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl",
711 }
712
713 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
714 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
715}
716
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700717type Sanitized struct {
718 None struct {
719 Srcs []string `android:"path,arch_variant"`
720 } `android:"arch_variant"`
721 Address struct {
722 Srcs []string `android:"path,arch_variant"`
723 } `android:"arch_variant"`
724 Hwaddress struct {
725 Srcs []string `android:"path,arch_variant"`
726 } `android:"arch_variant"`
727}
728
729func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
730 if sanitize == nil {
731 return nil
732 }
733 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
734 return sanitized.Address.Srcs
735 }
736 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
737 return sanitized.Hwaddress.Srcs
738 }
739 return sanitized.None.Srcs
740}