blob: 8a423aae23d2a91c12454e730fabc7dc60554f09 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -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 "strings"
19
20 "github.com/google/blueprint"
Colin Cross26c34ed2016-09-30 17:10:16 -070021 "github.com/google/blueprint/pathtools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
Jayant Chowdhary756b1c22017-02-08 13:45:53 -080024 "android/soong/cc/config"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
26
Colin Crossb916a382016-07-29 17:28:03 -070027type LibraryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070028 Static struct {
Colin Cross2f336352016-10-26 10:03:47 -070029 Srcs []string `android:"arch_variant"`
30 Cflags []string `android:"arch_variant"`
Colin Cross4d9c2d12016-07-29 12:48:20 -070031
Colin Cross4d9c2d12016-07-29 12:48:20 -070032 Enabled *bool `android:"arch_variant"`
33 Whole_static_libs []string `android:"arch_variant"`
34 Static_libs []string `android:"arch_variant"`
35 Shared_libs []string `android:"arch_variant"`
36 } `android:"arch_variant"`
37 Shared struct {
Colin Cross2f336352016-10-26 10:03:47 -070038 Srcs []string `android:"arch_variant"`
39 Cflags []string `android:"arch_variant"`
Colin Crossb916a382016-07-29 17:28:03 -070040
Colin Cross4d9c2d12016-07-29 12:48:20 -070041 Enabled *bool `android:"arch_variant"`
42 Whole_static_libs []string `android:"arch_variant"`
43 Static_libs []string `android:"arch_variant"`
44 Shared_libs []string `android:"arch_variant"`
45 } `android:"arch_variant"`
46
47 // local file name to pass to the linker as --version_script
48 Version_script *string `android:"arch_variant"`
49 // local file name to pass to the linker as -unexported_symbols_list
50 Unexported_symbols_list *string `android:"arch_variant"`
51 // local file name to pass to the linker as -force_symbols_not_weak_list
52 Force_symbols_not_weak_list *string `android:"arch_variant"`
53 // local file name to pass to the linker as -force_symbols_weak_list
54 Force_symbols_weak_list *string `android:"arch_variant"`
55
56 // rename host libraries to prevent overlap with system installed libraries
57 Unique_host_soname *bool
58
Dan Willemsene1240db2016-11-03 14:28:51 -070059 Aidl struct {
60 // export headers generated from .aidl sources
61 Export_aidl_headers bool
62 }
63
Colin Cross0c461f12016-10-20 16:11:43 -070064 Proto struct {
65 // export headers generated from .proto sources
66 Export_proto_headers bool
67 }
Colin Crossa48ab5b2017-02-14 15:28:44 -080068}
Colin Cross0c461f12016-10-20 16:11:43 -070069
Colin Crossa48ab5b2017-02-14 15:28:44 -080070type LibraryMutatedProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070071 VariantName string `blueprint:"mutated"`
Colin Crossb916a382016-07-29 17:28:03 -070072
73 // Build a static variant
74 BuildStatic bool `blueprint:"mutated"`
75 // Build a shared variant
76 BuildShared bool `blueprint:"mutated"`
77 // This variant is shared
78 VariantIsShared bool `blueprint:"mutated"`
79 // This variant is static
80 VariantIsStatic bool `blueprint:"mutated"`
81}
82
83type FlagExporterProperties struct {
84 // list of directories relative to the Blueprints file that will
Dan Willemsen273af7f2016-11-03 15:53:42 -070085 // be added to the include path (using -I) for this module and any module that links
86 // against this module
Colin Crossb916a382016-07-29 17:28:03 -070087 Export_include_dirs []string `android:"arch_variant"`
Dan Willemsen95421a42017-04-06 12:43:22 -070088
89 Target struct {
90 Vendor struct {
91 // list of exported include directories, like
92 // export_include_dirs, that will be applied to the
93 // vendor variant of this library. This will overwrite
94 // any other declarations.
95 Export_include_dirs []string
96 }
97 }
Colin Cross4d9c2d12016-07-29 12:48:20 -070098}
99
100func init() {
Colin Cross798bfce2016-10-12 14:28:16 -0700101 android.RegisterModuleType("cc_library_static", libraryStaticFactory)
102 android.RegisterModuleType("cc_library_shared", librarySharedFactory)
103 android.RegisterModuleType("cc_library", libraryFactory)
104 android.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
105 android.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
Colin Cross5950f382016-12-13 12:50:57 -0800106 android.RegisterModuleType("cc_library_headers", libraryHeaderFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107}
108
109// Module factory for combined static + shared libraries, device by default but with possible host
110// support
111func libraryFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800112 module, _ := NewLibrary(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700113 return module.Init()
114}
115
116// Module factory for static libraries
117func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800118 module, library := NewLibrary(android.HostAndDeviceSupported)
119 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 return module.Init()
121}
122
123// Module factory for shared libraries
124func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800125 module, library := NewLibrary(android.HostAndDeviceSupported)
126 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 return module.Init()
128}
129
130// Module factory for host static libraries
131func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800132 module, library := NewLibrary(android.HostSupported)
133 library.BuildOnlyStatic()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 return module.Init()
135}
136
137// Module factory for host shared libraries
138func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Crossab3b7322016-12-09 14:46:15 -0800139 module, library := NewLibrary(android.HostSupported)
140 library.BuildOnlyShared()
Colin Cross4d9c2d12016-07-29 12:48:20 -0700141 return module.Init()
142}
143
Colin Cross5950f382016-12-13 12:50:57 -0800144// Module factory for header-only libraries
145func libraryHeaderFactory() (blueprint.Module, []interface{}) {
146 module, library := NewLibrary(android.HostAndDeviceSupported)
147 library.HeaderOnly()
148 return module.Init()
149}
150
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151type flagExporter struct {
152 Properties FlagExporterProperties
153
Dan Willemsen847dcc72016-09-29 12:13:36 -0700154 flags []string
155 flagsDeps android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700156}
157
Dan Willemsen95421a42017-04-06 12:43:22 -0700158func (f *flagExporter) exportedIncludes(ctx ModuleContext) android.Paths {
159 if ctx.Vendor() && f.Properties.Target.Vendor.Export_include_dirs != nil {
160 return android.PathsForModuleSrc(ctx, f.Properties.Target.Vendor.Export_include_dirs)
161 } else {
162 return android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
163 }
164}
165
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Dan Willemsen95421a42017-04-06 12:43:22 -0700167 includeDirs := f.exportedIncludes(ctx)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700168 for _, dir := range includeDirs.Strings() {
169 f.flags = append(f.flags, inc+dir)
170 }
171}
172
173func (f *flagExporter) reexportFlags(flags []string) {
174 f.flags = append(f.flags, flags...)
175}
176
Dan Willemsen847dcc72016-09-29 12:13:36 -0700177func (f *flagExporter) reexportDeps(deps android.Paths) {
178 f.flagsDeps = append(f.flagsDeps, deps...)
179}
180
Colin Cross4d9c2d12016-07-29 12:48:20 -0700181func (f *flagExporter) exportedFlags() []string {
182 return f.flags
183}
184
Dan Willemsen847dcc72016-09-29 12:13:36 -0700185func (f *flagExporter) exportedFlagsDeps() android.Paths {
186 return f.flagsDeps
187}
188
Colin Cross4d9c2d12016-07-29 12:48:20 -0700189type exportedFlagsProducer interface {
190 exportedFlags() []string
Dan Willemsen847dcc72016-09-29 12:13:36 -0700191 exportedFlagsDeps() android.Paths
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192}
193
194var _ exportedFlagsProducer = (*flagExporter)(nil)
195
Colin Crossb916a382016-07-29 17:28:03 -0700196// libraryDecorator wraps baseCompiler, baseLinker and baseInstaller to provide library-specific
197// functionality: static vs. shared linkage, reusing object files for shared libraries
198type libraryDecorator struct {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800199 Properties LibraryProperties
200 MutatedProperties LibraryMutatedProperties
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201
202 // For reusing static library objects for shared library
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700203 reuseObjects Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700204 // table-of-contents file to optimize out relinking when possible
205 tocFile android.OptionalPath
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207 flagExporter
208 stripper
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700209 relocationPacker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700210
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 // If we're used as a whole_static_lib, our missing dependencies need
212 // to be given
213 wholeStaticMissingDeps []string
214
215 // For whole_static_libs
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700216 objects Objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
218 // Uses the module's name if empty, but can be overridden. Does not include
219 // shlib suffix.
220 libName string
Colin Crossb916a382016-07-29 17:28:03 -0700221
222 sanitize *sanitize
223
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800224 sabi *sabi
225
Dan Willemsen581341d2017-02-09 16:16:31 -0800226 // Output archive of gcno coverage information files
227 coverageOutputFile android.OptionalPath
228
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800229 // linked Source Abi Dump
230 sAbiOutputFile android.OptionalPath
231
232 // Source Abi Diff
233 sAbiDiff android.OptionalPath
234
Colin Crossb916a382016-07-29 17:28:03 -0700235 // Decorated interafaces
236 *baseCompiler
237 *baseLinker
238 *baseInstaller
Colin Cross4d9c2d12016-07-29 12:48:20 -0700239}
240
Colin Crossb916a382016-07-29 17:28:03 -0700241func (library *libraryDecorator) linkerProps() []interface{} {
242 var props []interface{}
243 props = append(props, library.baseLinker.linkerProps()...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 return append(props,
245 &library.Properties,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800246 &library.MutatedProperties,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 &library.flagExporter.Properties,
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700248 &library.stripper.StripProperties,
249 &library.relocationPacker.Properties)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700250}
251
Colin Crossb916a382016-07-29 17:28:03 -0700252func (library *libraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross42742b82016-08-01 13:20:05 -0700253 flags = library.baseLinker.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254
Colin Crossb916a382016-07-29 17:28:03 -0700255 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
256 // all code is position independent, and then those warnings get promoted to
257 // errors.
258 if ctx.Os() != android.Windows {
259 flags.CFlags = append(flags.CFlags, "-fPIC")
260 }
261
262 if library.static() {
263 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800264 } else if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700265 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
266 }
267
Colin Crossa48ab5b2017-02-14 15:28:44 -0800268 if library.shared() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 libName := library.getLibName(ctx)
270 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
271 sharedFlag := "-Wl,-shared"
272 if flags.Clang || ctx.Host() {
273 sharedFlag = "-shared"
274 }
275 var f []string
Dan Willemsen01a405a2016-06-13 17:19:03 -0700276 if ctx.toolchain().Bionic() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 f = append(f,
278 "-nostdlib",
279 "-Wl,--gc-sections",
280 )
281 }
282
283 if ctx.Darwin() {
284 f = append(f,
285 "-dynamiclib",
286 "-single_module",
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
288 )
Colin Cross7863cf52016-10-20 10:47:21 -0700289 if ctx.Arch().ArchType == android.X86 {
290 f = append(f,
291 "-read_only_relocs suppress",
292 )
293 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 } else {
295 f = append(f,
296 sharedFlag,
297 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
298 }
299
300 flags.LdFlags = append(f, flags.LdFlags...)
301 }
302
303 return flags
304}
305
Dan Willemsen273af7f2016-11-03 15:53:42 -0700306func (library *libraryDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsen95421a42017-04-06 12:43:22 -0700307 exportIncludeDirs := library.flagExporter.exportedIncludes(ctx)
Dan Willemsen273af7f2016-11-03 15:53:42 -0700308 if len(exportIncludeDirs) > 0 {
309 flags.GlobalFlags = append(flags.GlobalFlags, includeDirsToFlags(exportIncludeDirs))
310 }
311
312 return library.baseCompiler.compilerFlags(ctx, flags)
313}
314
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700315func extractExportIncludesFromFlags(flags []string) []string {
316 // This method is used in the generation of rules which produce
317 // abi-dumps for source files. Exported headers are needed to infer the
318 // abi exported by a library and filter out the rest of the abi dumped
319 // from a source. We extract the include flags exported by a library.
320 // This includes the flags exported which are re-exported from static
321 // library dependencies, exported header library dependencies and
322 // generated header dependencies. Re-exported shared library include
323 // flags are not in this set since shared library dependencies will
324 // themselves be included in the vndk. -isystem headers are not included
325 // since for bionic libraries, abi-filtering is taken care of by version
326 // scripts.
327 var exportedIncludes []string
328 for _, flag := range flags {
329 if strings.HasPrefix(flag, "-I") {
330 exportedIncludes = append(exportedIncludes, flag)
331 }
332 }
333 return exportedIncludes
334}
335
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700336func (library *libraryDecorator) compile(ctx ModuleContext, flags Flags, deps PathDeps) Objects {
Colin Cross5950f382016-12-13 12:50:57 -0800337 if !library.buildShared() && !library.buildStatic() {
338 if len(library.baseCompiler.Properties.Srcs) > 0 {
339 ctx.PropertyErrorf("srcs", "cc_library_headers must not have any srcs")
340 }
341 if len(library.Properties.Static.Srcs) > 0 {
342 ctx.PropertyErrorf("static.srcs", "cc_library_headers must not have any srcs")
343 }
344 if len(library.Properties.Shared.Srcs) > 0 {
345 ctx.PropertyErrorf("shared.srcs", "cc_library_headers must not have any srcs")
346 }
347 return Objects{}
348 }
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700349 if (ctx.createVndkSourceAbiDump() || (library.sabi.Properties.CreateSAbiDumps && ctx.Device())) && !ctx.Vendor() {
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800350 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
351 var SourceAbiFlags []string
352 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700353 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800354 }
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700355 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
356 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
357 }
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800358 flags.SAbiFlags = SourceAbiFlags
359 total_length := len(library.baseCompiler.Properties.Srcs) + len(deps.GeneratedSources) + len(library.Properties.Shared.Srcs) +
360 len(library.Properties.Static.Srcs)
361 if total_length > 0 {
362 flags.SAbiDump = true
363 }
364 }
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700365 objs := library.baseCompiler.compile(ctx, flags, deps)
366 library.reuseObjects = objs
Colin Cross2f336352016-10-26 10:03:47 -0700367 buildFlags := flagsToBuilderFlags(flags)
Colin Crossb916a382016-07-29 17:28:03 -0700368
Colin Cross4d9c2d12016-07-29 12:48:20 -0700369 if library.static() {
Colin Cross2f336352016-10-26 10:03:47 -0700370 srcs := android.PathsForModuleSrc(ctx, library.Properties.Static.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700371 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceStaticLibrary,
372 srcs, library.baseCompiler.deps))
Colin Crossa48ab5b2017-02-14 15:28:44 -0800373 } else if library.shared() {
Colin Cross2f336352016-10-26 10:03:47 -0700374 srcs := android.PathsForModuleSrc(ctx, library.Properties.Shared.Srcs)
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700375 objs = objs.Append(compileObjs(ctx, buildFlags, android.DeviceSharedLibrary,
376 srcs, library.baseCompiler.deps))
Colin Crossb916a382016-07-29 17:28:03 -0700377 }
378
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700379 return objs
Colin Crossb916a382016-07-29 17:28:03 -0700380}
381
382type libraryInterface interface {
383 getWholeStaticMissingDeps() []string
384 static() bool
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700385 objs() Objects
386 reuseObjs() Objects
Colin Cross26c34ed2016-09-30 17:10:16 -0700387 toc() android.OptionalPath
Colin Crossb916a382016-07-29 17:28:03 -0700388
389 // Returns true if the build options for the module have selected a static or shared build
390 buildStatic() bool
391 buildShared() bool
392
393 // Sets whether a specific variant is static or shared
Colin Crossa48ab5b2017-02-14 15:28:44 -0800394 setStatic()
395 setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700396}
397
398func (library *libraryDecorator) getLibName(ctx ModuleContext) string {
399 name := library.libName
400 if name == "" {
Colin Crossce75d2c2016-10-06 16:12:58 -0700401 name = ctx.baseModuleName()
Colin Crossb916a382016-07-29 17:28:03 -0700402 }
403
404 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
405 if !strings.HasSuffix(name, "-host") {
406 name = name + "-host"
407 }
408 }
409
Colin Crossa48ab5b2017-02-14 15:28:44 -0800410 return name + library.MutatedProperties.VariantName
Colin Crossb916a382016-07-29 17:28:03 -0700411}
412
413func (library *libraryDecorator) linkerInit(ctx BaseModuleContext) {
414 location := InstallInSystem
Vishwath Mohan0d0a6f32017-03-29 22:00:18 -0700415 if library.sanitize.inSanitizerDir() {
416 location = InstallInSanitizerDir
Colin Crossb916a382016-07-29 17:28:03 -0700417 }
418 library.baseInstaller.location = location
419
420 library.baseLinker.linkerInit(ctx)
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700421
422 library.relocationPacker.packingInit(ctx)
Colin Crossb916a382016-07-29 17:28:03 -0700423}
424
Colin Cross37047f12016-12-13 17:06:13 -0800425func (library *libraryDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700426 deps = library.baseLinker.linkerDeps(ctx, deps)
427
428 if library.static() {
429 deps.WholeStaticLibs = append(deps.WholeStaticLibs,
430 library.Properties.Static.Whole_static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700431 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
432 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
Colin Crossa48ab5b2017-02-14 15:28:44 -0800433 } else if library.shared() {
Dan Willemsen2e47b342016-11-17 01:02:25 -0800434 if ctx.toolchain().Bionic() && !Bool(library.baseLinker.Properties.Nocrt) {
Dan Willemsen95421a42017-04-06 12:43:22 -0700435 if !ctx.sdk() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700436 deps.CrtBegin = "crtbegin_so"
437 deps.CrtEnd = "crtend_so"
438 } else {
Dan Albertebedf672016-11-08 15:06:22 -0800439 // TODO(danalbert): Add generation of crt objects.
440 // For `sdk_version: "current"`, we don't actually have a
441 // freshly generated set of CRT objects. Use the last stable
442 // version.
443 version := ctx.sdkVersion()
444 if version == "current" {
445 version = ctx.AConfig().PlatformSdkVersion()
446 }
447 deps.CrtBegin = "ndk_crtbegin_so." + version
448 deps.CrtEnd = "ndk_crtend_so." + version
Colin Cross4d9c2d12016-07-29 12:48:20 -0700449 }
450 }
451 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
452 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
453 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
454 }
455
456 return deps
457}
458
Colin Crossb916a382016-07-29 17:28:03 -0700459func (library *libraryDecorator) linkStatic(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700460 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700461
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700462 library.objects = deps.WholeStaticLibObjs.Copy()
463 library.objects = library.objects.Append(objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700464
465 outputFile := android.PathForModuleOut(ctx,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800466 ctx.ModuleName()+library.MutatedProperties.VariantName+staticLibraryExtension)
Dan Willemsen581341d2017-02-09 16:16:31 -0800467 builderFlags := flagsToBuilderFlags(flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700468
Dan Willemsen581341d2017-02-09 16:16:31 -0800469 TransformObjToStaticLib(ctx, library.objects.objFiles, builderFlags, outputFile, objs.tidyFiles)
470
471 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, library.objects, builderFlags,
Colin Crossa48ab5b2017-02-14 15:28:44 -0800472 ctx.ModuleName()+library.MutatedProperties.VariantName)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700473
474 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
475
476 ctx.CheckbuildFile(outputFile)
477
478 return outputFile
479}
480
Colin Crossb916a382016-07-29 17:28:03 -0700481func (library *libraryDecorator) linkShared(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700482 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700483
484 var linkerDeps android.Paths
485
486 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
487 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
488 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
489 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
490 if !ctx.Darwin() {
491 if versionScript.Valid() {
492 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
493 linkerDeps = append(linkerDeps, versionScript.Path())
494 }
495 if unexportedSymbols.Valid() {
496 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
497 }
498 if forceNotWeakSymbols.Valid() {
499 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
500 }
501 if forceWeakSymbols.Valid() {
502 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
503 }
504 } else {
505 if versionScript.Valid() {
506 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
507 }
508 if unexportedSymbols.Valid() {
509 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
510 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
511 }
512 if forceNotWeakSymbols.Valid() {
513 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
514 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
515 }
516 if forceWeakSymbols.Valid() {
517 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
518 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
519 }
520 }
521
522 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
523 outputFile := android.PathForModuleOut(ctx, fileName)
524 ret := outputFile
525
526 builderFlags := flagsToBuilderFlags(flags)
527
Colin Cross89562dc2016-10-03 17:47:19 -0700528 if !ctx.Darwin() {
529 // Optimize out relinking against shared libraries whose interface hasn't changed by
530 // depending on a table of contents file instead of the library itself.
531 tocPath := outputFile.RelPathString()
532 tocPath = pathtools.ReplaceExtension(tocPath, flags.Toolchain.ShlibSuffix()[1:]+".toc")
533 tocFile := android.PathForOutput(ctx, tocPath)
534 library.tocFile = android.OptionalPathForPath(tocFile)
535 TransformSharedObjectToToc(ctx, outputFile, tocFile, builderFlags)
536 }
537
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700538 if library.relocationPacker.needsPacking(ctx) {
539 packedOutputFile := outputFile
540 outputFile = android.PathForModuleOut(ctx, "unpacked", fileName)
541 library.relocationPacker.pack(ctx, outputFile, packedOutputFile, builderFlags)
542 }
543
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544 if library.stripper.needsStrip(ctx) {
545 strippedOutputFile := outputFile
546 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
547 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
548 }
549
550 sharedLibs := deps.SharedLibs
551 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
552
Dan Albertd015c4a2016-08-10 14:34:08 -0700553 // TODO(danalbert): Clean this up when soong supports prebuilts.
554 if strings.HasPrefix(ctx.selectedStl(), "ndk_libc++") {
555 libDir := getNdkStlLibDir(ctx, flags.Toolchain, "libc++")
556
557 if strings.HasSuffix(ctx.selectedStl(), "_shared") {
558 deps.StaticLibs = append(deps.StaticLibs,
559 libDir.Join(ctx, "libandroid_support.a"))
560 } else {
561 deps.StaticLibs = append(deps.StaticLibs,
562 libDir.Join(ctx, "libc++abi.a"),
563 libDir.Join(ctx, "libandroid_support.a"))
564 }
565
566 if ctx.Arch().ArchType == android.Arm {
567 deps.StaticLibs = append(deps.StaticLibs,
568 libDir.Join(ctx, "libunwind.a"))
569 }
570 }
571
Colin Cross26c34ed2016-09-30 17:10:16 -0700572 linkerDeps = append(linkerDeps, deps.SharedLibsDeps...)
573 linkerDeps = append(linkerDeps, deps.LateSharedLibsDeps...)
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700574 linkerDeps = append(linkerDeps, objs.tidyFiles...)
Colin Cross26c34ed2016-09-30 17:10:16 -0700575
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700576 TransformObjToDynamicBinary(ctx, objs.objFiles, sharedLibs,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700577 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
578 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
579
Dan Willemsen581341d2017-02-09 16:16:31 -0800580 objs.coverageFiles = append(objs.coverageFiles, deps.StaticLibObjs.coverageFiles...)
581 objs.coverageFiles = append(objs.coverageFiles, deps.WholeStaticLibObjs.coverageFiles...)
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800582
583 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.StaticLibObjs.sAbiDumpFiles...)
584 objs.sAbiDumpFiles = append(objs.sAbiDumpFiles, deps.WholeStaticLibObjs.sAbiDumpFiles...)
585
Dan Willemsen581341d2017-02-09 16:16:31 -0800586 library.coverageOutputFile = TransformCoverageFilesToLib(ctx, objs, builderFlags, library.getLibName(ctx))
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800587 library.linkSAbiDumpFiles(ctx, objs, fileName)
Dan Willemsen581341d2017-02-09 16:16:31 -0800588
Colin Cross4d9c2d12016-07-29 12:48:20 -0700589 return ret
590}
591
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800592func (library *libraryDecorator) linkSAbiDumpFiles(ctx ModuleContext, objs Objects, fileName string) {
593 //Also take into account object re-use.
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700594 if len(objs.sAbiDumpFiles) > 0 && ctx.createVndkSourceAbiDump() && !ctx.Vendor() {
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800595 refSourceDumpFile := android.PathForVndkRefAbiDump(ctx, "current", fileName, vndkVsNdk(ctx), true)
596 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
597 var symbolFile android.OptionalPath
598 if versionScript.Valid() {
599 symbolFile = versionScript
600 }
601 exportIncludeDirs := android.PathsForModuleSrc(ctx, library.flagExporter.Properties.Export_include_dirs)
602 var SourceAbiFlags []string
603 for _, dir := range exportIncludeDirs.Strings() {
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700604 SourceAbiFlags = append(SourceAbiFlags, "-I"+dir)
605 }
606 for _, reexportedInclude := range extractExportIncludesFromFlags(library.sabi.Properties.ReexportedIncludeFlags) {
607 SourceAbiFlags = append(SourceAbiFlags, reexportedInclude)
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800608 }
609 exportedHeaderFlags := strings.Join(SourceAbiFlags, " ")
610 library.sAbiOutputFile = TransformDumpToLinkedDump(ctx, objs.sAbiDumpFiles, symbolFile, "current", fileName, exportedHeaderFlags)
611 if refSourceDumpFile.Valid() {
Jayant Chowdhary8cede072017-04-20 06:53:59 -0700612 unzippedRefDump := UnzipRefDump(ctx, refSourceDumpFile.Path(), fileName)
613 library.sAbiDiff = SourceAbiDiff(ctx, library.sAbiOutputFile.Path(), unzippedRefDump, fileName)
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800614 }
615 }
616}
617
618func vndkVsNdk(ctx ModuleContext) bool {
619 if inList(ctx.baseModuleName(), config.LLndkLibraries()) {
620 return false
621 }
622 return true
623}
624
Colin Crossb916a382016-07-29 17:28:03 -0700625func (library *libraryDecorator) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700626 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700627
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700628 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700629
630 var out android.Path
Colin Crossa48ab5b2017-02-14 15:28:44 -0800631 if library.static() || library.header() {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700632 out = library.linkStatic(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700633 } else {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700634 out = library.linkShared(ctx, flags, deps, objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700635 }
636
637 library.exportIncludes(ctx, "-I")
638 library.reexportFlags(deps.ReexportedFlags)
Dan Willemsen847dcc72016-09-29 12:13:36 -0700639 library.reexportDeps(deps.ReexportedFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700640
Dan Willemsene1240db2016-11-03 14:28:51 -0700641 if library.Properties.Aidl.Export_aidl_headers {
642 if library.baseCompiler.hasSrcExt(".aidl") {
643 library.reexportFlags([]string{
644 "-I" + android.PathForModuleGen(ctx, "aidl").String(),
645 })
646 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to aidl deps
647 }
648 }
649
650 if library.Properties.Proto.Export_proto_headers {
651 if library.baseCompiler.hasSrcExt(".proto") {
Colin Cross0c461f12016-10-20 16:11:43 -0700652 library.reexportFlags([]string{
653 "-I" + protoSubDir(ctx).String(),
654 "-I" + protoDir(ctx).String(),
655 })
656 library.reexportDeps(library.baseCompiler.deps) // TODO: restrict to proto deps
657 }
658 }
659
Colin Cross4d9c2d12016-07-29 12:48:20 -0700660 return out
661}
662
Colin Crossb916a382016-07-29 17:28:03 -0700663func (library *libraryDecorator) buildStatic() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800664 return library.MutatedProperties.BuildStatic &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700665 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
666}
667
Colin Crossb916a382016-07-29 17:28:03 -0700668func (library *libraryDecorator) buildShared() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800669 return library.MutatedProperties.BuildShared &&
Colin Cross4d9c2d12016-07-29 12:48:20 -0700670 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
671}
672
Colin Crossb916a382016-07-29 17:28:03 -0700673func (library *libraryDecorator) getWholeStaticMissingDeps() []string {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700674 return library.wholeStaticMissingDeps
675}
676
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700677func (library *libraryDecorator) objs() Objects {
678 return library.objects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700679}
680
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700681func (library *libraryDecorator) reuseObjs() Objects {
682 return library.reuseObjects
Colin Cross4d9c2d12016-07-29 12:48:20 -0700683}
684
Colin Cross26c34ed2016-09-30 17:10:16 -0700685func (library *libraryDecorator) toc() android.OptionalPath {
686 return library.tocFile
687}
688
Colin Crossb916a382016-07-29 17:28:03 -0700689func (library *libraryDecorator) install(ctx ModuleContext, file android.Path) {
690 if !ctx.static() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700691 library.baseInstaller.install(ctx, file)
692 }
693}
694
Colin Crossb916a382016-07-29 17:28:03 -0700695func (library *libraryDecorator) static() bool {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800696 return library.MutatedProperties.VariantIsStatic
Colin Cross4d9c2d12016-07-29 12:48:20 -0700697}
698
Colin Crossa48ab5b2017-02-14 15:28:44 -0800699func (library *libraryDecorator) shared() bool {
700 return library.MutatedProperties.VariantIsShared
701}
702
703func (library *libraryDecorator) header() bool {
704 return !library.static() && !library.shared()
705}
706
707func (library *libraryDecorator) setStatic() {
708 library.MutatedProperties.VariantIsStatic = true
709 library.MutatedProperties.VariantIsShared = false
710}
711
712func (library *libraryDecorator) setShared() {
713 library.MutatedProperties.VariantIsStatic = false
714 library.MutatedProperties.VariantIsShared = true
Colin Crossb916a382016-07-29 17:28:03 -0700715}
716
Colin Crossab3b7322016-12-09 14:46:15 -0800717func (library *libraryDecorator) BuildOnlyStatic() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800718 library.MutatedProperties.BuildShared = false
Colin Crossab3b7322016-12-09 14:46:15 -0800719}
720
721func (library *libraryDecorator) BuildOnlyShared() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800722 library.MutatedProperties.BuildStatic = false
Colin Crossab3b7322016-12-09 14:46:15 -0800723}
724
Colin Cross5950f382016-12-13 12:50:57 -0800725func (library *libraryDecorator) HeaderOnly() {
Colin Crossa48ab5b2017-02-14 15:28:44 -0800726 library.MutatedProperties.BuildShared = false
727 library.MutatedProperties.BuildStatic = false
Colin Cross5950f382016-12-13 12:50:57 -0800728}
729
Colin Crossab3b7322016-12-09 14:46:15 -0800730func NewLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700731 module := newModule(hod, android.MultilibBoth)
732
Colin Crossb916a382016-07-29 17:28:03 -0700733 library := &libraryDecorator{
Colin Crossa48ab5b2017-02-14 15:28:44 -0800734 MutatedProperties: LibraryMutatedProperties{
Colin Crossab3b7322016-12-09 14:46:15 -0800735 BuildShared: true,
736 BuildStatic: true,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700737 },
Colin Crossb916a382016-07-29 17:28:03 -0700738 baseCompiler: NewBaseCompiler(),
739 baseLinker: NewBaseLinker(),
740 baseInstaller: NewBaseInstaller("lib", "lib64", InstallInSystem),
741 sanitize: module.sanitize,
Jayant Chowdhary756b1c22017-02-08 13:45:53 -0800742 sabi: module.sabi,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700743 }
744
Colin Crossb916a382016-07-29 17:28:03 -0700745 module.compiler = library
746 module.linker = library
747 module.installer = library
748
749 return module, library
750}
751
752func linkageMutator(mctx android.BottomUpMutatorContext) {
753 if m, ok := mctx.Module().(*Module); ok && m.linker != nil {
754 if library, ok := m.linker.(libraryInterface); ok {
755 var modules []blueprint.Module
756 if library.buildStatic() && library.buildShared() {
757 modules = mctx.CreateLocalVariations("static", "shared")
758 static := modules[0].(*Module)
759 shared := modules[1].(*Module)
760
Colin Crossa48ab5b2017-02-14 15:28:44 -0800761 static.linker.(libraryInterface).setStatic()
762 shared.linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700763
764 if staticCompiler, ok := static.compiler.(*libraryDecorator); ok {
765 sharedCompiler := shared.compiler.(*libraryDecorator)
766 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
767 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
768 // Optimize out compiling common .o files twice for static+shared libraries
769 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
770 sharedCompiler.baseCompiler.Properties.Srcs = nil
771 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
772 }
773 }
774 } else if library.buildStatic() {
775 modules = mctx.CreateLocalVariations("static")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800776 modules[0].(*Module).linker.(libraryInterface).setStatic()
Colin Crossb916a382016-07-29 17:28:03 -0700777 } else if library.buildShared() {
778 modules = mctx.CreateLocalVariations("shared")
Colin Crossa48ab5b2017-02-14 15:28:44 -0800779 modules[0].(*Module).linker.(libraryInterface).setShared()
Colin Crossb916a382016-07-29 17:28:03 -0700780 }
781 }
782 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700783}