blob: 03a600a851bea3bba215542094e02e8f6c34489e [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)
Colin Crossc5075e92022-12-05 16:46:39 -080035 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 return module, library
297}
298
Paul Duffinbce90da2020-03-12 20:17:14 +0000299// cc_prebuilt_library installs a precompiled shared library that are
300// listed in the srcs property in the device's directory.
301func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000302 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000303
304 // Prebuilt shared libraries can be included in APEXes
305 android.InitApexModule(module)
306
307 return module.Init()
308}
309
Paul Duffinac6e6082019-12-11 15:22:32 +0000310// cc_prebuilt_library_shared installs a precompiled shared library that are
311// listed in the srcs property in the device's directory.
312func PrebuiltSharedLibraryFactory() android.Module {
313 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
314 return module.Init()
315}
316
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400317// cc_prebuilt_test_library_shared installs a precompiled shared library
318// to be used as a data dependency of a test-related module (such as cc_test, or
319// cc_test_library).
320func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000321 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400322 library.BuildOnlyShared()
323 library.baseInstaller = NewTestInstaller()
324 return module.Init()
325}
326
Paul Duffinac6e6082019-12-11 15:22:32 +0000327func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000328 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000329 library.BuildOnlyShared()
330
331 // Prebuilt shared libraries can be included in APEXes
332 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900333
Leo Li74f7b972017-05-17 11:30:45 -0700334 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700335}
336
Patrice Arruda3554a982019-03-27 19:09:10 -0700337// cc_prebuilt_library_static installs a precompiled static library that are
338// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900339func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700340 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
341 return module.Init()
342}
343
344func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000345 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700346 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000347
Leo Li74f7b972017-05-17 11:30:45 -0700348 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700349}
350
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400351type bazelPrebuiltLibraryStaticAttributes struct {
352 Static_library bazel.LabelAttribute
353 Export_includes bazel.StringListAttribute
354 Export_system_includes bazel.StringListAttribute
355}
356
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000357// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
358// Implements bp2build for cc_prebuilt_library modules. This will generate:
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000359// - Only a cc_prebuilt_library_static if the shared.enabled property is set to false across all variants.
360// - Only a cc_prebuilt_library_shared if the static.enabled property is set to false across all variants
361// - Both a cc_prebuilt_library_static and cc_prebuilt_library_shared if the aforementioned properties are not false across
Colin Crossd079e0b2022-08-16 10:27:33 -0700362// all variants
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000363//
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000364// In all cases, cc_prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000365func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
366 prebuiltLibraryStaticBp2Build(ctx, module, true)
367 prebuiltLibrarySharedBp2Build(ctx, module)
368}
369
370func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
371 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammer54549442022-05-11 13:55:06 -0400372 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400373
374 attrs := &bazelPrebuiltLibraryStaticAttributes{
375 Static_library: prebuiltAttrs.Src,
376 Export_includes: exportedIncludes.Includes,
377 Export_system_includes: exportedIncludes.SystemIncludes,
378 }
379
380 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000381 Rule_class: "cc_prebuilt_library_static",
382 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400383 }
384
385 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000386 if fullBuild {
387 name += "_bp2build_cc_library_static"
388 }
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000389
390 tags := android.ApexAvailableTags(module)
391 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, 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{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000406 Rule_class: "cc_prebuilt_library_shared",
407 Bzl_load_location: "//build/bazel/rules/cc: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())
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000411 tags := android.ApexAvailableTags(module)
412 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400413}
414
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000415type prebuiltObjectProperties struct {
416 Srcs []string `android:"path,arch_variant"`
417}
418
419type prebuiltObjectLinker struct {
420 android.Prebuilt
421 objectLinker
422
423 properties prebuiltObjectProperties
424}
425
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000426type prebuiltLibraryBazelHandler struct {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400427 module *Module
428 library *libraryDecorator
429}
430
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000431var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400432
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000433func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
434 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
435 return
436 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400437 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400438 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
439}
440
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000441func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
442 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
443 return
444 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400445 bazelCtx := ctx.Config().BazelContext
446 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400447 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400448 ctx.ModuleErrorf(err.Error())
449 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400450 }
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000451
452 if h.module.static() {
453 if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
454 return
455 }
456 } else if h.module.Shared() {
457 if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
458 return
459 }
460 } else {
461 return
462 }
463
464 h.module.maybeUnhideFromMake()
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500465
466 h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000467}
468
469func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400470 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400471 if len(staticLibs) > 1 {
472 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000473 return false
Liz Kammer3f9e1552021-04-02 18:47:09 -0400474 }
475
476 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
477
478 // TODO(eakammer):Add stub-related flags if this library is a stub library.
479 // h.library.exportVersioningMacroIfNeeded(ctx)
480
481 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
482 // validation will fail. For now, set this to an empty list.
483 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
484 h.library.collectedSnapshotHeaders = android.Paths{}
485
486 if len(staticLibs) == 0 {
487 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000488 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400489 }
490
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500491 var outputPath android.Path = android.PathForBazelOut(ctx, staticLibs[0])
492 if len(ccInfo.TidyFiles) > 0 {
493 h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
494 outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
495 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400496
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500497 h.module.outputFile = android.OptionalPathForPath(outputPath)
498
499 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputPath).Build()
Liz Kammer3f9e1552021-04-02 18:47:09 -0400500 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500501 StaticLibrary: outputPath,
Liz Kammer3f9e1552021-04-02 18:47:09 -0400502 TransitiveStaticLibrariesForOrdering: depSet,
503 })
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000504
505 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400506}
507
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000508func (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 -0400509 sharedLibs := ccInfo.CcSharedLibraryFiles
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000510 if len(sharedLibs) > 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400511 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000512 return false
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400513 }
514
515 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
516
517 // TODO(eakammer):Add stub-related flags if this library is a stub library.
518 // h.library.exportVersioningMacroIfNeeded(ctx)
519
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400520 if len(sharedLibs) == 0 {
521 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000522 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400523 }
524
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500525 var outputPath android.Path = android.PathForBazelOut(ctx, sharedLibs[0])
526 if len(ccInfo.TidyFiles) > 0 {
527 h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
528 outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
529 }
530
531 h.module.outputFile = android.OptionalPathForPath(outputPath)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400532
533 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500534 h.library.unstrippedOutputFile = outputPath
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400535
536 var toc android.Path
537 if len(ccInfo.TocFile) > 0 {
538 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
539 } else {
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500540 toc = outputPath // Just reuse `out` so ninja still gets an input but won't matter
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400541 }
542
543 info := SharedLibraryInfo{
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500544 SharedLibrary: outputPath,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400545 TableOfContents: android.OptionalPathForPath(toc),
546 Target: ctx.Target(),
547 }
548 ctx.SetProvider(SharedLibraryInfoProvider, info)
549
550 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
551 h.module.maybeUnhideFromMake()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000552 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400553}
554
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000555func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
556 return &p.Prebuilt
557}
558
559var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
560
561func (p *prebuiltObjectLinker) link(ctx ModuleContext,
562 flags Flags, deps PathDeps, objs Objects) android.Path {
563 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700564 // Copy objects to a name matching the final installed name
565 in := p.Prebuilt.SingleSourcePath(ctx)
566 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
567 ctx.Build(pctx, android.BuildParams{
568 Rule: android.CpExecutable,
569 Description: "prebuilt",
570 Output: outputFile,
571 Input: in,
572 })
573 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000574 }
575 return nil
576}
577
Inseob Kim1042d292020-06-01 23:23:05 +0900578func (p *prebuiltObjectLinker) object() bool {
579 return true
580}
581
Colin Cross7cabd422021-06-25 14:21:04 -0700582func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
583 module := newObject(hod)
Colin Crossc5075e92022-12-05 16:46:39 -0800584 module.bazelHandler = &prebuiltObjectBazelHandler{module: module}
585 module.bazelable = true
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000586 prebuilt := &prebuiltObjectLinker{
587 objectLinker: objectLinker{
588 baseLinker: NewBaseLinker(nil),
589 },
590 }
591 module.linker = prebuilt
592 module.AddProperties(&prebuilt.properties)
593 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000594 return module
595}
596
Colin Crossc5075e92022-12-05 16:46:39 -0800597type prebuiltObjectBazelHandler struct {
598 module *Module
599}
600
601var _ BazelHandler = (*prebuiltObjectBazelHandler)(nil)
602
603func (h *prebuiltObjectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
604 bazelCtx := ctx.Config().BazelContext
605 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
606}
607
608func (h *prebuiltObjectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
609 bazelCtx := ctx.Config().BazelContext
610 outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
611 if err != nil {
612 ctx.ModuleErrorf(err.Error())
613 return
614 }
615 if len(outputs) != 1 {
616 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
617 return
618 }
619 out := android.PathForBazelOut(ctx, outputs[0])
620 h.module.outputFile = android.OptionalPathForPath(out)
621 h.module.maybeUnhideFromMake()
622}
623
624type bazelPrebuiltObjectAttributes struct {
625 Src bazel.LabelAttribute
626}
627
628func prebuiltObjectBp2Build(ctx android.TopDownMutatorContext, module *Module) {
629 prebuiltAttrs := bp2BuildParsePrebuiltObjectProps(ctx, module)
630
631 attrs := &bazelPrebuiltObjectAttributes{
632 Src: prebuiltAttrs.Src,
633 }
634
635 props := bazel.BazelTargetModuleProperties{
636 Rule_class: "cc_prebuilt_object",
637 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_object.bzl",
638 }
639
640 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
641 tags := android.ApexAvailableTags(module)
642 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
643}
644
645func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700646 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000647 return module.Init()
648}
649
Colin Crossde89fb82017-03-17 13:28:06 -0700650type prebuiltBinaryLinker struct {
651 *binaryDecorator
652 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100653
654 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700655}
656
657var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
658
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100659func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
660 return p.toolPath
661}
662
Colin Crossde89fb82017-03-17 13:28:06 -0700663func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
664 flags Flags, deps PathDeps, objs Objects) android.Path {
665 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700666 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700667 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
668 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100669 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700670 p.unstrippedOutputFile = in
671
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100672 if ctx.Host() {
673 // Host binaries are symlinked to their prebuilt source locations. That
674 // way they are executed directly from there so the linker resolves their
675 // shared library dependencies relative to that location (using
676 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
677 // repository can supply the expected versions of the shared libraries
678 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700679
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100680 // These shared lib paths may point to copies of the libs in
681 // .intermediates, which isn't where the binary will load them from, but
682 // it's fine for dependency tracking. If a library dependency is updated,
683 // the symlink will get a new timestamp, along with any installed symlinks
684 // handled in make.
685 sharedLibPaths := deps.EarlySharedLibs
686 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
687 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
688
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100689 var fromPath = in.String()
690 if !filepath.IsAbs(fromPath) {
691 fromPath = "$$PWD/" + fromPath
692 }
693
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100694 ctx.Build(pctx, android.BuildParams{
695 Rule: android.Symlink,
696 Output: outputFile,
697 Input: in,
698 Implicits: sharedLibPaths,
699 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100700 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100701 },
702 })
703
704 p.toolPath = android.OptionalPathForPath(outputFile)
705 } else {
706 if p.stripper.NeedsStrip(ctx) {
707 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
708 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
709 in = stripped
710 }
711
712 // Copy binaries to a name matching the final installed name
713 ctx.Build(pctx, android.BuildParams{
714 Rule: android.CpExecutable,
715 Description: "prebuilt",
716 Output: outputFile,
717 Input: in,
718 })
719 }
Colin Cross94921e72017-08-08 16:20:15 -0700720
721 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700722 }
723
724 return nil
725}
726
Inseob Kim7f283f42020-06-01 21:53:49 +0900727func (p *prebuiltBinaryLinker) binary() bool {
728 return true
729}
730
Patrice Arruda3554a982019-03-27 19:09:10 -0700731// 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 +0000732// device's directory, for both the host and device
733func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700734 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
735 return module.Init()
736}
737
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000738type prebuiltBinaryBazelHandler struct {
739 module *Module
740 decorator *binaryDecorator
741}
742
Leo Li74f7b972017-05-17 11:30:45 -0700743func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000744 module, binary := newBinary(hod, true)
Colin Crossde89fb82017-03-17 13:28:06 -0700745 module.compiler = nil
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000746 module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary}
Colin Crossde89fb82017-03-17 13:28:06 -0700747
748 prebuilt := &prebuiltBinaryLinker{
749 binaryDecorator: binary,
750 }
751 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100752 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700753
Colin Cross74d73e22017-08-02 11:05:49 -0700754 module.AddProperties(&prebuilt.properties)
755
756 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700757 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700758}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700759
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000760var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil)
761
762func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
763 bazelCtx := ctx.Config().BazelContext
764 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
765}
766
767func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
768 bazelCtx := ctx.Config().BazelContext
769 outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
770 if err != nil {
771 ctx.ModuleErrorf(err.Error())
772 return
773 }
774 if len(outputs) != 1 {
775 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
776 return
777 }
778 out := android.PathForBazelOut(ctx, outputs[0])
779 h.module.outputFile = android.OptionalPathForPath(out)
780 h.module.maybeUnhideFromMake()
781}
782
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000783type bazelPrebuiltBinaryAttributes struct {
784 Src bazel.LabelAttribute
785 Strip stripAttributes
786}
787
788func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
789 prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module)
790
791 var la linkerAttributes
792 la.convertStripProps(ctx, module)
793 attrs := &bazelPrebuiltBinaryAttributes{
794 Src: prebuiltAttrs.Src,
795 Strip: stripAttrsFromLinkerAttrs(&la),
796 }
797
798 props := bazel.BazelTargetModuleProperties{
799 Rule_class: "cc_prebuilt_binary",
800 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl",
801 }
802
803 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000804 tags := android.ApexAvailableTags(module)
805 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000806}
807
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700808type Sanitized struct {
809 None struct {
810 Srcs []string `android:"path,arch_variant"`
811 } `android:"arch_variant"`
812 Address struct {
813 Srcs []string `android:"path,arch_variant"`
814 } `android:"arch_variant"`
815 Hwaddress struct {
816 Srcs []string `android:"path,arch_variant"`
817 } `android:"arch_variant"`
818}
819
820func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
821 if sanitize == nil {
822 return nil
823 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400824 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700825 return sanitized.Address.Srcs
826 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400827 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700828 return sanitized.Hwaddress.Srcs
829 }
830 return sanitized.None.Srcs
831}