blob: 7c87297099bf1ca4fb828013e6bbb890725a284a [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"
Spandan Das59223332024-08-15 21:54:09 +000019 "strings"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000020
Colin Crossa14fb6a2024-10-23 16:57:06 -070021 "github.com/google/blueprint/depset"
Spandan Das2b6dfb52024-01-19 00:22:22 +000022 "github.com/google/blueprint/proptools"
23
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000024 "android/soong/android"
Colin Crossce75d2c2016-10-06 16:12:58 -070025)
26
27func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000028 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
29}
30
31func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000032 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000033 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
34 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040035 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Colin Crossc5075e92022-12-05 16:46:39 -080036 ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +000037 ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070038}
39
40type prebuiltLinkerInterface interface {
41 Name(string) string
42 prebuilt() *android.Prebuilt
Spandan Das2b6dfb52024-01-19 00:22:22 +000043 sourceModuleName() string
Colin Crossce75d2c2016-10-06 16:12:58 -070044}
45
Patrice Arruda3554a982019-03-27 19:09:10 -070046type prebuiltLinkerProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +000047 // Name of the source soong module that gets shadowed by this prebuilt
48 // If unspecified, follows the naming convention that the source module of
49 // the prebuilt is Name() without "prebuilt_" prefix
50 Source_module_name *string
51
Patrice Arruda3554a982019-03-27 19:09:10 -070052 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
Cole Faust96a692b2024-08-08 14:47:51 -070053 Srcs proptools.Configurable[[]string] `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070054
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070055 Sanitized Sanitized `android:"arch_variant"`
56
Patrice Arruda3554a982019-03-27 19:09:10 -070057 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
58 // symbols, etc), default true.
59 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080060
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +010061 // if set, add an extra objcopy --prefix-symbols= step
62 Prefix_symbols *string
63
Yo Chianga3ad9b22020-03-18 14:19:07 +080064 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
65 // This is needed only if this library is linked by other modules in build time.
66 // Only makes sense for the Windows target.
67 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070068}
69
Colin Crossde89fb82017-03-17 13:28:06 -070070type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070071 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080072
Patrice Arruda3554a982019-03-27 19:09:10 -070073 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070074}
75
Colin Crossde89fb82017-03-17 13:28:06 -070076func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070077 return &p.Prebuilt
78}
79
Colin Cross33b2fb72019-05-14 14:07:01 -070080type prebuiltLibraryInterface interface {
81 libraryInterface
82 prebuiltLinkerInterface
83 disablePrebuilt()
84}
85
Colin Crossde89fb82017-03-17 13:28:06 -070086type prebuiltLibraryLinker struct {
87 *libraryDecorator
88 prebuiltLinker
89}
90
91var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070092var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070093
Yi Kong00981662018-08-13 16:02:49 -070094func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
95
96func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080097 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070098}
99
Yi Kong00981662018-08-13 16:02:49 -0700100func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
101 return p.libraryDecorator.linkerProps()
102}
103
Colin Crossce75d2c2016-10-06 16:12:58 -0700104func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700105 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000106
Colin Cross0de8a1e2020-09-18 14:15:30 -0700107 p.libraryDecorator.flagExporter.exportIncludes(ctx)
108 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
109 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
110 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
111 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
112 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
113
114 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000115
Colin Crossce75d2c2016-10-06 16:12:58 -0700116 // TODO(ccross): verify shared library dependencies
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700117 srcs := p.prebuiltSrcs(ctx)
Spandan Das357ffcc2024-07-24 18:07:48 +0000118 stubInfo := addStubDependencyProviders(ctx)
119
120 // Stub variants will create a stub .so file from stub .c files
121 if p.buildStubs() && objs.objFiles != nil {
122 // TODO (b/275273834): Make objs.objFiles == nil a hard error when the symbol files have been added to module sdk.
Spandan Das59223332024-08-15 21:54:09 +0000123
124 // The map.txt files of libclang_rt.* contain version information, but the checked in .so files do not.
125 // e.g. libclang_rt.* libs impl
126 // $ nm -D prebuilts/../libclang_rt.hwasan-aarch64-android.so
127 // __hwasan_init
128
129 // stubs generated from .map.txt
130 // $ nm -D out/soong/.intermediates/../<stubs>/libclang_rt.hwasan-aarch64-android.so
131 // __hwasan_init@@LIBCLANG_RT_ASAN
132
133 // Special-case libclang_rt.* libs to account for this discrepancy.
134 // TODO (spandandas): Remove this special case https://r.android.com/3236596 has been submitted, and a new set of map.txt
135 // files of libclang_rt.* libs have been generated.
136 if strings.Contains(ctx.ModuleName(), "libclang_rt.") {
137 p.versionScriptPath = android.OptionalPathForPath(nil)
138 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000139 return p.linkShared(ctx, flags, deps, objs)
140 }
141
Paul Duffinbce90da2020-03-12 20:17:14 +0000142 if len(srcs) > 0 {
Paul Duffinbce90da2020-03-12 20:17:14 +0000143 if len(srcs) > 1 {
144 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
145 return nil
146 }
147
Jiyong Park892a98f2020-12-14 09:20:00 +0900148 p.libraryDecorator.exportVersioningMacroIfNeeded(ctx)
149
Paul Duffinbce90da2020-03-12 20:17:14 +0000150 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700151
Pierre-Clément Tosi6f630ae2022-08-10 09:25:54 +0100152 if String(p.prebuiltLinker.properties.Prefix_symbols) != "" {
153 prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0])
154 transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols),
155 in, flagsToBuilderFlags(flags), prefixed)
156 in = prefixed
157 }
158
Yo Chianga3ad9b22020-03-18 14:19:07 +0800159 if p.static() {
Colin Crossa14fb6a2024-10-23 16:57:06 -0700160 depSet := depset.NewBuilder[android.Path](depset.TOPOLOGICAL).Direct(in).Build()
Colin Cross40213022023-12-13 15:19:49 -0800161 android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700162 StaticLibrary: in,
163
164 TransitiveStaticLibrariesForOrdering: depSet,
165 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800166 return in
167 }
168
Colin Cross88f6fef2018-09-05 14:20:03 -0700169 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700170 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700171 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800172 outputFile := android.PathForModuleOut(ctx, libName)
173 var implicits android.Paths
174
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200175 if p.stripper.NeedsStrip(ctx) {
176 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700177 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Thiébaud Weksteend4587452020-08-19 14:53:01 +0200178 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700179 in = stripped
180 }
181
Colin Crossb60190a2018-09-04 16:28:17 -0700182 // Optimize out relinking against shared libraries whose interface hasn't changed by
183 // depending on a table of contents file instead of the library itself.
184 tocFile := android.PathForModuleOut(ctx, libName+".toc")
185 p.tocFile = android.OptionalPathForPath(tocFile)
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400186 TransformSharedObjectToToc(ctx, outputFile, tocFile)
Colin Cross88f6fef2018-09-05 14:20:03 -0700187
Yo Chianga3ad9b22020-03-18 14:19:07 +0800188 if ctx.Windows() && p.properties.Windows_import_lib != nil {
189 // Consumers of this library actually links to the import library in build
190 // time and dynamically links to the DLL in run time. i.e.
191 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
192 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
193 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
194 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
195 implicits = append(implicits, importLibOutputFile)
196
197 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400198 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800199 Description: "prebuilt import library",
200 Input: importLibSrc,
201 Output: importLibOutputFile,
202 Args: map[string]string{
203 "cpFlags": "-L",
204 },
205 })
206 }
207
208 ctx.Build(pctx, android.BuildParams{
Alexander Koskovichc46c6d42023-08-12 23:09:00 -0400209 Rule: android.CpExecutable,
Yo Chianga3ad9b22020-03-18 14:19:07 +0800210 Description: "prebuilt shared library",
211 Implicits: implicits,
212 Input: in,
213 Output: outputFile,
214 Args: map[string]string{
215 "cpFlags": "-L",
216 },
217 })
218
Colin Cross40213022023-12-13 15:19:49 -0800219 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
Liz Kammeref6dfea2021-06-08 15:37:09 -0400220 SharedLibrary: outputFile,
221 Target: ctx.Target(),
Colin Cross0de8a1e2020-09-18 14:15:30 -0700222
223 TableOfContents: p.tocFile,
224 })
225
Yo Chianga3ad9b22020-03-18 14:19:07 +0800226 return outputFile
227 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000228 } else if p.shared() && len(stubInfo) > 0 {
229 // This is a prebuilt which does not have any implementation (nil `srcs`), but provides APIs.
230 // Provide the latest (i.e. `current`) stubs to reverse dependencies.
231 latestStub := stubInfo[len(stubInfo)-1].SharedLibraryInfo.SharedLibrary
232 android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{
233 SharedLibrary: latestStub,
234 Target: ctx.Target(),
235 })
236
237 return latestStub
Colin Crossce75d2c2016-10-06 16:12:58 -0700238 }
239
Colin Cross649d8172020-12-10 12:30:21 -0800240 if p.header() {
Colin Cross40213022023-12-13 15:19:49 -0800241 android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{})
Colin Cross649d8172020-12-10 12:30:21 -0800242
Martin Stjernholmd51cb5c2021-11-30 18:49:03 +0000243 // Need to return an output path so that the AndroidMk logic doesn't skip
244 // the prebuilt header. For compatibility, in case Android.mk files use a
245 // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as
246 // placeholder, just like non-prebuilt header modules do in linkStatic().
247 ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
248 transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil)
249 return ph
Colin Cross649d8172020-12-10 12:30:21 -0800250 }
251
Colin Crossce75d2c2016-10-06 16:12:58 -0700252 return nil
253}
254
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700255func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string {
256 sanitize := ctx.Module().(*Module).sanitize
Cole Faust96a692b2024-08-08 14:47:51 -0700257 srcs := p.properties.Srcs.GetOrDefault(ctx, nil)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700258 srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000259 if p.static() {
Cole Faust96a692b2024-08-08 14:47:51 -0700260 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700261 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000262 }
263 if p.shared() {
Cole Faust96a692b2024-08-08 14:47:51 -0700264 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs.GetOrDefault(ctx, nil)...)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700265 srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...)
Paul Duffinbce90da2020-03-12 20:17:14 +0000266 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000267 return srcs
268}
269
Jiyong Park379de2f2018-12-19 02:47:14 +0900270func (p *prebuiltLibraryLinker) shared() bool {
271 return p.libraryDecorator.shared()
272}
273
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700274func (p *prebuiltLibraryLinker) nativeCoverage() bool {
275 return false
276}
277
Colin Cross33b2fb72019-05-14 14:07:01 -0700278func (p *prebuiltLibraryLinker) disablePrebuilt() {
Cole Faust96a692b2024-08-08 14:47:51 -0700279 p.properties.Srcs = proptools.NewConfigurable[[]string](nil, nil)
Ryan Prichard21e2c102024-02-28 18:59:30 -0800280 p.properties.Sanitized.None.Srcs = nil
281 p.properties.Sanitized.Address.Srcs = nil
282 p.properties.Sanitized.Hwaddress.Srcs = nil
Colin Cross33b2fb72019-05-14 14:07:01 -0700283}
284
Jiyong Park892a98f2020-12-14 09:20:00 +0900285// Implements versionedInterface
286func (p *prebuiltLibraryLinker) implementationModuleName(name string) string {
Paul Duffin9804da02021-10-26 10:42:42 +0100287 return android.RemoveOptionalPrebuiltPrefix(name)
Jiyong Park892a98f2020-12-14 09:20:00 +0900288}
289
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000290func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700291 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700292
293 prebuilt := &prebuiltLibraryLinker{
294 libraryDecorator: library,
295 }
Spandan Das357ffcc2024-07-24 18:07:48 +0000296 module.compiler = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700297 module.linker = prebuilt
Colin Cross31076b32020-10-23 17:22:06 -0700298 module.library = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700299
Colin Cross74d73e22017-08-02 11:05:49 -0700300 module.AddProperties(&prebuilt.properties)
301
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000302 if srcsProperty == "" {
303 android.InitPrebuiltModuleWithoutSrcs(module)
304 } else {
305 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
306 return prebuilt.prebuiltSrcs(ctx)
307 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000308
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000309 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty)
310 }
Jiyong Park379de2f2018-12-19 02:47:14 +0900311
Paul Duffinac6e6082019-12-11 15:22:32 +0000312 return module, library
313}
314
Spandan Das357ffcc2024-07-24 18:07:48 +0000315func (p *prebuiltLibraryLinker) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
316 if p.buildStubs() && p.stubsVersion() != "" {
317 return p.compileModuleLibApiStubs(ctx, flags, deps)
318 }
319 return Objects{}
320}
321
Paul Duffinbce90da2020-03-12 20:17:14 +0000322// cc_prebuilt_library installs a precompiled shared library that are
323// listed in the srcs property in the device's directory.
324func PrebuiltLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000325 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Paul Duffinbce90da2020-03-12 20:17:14 +0000326
327 // Prebuilt shared libraries can be included in APEXes
328 android.InitApexModule(module)
329
330 return module.Init()
331}
332
Paul Duffinac6e6082019-12-11 15:22:32 +0000333// cc_prebuilt_library_shared installs a precompiled shared library that are
334// listed in the srcs property in the device's directory.
335func PrebuiltSharedLibraryFactory() android.Module {
336 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
337 return module.Init()
338}
339
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400340// cc_prebuilt_test_library_shared installs a precompiled shared library
341// to be used as a data dependency of a test-related module (such as cc_test, or
342// cc_test_library).
343func PrebuiltSharedTestLibraryFactory() android.Module {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000344 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs")
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400345 library.BuildOnlyShared()
346 library.baseInstaller = NewTestInstaller()
347 return module.Init()
348}
349
Paul Duffinac6e6082019-12-11 15:22:32 +0000350func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000351 module, library := NewPrebuiltLibrary(hod, "srcs")
Paul Duffinac6e6082019-12-11 15:22:32 +0000352 library.BuildOnlyShared()
353
354 // Prebuilt shared libraries can be included in APEXes
355 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900356
Leo Li74f7b972017-05-17 11:30:45 -0700357 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700358}
359
Patrice Arruda3554a982019-03-27 19:09:10 -0700360// cc_prebuilt_library_static installs a precompiled static library that are
361// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900362func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700363 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
364 return module.Init()
365}
366
367func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Martin Stjernholme65c3ae2021-11-23 01:24:06 +0000368 module, library := NewPrebuiltLibrary(hod, "srcs")
Colin Crossde89fb82017-03-17 13:28:06 -0700369 library.BuildOnlyStatic()
Trevor Radcliffe5d6fa4d2022-05-17 21:59:36 +0000370
Leo Li74f7b972017-05-17 11:30:45 -0700371 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700372}
373
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000374type prebuiltObjectProperties struct {
Spandan Das2b6dfb52024-01-19 00:22:22 +0000375 // Name of the source soong module that gets shadowed by this prebuilt
376 // If unspecified, follows the naming convention that the source module of
377 // the prebuilt is Name() without "prebuilt_" prefix
378 Source_module_name *string
379 Srcs []string `android:"path,arch_variant"`
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000380}
381
382type prebuiltObjectLinker struct {
383 android.Prebuilt
384 objectLinker
385
386 properties prebuiltObjectProperties
387}
388
389func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
390 return &p.Prebuilt
391}
392
Spandan Das2b6dfb52024-01-19 00:22:22 +0000393func (p *prebuiltObjectLinker) sourceModuleName() string {
394 return proptools.String(p.properties.Source_module_name)
395}
396
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000397var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
398
399func (p *prebuiltObjectLinker) link(ctx ModuleContext,
400 flags Flags, deps PathDeps, objs Objects) android.Path {
401 if len(p.properties.Srcs) > 0 {
Colin Crossee02aed2022-04-15 15:16:02 -0700402 // Copy objects to a name matching the final installed name
403 in := p.Prebuilt.SingleSourcePath(ctx)
404 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o")
405 ctx.Build(pctx, android.BuildParams{
406 Rule: android.CpExecutable,
407 Description: "prebuilt",
408 Output: outputFile,
409 Input: in,
410 })
411 return outputFile
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000412 }
413 return nil
414}
415
Inseob Kim1042d292020-06-01 23:23:05 +0900416func (p *prebuiltObjectLinker) object() bool {
417 return true
418}
419
Colin Cross7cabd422021-06-25 14:21:04 -0700420func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module {
421 module := newObject(hod)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000422 prebuilt := &prebuiltObjectLinker{
423 objectLinker: objectLinker{
424 baseLinker: NewBaseLinker(nil),
425 },
426 }
427 module.linker = prebuilt
428 module.AddProperties(&prebuilt.properties)
429 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000430 return module
431}
432
Colin Crossc5075e92022-12-05 16:46:39 -0800433func PrebuiltObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700434 module := NewPrebuiltObject(android.HostAndDeviceSupported)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000435 return module.Init()
436}
437
Colin Crossde89fb82017-03-17 13:28:06 -0700438type prebuiltBinaryLinker struct {
439 *binaryDecorator
440 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100441
442 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700443}
444
445var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
446
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100447func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
448 return p.toolPath
449}
450
Colin Crossde89fb82017-03-17 13:28:06 -0700451func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
452 flags Flags, deps PathDeps, objs Objects) android.Path {
453 // TODO(ccross): verify shared library dependencies
Cole Faust96a692b2024-08-08 14:47:51 -0700454 if len(p.properties.Srcs.GetOrDefault(ctx, nil)) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700455 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
456 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100457 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700458 p.unstrippedOutputFile = in
459
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100460 if ctx.Host() {
461 // Host binaries are symlinked to their prebuilt source locations. That
462 // way they are executed directly from there so the linker resolves their
463 // shared library dependencies relative to that location (using
464 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
465 // repository can supply the expected versions of the shared libraries
466 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700467
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100468 // These shared lib paths may point to copies of the libs in
469 // .intermediates, which isn't where the binary will load them from, but
470 // it's fine for dependency tracking. If a library dependency is updated,
471 // the symlink will get a new timestamp, along with any installed symlinks
472 // handled in make.
473 sharedLibPaths := deps.EarlySharedLibs
474 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
475 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
476
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100477 var fromPath = in.String()
478 if !filepath.IsAbs(fromPath) {
479 fromPath = "$$PWD/" + fromPath
480 }
481
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100482 ctx.Build(pctx, android.BuildParams{
483 Rule: android.Symlink,
484 Output: outputFile,
485 Input: in,
486 Implicits: sharedLibPaths,
487 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100488 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100489 },
490 })
491
492 p.toolPath = android.OptionalPathForPath(outputFile)
493 } else {
494 if p.stripper.NeedsStrip(ctx) {
495 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
496 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
497 in = stripped
498 }
499
500 // Copy binaries to a name matching the final installed name
501 ctx.Build(pctx, android.BuildParams{
502 Rule: android.CpExecutable,
503 Description: "prebuilt",
504 Output: outputFile,
505 Input: in,
506 })
507 }
Colin Cross94921e72017-08-08 16:20:15 -0700508
509 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700510 }
511
512 return nil
513}
514
Inseob Kim7f283f42020-06-01 21:53:49 +0900515func (p *prebuiltBinaryLinker) binary() bool {
516 return true
517}
518
Patrice Arruda3554a982019-03-27 19:09:10 -0700519// 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 +0000520// device's directory, for both the host and device
521func PrebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700522 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
523 return module.Init()
524}
525
526func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
Colin Cross8ff10582023-12-07 13:10:56 -0800527 module, binary := newBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700528 module.compiler = nil
529
530 prebuilt := &prebuiltBinaryLinker{
531 binaryDecorator: binary,
532 }
533 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100534 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700535
Colin Cross74d73e22017-08-02 11:05:49 -0700536 module.AddProperties(&prebuilt.properties)
537
Cole Faust96a692b2024-08-08 14:47:51 -0700538 android.InitConfigurablePrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700539 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700540}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700541
542type Sanitized struct {
543 None struct {
544 Srcs []string `android:"path,arch_variant"`
545 } `android:"arch_variant"`
546 Address struct {
547 Srcs []string `android:"path,arch_variant"`
548 } `android:"arch_variant"`
549 Hwaddress struct {
550 Srcs []string `android:"path,arch_variant"`
551 } `android:"arch_variant"`
552}
553
554func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string {
555 if sanitize == nil {
556 return nil
557 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400558 if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700559 return sanitized.Address.Srcs
560 }
Liz Kammer2c1d6aa2022-10-03 15:07:37 -0400561 if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700562 return sanitized.Hwaddress.Srcs
563 }
564 return sanitized.None.Srcs
565}
Spandan Das2b6dfb52024-01-19 00:22:22 +0000566
567func (p *prebuiltLinker) sourceModuleName() string {
568 return proptools.String(p.properties.Source_module_name)
569}