blob: 273005235c59f0b26901ebba355802186a04f7b5 [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// Copyright 2016 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Martin Stjernholm14ee8322020-09-21 21:45:49 +010018 "path/filepath"
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +000019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
21 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -040022 "android/soong/bazel"
Chris Parsonsf874e462022-05-10 13:50:12 -040023 "android/soong/bazel/cquery"
Colin Crossce75d2c2016-10-06 16:12:58 -070024)
25
26func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000027 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
28}
29
30func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000031 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000032 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
33 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040034 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000035 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000036 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
39type prebuiltLinkerInterface interface {
40 Name(string) string
41 prebuilt() *android.Prebuilt
42}
43
Patrice Arruda3554a982019-03-27 19:09:10 -070044type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070045 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
46 Srcs []string `android:"path,arch_variant"`
47
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070048 Sanitized Sanitized `android:"arch_variant"`
49
Patrice Arruda3554a982019-03-27 19:09:10 -070050 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
51 // symbols, etc), default true.
52 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080053
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010054 // if set, add an extra objcopy --prefix-symbols= step
55 Prefix_symbols *string
56
Yo Chianga3ad9b22020-03-18 14:19:07 +080057 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
58 // This is needed only if this library is linked by other modules in build time.
59 // Only makes sense for the Windows target.
60 Windows_import_lib *string `android:"path,arch_variant"`
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +000061
62 // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either
63 // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with
64 // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this
65 // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see
66 // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled.
67 MixedBuildsDisabled bool `blueprint:"mutated"`
Patrice Arruda3554a982019-03-27 19:09:10 -070068}
69
Colin Crossde89fb82017-03-17 13:28:06 -070070type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070071 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080072
Patrice Arruda3554a982019-03-27 19:09:10 -070073 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070074}
75
Colin Crossde89fb82017-03-17 13:28:06 -070076func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070077 return &p.Prebuilt
78}
79
Colin Cross74d73e22017-08-02 11:05:49 -070080func (p *prebuiltLinker) PrebuiltSrcs() []string {
81 return p.properties.Srcs
82}
83
Colin Cross33b2fb72019-05-14 14:07:01 -070084type prebuiltLibraryInterface interface {
85 libraryInterface
86 prebuiltLinkerInterface
87 disablePrebuilt()
88}
89
Colin Crossde89fb82017-03-17 13:28:06 -070090type prebuiltLibraryLinker struct {
91 *libraryDecorator
92 prebuiltLinker
93}
94
95var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070096var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070097
Yi Kong00981662018-08-13 16:02:49 -070098func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
99
100func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +0800101 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -0700102}
103
104func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -0700105 return flags
Yi Kong00981662018-08-13 16:02:49 -0700106}
107
108func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
109 return p.libraryDecorator.linkerProps()
110}
111
Colin Crossce75d2c2016-10-06 16:12:58 -0700112func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700113 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000114
Colin Cross0de8a1e2020-09-18 14:15:30 -0700115 p.libraryDecorator.flagExporter.exportIncludes(ctx)
116 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
117 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
118 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
119 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
120 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
121
122 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000123
Colin Crossce75d2c2016-10-06 16:12:58 -0700124 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700125 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000126 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000127 if len(srcs) > 1 {
128 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
129 return nil
130 }
131
Jiyong Park892a98f2020-12-14 09:20:00 +0900132 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
133
Paul Duffinbce90da2020-03-12 20:17:14 +0000134 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700135
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100136 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
137 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
138 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
139 in, flagsToBuilderFlags(flags), prefixed)
140 in = prefixed
141 }
142
Yo Chianga3ad9b22020-03-18 14:19:07 +0800143 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700144 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
145 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
146 StaticLibrary: in,
147
148 TransitiveStaticLibrariesForOrdering: depSet,
149 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800150 return in
151 }
152
Colin Cross88f6fef2018-09-05 14:20:03 -0700153 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700154 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700155 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800156 outputFile := android.PathForModuleOut(ctx, libName)
157 var implicits android.Paths
158
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200159 if p.stripper.NeedsStrip(ctx) {
160 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700161 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200162 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700163 in = stripped
164 }
165
Colin Crossb60190a2018-09-04 16:28:17 -0700166 // Optimize out relinking against shared libraries whose interface hasn't changed by
167 // depending on a table of contents file instead of the library itself.
168 tocFile := android.PathForModuleOut(ctx, libName+".toc")
169 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400170 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700171
Yo Chianga3ad9b22020-03-18 14:19:07 +0800172 if ctx.Windows() && p.properties.Windows_import_lib != nil {
173 // Consumers of this library actually links to the import library in build
174 // time and dynamically links to the DLL in run time. i.e.
175 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
176 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
177 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
178 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
179 implicits = append(implicits, importLibOutputFile)
180
181 ctx.Build(pctx, android.BuildParams{
182 Rule: android.Cp,
183 Description: "prebuilt import library",
184 Input: importLibSrc,
185 Output: importLibOutputFile,
186 Args: map[string]string{
187 "cpFlags": "-L",
188 },
189 })
190 }
191
192 ctx.Build(pctx, android.BuildParams{
193 Rule: android.Cp,
194 Description: "prebuilt shared library",
195 Implicits: implicits,
196 Input: in,
197 Output: outputFile,
198 Args: map[string]string{
199 "cpFlags": "-L",
200 },
201 })
202
Colin Cross0de8a1e2020-09-18 14:15:30 -0700203 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400204 SharedLibrary: outputFile,
205 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700206
207 TableOfContents: p.tocFile,
208 })
209
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000210 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
211 // library as their source and must not be installed, but libclang_rt.* libraries
212 // have stubs because they are LLNDK libraries, but use an implementation library
213 // as their source and need to be installed. This discrepancy should be resolved
214 // without the prefix hack below.
215 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() &&
216 !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") {
217 ctx.Module().MakeUninstallable()
218 }
219
Yo Chianga3ad9b22020-03-18 14:19:07 +0800220 return outputFile
221 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700222 }
223
Colin Cross649d8172020-12-10 12:30:21 -0800224 if p.header() {
225 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
226
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000227 // Need to return an output path so that the AndroidMk logic doesn't skip
228 // the prebuilt header. For compatibility, in case Android.mk files use a
229 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
230 // placeholder, just like non-prebuilt header modules do in linkStatic().
231 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
232 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
233 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800234 }
235
Colin Crossce75d2c2016-10-06 16:12:58 -0700236 return nil
237}
238
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700239func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
240 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000241 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700242 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000243 if p.static() {
244 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700245 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000246 }
247 if p.shared() {
248 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700249 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000250 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000251 return srcs
252}
253
Jiyong Park379de2f2018-12-19 02:47:14 +0900254func (p *prebuiltLibraryLinker) shared() bool {
255 return p.libraryDecorator.shared()
256}
257
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700258func (p *prebuiltLibraryLinker) nativeCoverage() bool {
259 return false
260}
261
Colin Cross33b2fb72019-05-14 14:07:01 -0700262func (p *prebuiltLibraryLinker) disablePrebuilt() {
263 p.properties.Srcs = nil
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000264 p.properties.MixedBuildsDisabled = true
Colin Cross33b2fb72019-05-14 14:07:01 -0700265}
266
Jiyong Park892a98f2020-12-14 09:20:00 +0900267// Implements versionedInterface
268func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100269 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900270}
271
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000272func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700273 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700274 module.compiler = nil
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000275 module.bazelable = true
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000276 module.bazelHandler = &prebuiltLibraryBazelHandler{module: module, library: library}
Colin Crossce75d2c2016-10-06 16:12:58 -0700277
278 prebuilt := &prebuiltLibraryLinker{
279 libraryDecorator: library,
280 }
281 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700282 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700283
Colin Cross74d73e22017-08-02 11:05:49 -0700284 module.AddProperties(&prebuilt.properties)
285
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000286 if srcsProperty == "" {
287 android.InitPrebuiltModuleWithoutSrcs(module)
288 } else {
289 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
290 return prebuilt.prebuiltSrcs(ctx)
291 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000292
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000293 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
294 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900295
Paul Duffinac6e6082019-12-11 15:22:32 +0000296 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900297 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000298 return module, library
299}
300
Paul Duffinbce90da2020-03-12 20:17:14 +0000301// cc_prebuilt_library installs a precompiled shared library that are
302// listed in the srcs property in the device's directory.
303func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000304 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000305
306 // Prebuilt shared libraries can be included in APEXes
307 android.InitApexModule(module)
308
309 return module.Init()
310}
311
Paul Duffinac6e6082019-12-11 15:22:32 +0000312// cc_prebuilt_library_shared installs a precompiled shared library that are
313// listed in the srcs property in the device's directory.
314func PrebuiltSharedLibraryFactory() android.Module {
315 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
316 return module.Init()
317}
318
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400319// cc_prebuilt_test_library_shared installs a precompiled shared library
320// to be used as a data dependency of a test-related module (such as cc_test, or
321// cc_test_library).
322func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000323 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400324 library.BuildOnlyShared()
325 library.baseInstaller = NewTestInstaller()
326 return module.Init()
327}
328
Paul Duffinac6e6082019-12-11 15:22:32 +0000329func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000330 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000331 library.BuildOnlyShared()
332
333 // Prebuilt shared libraries can be included in APEXes
334 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900335
Leo Li74f7b972017-05-17 11:30:45 -0700336 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700337}
338
Patrice Arruda3554a982019-03-27 19:09:10 -0700339// cc_prebuilt_library_static installs a precompiled static library that are
340// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900341func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700342 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
343 return module.Init()
344}
345
346func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000347 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700348 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000349
Leo Li74f7b972017-05-17 11:30:45 -0700350 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700351}
352
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400353type bazelPrebuiltLibraryStaticAttributes struct {
354 Static_library bazel.LabelAttribute
355 Export_includes bazel.StringListAttribute
356 Export_system_includes bazel.StringListAttribute
357}
358
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000359// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
360// Implements bp2build for cc_prebuilt_library modules. This will generate:
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000361// - Only a cc_prebuilt_library_static if the shared.enabled property is set to false across all variants.
362// - Only a cc_prebuilt_library_shared if the static.enabled property is set to false across all variants
363// - 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 -0700364// all variants
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000365//
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000366// In all cases, cc_prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000367func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
368 prebuiltLibraryStaticBp2Build(ctx, module, true)
369 prebuiltLibrarySharedBp2Build(ctx, module)
370}
371
372func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
373 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammer54549442022-05-11 13:55:06 -0400374 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400375
376 attrs := &bazelPrebuiltLibraryStaticAttributes{
377 Static_library: prebuiltAttrs.Src,
378 Export_includes: exportedIncludes.Includes,
379 Export_system_includes: exportedIncludes.SystemIncludes,
380 }
381
382 props := bazel.BazelTargetModuleProperties{
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
392 tags := android.ApexAvailableTags(module)
393 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400394}
395
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400396type bazelPrebuiltLibrarySharedAttributes struct {
397 Shared_library bazel.LabelAttribute
398}
399
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400400func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000401 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400402
403 attrs := &bazelPrebuiltLibrarySharedAttributes{
404 Shared_library: prebuiltAttrs.Src,
405 }
406
407 props := bazel.BazelTargetModuleProperties{
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc5184ec2022-10-17 14:48:57 +0000408 Rule_class: "cc_prebuilt_library_shared",
409 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 -0400410 }
411
412 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000413 tags := android.ApexAvailableTags(module)
414 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 -0400415}
416
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000417type prebuiltObjectProperties struct {
418 Srcs []string `android:"path,arch_variant"`
419}
420
421type prebuiltObjectLinker struct {
422 android.Prebuilt
423 objectLinker
424
425 properties prebuiltObjectProperties
426}
427
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000428type prebuiltLibraryBazelHandler struct {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400429 module *Module
430 library *libraryDecorator
431}
432
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000433var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400434
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000435func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
436 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
437 return
438 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400439 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400440 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
441}
442
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000443func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
444 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
445 return
446 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400447 bazelCtx := ctx.Config().BazelContext
448 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400449 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400450 ctx.ModuleErrorf(err.Error())
451 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400452 }
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000453
454 if h.module.static() {
455 if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
456 return
457 }
458 } else if h.module.Shared() {
459 if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
460 return
461 }
462 } else {
463 return
464 }
465
466 h.module.maybeUnhideFromMake()
467}
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
491 out := android.PathForBazelOut(ctx, staticLibs[0])
492 h.module.outputFile = android.OptionalPathForPath(out)
493
494 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
495 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
496 StaticLibrary: out,
497
498 TransitiveStaticLibrariesForOrdering: depSet,
499 })
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000500
501 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400502}
503
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000504func (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 -0400505 sharedLibs := ccInfo.CcSharedLibraryFiles
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000506 if len(sharedLibs) > 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400507 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000508 return false
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400509 }
510
511 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
512
513 // TODO(eakammer):Add stub-related flags if this library is a stub library.
514 // h.library.exportVersioningMacroIfNeeded(ctx)
515
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400516 if len(sharedLibs) == 0 {
517 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000518 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400519 }
520
521 out := android.PathForBazelOut(ctx, sharedLibs[0])
522 h.module.outputFile = android.OptionalPathForPath(out)
523
524 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
525 h.library.unstrippedOutputFile = out
526
527 var toc android.Path
528 if len(ccInfo.TocFile) > 0 {
529 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
530 } else {
531 toc = out // Just reuse `out` so ninja still gets an input but won't matter
532 }
533
534 info := SharedLibraryInfo{
535 SharedLibrary: out,
536 TableOfContents: android.OptionalPathForPath(toc),
537 Target: ctx.Target(),
538 }
539 ctx.SetProvider(SharedLibraryInfoProvider, info)
540
541 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
542 h.module.maybeUnhideFromMake()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000543 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400544}
545
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000546func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
547 return &p.Prebuilt
548}
549
550var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
551
552func (p *prebuiltObjectLinker) link(ctx ModuleContext,
553 flags Flags, deps PathDeps, objs Objects) android.Path {
554 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700555 // Copy objects to a name matching the final installed name
556 in := p.Prebuilt.SingleSourcePath(ctx)
557 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
558 ctx.Build(pctx, android.BuildParams{
559 Rule: android.CpExecutable,
560 Description: "prebuilt",
561 Output: outputFile,
562 Input: in,
563 })
564 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000565 }
566 return nil
567}
568
Inseob Kim1042d292020-06-01 23:23:05 +0900569func (p *prebuiltObjectLinker) object() bool {
570 return true
571}
572
Colin Cross7cabd422021-06-25 14:21:04 -0700573func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
574 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000575 prebuilt := &prebuiltObjectLinker{
576 objectLinker: objectLinker{
577 baseLinker: NewBaseLinker(nil),
578 },
579 }
580 module.linker = prebuilt
581 module.AddProperties(&prebuilt.properties)
582 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
583 android.InitSdkAwareModule(module)
584 return module
585}
586
587func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700588 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000589 return module.Init()
590}
591
Colin Crossde89fb82017-03-17 13:28:06 -0700592type prebuiltBinaryLinker struct {
593 *binaryDecorator
594 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100595
596 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700597}
598
599var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
600
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100601func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
602 return p.toolPath
603}
604
Colin Crossde89fb82017-03-17 13:28:06 -0700605func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
606 flags Flags, deps PathDeps, objs Objects) android.Path {
607 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700608 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700609 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
610 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100611 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700612 p.unstrippedOutputFile = in
613
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100614 if ctx.Host() {
615 // Host binaries are symlinked to their prebuilt source locations. That
616 // way they are executed directly from there so the linker resolves their
617 // shared library dependencies relative to that location (using
618 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
619 // repository can supply the expected versions of the shared libraries
620 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700621
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100622 // These shared lib paths may point to copies of the libs in
623 // .intermediates, which isn't where the binary will load them from, but
624 // it's fine for dependency tracking. If a library dependency is updated,
625 // the symlink will get a new timestamp, along with any installed symlinks
626 // handled in make.
627 sharedLibPaths := deps.EarlySharedLibs
628 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
629 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
630
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100631 var fromPath = in.String()
632 if !filepath.IsAbs(fromPath) {
633 fromPath = "$$PWD/" + fromPath
634 }
635
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100636 ctx.Build(pctx, android.BuildParams{
637 Rule: android.Symlink,
638 Output: outputFile,
639 Input: in,
640 Implicits: sharedLibPaths,
641 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100642 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100643 },
644 })
645
646 p.toolPath = android.OptionalPathForPath(outputFile)
647 } else {
648 if p.stripper.NeedsStrip(ctx) {
649 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
650 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
651 in = stripped
652 }
653
654 // Copy binaries to a name matching the final installed name
655 ctx.Build(pctx, android.BuildParams{
656 Rule: android.CpExecutable,
657 Description: "prebuilt",
658 Output: outputFile,
659 Input: in,
660 })
661 }
Colin Cross94921e72017-08-08 16:20:15 -0700662
663 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700664 }
665
666 return nil
667}
668
Inseob Kim7f283f42020-06-01 21:53:49 +0900669func (p *prebuiltBinaryLinker) binary() bool {
670 return true
671}
672
Patrice Arruda3554a982019-03-27 19:09:10 -0700673// 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 +0000674// device's directory, for both the host and device
675func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700676 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
677 return module.Init()
678}
679
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000680type prebuiltBinaryBazelHandler struct {
681 module *Module
682 decorator *binaryDecorator
683}
684
Leo Li74f7b972017-05-17 11:30:45 -0700685func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000686 module, binary := newBinary(hod, true)
Colin Crossde89fb82017-03-17 13:28:06 -0700687 module.compiler = nil
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000688 module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary}
Colin Crossde89fb82017-03-17 13:28:06 -0700689
690 prebuilt := &prebuiltBinaryLinker{
691 binaryDecorator: binary,
692 }
693 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100694 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700695
Colin Cross74d73e22017-08-02 11:05:49 -0700696 module.AddProperties(&prebuilt.properties)
697
698 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700699 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700700}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700701
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux256e3b42022-10-04 18:24:58 +0000702var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil)
703
704func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
705 bazelCtx := ctx.Config().BazelContext
706 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
707}
708
709func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
710 bazelCtx := ctx.Config().BazelContext
711 outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
712 if err != nil {
713 ctx.ModuleErrorf(err.Error())
714 return
715 }
716 if len(outputs) != 1 {
717 ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs)
718 return
719 }
720 out := android.PathForBazelOut(ctx, outputs[0])
721 h.module.outputFile = android.OptionalPathForPath(out)
722 h.module.maybeUnhideFromMake()
723}
724
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000725type bazelPrebuiltBinaryAttributes struct {
726 Src bazel.LabelAttribute
727 Strip stripAttributes
728}
729
730func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
731 prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module)
732
733 var la linkerAttributes
734 la.convertStripProps(ctx, module)
735 attrs := &bazelPrebuiltBinaryAttributes{
736 Src: prebuiltAttrs.Src,
737 Strip: stripAttrsFromLinkerAttrs(&la),
738 }
739
740 props := bazel.BazelTargetModuleProperties{
741 Rule_class: "cc_prebuilt_binary",
742 Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl",
743 }
744
745 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Jingwen Chenc4c34e12022-11-29 12:07:45 +0000746 tags := android.ApexAvailableTags(module)
747 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 +0000748}
749
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700750type Sanitized struct {
751 None struct {
752 Srcs []string `android:"path,arch_variant"`
753 } `android:"arch_variant"`
754 Address struct {
755 Srcs []string `android:"path,arch_variant"`
756 } `android:"arch_variant"`
757 Hwaddress struct {
758 Srcs []string `android:"path,arch_variant"`
759 } `android:"arch_variant"`
760}
761
762func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
763 if sanitize == nil {
764 return nil
765 }
766 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
767 return sanitized.Address.Srcs
768 }
769 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
770 return sanitized.Hwaddress.Srcs
771 }
772 return sanitized.None.Srcs
773}