blob: e023a324c64c08a6efd2129ede485cd062843404 [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
Spandan Das2b6dfb52024-01-19 00:22:22 +000020 "github.com/google/blueprint/proptools"
21
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000022 "android/soong/android"
Colin Crossce75d2c2016-10-06 16:12:58 -070023)
24
25func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000026 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
27}
28
29func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000030 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000031 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
32 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040033 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Colin Crossc5075e92022-12-05 16:46:39 -080034 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000035 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070036}
37
38type prebuiltLinkerInterface interface {
39 Name(string) string
40 prebuilt() *android.Prebuilt
Spandan Das2b6dfb52024-01-19 00:22:22 +000041 sourceModuleName() string
Colin Crossce75d2c2016-10-06 16:12:58 -070042}
43
Patrice Arruda3554a982019-03-27 19:09:10 -070044type prebuiltLinkerProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +000045 // Name of the source soong module that gets shadowed by this prebuilt
46 // If unspecified, follows the naming convention that the source module of
47 // the prebuilt is Name() without "prebuilt_" prefix
48 Source_module_name *string
49
Patrice Arruda3554a982019-03-27 19:09:10 -070050 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
51 Srcs []string `android:"path,arch_variant"`
52
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070053 Sanitized Sanitized `android:"arch_variant"`
54
Patrice Arruda3554a982019-03-27 19:09:10 -070055 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
56 // symbols, etc), default true.
57 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080058
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010059 // if set, add an extra objcopy --prefix-symbols= step
60 Prefix_symbols *string
61
Yo Chianga3ad9b22020-03-18 14:19:07 +080062 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
63 // This is needed only if this library is linked by other modules in build time.
64 // Only makes sense for the Windows target.
65 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070066}
67
Colin Crossde89fb82017-03-17 13:28:06 -070068type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070069 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080070
Patrice Arruda3554a982019-03-27 19:09:10 -070071 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070072}
73
Colin Crossde89fb82017-03-17 13:28:06 -070074func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070075 return &p.Prebuilt
76}
77
Colin Cross74d73e22017-08-02 11:05:49 -070078func (p *prebuiltLinker) PrebuiltSrcs() []string {
79 return p.properties.Srcs
80}
81
Colin Cross33b2fb72019-05-14 14:07:01 -070082type prebuiltLibraryInterface interface {
83 libraryInterface
84 prebuiltLinkerInterface
85 disablePrebuilt()
86}
87
Colin Crossde89fb82017-03-17 13:28:06 -070088type prebuiltLibraryLinker struct {
89 *libraryDecorator
90 prebuiltLinker
91}
92
93var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070094var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070095
Yi Kong00981662018-08-13 16:02:49 -070096func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
97
98func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080099 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -0700100}
101
102func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -0700103 return flags
Yi Kong00981662018-08-13 16:02:49 -0700104}
105
106func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
107 return p.libraryDecorator.linkerProps()
108}
109
Colin Crossce75d2c2016-10-06 16:12:58 -0700110func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700111 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000112
Colin Cross0de8a1e2020-09-18 14:15:30 -0700113 p.libraryDecorator.flagExporter.exportIncludes(ctx)
114 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
115 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
116 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
117 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
118 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
119
120 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000121
Colin Crossce75d2c2016-10-06 16:12:58 -0700122 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700123 srcs := p.prebuiltSrcs(ctx)
Paul Duffinbce90da2020-03-12 20:17:14 +0000124 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000125 if len(srcs) > 1 {
126 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
127 return nil
128 }
129
Jiyong Park892a98f2020-12-14 09:20:00 +0900130 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
131
Paul Duffinbce90da2020-03-12 20:17:14 +0000132 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700133
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100134 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
135 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
136 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
137 in, flagsToBuilderFlags(flags), prefixed)
138 in = prefixed
139 }
140
Yo Chianga3ad9b22020-03-18 14:19:07 +0800141 if p.static() {
Colin Crossc85750b2022-04-21 12:50:51 -0700142 depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build()
Colin Cross40213022023-12-13 15:19:49 -0800143 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700144 StaticLibrary: in,
145
146 TransitiveStaticLibrariesForOrdering: depSet,
147 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800148 return in
149 }
150
Colin Cross88f6fef2018-09-05 14:20:03 -0700151 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700152 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700153 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800154 outputFile := android.PathForModuleOut(ctx, libName)
155 var implicits android.Paths
156
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200157 if p.stripper.NeedsStrip(ctx) {
158 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700159 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200160 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700161 in = stripped
162 }
163
Colin Crossb60190a2018-09-04 16:28:17 -0700164 // Optimize out relinking against shared libraries whose interface hasn't changed by
165 // depending on a table of contents file instead of the library itself.
166 tocFile := android.PathForModuleOut(ctx, libName+".toc")
167 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400168 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700169
Yo Chianga3ad9b22020-03-18 14:19:07 +0800170 if ctx.Windows() && p.properties.Windows_import_lib != nil {
171 // Consumers of this library actually links to the import library in build
172 // time and dynamically links to the DLL in run time. i.e.
173 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
174 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
175 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
176 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
177 implicits = append(implicits, importLibOutputFile)
178
179 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400180 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800181 Description: "prebuilt import library",
182 Input: importLibSrc,
183 Output: importLibOutputFile,
184 Args: map[string]string{
185 "cpFlags": "-L",
186 },
187 })
188 }
189
190 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400191 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800192 Description: "prebuilt shared library",
193 Implicits: implicits,
194 Input: in,
195 Output: outputFile,
196 Args: map[string]string{
197 "cpFlags": "-L",
198 },
199 })
200
Colin Cross40213022023-12-13 15:19:49 -0800201 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400202 SharedLibrary: outputFile,
203 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700204
205 TableOfContents: p.tocFile,
206 })
207
Yo Chianga3ad9b22020-03-18 14:19:07 +0800208 return outputFile
209 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700210 }
211
Colin Cross649d8172020-12-10 12:30:21 -0800212 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800213 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800214
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000215 // Need to return an output path so that the AndroidMk logic doesn't skip
216 // the prebuilt header. For compatibility, in case Android.mk files use a
217 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
218 // placeholder, just like non-prebuilt header modules do in linkStatic().
219 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
220 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
221 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800222 }
223
Colin Crossce75d2c2016-10-06 16:12:58 -0700224 return nil
225}
226
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700227func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
228 sanitize := ctx.Module().(*Module).sanitize
Paul Duffinbce90da2020-03-12 20:17:14 +0000229 srcs := p.properties.Srcs
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700230 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000231 if p.static() {
232 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700233 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000234 }
235 if p.shared() {
236 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700237 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000238 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000239 return srcs
240}
241
Jiyong Park379de2f2018-12-19 02:47:14 +0900242func (p *prebuiltLibraryLinker) shared() bool {
243 return p.libraryDecorator.shared()
244}
245
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700246func (p *prebuiltLibraryLinker) nativeCoverage() bool {
247 return false
248}
249
Colin Cross33b2fb72019-05-14 14:07:01 -0700250func (p *prebuiltLibraryLinker) disablePrebuilt() {
251 p.properties.Srcs = nil
Ryan Prichard21e2c102024-02-28 18:59:30 -0800252 p.properties.Sanitized.None.Srcs = nil
253 p.properties.Sanitized.Address.Srcs = nil
254 p.properties.Sanitized.Hwaddress.Srcs = nil
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
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 {
Spandan Das2b6dfb52024-01-19 00:22:22 +0000340 // Name of the source soong module that gets shadowed by this prebuilt
341 // If unspecified, follows the naming convention that the source module of
342 // the prebuilt is Name() without "prebuilt_" prefix
343 Source_module_name *string
344 Srcs []string `android:"path,arch_variant"`
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000345}
346
347type prebuiltObjectLinker struct {
348 android.Prebuilt
349 objectLinker
350
351 properties prebuiltObjectProperties
352}
353
354func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
355 return &p.Prebuilt
356}
357
Spandan Das2b6dfb52024-01-19 00:22:22 +0000358func (p *prebuiltObjectLinker) sourceModuleName() string {
359 return proptools.String(p.properties.Source_module_name)
360}
361
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000362var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
363
364func (p *prebuiltObjectLinker) link(ctx ModuleContext,
365 flags Flags, deps PathDeps, objs Objects) android.Path {
366 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700367 // Copy objects to a name matching the final installed name
368 in := p.Prebuilt.SingleSourcePath(ctx)
369 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
370 ctx.Build(pctx, android.BuildParams{
371 Rule: android.CpExecutable,
372 Description: "prebuilt",
373 Output: outputFile,
374 Input: in,
375 })
376 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000377 }
378 return nil
379}
380
Inseob Kim1042d292020-06-01 23:23:05 +0900381func (p *prebuiltObjectLinker) object() bool {
382 return true
383}
384
Colin Cross7cabd422021-06-25 14:21:04 -0700385func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
386 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000387 prebuilt := &prebuiltObjectLinker{
388 objectLinker: objectLinker{
389 baseLinker: NewBaseLinker(nil),
390 },
391 }
392 module.linker = prebuilt
393 module.AddProperties(&prebuilt.properties)
394 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000395 return module
396}
397
Colin Crossc5075e92022-12-05 16:46:39 -0800398func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700399 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000400 return module.Init()
401}
402
Colin Crossde89fb82017-03-17 13:28:06 -0700403type prebuiltBinaryLinker struct {
404 *binaryDecorator
405 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100406
407 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700408}
409
410var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
411
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100412func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
413 return p.toolPath
414}
415
Colin Crossde89fb82017-03-17 13:28:06 -0700416func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
417 flags Flags, deps PathDeps, objs Objects) android.Path {
418 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700419 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700420 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
421 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100422 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700423 p.unstrippedOutputFile = in
424
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100425 if ctx.Host() {
426 // Host binaries are symlinked to their prebuilt source locations. That
427 // way they are executed directly from there so the linker resolves their
428 // shared library dependencies relative to that location (using
429 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
430 // repository can supply the expected versions of the shared libraries
431 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700432
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100433 // These shared lib paths may point to copies of the libs in
434 // .intermediates, which isn't where the binary will load them from, but
435 // it's fine for dependency tracking. If a library dependency is updated,
436 // the symlink will get a new timestamp, along with any installed symlinks
437 // handled in make.
438 sharedLibPaths := deps.EarlySharedLibs
439 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
440 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
441
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100442 var fromPath = in.String()
443 if !filepath.IsAbs(fromPath) {
444 fromPath = "$$PWD/" + fromPath
445 }
446
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100447 ctx.Build(pctx, android.BuildParams{
448 Rule: android.Symlink,
449 Output: outputFile,
450 Input: in,
451 Implicits: sharedLibPaths,
452 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100453 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100454 },
455 })
456
457 p.toolPath = android.OptionalPathForPath(outputFile)
458 } else {
459 if p.stripper.NeedsStrip(ctx) {
460 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
461 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
462 in = stripped
463 }
464
465 // Copy binaries to a name matching the final installed name
466 ctx.Build(pctx, android.BuildParams{
467 Rule: android.CpExecutable,
468 Description: "prebuilt",
469 Output: outputFile,
470 Input: in,
471 })
472 }
Colin Cross94921e72017-08-08 16:20:15 -0700473
474 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700475 }
476
477 return nil
478}
479
Inseob Kim7f283f42020-06-01 21:53:49 +0900480func (p *prebuiltBinaryLinker) binary() bool {
481 return true
482}
483
Patrice Arruda3554a982019-03-27 19:09:10 -0700484// 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 +0000485// device's directory, for both the host and device
486func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700487 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
488 return module.Init()
489}
490
491func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800492 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700493 module.compiler = nil
494
495 prebuilt := &prebuiltBinaryLinker{
496 binaryDecorator: binary,
497 }
498 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100499 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700500
Colin Cross74d73e22017-08-02 11:05:49 -0700501 module.AddProperties(&prebuilt.properties)
502
503 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700504 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700505}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700506
507type Sanitized struct {
508 None struct {
509 Srcs []string `android:"path,arch_variant"`
510 } `android:"arch_variant"`
511 Address struct {
512 Srcs []string `android:"path,arch_variant"`
513 } `android:"arch_variant"`
514 Hwaddress struct {
515 Srcs []string `android:"path,arch_variant"`
516 } `android:"arch_variant"`
517}
518
519func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
520 if sanitize == nil {
521 return nil
522 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400523 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700524 return sanitized.Address.Srcs
525 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400526 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700527 return sanitized.Hwaddress.Srcs
528 }
529 return sanitized.None.Srcs
530}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000531
532func (p *prebuiltLinker) sourceModuleName() string {
533 return proptools.String(p.properties.Source_module_name)
534}