blob: f21e4a943b4de0acd12dc37c458fe0db547cb878 [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 (
18 "android/soong/android"
Martin Stjernholm14ee8322020-09-21 21:45:49 +010019 "path/filepath"
Colin Crossce75d2c2016-10-06 16:12:58 -070020)
21
22func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000023 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
24}
25
26func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000027 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000028 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
29 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Chris Parsons1f6d90f2020-06-17 16:10:42 -040030 ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000031 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000032 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070033}
34
35type prebuiltLinkerInterface interface {
36 Name(string) string
37 prebuilt() *android.Prebuilt
38}
39
Patrice Arruda3554a982019-03-27 19:09:10 -070040type prebuiltLinkerProperties struct {
41
42 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
43 Srcs []string `android:"path,arch_variant"`
44
45 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
46 // symbols, etc), default true.
47 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080048
49 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
50 // This is needed only if this library is linked by other modules in build time.
51 // Only makes sense for the Windows target.
52 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070053}
54
Colin Crossde89fb82017-03-17 13:28:06 -070055type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070056 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080057
Patrice Arruda3554a982019-03-27 19:09:10 -070058 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070059}
60
Colin Crossde89fb82017-03-17 13:28:06 -070061func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070062 return &p.Prebuilt
63}
64
Colin Cross74d73e22017-08-02 11:05:49 -070065func (p *prebuiltLinker) PrebuiltSrcs() []string {
66 return p.properties.Srcs
67}
68
Colin Cross33b2fb72019-05-14 14:07:01 -070069type prebuiltLibraryInterface interface {
70 libraryInterface
71 prebuiltLinkerInterface
72 disablePrebuilt()
73}
74
Colin Crossde89fb82017-03-17 13:28:06 -070075type prebuiltLibraryLinker struct {
76 *libraryDecorator
77 prebuiltLinker
78}
79
80var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070081var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070082
Yi Kong00981662018-08-13 16:02:49 -070083func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
84
85func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080086 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070087}
88
89func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070090 return flags
Yi Kong00981662018-08-13 16:02:49 -070091}
92
93func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
94 return p.libraryDecorator.linkerProps()
95}
96
Colin Crossce75d2c2016-10-06 16:12:58 -070097func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070098 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +000099
Colin Cross0de8a1e2020-09-18 14:15:30 -0700100 p.libraryDecorator.flagExporter.exportIncludes(ctx)
101 p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...)
102 p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...)
103 p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...)
104 p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...)
105 p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
106
107 p.libraryDecorator.flagExporter.setProvider(ctx)
Paul Duffinf5ea9e12020-02-21 10:57:00 +0000108
Colin Crossce75d2c2016-10-06 16:12:58 -0700109 // TODO(ccross): verify shared library dependencies
Paul Duffinbce90da2020-03-12 20:17:14 +0000110 srcs := p.prebuiltSrcs()
111 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700112 builderFlags := flagsToBuilderFlags(flags)
113
Paul Duffinbce90da2020-03-12 20:17:14 +0000114 if len(srcs) > 1 {
115 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
116 return nil
117 }
118
119 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700120
Yo Chianga3ad9b22020-03-18 14:19:07 +0800121 if p.static() {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700122 depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build()
123 ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{
124 StaticLibrary: in,
125
126 TransitiveStaticLibrariesForOrdering: depSet,
127 })
Yo Chianga3ad9b22020-03-18 14:19:07 +0800128 return in
129 }
130
Colin Cross88f6fef2018-09-05 14:20:03 -0700131 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700132 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700133 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800134 outputFile := android.PathForModuleOut(ctx, libName)
135 var implicits android.Paths
136
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200137 if p.stripper.NeedsStrip(ctx) {
138 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700139 stripped := android.PathForModuleOut(ctx, "stripped", libName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200140 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700141 in = stripped
142 }
143
Colin Crossb60190a2018-09-04 16:28:17 -0700144 // Optimize out relinking against shared libraries whose interface hasn't changed by
145 // depending on a table of contents file instead of the library itself.
146 tocFile := android.PathForModuleOut(ctx, libName+".toc")
147 p.tocFile = android.OptionalPathForPath(tocFile)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800148 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700149
Yo Chianga3ad9b22020-03-18 14:19:07 +0800150 if ctx.Windows() && p.properties.Windows_import_lib != nil {
151 // Consumers of this library actually links to the import library in build
152 // time and dynamically links to the DLL in run time. i.e.
153 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
154 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
155 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
156 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
157 implicits = append(implicits, importLibOutputFile)
158
159 ctx.Build(pctx, android.BuildParams{
160 Rule: android.Cp,
161 Description: "prebuilt import library",
162 Input: importLibSrc,
163 Output: importLibOutputFile,
164 Args: map[string]string{
165 "cpFlags": "-L",
166 },
167 })
168 }
169
170 ctx.Build(pctx, android.BuildParams{
171 Rule: android.Cp,
172 Description: "prebuilt shared library",
173 Implicits: implicits,
174 Input: in,
175 Output: outputFile,
176 Args: map[string]string{
177 "cpFlags": "-L",
178 },
179 })
180
Colin Cross0de8a1e2020-09-18 14:15:30 -0700181 ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{
182 SharedLibrary: outputFile,
183 UnstrippedSharedLibrary: p.unstrippedOutputFile,
184
185 TableOfContents: p.tocFile,
186 })
187
Yo Chianga3ad9b22020-03-18 14:19:07 +0800188 return outputFile
189 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700190 }
191
192 return nil
193}
194
Paul Duffinbce90da2020-03-12 20:17:14 +0000195func (p *prebuiltLibraryLinker) prebuiltSrcs() []string {
196 srcs := p.properties.Srcs
197 if p.static() {
198 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
199 }
200 if p.shared() {
201 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
202 }
203
204 return srcs
205}
206
Jiyong Park379de2f2018-12-19 02:47:14 +0900207func (p *prebuiltLibraryLinker) shared() bool {
208 return p.libraryDecorator.shared()
209}
210
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700211func (p *prebuiltLibraryLinker) nativeCoverage() bool {
212 return false
213}
214
Colin Cross33b2fb72019-05-14 14:07:01 -0700215func (p *prebuiltLibraryLinker) disablePrebuilt() {
216 p.properties.Srcs = nil
217}
218
Paul Duffinac6e6082019-12-11 15:22:32 +0000219func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700220 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700221 module.compiler = nil
222
223 prebuilt := &prebuiltLibraryLinker{
224 libraryDecorator: library,
225 }
226 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700227
Colin Cross74d73e22017-08-02 11:05:49 -0700228 module.AddProperties(&prebuilt.properties)
229
Paul Duffinbce90da2020-03-12 20:17:14 +0000230 srcsSupplier := func() []string {
231 return prebuilt.prebuiltSrcs()
232 }
233
234 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900235
Paul Duffinac6e6082019-12-11 15:22:32 +0000236 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900237 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000238 return module, library
239}
240
Paul Duffinbce90da2020-03-12 20:17:14 +0000241// cc_prebuilt_library installs a precompiled shared library that are
242// listed in the srcs property in the device's directory.
243func PrebuiltLibraryFactory() android.Module {
244 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
245
246 // Prebuilt shared libraries can be included in APEXes
247 android.InitApexModule(module)
248
249 return module.Init()
250}
251
Paul Duffinac6e6082019-12-11 15:22:32 +0000252// cc_prebuilt_library_shared installs a precompiled shared library that are
253// listed in the srcs property in the device's directory.
254func PrebuiltSharedLibraryFactory() android.Module {
255 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
256 return module.Init()
257}
258
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400259// cc_prebuilt_test_library_shared installs a precompiled shared library
260// to be used as a data dependency of a test-related module (such as cc_test, or
261// cc_test_library).
262func PrebuiltSharedTestLibraryFactory() android.Module {
263 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
264 library.BuildOnlyShared()
265 library.baseInstaller = NewTestInstaller()
266 return module.Init()
267}
268
Paul Duffinac6e6082019-12-11 15:22:32 +0000269func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
270 module, library := NewPrebuiltLibrary(hod)
271 library.BuildOnlyShared()
272
273 // Prebuilt shared libraries can be included in APEXes
274 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900275
Leo Li74f7b972017-05-17 11:30:45 -0700276 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700277}
278
Patrice Arruda3554a982019-03-27 19:09:10 -0700279// cc_prebuilt_library_static installs a precompiled static library that are
280// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900281func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700282 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
283 return module.Init()
284}
285
286func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000287 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700288 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700289 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700290}
291
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000292type prebuiltObjectProperties struct {
293 Srcs []string `android:"path,arch_variant"`
294}
295
296type prebuiltObjectLinker struct {
297 android.Prebuilt
298 objectLinker
299
300 properties prebuiltObjectProperties
301}
302
303func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
304 return &p.Prebuilt
305}
306
307var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
308
309func (p *prebuiltObjectLinker) link(ctx ModuleContext,
310 flags Flags, deps PathDeps, objs Objects) android.Path {
311 if len(p.properties.Srcs) > 0 {
312 return p.Prebuilt.SingleSourcePath(ctx)
313 }
314 return nil
315}
316
Inseob Kim1042d292020-06-01 23:23:05 +0900317func (p *prebuiltObjectLinker) object() bool {
318 return true
319}
320
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000321func newPrebuiltObject() *Module {
322 module := newObject()
323 prebuilt := &prebuiltObjectLinker{
324 objectLinker: objectLinker{
325 baseLinker: NewBaseLinker(nil),
326 },
327 }
328 module.linker = prebuilt
329 module.AddProperties(&prebuilt.properties)
330 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
331 android.InitSdkAwareModule(module)
332 return module
333}
334
335func prebuiltObjectFactory() android.Module {
336 module := newPrebuiltObject()
337 return module.Init()
338}
339
Colin Crossde89fb82017-03-17 13:28:06 -0700340type prebuiltBinaryLinker struct {
341 *binaryDecorator
342 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100343
344 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700345}
346
347var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
348
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100349func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
350 return p.toolPath
351}
352
Colin Crossde89fb82017-03-17 13:28:06 -0700353func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
354 flags Flags, deps PathDeps, objs Objects) android.Path {
355 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700356 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700357 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
358 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100359 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700360 p.unstrippedOutputFile = in
361
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100362 if ctx.Host() {
363 // Host binaries are symlinked to their prebuilt source locations. That
364 // way they are executed directly from there so the linker resolves their
365 // shared library dependencies relative to that location (using
366 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
367 // repository can supply the expected versions of the shared libraries
368 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700369
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100370 // These shared lib paths may point to copies of the libs in
371 // .intermediates, which isn't where the binary will load them from, but
372 // it's fine for dependency tracking. If a library dependency is updated,
373 // the symlink will get a new timestamp, along with any installed symlinks
374 // handled in make.
375 sharedLibPaths := deps.EarlySharedLibs
376 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
377 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
378
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100379 var fromPath = in.String()
380 if !filepath.IsAbs(fromPath) {
381 fromPath = "$$PWD/" + fromPath
382 }
383
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100384 ctx.Build(pctx, android.BuildParams{
385 Rule: android.Symlink,
386 Output: outputFile,
387 Input: in,
388 Implicits: sharedLibPaths,
389 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100390 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100391 },
392 })
393
394 p.toolPath = android.OptionalPathForPath(outputFile)
395 } else {
396 if p.stripper.NeedsStrip(ctx) {
397 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
398 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
399 in = stripped
400 }
401
402 // Copy binaries to a name matching the final installed name
403 ctx.Build(pctx, android.BuildParams{
404 Rule: android.CpExecutable,
405 Description: "prebuilt",
406 Output: outputFile,
407 Input: in,
408 })
409 }
Colin Cross94921e72017-08-08 16:20:15 -0700410
411 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700412 }
413
414 return nil
415}
416
Inseob Kim7f283f42020-06-01 21:53:49 +0900417func (p *prebuiltBinaryLinker) binary() bool {
418 return true
419}
420
Patrice Arruda3554a982019-03-27 19:09:10 -0700421// cc_prebuilt_binary installs a precompiled executable in srcs property in the
422// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700423func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700424 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
425 return module.Init()
426}
427
428func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
429 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700430 module.compiler = nil
431
432 prebuilt := &prebuiltBinaryLinker{
433 binaryDecorator: binary,
434 }
435 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100436 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700437
Colin Cross74d73e22017-08-02 11:05:49 -0700438 module.AddProperties(&prebuilt.properties)
439
440 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700441 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700442}