blob: 0751f1ca60815c74e83b81f36bca1bd90b297738 [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"
Colin Crossce75d2c2016-10-06 16:12:58 -070019)
20
21func init() {
Paul Duffin59986b22019-12-19 14:38:36 +000022 RegisterPrebuiltBuildComponents(android.InitRegistrationContext)
23}
24
25func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) {
Paul Duffinbce90da2020-03-12 20:17:14 +000026 ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000027 ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory)
28 ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000029 ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory)
Paul Duffin59986b22019-12-19 14:38:36 +000030 ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070031}
32
33type prebuiltLinkerInterface interface {
34 Name(string) string
35 prebuilt() *android.Prebuilt
36}
37
Patrice Arruda3554a982019-03-27 19:09:10 -070038type prebuiltLinkerProperties struct {
39
40 // a prebuilt library or binary. Can reference a genrule module that generates an executable file.
41 Srcs []string `android:"path,arch_variant"`
42
43 // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined
44 // symbols, etc), default true.
45 Check_elf_files *bool
Yo Chianga3ad9b22020-03-18 14:19:07 +080046
47 // Optionally provide an import library if this is a Windows PE DLL prebuilt.
48 // This is needed only if this library is linked by other modules in build time.
49 // Only makes sense for the Windows target.
50 Windows_import_lib *string `android:"path,arch_variant"`
Patrice Arruda3554a982019-03-27 19:09:10 -070051}
52
Colin Crossde89fb82017-03-17 13:28:06 -070053type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070054 android.Prebuilt
Logan Chien4fcea3d2018-11-20 11:59:08 +080055
Patrice Arruda3554a982019-03-27 19:09:10 -070056 properties prebuiltLinkerProperties
Colin Crossce75d2c2016-10-06 16:12:58 -070057}
58
Colin Crossde89fb82017-03-17 13:28:06 -070059func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070060 return &p.Prebuilt
61}
62
Colin Cross74d73e22017-08-02 11:05:49 -070063func (p *prebuiltLinker) PrebuiltSrcs() []string {
64 return p.properties.Srcs
65}
66
Colin Cross33b2fb72019-05-14 14:07:01 -070067type prebuiltLibraryInterface interface {
68 libraryInterface
69 prebuiltLinkerInterface
70 disablePrebuilt()
71}
72
Colin Crossde89fb82017-03-17 13:28:06 -070073type prebuiltLibraryLinker struct {
74 *libraryDecorator
75 prebuiltLinker
76}
77
78var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
Colin Cross33b2fb72019-05-14 14:07:01 -070079var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil)
Colin Crossde89fb82017-03-17 13:28:06 -070080
Yi Kong00981662018-08-13 16:02:49 -070081func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
82
83func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Logan Chienc7f797e2019-01-14 15:35:08 +080084 return p.libraryDecorator.linkerDeps(ctx, deps)
Yi Kong00981662018-08-13 16:02:49 -070085}
86
87func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross1ab10a72018-09-04 11:02:37 -070088 return flags
Yi Kong00981662018-08-13 16:02:49 -070089}
90
91func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
92 return p.libraryDecorator.linkerProps()
93}
94
Colin Crossce75d2c2016-10-06 16:12:58 -070095func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070096 flags Flags, deps PathDeps, objs Objects) android.Path {
Paul Duffinf5ea9e12020-02-21 10:57:00 +000097
98 p.libraryDecorator.exportIncludes(ctx)
99 p.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
100 p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
101 p.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
102 p.libraryDecorator.reexportDeps(deps.ReexportedDeps...)
103 p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
104
Colin Crossce75d2c2016-10-06 16:12:58 -0700105 // TODO(ccross): verify shared library dependencies
Paul Duffinbce90da2020-03-12 20:17:14 +0000106 srcs := p.prebuiltSrcs()
107 if len(srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700108 builderFlags := flagsToBuilderFlags(flags)
109
Paul Duffinbce90da2020-03-12 20:17:14 +0000110 if len(srcs) > 1 {
111 ctx.PropertyErrorf("srcs", "multiple prebuilt source files")
112 return nil
113 }
114
115 in := android.PathForModuleSrc(ctx, srcs[0])
Colin Cross88f6fef2018-09-05 14:20:03 -0700116
Yo Chianga3ad9b22020-03-18 14:19:07 +0800117 if p.static() {
118 return in
119 }
120
Colin Cross88f6fef2018-09-05 14:20:03 -0700121 if p.shared() {
Colin Crossb60190a2018-09-04 16:28:17 -0700122 p.unstrippedOutputFile = in
Colin Cross0fd6a412019-08-16 14:22:10 -0700123 libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Yo Chianga3ad9b22020-03-18 14:19:07 +0800124 outputFile := android.PathForModuleOut(ctx, libName)
125 var implicits android.Paths
126
Colin Cross88f6fef2018-09-05 14:20:03 -0700127 if p.needsStrip(ctx) {
128 stripped := android.PathForModuleOut(ctx, "stripped", libName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700129 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700130 in = stripped
131 }
132
Colin Crossb60190a2018-09-04 16:28:17 -0700133 // Optimize out relinking against shared libraries whose interface hasn't changed by
134 // depending on a table of contents file instead of the library itself.
135 tocFile := android.PathForModuleOut(ctx, libName+".toc")
136 p.tocFile = android.OptionalPathForPath(tocFile)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800137 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700138
Yo Chianga3ad9b22020-03-18 14:19:07 +0800139 if ctx.Windows() && p.properties.Windows_import_lib != nil {
140 // Consumers of this library actually links to the import library in build
141 // time and dynamically links to the DLL in run time. i.e.
142 // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll
143 importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib))
144 importLibName := p.libraryDecorator.getLibName(ctx) + ".lib"
145 importLibOutputFile := android.PathForModuleOut(ctx, importLibName)
146 implicits = append(implicits, importLibOutputFile)
147
148 ctx.Build(pctx, android.BuildParams{
149 Rule: android.Cp,
150 Description: "prebuilt import library",
151 Input: importLibSrc,
152 Output: importLibOutputFile,
153 Args: map[string]string{
154 "cpFlags": "-L",
155 },
156 })
157 }
158
159 ctx.Build(pctx, android.BuildParams{
160 Rule: android.Cp,
161 Description: "prebuilt shared library",
162 Implicits: implicits,
163 Input: in,
164 Output: outputFile,
165 Args: map[string]string{
166 "cpFlags": "-L",
167 },
168 })
169
170 return outputFile
171 }
Colin Crossce75d2c2016-10-06 16:12:58 -0700172 }
173
174 return nil
175}
176
Paul Duffinbce90da2020-03-12 20:17:14 +0000177func (p *prebuiltLibraryLinker) prebuiltSrcs() []string {
178 srcs := p.properties.Srcs
179 if p.static() {
180 srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...)
181 }
182 if p.shared() {
183 srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...)
184 }
185
186 return srcs
187}
188
Jiyong Park379de2f2018-12-19 02:47:14 +0900189func (p *prebuiltLibraryLinker) shared() bool {
190 return p.libraryDecorator.shared()
191}
192
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700193func (p *prebuiltLibraryLinker) nativeCoverage() bool {
194 return false
195}
196
Colin Cross33b2fb72019-05-14 14:07:01 -0700197func (p *prebuiltLibraryLinker) disablePrebuilt() {
198 p.properties.Srcs = nil
199}
200
Martin Stjernholmbf37d162020-03-31 16:05:34 +0100201func (p *prebuiltLibraryLinker) skipInstall(mod *Module) {
202 mod.ModuleBase.SkipInstall()
203}
204
Paul Duffinac6e6082019-12-11 15:22:32 +0000205func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Leo Li74f7b972017-05-17 11:30:45 -0700206 module, library := NewLibrary(hod)
Colin Crossce75d2c2016-10-06 16:12:58 -0700207 module.compiler = nil
208
209 prebuilt := &prebuiltLibraryLinker{
210 libraryDecorator: library,
211 }
212 module.linker = prebuilt
Martin Stjernholmbf37d162020-03-31 16:05:34 +0100213 module.installer = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -0700214
Colin Cross74d73e22017-08-02 11:05:49 -0700215 module.AddProperties(&prebuilt.properties)
216
Paul Duffinbce90da2020-03-12 20:17:14 +0000217 srcsSupplier := func() []string {
218 return prebuilt.prebuiltSrcs()
219 }
220
221 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs")
Jiyong Park379de2f2018-12-19 02:47:14 +0900222
Paul Duffinac6e6082019-12-11 15:22:32 +0000223 // Prebuilt libraries can be used in SDKs.
Jiyong Parkd1063c12019-07-17 20:08:41 +0900224 android.InitSdkAwareModule(module)
Paul Duffinac6e6082019-12-11 15:22:32 +0000225 return module, library
226}
227
Paul Duffinbce90da2020-03-12 20:17:14 +0000228// cc_prebuilt_library installs a precompiled shared library that are
229// listed in the srcs property in the device's directory.
230func PrebuiltLibraryFactory() android.Module {
231 module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported)
232
233 // Prebuilt shared libraries can be included in APEXes
234 android.InitApexModule(module)
235
236 return module.Init()
237}
238
Paul Duffinac6e6082019-12-11 15:22:32 +0000239// cc_prebuilt_library_shared installs a precompiled shared library that are
240// listed in the srcs property in the device's directory.
241func PrebuiltSharedLibraryFactory() android.Module {
242 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
243 return module.Init()
244}
245
246func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
247 module, library := NewPrebuiltLibrary(hod)
248 library.BuildOnlyShared()
249
250 // Prebuilt shared libraries can be included in APEXes
251 android.InitApexModule(module)
Jiyong Park379de2f2018-12-19 02:47:14 +0900252
Leo Li74f7b972017-05-17 11:30:45 -0700253 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700254}
255
Patrice Arruda3554a982019-03-27 19:09:10 -0700256// cc_prebuilt_library_static installs a precompiled static library that are
257// listed in the srcs property in the device's directory.
Jooyung Han344d5432019-08-23 11:17:39 +0900258func PrebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700259 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
260 return module.Init()
261}
262
263func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Paul Duffinac6e6082019-12-11 15:22:32 +0000264 module, library := NewPrebuiltLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700265 library.BuildOnlyStatic()
Leo Li74f7b972017-05-17 11:30:45 -0700266 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700267}
268
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000269type prebuiltObjectProperties struct {
270 Srcs []string `android:"path,arch_variant"`
271}
272
273type prebuiltObjectLinker struct {
274 android.Prebuilt
275 objectLinker
276
277 properties prebuiltObjectProperties
278}
279
280func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt {
281 return &p.Prebuilt
282}
283
284var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil)
285
286func (p *prebuiltObjectLinker) link(ctx ModuleContext,
287 flags Flags, deps PathDeps, objs Objects) android.Path {
288 if len(p.properties.Srcs) > 0 {
289 return p.Prebuilt.SingleSourcePath(ctx)
290 }
291 return nil
292}
293
Inseob Kim1042d292020-06-01 23:23:05 +0900294func (p *prebuiltObjectLinker) object() bool {
295 return true
296}
297
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000298func newPrebuiltObject() *Module {
299 module := newObject()
300 prebuilt := &prebuiltObjectLinker{
301 objectLinker: objectLinker{
302 baseLinker: NewBaseLinker(nil),
303 },
304 }
305 module.linker = prebuilt
306 module.AddProperties(&prebuilt.properties)
307 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
308 android.InitSdkAwareModule(module)
309 return module
310}
311
312func prebuiltObjectFactory() android.Module {
313 module := newPrebuiltObject()
314 return module.Init()
315}
316
Colin Crossde89fb82017-03-17 13:28:06 -0700317type prebuiltBinaryLinker struct {
318 *binaryDecorator
319 prebuiltLinker
320}
321
322var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
323
Colin Crossde89fb82017-03-17 13:28:06 -0700324func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
325 flags Flags, deps PathDeps, objs Objects) android.Path {
326 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700327 if len(p.properties.Srcs) > 0 {
Colin Cross88f6fef2018-09-05 14:20:03 -0700328 builderFlags := flagsToBuilderFlags(flags)
329
330 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
331 in := p.Prebuilt.SingleSourcePath(ctx)
332
Colin Crossb60190a2018-09-04 16:28:17 -0700333 p.unstrippedOutputFile = in
334
Colin Cross88f6fef2018-09-05 14:20:03 -0700335 if p.needsStrip(ctx) {
336 stripped := android.PathForModuleOut(ctx, "stripped", fileName)
Ryan Prichardf979d732019-05-30 20:53:29 -0700337 p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags)
Colin Cross88f6fef2018-09-05 14:20:03 -0700338 in = stripped
339 }
Colin Cross94921e72017-08-08 16:20:15 -0700340
341 // Copy binaries to a name matching the final installed name
Colin Cross94921e72017-08-08 16:20:15 -0700342 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Crossae887032017-10-23 17:16:14 -0700343 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700344 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700345 Description: "prebuilt",
346 Output: outputFile,
Colin Cross88f6fef2018-09-05 14:20:03 -0700347 Input: in,
Colin Cross94921e72017-08-08 16:20:15 -0700348 })
349
350 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700351 }
352
353 return nil
354}
355
Inseob Kim7f283f42020-06-01 21:53:49 +0900356func (p *prebuiltBinaryLinker) binary() bool {
357 return true
358}
359
Patrice Arruda3554a982019-03-27 19:09:10 -0700360// cc_prebuilt_binary installs a precompiled executable in srcs property in the
361// device's directory.
Colin Cross36242852017-06-23 15:06:31 -0700362func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700363 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
364 return module.Init()
365}
366
367func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
368 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700369 module.compiler = nil
370
371 prebuilt := &prebuiltBinaryLinker{
372 binaryDecorator: binary,
373 }
374 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700375
Colin Cross74d73e22017-08-02 11:05:49 -0700376 module.AddProperties(&prebuilt.properties)
377
378 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700379 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700380}