blob: 3b3128f360e8621690e3627798fe059558bcae90 [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")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
82
83 // COMMON_RELEASE_CFLAGS
84 "-DNDEBUG",
85 "-UDEBUG",
86 }
87
88 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080089 "-fdiagnostics-color",
90
Colin Cross3f40fa42015-01-30 17:27:36 -080091 // TARGET_ERROR_FLAGS
92 "-Werror=return-type",
93 "-Werror=non-virtual-dtor",
94 "-Werror=address",
95 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080096 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080097 }
98
99 hostGlobalCflags = []string{}
100
101 commonGlobalCppflags = []string{
102 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700103 }
104
Dan Willemsenbe03f342016-03-03 17:21:04 -0800105 noOverrideGlobalCflags = []string{
106 "-Werror=int-to-pointer-cast",
107 "-Werror=pointer-to-int-cast",
108 }
109
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700110 illegalFlags = []string{
111 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800112 }
113)
114
115func init() {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700116 if common.CurrentHostType() == common.Linux {
117 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
118 }
119
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
121 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
122 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800123 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800124
125 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
126
127 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800128 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800129 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800130 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800131 pctx.StaticVariable("hostClangGlobalCflags",
132 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800133 pctx.StaticVariable("noOverrideClangGlobalCflags",
134 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
135
Tim Kilbournf2948142015-03-11 12:03:03 -0700136 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800137 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800138
139 // Everything in this list is a crime against abstraction and dependency tracking.
140 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800141 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 []string{
143 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800144 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700145 "hardware/libhardware/include",
146 "hardware/libhardware_legacy/include",
147 "hardware/ril/include",
148 "libnativehelper/include",
149 "frameworks/native/include",
150 "frameworks/native/opengl/include",
151 "frameworks/av/include",
152 "frameworks/base/include",
153 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800154 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
155 // with this, since there is no associated library.
156 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
157 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800158
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700159 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
160 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
161 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
162 return override, nil
163 }
164 return "${clangDefaultBase}", nil
165 })
166 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
167 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
168 return override, nil
169 }
Colin Cross7253e0b2016-03-21 15:12:34 -0700170 return "clang-2690385", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700171 })
172 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800173}
174
Colin Crossca860ac2016-01-04 14:34:37 -0800175type Deps struct {
176 SharedLibs, LateSharedLibs []string
177 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700178
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179 ObjFiles common.Paths
180
181 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700182
Colin Cross97ba0732015-03-23 17:50:24 -0700183 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700184}
185
Colin Crossca860ac2016-01-04 14:34:37 -0800186type PathDeps struct {
187 SharedLibs, LateSharedLibs common.Paths
188 StaticLibs, LateStaticLibs, WholeStaticLibs common.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189
190 ObjFiles common.Paths
191 WholeStaticLibObjFiles common.Paths
192
193 Cflags, ReexportedCflags []string
194
195 CrtBegin, CrtEnd common.OptionalPath
196}
197
Colin Crossca860ac2016-01-04 14:34:37 -0800198type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700199 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
200 AsFlags []string // Flags that apply to assembly source files
201 CFlags []string // Flags that apply to C and C++ source files
202 ConlyFlags []string // Flags that apply to C source files
203 CppFlags []string // Flags that apply to C++ source files
204 YaccFlags []string // Flags that apply to Yacc source files
205 LdFlags []string // Flags that apply to linker command lines
206
207 Nocrt bool
208 Toolchain Toolchain
209 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800210
211 RequiredInstructionSet string
Colin Crossc472d572015-03-17 15:06:21 -0700212}
213
Colin Crossca860ac2016-01-04 14:34:37 -0800214type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700215 // 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 -0700216 Srcs []string `android:"arch_variant"`
217
218 // list of source files that should not be used to build the C/C++ module.
219 // This is most useful in the arch/multilib variants to remove non-common files
220 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700221
222 // list of module-specific flags that will be used for C and C++ compiles.
223 Cflags []string `android:"arch_variant"`
224
225 // list of module-specific flags that will be used for C++ compiles
226 Cppflags []string `android:"arch_variant"`
227
228 // list of module-specific flags that will be used for C compiles
229 Conlyflags []string `android:"arch_variant"`
230
231 // list of module-specific flags that will be used for .S compiles
232 Asflags []string `android:"arch_variant"`
233
Colin Crossca860ac2016-01-04 14:34:37 -0800234 // list of module-specific flags that will be used for C and C++ compiles when
235 // compiling with clang
236 Clang_cflags []string `android:"arch_variant"`
237
238 // list of module-specific flags that will be used for .S compiles when
239 // compiling with clang
240 Clang_asflags []string `android:"arch_variant"`
241
Colin Cross7d5136f2015-05-11 13:39:40 -0700242 // list of module-specific flags that will be used for .y and .yy compiles
243 Yaccflags []string
244
Colin Cross7d5136f2015-05-11 13:39:40 -0700245 // the instruction set architecture to use to compile the C/C++
246 // module.
247 Instruction_set string `android:"arch_variant"`
248
249 // list of directories relative to the root of the source tree that will
250 // be added to the include path using -I.
251 // If possible, don't use this. If adding paths from the current directory use
252 // local_include_dirs, if adding paths from other modules use export_include_dirs in
253 // that module.
254 Include_dirs []string `android:"arch_variant"`
255
Colin Cross39d97f22015-09-14 12:30:50 -0700256 // list of files relative to the root of the source tree that will be included
257 // using -include.
258 // If possible, don't use this.
259 Include_files []string `android:"arch_variant"`
260
Colin Cross7d5136f2015-05-11 13:39:40 -0700261 // list of directories relative to the Blueprints file that will
262 // be added to the include path using -I
263 Local_include_dirs []string `android:"arch_variant"`
264
Colin Cross39d97f22015-09-14 12:30:50 -0700265 // list of files relative to the Blueprints file that will be included
266 // using -include.
267 // If possible, don't use this.
268 Local_include_files []string `android:"arch_variant"`
269
Colin Crossca860ac2016-01-04 14:34:37 -0800270 // pass -frtti instead of -fno-rtti
271 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700272
Colin Crossca860ac2016-01-04 14:34:37 -0800273 Debug, Release struct {
274 // list of module-specific flags that will be used for C and C++ compiles in debug or
275 // release builds
276 Cflags []string `android:"arch_variant"`
277 } `android:"arch_variant"`
278}
Colin Cross7d5136f2015-05-11 13:39:40 -0700279
Colin Crossca860ac2016-01-04 14:34:37 -0800280type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700281 // list of modules whose object files should be linked into this module
282 // in their entirety. For static library modules, all of the .o files from the intermediate
283 // directory of the dependency will be linked into this modules .a file. For a shared library,
284 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
285 Whole_static_libs []string `android:"arch_variant"`
286
287 // list of modules that should be statically linked into this module.
288 Static_libs []string `android:"arch_variant"`
289
290 // list of modules that should be dynamically linked into this module.
291 Shared_libs []string `android:"arch_variant"`
292
Colin Crossca860ac2016-01-04 14:34:37 -0800293 // list of module-specific flags that will be used for all link steps
294 Ldflags []string `android:"arch_variant"`
295
296 // don't insert default compiler flags into asflags, cflags,
297 // cppflags, conlyflags, ldflags, or include_dirs
298 No_default_compiler_flags *bool
299
300 // list of system libraries that will be dynamically linked to
301 // shared library and executable modules. If unset, generally defaults to libc
302 // and libm. Set to [] to prevent linking against libc and libm.
303 System_shared_libs []string
304
Colin Cross7d5136f2015-05-11 13:39:40 -0700305 // allow the module to contain undefined symbols. By default,
306 // modules cannot contain undefined symbols that are not satisified by their immediate
307 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
308 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700309 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700310
Dan Willemsend67be222015-09-16 15:19:33 -0700311 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700312 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700313
Colin Cross7d5136f2015-05-11 13:39:40 -0700314 // -l arguments to pass to linker for host-provided shared libraries
315 Host_ldlibs []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800316}
Colin Cross7d5136f2015-05-11 13:39:40 -0700317
Colin Crossca860ac2016-01-04 14:34:37 -0800318type LibraryCompilerProperties struct {
319 Static struct {
320 Srcs []string `android:"arch_variant"`
321 Exclude_srcs []string `android:"arch_variant"`
322 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700323 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800324 Shared struct {
325 Srcs []string `android:"arch_variant"`
326 Exclude_srcs []string `android:"arch_variant"`
327 Cflags []string `android:"arch_variant"`
328 } `android:"arch_variant"`
329}
330
331type LibraryLinkerProperties struct {
332 Static struct {
333 Whole_static_libs []string `android:"arch_variant"`
334 Static_libs []string `android:"arch_variant"`
335 Shared_libs []string `android:"arch_variant"`
336 } `android:"arch_variant"`
337 Shared struct {
338 Whole_static_libs []string `android:"arch_variant"`
339 Static_libs []string `android:"arch_variant"`
340 Shared_libs []string `android:"arch_variant"`
341 } `android:"arch_variant"`
342
343 // local file name to pass to the linker as --version_script
344 Version_script *string `android:"arch_variant"`
345 // local file name to pass to the linker as -unexported_symbols_list
346 Unexported_symbols_list *string `android:"arch_variant"`
347 // local file name to pass to the linker as -force_symbols_not_weak_list
348 Force_symbols_not_weak_list *string `android:"arch_variant"`
349 // local file name to pass to the linker as -force_symbols_weak_list
350 Force_symbols_weak_list *string `android:"arch_variant"`
351
352 // list of directories relative to the Blueprints file that will
353 // be added to the include path using -I for any module that links against this module
354 Export_include_dirs []string `android:"arch_variant"`
355
356 // don't link in crt_begin and crt_end. This flag should only be necessary for
357 // compiling crt or libc.
358 Nocrt *bool `android:"arch_variant"`
359}
360
361type BinaryLinkerProperties struct {
362 // compile executable with -static
363 Static_executable *bool
364
365 // set the name of the output
366 Stem string `android:"arch_variant"`
367
368 // append to the name of the output
369 Suffix string `android:"arch_variant"`
370
371 // if set, add an extra objcopy --prefix-symbols= step
372 Prefix_symbols string
373}
374
375type TestLinkerProperties struct {
376 // if set, build against the gtest library. Defaults to true.
377 Gtest bool
378
379 // Create a separate binary for each source file. Useful when there is
380 // global state that can not be torn down and reset between each test suite.
381 Test_per_src *bool
382}
383
384// Properties used to compile all C or C++ modules
385type BaseProperties struct {
386 // compile module with clang instead of gcc
387 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700388
389 // Minimum sdk version supported when compiling against the ndk
390 Sdk_version string
391
Colin Crossca860ac2016-01-04 14:34:37 -0800392 // don't insert default compiler flags into asflags, cflags,
393 // cppflags, conlyflags, ldflags, or include_dirs
394 No_default_compiler_flags *bool
395}
396
397type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700398 // install to a subdirectory of the default install path for the module
399 Relative_install_path string
400}
401
Colin Crossca860ac2016-01-04 14:34:37 -0800402type UnusedProperties struct {
Colin Crosscfad1192015-11-02 16:43:11 -0800403 Native_coverage *bool
404 Required []string
405 Sanitize []string `android:"arch_variant"`
406 Sanitize_recover []string
407 Strip string
408 Tags []string
409}
410
Colin Crossca860ac2016-01-04 14:34:37 -0800411type ModuleContextIntf interface {
412 module() *Module
413 static() bool
414 staticBinary() bool
415 clang() bool
416 toolchain() Toolchain
417 noDefaultCompilerFlags() bool
418 sdk() bool
419 sdkVersion() string
420}
421
422type ModuleContext interface {
423 common.AndroidModuleContext
424 ModuleContextIntf
425}
426
427type BaseModuleContext interface {
428 common.AndroidBaseContext
429 ModuleContextIntf
430}
431
432type Customizer interface {
433 CustomizeProperties(BaseModuleContext)
434 Properties() []interface{}
435}
436
437type feature interface {
438 begin(ctx BaseModuleContext)
439 deps(ctx BaseModuleContext, deps Deps) Deps
440 flags(ctx ModuleContext, flags Flags) Flags
441 props() []interface{}
442}
443
444type compiler interface {
445 feature
446 compile(ctx ModuleContext, flags Flags) common.Paths
447}
448
449type linker interface {
450 feature
451 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles common.Paths) common.Path
452}
453
454type installer interface {
455 props() []interface{}
456 install(ctx ModuleContext, path common.Path)
457 inData() bool
458}
459
460// Module contains the properties and members used by all C/C++ module types, and implements
461// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
462// to construct the output file. Behavior can be customized with a Customizer interface
463type Module struct {
Colin Crossc472d572015-03-17 15:06:21 -0700464 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800465 common.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700466
Colin Crossca860ac2016-01-04 14:34:37 -0800467 Properties BaseProperties
468 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700469
Colin Crossca860ac2016-01-04 14:34:37 -0800470 // initialize before calling Init
471 hod common.HostOrDeviceSupported
472 multilib common.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700473
Colin Crossca860ac2016-01-04 14:34:37 -0800474 // delegates, initialize before calling Init
475 customizer Customizer
476 features []feature
477 compiler compiler
478 linker linker
479 installer installer
Colin Cross74d1ec02015-04-28 13:30:13 -0700480
Colin Crossca860ac2016-01-04 14:34:37 -0800481 deps Deps
482 outputFile common.OptionalPath
483
484 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700485}
486
Colin Crossca860ac2016-01-04 14:34:37 -0800487func (c *Module) Init() (blueprint.Module, []interface{}) {
488 props := []interface{}{&c.Properties, &c.unused}
489 if c.customizer != nil {
490 props = append(props, c.customizer.Properties()...)
491 }
492 if c.compiler != nil {
493 props = append(props, c.compiler.props()...)
494 }
495 if c.linker != nil {
496 props = append(props, c.linker.props()...)
497 }
498 if c.installer != nil {
499 props = append(props, c.installer.props()...)
500 }
501 for _, feature := range c.features {
502 props = append(props, feature.props()...)
503 }
Colin Crossc472d572015-03-17 15:06:21 -0700504
Colin Crossca860ac2016-01-04 14:34:37 -0800505 _, props = common.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700506
Colin Crossca860ac2016-01-04 14:34:37 -0800507 return common.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700508}
509
Colin Crossca860ac2016-01-04 14:34:37 -0800510type baseModuleContext struct {
511 common.AndroidBaseContext
512 moduleContextImpl
513}
514
515type moduleContext struct {
516 common.AndroidModuleContext
517 moduleContextImpl
518}
519
520type moduleContextImpl struct {
521 mod *Module
522 ctx BaseModuleContext
523}
524
525func (ctx *moduleContextImpl) module() *Module {
526 return ctx.mod
527}
528
529func (ctx *moduleContextImpl) clang() bool {
530 return ctx.mod.clang(ctx.ctx)
531}
532
533func (ctx *moduleContextImpl) toolchain() Toolchain {
534 return ctx.mod.toolchain(ctx.ctx)
535}
536
537func (ctx *moduleContextImpl) static() bool {
538 if ctx.mod.linker == nil {
539 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
540 }
541 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
542 return linker.static()
543 } else {
544 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
545 }
546}
547
548func (ctx *moduleContextImpl) staticBinary() bool {
549 if ctx.mod.linker == nil {
550 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
551 }
552 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
553 return linker.staticBinary()
554 } else {
555 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
556 }
557}
558
559func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
560 return Bool(ctx.mod.Properties.No_default_compiler_flags)
561}
562
563func (ctx *moduleContextImpl) sdk() bool {
564 return ctx.mod.Properties.Sdk_version != ""
565}
566
567func (ctx *moduleContextImpl) sdkVersion() string {
568 return ctx.mod.Properties.Sdk_version
569}
570
571func newBaseModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
572 return &Module{
573 hod: hod,
574 multilib: multilib,
575 }
576}
577
578func newModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
579 module := newBaseModule(hod, multilib)
580 module.features = []feature{
581 &stlFeature{},
582 }
583 return module
584}
585
586func (c *Module) GenerateAndroidBuildActions(actx common.AndroidModuleContext) {
587 ctx := &moduleContext{
588 AndroidModuleContext: actx,
589 moduleContextImpl: moduleContextImpl{
590 mod: c,
591 },
592 }
593 ctx.ctx = ctx
594
595 flags := Flags{
596 Toolchain: c.toolchain(ctx),
597 Clang: c.clang(ctx),
598 }
599
600 if c.compiler != nil {
601 flags = c.compiler.flags(ctx, flags)
602 }
603 if c.linker != nil {
604 flags = c.linker.flags(ctx, flags)
605 }
606 for _, feature := range c.features {
607 flags = feature.flags(ctx, flags)
608 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800609 if ctx.Failed() {
610 return
611 }
612
Colin Crossca860ac2016-01-04 14:34:37 -0800613 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
614 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
615 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800616
Colin Crossca860ac2016-01-04 14:34:37 -0800617 // Optimization to reduce size of build.ninja
618 // Replace the long list of flags for each file with a module-local variable
619 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
620 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
621 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
622 flags.CFlags = []string{"$cflags"}
623 flags.CppFlags = []string{"$cppflags"}
624 flags.AsFlags = []string{"$asflags"}
625
626 deps := c.depsToPaths(actx, c.deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800627 if ctx.Failed() {
628 return
629 }
630
Colin Cross28344522015-04-22 13:07:53 -0700631 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700632
Colin Crossca860ac2016-01-04 14:34:37 -0800633 var objFiles common.Paths
634 if c.compiler != nil {
635 objFiles = c.compiler.compile(ctx, flags)
636 if ctx.Failed() {
637 return
638 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800639 }
640
Colin Crossca860ac2016-01-04 14:34:37 -0800641 if c.linker != nil {
642 outputFile := c.linker.link(ctx, flags, deps, objFiles)
643 if ctx.Failed() {
644 return
645 }
646 c.outputFile = common.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700647
Colin Crossca860ac2016-01-04 14:34:37 -0800648 if c.installer != nil {
649 c.installer.install(ctx, outputFile)
650 if ctx.Failed() {
651 return
652 }
653 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700654 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800655}
656
Colin Crossca860ac2016-01-04 14:34:37 -0800657func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
658 if c.cachedToolchain == nil {
659 arch := ctx.Arch()
660 hod := ctx.HostOrDevice()
661 ht := ctx.HostType()
662 factory := toolchainFactories[hod][ht][arch.ArchType]
663 if factory == nil {
664 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
665 return nil
666 }
667 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800668 }
Colin Crossca860ac2016-01-04 14:34:37 -0800669 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800670}
671
Colin Crossca860ac2016-01-04 14:34:37 -0800672func (c *Module) begin(ctx BaseModuleContext) {
673 if c.compiler != nil {
674 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700675 }
Colin Crossca860ac2016-01-04 14:34:37 -0800676 if c.linker != nil {
677 c.linker.begin(ctx)
678 }
679 for _, feature := range c.features {
680 feature.begin(ctx)
681 }
682}
683
684func (c *Module) depsMutator(actx common.AndroidBottomUpMutatorContext) {
685 ctx := &baseModuleContext{
686 AndroidBaseContext: actx,
687 moduleContextImpl: moduleContextImpl{
688 mod: c,
689 },
690 }
691 ctx.ctx = ctx
692
693 if c.customizer != nil {
694 c.customizer.CustomizeProperties(ctx)
695 }
696
697 c.begin(ctx)
698
699 c.deps = Deps{}
700
701 if c.compiler != nil {
702 c.deps = c.compiler.deps(ctx, c.deps)
703 }
704 if c.linker != nil {
705 c.deps = c.linker.deps(ctx, c.deps)
706 }
707 for _, feature := range c.features {
708 c.deps = feature.deps(ctx, c.deps)
709 }
710
711 c.deps.WholeStaticLibs = lastUniqueElements(c.deps.WholeStaticLibs)
712 c.deps.StaticLibs = lastUniqueElements(c.deps.StaticLibs)
713 c.deps.LateStaticLibs = lastUniqueElements(c.deps.LateStaticLibs)
714 c.deps.SharedLibs = lastUniqueElements(c.deps.SharedLibs)
715 c.deps.LateSharedLibs = lastUniqueElements(c.deps.LateSharedLibs)
716
717 staticLibs := c.deps.WholeStaticLibs
718 staticLibs = append(staticLibs, c.deps.StaticLibs...)
719 staticLibs = append(staticLibs, c.deps.LateStaticLibs...)
720 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
721
722 sharedLibs := c.deps.SharedLibs
723 sharedLibs = append(sharedLibs, c.deps.LateSharedLibs...)
724 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedLibs...)
725
726 actx.AddDependency(ctx.module(), c.deps.ObjFiles.Strings()...)
727 if c.deps.CrtBegin != "" {
728 actx.AddDependency(ctx.module(), c.deps.CrtBegin)
729 }
730 if c.deps.CrtEnd != "" {
731 actx.AddDependency(ctx.module(), c.deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700732 }
Colin Cross6362e272015-10-29 15:25:03 -0700733}
Colin Cross21b9a242015-03-24 14:15:58 -0700734
Colin Cross6362e272015-10-29 15:25:03 -0700735func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800736 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700737 c.depsMutator(ctx)
738 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800739}
740
Colin Crossca860ac2016-01-04 14:34:37 -0800741func (c *Module) clang(ctx BaseModuleContext) bool {
742 clang := Bool(c.Properties.Clang)
743
744 if c.Properties.Clang == nil {
745 if ctx.Host() {
746 clang = true
747 }
748
749 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
750 clang = true
751 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800752 }
Colin Cross28344522015-04-22 13:07:53 -0700753
Colin Crossca860ac2016-01-04 14:34:37 -0800754 if !c.toolchain(ctx).ClangSupported() {
755 clang = false
756 }
757
758 return clang
759}
760
761func (c *Module) depsToPathsFromList(ctx common.AndroidModuleContext,
762 names []string) (modules []common.AndroidModule,
763 outputFiles common.Paths, exportedFlags []string) {
764
765 for _, n := range names {
766 found := false
767 ctx.VisitDirectDeps(func(m blueprint.Module) {
768 otherName := ctx.OtherModuleName(m)
769 if otherName != n {
770 return
771 }
772
773 if a, ok := m.(*Module); ok {
774 if !a.Enabled() {
775 ctx.ModuleErrorf("depends on disabled module %q", otherName)
776 return
777 }
778 if a.HostOrDevice() != ctx.HostOrDevice() {
779 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
780 otherName)
781 return
782 }
783
784 if outputFile := a.outputFile; outputFile.Valid() {
785 if found {
786 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
787 return
788 }
789 outputFiles = append(outputFiles, outputFile.Path())
790 modules = append(modules, a)
791 if i, ok := a.linker.(exportedFlagsProducer); ok {
792 exportedFlags = append(exportedFlags, i.exportedFlags()...)
793 }
794 found = true
795 } else {
796 ctx.ModuleErrorf("module %q missing output file", otherName)
797 return
798 }
799 } else {
800 ctx.ModuleErrorf("module %q not an android module", otherName)
801 return
802 }
803 })
804 if !found && !inList(n, ctx.GetMissingDependencies()) {
805 ctx.ModuleErrorf("unsatisified dependency on %q", n)
806 }
807 }
808
809 return modules, outputFiles, exportedFlags
810}
811
812// Convert dependency names to paths. Takes a Deps containing names and returns a PathDeps
813// containing paths
814func (c *Module) depsToPaths(ctx common.AndroidModuleContext, deps Deps) PathDeps {
815 var depPaths PathDeps
816 var newCflags []string
817
818 var wholeStaticLibModules []common.AndroidModule
819
820 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
821 c.depsToPathsFromList(ctx, deps.WholeStaticLibs)
822 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
823 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
824
825 for _, am := range wholeStaticLibModules {
826 if m, ok := am.(*Module); ok {
827 if staticLib, ok := m.linker.(*libraryLinker); ok && staticLib.static() {
828 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
829 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
830 for i := range missingDeps {
831 missingDeps[i] += postfix
832 }
833 ctx.AddMissingDependencies(missingDeps)
834 }
835 depPaths.WholeStaticLibObjFiles =
836 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
837 } else {
838 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
839 }
840 } else {
841 ctx.ModuleErrorf("module %q not an android module", ctx.OtherModuleName(m))
842 }
843 }
844
845 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, deps.StaticLibs)
846 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
847
848 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, deps.LateStaticLibs)
849 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
850
851 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, deps.SharedLibs)
852 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
853
854 _, depPaths.LateSharedLibs, newCflags = c.depsToPathsFromList(ctx, deps.LateSharedLibs)
855 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
856
857 ctx.VisitDirectDeps(func(bm blueprint.Module) {
858 if m, ok := bm.(*Module); ok {
859 otherName := ctx.OtherModuleName(m)
860 if otherName == deps.CrtBegin {
861 depPaths.CrtBegin = m.outputFile
862 } else if otherName == deps.CrtEnd {
863 depPaths.CrtEnd = m.outputFile
864 } else {
865 output := m.outputFile
866 if output.Valid() {
867 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
868 } else {
869 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
870 }
871 }
872 }
873 })
874
875 return depPaths
876}
877
878func (c *Module) InstallInData() bool {
879 if c.installer == nil {
880 return false
881 }
882 return c.installer.inData()
883}
884
885// Compiler
886
887type baseCompiler struct {
888 Properties BaseCompilerProperties
889}
890
891var _ compiler = (*baseCompiler)(nil)
892
893func (compiler *baseCompiler) props() []interface{} {
894 return []interface{}{&compiler.Properties}
895}
896
897func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
898func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps { return deps }
899
900// Create a Flags struct that collects the compile flags from global values,
901// per-target values, module type values, and per-module Blueprints properties
902func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
903 toolchain := ctx.toolchain()
904
905 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
906 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
907 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
908 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
909 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
910
Colin Cross28344522015-04-22 13:07:53 -0700911 // Include dir cflags
Colin Crossca860ac2016-01-04 14:34:37 -0800912 rootIncludeDirs := common.PathsForSource(ctx, compiler.Properties.Include_dirs)
913 localIncludeDirs := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700914 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700915 includeDirsToFlags(localIncludeDirs),
916 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700917
Colin Crossca860ac2016-01-04 14:34:37 -0800918 rootIncludeFiles := common.PathsForSource(ctx, compiler.Properties.Include_files)
919 localIncludeFiles := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700920
921 flags.GlobalFlags = append(flags.GlobalFlags,
922 includeFilesToFlags(rootIncludeFiles),
923 includeFilesToFlags(localIncludeFiles))
924
Colin Crossca860ac2016-01-04 14:34:37 -0800925 if !ctx.noDefaultCompilerFlags() {
926 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700927 flags.GlobalFlags = append(flags.GlobalFlags,
928 "${commonGlobalIncludes}",
929 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800930 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700931 }
932
933 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700934 "-I" + common.PathForModuleSrc(ctx).String(),
935 "-I" + common.PathForModuleOut(ctx).String(),
936 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700937 }...)
938 }
939
Colin Crossca860ac2016-01-04 14:34:37 -0800940 instructionSet := compiler.Properties.Instruction_set
941 if flags.RequiredInstructionSet != "" {
942 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -0800943 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800944 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
945 if flags.Clang {
946 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
947 }
948 if err != nil {
949 ctx.ModuleErrorf("%s", err)
950 }
951
952 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -0800953 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800954
Colin Cross97ba0732015-03-23 17:50:24 -0700955 if flags.Clang {
956 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -0800957 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
958 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700959 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
960 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
961 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800962
963 target := "-target " + toolchain.ClangTriple()
964 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
965
Colin Cross97ba0732015-03-23 17:50:24 -0700966 flags.CFlags = append(flags.CFlags, target, gccPrefix)
967 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
968 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800969 }
970
Colin Crossca860ac2016-01-04 14:34:37 -0800971 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -0700972 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
973
Colin Cross97ba0732015-03-23 17:50:24 -0700974 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800975 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700976 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700977 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800978 toolchain.ClangCflags(),
979 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700980 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800981
982 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800983 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700984 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700985 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800986 toolchain.Cflags(),
987 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700988 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800989 }
990
Colin Cross7b66f152015-12-15 16:07:43 -0800991 if Bool(ctx.AConfig().ProductVariables.Brillo) {
992 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
993 }
994
Colin Crossf6566ed2015-03-24 11:13:38 -0700995 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -0800996 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700997 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800998 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700999 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001000 }
1001 }
1002
Colin Cross97ba0732015-03-23 17:50:24 -07001003 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001004
Colin Cross97ba0732015-03-23 17:50:24 -07001005 if flags.Clang {
1006 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001007 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001008 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001009 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001010 }
1011
Colin Crossc4bde762015-11-23 16:11:30 -08001012 if flags.Clang {
1013 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1014 } else {
1015 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001016 }
1017
Colin Crossca860ac2016-01-04 14:34:37 -08001018 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001019 if ctx.Host() && !flags.Clang {
1020 // The host GCC doesn't support C++14 (and is deprecated, so likely
1021 // never will). Build these modules with C++11.
1022 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1023 } else {
1024 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1025 }
1026 }
1027
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001028 // We can enforce some rules more strictly in the code we own. strict
1029 // indicates if this is code that we can be stricter with. If we have
1030 // rules that we want to apply to *our* code (but maybe can't for
1031 // vendor/device specific things), we could extend this to be a ternary
1032 // value.
1033 strict := true
1034 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
1035 strict = false
1036 }
1037
1038 // Can be used to make some annotations stricter for code we can fix
1039 // (such as when we mark functions as deprecated).
1040 if strict {
1041 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1042 }
1043
Colin Cross3f40fa42015-01-30 17:27:36 -08001044 return flags
1045}
1046
Colin Crossca860ac2016-01-04 14:34:37 -08001047func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1048 // Compile files listed in c.Properties.Srcs into objects
1049 objFiles := compiler.compileObjs(ctx, flags, "", compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
1050 if ctx.Failed() {
1051 return nil
1052 }
1053
1054 var genSrcs common.Paths
1055 ctx.VisitDirectDeps(func(module blueprint.Module) {
1056 if gen, ok := module.(genrule.SourceFileGenerator); ok {
1057 genSrcs = append(genSrcs, gen.GeneratedSourceFiles()...)
1058 }
1059 })
1060
1061 if len(genSrcs) != 0 {
1062 genObjs := TransformSourceToObj(ctx, "", genSrcs, flagsToBuilderFlags(flags), nil)
1063 objFiles = append(objFiles, genObjs...)
1064 }
1065
1066 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001067}
1068
1069// Compile a list of source files into objects a specified subdirectory
Colin Crossca860ac2016-01-04 14:34:37 -08001070func (compiler *baseCompiler) compileObjs(ctx common.AndroidModuleContext, flags Flags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001071 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001072
Colin Crossca860ac2016-01-04 14:34:37 -08001073 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001074
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001075 inputFiles := ctx.ExpandSources(srcFiles, excludes)
1076 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001077
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001078 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001079}
1080
Colin Crossca860ac2016-01-04 14:34:37 -08001081// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1082type baseLinker struct {
1083 Properties BaseLinkerProperties
1084 dynamicProperties struct {
1085 VariantIsShared bool `blueprint:"mutated"`
1086 VariantIsStatic bool `blueprint:"mutated"`
1087 VariantIsStaticBinary bool `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001088 }
Dan Willemsend30e6102016-03-30 17:35:50 -07001089
1090 runPaths []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001091}
1092
Dan Willemsend30e6102016-03-30 17:35:50 -07001093func (linker *baseLinker) begin(ctx BaseModuleContext) {
1094 if ctx.toolchain().Is64Bit() {
1095 linker.runPaths = []string{"../lib64", "lib64"}
1096 } else {
1097 linker.runPaths = []string{"../lib", "lib"}
1098 }
1099}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001100
Colin Crossca860ac2016-01-04 14:34:37 -08001101func (linker *baseLinker) props() []interface{} {
1102 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001103}
1104
Colin Crossca860ac2016-01-04 14:34:37 -08001105func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1106 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1107 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1108 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001109
Colin Cross74d1ec02015-04-28 13:30:13 -07001110 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001111 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001112 }
1113
Colin Crossf6566ed2015-03-24 11:13:38 -07001114 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001115 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001116 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1117 if !Bool(linker.Properties.No_libgcc) {
1118 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001119 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001120
Colin Crossca860ac2016-01-04 14:34:37 -08001121 if !linker.static() {
1122 if linker.Properties.System_shared_libs != nil {
1123 deps.LateSharedLibs = append(deps.LateSharedLibs,
1124 linker.Properties.System_shared_libs...)
1125 } else if !ctx.sdk() {
1126 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1127 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001128 }
Colin Cross577f6e42015-03-27 18:23:34 -07001129
Colin Crossca860ac2016-01-04 14:34:37 -08001130 if ctx.sdk() {
1131 version := ctx.sdkVersion()
1132 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001133 "ndk_libc."+version,
1134 "ndk_libm."+version,
1135 )
1136 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001137 }
1138
Colin Crossca860ac2016-01-04 14:34:37 -08001139 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001140}
1141
Colin Crossca860ac2016-01-04 14:34:37 -08001142func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1143 toolchain := ctx.toolchain()
1144
1145 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1146
1147 if !ctx.noDefaultCompilerFlags() {
1148 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1149 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1150 }
1151
1152 if flags.Clang {
1153 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1154 } else {
1155 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1156 }
1157
1158 if ctx.Host() {
1159 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1160 }
1161 }
1162
Dan Willemsend30e6102016-03-30 17:35:50 -07001163 if ctx.Host() && !linker.static() {
1164 rpath_prefix := `\$$ORIGIN/`
1165 if ctx.Darwin() {
1166 rpath_prefix = "@loader_path/"
1167 }
1168
1169 for _, rpath := range linker.runPaths {
1170 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1171 }
1172 }
1173
Dan Willemsene7174922016-03-30 17:33:52 -07001174 if flags.Clang {
1175 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1176 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001177 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1178 }
1179
1180 return flags
1181}
1182
1183func (linker *baseLinker) static() bool {
1184 return linker.dynamicProperties.VariantIsStatic
1185}
1186
1187func (linker *baseLinker) staticBinary() bool {
1188 return linker.dynamicProperties.VariantIsStaticBinary
1189}
1190
1191func (linker *baseLinker) setStatic(static bool) {
1192 linker.dynamicProperties.VariantIsStatic = static
1193}
1194
1195type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001196 // Returns true if the build options for the module have selected a static or shared build
1197 buildStatic() bool
1198 buildShared() bool
1199
1200 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001201 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001202
Colin Cross18b6dc52015-04-28 13:20:37 -07001203 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001204 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001205
1206 // Returns whether a module is a static binary
1207 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001208}
1209
Colin Crossca860ac2016-01-04 14:34:37 -08001210type exportedFlagsProducer interface {
Colin Cross28344522015-04-22 13:07:53 -07001211 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001212}
1213
Colin Crossca860ac2016-01-04 14:34:37 -08001214type baseInstaller struct {
1215 Properties InstallerProperties
1216
1217 dir string
1218 dir64 string
1219 data bool
1220
Colin Crossa2344662016-03-24 13:14:12 -07001221 path common.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001222}
1223
1224var _ installer = (*baseInstaller)(nil)
1225
1226func (installer *baseInstaller) props() []interface{} {
1227 return []interface{}{&installer.Properties}
1228}
1229
1230func (installer *baseInstaller) install(ctx ModuleContext, file common.Path) {
1231 subDir := installer.dir
1232 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1233 subDir = installer.dir64
1234 }
1235 dir := common.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
1236 installer.path = ctx.InstallFile(dir, file)
1237}
1238
1239func (installer *baseInstaller) inData() bool {
1240 return installer.data
1241}
1242
Colin Cross3f40fa42015-01-30 17:27:36 -08001243//
1244// Combined static+shared libraries
1245//
1246
Colin Crossca860ac2016-01-04 14:34:37 -08001247type libraryCompiler struct {
1248 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001249
Colin Crossca860ac2016-01-04 14:34:37 -08001250 linker *libraryLinker
1251 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001252
Colin Crossca860ac2016-01-04 14:34:37 -08001253 // For reusing static library objects for shared library
1254 reuseFrom *libraryCompiler
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001255 reuseObjFiles common.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001256}
1257
Colin Crossca860ac2016-01-04 14:34:37 -08001258var _ compiler = (*libraryCompiler)(nil)
1259
1260func (library *libraryCompiler) props() []interface{} {
1261 props := library.baseCompiler.props()
1262 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001263}
1264
Colin Crossca860ac2016-01-04 14:34:37 -08001265func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1266 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001267
Dan Willemsen490fd492015-11-24 17:53:15 -08001268 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1269 // all code is position independent, and then those warnings get promoted to
1270 // errors.
1271 if ctx.HostType() != common.Windows {
1272 flags.CFlags = append(flags.CFlags, "-fPIC")
1273 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001274
Colin Crossca860ac2016-01-04 14:34:37 -08001275 if library.linker.static() {
1276 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001277 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001278 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001279 }
1280
Colin Crossca860ac2016-01-04 14:34:37 -08001281 return flags
1282}
1283
1284func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1285 var objFiles common.Paths
1286
1287 if library.reuseFrom != library && library.reuseFrom.Properties.Static.Cflags == nil &&
1288 library.Properties.Shared.Cflags == nil {
1289 objFiles = append(common.Paths(nil), library.reuseFrom.reuseObjFiles...)
1290 } else {
1291 objFiles = library.baseCompiler.compile(ctx, flags)
1292 library.reuseObjFiles = objFiles
1293 }
1294
1295 if library.linker.static() {
1296 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceStaticLibrary,
1297 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs)...)
1298 } else {
1299 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceSharedLibrary,
1300 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs)...)
1301 }
1302
1303 return objFiles
1304}
1305
1306type libraryLinker struct {
1307 baseLinker
1308
1309 Properties LibraryLinkerProperties
1310
1311 dynamicProperties struct {
1312 BuildStatic bool `blueprint:"mutated"`
1313 BuildShared bool `blueprint:"mutated"`
1314 }
1315
1316 exportFlags []string
1317
1318 // If we're used as a whole_static_lib, our missing dependencies need
1319 // to be given
1320 wholeStaticMissingDeps []string
1321
1322 // For whole_static_libs
1323 objFiles common.Paths
1324}
1325
1326var _ linker = (*libraryLinker)(nil)
1327var _ exportedFlagsProducer = (*libraryLinker)(nil)
1328
1329func (library *libraryLinker) props() []interface{} {
1330 props := library.baseLinker.props()
1331 return append(props, &library.Properties, &library.dynamicProperties)
1332}
1333
1334func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1335 flags = library.baseLinker.flags(ctx, flags)
1336
1337 flags.Nocrt = Bool(library.Properties.Nocrt)
1338
1339 if !library.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001340 libName := ctx.ModuleName()
1341 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1342 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001343 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001344 sharedFlag = "-shared"
1345 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001346 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001347 flags.LdFlags = append(flags.LdFlags,
1348 "-nostdlib",
1349 "-Wl,--gc-sections",
1350 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001351 }
Colin Cross97ba0732015-03-23 17:50:24 -07001352
Colin Cross0af4b842015-04-30 16:36:18 -07001353 if ctx.Darwin() {
1354 flags.LdFlags = append(flags.LdFlags,
1355 "-dynamiclib",
1356 "-single_module",
1357 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001358 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001359 )
1360 } else {
1361 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001362 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001363 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001364 )
1365 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001366 }
Colin Cross97ba0732015-03-23 17:50:24 -07001367
1368 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001369}
1370
Colin Crossca860ac2016-01-04 14:34:37 -08001371func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1372 deps = library.baseLinker.deps(ctx, deps)
1373 if library.static() {
1374 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1375 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1376 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1377 } else {
1378 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1379 if !ctx.sdk() {
1380 deps.CrtBegin = "crtbegin_so"
1381 deps.CrtEnd = "crtend_so"
1382 } else {
1383 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1384 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1385 }
1386 }
1387 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1388 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1389 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1390 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001391
Colin Crossca860ac2016-01-04 14:34:37 -08001392 return deps
1393}
Colin Cross3f40fa42015-01-30 17:27:36 -08001394
Colin Crossca860ac2016-01-04 14:34:37 -08001395func (library *libraryLinker) exportedFlags() []string {
1396 return library.exportFlags
1397}
1398
1399func (library *libraryLinker) linkStatic(ctx ModuleContext,
1400 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
1401
Colin Cross21b9a242015-03-24 14:15:58 -07001402 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Crossca860ac2016-01-04 14:34:37 -08001403 library.objFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001404
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001405 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001406
Colin Cross0af4b842015-04-30 16:36:18 -07001407 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001408 TransformDarwinObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001409 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001410 TransformObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001411 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001412
Colin Crossca860ac2016-01-04 14:34:37 -08001413 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001414
1415 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001416
1417 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001418}
1419
Colin Crossca860ac2016-01-04 14:34:37 -08001420func (library *libraryLinker) linkShared(ctx ModuleContext,
1421 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001422
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001423 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001424
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001425 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001426
Colin Crossca860ac2016-01-04 14:34:37 -08001427 versionScript := common.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1428 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1429 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1430 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001431 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001432 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001433 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001434 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001435 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001437 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1438 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001439 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001440 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1441 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001442 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001443 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1444 }
1445 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001446 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001447 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1448 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001449 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001450 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001451 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001452 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001453 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001454 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001455 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001456 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001457 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001458 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001459 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001460 }
Colin Crossaee540a2015-07-06 17:48:31 -07001461 }
1462
Colin Crossca860ac2016-01-04 14:34:37 -08001463 sharedLibs := deps.SharedLibs
1464 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001465
Colin Crossca860ac2016-01-04 14:34:37 -08001466 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1467 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
1468 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, flagsToBuilderFlags(flags), outputFile)
1469
1470 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001471}
1472
Colin Crossca860ac2016-01-04 14:34:37 -08001473func (library *libraryLinker) link(ctx ModuleContext,
1474 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001475
Colin Crossca860ac2016-01-04 14:34:37 -08001476 var out common.Path
1477 if library.static() {
1478 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001479 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001480 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001481 }
1482
Colin Crossca860ac2016-01-04 14:34:37 -08001483 includeDirs := common.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)
1484 library.exportFlags = []string{includeDirsToFlags(includeDirs)}
1485 library.exportFlags = append(library.exportFlags, deps.ReexportedCflags...)
1486
1487 return out
1488}
1489
1490func (library *libraryLinker) buildStatic() bool {
1491 return library.dynamicProperties.BuildStatic
1492}
1493
1494func (library *libraryLinker) buildShared() bool {
1495 return library.dynamicProperties.BuildShared
1496}
1497
1498func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1499 return library.wholeStaticMissingDeps
1500}
1501
1502type libraryInstaller struct {
1503 baseInstaller
1504
1505 linker *libraryLinker
1506}
1507
1508func (library *libraryInstaller) install(ctx ModuleContext, file common.Path) {
1509 if !library.linker.static() {
1510 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001511 }
1512}
1513
Colin Crossca860ac2016-01-04 14:34:37 -08001514func NewLibrary(hod common.HostOrDeviceSupported, shared, static bool) *Module {
1515 module := newModule(hod, common.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001516
Colin Crossca860ac2016-01-04 14:34:37 -08001517 linker := &libraryLinker{}
1518 linker.dynamicProperties.BuildShared = shared
1519 linker.dynamicProperties.BuildStatic = static
1520 module.linker = linker
1521
1522 module.compiler = &libraryCompiler{
1523 linker: linker,
1524 }
1525 module.installer = &libraryInstaller{
1526 baseInstaller: baseInstaller{
1527 dir: "lib",
1528 dir64: "lib64",
1529 },
1530 linker: linker,
Dan Albertc403f7c2015-03-18 14:01:18 -07001531 }
1532
Colin Crossca860ac2016-01-04 14:34:37 -08001533 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001534}
1535
Colin Crossca860ac2016-01-04 14:34:37 -08001536func libraryFactory() (blueprint.Module, []interface{}) {
1537 module := NewLibrary(common.HostAndDeviceSupported, true, true)
1538 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001539}
1540
Colin Cross3f40fa42015-01-30 17:27:36 -08001541//
1542// Objects (for crt*.o)
1543//
1544
Colin Crossca860ac2016-01-04 14:34:37 -08001545type objectLinker struct {
Dan Albertc3144b12015-04-28 18:17:56 -07001546}
1547
Colin Crossca860ac2016-01-04 14:34:37 -08001548func objectFactory() (blueprint.Module, []interface{}) {
1549 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
1550 module.compiler = &baseCompiler{}
1551 module.linker = &objectLinker{}
1552 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001553}
1554
Colin Crossca860ac2016-01-04 14:34:37 -08001555func (*objectLinker) props() []interface{} {
1556 return nil
Dan Albertc3144b12015-04-28 18:17:56 -07001557}
1558
Colin Crossca860ac2016-01-04 14:34:37 -08001559func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001560
Colin Crossca860ac2016-01-04 14:34:37 -08001561func (*objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross21b9a242015-03-24 14:15:58 -07001562 // object files can't have any dynamic dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08001563 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001564}
1565
Colin Crossca860ac2016-01-04 14:34:37 -08001566func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001567 if flags.Clang {
1568 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1569 } else {
1570 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1571 }
1572
Colin Crossca860ac2016-01-04 14:34:37 -08001573 return flags
1574}
1575
1576func (object *objectLinker) link(ctx ModuleContext,
1577 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001578
Colin Cross97ba0732015-03-23 17:50:24 -07001579 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001580
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001581 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001582 if len(objFiles) == 1 {
1583 outputFile = objFiles[0]
1584 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001585 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001586 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001587 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001588 }
1589
Colin Cross3f40fa42015-01-30 17:27:36 -08001590 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001591 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001592}
1593
Colin Cross3f40fa42015-01-30 17:27:36 -08001594//
1595// Executables
1596//
1597
Colin Crossca860ac2016-01-04 14:34:37 -08001598type binaryLinker struct {
1599 baseLinker
Colin Cross7d5136f2015-05-11 13:39:40 -07001600
Colin Crossca860ac2016-01-04 14:34:37 -08001601 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001602
Colin Crossca860ac2016-01-04 14:34:37 -08001603 hostToolPath common.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001604}
1605
Colin Crossca860ac2016-01-04 14:34:37 -08001606var _ linker = (*binaryLinker)(nil)
1607
1608func (binary *binaryLinker) props() []interface{} {
1609 return append(binary.baseLinker.props(), &binary.Properties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001610}
1611
Colin Crossca860ac2016-01-04 14:34:37 -08001612func (binary *binaryLinker) buildStatic() bool {
1613 return Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001614}
1615
Colin Crossca860ac2016-01-04 14:34:37 -08001616func (binary *binaryLinker) buildShared() bool {
1617 return !Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001618}
1619
Colin Crossca860ac2016-01-04 14:34:37 -08001620func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001621 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001622 if binary.Properties.Stem != "" {
1623 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001624 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001625
Colin Crossca860ac2016-01-04 14:34:37 -08001626 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001627}
1628
Colin Crossca860ac2016-01-04 14:34:37 -08001629func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1630 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001631 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001632 if !ctx.sdk() {
1633 if Bool(binary.Properties.Static_executable) {
1634 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001635 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001636 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001637 }
Colin Crossca860ac2016-01-04 14:34:37 -08001638 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001639 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001640 if Bool(binary.Properties.Static_executable) {
1641 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001642 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001643 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001644 }
Colin Crossca860ac2016-01-04 14:34:37 -08001645 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001646 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001647
Colin Crossca860ac2016-01-04 14:34:37 -08001648 if Bool(binary.Properties.Static_executable) {
1649 if inList("libc++_static", deps.StaticLibs) {
1650 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001651 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001652 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1653 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1654 // move them to the beginning of deps.LateStaticLibs
1655 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001656 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001657 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001658 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001659 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001660 }
Colin Crossca860ac2016-01-04 14:34:37 -08001661
1662 if !Bool(binary.Properties.Static_executable) && inList("libc", deps.StaticLibs) {
1663 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1664 "from static libs or set static_executable: true")
1665 }
1666 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001667}
1668
Colin Crossca860ac2016-01-04 14:34:37 -08001669func NewBinary(hod common.HostOrDeviceSupported) *Module {
1670 module := newModule(hod, common.MultilibFirst)
1671 module.compiler = &baseCompiler{}
1672 module.linker = &binaryLinker{}
1673 module.installer = &baseInstaller{
1674 dir: "bin",
1675 }
1676 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001677}
1678
Colin Crossca860ac2016-01-04 14:34:37 -08001679func binaryFactory() (blueprint.Module, []interface{}) {
1680 module := NewBinary(common.HostAndDeviceSupported)
1681 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001682}
1683
Colin Crossca860ac2016-01-04 14:34:37 -08001684func (binary *binaryLinker) ModifyProperties(ctx ModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001685 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001686 binary.Properties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001687 }
Colin Crossca860ac2016-01-04 14:34:37 -08001688 if Bool(binary.Properties.Static_executable) {
1689 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001690 }
1691}
1692
Colin Crossca860ac2016-01-04 14:34:37 -08001693func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1694 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001695
Dan Willemsen490fd492015-11-24 17:53:15 -08001696 if ctx.Host() {
1697 flags.LdFlags = append(flags.LdFlags, "-pie")
1698 if ctx.HostType() == common.Windows {
1699 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1700 }
1701 }
1702
1703 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1704 // all code is position independent, and then those warnings get promoted to
1705 // errors.
1706 if ctx.HostType() != common.Windows {
1707 flags.CFlags = append(flags.CFlags, "-fpie")
1708 }
Colin Cross97ba0732015-03-23 17:50:24 -07001709
Colin Crossf6566ed2015-03-24 11:13:38 -07001710 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001711 if Bool(binary.Properties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001712 // Clang driver needs -static to create static executable.
1713 // However, bionic/linker uses -shared to overwrite.
1714 // Linker for x86 targets does not allow coexistance of -static and -shared,
1715 // so we add -static only if -shared is not used.
1716 if !inList("-shared", flags.LdFlags) {
1717 flags.LdFlags = append(flags.LdFlags, "-static")
1718 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001719
Colin Crossed4cf0b2015-03-26 14:43:45 -07001720 flags.LdFlags = append(flags.LdFlags,
1721 "-nostdlib",
1722 "-Bstatic",
1723 "-Wl,--gc-sections",
1724 )
1725
1726 } else {
1727 linker := "/system/bin/linker"
1728 if flags.Toolchain.Is64Bit() {
Colin Crossca860ac2016-01-04 14:34:37 -08001729 linker += "64"
Colin Crossed4cf0b2015-03-26 14:43:45 -07001730 }
1731
1732 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001733 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001734 "-nostdlib",
1735 "-Bdynamic",
1736 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1737 "-Wl,--gc-sections",
1738 "-Wl,-z,nocopyreloc",
1739 )
1740 }
Colin Cross0af4b842015-04-30 16:36:18 -07001741 } else if ctx.Darwin() {
1742 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001743 }
1744
Colin Cross97ba0732015-03-23 17:50:24 -07001745 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001746}
1747
Colin Crossca860ac2016-01-04 14:34:37 -08001748func (binary *binaryLinker) link(ctx ModuleContext,
1749 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001750
Colin Crossca860ac2016-01-04 14:34:37 -08001751 outputFile := common.PathForModuleOut(ctx, binary.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
1752 if ctx.HostOrDevice().Host() {
1753 binary.hostToolPath = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001754 }
Colin Crossca860ac2016-01-04 14:34:37 -08001755 ret := outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001756
Colin Crossca860ac2016-01-04 14:34:37 -08001757 if binary.Properties.Prefix_symbols != "" {
Colin Crossbfae8852015-03-26 14:44:11 -07001758 afterPrefixSymbols := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001759 outputFile = common.PathForModuleOut(ctx, binary.getStem(ctx)+".intermediate")
1760 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1761 flagsToBuilderFlags(flags), afterPrefixSymbols)
Colin Crossbfae8852015-03-26 14:44:11 -07001762 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001763
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001764 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001765
Colin Crossca860ac2016-01-04 14:34:37 -08001766 sharedLibs := deps.SharedLibs
1767 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1768
1769 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001770 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossca860ac2016-01-04 14:34:37 -08001771 flagsToBuilderFlags(flags), outputFile)
1772
1773 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001774}
Colin Cross3f40fa42015-01-30 17:27:36 -08001775
Colin Crossca860ac2016-01-04 14:34:37 -08001776func (binary *binaryLinker) HostToolPath() common.OptionalPath {
1777 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001778}
1779
Colin Cross6362e272015-10-29 15:25:03 -07001780func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08001781 if m, ok := mctx.Module().(*Module); ok {
1782 if test, ok := m.linker.(*testLinker); ok {
1783 if Bool(test.Properties.Test_per_src) {
1784 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
1785 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1786 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1787 }
1788 tests := mctx.CreateLocalVariations(testNames...)
1789 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1790 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
1791 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
1792 }
Colin Cross6002e052015-09-16 16:00:08 -07001793 }
1794 }
1795 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001796}
1797
Colin Crossca860ac2016-01-04 14:34:37 -08001798type testLinker struct {
1799 binaryLinker
1800 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001801}
1802
Dan Willemsend30e6102016-03-30 17:35:50 -07001803func (test *testLinker) begin(ctx BaseModuleContext) {
1804 test.binaryLinker.begin(ctx)
1805
1806 runpath := "../../lib"
1807 if ctx.toolchain().Is64Bit() {
1808 runpath += "64"
1809 }
1810 test.runPaths = append([]string{runpath}, test.runPaths...)
1811}
1812
Colin Crossca860ac2016-01-04 14:34:37 -08001813func (test *testLinker) props() []interface{} {
1814 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07001815}
1816
Colin Crossca860ac2016-01-04 14:34:37 -08001817func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
1818 flags = test.binaryLinker.flags(ctx, flags)
1819
1820 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001821 return flags
1822 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001823
Colin Cross97ba0732015-03-23 17:50:24 -07001824 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001825 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001826 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001827
1828 if ctx.HostType() == common.Windows {
1829 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1830 } else {
1831 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1832 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1833 }
1834 } else {
1835 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001836 }
1837
1838 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001839 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001840 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001841
Colin Cross21b9a242015-03-24 14:15:58 -07001842 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001843}
1844
Colin Crossca860ac2016-01-04 14:34:37 -08001845func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1846 if test.Properties.Gtest {
1847 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001848 }
Colin Crossca860ac2016-01-04 14:34:37 -08001849 deps = test.binaryLinker.deps(ctx, deps)
1850 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07001851}
1852
Colin Crossca860ac2016-01-04 14:34:37 -08001853type testInstaller struct {
1854 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08001855}
1856
Colin Crossca860ac2016-01-04 14:34:37 -08001857func (installer *testInstaller) install(ctx ModuleContext, file common.Path) {
1858 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
1859 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
1860 installer.baseInstaller.install(ctx, file)
1861}
1862
1863func NewTest(hod common.HostOrDeviceSupported) *Module {
1864 module := newModule(hod, common.MultilibBoth)
1865 module.compiler = &baseCompiler{}
1866 linker := &testLinker{}
1867 linker.Properties.Gtest = true
1868 module.linker = linker
1869 module.installer = &testInstaller{
1870 baseInstaller: baseInstaller{
1871 dir: "nativetest",
1872 dir64: "nativetest64",
1873 data: true,
1874 },
Dan Albertc403f7c2015-03-18 14:01:18 -07001875 }
Colin Crossca860ac2016-01-04 14:34:37 -08001876 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001877}
1878
Colin Crossca860ac2016-01-04 14:34:37 -08001879func testFactory() (blueprint.Module, []interface{}) {
1880 module := NewTest(common.HostAndDeviceSupported)
1881 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001882}
1883
Colin Crossca860ac2016-01-04 14:34:37 -08001884type benchmarkLinker struct {
1885 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07001886}
1887
Colin Crossca860ac2016-01-04 14:34:37 -08001888func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1889 deps = benchmark.binaryLinker.deps(ctx, deps)
1890 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
1891 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07001892}
1893
Colin Crossca860ac2016-01-04 14:34:37 -08001894func NewBenchmark(hod common.HostOrDeviceSupported) *Module {
1895 module := newModule(hod, common.MultilibFirst)
1896 module.compiler = &baseCompiler{}
1897 module.linker = &benchmarkLinker{}
1898 module.installer = &baseInstaller{
1899 dir: "nativetest",
1900 dir64: "nativetest64",
1901 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07001902 }
Colin Crossca860ac2016-01-04 14:34:37 -08001903 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07001904}
1905
Colin Crossca860ac2016-01-04 14:34:37 -08001906func benchmarkFactory() (blueprint.Module, []interface{}) {
1907 module := NewBenchmark(common.HostAndDeviceSupported)
1908 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001909}
1910
Colin Cross3f40fa42015-01-30 17:27:36 -08001911//
1912// Static library
1913//
1914
Colin Crossca860ac2016-01-04 14:34:37 -08001915func libraryStaticFactory() (blueprint.Module, []interface{}) {
1916 module := NewLibrary(common.HostAndDeviceSupported, false, true)
1917 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001918}
1919
1920//
1921// Shared libraries
1922//
1923
Colin Crossca860ac2016-01-04 14:34:37 -08001924func librarySharedFactory() (blueprint.Module, []interface{}) {
1925 module := NewLibrary(common.HostAndDeviceSupported, true, false)
1926 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001927}
1928
1929//
1930// Host static library
1931//
1932
Colin Crossca860ac2016-01-04 14:34:37 -08001933func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
1934 module := NewLibrary(common.HostSupported, false, true)
1935 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001936}
1937
1938//
1939// Host Shared libraries
1940//
1941
Colin Crossca860ac2016-01-04 14:34:37 -08001942func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
1943 module := NewLibrary(common.HostSupported, true, false)
1944 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001945}
1946
1947//
1948// Host Binaries
1949//
1950
Colin Crossca860ac2016-01-04 14:34:37 -08001951func binaryHostFactory() (blueprint.Module, []interface{}) {
1952 module := NewBinary(common.HostSupported)
1953 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001954}
1955
1956//
Colin Cross1f8f2342015-03-26 16:09:47 -07001957// Host Tests
1958//
1959
Colin Crossca860ac2016-01-04 14:34:37 -08001960func testHostFactory() (blueprint.Module, []interface{}) {
1961 module := NewTest(common.HostSupported)
1962 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07001963}
1964
1965//
Colin Cross2ba19d92015-05-07 15:44:20 -07001966// Host Benchmarks
1967//
1968
Colin Crossca860ac2016-01-04 14:34:37 -08001969func benchmarkHostFactory() (blueprint.Module, []interface{}) {
1970 module := NewBenchmark(common.HostSupported)
1971 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001972}
1973
1974//
Colin Crosscfad1192015-11-02 16:43:11 -08001975// Defaults
1976//
Colin Crossca860ac2016-01-04 14:34:37 -08001977type Defaults struct {
Colin Crosscfad1192015-11-02 16:43:11 -08001978 common.AndroidModuleBase
1979 common.DefaultsModule
1980}
1981
Colin Crossca860ac2016-01-04 14:34:37 -08001982func (*Defaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001983}
1984
Colin Crossca860ac2016-01-04 14:34:37 -08001985func defaultsFactory() (blueprint.Module, []interface{}) {
1986 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001987
1988 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08001989 &BaseProperties{},
1990 &BaseCompilerProperties{},
1991 &BaseLinkerProperties{},
1992 &LibraryCompilerProperties{},
1993 &LibraryLinkerProperties{},
1994 &BinaryLinkerProperties{},
1995 &TestLinkerProperties{},
1996 &UnusedProperties{},
1997 &StlProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001998 }
1999
Dan Willemsen218f6562015-07-08 18:13:11 -07002000 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
2001 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002002
2003 return common.InitDefaultsModule(module, module, propertyStructs...)
2004}
2005
2006//
Colin Cross3f40fa42015-01-30 17:27:36 -08002007// Device libraries shipped with gcc
2008//
2009
Colin Crossca860ac2016-01-04 14:34:37 -08002010type toolchainLibraryLinker struct {
2011 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002012}
2013
Colin Crossca860ac2016-01-04 14:34:37 -08002014var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2015
2016func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002017 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002018 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002019}
2020
Colin Crossca860ac2016-01-04 14:34:37 -08002021func (*toolchainLibraryLinker) buildStatic() bool {
2022 return true
2023}
Colin Cross3f40fa42015-01-30 17:27:36 -08002024
Colin Crossca860ac2016-01-04 14:34:37 -08002025func (*toolchainLibraryLinker) buildShared() bool {
2026 return false
2027}
2028
2029func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
2030 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2031 module.compiler = &baseCompiler{}
2032 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002033 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002034 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002035}
2036
Colin Crossca860ac2016-01-04 14:34:37 -08002037func (library *toolchainLibraryLinker) link(ctx ModuleContext,
2038 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002039
2040 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002041 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002042
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002043 if flags.Clang {
2044 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2045 }
2046
Colin Crossca860ac2016-01-04 14:34:37 -08002047 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002048
2049 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002050
Colin Crossca860ac2016-01-04 14:34:37 -08002051 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002052}
2053
Dan Albertbe961682015-03-18 23:38:50 -07002054// NDK prebuilt libraries.
2055//
2056// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2057// either (with the exception of the shared STLs, which are installed to the app's directory rather
2058// than to the system image).
2059
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002060func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
2061 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
2062 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07002063}
2064
Dan Albertc3144b12015-04-28 18:17:56 -07002065func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002066 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002067
2068 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2069 // We want to translate to just NAME.EXT
2070 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2071 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002072 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002073}
2074
Colin Crossca860ac2016-01-04 14:34:37 -08002075type ndkPrebuiltObjectLinker struct {
2076 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002077}
2078
Colin Crossca860ac2016-01-04 14:34:37 -08002079func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002080 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002081 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002082}
2083
Colin Crossca860ac2016-01-04 14:34:37 -08002084func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
2085 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2086 module.linker = &ndkPrebuiltObjectLinker{}
2087 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002088}
2089
Colin Crossca860ac2016-01-04 14:34:37 -08002090func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
2091 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002092 // A null build step, but it sets up the output path.
2093 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2094 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2095 }
2096
Colin Crossca860ac2016-01-04 14:34:37 -08002097 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002098}
2099
Colin Crossca860ac2016-01-04 14:34:37 -08002100type ndkPrebuiltLibraryLinker struct {
2101 libraryLinker
2102 Properties struct {
2103 Export_include_dirs []string `android:"arch_variant"`
2104 }
Dan Albertc3144b12015-04-28 18:17:56 -07002105}
2106
Colin Crossca860ac2016-01-04 14:34:37 -08002107var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2108var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002109
Colin Crossca860ac2016-01-04 14:34:37 -08002110func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
2111 return []interface{}{&ndk.Properties}
Dan Albertbe961682015-03-18 23:38:50 -07002112}
2113
Colin Crossca860ac2016-01-04 14:34:37 -08002114func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002115 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002116 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002117}
2118
Colin Crossca860ac2016-01-04 14:34:37 -08002119func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2120 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2121 linker := &ndkPrebuiltLibraryLinker{}
2122 linker.dynamicProperties.BuildShared = true
2123 module.linker = linker
2124 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002125}
2126
Colin Crossca860ac2016-01-04 14:34:37 -08002127func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
2128 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002129 // A null build step, but it sets up the output path.
2130 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2131 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2132 }
2133
Colin Crossca860ac2016-01-04 14:34:37 -08002134 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2135 ndk.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002136
Colin Crossca860ac2016-01-04 14:34:37 -08002137 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2138 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002139}
2140
2141// The NDK STLs are slightly different from the prebuilt system libraries:
2142// * Are not specific to each platform version.
2143// * The libraries are not in a predictable location for each STL.
2144
Colin Crossca860ac2016-01-04 14:34:37 -08002145type ndkPrebuiltStlLinker struct {
2146 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002147}
2148
Colin Crossca860ac2016-01-04 14:34:37 -08002149func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2150 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2151 linker := &ndkPrebuiltStlLinker{}
2152 linker.dynamicProperties.BuildShared = true
2153 module.linker = linker
2154 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002155}
2156
Colin Crossca860ac2016-01-04 14:34:37 -08002157func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2158 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2159 linker := &ndkPrebuiltStlLinker{}
2160 linker.dynamicProperties.BuildStatic = true
2161 module.linker = linker
2162 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002163}
2164
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002165func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002166 gccVersion := toolchain.GccVersion()
2167 var libDir string
2168 switch stl {
2169 case "libstlport":
2170 libDir = "cxx-stl/stlport/libs"
2171 case "libc++":
2172 libDir = "cxx-stl/llvm-libc++/libs"
2173 case "libgnustl":
2174 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2175 }
2176
2177 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002178 ndkSrcRoot := "prebuilts/ndk/current/sources"
2179 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002180 }
2181
2182 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002183 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002184}
2185
Colin Crossca860ac2016-01-04 14:34:37 -08002186func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
2187 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002188 // A null build step, but it sets up the output path.
2189 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2190 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2191 }
2192
Colin Crossca860ac2016-01-04 14:34:37 -08002193 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2194 ndk.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002195
2196 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002197 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002198 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002199 libExt = staticLibraryExtension
2200 }
2201
2202 stlName := strings.TrimSuffix(libName, "_shared")
2203 stlName = strings.TrimSuffix(stlName, "_static")
2204 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002205 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002206}
2207
Colin Cross6362e272015-10-29 15:25:03 -07002208func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002209 if m, ok := mctx.Module().(*Module); ok {
2210 if m.linker != nil {
2211 if linker, ok := m.linker.(baseLinkerInterface); ok {
2212 var modules []blueprint.Module
2213 if linker.buildStatic() && linker.buildShared() {
2214 modules = mctx.CreateLocalVariations("static", "shared")
2215 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
Colin Cross7b106e42016-03-25 17:31:43 -07002216 modules[0].(*Module).installer = nil
Colin Crossca860ac2016-01-04 14:34:37 -08002217 modules[1].(*Module).linker.(baseLinkerInterface).setStatic(false)
2218 } else if linker.buildStatic() {
2219 modules = mctx.CreateLocalVariations("static")
2220 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
Colin Cross7b106e42016-03-25 17:31:43 -07002221 modules[0].(*Module).installer = nil
Colin Crossca860ac2016-01-04 14:34:37 -08002222 } else if linker.buildShared() {
2223 modules = mctx.CreateLocalVariations("shared")
2224 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2225 } else {
2226 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2227 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002228
Colin Crossca860ac2016-01-04 14:34:37 -08002229 if _, ok := m.compiler.(*libraryCompiler); ok {
2230 reuseFrom := modules[0].(*Module).compiler.(*libraryCompiler)
2231 for _, m := range modules {
2232 m.(*Module).compiler.(*libraryCompiler).reuseFrom = reuseFrom
2233 }
2234 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002235 }
2236 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002237 }
2238}
Colin Cross74d1ec02015-04-28 13:30:13 -07002239
2240// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2241// modifies the slice contents in place, and returns a subslice of the original slice
2242func lastUniqueElements(list []string) []string {
2243 totalSkip := 0
2244 for i := len(list) - 1; i >= totalSkip; i-- {
2245 skip := 0
2246 for j := i - 1; j >= totalSkip; j-- {
2247 if list[i] == list[j] {
2248 skip++
2249 } else {
2250 list[j+skip] = list[j]
2251 }
2252 }
2253 totalSkip += skip
2254 }
2255 return list[totalSkip:]
2256}
Colin Cross06a931b2015-10-28 17:23:31 -07002257
2258var Bool = proptools.Bool