blob: 5b7ba434685410d2ae16b428ccd5fc44c7ea6ce1 [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.") {
Jingwen Chen8ac7d7d2023-03-20 11:05:16 +0000217 ctx.Module().MakeUninstallable()
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000218 }
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
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc353abd2023-03-10 20:57:38 +0000355 Alwayslink bazel.BoolAttribute
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400356}
357
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000358// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
359// 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 +0000360// - Only a cc_prebuilt_library_static if the shared.enabled property is set to false across all variants.
361// - Only a cc_prebuilt_library_shared if the static.enabled property is set to false across all variants
362// - 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 -0700363// all variants
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000364//
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000365// In all cases, cc_prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000366func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
367 prebuiltLibraryStaticBp2Build(ctx, module, true)
368 prebuiltLibrarySharedBp2Build(ctx, module)
369}
370
371func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
372 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammer54549442022-05-11 13:55:06 -0400373 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400374
375 attrs := &bazelPrebuiltLibraryStaticAttributes{
376 Static_library: prebuiltAttrs.Src,
377 Export_includes: exportedIncludes.Includes,
378 Export_system_includes: exportedIncludes.SystemIncludes,
379 }
380
381 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000382 Rule_class: "cc_prebuilt_library_static",
383 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400384 }
385
386 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000387 if fullBuild {
388 name += "_bp2build_cc_library_static"
389 }
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000390
391 tags := android.ApexAvailableTags(module)
392 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc353abd2023-03-10 20:57:38 +0000393
394 _true := true
395 alwayslinkAttrs := *attrs
396 alwayslinkAttrs.Alwayslink.SetValue(&_true)
397 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name + "_alwayslink", Tags: tags}, &alwayslinkAttrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400398}
399
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400400type bazelPrebuiltLibrarySharedAttributes struct {
401 Shared_library bazel.LabelAttribute
402}
403
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400404func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000405 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400406
407 attrs := &bazelPrebuiltLibrarySharedAttributes{
408 Shared_library: prebuiltAttrs.Src,
409 }
410
411 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000412 Rule_class: "cc_prebuilt_library_shared",
413 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 -0400414 }
415
416 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000417 tags := android.ApexAvailableTags(module)
418 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 -0400419}
420
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000421type prebuiltObjectProperties struct {
422 Srcs []string `android:"path,arch_variant"`
423}
424
425type prebuiltObjectLinker struct {
426 android.Prebuilt
427 objectLinker
428
429 properties prebuiltObjectProperties
430}
431
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000432type prebuiltLibraryBazelHandler struct {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400433 module *Module
434 library *libraryDecorator
435}
436
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000437var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400438
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000439func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
440 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
441 return
442 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400443 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400444 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
445}
446
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000447func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
448 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
449 return
450 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400451 bazelCtx := ctx.Config().BazelContext
452 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400453 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400454 ctx.ModuleErrorf(err.Error())
455 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400456 }
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000457
458 if h.module.static() {
459 if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
460 return
461 }
462 } else if h.module.Shared() {
463 if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
464 return
465 }
466 } else {
467 return
468 }
469
470 h.module.maybeUnhideFromMake()
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500471
472 h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000473}
474
475func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400476 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400477 if len(staticLibs) > 1 {
478 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000479 return false
Liz Kammer3f9e1552021-04-02 18:47:09 -0400480 }
481
482 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
483
484 // TODO(eakammer):Add stub-related flags if this library is a stub library.
485 // h.library.exportVersioningMacroIfNeeded(ctx)
486
487 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
488 // validation will fail. For now, set this to an empty list.
489 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
490 h.library.collectedSnapshotHeaders = android.Paths{}
491
492 if len(staticLibs) == 0 {
493 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000494 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400495 }
496
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500497 var outputPath android.Path = android.PathForBazelOut(ctx, staticLibs[0])
498 if len(ccInfo.TidyFiles) > 0 {
499 h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
500 outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
501 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400502
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500503 h.module.outputFile = android.OptionalPathForPath(outputPath)
504
505 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputPath).Build()
Liz Kammer3f9e1552021-04-02 18:47:09 -0400506 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500507 StaticLibrary: outputPath,
Liz Kammer3f9e1552021-04-02 18:47:09 -0400508 TransitiveStaticLibrariesForOrdering: depSet,
509 })
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000510
511 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400512}
513
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000514func (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 -0400515 sharedLibs := ccInfo.CcSharedLibraryFiles
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000516 if len(sharedLibs) > 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400517 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000518 return false
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400519 }
520
521 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
522
523 // TODO(eakammer):Add stub-related flags if this library is a stub library.
524 // h.library.exportVersioningMacroIfNeeded(ctx)
525
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400526 if len(sharedLibs) == 0 {
527 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000528 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400529 }
530
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500531 var outputPath android.Path = android.PathForBazelOut(ctx, sharedLibs[0])
532 if len(ccInfo.TidyFiles) > 0 {
533 h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
534 outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
535 }
536
537 h.module.outputFile = android.OptionalPathForPath(outputPath)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400538
539 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500540 h.library.unstrippedOutputFile = outputPath
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400541
542 var toc android.Path
543 if len(ccInfo.TocFile) > 0 {
544 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
545 } else {
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500546 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 -0400547 }
548
549 info := SharedLibraryInfo{
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500550 SharedLibrary: outputPath,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400551 TableOfContents: android.OptionalPathForPath(toc),
552 Target: ctx.Target(),
553 }
554 ctx.SetProvider(SharedLibraryInfoProvider, info)
555
556 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
557 h.module.maybeUnhideFromMake()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000558 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400559}
560
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000561func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
562 return &p.Prebuilt
563}
564
565var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
566
567func (p *prebuiltObjectLinker) link(ctx ModuleContext,
568 flags Flags, deps PathDeps, objs Objects) android.Path {
569 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700570 // Copy objects to a name matching the final installed name
571 in := p.Prebuilt.SingleSourcePath(ctx)
572 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
573 ctx.Build(pctx, android.BuildParams{
574 Rule: android.CpExecutable,
575 Description: "prebuilt",
576 Output: outputFile,
577 Input: in,
578 })
579 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000580 }
581 return nil
582}
583
Inseob Kim1042d292020-06-01 23:23:05 +0900584func (p *prebuiltObjectLinker) object() bool {
585 return true
586}
587
Colin Cross7cabd422021-06-25 14:21:04 -0700588func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
589 module := newObject(hod)
Colin Crossc5075e92022-12-05 16:46:39 -0800590 module.bazelHandler = &prebuiltObjectBazelHandler{module: module}
591 module.bazelable = true
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000592 prebuilt := &prebuiltObjectLinker{
593 objectLinker: objectLinker{
594 baseLinker: NewBaseLinker(nil),
595 },
596 }
597 module.linker = prebuilt
598 module.AddProperties(&prebuilt.properties)
599 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000600 return module
601}
602
Colin Crossc5075e92022-12-05 16:46:39 -0800603type prebuiltObjectBazelHandler struct {
604 module *Module
605}
606
607var _ BazelHandler = (*prebuiltObjectBazelHandler)(nil)
608
609func (h *prebuiltObjectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
610 bazelCtx := ctx.Config().BazelContext
611 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
612}
613
614func (h *prebuiltObjectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
615 bazelCtx := ctx.Config().BazelContext
616 outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
617 if err != nil {
618 ctx.ModuleErrorf(err.Error())
619 return
620 }
621 if len(outputs) != 1 {
622 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
623 return
624 }
625 out := android.PathForBazelOut(ctx, outputs[0])
626 h.module.outputFile = android.OptionalPathForPath(out)
627 h.module.maybeUnhideFromMake()
628}
629
630type bazelPrebuiltObjectAttributes struct {
631 Src bazel.LabelAttribute
632}
633
634func prebuiltObjectBp2Build(ctx android.TopDownMutatorContext, module *Module) {
635 prebuiltAttrs := bp2BuildParsePrebuiltObjectProps(ctx, module)
636
637 attrs := &bazelPrebuiltObjectAttributes{
638 Src: prebuiltAttrs.Src,
639 }
640
641 props := bazel.BazelTargetModuleProperties{
642 Rule_class: "cc_prebuilt_object",
643 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_object.bzl",
644 }
645
646 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
647 tags := android.ApexAvailableTags(module)
648 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
649}
650
651func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700652 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000653 return module.Init()
654}
655
Colin Crossde89fb82017-03-17 13:28:06 -0700656type prebuiltBinaryLinker struct {
657 *binaryDecorator
658 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100659
660 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700661}
662
663var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
664
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100665func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
666 return p.toolPath
667}
668
Colin Crossde89fb82017-03-17 13:28:06 -0700669func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
670 flags Flags, deps PathDeps, objs Objects) android.Path {
671 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700672 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700673 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
674 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100675 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700676 p.unstrippedOutputFile = in
677
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100678 if ctx.Host() {
679 // Host binaries are symlinked to their prebuilt source locations. That
680 // way they are executed directly from there so the linker resolves their
681 // shared library dependencies relative to that location (using
682 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
683 // repository can supply the expected versions of the shared libraries
684 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700685
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100686 // These shared lib paths may point to copies of the libs in
687 // .intermediates, which isn't where the binary will load them from, but
688 // it's fine for dependency tracking. If a library dependency is updated,
689 // the symlink will get a new timestamp, along with any installed symlinks
690 // handled in make.
691 sharedLibPaths := deps.EarlySharedLibs
692 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
693 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
694
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100695 var fromPath = in.String()
696 if !filepath.IsAbs(fromPath) {
697 fromPath = "$$PWD/" + fromPath
698 }
699
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100700 ctx.Build(pctx, android.BuildParams{
701 Rule: android.Symlink,
702 Output: outputFile,
703 Input: in,
704 Implicits: sharedLibPaths,
705 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100706 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100707 },
708 })
709
710 p.toolPath = android.OptionalPathForPath(outputFile)
711 } else {
712 if p.stripper.NeedsStrip(ctx) {
713 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
714 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
715 in = stripped
716 }
717
718 // Copy binaries to a name matching the final installed name
719 ctx.Build(pctx, android.BuildParams{
720 Rule: android.CpExecutable,
721 Description: "prebuilt",
722 Output: outputFile,
723 Input: in,
724 })
725 }
Colin Cross94921e72017-08-08 16:20:15 -0700726
727 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700728 }
729
730 return nil
731}
732
Inseob Kim7f283f42020-06-01 21:53:49 +0900733func (p *prebuiltBinaryLinker) binary() bool {
734 return true
735}
736
Patrice Arruda3554a982019-03-27 19:09:10 -0700737// 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 +0000738// device's directory, for both the host and device
739func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700740 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
741 return module.Init()
742}
743
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000744type prebuiltBinaryBazelHandler struct {
745 module *Module
746 decorator *binaryDecorator
747}
748
Leo Li74f7b972017-05-17 11:30:45 -0700749func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000750 module, binary := newBinary(hod, true)
Colin Crossde89fb82017-03-17 13:28:06 -0700751 module.compiler = nil
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000752 module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary}
Colin Crossde89fb82017-03-17 13:28:06 -0700753
754 prebuilt := &prebuiltBinaryLinker{
755 binaryDecorator: binary,
756 }
757 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100758 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700759
Colin Cross74d73e22017-08-02 11:05:49 -0700760 module.AddProperties(&prebuilt.properties)
761
762 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700763 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700764}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700765
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000766var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil)
767
768func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
769 bazelCtx := ctx.Config().BazelContext
Yu Liue4312402023-01-18 09:15:31 -0800770 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx)))
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000771}
772
773func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
774 bazelCtx := ctx.Config().BazelContext
Yu Liue4312402023-01-18 09:15:31 -0800775 outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx)))
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000776 if err != nil {
777 ctx.ModuleErrorf(err.Error())
778 return
779 }
780 if len(outputs) != 1 {
781 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
782 return
783 }
784 out := android.PathForBazelOut(ctx, outputs[0])
785 h.module.outputFile = android.OptionalPathForPath(out)
786 h.module.maybeUnhideFromMake()
787}
788
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000789type bazelPrebuiltBinaryAttributes struct {
790 Src bazel.LabelAttribute
791 Strip stripAttributes
792}
793
794func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
795 prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module)
796
797 var la linkerAttributes
798 la.convertStripProps(ctx, module)
799 attrs := &bazelPrebuiltBinaryAttributes{
800 Src: prebuiltAttrs.Src,
801 Strip: stripAttrsFromLinkerAttrs(&la),
802 }
803
804 props := bazel.BazelTargetModuleProperties{
805 Rule_class: "cc_prebuilt_binary",
806 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl",
807 }
808
809 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000810 tags := android.ApexAvailableTags(module)
811 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 +0000812}
813
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700814type Sanitized struct {
815 None struct {
816 Srcs []string `android:"path,arch_variant"`
817 } `android:"arch_variant"`
818 Address struct {
819 Srcs []string `android:"path,arch_variant"`
820 } `android:"arch_variant"`
821 Hwaddress struct {
822 Srcs []string `android:"path,arch_variant"`
823 } `android:"arch_variant"`
824}
825
826func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
827 if sanitize == nil {
828 return nil
829 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400830 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700831 return sanitized.Address.Srcs
832 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400833 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700834 return sanitized.Hwaddress.Srcs
835 }
836 return sanitized.None.Srcs
837}