blob: 9d1b016085df656e02d77fa5ed42dc2e98f811b1 [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
100 p.libraryDecorator.exportIncludes(ctx)
101 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
102 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
103 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
104 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
105 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
106
Colin Crossce75d2c2016-10-06 16:12:58 -0700107 // TODO(ccross): verify shared library dependencies
Paul Duffinbce90da2020-03-12 20:17:14 +0000108 srcs := p.prebuiltSrcs()
109 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700110 builderFlags := flagsToBuilderFlags(flags)
111
Paul Duffinbce90da2020-03-12 20:17:14 +0000112 if len(srcs) > 1 {
113 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
114 return nil
115 }
116
117 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700118
Yo Chianga3ad9b22020-03-18 14:19:07 +0800119 if p.static() {
120 return in
121 }
122
Colin Cross88f6fef2018-09-05 14:20:03 -0700123 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700124 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700125 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800126 outputFile := android.PathForModuleOut(ctx, libName)
127 var implicits android.Paths
128
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200129 if p.stripper.NeedsStrip(ctx) {
130 stripFlags := flagsToStripFlags(flags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700131 stripped := android.PathForModuleOut(ctx, "stripped", libName)
ThiƩbaud Weksteend4587452020-08-19 14:53:01 +0200132 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700133 in = stripped
134 }
135
Colin Crossb60190a2018-09-04 16:28:17 -0700136 // Optimize out relinking against shared libraries whose interface hasn't changed by
137 // depending on a table of contents file instead of the library itself.
138 tocFile := android.PathForModuleOut(ctx, libName+".toc")
139 p.tocFile = android.OptionalPathForPath(tocFile)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800140 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700141
Yo Chianga3ad9b22020-03-18 14:19:07 +0800142 if ctx.Windows() && p.properties.Windows_import_lib != nil {
143 // Consumers of this library actually links to the import library in build
144 // time and dynamically links to the DLL in run time. i.e.
145 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
146 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
147 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
148 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
149 implicits = append(implicits, importLibOutputFile)
150
151 ctx.Build(pctx, android.BuildParams{
152 Rule: android.Cp,
153 Description: "prebuilt import library",
154 Input: importLibSrc,
155 Output: importLibOutputFile,
156 Args: map[string]string{
157 "cpFlags": "-L",
158 },
159 })
160 }
161
162 ctx.Build(pctx, android.BuildParams{
163 Rule: android.Cp,
164 Description: "prebuilt shared library",
165 Implicits: implicits,
166 Input: in,
167 Output: outputFile,
168 Args: map[string]string{
169 "cpFlags": "-L",
170 },
171 })
172
173 return outputFile
174 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700175 }
176
177 return nil
178}
179
Paul Duffinbce90da2020-03-12 20:17:14 +0000180func (p *prebuiltLibraryLinker) prebuiltSrcs() []string {
181 srcs := p.properties.Srcs
182 if p.static() {
183 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
184 }
185 if p.shared() {
186 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
187 }
188
189 return srcs
190}
191
Jiyong Park379de2f2018-12-19 02:47:14 +0900192func (p *prebuiltLibraryLinker) shared() bool {
193 return p.libraryDecorator.shared()
194}
195
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700196func (p *prebuiltLibraryLinker) nativeCoverage() bool {
197 return false
198}
199
Colin Cross33b2fb72019-05-14 14:07:01 -0700200func (p *prebuiltLibraryLinker) disablePrebuilt() {
201 p.properties.Srcs = nil
202}
203
Paul Duffinac6e6082019-12-11 15:22:32 +0000204func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700205 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700206 module.compiler = nil
207
208 prebuilt := &prebuiltLibraryLinker{
209 libraryDecorator: library,
210 }
211 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700212
Colin Cross74d73e22017-08-02 11:05:49 -0700213 module.AddProperties(&prebuilt.properties)
214
Paul Duffinbce90da2020-03-12 20:17:14 +0000215 srcsSupplier := func() []string {
216 return prebuilt.prebuiltSrcs()
217 }
218
219 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900220
Paul Duffinac6e6082019-12-11 15:22:32 +0000221 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900222 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000223 return module, library
224}
225
Paul Duffinbce90da2020-03-12 20:17:14 +0000226// cc_prebuilt_library installs a precompiled shared library that are
227// listed in the srcs property in the device's directory.
228func PrebuiltLibraryFactory() android.Module {
229 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
230
231 // Prebuilt shared libraries can be included in APEXes
232 android.InitApexModule(module)
233
234 return module.Init()
235}
236
Paul Duffinac6e6082019-12-11 15:22:32 +0000237// cc_prebuilt_library_shared installs a precompiled shared library that are
238// listed in the srcs property in the device's directory.
239func PrebuiltSharedLibraryFactory() android.Module {
240 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
241 return module.Init()
242}
243
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400244// cc_prebuilt_test_library_shared installs a precompiled shared library
245// to be used as a data dependency of a test-related module (such as cc_test, or
246// cc_test_library).
247func PrebuiltSharedTestLibraryFactory() android.Module {
248 module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported)
249 library.BuildOnlyShared()
250 library.baseInstaller = NewTestInstaller()
251 return module.Init()
252}
253
Paul Duffinac6e6082019-12-11 15:22:32 +0000254func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
255 module, library := NewPrebuiltLibrary(hod)
256 library.BuildOnlyShared()
257
258 // Prebuilt shared libraries can be included in APEXes
259 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900260
Leo Li74f7b972017-05-17 11:30:45 -0700261 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700262}
263
Patrice Arruda3554a982019-03-27 19:09:10 -0700264// cc_prebuilt_library_static installs a precompiled static library that are
265// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900266func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700267 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
268 return module.Init()
269}
270
271func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000272 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700273 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700274 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700275}
276
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000277type prebuiltObjectProperties struct {
278 Srcs []string `android:"path,arch_variant"`
279}
280
281type prebuiltObjectLinker struct {
282 android.Prebuilt
283 objectLinker
284
285 properties prebuiltObjectProperties
286}
287
288func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
289 return &p.Prebuilt
290}
291
292var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
293
294func (p *prebuiltObjectLinker) link(ctx ModuleContext,
295 flags Flags, deps PathDeps, objs Objects) android.Path {
296 if len(p.properties.Srcs) > 0 {
297 return p.Prebuilt.SingleSourcePath(ctx)
298 }
299 return nil
300}
301
Inseob Kim1042d292020-06-01 23:23:05 +0900302func (p *prebuiltObjectLinker) object() bool {
303 return true
304}
305
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000306func newPrebuiltObject() *Module {
307 module := newObject()
308 prebuilt := &prebuiltObjectLinker{
309 objectLinker: objectLinker{
310 baseLinker: NewBaseLinker(nil),
311 },
312 }
313 module.linker = prebuilt
314 module.AddProperties(&prebuilt.properties)
315 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
316 android.InitSdkAwareModule(module)
317 return module
318}
319
320func prebuiltObjectFactory() android.Module {
321 module := newPrebuiltObject()
322 return module.Init()
323}
324
Colin Crossde89fb82017-03-17 13:28:06 -0700325type prebuiltBinaryLinker struct {
326 *binaryDecorator
327 prebuiltLinker
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100328
329 toolPath android.OptionalPath
Colin Crossde89fb82017-03-17 13:28:06 -0700330}
331
332var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
333
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100334func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath {
335 return p.toolPath
336}
337
Colin Crossde89fb82017-03-17 13:28:06 -0700338func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
339 flags Flags, deps PathDeps, objs Objects) android.Path {
340 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700341 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700342 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
343 in := p.Prebuilt.SingleSourcePath(ctx)
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100344 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossb60190a2018-09-04 16:28:17 -0700345 p.unstrippedOutputFile = in
346
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100347 if ctx.Host() {
348 // Host binaries are symlinked to their prebuilt source locations. That
349 // way they are executed directly from there so the linker resolves their
350 // shared library dependencies relative to that location (using
351 // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt
352 // repository can supply the expected versions of the shared libraries
353 // without interference from what is in the out tree.
Colin Cross94921e72017-08-08 16:20:15 -0700354
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100355 // These shared lib paths may point to copies of the libs in
356 // .intermediates, which isn't where the binary will load them from, but
357 // it's fine for dependency tracking. If a library dependency is updated,
358 // the symlink will get a new timestamp, along with any installed symlinks
359 // handled in make.
360 sharedLibPaths := deps.EarlySharedLibs
361 sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...)
362 sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...)
363
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100364 var fromPath = in.String()
365 if !filepath.IsAbs(fromPath) {
366 fromPath = "$$PWD/" + fromPath
367 }
368
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100369 ctx.Build(pctx, android.BuildParams{
370 Rule: android.Symlink,
371 Output: outputFile,
372 Input: in,
373 Implicits: sharedLibPaths,
374 Args: map[string]string{
Martin Stjernholm14ee8322020-09-21 21:45:49 +0100375 "fromPath": fromPath,
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100376 },
377 })
378
379 p.toolPath = android.OptionalPathForPath(outputFile)
380 } else {
381 if p.stripper.NeedsStrip(ctx) {
382 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
383 p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags))
384 in = stripped
385 }
386
387 // Copy binaries to a name matching the final installed name
388 ctx.Build(pctx, android.BuildParams{
389 Rule: android.CpExecutable,
390 Description: "prebuilt",
391 Output: outputFile,
392 Input: in,
393 })
394 }
Colin Cross94921e72017-08-08 16:20:15 -0700395
396 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700397 }
398
399 return nil
400}
401
Inseob Kim7f283f42020-06-01 21:53:49 +0900402func (p *prebuiltBinaryLinker) binary() bool {
403 return true
404}
405
Patrice Arruda3554a982019-03-27 19:09:10 -0700406// cc_prebuilt_binary installs a precompiled executable in srcs property in the
407// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700408func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700409 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
410 return module.Init()
411}
412
413func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
414 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700415 module.compiler = nil
416
417 prebuilt := &prebuiltBinaryLinker{
418 binaryDecorator: binary,
419 }
420 module.linker = prebuilt
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100421 module.installer = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700422
Colin Cross74d73e22017-08-02 11:05:49 -0700423 module.AddProperties(&prebuilt.properties)
424
425 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700426 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700427}