blob: f3400e7cdea8f41d53d3e14fb3eae6c6db4b97be [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
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
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080035 soong.RegisterModuleType("cc_library_static", libraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", librarySharedFactory)
37 soong.RegisterModuleType("cc_library", libraryFactory)
38 soong.RegisterModuleType("cc_object", objectFactory)
39 soong.RegisterModuleType("cc_binary", binaryFactory)
40 soong.RegisterModuleType("cc_test", testFactory)
41 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
42 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Crossca860ac2016-01-04 14:34:37 -080044 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070049
Colin Crossca860ac2016-01-04 14:34:37 -080050 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", testHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070055
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
Colin Cross3f40fa42015-01-30 17:27:36 -080068)
69
70// Flags used by lots of devices. Putting them in package static variables will save bytes in
71// build.ninja so they aren't repeated for every file
72var (
73 commonGlobalCflags = []string{
74 "-DANDROID",
75 "-fmessage-length=0",
76 "-W",
77 "-Wall",
78 "-Wno-unused",
79 "-Winit-self",
80 "-Wpointer-arith",
81
82 // COMMON_RELEASE_CFLAGS
83 "-DNDEBUG",
84 "-UDEBUG",
85 }
86
87 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080088 "-fdiagnostics-color",
89
Colin Cross3f40fa42015-01-30 17:27:36 -080090 // TARGET_ERROR_FLAGS
91 "-Werror=return-type",
92 "-Werror=non-virtual-dtor",
93 "-Werror=address",
94 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080095 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080096 }
97
98 hostGlobalCflags = []string{}
99
100 commonGlobalCppflags = []string{
101 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700102 }
103
Dan Willemsenbe03f342016-03-03 17:21:04 -0800104 noOverrideGlobalCflags = []string{
105 "-Werror=int-to-pointer-cast",
106 "-Werror=pointer-to-int-cast",
107 }
108
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700109 illegalFlags = []string{
110 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800111 }
112)
113
114func init() {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700115 if common.CurrentHostType() == common.Linux {
116 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
117 }
118
Colin Cross3f40fa42015-01-30 17:27:36 -0800119 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
120 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
121 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800122 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800123
124 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
125
126 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800127 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800129 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800130 pctx.StaticVariable("hostClangGlobalCflags",
131 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800132 pctx.StaticVariable("noOverrideClangGlobalCflags",
133 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
134
Tim Kilbournf2948142015-03-11 12:03:03 -0700135 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800136 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800137
138 // Everything in this list is a crime against abstraction and dependency tracking.
139 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800140 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700141 []string{
142 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800143 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700144 "hardware/libhardware/include",
145 "hardware/libhardware_legacy/include",
146 "hardware/ril/include",
147 "libnativehelper/include",
148 "frameworks/native/include",
149 "frameworks/native/opengl/include",
150 "frameworks/av/include",
151 "frameworks/base/include",
152 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800153 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
154 // with this, since there is no associated library.
155 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
156 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800157
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700158 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
159 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
160 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
161 return override, nil
162 }
163 return "${clangDefaultBase}", nil
164 })
165 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
166 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
167 return override, nil
168 }
Colin Cross7253e0b2016-03-21 15:12:34 -0700169 return "clang-2690385", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700170 })
171 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800172}
173
Colin Crossca860ac2016-01-04 14:34:37 -0800174type Deps struct {
175 SharedLibs, LateSharedLibs []string
176 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700177
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700178 ObjFiles common.Paths
179
180 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700181
Colin Cross97ba0732015-03-23 17:50:24 -0700182 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700183}
184
Colin Crossca860ac2016-01-04 14:34:37 -0800185type PathDeps struct {
186 SharedLibs, LateSharedLibs common.Paths
187 StaticLibs, LateStaticLibs, WholeStaticLibs common.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700188
189 ObjFiles common.Paths
190 WholeStaticLibObjFiles common.Paths
191
192 Cflags, ReexportedCflags []string
193
194 CrtBegin, CrtEnd common.OptionalPath
195}
196
Colin Crossca860ac2016-01-04 14:34:37 -0800197type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700198 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
199 AsFlags []string // Flags that apply to assembly source files
200 CFlags []string // Flags that apply to C and C++ source files
201 ConlyFlags []string // Flags that apply to C source files
202 CppFlags []string // Flags that apply to C++ source files
203 YaccFlags []string // Flags that apply to Yacc source files
204 LdFlags []string // Flags that apply to linker command lines
205
206 Nocrt bool
207 Toolchain Toolchain
208 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800209
210 RequiredInstructionSet string
Colin Crossc472d572015-03-17 15:06:21 -0700211}
212
Colin Crossca860ac2016-01-04 14:34:37 -0800213type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700214 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700215 Srcs []string `android:"arch_variant"`
216
217 // list of source files that should not be used to build the C/C++ module.
218 // This is most useful in the arch/multilib variants to remove non-common files
219 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700220
221 // list of module-specific flags that will be used for C and C++ compiles.
222 Cflags []string `android:"arch_variant"`
223
224 // list of module-specific flags that will be used for C++ compiles
225 Cppflags []string `android:"arch_variant"`
226
227 // list of module-specific flags that will be used for C compiles
228 Conlyflags []string `android:"arch_variant"`
229
230 // list of module-specific flags that will be used for .S compiles
231 Asflags []string `android:"arch_variant"`
232
Colin Crossca860ac2016-01-04 14:34:37 -0800233 // list of module-specific flags that will be used for C and C++ compiles when
234 // compiling with clang
235 Clang_cflags []string `android:"arch_variant"`
236
237 // list of module-specific flags that will be used for .S compiles when
238 // compiling with clang
239 Clang_asflags []string `android:"arch_variant"`
240
Colin Cross7d5136f2015-05-11 13:39:40 -0700241 // list of module-specific flags that will be used for .y and .yy compiles
242 Yaccflags []string
243
Colin Cross7d5136f2015-05-11 13:39:40 -0700244 // the instruction set architecture to use to compile the C/C++
245 // module.
246 Instruction_set string `android:"arch_variant"`
247
248 // list of directories relative to the root of the source tree that will
249 // be added to the include path using -I.
250 // If possible, don't use this. If adding paths from the current directory use
251 // local_include_dirs, if adding paths from other modules use export_include_dirs in
252 // that module.
253 Include_dirs []string `android:"arch_variant"`
254
Colin Cross39d97f22015-09-14 12:30:50 -0700255 // list of files relative to the root of the source tree that will be included
256 // using -include.
257 // If possible, don't use this.
258 Include_files []string `android:"arch_variant"`
259
Colin Cross7d5136f2015-05-11 13:39:40 -0700260 // list of directories relative to the Blueprints file that will
261 // be added to the include path using -I
262 Local_include_dirs []string `android:"arch_variant"`
263
Colin Cross39d97f22015-09-14 12:30:50 -0700264 // list of files relative to the Blueprints file that will be included
265 // using -include.
266 // If possible, don't use this.
267 Local_include_files []string `android:"arch_variant"`
268
Colin Crossca860ac2016-01-04 14:34:37 -0800269 // pass -frtti instead of -fno-rtti
270 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700271
Colin Crossca860ac2016-01-04 14:34:37 -0800272 Debug, Release struct {
273 // list of module-specific flags that will be used for C and C++ compiles in debug or
274 // release builds
275 Cflags []string `android:"arch_variant"`
276 } `android:"arch_variant"`
277}
Colin Cross7d5136f2015-05-11 13:39:40 -0700278
Colin Crossca860ac2016-01-04 14:34:37 -0800279type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700280 // list of modules whose object files should be linked into this module
281 // in their entirety. For static library modules, all of the .o files from the intermediate
282 // directory of the dependency will be linked into this modules .a file. For a shared library,
283 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
284 Whole_static_libs []string `android:"arch_variant"`
285
286 // list of modules that should be statically linked into this module.
287 Static_libs []string `android:"arch_variant"`
288
289 // list of modules that should be dynamically linked into this module.
290 Shared_libs []string `android:"arch_variant"`
291
Colin Crossca860ac2016-01-04 14:34:37 -0800292 // list of module-specific flags that will be used for all link steps
293 Ldflags []string `android:"arch_variant"`
294
295 // don't insert default compiler flags into asflags, cflags,
296 // cppflags, conlyflags, ldflags, or include_dirs
297 No_default_compiler_flags *bool
298
299 // list of system libraries that will be dynamically linked to
300 // shared library and executable modules. If unset, generally defaults to libc
301 // and libm. Set to [] to prevent linking against libc and libm.
302 System_shared_libs []string
303
Colin Cross7d5136f2015-05-11 13:39:40 -0700304 // allow the module to contain undefined symbols. By default,
305 // modules cannot contain undefined symbols that are not satisified by their immediate
306 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
307 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700308 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700309
Dan Willemsend67be222015-09-16 15:19:33 -0700310 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700311 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700312
Colin Cross7d5136f2015-05-11 13:39:40 -0700313 // -l arguments to pass to linker for host-provided shared libraries
314 Host_ldlibs []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800315}
Colin Cross7d5136f2015-05-11 13:39:40 -0700316
Colin Crossca860ac2016-01-04 14:34:37 -0800317type LibraryCompilerProperties struct {
318 Static struct {
319 Srcs []string `android:"arch_variant"`
320 Exclude_srcs []string `android:"arch_variant"`
321 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700322 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800323 Shared struct {
324 Srcs []string `android:"arch_variant"`
325 Exclude_srcs []string `android:"arch_variant"`
326 Cflags []string `android:"arch_variant"`
327 } `android:"arch_variant"`
328}
329
330type LibraryLinkerProperties struct {
331 Static struct {
332 Whole_static_libs []string `android:"arch_variant"`
333 Static_libs []string `android:"arch_variant"`
334 Shared_libs []string `android:"arch_variant"`
335 } `android:"arch_variant"`
336 Shared struct {
337 Whole_static_libs []string `android:"arch_variant"`
338 Static_libs []string `android:"arch_variant"`
339 Shared_libs []string `android:"arch_variant"`
340 } `android:"arch_variant"`
341
342 // local file name to pass to the linker as --version_script
343 Version_script *string `android:"arch_variant"`
344 // local file name to pass to the linker as -unexported_symbols_list
345 Unexported_symbols_list *string `android:"arch_variant"`
346 // local file name to pass to the linker as -force_symbols_not_weak_list
347 Force_symbols_not_weak_list *string `android:"arch_variant"`
348 // local file name to pass to the linker as -force_symbols_weak_list
349 Force_symbols_weak_list *string `android:"arch_variant"`
350
351 // list of directories relative to the Blueprints file that will
352 // be added to the include path using -I for any module that links against this module
353 Export_include_dirs []string `android:"arch_variant"`
354
355 // don't link in crt_begin and crt_end. This flag should only be necessary for
356 // compiling crt or libc.
357 Nocrt *bool `android:"arch_variant"`
358}
359
360type BinaryLinkerProperties struct {
361 // compile executable with -static
362 Static_executable *bool
363
364 // set the name of the output
365 Stem string `android:"arch_variant"`
366
367 // append to the name of the output
368 Suffix string `android:"arch_variant"`
369
370 // if set, add an extra objcopy --prefix-symbols= step
371 Prefix_symbols string
372}
373
374type TestLinkerProperties struct {
375 // if set, build against the gtest library. Defaults to true.
376 Gtest bool
377
378 // Create a separate binary for each source file. Useful when there is
379 // global state that can not be torn down and reset between each test suite.
380 Test_per_src *bool
381}
382
383// Properties used to compile all C or C++ modules
384type BaseProperties struct {
385 // compile module with clang instead of gcc
386 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700387
388 // Minimum sdk version supported when compiling against the ndk
389 Sdk_version string
390
Colin Crossca860ac2016-01-04 14:34:37 -0800391 // don't insert default compiler flags into asflags, cflags,
392 // cppflags, conlyflags, ldflags, or include_dirs
393 No_default_compiler_flags *bool
394}
395
396type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700397 // install to a subdirectory of the default install path for the module
398 Relative_install_path string
399}
400
Colin Crossca860ac2016-01-04 14:34:37 -0800401type UnusedProperties struct {
Colin Crosscfad1192015-11-02 16:43:11 -0800402 Native_coverage *bool
403 Required []string
404 Sanitize []string `android:"arch_variant"`
405 Sanitize_recover []string
406 Strip string
407 Tags []string
408}
409
Colin Crossca860ac2016-01-04 14:34:37 -0800410type ModuleContextIntf interface {
411 module() *Module
412 static() bool
413 staticBinary() bool
414 clang() bool
415 toolchain() Toolchain
416 noDefaultCompilerFlags() bool
417 sdk() bool
418 sdkVersion() string
419}
420
421type ModuleContext interface {
422 common.AndroidModuleContext
423 ModuleContextIntf
424}
425
426type BaseModuleContext interface {
427 common.AndroidBaseContext
428 ModuleContextIntf
429}
430
431type Customizer interface {
432 CustomizeProperties(BaseModuleContext)
433 Properties() []interface{}
434}
435
436type feature interface {
437 begin(ctx BaseModuleContext)
438 deps(ctx BaseModuleContext, deps Deps) Deps
439 flags(ctx ModuleContext, flags Flags) Flags
440 props() []interface{}
441}
442
443type compiler interface {
444 feature
445 compile(ctx ModuleContext, flags Flags) common.Paths
446}
447
448type linker interface {
449 feature
450 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles common.Paths) common.Path
451}
452
453type installer interface {
454 props() []interface{}
455 install(ctx ModuleContext, path common.Path)
456 inData() bool
457}
458
459// Module contains the properties and members used by all C/C++ module types, and implements
460// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
461// to construct the output file. Behavior can be customized with a Customizer interface
462type Module struct {
Colin Crossc472d572015-03-17 15:06:21 -0700463 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800464 common.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700465
Colin Crossca860ac2016-01-04 14:34:37 -0800466 Properties BaseProperties
467 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700468
Colin Crossca860ac2016-01-04 14:34:37 -0800469 // initialize before calling Init
470 hod common.HostOrDeviceSupported
471 multilib common.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700472
Colin Crossca860ac2016-01-04 14:34:37 -0800473 // delegates, initialize before calling Init
474 customizer Customizer
475 features []feature
476 compiler compiler
477 linker linker
478 installer installer
Colin Cross74d1ec02015-04-28 13:30:13 -0700479
Colin Crossca860ac2016-01-04 14:34:37 -0800480 deps Deps
481 outputFile common.OptionalPath
482
483 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700484}
485
Colin Crossca860ac2016-01-04 14:34:37 -0800486func (c *Module) Init() (blueprint.Module, []interface{}) {
487 props := []interface{}{&c.Properties, &c.unused}
488 if c.customizer != nil {
489 props = append(props, c.customizer.Properties()...)
490 }
491 if c.compiler != nil {
492 props = append(props, c.compiler.props()...)
493 }
494 if c.linker != nil {
495 props = append(props, c.linker.props()...)
496 }
497 if c.installer != nil {
498 props = append(props, c.installer.props()...)
499 }
500 for _, feature := range c.features {
501 props = append(props, feature.props()...)
502 }
Colin Crossc472d572015-03-17 15:06:21 -0700503
Colin Crossca860ac2016-01-04 14:34:37 -0800504 _, props = common.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700505
Colin Crossca860ac2016-01-04 14:34:37 -0800506 return common.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700507}
508
Colin Crossca860ac2016-01-04 14:34:37 -0800509type baseModuleContext struct {
510 common.AndroidBaseContext
511 moduleContextImpl
512}
513
514type moduleContext struct {
515 common.AndroidModuleContext
516 moduleContextImpl
517}
518
519type moduleContextImpl struct {
520 mod *Module
521 ctx BaseModuleContext
522}
523
524func (ctx *moduleContextImpl) module() *Module {
525 return ctx.mod
526}
527
528func (ctx *moduleContextImpl) clang() bool {
529 return ctx.mod.clang(ctx.ctx)
530}
531
532func (ctx *moduleContextImpl) toolchain() Toolchain {
533 return ctx.mod.toolchain(ctx.ctx)
534}
535
536func (ctx *moduleContextImpl) static() bool {
537 if ctx.mod.linker == nil {
538 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
539 }
540 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
541 return linker.static()
542 } else {
543 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
544 }
545}
546
547func (ctx *moduleContextImpl) staticBinary() bool {
548 if ctx.mod.linker == nil {
549 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
550 }
551 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
552 return linker.staticBinary()
553 } else {
554 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
555 }
556}
557
558func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
559 return Bool(ctx.mod.Properties.No_default_compiler_flags)
560}
561
562func (ctx *moduleContextImpl) sdk() bool {
563 return ctx.mod.Properties.Sdk_version != ""
564}
565
566func (ctx *moduleContextImpl) sdkVersion() string {
567 return ctx.mod.Properties.Sdk_version
568}
569
570func newBaseModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
571 return &Module{
572 hod: hod,
573 multilib: multilib,
574 }
575}
576
577func newModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
578 module := newBaseModule(hod, multilib)
579 module.features = []feature{
580 &stlFeature{},
581 }
582 return module
583}
584
585func (c *Module) GenerateAndroidBuildActions(actx common.AndroidModuleContext) {
586 ctx := &moduleContext{
587 AndroidModuleContext: actx,
588 moduleContextImpl: moduleContextImpl{
589 mod: c,
590 },
591 }
592 ctx.ctx = ctx
593
594 flags := Flags{
595 Toolchain: c.toolchain(ctx),
596 Clang: c.clang(ctx),
597 }
598
599 if c.compiler != nil {
600 flags = c.compiler.flags(ctx, flags)
601 }
602 if c.linker != nil {
603 flags = c.linker.flags(ctx, flags)
604 }
605 for _, feature := range c.features {
606 flags = feature.flags(ctx, flags)
607 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800608 if ctx.Failed() {
609 return
610 }
611
Colin Crossca860ac2016-01-04 14:34:37 -0800612 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
613 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
614 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800615
Colin Crossca860ac2016-01-04 14:34:37 -0800616 // Optimization to reduce size of build.ninja
617 // Replace the long list of flags for each file with a module-local variable
618 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
619 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
620 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
621 flags.CFlags = []string{"$cflags"}
622 flags.CppFlags = []string{"$cppflags"}
623 flags.AsFlags = []string{"$asflags"}
624
625 deps := c.depsToPaths(actx, c.deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800626 if ctx.Failed() {
627 return
628 }
629
Colin Cross28344522015-04-22 13:07:53 -0700630 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700631
Colin Crossca860ac2016-01-04 14:34:37 -0800632 var objFiles common.Paths
633 if c.compiler != nil {
634 objFiles = c.compiler.compile(ctx, flags)
635 if ctx.Failed() {
636 return
637 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800638 }
639
Colin Crossca860ac2016-01-04 14:34:37 -0800640 if c.linker != nil {
641 outputFile := c.linker.link(ctx, flags, deps, objFiles)
642 if ctx.Failed() {
643 return
644 }
645 c.outputFile = common.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700646
Colin Crossca860ac2016-01-04 14:34:37 -0800647 if c.installer != nil {
648 c.installer.install(ctx, outputFile)
649 if ctx.Failed() {
650 return
651 }
652 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700653 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800654}
655
Colin Crossca860ac2016-01-04 14:34:37 -0800656func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
657 if c.cachedToolchain == nil {
658 arch := ctx.Arch()
659 hod := ctx.HostOrDevice()
660 ht := ctx.HostType()
661 factory := toolchainFactories[hod][ht][arch.ArchType]
662 if factory == nil {
663 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
664 return nil
665 }
666 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800667 }
Colin Crossca860ac2016-01-04 14:34:37 -0800668 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800669}
670
Colin Crossca860ac2016-01-04 14:34:37 -0800671func (c *Module) begin(ctx BaseModuleContext) {
672 if c.compiler != nil {
673 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700674 }
Colin Crossca860ac2016-01-04 14:34:37 -0800675 if c.linker != nil {
676 c.linker.begin(ctx)
677 }
678 for _, feature := range c.features {
679 feature.begin(ctx)
680 }
681}
682
683func (c *Module) depsMutator(actx common.AndroidBottomUpMutatorContext) {
684 ctx := &baseModuleContext{
685 AndroidBaseContext: actx,
686 moduleContextImpl: moduleContextImpl{
687 mod: c,
688 },
689 }
690 ctx.ctx = ctx
691
692 if c.customizer != nil {
693 c.customizer.CustomizeProperties(ctx)
694 }
695
696 c.begin(ctx)
697
698 c.deps = Deps{}
699
700 if c.compiler != nil {
701 c.deps = c.compiler.deps(ctx, c.deps)
702 }
703 if c.linker != nil {
704 c.deps = c.linker.deps(ctx, c.deps)
705 }
706 for _, feature := range c.features {
707 c.deps = feature.deps(ctx, c.deps)
708 }
709
710 c.deps.WholeStaticLibs = lastUniqueElements(c.deps.WholeStaticLibs)
711 c.deps.StaticLibs = lastUniqueElements(c.deps.StaticLibs)
712 c.deps.LateStaticLibs = lastUniqueElements(c.deps.LateStaticLibs)
713 c.deps.SharedLibs = lastUniqueElements(c.deps.SharedLibs)
714 c.deps.LateSharedLibs = lastUniqueElements(c.deps.LateSharedLibs)
715
716 staticLibs := c.deps.WholeStaticLibs
717 staticLibs = append(staticLibs, c.deps.StaticLibs...)
718 staticLibs = append(staticLibs, c.deps.LateStaticLibs...)
719 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
720
721 sharedLibs := c.deps.SharedLibs
722 sharedLibs = append(sharedLibs, c.deps.LateSharedLibs...)
723 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedLibs...)
724
725 actx.AddDependency(ctx.module(), c.deps.ObjFiles.Strings()...)
726 if c.deps.CrtBegin != "" {
727 actx.AddDependency(ctx.module(), c.deps.CrtBegin)
728 }
729 if c.deps.CrtEnd != "" {
730 actx.AddDependency(ctx.module(), c.deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700731 }
Colin Cross6362e272015-10-29 15:25:03 -0700732}
Colin Cross21b9a242015-03-24 14:15:58 -0700733
Colin Cross6362e272015-10-29 15:25:03 -0700734func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800735 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700736 c.depsMutator(ctx)
737 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800738}
739
Colin Crossca860ac2016-01-04 14:34:37 -0800740func (c *Module) clang(ctx BaseModuleContext) bool {
741 clang := Bool(c.Properties.Clang)
742
743 if c.Properties.Clang == nil {
744 if ctx.Host() {
745 clang = true
746 }
747
748 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
749 clang = true
750 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800751 }
Colin Cross28344522015-04-22 13:07:53 -0700752
Colin Crossca860ac2016-01-04 14:34:37 -0800753 if !c.toolchain(ctx).ClangSupported() {
754 clang = false
755 }
756
757 return clang
758}
759
760func (c *Module) depsToPathsFromList(ctx common.AndroidModuleContext,
761 names []string) (modules []common.AndroidModule,
762 outputFiles common.Paths, exportedFlags []string) {
763
764 for _, n := range names {
765 found := false
766 ctx.VisitDirectDeps(func(m blueprint.Module) {
767 otherName := ctx.OtherModuleName(m)
768 if otherName != n {
769 return
770 }
771
772 if a, ok := m.(*Module); ok {
773 if !a.Enabled() {
774 ctx.ModuleErrorf("depends on disabled module %q", otherName)
775 return
776 }
777 if a.HostOrDevice() != ctx.HostOrDevice() {
778 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
779 otherName)
780 return
781 }
782
783 if outputFile := a.outputFile; outputFile.Valid() {
784 if found {
785 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
786 return
787 }
788 outputFiles = append(outputFiles, outputFile.Path())
789 modules = append(modules, a)
790 if i, ok := a.linker.(exportedFlagsProducer); ok {
791 exportedFlags = append(exportedFlags, i.exportedFlags()...)
792 }
793 found = true
794 } else {
795 ctx.ModuleErrorf("module %q missing output file", otherName)
796 return
797 }
798 } else {
799 ctx.ModuleErrorf("module %q not an android module", otherName)
800 return
801 }
802 })
803 if !found && !inList(n, ctx.GetMissingDependencies()) {
804 ctx.ModuleErrorf("unsatisified dependency on %q", n)
805 }
806 }
807
808 return modules, outputFiles, exportedFlags
809}
810
811// Convert dependency names to paths. Takes a Deps containing names and returns a PathDeps
812// containing paths
813func (c *Module) depsToPaths(ctx common.AndroidModuleContext, deps Deps) PathDeps {
814 var depPaths PathDeps
815 var newCflags []string
816
817 var wholeStaticLibModules []common.AndroidModule
818
819 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
820 c.depsToPathsFromList(ctx, deps.WholeStaticLibs)
821 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
822 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
823
824 for _, am := range wholeStaticLibModules {
825 if m, ok := am.(*Module); ok {
826 if staticLib, ok := m.linker.(*libraryLinker); ok && staticLib.static() {
827 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
828 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
829 for i := range missingDeps {
830 missingDeps[i] += postfix
831 }
832 ctx.AddMissingDependencies(missingDeps)
833 }
834 depPaths.WholeStaticLibObjFiles =
835 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
836 } else {
837 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
838 }
839 } else {
840 ctx.ModuleErrorf("module %q not an android module", ctx.OtherModuleName(m))
841 }
842 }
843
844 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, deps.StaticLibs)
845 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
846
847 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, deps.LateStaticLibs)
848 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
849
850 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, deps.SharedLibs)
851 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
852
853 _, depPaths.LateSharedLibs, newCflags = c.depsToPathsFromList(ctx, deps.LateSharedLibs)
854 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
855
856 ctx.VisitDirectDeps(func(bm blueprint.Module) {
857 if m, ok := bm.(*Module); ok {
858 otherName := ctx.OtherModuleName(m)
859 if otherName == deps.CrtBegin {
860 depPaths.CrtBegin = m.outputFile
861 } else if otherName == deps.CrtEnd {
862 depPaths.CrtEnd = m.outputFile
863 } else {
864 output := m.outputFile
865 if output.Valid() {
866 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
867 } else {
868 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
869 }
870 }
871 }
872 })
873
874 return depPaths
875}
876
877func (c *Module) InstallInData() bool {
878 if c.installer == nil {
879 return false
880 }
881 return c.installer.inData()
882}
883
884// Compiler
885
886type baseCompiler struct {
887 Properties BaseCompilerProperties
888}
889
890var _ compiler = (*baseCompiler)(nil)
891
892func (compiler *baseCompiler) props() []interface{} {
893 return []interface{}{&compiler.Properties}
894}
895
896func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
897func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps { return deps }
898
899// Create a Flags struct that collects the compile flags from global values,
900// per-target values, module type values, and per-module Blueprints properties
901func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
902 toolchain := ctx.toolchain()
903
904 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
905 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
906 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
907 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
908 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
909
Colin Cross28344522015-04-22 13:07:53 -0700910 // Include dir cflags
Colin Crossca860ac2016-01-04 14:34:37 -0800911 rootIncludeDirs := common.PathsForSource(ctx, compiler.Properties.Include_dirs)
912 localIncludeDirs := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700913 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700914 includeDirsToFlags(localIncludeDirs),
915 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700916
Colin Crossca860ac2016-01-04 14:34:37 -0800917 rootIncludeFiles := common.PathsForSource(ctx, compiler.Properties.Include_files)
918 localIncludeFiles := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700919
920 flags.GlobalFlags = append(flags.GlobalFlags,
921 includeFilesToFlags(rootIncludeFiles),
922 includeFilesToFlags(localIncludeFiles))
923
Colin Crossca860ac2016-01-04 14:34:37 -0800924 if !ctx.noDefaultCompilerFlags() {
925 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700926 flags.GlobalFlags = append(flags.GlobalFlags,
927 "${commonGlobalIncludes}",
928 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800929 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700930 }
931
932 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700933 "-I" + common.PathForModuleSrc(ctx).String(),
934 "-I" + common.PathForModuleOut(ctx).String(),
935 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700936 }...)
937 }
938
Colin Crossca860ac2016-01-04 14:34:37 -0800939 instructionSet := compiler.Properties.Instruction_set
940 if flags.RequiredInstructionSet != "" {
941 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -0800942 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800943 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
944 if flags.Clang {
945 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
946 }
947 if err != nil {
948 ctx.ModuleErrorf("%s", err)
949 }
950
951 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -0800952 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800953
Colin Cross97ba0732015-03-23 17:50:24 -0700954 if flags.Clang {
955 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -0800956 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
957 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700958 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
959 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
960 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800961
962 target := "-target " + toolchain.ClangTriple()
963 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
964
Colin Cross97ba0732015-03-23 17:50:24 -0700965 flags.CFlags = append(flags.CFlags, target, gccPrefix)
966 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
967 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800968 }
969
Colin Crossca860ac2016-01-04 14:34:37 -0800970 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -0700971 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
972
Colin Cross97ba0732015-03-23 17:50:24 -0700973 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800974 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700975 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700976 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800977 toolchain.ClangCflags(),
978 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700979 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800980
981 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800982 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700983 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700984 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800985 toolchain.Cflags(),
986 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700987 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800988 }
989
Colin Cross7b66f152015-12-15 16:07:43 -0800990 if Bool(ctx.AConfig().ProductVariables.Brillo) {
991 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
992 }
993
Colin Crossf6566ed2015-03-24 11:13:38 -0700994 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -0800995 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700996 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800997 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700998 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800999 }
1000 }
1001
Colin Cross97ba0732015-03-23 17:50:24 -07001002 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001003
Colin Cross97ba0732015-03-23 17:50:24 -07001004 if flags.Clang {
1005 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001006 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001007 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001008 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001009 }
1010
Colin Crossc4bde762015-11-23 16:11:30 -08001011 if flags.Clang {
1012 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1013 } else {
1014 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001015 }
1016
Colin Crossca860ac2016-01-04 14:34:37 -08001017 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001018 if ctx.Host() && !flags.Clang {
1019 // The host GCC doesn't support C++14 (and is deprecated, so likely
1020 // never will). Build these modules with C++11.
1021 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1022 } else {
1023 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1024 }
1025 }
1026
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001027 // We can enforce some rules more strictly in the code we own. strict
1028 // indicates if this is code that we can be stricter with. If we have
1029 // rules that we want to apply to *our* code (but maybe can't for
1030 // vendor/device specific things), we could extend this to be a ternary
1031 // value.
1032 strict := true
1033 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
1034 strict = false
1035 }
1036
1037 // Can be used to make some annotations stricter for code we can fix
1038 // (such as when we mark functions as deprecated).
1039 if strict {
1040 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1041 }
1042
Colin Cross3f40fa42015-01-30 17:27:36 -08001043 return flags
1044}
1045
Colin Crossca860ac2016-01-04 14:34:37 -08001046func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1047 // Compile files listed in c.Properties.Srcs into objects
1048 objFiles := compiler.compileObjs(ctx, flags, "", compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
1049 if ctx.Failed() {
1050 return nil
1051 }
1052
1053 var genSrcs common.Paths
1054 ctx.VisitDirectDeps(func(module blueprint.Module) {
1055 if gen, ok := module.(genrule.SourceFileGenerator); ok {
1056 genSrcs = append(genSrcs, gen.GeneratedSourceFiles()...)
1057 }
1058 })
1059
1060 if len(genSrcs) != 0 {
1061 genObjs := TransformSourceToObj(ctx, "", genSrcs, flagsToBuilderFlags(flags), nil)
1062 objFiles = append(objFiles, genObjs...)
1063 }
1064
1065 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001066}
1067
1068// Compile a list of source files into objects a specified subdirectory
Colin Crossca860ac2016-01-04 14:34:37 -08001069func (compiler *baseCompiler) compileObjs(ctx common.AndroidModuleContext, flags Flags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001070 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001071
Colin Crossca860ac2016-01-04 14:34:37 -08001072 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001073
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001074 inputFiles := ctx.ExpandSources(srcFiles, excludes)
1075 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001076
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001077 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001078}
1079
Colin Crossca860ac2016-01-04 14:34:37 -08001080// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1081type baseLinker struct {
1082 Properties BaseLinkerProperties
1083 dynamicProperties struct {
1084 VariantIsShared bool `blueprint:"mutated"`
1085 VariantIsStatic bool `blueprint:"mutated"`
1086 VariantIsStaticBinary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001087 }
Dan Willemsend30e6102016-03-30 17:35:50 -07001088
1089 runPaths []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001090}
1091
Dan Willemsend30e6102016-03-30 17:35:50 -07001092func (linker *baseLinker) begin(ctx BaseModuleContext) {
1093 if ctx.toolchain().Is64Bit() {
1094 linker.runPaths = []string{"../lib64", "lib64"}
1095 } else {
1096 linker.runPaths = []string{"../lib", "lib"}
1097 }
1098}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001099
Colin Crossca860ac2016-01-04 14:34:37 -08001100func (linker *baseLinker) props() []interface{} {
1101 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001102}
1103
Colin Crossca860ac2016-01-04 14:34:37 -08001104func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1105 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1106 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1107 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001108
Colin Cross74d1ec02015-04-28 13:30:13 -07001109 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001110 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001111 }
1112
Colin Crossf6566ed2015-03-24 11:13:38 -07001113 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001114 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001115 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1116 if !Bool(linker.Properties.No_libgcc) {
1117 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001118 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001119
Colin Crossca860ac2016-01-04 14:34:37 -08001120 if !linker.static() {
1121 if linker.Properties.System_shared_libs != nil {
1122 deps.LateSharedLibs = append(deps.LateSharedLibs,
1123 linker.Properties.System_shared_libs...)
1124 } else if !ctx.sdk() {
1125 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1126 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001127 }
Colin Cross577f6e42015-03-27 18:23:34 -07001128
Colin Crossca860ac2016-01-04 14:34:37 -08001129 if ctx.sdk() {
1130 version := ctx.sdkVersion()
1131 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001132 "ndk_libc."+version,
1133 "ndk_libm."+version,
1134 )
1135 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001136 }
1137
Colin Crossca860ac2016-01-04 14:34:37 -08001138 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001139}
1140
Colin Crossca860ac2016-01-04 14:34:37 -08001141func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1142 toolchain := ctx.toolchain()
1143
1144 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1145
1146 if !ctx.noDefaultCompilerFlags() {
1147 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1148 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1149 }
1150
1151 if flags.Clang {
1152 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1153 } else {
1154 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1155 }
1156
1157 if ctx.Host() {
1158 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1159 }
1160 }
1161
Dan Willemsend30e6102016-03-30 17:35:50 -07001162 if ctx.Host() && !linker.static() {
1163 rpath_prefix := `\$$ORIGIN/`
1164 if ctx.Darwin() {
1165 rpath_prefix = "@loader_path/"
1166 }
1167
1168 for _, rpath := range linker.runPaths {
1169 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1170 }
1171 }
1172
Dan Willemsene7174922016-03-30 17:33:52 -07001173 if flags.Clang {
1174 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1175 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001176 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1177 }
1178
1179 return flags
1180}
1181
1182func (linker *baseLinker) static() bool {
1183 return linker.dynamicProperties.VariantIsStatic
1184}
1185
1186func (linker *baseLinker) staticBinary() bool {
1187 return linker.dynamicProperties.VariantIsStaticBinary
1188}
1189
1190func (linker *baseLinker) setStatic(static bool) {
1191 linker.dynamicProperties.VariantIsStatic = static
1192}
1193
1194type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001195 // Returns true if the build options for the module have selected a static or shared build
1196 buildStatic() bool
1197 buildShared() bool
1198
1199 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001200 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001201
Colin Cross18b6dc52015-04-28 13:20:37 -07001202 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001203 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001204
1205 // Returns whether a module is a static binary
1206 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001207}
1208
Colin Crossca860ac2016-01-04 14:34:37 -08001209type exportedFlagsProducer interface {
Colin Cross28344522015-04-22 13:07:53 -07001210 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001211}
1212
Colin Crossca860ac2016-01-04 14:34:37 -08001213type baseInstaller struct {
1214 Properties InstallerProperties
1215
1216 dir string
1217 dir64 string
1218 data bool
1219
Colin Crossa2344662016-03-24 13:14:12 -07001220 path common.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001221}
1222
1223var _ installer = (*baseInstaller)(nil)
1224
1225func (installer *baseInstaller) props() []interface{} {
1226 return []interface{}{&installer.Properties}
1227}
1228
1229func (installer *baseInstaller) install(ctx ModuleContext, file common.Path) {
1230 subDir := installer.dir
1231 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1232 subDir = installer.dir64
1233 }
1234 dir := common.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
1235 installer.path = ctx.InstallFile(dir, file)
1236}
1237
1238func (installer *baseInstaller) inData() bool {
1239 return installer.data
1240}
1241
Colin Cross3f40fa42015-01-30 17:27:36 -08001242//
1243// Combined static+shared libraries
1244//
1245
Colin Crossca860ac2016-01-04 14:34:37 -08001246type libraryCompiler struct {
1247 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001248
Colin Crossca860ac2016-01-04 14:34:37 -08001249 linker *libraryLinker
1250 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001251
Colin Crossca860ac2016-01-04 14:34:37 -08001252 // For reusing static library objects for shared library
1253 reuseFrom *libraryCompiler
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001254 reuseObjFiles common.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001255}
1256
Colin Crossca860ac2016-01-04 14:34:37 -08001257var _ compiler = (*libraryCompiler)(nil)
1258
1259func (library *libraryCompiler) props() []interface{} {
1260 props := library.baseCompiler.props()
1261 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001262}
1263
Colin Crossca860ac2016-01-04 14:34:37 -08001264func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1265 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001266
Dan Willemsen490fd492015-11-24 17:53:15 -08001267 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1268 // all code is position independent, and then those warnings get promoted to
1269 // errors.
1270 if ctx.HostType() != common.Windows {
1271 flags.CFlags = append(flags.CFlags, "-fPIC")
1272 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001273
Colin Crossca860ac2016-01-04 14:34:37 -08001274 if library.linker.static() {
1275 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001276 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001277 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001278 }
1279
Colin Crossca860ac2016-01-04 14:34:37 -08001280 return flags
1281}
1282
1283func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1284 var objFiles common.Paths
1285
1286 if library.reuseFrom != library && library.reuseFrom.Properties.Static.Cflags == nil &&
1287 library.Properties.Shared.Cflags == nil {
1288 objFiles = append(common.Paths(nil), library.reuseFrom.reuseObjFiles...)
1289 } else {
1290 objFiles = library.baseCompiler.compile(ctx, flags)
1291 library.reuseObjFiles = objFiles
1292 }
1293
1294 if library.linker.static() {
1295 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceStaticLibrary,
1296 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs)...)
1297 } else {
1298 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceSharedLibrary,
1299 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs)...)
1300 }
1301
1302 return objFiles
1303}
1304
1305type libraryLinker struct {
1306 baseLinker
1307
1308 Properties LibraryLinkerProperties
1309
1310 dynamicProperties struct {
1311 BuildStatic bool `blueprint:"mutated"`
1312 BuildShared bool `blueprint:"mutated"`
1313 }
1314
1315 exportFlags []string
1316
1317 // If we're used as a whole_static_lib, our missing dependencies need
1318 // to be given
1319 wholeStaticMissingDeps []string
1320
1321 // For whole_static_libs
1322 objFiles common.Paths
1323}
1324
1325var _ linker = (*libraryLinker)(nil)
1326var _ exportedFlagsProducer = (*libraryLinker)(nil)
1327
1328func (library *libraryLinker) props() []interface{} {
1329 props := library.baseLinker.props()
1330 return append(props, &library.Properties, &library.dynamicProperties)
1331}
1332
1333func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1334 flags = library.baseLinker.flags(ctx, flags)
1335
1336 flags.Nocrt = Bool(library.Properties.Nocrt)
1337
1338 if !library.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001339 libName := ctx.ModuleName()
1340 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1341 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001342 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001343 sharedFlag = "-shared"
1344 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001345 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001346 flags.LdFlags = append(flags.LdFlags,
1347 "-nostdlib",
1348 "-Wl,--gc-sections",
1349 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001350 }
Colin Cross97ba0732015-03-23 17:50:24 -07001351
Colin Cross0af4b842015-04-30 16:36:18 -07001352 if ctx.Darwin() {
1353 flags.LdFlags = append(flags.LdFlags,
1354 "-dynamiclib",
1355 "-single_module",
1356 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001357 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001358 )
1359 } else {
1360 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001361 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001362 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001363 )
1364 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001365 }
Colin Cross97ba0732015-03-23 17:50:24 -07001366
1367 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001368}
1369
Colin Crossca860ac2016-01-04 14:34:37 -08001370func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1371 deps = library.baseLinker.deps(ctx, deps)
1372 if library.static() {
1373 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1374 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1375 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1376 } else {
1377 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1378 if !ctx.sdk() {
1379 deps.CrtBegin = "crtbegin_so"
1380 deps.CrtEnd = "crtend_so"
1381 } else {
1382 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1383 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1384 }
1385 }
1386 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1387 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1388 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1389 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001390
Colin Crossca860ac2016-01-04 14:34:37 -08001391 return deps
1392}
Colin Cross3f40fa42015-01-30 17:27:36 -08001393
Colin Crossca860ac2016-01-04 14:34:37 -08001394func (library *libraryLinker) exportedFlags() []string {
1395 return library.exportFlags
1396}
1397
1398func (library *libraryLinker) linkStatic(ctx ModuleContext,
1399 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
1400
Colin Cross21b9a242015-03-24 14:15:58 -07001401 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Crossca860ac2016-01-04 14:34:37 -08001402 library.objFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001403
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001404 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001405
Colin Cross0af4b842015-04-30 16:36:18 -07001406 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001407 TransformDarwinObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001408 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001409 TransformObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001410 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001411
Colin Crossca860ac2016-01-04 14:34:37 -08001412 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001413
1414 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001415
1416 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001417}
1418
Colin Crossca860ac2016-01-04 14:34:37 -08001419func (library *libraryLinker) linkShared(ctx ModuleContext,
1420 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001421
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001422 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001423
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001424 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001425
Colin Crossca860ac2016-01-04 14:34:37 -08001426 versionScript := common.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1427 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1428 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1429 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001430 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001431 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001432 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001433 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001434 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001435 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001436 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1437 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001438 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001439 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1440 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001441 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001442 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1443 }
1444 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001445 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001446 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1447 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001448 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001449 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001450 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001451 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001452 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001453 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001454 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001455 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001456 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001457 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001458 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001459 }
Colin Crossaee540a2015-07-06 17:48:31 -07001460 }
1461
Colin Crossca860ac2016-01-04 14:34:37 -08001462 sharedLibs := deps.SharedLibs
1463 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001464
Colin Crossca860ac2016-01-04 14:34:37 -08001465 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1466 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
1467 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, flagsToBuilderFlags(flags), outputFile)
1468
1469 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001470}
1471
Colin Crossca860ac2016-01-04 14:34:37 -08001472func (library *libraryLinker) link(ctx ModuleContext,
1473 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001474
Colin Crossca860ac2016-01-04 14:34:37 -08001475 var out common.Path
1476 if library.static() {
1477 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001478 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001479 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001480 }
1481
Colin Crossca860ac2016-01-04 14:34:37 -08001482 includeDirs := common.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)
1483 library.exportFlags = []string{includeDirsToFlags(includeDirs)}
1484 library.exportFlags = append(library.exportFlags, deps.ReexportedCflags...)
1485
1486 return out
1487}
1488
1489func (library *libraryLinker) buildStatic() bool {
1490 return library.dynamicProperties.BuildStatic
1491}
1492
1493func (library *libraryLinker) buildShared() bool {
1494 return library.dynamicProperties.BuildShared
1495}
1496
1497func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1498 return library.wholeStaticMissingDeps
1499}
1500
1501type libraryInstaller struct {
1502 baseInstaller
1503
1504 linker *libraryLinker
1505}
1506
1507func (library *libraryInstaller) install(ctx ModuleContext, file common.Path) {
1508 if !library.linker.static() {
1509 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001510 }
1511}
1512
Colin Crossca860ac2016-01-04 14:34:37 -08001513func NewLibrary(hod common.HostOrDeviceSupported, shared, static bool) *Module {
1514 module := newModule(hod, common.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001515
Colin Crossca860ac2016-01-04 14:34:37 -08001516 linker := &libraryLinker{}
1517 linker.dynamicProperties.BuildShared = shared
1518 linker.dynamicProperties.BuildStatic = static
1519 module.linker = linker
1520
1521 module.compiler = &libraryCompiler{
1522 linker: linker,
1523 }
1524 module.installer = &libraryInstaller{
1525 baseInstaller: baseInstaller{
1526 dir: "lib",
1527 dir64: "lib64",
1528 },
1529 linker: linker,
Dan Albertc403f7c2015-03-18 14:01:18 -07001530 }
1531
Colin Crossca860ac2016-01-04 14:34:37 -08001532 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001533}
1534
Colin Crossca860ac2016-01-04 14:34:37 -08001535func libraryFactory() (blueprint.Module, []interface{}) {
1536 module := NewLibrary(common.HostAndDeviceSupported, true, true)
1537 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001538}
1539
Colin Cross3f40fa42015-01-30 17:27:36 -08001540//
1541// Objects (for crt*.o)
1542//
1543
Colin Crossca860ac2016-01-04 14:34:37 -08001544type objectLinker struct {
Dan Albertc3144b12015-04-28 18:17:56 -07001545}
1546
Colin Crossca860ac2016-01-04 14:34:37 -08001547func objectFactory() (blueprint.Module, []interface{}) {
1548 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
1549 module.compiler = &baseCompiler{}
1550 module.linker = &objectLinker{}
1551 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001552}
1553
Colin Crossca860ac2016-01-04 14:34:37 -08001554func (*objectLinker) props() []interface{} {
1555 return nil
Dan Albertc3144b12015-04-28 18:17:56 -07001556}
1557
Colin Crossca860ac2016-01-04 14:34:37 -08001558func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001559
Colin Crossca860ac2016-01-04 14:34:37 -08001560func (*objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross21b9a242015-03-24 14:15:58 -07001561 // object files can't have any dynamic dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08001562 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001563}
1564
Colin Crossca860ac2016-01-04 14:34:37 -08001565func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001566 if flags.Clang {
1567 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1568 } else {
1569 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1570 }
1571
Colin Crossca860ac2016-01-04 14:34:37 -08001572 return flags
1573}
1574
1575func (object *objectLinker) link(ctx ModuleContext,
1576 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001577
Colin Cross97ba0732015-03-23 17:50:24 -07001578 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001579
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001580 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001581 if len(objFiles) == 1 {
1582 outputFile = objFiles[0]
1583 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001584 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001585 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001586 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001587 }
1588
Colin Cross3f40fa42015-01-30 17:27:36 -08001589 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001590 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001591}
1592
Colin Cross3f40fa42015-01-30 17:27:36 -08001593//
1594// Executables
1595//
1596
Colin Crossca860ac2016-01-04 14:34:37 -08001597type binaryLinker struct {
1598 baseLinker
Colin Cross7d5136f2015-05-11 13:39:40 -07001599
Colin Crossca860ac2016-01-04 14:34:37 -08001600 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001601
Colin Crossca860ac2016-01-04 14:34:37 -08001602 hostToolPath common.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001603}
1604
Colin Crossca860ac2016-01-04 14:34:37 -08001605var _ linker = (*binaryLinker)(nil)
1606
1607func (binary *binaryLinker) props() []interface{} {
1608 return append(binary.baseLinker.props(), &binary.Properties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001609}
1610
Colin Crossca860ac2016-01-04 14:34:37 -08001611func (binary *binaryLinker) buildStatic() bool {
1612 return Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001613}
1614
Colin Crossca860ac2016-01-04 14:34:37 -08001615func (binary *binaryLinker) buildShared() bool {
1616 return !Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001617}
1618
Colin Crossca860ac2016-01-04 14:34:37 -08001619func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001620 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001621 if binary.Properties.Stem != "" {
1622 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001623 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001624
Colin Crossca860ac2016-01-04 14:34:37 -08001625 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001626}
1627
Colin Crossca860ac2016-01-04 14:34:37 -08001628func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1629 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001630 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001631 if !ctx.sdk() {
1632 if Bool(binary.Properties.Static_executable) {
1633 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001634 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001635 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001636 }
Colin Crossca860ac2016-01-04 14:34:37 -08001637 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001638 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001639 if Bool(binary.Properties.Static_executable) {
1640 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001641 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001642 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001643 }
Colin Crossca860ac2016-01-04 14:34:37 -08001644 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001645 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001646
Colin Crossca860ac2016-01-04 14:34:37 -08001647 if Bool(binary.Properties.Static_executable) {
1648 if inList("libc++_static", deps.StaticLibs) {
1649 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001650 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001651 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1652 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1653 // move them to the beginning of deps.LateStaticLibs
1654 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001655 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001656 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001657 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001658 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001659 }
Colin Crossca860ac2016-01-04 14:34:37 -08001660
1661 if !Bool(binary.Properties.Static_executable) && inList("libc", deps.StaticLibs) {
1662 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1663 "from static libs or set static_executable: true")
1664 }
1665 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001666}
1667
Colin Crossca860ac2016-01-04 14:34:37 -08001668func NewBinary(hod common.HostOrDeviceSupported) *Module {
1669 module := newModule(hod, common.MultilibFirst)
1670 module.compiler = &baseCompiler{}
1671 module.linker = &binaryLinker{}
1672 module.installer = &baseInstaller{
1673 dir: "bin",
1674 }
1675 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001676}
1677
Colin Crossca860ac2016-01-04 14:34:37 -08001678func binaryFactory() (blueprint.Module, []interface{}) {
1679 module := NewBinary(common.HostAndDeviceSupported)
1680 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001681}
1682
Colin Crossca860ac2016-01-04 14:34:37 -08001683func (binary *binaryLinker) ModifyProperties(ctx ModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001684 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001685 binary.Properties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001686 }
Colin Crossca860ac2016-01-04 14:34:37 -08001687 if Bool(binary.Properties.Static_executable) {
1688 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001689 }
1690}
1691
Colin Crossca860ac2016-01-04 14:34:37 -08001692func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1693 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001694
Dan Willemsen490fd492015-11-24 17:53:15 -08001695 if ctx.Host() {
1696 flags.LdFlags = append(flags.LdFlags, "-pie")
1697 if ctx.HostType() == common.Windows {
1698 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1699 }
1700 }
1701
1702 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1703 // all code is position independent, and then those warnings get promoted to
1704 // errors.
1705 if ctx.HostType() != common.Windows {
1706 flags.CFlags = append(flags.CFlags, "-fpie")
1707 }
Colin Cross97ba0732015-03-23 17:50:24 -07001708
Colin Crossf6566ed2015-03-24 11:13:38 -07001709 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001710 if Bool(binary.Properties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001711 // Clang driver needs -static to create static executable.
1712 // However, bionic/linker uses -shared to overwrite.
1713 // Linker for x86 targets does not allow coexistance of -static and -shared,
1714 // so we add -static only if -shared is not used.
1715 if !inList("-shared", flags.LdFlags) {
1716 flags.LdFlags = append(flags.LdFlags, "-static")
1717 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001718
Colin Crossed4cf0b2015-03-26 14:43:45 -07001719 flags.LdFlags = append(flags.LdFlags,
1720 "-nostdlib",
1721 "-Bstatic",
1722 "-Wl,--gc-sections",
1723 )
1724
1725 } else {
1726 linker := "/system/bin/linker"
1727 if flags.Toolchain.Is64Bit() {
Colin Crossca860ac2016-01-04 14:34:37 -08001728 linker += "64"
Colin Crossed4cf0b2015-03-26 14:43:45 -07001729 }
1730
1731 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001732 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001733 "-nostdlib",
1734 "-Bdynamic",
1735 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1736 "-Wl,--gc-sections",
1737 "-Wl,-z,nocopyreloc",
1738 )
1739 }
Colin Cross0af4b842015-04-30 16:36:18 -07001740 } else if ctx.Darwin() {
1741 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001742 }
1743
Colin Cross97ba0732015-03-23 17:50:24 -07001744 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001745}
1746
Colin Crossca860ac2016-01-04 14:34:37 -08001747func (binary *binaryLinker) link(ctx ModuleContext,
1748 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001749
Colin Crossca860ac2016-01-04 14:34:37 -08001750 outputFile := common.PathForModuleOut(ctx, binary.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
1751 if ctx.HostOrDevice().Host() {
1752 binary.hostToolPath = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001753 }
Colin Crossca860ac2016-01-04 14:34:37 -08001754 ret := outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001755
Colin Crossca860ac2016-01-04 14:34:37 -08001756 if binary.Properties.Prefix_symbols != "" {
Colin Crossbfae8852015-03-26 14:44:11 -07001757 afterPrefixSymbols := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001758 outputFile = common.PathForModuleOut(ctx, binary.getStem(ctx)+".intermediate")
1759 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1760 flagsToBuilderFlags(flags), afterPrefixSymbols)
Colin Crossbfae8852015-03-26 14:44:11 -07001761 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001762
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001763 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001764
Colin Crossca860ac2016-01-04 14:34:37 -08001765 sharedLibs := deps.SharedLibs
1766 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1767
1768 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001769 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossca860ac2016-01-04 14:34:37 -08001770 flagsToBuilderFlags(flags), outputFile)
1771
1772 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001773}
Colin Cross3f40fa42015-01-30 17:27:36 -08001774
Colin Crossca860ac2016-01-04 14:34:37 -08001775func (binary *binaryLinker) HostToolPath() common.OptionalPath {
1776 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001777}
1778
Colin Cross6362e272015-10-29 15:25:03 -07001779func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08001780 if m, ok := mctx.Module().(*Module); ok {
1781 if test, ok := m.linker.(*testLinker); ok {
1782 if Bool(test.Properties.Test_per_src) {
1783 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
1784 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1785 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1786 }
1787 tests := mctx.CreateLocalVariations(testNames...)
1788 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1789 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
1790 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
1791 }
Colin Cross6002e052015-09-16 16:00:08 -07001792 }
1793 }
1794 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001795}
1796
Colin Crossca860ac2016-01-04 14:34:37 -08001797type testLinker struct {
1798 binaryLinker
1799 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001800}
1801
Dan Willemsend30e6102016-03-30 17:35:50 -07001802func (test *testLinker) begin(ctx BaseModuleContext) {
1803 test.binaryLinker.begin(ctx)
1804
1805 runpath := "../../lib"
1806 if ctx.toolchain().Is64Bit() {
1807 runpath += "64"
1808 }
1809 test.runPaths = append([]string{runpath}, test.runPaths...)
1810}
1811
Colin Crossca860ac2016-01-04 14:34:37 -08001812func (test *testLinker) props() []interface{} {
1813 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07001814}
1815
Colin Crossca860ac2016-01-04 14:34:37 -08001816func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
1817 flags = test.binaryLinker.flags(ctx, flags)
1818
1819 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001820 return flags
1821 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001822
Colin Cross97ba0732015-03-23 17:50:24 -07001823 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001824 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001825 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001826
1827 if ctx.HostType() == common.Windows {
1828 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1829 } else {
1830 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1831 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1832 }
1833 } else {
1834 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001835 }
1836
1837 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001838 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001839 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001840
Colin Cross21b9a242015-03-24 14:15:58 -07001841 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001842}
1843
Colin Crossca860ac2016-01-04 14:34:37 -08001844func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1845 if test.Properties.Gtest {
1846 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001847 }
Colin Crossca860ac2016-01-04 14:34:37 -08001848 deps = test.binaryLinker.deps(ctx, deps)
1849 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07001850}
1851
Colin Crossca860ac2016-01-04 14:34:37 -08001852type testInstaller struct {
1853 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08001854}
1855
Colin Crossca860ac2016-01-04 14:34:37 -08001856func (installer *testInstaller) install(ctx ModuleContext, file common.Path) {
1857 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
1858 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
1859 installer.baseInstaller.install(ctx, file)
1860}
1861
1862func NewTest(hod common.HostOrDeviceSupported) *Module {
1863 module := newModule(hod, common.MultilibBoth)
1864 module.compiler = &baseCompiler{}
1865 linker := &testLinker{}
1866 linker.Properties.Gtest = true
1867 module.linker = linker
1868 module.installer = &testInstaller{
1869 baseInstaller: baseInstaller{
1870 dir: "nativetest",
1871 dir64: "nativetest64",
1872 data: true,
1873 },
Dan Albertc403f7c2015-03-18 14:01:18 -07001874 }
Colin Crossca860ac2016-01-04 14:34:37 -08001875 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001876}
1877
Colin Crossca860ac2016-01-04 14:34:37 -08001878func testFactory() (blueprint.Module, []interface{}) {
1879 module := NewTest(common.HostAndDeviceSupported)
1880 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001881}
1882
Colin Crossca860ac2016-01-04 14:34:37 -08001883type benchmarkLinker struct {
1884 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07001885}
1886
Colin Crossca860ac2016-01-04 14:34:37 -08001887func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1888 deps = benchmark.binaryLinker.deps(ctx, deps)
1889 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
1890 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07001891}
1892
Colin Crossca860ac2016-01-04 14:34:37 -08001893func NewBenchmark(hod common.HostOrDeviceSupported) *Module {
1894 module := newModule(hod, common.MultilibFirst)
1895 module.compiler = &baseCompiler{}
1896 module.linker = &benchmarkLinker{}
1897 module.installer = &baseInstaller{
1898 dir: "nativetest",
1899 dir64: "nativetest64",
1900 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07001901 }
Colin Crossca860ac2016-01-04 14:34:37 -08001902 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07001903}
1904
Colin Crossca860ac2016-01-04 14:34:37 -08001905func benchmarkFactory() (blueprint.Module, []interface{}) {
1906 module := NewBenchmark(common.HostAndDeviceSupported)
1907 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001908}
1909
Colin Cross3f40fa42015-01-30 17:27:36 -08001910//
1911// Static library
1912//
1913
Colin Crossca860ac2016-01-04 14:34:37 -08001914func libraryStaticFactory() (blueprint.Module, []interface{}) {
1915 module := NewLibrary(common.HostAndDeviceSupported, false, true)
1916 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001917}
1918
1919//
1920// Shared libraries
1921//
1922
Colin Crossca860ac2016-01-04 14:34:37 -08001923func librarySharedFactory() (blueprint.Module, []interface{}) {
1924 module := NewLibrary(common.HostAndDeviceSupported, true, false)
1925 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001926}
1927
1928//
1929// Host static library
1930//
1931
Colin Crossca860ac2016-01-04 14:34:37 -08001932func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
1933 module := NewLibrary(common.HostSupported, false, true)
1934 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001935}
1936
1937//
1938// Host Shared libraries
1939//
1940
Colin Crossca860ac2016-01-04 14:34:37 -08001941func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
1942 module := NewLibrary(common.HostSupported, true, false)
1943 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001944}
1945
1946//
1947// Host Binaries
1948//
1949
Colin Crossca860ac2016-01-04 14:34:37 -08001950func binaryHostFactory() (blueprint.Module, []interface{}) {
1951 module := NewBinary(common.HostSupported)
1952 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001953}
1954
1955//
Colin Cross1f8f2342015-03-26 16:09:47 -07001956// Host Tests
1957//
1958
Colin Crossca860ac2016-01-04 14:34:37 -08001959func testHostFactory() (blueprint.Module, []interface{}) {
1960 module := NewTest(common.HostSupported)
1961 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07001962}
1963
1964//
Colin Cross2ba19d92015-05-07 15:44:20 -07001965// Host Benchmarks
1966//
1967
Colin Crossca860ac2016-01-04 14:34:37 -08001968func benchmarkHostFactory() (blueprint.Module, []interface{}) {
1969 module := NewBenchmark(common.HostSupported)
1970 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001971}
1972
1973//
Colin Crosscfad1192015-11-02 16:43:11 -08001974// Defaults
1975//
Colin Crossca860ac2016-01-04 14:34:37 -08001976type Defaults struct {
Colin Crosscfad1192015-11-02 16:43:11 -08001977 common.AndroidModuleBase
1978 common.DefaultsModule
1979}
1980
Colin Crossca860ac2016-01-04 14:34:37 -08001981func (*Defaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001982}
1983
Colin Crossca860ac2016-01-04 14:34:37 -08001984func defaultsFactory() (blueprint.Module, []interface{}) {
1985 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001986
1987 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08001988 &BaseProperties{},
1989 &BaseCompilerProperties{},
1990 &BaseLinkerProperties{},
1991 &LibraryCompilerProperties{},
1992 &LibraryLinkerProperties{},
1993 &BinaryLinkerProperties{},
1994 &TestLinkerProperties{},
1995 &UnusedProperties{},
1996 &StlProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001997 }
1998
Dan Willemsen218f6562015-07-08 18:13:11 -07001999 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
2000 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002001
2002 return common.InitDefaultsModule(module, module, propertyStructs...)
2003}
2004
2005//
Colin Cross3f40fa42015-01-30 17:27:36 -08002006// Device libraries shipped with gcc
2007//
2008
Colin Crossca860ac2016-01-04 14:34:37 -08002009type toolchainLibraryLinker struct {
2010 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002011}
2012
Colin Crossca860ac2016-01-04 14:34:37 -08002013var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2014
2015func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002016 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002017 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002018}
2019
Colin Crossca860ac2016-01-04 14:34:37 -08002020func (*toolchainLibraryLinker) buildStatic() bool {
2021 return true
2022}
Colin Cross3f40fa42015-01-30 17:27:36 -08002023
Colin Crossca860ac2016-01-04 14:34:37 -08002024func (*toolchainLibraryLinker) buildShared() bool {
2025 return false
2026}
2027
2028func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
2029 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2030 module.compiler = &baseCompiler{}
2031 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002032 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002033 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002034}
2035
Colin Crossca860ac2016-01-04 14:34:37 -08002036func (library *toolchainLibraryLinker) link(ctx ModuleContext,
2037 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002038
2039 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002040 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002041
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002042 if flags.Clang {
2043 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2044 }
2045
Colin Crossca860ac2016-01-04 14:34:37 -08002046 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002047
2048 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002049
Colin Crossca860ac2016-01-04 14:34:37 -08002050 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002051}
2052
Dan Albertbe961682015-03-18 23:38:50 -07002053// NDK prebuilt libraries.
2054//
2055// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2056// either (with the exception of the shared STLs, which are installed to the app's directory rather
2057// than to the system image).
2058
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002059func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
2060 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
2061 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07002062}
2063
Dan Albertc3144b12015-04-28 18:17:56 -07002064func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002065 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002066
2067 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2068 // We want to translate to just NAME.EXT
2069 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2070 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002071 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002072}
2073
Colin Crossca860ac2016-01-04 14:34:37 -08002074type ndkPrebuiltObjectLinker struct {
2075 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002076}
2077
Colin Crossca860ac2016-01-04 14:34:37 -08002078func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002079 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002080 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002081}
2082
Colin Crossca860ac2016-01-04 14:34:37 -08002083func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
2084 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2085 module.linker = &ndkPrebuiltObjectLinker{}
2086 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002087}
2088
Colin Crossca860ac2016-01-04 14:34:37 -08002089func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
2090 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002091 // A null build step, but it sets up the output path.
2092 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2093 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2094 }
2095
Colin Crossca860ac2016-01-04 14:34:37 -08002096 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002097}
2098
Colin Crossca860ac2016-01-04 14:34:37 -08002099type ndkPrebuiltLibraryLinker struct {
2100 libraryLinker
2101 Properties struct {
2102 Export_include_dirs []string `android:"arch_variant"`
2103 }
Dan Albertc3144b12015-04-28 18:17:56 -07002104}
2105
Colin Crossca860ac2016-01-04 14:34:37 -08002106var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2107var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002108
Colin Crossca860ac2016-01-04 14:34:37 -08002109func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
2110 return []interface{}{&ndk.Properties}
Dan Albertbe961682015-03-18 23:38:50 -07002111}
2112
Colin Crossca860ac2016-01-04 14:34:37 -08002113func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002114 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002115 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002116}
2117
Colin Crossca860ac2016-01-04 14:34:37 -08002118func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2119 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2120 linker := &ndkPrebuiltLibraryLinker{}
2121 linker.dynamicProperties.BuildShared = true
2122 module.linker = linker
2123 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002124}
2125
Colin Crossca860ac2016-01-04 14:34:37 -08002126func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
2127 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002128 // A null build step, but it sets up the output path.
2129 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2130 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2131 }
2132
Colin Crossca860ac2016-01-04 14:34:37 -08002133 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2134 ndk.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002135
Colin Crossca860ac2016-01-04 14:34:37 -08002136 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2137 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002138}
2139
2140// The NDK STLs are slightly different from the prebuilt system libraries:
2141// * Are not specific to each platform version.
2142// * The libraries are not in a predictable location for each STL.
2143
Colin Crossca860ac2016-01-04 14:34:37 -08002144type ndkPrebuiltStlLinker struct {
2145 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002146}
2147
Colin Crossca860ac2016-01-04 14:34:37 -08002148func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2149 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2150 linker := &ndkPrebuiltStlLinker{}
2151 linker.dynamicProperties.BuildShared = true
2152 module.linker = linker
2153 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002154}
2155
Colin Crossca860ac2016-01-04 14:34:37 -08002156func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2157 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2158 linker := &ndkPrebuiltStlLinker{}
2159 linker.dynamicProperties.BuildStatic = true
2160 module.linker = linker
2161 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002162}
2163
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002164func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002165 gccVersion := toolchain.GccVersion()
2166 var libDir string
2167 switch stl {
2168 case "libstlport":
2169 libDir = "cxx-stl/stlport/libs"
2170 case "libc++":
2171 libDir = "cxx-stl/llvm-libc++/libs"
2172 case "libgnustl":
2173 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2174 }
2175
2176 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002177 ndkSrcRoot := "prebuilts/ndk/current/sources"
2178 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002179 }
2180
2181 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002182 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002183}
2184
Colin Crossca860ac2016-01-04 14:34:37 -08002185func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
2186 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002187 // A null build step, but it sets up the output path.
2188 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2189 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2190 }
2191
Colin Crossca860ac2016-01-04 14:34:37 -08002192 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2193 ndk.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002194
2195 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002196 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002197 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002198 libExt = staticLibraryExtension
2199 }
2200
2201 stlName := strings.TrimSuffix(libName, "_shared")
2202 stlName = strings.TrimSuffix(stlName, "_static")
2203 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002204 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002205}
2206
Colin Cross6362e272015-10-29 15:25:03 -07002207func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002208 if m, ok := mctx.Module().(*Module); ok {
2209 if m.linker != nil {
2210 if linker, ok := m.linker.(baseLinkerInterface); ok {
2211 var modules []blueprint.Module
2212 if linker.buildStatic() && linker.buildShared() {
2213 modules = mctx.CreateLocalVariations("static", "shared")
2214 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
Colin Cross7b106e42016-03-25 17:31:43 -07002215 modules[0].(*Module).installer = nil
Colin Crossca860ac2016-01-04 14:34:37 -08002216 modules[1].(*Module).linker.(baseLinkerInterface).setStatic(false)
2217 } else if linker.buildStatic() {
2218 modules = mctx.CreateLocalVariations("static")
2219 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
Colin Cross7b106e42016-03-25 17:31:43 -07002220 modules[0].(*Module).installer = nil
Colin Crossca860ac2016-01-04 14:34:37 -08002221 } else if linker.buildShared() {
2222 modules = mctx.CreateLocalVariations("shared")
2223 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2224 } else {
2225 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2226 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002227
Colin Crossca860ac2016-01-04 14:34:37 -08002228 if _, ok := m.compiler.(*libraryCompiler); ok {
2229 reuseFrom := modules[0].(*Module).compiler.(*libraryCompiler)
2230 for _, m := range modules {
2231 m.(*Module).compiler.(*libraryCompiler).reuseFrom = reuseFrom
2232 }
2233 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002234 }
2235 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002236 }
2237}
Colin Cross74d1ec02015-04-28 13:30:13 -07002238
2239// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2240// modifies the slice contents in place, and returns a subslice of the original slice
2241func lastUniqueElements(list []string) []string {
2242 totalSkip := 0
2243 for i := len(list) - 1; i >= totalSkip; i-- {
2244 skip := 0
2245 for j := i - 1; j >= totalSkip; j-- {
2246 if list[i] == list[j] {
2247 skip++
2248 } else {
2249 list[j+skip] = list[j]
2250 }
2251 }
2252 totalSkip += skip
2253 }
2254 return list[totalSkip:]
2255}
Colin Cross06a931b2015-10-28 17:23:31 -07002256
2257var Bool = proptools.Bool