blob: 8c404d30f6528881d30a8a85117e98780de7dc39 [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)
Paul Duffin59986b22019-12-19 14:38:36 +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
54 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
55 // This is needed only if this library is linked by other modules in build time.
56 // Only makes sense for the Windows target.
57 Windows_import_lib *string `android:"path,arch_variant"`
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +000058
59 // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either
60 // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with
61 // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this
62 // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see
63 // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled.
64 MixedBuildsDisabled bool `blueprint:"mutated"`
Patrice Arruda3554a982019-03-27 19:09:10 -070065}
66
Colin Crossde89fb82017-03-17 13:28:06 -070067type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070068 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080069
Patrice Arruda3554a982019-03-27 19:09:10 -070070 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070071}
72
Colin Crossde89fb82017-03-17 13:28:06 -070073func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070074 return &p.Prebuilt
75}
76
Colin Cross74d73e22017-08-02 11:05:49 -070077func (p *prebuiltLinker) PrebuiltSrcs() []string {
78 return p.properties.Srcs
79}
80
Colin Cross33b2fb72019-05-14 14:07:01 -070081type prebuiltLibraryInterface interface {
82 libraryInterface
83 prebuiltLinkerInterface
84 disablePrebuilt()
85}
86
Colin Crossde89fb82017-03-17 13:28:06 -070087type prebuiltLibraryLinker struct {
88 *libraryDecorator
89 prebuiltLinker
90}
91
92var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070093var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070094
Yi Kong00981662018-08-13 16:02:49 -070095func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
96
97func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080098 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070099}
100
101func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -0700102 return flags
Yi Kong00981662018-08-13 16:02:49 -0700103}
104
105func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
106 return p.libraryDecorator.linkerProps()
107}
108
Colin Crossce75d2c2016-10-06 16:12:58 -0700109func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700110 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000111
Colin Cross0de8a1e2020-09-18 14:15:30 -0700112 p.libraryDecorator.flagExporter.exportIncludes(ctx)
113 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
114 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
115 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
116 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
117 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
118
119 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000120
Colin Crossce75d2c2016-10-06 16:12:58 -0700121 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700122 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000123 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000124 if len(srcs) > 1 {
125 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
126 return nil
127 }
128
Jiyong Park892a98f2020-12-14 09:20:00 +0900129 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
130
Paul Duffinbce90da2020-03-12 20:17:14 +0000131 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700132
Yo Chianga3ad9b22020-03-18 14:19:07 +0800133 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700134 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
135 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
136 StaticLibrary: in,
137
138 TransitiveStaticLibrariesForOrdering: depSet,
139 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800140 return in
141 }
142
Colin Cross88f6fef2018-09-05 14:20:03 -0700143 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700144 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700145 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800146 outputFile := android.PathForModuleOut(ctx, libName)
147 var implicits android.Paths
148
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200149 if p.stripper.NeedsStrip(ctx) {
150 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700151 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200152 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700153 in = stripped
154 }
155
Colin Crossb60190a2018-09-04 16:28:17 -0700156 // Optimize out relinking against shared libraries whose interface hasn't changed by
157 // depending on a table of contents file instead of the library itself.
158 tocFile := android.PathForModuleOut(ctx, libName+".toc")
159 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400160 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700161
Yo Chianga3ad9b22020-03-18 14:19:07 +0800162 if ctx.Windows() && p.properties.Windows_import_lib != nil {
163 // Consumers of this library actually links to the import library in build
164 // time and dynamically links to the DLL in run time. i.e.
165 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
166 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
167 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
168 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
169 implicits = append(implicits, importLibOutputFile)
170
171 ctx.Build(pctx, android.BuildParams{
172 Rule: android.Cp,
173 Description: "prebuilt import library",
174 Input: importLibSrc,
175 Output: importLibOutputFile,
176 Args: map[string]string{
177 "cpFlags": "-L",
178 },
179 })
180 }
181
182 ctx.Build(pctx, android.BuildParams{
183 Rule: android.Cp,
184 Description: "prebuilt shared library",
185 Implicits: implicits,
186 Input: in,
187 Output: outputFile,
188 Args: map[string]string{
189 "cpFlags": "-L",
190 },
191 })
192
Colin Cross0de8a1e2020-09-18 14:15:30 -0700193 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400194 SharedLibrary: outputFile,
195 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700196
197 TableOfContents: p.tocFile,
198 })
199
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000200 // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub
201 // library as their source and must not be installed, but libclang_rt.* libraries
202 // have stubs because they are LLNDK libraries, but use an implementation library
203 // as their source and need to be installed. This discrepancy should be resolved
204 // without the prefix hack below.
205 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() &&
206 !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") {
207 ctx.Module().MakeUninstallable()
208 }
209
Yo Chianga3ad9b22020-03-18 14:19:07 +0800210 return outputFile
211 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700212 }
213
Colin Cross649d8172020-12-10 12:30:21 -0800214 if p.header() {
215 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
216
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000217 // Need to return an output path so that the AndroidMk logic doesn't skip
218 // the prebuilt header. For compatibility, in case Android.mk files use a
219 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
220 // placeholder, just like non-prebuilt header modules do in linkStatic().
221 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
222 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
223 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800224 }
225
Colin Crossce75d2c2016-10-06 16:12:58 -0700226 return nil
227}
228
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700229func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
230 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000231 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700232 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000233 if p.static() {
234 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700235 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000236 }
237 if p.shared() {
238 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700239 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000240 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000241 return srcs
242}
243
Jiyong Park379de2f2018-12-19 02:47:14 +0900244func (p *prebuiltLibraryLinker) shared() bool {
245 return p.libraryDecorator.shared()
246}
247
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700248func (p *prebuiltLibraryLinker) nativeCoverage() bool {
249 return false
250}
251
Colin Cross33b2fb72019-05-14 14:07:01 -0700252func (p *prebuiltLibraryLinker) disablePrebuilt() {
253 p.properties.Srcs = nil
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000254 p.properties.MixedBuildsDisabled = true
Colin Cross33b2fb72019-05-14 14:07:01 -0700255}
256
Jiyong Park892a98f2020-12-14 09:20:00 +0900257// Implements versionedInterface
258func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100259 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900260}
261
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000262func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700263 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700264 module.compiler = nil
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000265 module.bazelable = true
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000266 module.bazelHandler = &prebuiltLibraryBazelHandler{module: module, library: library}
Colin Crossce75d2c2016-10-06 16:12:58 -0700267
268 prebuilt := &prebuiltLibraryLinker{
269 libraryDecorator: library,
270 }
271 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700272 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700273
Colin Cross74d73e22017-08-02 11:05:49 -0700274 module.AddProperties(&prebuilt.properties)
275
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000276 if srcsProperty == "" {
277 android.InitPrebuiltModuleWithoutSrcs(module)
278 } else {
279 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
280 return prebuilt.prebuiltSrcs(ctx)
281 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000282
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000283 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
284 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900285
Paul Duffinac6e6082019-12-11 15:22:32 +0000286 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900287 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000288 return module, library
289}
290
Paul Duffinbce90da2020-03-12 20:17:14 +0000291// cc_prebuilt_library installs a precompiled shared library that are
292// listed in the srcs property in the device's directory.
293func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000294 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000295
296 // Prebuilt shared libraries can be included in APEXes
297 android.InitApexModule(module)
298
299 return module.Init()
300}
301
Paul Duffinac6e6082019-12-11 15:22:32 +0000302// cc_prebuilt_library_shared installs a precompiled shared library that are
303// listed in the srcs property in the device's directory.
304func PrebuiltSharedLibraryFactory() android.Module {
305 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
306 return module.Init()
307}
308
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400309// cc_prebuilt_test_library_shared installs a precompiled shared library
310// to be used as a data dependency of a test-related module (such as cc_test, or
311// cc_test_library).
312func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000313 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400314 library.BuildOnlyShared()
315 library.baseInstaller = NewTestInstaller()
316 return module.Init()
317}
318
Paul Duffinac6e6082019-12-11 15:22:32 +0000319func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000320 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000321 library.BuildOnlyShared()
322
323 // Prebuilt shared libraries can be included in APEXes
324 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900325
Leo Li74f7b972017-05-17 11:30:45 -0700326 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700327}
328
Patrice Arruda3554a982019-03-27 19:09:10 -0700329// cc_prebuilt_library_static installs a precompiled static library that are
330// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900331func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700332 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
333 return module.Init()
334}
335
336func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000337 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700338 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000339
Leo Li74f7b972017-05-17 11:30:45 -0700340 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700341}
342
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400343type bazelPrebuiltLibraryStaticAttributes struct {
344 Static_library bazel.LabelAttribute
345 Export_includes bazel.StringListAttribute
346 Export_system_includes bazel.StringListAttribute
347}
348
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000349// TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated
350// Implements bp2build for cc_prebuilt_library modules. This will generate:
351// * Only a prebuilt_library_static if the shared.enabled property is set to false across all variants.
352// * Only a prebuilt_library_shared if the static.enabled property is set to false across all variants
353// * Both a prebuilt_library_static and prebuilt_library_shared if the aforementioned properties are not false across
354// all variants
355//
356// In all cases, prebuilt_library_static target names will be appended with "_bp2build_cc_library_static".
357func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) {
358 prebuiltLibraryStaticBp2Build(ctx, module, true)
359 prebuiltLibrarySharedBp2Build(ctx, module)
360}
361
362func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) {
363 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true)
Liz Kammer54549442022-05-11 13:55:06 -0400364 exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400365
366 attrs := &bazelPrebuiltLibraryStaticAttributes{
367 Static_library: prebuiltAttrs.Src,
368 Export_includes: exportedIncludes.Includes,
369 Export_system_includes: exportedIncludes.SystemIncludes,
370 }
371
372 props := bazel.BazelTargetModuleProperties{
373 Rule_class: "prebuilt_library_static",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500374 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400375 }
376
377 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000378 if fullBuild {
379 name += "_bp2build_cc_library_static"
380 }
381 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled)
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400382}
383
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400384type bazelPrebuiltLibrarySharedAttributes struct {
385 Shared_library bazel.LabelAttribute
386}
387
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400388func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000389 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400390
391 attrs := &bazelPrebuiltLibrarySharedAttributes{
392 Shared_library: prebuiltAttrs.Src,
393 }
394
395 props := bazel.BazelTargetModuleProperties{
396 Rule_class: "prebuilt_library_shared",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500397 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_shared.bzl",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400398 }
399
400 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000401 ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400402}
403
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000404type prebuiltObjectProperties struct {
405 Srcs []string `android:"path,arch_variant"`
406}
407
408type prebuiltObjectLinker struct {
409 android.Prebuilt
410 objectLinker
411
412 properties prebuiltObjectProperties
413}
414
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000415type prebuiltLibraryBazelHandler struct {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400416 module *Module
417 library *libraryDecorator
418}
419
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000420var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil)
Chris Parsons6ce2cf92022-05-20 10:54:17 -0400421
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000422func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
423 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
424 return
425 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400426 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -0400427 bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx))
428}
429
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000430func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
431 if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled {
432 return
433 }
Chris Parsonsf874e462022-05-10 13:50:12 -0400434 bazelCtx := ctx.Config().BazelContext
435 ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400436 if err != nil {
Chris Parsonsf874e462022-05-10 13:50:12 -0400437 ctx.ModuleErrorf(err.Error())
438 return
Liz Kammer3f9e1552021-04-02 18:47:09 -0400439 }
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000440
441 if h.module.static() {
442 if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok {
443 return
444 }
445 } else if h.module.Shared() {
446 if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok {
447 return
448 }
449 } else {
450 return
451 }
452
453 h.module.maybeUnhideFromMake()
454}
455
456func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool {
Liz Kammerfe23bf32021-04-09 16:17:05 -0400457 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400458 if len(staticLibs) > 1 {
459 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000460 return false
Liz Kammer3f9e1552021-04-02 18:47:09 -0400461 }
462
463 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
464
465 // TODO(eakammer):Add stub-related flags if this library is a stub library.
466 // h.library.exportVersioningMacroIfNeeded(ctx)
467
468 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
469 // validation will fail. For now, set this to an empty list.
470 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
471 h.library.collectedSnapshotHeaders = android.Paths{}
472
473 if len(staticLibs) == 0 {
474 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000475 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400476 }
477
478 out := android.PathForBazelOut(ctx, staticLibs[0])
479 h.module.outputFile = android.OptionalPathForPath(out)
480
481 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
482 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
483 StaticLibrary: out,
484
485 TransitiveStaticLibrariesForOrdering: depSet,
486 })
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000487
488 return true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400489}
490
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000491func (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 -0400492 sharedLibs := ccInfo.CcSharedLibraryFiles
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000493 if len(sharedLibs) > 1 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400494 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000495 return false
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400496 }
497
498 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
499
500 // TODO(eakammer):Add stub-related flags if this library is a stub library.
501 // h.library.exportVersioningMacroIfNeeded(ctx)
502
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400503 if len(sharedLibs) == 0 {
504 h.module.outputFile = android.OptionalPath{}
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000505 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400506 }
507
508 out := android.PathForBazelOut(ctx, sharedLibs[0])
509 h.module.outputFile = android.OptionalPathForPath(out)
510
511 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
512 h.library.unstrippedOutputFile = out
513
514 var toc android.Path
515 if len(ccInfo.TocFile) > 0 {
516 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
517 } else {
518 toc = out // Just reuse `out` so ninja still gets an input but won't matter
519 }
520
521 info := SharedLibraryInfo{
522 SharedLibrary: out,
523 TableOfContents: android.OptionalPathForPath(toc),
524 Target: ctx.Target(),
525 }
526 ctx.SetProvider(SharedLibraryInfoProvider, info)
527
528 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
529 h.module.maybeUnhideFromMake()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000530 return true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400531}
532
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000533func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
534 return &p.Prebuilt
535}
536
537var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
538
539func (p *prebuiltObjectLinker) link(ctx ModuleContext,
540 flags Flags, deps PathDeps, objs Objects) android.Path {
541 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700542 // Copy objects to a name matching the final installed name
543 in := p.Prebuilt.SingleSourcePath(ctx)
544 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
545 ctx.Build(pctx, android.BuildParams{
546 Rule: android.CpExecutable,
547 Description: "prebuilt",
548 Output: outputFile,
549 Input: in,
550 })
551 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000552 }
553 return nil
554}
555
Inseob Kim1042d292020-06-01 23:23:05 +0900556func (p *prebuiltObjectLinker) object() bool {
557 return true
558}
559
Colin Cross7cabd422021-06-25 14:21:04 -0700560func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
561 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000562 prebuilt := &prebuiltObjectLinker{
563 objectLinker: objectLinker{
564 baseLinker: NewBaseLinker(nil),
565 },
566 }
567 module.linker = prebuilt
568 module.AddProperties(&prebuilt.properties)
569 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
570 android.InitSdkAwareModule(module)
571 return module
572}
573
574func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700575 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000576 return module.Init()
577}
578
Colin Crossde89fb82017-03-17 13:28:06 -0700579type prebuiltBinaryLinker struct {
580 *binaryDecorator
581 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100582
583 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700584}
585
586var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
587
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100588func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
589 return p.toolPath
590}
591
Colin Crossde89fb82017-03-17 13:28:06 -0700592func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
593 flags Flags, deps PathDeps, objs Objects) android.Path {
594 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700595 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700596 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
597 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100598 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700599 p.unstrippedOutputFile = in
600
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100601 if ctx.Host() {
602 // Host binaries are symlinked to their prebuilt source locations. That
603 // way they are executed directly from there so the linker resolves their
604 // shared library dependencies relative to that location (using
605 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
606 // repository can supply the expected versions of the shared libraries
607 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700608
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100609 // These shared lib paths may point to copies of the libs in
610 // .intermediates, which isn't where the binary will load them from, but
611 // it's fine for dependency tracking. If a library dependency is updated,
612 // the symlink will get a new timestamp, along with any installed symlinks
613 // handled in make.
614 sharedLibPaths := deps.EarlySharedLibs
615 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
616 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
617
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100618 var fromPath = in.String()
619 if !filepath.IsAbs(fromPath) {
620 fromPath = "$$PWD/" + fromPath
621 }
622
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100623 ctx.Build(pctx, android.BuildParams{
624 Rule: android.Symlink,
625 Output: outputFile,
626 Input: in,
627 Implicits: sharedLibPaths,
628 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100629 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100630 },
631 })
632
633 p.toolPath = android.OptionalPathForPath(outputFile)
634 } else {
635 if p.stripper.NeedsStrip(ctx) {
636 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
637 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
638 in = stripped
639 }
640
641 // Copy binaries to a name matching the final installed name
642 ctx.Build(pctx, android.BuildParams{
643 Rule: android.CpExecutable,
644 Description: "prebuilt",
645 Output: outputFile,
646 Input: in,
647 })
648 }
Colin Cross94921e72017-08-08 16:20:15 -0700649
650 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700651 }
652
653 return nil
654}
655
Inseob Kim7f283f42020-06-01 21:53:49 +0900656func (p *prebuiltBinaryLinker) binary() bool {
657 return true
658}
659
Patrice Arruda3554a982019-03-27 19:09:10 -0700660// cc_prebuilt_binary installs a precompiled executable in srcs property in the
661// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700662func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700663 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
664 return module.Init()
665}
666
667func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbdc922f2021-12-14 14:21:21 -0500668 module, binary := newBinary(hod, false)
Colin Crossde89fb82017-03-17 13:28:06 -0700669 module.compiler = nil
670
671 prebuilt := &prebuiltBinaryLinker{
672 binaryDecorator: binary,
673 }
674 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100675 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700676
Colin Cross74d73e22017-08-02 11:05:49 -0700677 module.AddProperties(&prebuilt.properties)
678
679 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700680 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700681}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700682
683type Sanitized struct {
684 None struct {
685 Srcs []string `android:"path,arch_variant"`
686 } `android:"arch_variant"`
687 Address struct {
688 Srcs []string `android:"path,arch_variant"`
689 } `android:"arch_variant"`
690 Hwaddress struct {
691 Srcs []string `android:"path,arch_variant"`
692 } `android:"arch_variant"`
693}
694
695func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
696 if sanitize == nil {
697 return nil
698 }
699 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
700 return sanitized.Address.Srcs
701 }
702 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
703 return sanitized.Hwaddress.Srcs
704 }
705 return sanitized.None.Srcs
706}