blob: e721c538827082ae13d4edcd7911a3d9537dd77b [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"
Colin Crossce75d2c2016-10-06 16:12:58 -070021)
22
23func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000024 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
25}
26
27func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000028 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000029 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
30 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040031 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Colin Crossc5075e92022-12-05 16:46:39 -080032 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000033 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070034}
35
36type prebuiltLinkerInterface interface {
37 Name(string) string
38 prebuilt() *android.Prebuilt
39}
40
Patrice Arruda3554a982019-03-27 19:09:10 -070041type prebuiltLinkerProperties struct {
Patrice Arruda3554a982019-03-27 19:09:10 -070042 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
43 Srcs []string `android:"path,arch_variant"`
44
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070045 Sanitized Sanitized `android:"arch_variant"`
46
Patrice Arruda3554a982019-03-27 19:09:10 -070047 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
48 // symbols, etc), default true.
49 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080050
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010051 // if set, add an extra objcopy --prefix-symbols= step
52 Prefix_symbols *string
53
Yo Chianga3ad9b22020-03-18 14:19:07 +080054 // 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"`
Patrice Arruda3554a982019-03-27 19:09:10 -070058}
59
Colin Crossde89fb82017-03-17 13:28:06 -070060type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070061 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080062
Patrice Arruda3554a982019-03-27 19:09:10 -070063 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070064}
65
Colin Crossde89fb82017-03-17 13:28:06 -070066func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070067 return &p.Prebuilt
68}
69
Colin Cross74d73e22017-08-02 11:05:49 -070070func (p *prebuiltLinker) PrebuiltSrcs() []string {
71 return p.properties.Srcs
72}
73
Colin Cross33b2fb72019-05-14 14:07:01 -070074type prebuiltLibraryInterface interface {
75 libraryInterface
76 prebuiltLinkerInterface
77 disablePrebuilt()
78}
79
Colin Crossde89fb82017-03-17 13:28:06 -070080type prebuiltLibraryLinker struct {
81 *libraryDecorator
82 prebuiltLinker
83}
84
85var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070086var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070087
Yi Kong00981662018-08-13 16:02:49 -070088func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
89
90func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080091 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070092}
93
94func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070095 return flags
Yi Kong00981662018-08-13 16:02:49 -070096}
97
98func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
99 return p.libraryDecorator.linkerProps()
100}
101
Colin Crossce75d2c2016-10-06 16:12:58 -0700102func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700103 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000104
Colin Cross0de8a1e2020-09-18 14:15:30 -0700105 p.libraryDecorator.flagExporter.exportIncludes(ctx)
106 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
107 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
108 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
109 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
110 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
111
112 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000113
Colin Crossce75d2c2016-10-06 16:12:58 -0700114 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700115 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000116 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000117 if len(srcs) > 1 {
118 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
119 return nil
120 }
121
Jiyong Park892a98f2020-12-14 09:20:00 +0900122 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
123
Paul Duffinbce90da2020-03-12 20:17:14 +0000124 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700125
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100126 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
127 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
128 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
129 in, flagsToBuilderFlags(flags), prefixed)
130 in = prefixed
131 }
132
Yo Chianga3ad9b22020-03-18 14:19:07 +0800133 if p.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700134 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build()
Colin Cross40213022023-12-13 15:19:49 -0800135 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700136 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 Cross40213022023-12-13 15:19:49 -0800193 android.SetProvider(ctx, 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
Jooyung Hane5f063e2023-03-23 14:31:19 +0900201 // library as their source and must not be installed, but other prebuilts like
202 // libclang_rt.* libraries set `stubs` property because they are LLNDK libraries,
203 // but use an implementation library as their source and need to be installed.
204 // This discrepancy should be resolved without the prefix hack below.
205 isModuleSdkPrebuilts := android.HasAnyPrefix(ctx.ModuleDir(), []string{
206 "prebuilts/runtime/mainline/", "prebuilts/module_sdk/"})
207 if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() && isModuleSdkPrebuilts {
Jingwen Chen8ac7d7d2023-03-20 11:05:16 +0000208 ctx.Module().MakeUninstallable()
Martin Stjernholm5bdf2d52022-02-06 22:07:45 +0000209 }
210
Yo Chianga3ad9b22020-03-18 14:19:07 +0800211 return outputFile
212 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700213 }
214
Colin Cross649d8172020-12-10 12:30:21 -0800215 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800216 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800217
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000218 // Need to return an output path so that the AndroidMk logic doesn't skip
219 // the prebuilt header. For compatibility, in case Android.mk files use a
220 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
221 // placeholder, just like non-prebuilt header modules do in linkStatic().
222 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
223 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
224 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800225 }
226
Colin Crossce75d2c2016-10-06 16:12:58 -0700227 return nil
228}
229
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700230func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
231 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000232 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700233 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000234 if p.static() {
235 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700236 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000237 }
238 if p.shared() {
239 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700240 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000241 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000242 return srcs
243}
244
Jiyong Park379de2f2018-12-19 02:47:14 +0900245func (p *prebuiltLibraryLinker) shared() bool {
246 return p.libraryDecorator.shared()
247}
248
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700249func (p *prebuiltLibraryLinker) nativeCoverage() bool {
250 return false
251}
252
Colin Cross33b2fb72019-05-14 14:07:01 -0700253func (p *prebuiltLibraryLinker) disablePrebuilt() {
254 p.properties.Srcs = nil
255}
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
265
266 prebuilt := &prebuiltLibraryLinker{
267 libraryDecorator: library,
268 }
269 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700270 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700271
Colin Cross74d73e22017-08-02 11:05:49 -0700272 module.AddProperties(&prebuilt.properties)
273
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000274 if srcsProperty == "" {
275 android.InitPrebuiltModuleWithoutSrcs(module)
276 } else {
277 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
278 return prebuilt.prebuiltSrcs(ctx)
279 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000280
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000281 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
282 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900283
Paul Duffinac6e6082019-12-11 15:22:32 +0000284 return module, library
285}
286
Paul Duffinbce90da2020-03-12 20:17:14 +0000287// cc_prebuilt_library installs a precompiled shared library that are
288// listed in the srcs property in the device's directory.
289func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000290 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000291
292 // Prebuilt shared libraries can be included in APEXes
293 android.InitApexModule(module)
294
295 return module.Init()
296}
297
Paul Duffinac6e6082019-12-11 15:22:32 +0000298// cc_prebuilt_library_shared installs a precompiled shared library that are
299// listed in the srcs property in the device's directory.
300func PrebuiltSharedLibraryFactory() android.Module {
301 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
302 return module.Init()
303}
304
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400305// cc_prebuilt_test_library_shared installs a precompiled shared library
306// to be used as a data dependency of a test-related module (such as cc_test, or
307// cc_test_library).
308func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000309 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400310 library.BuildOnlyShared()
311 library.baseInstaller = NewTestInstaller()
312 return module.Init()
313}
314
Paul Duffinac6e6082019-12-11 15:22:32 +0000315func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000316 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000317 library.BuildOnlyShared()
318
319 // Prebuilt shared libraries can be included in APEXes
320 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900321
Leo Li74f7b972017-05-17 11:30:45 -0700322 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700323}
324
Patrice Arruda3554a982019-03-27 19:09:10 -0700325// cc_prebuilt_library_static installs a precompiled static library that are
326// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900327func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700328 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
329 return module.Init()
330}
331
332func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000333 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700334 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000335
Leo Li74f7b972017-05-17 11:30:45 -0700336 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700337}
338
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000339type prebuiltObjectProperties struct {
340 Srcs []string `android:"path,arch_variant"`
341}
342
343type prebuiltObjectLinker struct {
344 android.Prebuilt
345 objectLinker
346
347 properties prebuiltObjectProperties
348}
349
350func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
351 return &p.Prebuilt
352}
353
354var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
355
356func (p *prebuiltObjectLinker) link(ctx ModuleContext,
357 flags Flags, deps PathDeps, objs Objects) android.Path {
358 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700359 // Copy objects to a name matching the final installed name
360 in := p.Prebuilt.SingleSourcePath(ctx)
361 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
362 ctx.Build(pctx, android.BuildParams{
363 Rule: android.CpExecutable,
364 Description: "prebuilt",
365 Output: outputFile,
366 Input: in,
367 })
368 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000369 }
370 return nil
371}
372
Inseob Kim1042d292020-06-01 23:23:05 +0900373func (p *prebuiltObjectLinker) object() bool {
374 return true
375}
376
Colin Cross7cabd422021-06-25 14:21:04 -0700377func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
378 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000379 prebuilt := &prebuiltObjectLinker{
380 objectLinker: objectLinker{
381 baseLinker: NewBaseLinker(nil),
382 },
383 }
384 module.linker = prebuilt
385 module.AddProperties(&prebuilt.properties)
386 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000387 return module
388}
389
Colin Crossc5075e92022-12-05 16:46:39 -0800390func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700391 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000392 return module.Init()
393}
394
Colin Crossde89fb82017-03-17 13:28:06 -0700395type prebuiltBinaryLinker struct {
396 *binaryDecorator
397 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100398
399 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700400}
401
402var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
403
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100404func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
405 return p.toolPath
406}
407
Colin Crossde89fb82017-03-17 13:28:06 -0700408func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
409 flags Flags, deps PathDeps, objs Objects) android.Path {
410 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700411 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700412 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
413 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100414 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700415 p.unstrippedOutputFile = in
416
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100417 if ctx.Host() {
418 // Host binaries are symlinked to their prebuilt source locations. That
419 // way they are executed directly from there so the linker resolves their
420 // shared library dependencies relative to that location (using
421 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
422 // repository can supply the expected versions of the shared libraries
423 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700424
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100425 // These shared lib paths may point to copies of the libs in
426 // .intermediates, which isn't where the binary will load them from, but
427 // it's fine for dependency tracking. If a library dependency is updated,
428 // the symlink will get a new timestamp, along with any installed symlinks
429 // handled in make.
430 sharedLibPaths := deps.EarlySharedLibs
431 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
432 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
433
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100434 var fromPath = in.String()
435 if !filepath.IsAbs(fromPath) {
436 fromPath = "$$PWD/" + fromPath
437 }
438
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100439 ctx.Build(pctx, android.BuildParams{
440 Rule: android.Symlink,
441 Output: outputFile,
442 Input: in,
443 Implicits: sharedLibPaths,
444 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100445 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100446 },
447 })
448
449 p.toolPath = android.OptionalPathForPath(outputFile)
450 } else {
451 if p.stripper.NeedsStrip(ctx) {
452 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
453 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
454 in = stripped
455 }
456
457 // Copy binaries to a name matching the final installed name
458 ctx.Build(pctx, android.BuildParams{
459 Rule: android.CpExecutable,
460 Description: "prebuilt",
461 Output: outputFile,
462 Input: in,
463 })
464 }
Colin Cross94921e72017-08-08 16:20:15 -0700465
466 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700467 }
468
469 return nil
470}
471
Inseob Kim7f283f42020-06-01 21:53:49 +0900472func (p *prebuiltBinaryLinker) binary() bool {
473 return true
474}
475
Patrice Arruda3554a982019-03-27 19:09:10 -0700476// 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 +0000477// device's directory, for both the host and device
478func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700479 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
480 return module.Init()
481}
482
483func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800484 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700485 module.compiler = nil
486
487 prebuilt := &prebuiltBinaryLinker{
488 binaryDecorator: binary,
489 }
490 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100491 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700492
Colin Cross74d73e22017-08-02 11:05:49 -0700493 module.AddProperties(&prebuilt.properties)
494
495 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700496 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700497}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700498
499type Sanitized struct {
500 None struct {
501 Srcs []string `android:"path,arch_variant"`
502 } `android:"arch_variant"`
503 Address struct {
504 Srcs []string `android:"path,arch_variant"`
505 } `android:"arch_variant"`
506 Hwaddress struct {
507 Srcs []string `android:"path,arch_variant"`
508 } `android:"arch_variant"`
509}
510
511func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
512 if sanitize == nil {
513 return nil
514 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400515 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700516 return sanitized.Address.Srcs
517 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400518 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700519 return sanitized.Hwaddress.Srcs
520 }
521 return sanitized.None.Srcs
522}