blob: 339a16d9de1b1915304b6aabf3bc8afe7c959ebd [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"
Colin Crossce75d2c2016-10-06 16:12:58 -070022)
23
24func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000025 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
26}
27
28func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000029 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000030 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
31 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040032 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000033 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000034 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070035}
36
37type prebuiltLinkerInterface interface {
38 Name(string) string
39 prebuilt() *android.Prebuilt
40}
41
Patrice Arruda3554a982019-03-27 19:09:10 -070042type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070043 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
44 Srcs []string `android:"path,arch_variant"`
45
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070046 Sanitized Sanitized `android:"arch_variant"`
47
Patrice Arruda3554a982019-03-27 19:09:10 -070048 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
49 // symbols, etc), default true.
50 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080051
52 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
53 // This is needed only if this library is linked by other modules in build time.
54 // Only makes sense for the Windows target.
55 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070056}
57
Colin Crossde89fb82017-03-17 13:28:06 -070058type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070059 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080060
Patrice Arruda3554a982019-03-27 19:09:10 -070061 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070062}
63
Colin Crossde89fb82017-03-17 13:28:06 -070064func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070065 return &p.Prebuilt
66}
67
Colin Cross74d73e22017-08-02 11:05:49 -070068func (p *prebuiltLinker) PrebuiltSrcs() []string {
69 return p.properties.Srcs
70}
71
Colin Cross33b2fb72019-05-14 14:07:01 -070072type prebuiltLibraryInterface interface {
73 libraryInterface
74 prebuiltLinkerInterface
75 disablePrebuilt()
76}
77
Colin Crossde89fb82017-03-17 13:28:06 -070078type prebuiltLibraryLinker struct {
79 *libraryDecorator
80 prebuiltLinker
81}
82
83var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070084var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070085
Yi Kong00981662018-08-13 16:02:49 -070086func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
87
88func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080089 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070090}
91
92func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070093 return flags
Yi Kong00981662018-08-13 16:02:49 -070094}
95
96func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
97 return p.libraryDecorator.linkerProps()
98}
99
Colin Crossce75d2c2016-10-06 16:12:58 -0700100func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700101 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000102
Colin Cross0de8a1e2020-09-18 14:15:30 -0700103 p.libraryDecorator.flagExporter.exportIncludes(ctx)
104 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
105 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
106 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
107 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
108 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
109
110 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000111
Colin Crossce75d2c2016-10-06 16:12:58 -0700112 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700113 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000114 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000115 if len(srcs) > 1 {
116 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
117 return nil
118 }
119
Jiyong Park892a98f2020-12-14 09:20:00 +0900120 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
121
Paul Duffinbce90da2020-03-12 20:17:14 +0000122 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700123
Yo Chianga3ad9b22020-03-18 14:19:07 +0800124 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700125 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
126 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
127 StaticLibrary: in,
128
129 TransitiveStaticLibrariesForOrdering: depSet,
130 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800131 return in
132 }
133
Colin Cross88f6fef2018-09-05 14:20:03 -0700134 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700135 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700136 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800137 outputFile := android.PathForModuleOut(ctx, libName)
138 var implicits android.Paths
139
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200140 if p.stripper.NeedsStrip(ctx) {
141 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700142 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200143 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700144 in = stripped
145 }
146
Colin Crossb60190a2018-09-04 16:28:17 -0700147 // Optimize out relinking against shared libraries whose interface hasn't changed by
148 // depending on a table of contents file instead of the library itself.
149 tocFile := android.PathForModuleOut(ctx, libName+".toc")
150 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400151 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700152
Yo Chianga3ad9b22020-03-18 14:19:07 +0800153 if ctx.Windows() && p.properties.Windows_import_lib != nil {
154 // Consumers of this library actually links to the import library in build
155 // time and dynamically links to the DLL in run time. i.e.
156 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
157 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
158 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
159 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
160 implicits = append(implicits, importLibOutputFile)
161
162 ctx.Build(pctx, android.BuildParams{
163 Rule: android.Cp,
164 Description: "prebuilt import library",
165 Input: importLibSrc,
166 Output: importLibOutputFile,
167 Args: map[string]string{
168 "cpFlags": "-L",
169 },
170 })
171 }
172
173 ctx.Build(pctx, android.BuildParams{
174 Rule: android.Cp,
175 Description: "prebuilt shared library",
176 Implicits: implicits,
177 Input: in,
178 Output: outputFile,
179 Args: map[string]string{
180 "cpFlags": "-L",
181 },
182 })
183
Colin Cross0de8a1e2020-09-18 14:15:30 -0700184 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400185 SharedLibrary: outputFile,
186 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700187
188 TableOfContents: p.tocFile,
189 })
190
Yo Chianga3ad9b22020-03-18 14:19:07 +0800191 return outputFile
192 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700193 }
194
Colin Cross649d8172020-12-10 12:30:21 -0800195 if p.header() {
196 ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{})
197
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000198 // Need to return an output path so that the AndroidMk logic doesn't skip
199 // the prebuilt header. For compatibility, in case Android.mk files use a
200 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
201 // placeholder, just like non-prebuilt header modules do in linkStatic().
202 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
203 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
204 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800205 }
206
Colin Crossce75d2c2016-10-06 16:12:58 -0700207 return nil
208}
209
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700210func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
211 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000212 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700213 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000214 if p.static() {
215 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700216 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000217 }
218 if p.shared() {
219 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700220 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000221 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000222 return srcs
223}
224
Jiyong Park379de2f2018-12-19 02:47:14 +0900225func (p *prebuiltLibraryLinker) shared() bool {
226 return p.libraryDecorator.shared()
227}
228
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700229func (p *prebuiltLibraryLinker) nativeCoverage() bool {
230 return false
231}
232
Colin Cross33b2fb72019-05-14 14:07:01 -0700233func (p *prebuiltLibraryLinker) disablePrebuilt() {
234 p.properties.Srcs = nil
235}
236
Jiyong Park892a98f2020-12-14 09:20:00 +0900237// Implements versionedInterface
238func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100239 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900240}
241
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000242func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700243 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700244 module.compiler = nil
245
246 prebuilt := &prebuiltLibraryLinker{
247 libraryDecorator: library,
248 }
249 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700250 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700251
Colin Cross74d73e22017-08-02 11:05:49 -0700252 module.AddProperties(&prebuilt.properties)
253
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000254 if srcsProperty == "" {
255 android.InitPrebuiltModuleWithoutSrcs(module)
256 } else {
257 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
258 return prebuilt.prebuiltSrcs(ctx)
259 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000260
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000261 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
262 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900263
Paul Duffinac6e6082019-12-11 15:22:32 +0000264 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900265 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000266 return module, library
267}
268
Paul Duffinbce90da2020-03-12 20:17:14 +0000269// cc_prebuilt_library installs a precompiled shared library that are
270// listed in the srcs property in the device's directory.
271func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000272 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000273
274 // Prebuilt shared libraries can be included in APEXes
275 android.InitApexModule(module)
276
277 return module.Init()
278}
279
Paul Duffinac6e6082019-12-11 15:22:32 +0000280// cc_prebuilt_library_shared installs a precompiled shared library that are
281// listed in the srcs property in the device's directory.
282func PrebuiltSharedLibraryFactory() android.Module {
283 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
284 return module.Init()
285}
286
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400287// cc_prebuilt_test_library_shared installs a precompiled shared library
288// to be used as a data dependency of a test-related module (such as cc_test, or
289// cc_test_library).
290func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000291 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400292 library.BuildOnlyShared()
293 library.baseInstaller = NewTestInstaller()
294 return module.Init()
295}
296
Paul Duffinac6e6082019-12-11 15:22:32 +0000297func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000298 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000299 library.BuildOnlyShared()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400300 module.bazelable = true
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400301 module.bazelHandler = &prebuiltSharedLibraryBazelHandler{module: module, library: library}
Paul Duffinac6e6082019-12-11 15:22:32 +0000302
303 // Prebuilt shared libraries can be included in APEXes
304 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900305
Leo Li74f7b972017-05-17 11:30:45 -0700306 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700307}
308
Patrice Arruda3554a982019-03-27 19:09:10 -0700309// cc_prebuilt_library_static installs a precompiled static library that are
310// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900311func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700312 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
313 return module.Init()
314}
315
316func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000317 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700318 library.BuildOnlyStatic()
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400319 module.bazelable = true
Liz Kammer3f9e1552021-04-02 18:47:09 -0400320 module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library}
Leo Li74f7b972017-05-17 11:30:45 -0700321 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700322}
323
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400324type bazelPrebuiltLibraryStaticAttributes struct {
325 Static_library bazel.LabelAttribute
326 Export_includes bazel.StringListAttribute
327 Export_system_includes bazel.StringListAttribute
328}
329
330func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module) {
331 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module)
332 exportedIncludes := Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx, module)
333
334 attrs := &bazelPrebuiltLibraryStaticAttributes{
335 Static_library: prebuiltAttrs.Src,
336 Export_includes: exportedIncludes.Includes,
337 Export_system_includes: exportedIncludes.SystemIncludes,
338 }
339
340 props := bazel.BazelTargetModuleProperties{
341 Rule_class: "prebuilt_library_static",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500342 Bzl_load_location: "//build/bazel/rules/cc:prebuilt_library_static.bzl",
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400343 }
344
345 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
346 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
347}
348
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400349type bazelPrebuiltLibrarySharedAttributes struct {
350 Shared_library bazel.LabelAttribute
351}
352
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400353func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux7fa06962021-10-25 10:28:33 -0400354 prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module)
355
356 attrs := &bazelPrebuiltLibrarySharedAttributes{
357 Shared_library: prebuiltAttrs.Src,
358 }
359
360 props := bazel.BazelTargetModuleProperties{
361 Rule_class: "prebuilt_library_shared",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500362 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 -0400363 }
364
365 name := android.RemoveOptionalPrebuiltPrefix(module.Name())
366 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs)
367}
368
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000369type prebuiltObjectProperties struct {
370 Srcs []string `android:"path,arch_variant"`
371}
372
373type prebuiltObjectLinker struct {
374 android.Prebuilt
375 objectLinker
376
377 properties prebuiltObjectProperties
378}
379
Liz Kammer3f9e1552021-04-02 18:47:09 -0400380type prebuiltStaticLibraryBazelHandler struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000381 android.BazelHandler
Liz Kammer3f9e1552021-04-02 18:47:09 -0400382
383 module *Module
384 library *libraryDecorator
385}
386
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +0000387func (h *prebuiltStaticLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer3f9e1552021-04-02 18:47:09 -0400388 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -0400389 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
Liz Kammerfe23bf32021-04-09 16:17:05 -0400390 if err != nil {
391 ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err)
392 }
Liz Kammer3f9e1552021-04-02 18:47:09 -0400393 if !ok {
394 return false
395 }
Liz Kammerfe23bf32021-04-09 16:17:05 -0400396 staticLibs := ccInfo.CcStaticLibraryFiles
Liz Kammer3f9e1552021-04-02 18:47:09 -0400397 if len(staticLibs) > 1 {
398 ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs)
399 return false
400 }
401
402 // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags
403
404 // TODO(eakammer):Add stub-related flags if this library is a stub library.
405 // h.library.exportVersioningMacroIfNeeded(ctx)
406
407 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
408 // validation will fail. For now, set this to an empty list.
409 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
410 h.library.collectedSnapshotHeaders = android.Paths{}
411
412 if len(staticLibs) == 0 {
413 h.module.outputFile = android.OptionalPath{}
414 return true
415 }
416
417 out := android.PathForBazelOut(ctx, staticLibs[0])
418 h.module.outputFile = android.OptionalPathForPath(out)
419
420 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build()
421 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
422 StaticLibrary: out,
423
424 TransitiveStaticLibrariesForOrdering: depSet,
425 })
426
427 return true
428}
429
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxc3b97c32021-10-05 13:43:23 -0400430type prebuiltSharedLibraryBazelHandler struct {
431 android.BazelHandler
432
433 module *Module
434 library *libraryDecorator
435}
436
437func (h *prebuiltSharedLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
438 bazelCtx := ctx.Config().BazelContext
439 ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx))
440 if err != nil {
441 ctx.ModuleErrorf("Error getting Bazel CcInfo for %s: %s", label, err)
442 }
443 if !ok {
444 return false
445 }
446 sharedLibs := ccInfo.CcSharedLibraryFiles
447 if len(sharedLibs) != 1 {
448 ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs)
449 return false
450 }
451
452 // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags
453
454 // TODO(eakammer):Add stub-related flags if this library is a stub library.
455 // h.library.exportVersioningMacroIfNeeded(ctx)
456
457 // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise
458 // validation will fail. For now, set this to an empty list.
459 // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation.
460 h.library.collectedSnapshotHeaders = android.Paths{}
461
462 if len(sharedLibs) == 0 {
463 h.module.outputFile = android.OptionalPath{}
464 return true
465 }
466
467 out := android.PathForBazelOut(ctx, sharedLibs[0])
468 h.module.outputFile = android.OptionalPathForPath(out)
469
470 // FIXME(b/214600441): We don't yet strip prebuilt shared libraries
471 h.library.unstrippedOutputFile = out
472
473 var toc android.Path
474 if len(ccInfo.TocFile) > 0 {
475 toc = android.PathForBazelOut(ctx, ccInfo.TocFile)
476 } else {
477 toc = out // Just reuse `out` so ninja still gets an input but won't matter
478 }
479
480 info := SharedLibraryInfo{
481 SharedLibrary: out,
482 TableOfContents: android.OptionalPathForPath(toc),
483 Target: ctx.Target(),
484 }
485 ctx.SetProvider(SharedLibraryInfoProvider, info)
486
487 h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo)
488 h.module.maybeUnhideFromMake()
489
490 return true
491}
492
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000493func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
494 return &p.Prebuilt
495}
496
497var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
498
499func (p *prebuiltObjectLinker) link(ctx ModuleContext,
500 flags Flags, deps PathDeps, objs Objects) android.Path {
501 if len(p.properties.Srcs) > 0 {
502 return p.Prebuilt.SingleSourcePath(ctx)
503 }
504 return nil
505}
506
Inseob Kim1042d292020-06-01 23:23:05 +0900507func (p *prebuiltObjectLinker) object() bool {
508 return true
509}
510
Colin Cross7cabd422021-06-25 14:21:04 -0700511func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
512 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000513 prebuilt := &prebuiltObjectLinker{
514 objectLinker: objectLinker{
515 baseLinker: NewBaseLinker(nil),
516 },
517 }
518 module.linker = prebuilt
519 module.AddProperties(&prebuilt.properties)
520 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
521 android.InitSdkAwareModule(module)
522 return module
523}
524
525func prebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700526 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000527 return module.Init()
528}
529
Colin Crossde89fb82017-03-17 13:28:06 -0700530type prebuiltBinaryLinker struct {
531 *binaryDecorator
532 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100533
534 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700535}
536
537var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
538
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100539func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
540 return p.toolPath
541}
542
Colin Crossde89fb82017-03-17 13:28:06 -0700543func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
544 flags Flags, deps PathDeps, objs Objects) android.Path {
545 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700546 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700547 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
548 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100549 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700550 p.unstrippedOutputFile = in
551
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100552 if ctx.Host() {
553 // Host binaries are symlinked to their prebuilt source locations. That
554 // way they are executed directly from there so the linker resolves their
555 // shared library dependencies relative to that location (using
556 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
557 // repository can supply the expected versions of the shared libraries
558 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700559
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100560 // These shared lib paths may point to copies of the libs in
561 // .intermediates, which isn't where the binary will load them from, but
562 // it's fine for dependency tracking. If a library dependency is updated,
563 // the symlink will get a new timestamp, along with any installed symlinks
564 // handled in make.
565 sharedLibPaths := deps.EarlySharedLibs
566 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
567 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
568
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100569 var fromPath = in.String()
570 if !filepath.IsAbs(fromPath) {
571 fromPath = "$$PWD/" + fromPath
572 }
573
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100574 ctx.Build(pctx, android.BuildParams{
575 Rule: android.Symlink,
576 Output: outputFile,
577 Input: in,
578 Implicits: sharedLibPaths,
579 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100580 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100581 },
582 })
583
584 p.toolPath = android.OptionalPathForPath(outputFile)
585 } else {
586 if p.stripper.NeedsStrip(ctx) {
587 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
588 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
589 in = stripped
590 }
591
592 // Copy binaries to a name matching the final installed name
593 ctx.Build(pctx, android.BuildParams{
594 Rule: android.CpExecutable,
595 Description: "prebuilt",
596 Output: outputFile,
597 Input: in,
598 })
599 }
Colin Cross94921e72017-08-08 16:20:15 -0700600
601 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700602 }
603
604 return nil
605}
606
Inseob Kim7f283f42020-06-01 21:53:49 +0900607func (p *prebuiltBinaryLinker) binary() bool {
608 return true
609}
610
Patrice Arruda3554a982019-03-27 19:09:10 -0700611// cc_prebuilt_binary installs a precompiled executable in srcs property in the
612// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700613func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700614 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
615 return module.Init()
616}
617
618func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Liz Kammerbdc922f2021-12-14 14:21:21 -0500619 module, binary := newBinary(hod, false)
Colin Crossde89fb82017-03-17 13:28:06 -0700620 module.compiler = nil
621
622 prebuilt := &prebuiltBinaryLinker{
623 binaryDecorator: binary,
624 }
625 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100626 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700627
Colin Cross74d73e22017-08-02 11:05:49 -0700628 module.AddProperties(&prebuilt.properties)
629
630 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700631 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700632}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700633
634type Sanitized struct {
635 None struct {
636 Srcs []string `android:"path,arch_variant"`
637 } `android:"arch_variant"`
638 Address struct {
639 Srcs []string `android:"path,arch_variant"`
640 } `android:"arch_variant"`
641 Hwaddress struct {
642 Srcs []string `android:"path,arch_variant"`
643 } `android:"arch_variant"`
644}
645
646func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
647 if sanitize == nil {
648 return nil
649 }
650 if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil {
651 return sanitized.Address.Srcs
652 }
653 if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil {
654 return sanitized.Hwaddress.Srcs
655 }
656 return sanitized.None.Srcs
657}