blob: e2cd3ad73e599c9e84d4809b994ffe703095fff6 [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"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000019
20 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -040021 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040022 "android/soong/bazel/cquery"
Colin Crossce75d2c2016-10-06 16:12:58 -070023)
24
25func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000026 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
27}
28
29func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000030 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000031 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
32 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040033 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Colin Crossc5075e92022-12-05 16:46:39 -080034 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000035 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070036}
37
38type prebuiltLinkerInterface interface {
39 Name(string) string
40 prebuilt() *android.Prebuilt
41}
42
Patrice Arruda3554a982019-03-27 19:09:10 -070043type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070044 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
45 Srcs []string `android:"path,arch_variant"`
46
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070047 Sanitized Sanitized `android:"arch_variant"`
48
Patrice Arruda3554a982019-03-27 19:09:10 -070049 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
50 // symbols, etc), default true.
51 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080052
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010053 // if set, add an extra objcopy --prefix-symbols= step
54 Prefix_symbols *string
55
Yo Chianga3ad9b22020-03-18 14:19:07 +080056 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
57 // This is needed only if this library is linked by other modules in build time.
58 // Only makes sense for the Windows target.
59 Windows_import_lib *string `android:"path,arch_variant"`
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +000060
61 // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either
62 // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with
63 // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this
64 // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see
65 // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled.
66 MixedBuildsDisabled bool `blueprint:"mutated"`
Patrice Arruda3554a982019-03-27 19:09:10 -070067}
68
Colin Crossde89fb82017-03-17 13:28:06 -070069type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070070 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080071
Patrice Arruda3554a982019-03-27 19:09:10 -070072 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070073}
74
Colin Crossde89fb82017-03-17 13:28:06 -070075func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070076 return &p.Prebuilt
77}
78
Colin Cross74d73e22017-08-02 11:05:49 -070079func (p *prebuiltLinker) PrebuiltSrcs() []string {
80 return p.properties.Srcs
81}
82
Colin Cross33b2fb72019-05-14 14:07:01 -070083type prebuiltLibraryInterface interface {
84 libraryInterface
85 prebuiltLinkerInterface
86 disablePrebuilt()
87}
88
Colin Crossde89fb82017-03-17 13:28:06 -070089type prebuiltLibraryLinker struct {
90 *libraryDecorator
91 prebuiltLinker
92}
93
94var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070095var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070096
Yi Kong00981662018-08-13 16:02:49 -070097func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
98
99func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +0800100 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -0700101}
102
103func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -0700104 return flags
Yi Kong00981662018-08-13 16:02:49 -0700105}
106
107func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
108 return p.libraryDecorator.linkerProps()
109}
110
Colin Crossce75d2c2016-10-06 16:12:58 -0700111func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700112 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000113
Colin Cross0de8a1e2020-09-18 14:15:30 -0700114 p.libraryDecorator.flagExporter.exportIncludes(ctx)
115 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
116 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
117 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
118 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
119 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
120
121 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000122
Colin Crossce75d2c2016-10-06 16:12:58 -0700123 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700124 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000125 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000126 if len(srcs) > 1 {
127 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
128 return nil
129 }
130
Jiyong Park892a98f2020-12-14 09:20:00 +0900131 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
132
Paul Duffinbce90da2020-03-12 20:17:14 +0000133 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700134
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100135 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
136 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
137 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
138 in, flagsToBuilderFlags(flags), prefixed)
139 in = prefixed
140 }
141
Yo Chianga3ad9b22020-03-18 14:19:07 +0800142 if p.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700143 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700144 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
145 StaticLibrary: in,
146
147 TransitiveStaticLibrariesForOrdering: depSet,
148 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800149 return in
150 }
151
Colin Cross88f6fef2018-09-05 14:20:03 -0700152 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700153 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700154 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800155 outputFile := android.PathForModuleOut(ctx, libName)
156 var implicits android.Paths
157
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200158 if p.stripper.NeedsStrip(ctx) {
159 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700160 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200161 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700162 in = stripped
163 }
164
Colin Crossb60190a2018-09-04 16:28:17 -0700165 // Optimize out relinking against shared libraries whose interface hasn't changed by
166 // depending on a table of contents file instead of the library itself.
167 tocFile := android.PathForModuleOut(ctx, libName+".toc")
168 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400169 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700170
Yo Chianga3ad9b22020-03-18 14:19:07 +0800171 if ctx.Windows() && p.properties.Windows_import_lib != nil {
172 // Consumers of this library actually links to the import library in build
173 // time and dynamically links to the DLL in run time. i.e.
174 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
175 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
176 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
177 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
178 implicits = append(implicits, importLibOutputFile)
179
180 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400181 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800182 Description: "prebuilt import library",
183 Input: importLibSrc,
184 Output: importLibOutputFile,
185 Args: map[string]string{
186 "cpFlags": "-L",
187 },
188 })
189 }
190
191 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400192 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800193 Description: "prebuilt shared library",
194 Implicits: implicits,
195 Input: in,
196 Output: outputFile,
197 Args: map[string]string{
198 "cpFlags": "-L",
199 },
200 })
201
Colin Cross0de8a1e2020-09-18 14:15:30 -0700202 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400203 SharedLibrary: outputFile,
204 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700205
206 TableOfContents: p.tocFile,
207 })
208
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000209 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
Jooyung Hane5f063e2023-03-23 14:31:19 +0900210 // library as their source and must not be installed, but other prebuilts like
211 // libclang_rt.* libraries set `stubs` property because they are LLNDK libraries,
212 // but use an implementation library as their source and need to be installed.
213 // This discrepancy should be resolved without the prefix hack below.
214 isModuleSdkPrebuilts := android.HasAnyPrefix(ctx.ModuleDir(), []string{
215 "prebuilts/runtime/mainline/", "prebuilts/module_sdk/"})
216 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() && isModuleSdkPrebuilts {
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,
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc641cc42023-03-16 17:03:43 +0000379 // TODO: ¿Alwayslink?
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400380 }
381
382 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000383 Rule_class: "cc_prebuilt_library_static",
384 Bzl_load_location: "//build/bazel/rules/cc: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 }
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000391
Spandan Das39b6cc52023-04-12 19:05:49 +0000392 tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000393 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 +0000394
395 _true := true
396 alwayslinkAttrs := *attrs
397 alwayslinkAttrs.Alwayslink.SetValue(&_true)
398 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name + "_alwayslink", Tags: tags}, &alwayslinkAttrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400399}
400
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400401type bazelPrebuiltLibrarySharedAttributes struct {
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc641cc42023-03-16 17:03:43 +0000402 Shared_library bazel.LabelAttribute
403 Export_includes bazel.StringListAttribute
404 Export_system_includes bazel.StringListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400405}
406
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400407func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000408 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc641cc42023-03-16 17:03:43 +0000409 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400410
411 attrs := &bazelPrebuiltLibrarySharedAttributes{
Alex Márquez Pérez Muñíz Díaz Puras Thaureauxc641cc42023-03-16 17:03:43 +0000412 Shared_library: prebuiltAttrs.Src,
413 Export_includes: exportedIncludes.Includes,
414 Export_system_includes: exportedIncludes.SystemIncludes,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400415 }
416
417 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000418 Rule_class: "cc_prebuilt_library_shared",
419 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 -0400420 }
421
422 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Spandan Das39b6cc52023-04-12 19:05:49 +0000423 tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000424 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 -0400425}
426
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000427type prebuiltObjectProperties struct {
428 Srcs []string `android:"path,arch_variant"`
429}
430
431type prebuiltObjectLinker struct {
432 android.Prebuilt
433 objectLinker
434
435 properties prebuiltObjectProperties
436}
437
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000438type prebuiltLibraryBazelHandler struct {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400439 module *Module
440 library *libraryDecorator
441}
442
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000443var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400444
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000445func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
446 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
447 return
448 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400449 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400450 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
451}
452
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000453func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
454 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
455 return
456 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400457 bazelCtx := ctx.Config().BazelContext
458 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400459 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400460 ctx.ModuleErrorf(err.Error())
461 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400462 }
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000463
464 if h.module.static() {
465 if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
466 return
467 }
468 } else if h.module.Shared() {
469 if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
470 return
471 }
472 } else {
473 return
474 }
475
476 h.module.maybeUnhideFromMake()
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500477
478 h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000479}
480
481func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400482 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400483 if len(staticLibs) > 1 {
484 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000485 return false
Liz Kammer3f9e1552021-04-02 18:47:09 -0400486 }
487
488 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
489
490 // TODO(eakammer):Add stub-related flags if this library is a stub library.
491 // h.library.exportVersioningMacroIfNeeded(ctx)
492
493 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
494 // validation will fail. For now, set this to an empty list.
495 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
496 h.library.collectedSnapshotHeaders = android.Paths{}
497
498 if len(staticLibs) == 0 {
499 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000500 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400501 }
502
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500503 var outputPath android.Path = android.PathForBazelOut(ctx, staticLibs[0])
504 if len(ccInfo.TidyFiles) > 0 {
505 h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
506 outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
507 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400508
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500509 h.module.outputFile = android.OptionalPathForPath(outputPath)
510
Colin Crossc85750b2022-04-21 12:50:51 -0700511 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputPath).Build()
Liz Kammer3f9e1552021-04-02 18:47:09 -0400512 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500513 StaticLibrary: outputPath,
Liz Kammer3f9e1552021-04-02 18:47:09 -0400514 TransitiveStaticLibrariesForOrdering: depSet,
515 })
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000516
517 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400518}
519
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000520func (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 -0400521 sharedLibs := ccInfo.CcSharedLibraryFiles
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000522 if len(sharedLibs) > 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400523 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000524 return false
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400525 }
526
527 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
528
529 // TODO(eakammer):Add stub-related flags if this library is a stub library.
530 // h.library.exportVersioningMacroIfNeeded(ctx)
531
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400532 if len(sharedLibs) == 0 {
533 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000534 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400535 }
536
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500537 var outputPath android.Path = android.PathForBazelOut(ctx, sharedLibs[0])
538 if len(ccInfo.TidyFiles) > 0 {
539 h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles)
540 outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles)
541 }
542
543 h.module.outputFile = android.OptionalPathForPath(outputPath)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400544
545 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500546 h.library.unstrippedOutputFile = outputPath
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400547
548 var toc android.Path
549 if len(ccInfo.TocFile) > 0 {
550 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
551 } else {
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500552 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 -0400553 }
554
555 info := SharedLibraryInfo{
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500556 SharedLibrary: outputPath,
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400557 TableOfContents: android.OptionalPathForPath(toc),
558 Target: ctx.Target(),
559 }
560 ctx.SetProvider(SharedLibraryInfoProvider, info)
561
562 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
563 h.module.maybeUnhideFromMake()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000564 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400565}
566
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000567func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
568 return &p.Prebuilt
569}
570
571var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
572
573func (p *prebuiltObjectLinker) link(ctx ModuleContext,
574 flags Flags, deps PathDeps, objs Objects) android.Path {
575 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700576 // Copy objects to a name matching the final installed name
577 in := p.Prebuilt.SingleSourcePath(ctx)
578 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
579 ctx.Build(pctx, android.BuildParams{
580 Rule: android.CpExecutable,
581 Description: "prebuilt",
582 Output: outputFile,
583 Input: in,
584 })
585 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000586 }
587 return nil
588}
589
Inseob Kim1042d292020-06-01 23:23:05 +0900590func (p *prebuiltObjectLinker) object() bool {
591 return true
592}
593
Colin Cross7cabd422021-06-25 14:21:04 -0700594func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
595 module := newObject(hod)
Colin Crossc5075e92022-12-05 16:46:39 -0800596 module.bazelHandler = &prebuiltObjectBazelHandler{module: module}
597 module.bazelable = true
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000598 prebuilt := &prebuiltObjectLinker{
599 objectLinker: objectLinker{
600 baseLinker: NewBaseLinker(nil),
601 },
602 }
603 module.linker = prebuilt
604 module.AddProperties(&prebuilt.properties)
605 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000606 return module
607}
608
Colin Crossc5075e92022-12-05 16:46:39 -0800609type prebuiltObjectBazelHandler struct {
610 module *Module
611}
612
613var _ BazelHandler = (*prebuiltObjectBazelHandler)(nil)
614
615func (h *prebuiltObjectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
616 bazelCtx := ctx.Config().BazelContext
617 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
618}
619
620func (h *prebuiltObjectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
621 bazelCtx := ctx.Config().BazelContext
622 outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
623 if err != nil {
624 ctx.ModuleErrorf(err.Error())
625 return
626 }
627 if len(outputs) != 1 {
628 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
629 return
630 }
631 out := android.PathForBazelOut(ctx, outputs[0])
632 h.module.outputFile = android.OptionalPathForPath(out)
633 h.module.maybeUnhideFromMake()
634}
635
636type bazelPrebuiltObjectAttributes struct {
637 Src bazel.LabelAttribute
638}
639
640func prebuiltObjectBp2Build(ctx android.TopDownMutatorContext, module *Module) {
641 prebuiltAttrs := bp2BuildParsePrebuiltObjectProps(ctx, module)
642
643 attrs := &bazelPrebuiltObjectAttributes{
644 Src: prebuiltAttrs.Src,
645 }
646
647 props := bazel.BazelTargetModuleProperties{
648 Rule_class: "cc_prebuilt_object",
649 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_object.bzl",
650 }
651
652 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Spandan Das39b6cc52023-04-12 19:05:49 +0000653 tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
Colin Crossc5075e92022-12-05 16:46:39 -0800654 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs)
655}
656
657func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700658 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000659 return module.Init()
660}
661
Colin Crossde89fb82017-03-17 13:28:06 -0700662type prebuiltBinaryLinker struct {
663 *binaryDecorator
664 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100665
666 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700667}
668
669var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
670
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100671func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
672 return p.toolPath
673}
674
Colin Crossde89fb82017-03-17 13:28:06 -0700675func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
676 flags Flags, deps PathDeps, objs Objects) android.Path {
677 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700678 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700679 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
680 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100681 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700682 p.unstrippedOutputFile = in
683
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100684 if ctx.Host() {
685 // Host binaries are symlinked to their prebuilt source locations. That
686 // way they are executed directly from there so the linker resolves their
687 // shared library dependencies relative to that location (using
688 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
689 // repository can supply the expected versions of the shared libraries
690 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700691
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100692 // These shared lib paths may point to copies of the libs in
693 // .intermediates, which isn't where the binary will load them from, but
694 // it's fine for dependency tracking. If a library dependency is updated,
695 // the symlink will get a new timestamp, along with any installed symlinks
696 // handled in make.
697 sharedLibPaths := deps.EarlySharedLibs
698 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
699 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
700
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100701 var fromPath = in.String()
702 if !filepath.IsAbs(fromPath) {
703 fromPath = "$$PWD/" + fromPath
704 }
705
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100706 ctx.Build(pctx, android.BuildParams{
707 Rule: android.Symlink,
708 Output: outputFile,
709 Input: in,
710 Implicits: sharedLibPaths,
711 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100712 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100713 },
714 })
715
716 p.toolPath = android.OptionalPathForPath(outputFile)
717 } else {
718 if p.stripper.NeedsStrip(ctx) {
719 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
720 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
721 in = stripped
722 }
723
724 // Copy binaries to a name matching the final installed name
725 ctx.Build(pctx, android.BuildParams{
726 Rule: android.CpExecutable,
727 Description: "prebuilt",
728 Output: outputFile,
729 Input: in,
730 })
731 }
Colin Cross94921e72017-08-08 16:20:15 -0700732
733 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700734 }
735
736 return nil
737}
738
Inseob Kim7f283f42020-06-01 21:53:49 +0900739func (p *prebuiltBinaryLinker) binary() bool {
740 return true
741}
742
Patrice Arruda3554a982019-03-27 19:09:10 -0700743// 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 +0000744// device's directory, for both the host and device
745func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700746 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
747 return module.Init()
748}
749
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000750type prebuiltBinaryBazelHandler struct {
751 module *Module
752 decorator *binaryDecorator
753}
754
Leo Li74f7b972017-05-17 11:30:45 -0700755func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000756 module, binary := newBinary(hod, true)
Colin Crossde89fb82017-03-17 13:28:06 -0700757 module.compiler = nil
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000758 module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary}
Colin Crossde89fb82017-03-17 13:28:06 -0700759
760 prebuilt := &prebuiltBinaryLinker{
761 binaryDecorator: binary,
762 }
763 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100764 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700765
Colin Cross74d73e22017-08-02 11:05:49 -0700766 module.AddProperties(&prebuilt.properties)
767
768 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700769 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700770}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700771
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000772var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil)
773
774func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
775 bazelCtx := ctx.Config().BazelContext
Yu Liue4312402023-01-18 09:15:31 -0800776 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 +0000777}
778
779func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
780 bazelCtx := ctx.Config().BazelContext
Yu Liue4312402023-01-18 09:15:31 -0800781 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 +0000782 if err != nil {
783 ctx.ModuleErrorf(err.Error())
784 return
785 }
786 if len(outputs) != 1 {
787 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
788 return
789 }
790 out := android.PathForBazelOut(ctx, outputs[0])
791 h.module.outputFile = android.OptionalPathForPath(out)
792 h.module.maybeUnhideFromMake()
793}
794
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000795type bazelPrebuiltBinaryAttributes struct {
796 Src bazel.LabelAttribute
797 Strip stripAttributes
798}
799
800func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
801 prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module)
802
803 var la linkerAttributes
804 la.convertStripProps(ctx, module)
805 attrs := &bazelPrebuiltBinaryAttributes{
806 Src: prebuiltAttrs.Src,
807 Strip: stripAttrsFromLinkerAttrs(&la),
808 }
809
810 props := bazel.BazelTargetModuleProperties{
811 Rule_class: "cc_prebuilt_binary",
812 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl",
813 }
814
815 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Spandan Das39b6cc52023-04-12 19:05:49 +0000816 tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module)
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000817 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 +0000818}
819
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700820type Sanitized struct {
821 None struct {
822 Srcs []string `android:"path,arch_variant"`
823 } `android:"arch_variant"`
824 Address struct {
825 Srcs []string `android:"path,arch_variant"`
826 } `android:"arch_variant"`
827 Hwaddress struct {
828 Srcs []string `android:"path,arch_variant"`
829 } `android:"arch_variant"`
830}
831
832func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
833 if sanitize == nil {
834 return nil
835 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400836 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700837 return sanitized.Address.Srcs
838 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400839 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700840 return sanitized.Hwaddress.Srcs
841 }
842 return sanitized.None.Srcs
843}