blob: 96a07bc3575a33dfa84a0078ad1fba3035fbbd22 [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
Colin Crossa14fb6a2024-10-23 16:57:06 -070020 "github.com/google/blueprint/depset"
Spandan Das2b6dfb52024-01-19 00:22:22 +000021 "github.com/google/blueprint/proptools"
22
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000023 "android/soong/android"
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)
Colin Crossc5075e92022-12-05 16:46:39 -080035 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000036 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
39type prebuiltLinkerInterface interface {
40 Name(string) string
41 prebuilt() *android.Prebuilt
Spandan Das2b6dfb52024-01-19 00:22:22 +000042 sourceModuleName() string
Colin Crossce75d2c2016-10-06 16:12:58 -070043}
44
Patrice Arruda3554a982019-03-27 19:09:10 -070045type prebuiltLinkerProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +000046 // Name of the source soong module that gets shadowed by this prebuilt
47 // If unspecified, follows the naming convention that the source module of
48 // the prebuilt is Name() without "prebuilt_" prefix
49 Source_module_name *string
50
Patrice Arruda3554a982019-03-27 19:09:10 -070051 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
Cole Faust96a692b2024-08-08 14:47:51 -070052 Srcs proptools.Configurable[[]string] `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070053
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070054 Sanitized Sanitized `android:"arch_variant"`
55
Patrice Arruda3554a982019-03-27 19:09:10 -070056 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
57 // symbols, etc), default true.
58 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080059
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010060 // if set, add an extra objcopy --prefix-symbols= step
61 Prefix_symbols *string
62
Yo Chianga3ad9b22020-03-18 14:19:07 +080063 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
64 // This is needed only if this library is linked by other modules in build time.
65 // Only makes sense for the Windows target.
66 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070067}
68
Colin Crossde89fb82017-03-17 13:28:06 -070069type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070070 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080071
Patrice Arruda3554a982019-03-27 19:09:10 -070072 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070073}
74
Colin Crossde89fb82017-03-17 13:28:06 -070075func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070076 return &p.Prebuilt
77}
78
Colin Cross33b2fb72019-05-14 14:07:01 -070079type prebuiltLibraryInterface interface {
80 libraryInterface
81 prebuiltLinkerInterface
82 disablePrebuilt()
83}
84
Colin Crossde89fb82017-03-17 13:28:06 -070085type prebuiltLibraryLinker struct {
86 *libraryDecorator
87 prebuiltLinker
88}
89
90var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070091var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070092
Yi Kong00981662018-08-13 16:02:49 -070093func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
94
95func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080096 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070097}
98
Yi Kong00981662018-08-13 16:02:49 -070099func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
100 return p.libraryDecorator.linkerProps()
101}
102
Colin Crossce75d2c2016-10-06 16:12:58 -0700103func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700104 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000105
Colin Cross0de8a1e2020-09-18 14:15:30 -0700106 p.libraryDecorator.flagExporter.exportIncludes(ctx)
107 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
108 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
109 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
110 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
111 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
112
113 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000114
Colin Crossce75d2c2016-10-06 16:12:58 -0700115 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700116 srcs := p.prebuiltSrcs(ctx)
Spandan Das357ffcc2024-07-24 18:07:48 +0000117 stubInfo := addStubDependencyProviders(ctx)
118
119 // Stub variants will create a stub .so file from stub .c files
120 if p.buildStubs() && objs.objFiles != nil {
121 // TODO (b/275273834): Make objs.objFiles == nil a hard error when the symbol files have been added to module sdk.
122 return p.linkShared(ctx, flags, deps, objs)
123 }
124
Paul Duffinbce90da2020-03-12 20:17:14 +0000125 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000126 if len(srcs) > 1 {
127 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
128 return nil
129 }
130
Jiyong Park892a98f2020-12-14 09:20:00 +0900131 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
132
Paul Duffinbce90da2020-03-12 20:17:14 +0000133 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700134
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100135 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
136 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
137 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
138 in, flagsToBuilderFlags(flags), prefixed)
139 in = prefixed
140 }
141
Yo Chianga3ad9b22020-03-18 14:19:07 +0800142 if p.static() {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700143 depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(in).Build()
Colin Cross40213022023-12-13 15:19:49 -0800144 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700145 StaticLibrary: in,
146
147 TransitiveStaticLibrariesForOrdering: depSet,
148 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800149 return in
150 }
151
Colin Cross88f6fef2018-09-05 14:20:03 -0700152 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700153 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700154 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800155 outputFile := android.PathForModuleOut(ctx, libName)
156 var implicits android.Paths
157
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200158 if p.stripper.NeedsStrip(ctx) {
159 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700160 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200161 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700162 in = stripped
163 }
164
Colin Crossb60190a2018-09-04 16:28:17 -0700165 // Optimize out relinking against shared libraries whose interface hasn't changed by
166 // depending on a table of contents file instead of the library itself.
167 tocFile := android.PathForModuleOut(ctx, libName+".toc")
168 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400169 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700170
Yo Chianga3ad9b22020-03-18 14:19:07 +0800171 if ctx.Windows() && p.properties.Windows_import_lib != nil {
172 // Consumers of this library actually links to the import library in build
173 // time and dynamically links to the DLL in run time. i.e.
174 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
175 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
176 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
177 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
178 implicits = append(implicits, importLibOutputFile)
179
180 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400181 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800182 Description: "prebuilt import library",
183 Input: importLibSrc,
184 Output: importLibOutputFile,
185 Args: map[string]string{
186 "cpFlags": "-L",
187 },
188 })
189 }
190
191 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400192 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800193 Description: "prebuilt shared library",
194 Implicits: implicits,
195 Input: in,
196 Output: outputFile,
197 Args: map[string]string{
198 "cpFlags": "-L",
199 },
200 })
201
Colin Cross40213022023-12-13 15:19:49 -0800202 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400203 SharedLibrary: outputFile,
204 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700205
206 TableOfContents: p.tocFile,
Colin Crossb614cd42024-10-11 12:52:21 -0700207 IsStubs: p.buildStubs(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700208 })
209
Yo Chianga3ad9b22020-03-18 14:19:07 +0800210 return outputFile
211 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000212 } else if p.shared() && len(stubInfo) > 0 {
213 // This is a prebuilt which does not have any implementation (nil `srcs`), but provides APIs.
214 // Provide the latest (i.e. `current`) stubs to reverse dependencies.
215 latestStub := stubInfo[len(stubInfo)-1].SharedLibraryInfo.SharedLibrary
216 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
217 SharedLibrary: latestStub,
218 Target: ctx.Target(),
Colin Crossb614cd42024-10-11 12:52:21 -0700219 IsStubs: true,
Spandan Das357ffcc2024-07-24 18:07:48 +0000220 })
221
222 return latestStub
Colin Crossce75d2c2016-10-06 16:12:58 -0700223 }
224
Colin Cross649d8172020-12-10 12:30:21 -0800225 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800226 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800227
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000228 // Need to return an output path so that the AndroidMk logic doesn't skip
229 // the prebuilt header. For compatibility, in case Android.mk files use a
230 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
231 // placeholder, just like non-prebuilt header modules do in linkStatic().
232 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
233 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
234 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800235 }
236
Colin Crossce75d2c2016-10-06 16:12:58 -0700237 return nil
238}
239
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700240func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
241 sanitize := ctx.Module().(*Module).sanitize
Cole Faust96a692b2024-08-08 14:47:51 -0700242 srcs := p.properties.Srcs.GetOrDefault(ctx, nil)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700243 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000244 if p.static() {
Cole Faust96a692b2024-08-08 14:47:51 -0700245 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700246 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000247 }
248 if p.shared() {
Cole Faust96a692b2024-08-08 14:47:51 -0700249 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700250 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000251 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000252 return srcs
253}
254
Jiyong Park379de2f2018-12-19 02:47:14 +0900255func (p *prebuiltLibraryLinker) shared() bool {
256 return p.libraryDecorator.shared()
257}
258
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700259func (p *prebuiltLibraryLinker) nativeCoverage() bool {
260 return false
261}
262
Colin Cross33b2fb72019-05-14 14:07:01 -0700263func (p *prebuiltLibraryLinker) disablePrebuilt() {
Cole Faust96a692b2024-08-08 14:47:51 -0700264 p.properties.Srcs = proptools.NewConfigurable[[]string](nil, nil)
Ryan Prichard21e2c102024-02-28 18:59:30 -0800265 p.properties.Sanitized.None.Srcs = nil
266 p.properties.Sanitized.Address.Srcs = nil
267 p.properties.Sanitized.Hwaddress.Srcs = nil
Colin Cross33b2fb72019-05-14 14:07:01 -0700268}
269
Jiyong Park892a98f2020-12-14 09:20:00 +0900270// Implements versionedInterface
271func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100272 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900273}
274
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000275func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700276 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700277
278 prebuilt := &prebuiltLibraryLinker{
279 libraryDecorator: library,
280 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000281 module.compiler = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700282 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700283 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700284
Colin Cross74d73e22017-08-02 11:05:49 -0700285 module.AddProperties(&prebuilt.properties)
286
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000287 if srcsProperty == "" {
288 android.InitPrebuiltModuleWithoutSrcs(module)
289 } else {
290 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
291 return prebuilt.prebuiltSrcs(ctx)
292 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000293
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000294 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
295 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900296
Paul Duffinac6e6082019-12-11 15:22:32 +0000297 return module, library
298}
299
Spandan Das357ffcc2024-07-24 18:07:48 +0000300func (p *prebuiltLibraryLinker) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
301 if p.buildStubs() && p.stubsVersion() != "" {
302 return p.compileModuleLibApiStubs(ctx, flags, deps)
303 }
304 return Objects{}
305}
306
Paul Duffinbce90da2020-03-12 20:17:14 +0000307// cc_prebuilt_library installs a precompiled shared library that are
308// listed in the srcs property in the device's directory.
309func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000310 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000311
312 // Prebuilt shared libraries can be included in APEXes
313 android.InitApexModule(module)
314
315 return module.Init()
316}
317
Paul Duffinac6e6082019-12-11 15:22:32 +0000318// cc_prebuilt_library_shared installs a precompiled shared library that are
319// listed in the srcs property in the device's directory.
320func PrebuiltSharedLibraryFactory() android.Module {
321 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
322 return module.Init()
323}
324
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400325// cc_prebuilt_test_library_shared installs a precompiled shared library
326// to be used as a data dependency of a test-related module (such as cc_test, or
327// cc_test_library).
328func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000329 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400330 library.BuildOnlyShared()
331 library.baseInstaller = NewTestInstaller()
332 return module.Init()
333}
334
Paul Duffinac6e6082019-12-11 15:22:32 +0000335func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000336 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000337 library.BuildOnlyShared()
338
339 // Prebuilt shared libraries can be included in APEXes
340 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900341
Leo Li74f7b972017-05-17 11:30:45 -0700342 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700343}
344
Patrice Arruda3554a982019-03-27 19:09:10 -0700345// cc_prebuilt_library_static installs a precompiled static library that are
346// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900347func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700348 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
349 return module.Init()
350}
351
352func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000353 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700354 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000355
Leo Li74f7b972017-05-17 11:30:45 -0700356 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700357}
358
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000359type prebuiltObjectProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +0000360 // Name of the source soong module that gets shadowed by this prebuilt
361 // If unspecified, follows the naming convention that the source module of
362 // the prebuilt is Name() without "prebuilt_" prefix
363 Source_module_name *string
364 Srcs []string `android:"path,arch_variant"`
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000365}
366
367type prebuiltObjectLinker struct {
368 android.Prebuilt
369 objectLinker
370
371 properties prebuiltObjectProperties
372}
373
374func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
375 return &p.Prebuilt
376}
377
Spandan Das2b6dfb52024-01-19 00:22:22 +0000378func (p *prebuiltObjectLinker) sourceModuleName() string {
379 return proptools.String(p.properties.Source_module_name)
380}
381
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000382var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
383
384func (p *prebuiltObjectLinker) link(ctx ModuleContext,
385 flags Flags, deps PathDeps, objs Objects) android.Path {
386 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700387 // Copy objects to a name matching the final installed name
388 in := p.Prebuilt.SingleSourcePath(ctx)
389 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
390 ctx.Build(pctx, android.BuildParams{
391 Rule: android.CpExecutable,
392 Description: "prebuilt",
393 Output: outputFile,
394 Input: in,
395 })
396 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000397 }
398 return nil
399}
400
Inseob Kim1042d292020-06-01 23:23:05 +0900401func (p *prebuiltObjectLinker) object() bool {
402 return true
403}
404
Colin Cross7cabd422021-06-25 14:21:04 -0700405func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
406 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000407 prebuilt := &prebuiltObjectLinker{
408 objectLinker: objectLinker{
409 baseLinker: NewBaseLinker(nil),
410 },
411 }
412 module.linker = prebuilt
413 module.AddProperties(&prebuilt.properties)
414 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000415 return module
416}
417
Colin Crossc5075e92022-12-05 16:46:39 -0800418func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700419 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000420 return module.Init()
421}
422
Colin Crossde89fb82017-03-17 13:28:06 -0700423type prebuiltBinaryLinker struct {
424 *binaryDecorator
425 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100426
427 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700428}
429
430var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
431
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100432func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
433 return p.toolPath
434}
435
Colin Crossde89fb82017-03-17 13:28:06 -0700436func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
437 flags Flags, deps PathDeps, objs Objects) android.Path {
438 // TODO(ccross): verify shared library dependencies
Cole Faust96a692b2024-08-08 14:47:51 -0700439 if len(p.properties.Srcs.GetOrDefault(ctx, nil)) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700440 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
441 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100442 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700443 p.unstrippedOutputFile = in
444
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100445 if ctx.Host() {
446 // Host binaries are symlinked to their prebuilt source locations. That
447 // way they are executed directly from there so the linker resolves their
448 // shared library dependencies relative to that location (using
449 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
450 // repository can supply the expected versions of the shared libraries
451 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700452
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100453 // These shared lib paths may point to copies of the libs in
454 // .intermediates, which isn't where the binary will load them from, but
455 // it's fine for dependency tracking. If a library dependency is updated,
456 // the symlink will get a new timestamp, along with any installed symlinks
457 // handled in make.
458 sharedLibPaths := deps.EarlySharedLibs
459 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
460 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
461
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100462 var fromPath = in.String()
463 if !filepath.IsAbs(fromPath) {
464 fromPath = "$$PWD/" + fromPath
465 }
466
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100467 ctx.Build(pctx, android.BuildParams{
468 Rule: android.Symlink,
469 Output: outputFile,
470 Input: in,
471 Implicits: sharedLibPaths,
472 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100473 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100474 },
475 })
476
477 p.toolPath = android.OptionalPathForPath(outputFile)
478 } else {
479 if p.stripper.NeedsStrip(ctx) {
480 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
481 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
482 in = stripped
483 }
484
485 // Copy binaries to a name matching the final installed name
486 ctx.Build(pctx, android.BuildParams{
487 Rule: android.CpExecutable,
488 Description: "prebuilt",
489 Output: outputFile,
490 Input: in,
491 })
492 }
Colin Cross94921e72017-08-08 16:20:15 -0700493
494 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700495 }
496
497 return nil
498}
499
Inseob Kim7f283f42020-06-01 21:53:49 +0900500func (p *prebuiltBinaryLinker) binary() bool {
501 return true
502}
503
Patrice Arruda3554a982019-03-27 19:09:10 -0700504// 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 +0000505// device's directory, for both the host and device
506func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700507 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
508 return module.Init()
509}
510
511func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800512 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700513 module.compiler = nil
514
515 prebuilt := &prebuiltBinaryLinker{
516 binaryDecorator: binary,
517 }
518 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100519 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700520
Colin Cross74d73e22017-08-02 11:05:49 -0700521 module.AddProperties(&prebuilt.properties)
522
Cole Faust96a692b2024-08-08 14:47:51 -0700523 android.InitConfigurablePrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700524 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700525}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700526
527type Sanitized struct {
528 None struct {
529 Srcs []string `android:"path,arch_variant"`
530 } `android:"arch_variant"`
531 Address struct {
532 Srcs []string `android:"path,arch_variant"`
533 } `android:"arch_variant"`
534 Hwaddress struct {
535 Srcs []string `android:"path,arch_variant"`
536 } `android:"arch_variant"`
537}
538
539func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
540 if sanitize == nil {
541 return nil
542 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400543 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700544 return sanitized.Address.Srcs
545 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400546 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700547 return sanitized.Hwaddress.Srcs
548 }
549 return sanitized.None.Srcs
550}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000551
552func (p *prebuiltLinker) sourceModuleName() string {
553 return proptools.String(p.properties.Source_module_name)
554}