blob: 56655b96f712ce9c9028fbcf765d4fcb002ccdeb [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 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001089}
1090
Colin Crossca860ac2016-01-04 14:34:37 -08001091func (linker *baseLinker) begin(ctx BaseModuleContext) {}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001092
Colin Crossca860ac2016-01-04 14:34:37 -08001093func (linker *baseLinker) props() []interface{} {
1094 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001095}
1096
Colin Crossca860ac2016-01-04 14:34:37 -08001097func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1098 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1099 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1100 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001101
Colin Cross74d1ec02015-04-28 13:30:13 -07001102 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001103 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001104 }
1105
Colin Crossf6566ed2015-03-24 11:13:38 -07001106 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001107 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001108 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1109 if !Bool(linker.Properties.No_libgcc) {
1110 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001111 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001112
Colin Crossca860ac2016-01-04 14:34:37 -08001113 if !linker.static() {
1114 if linker.Properties.System_shared_libs != nil {
1115 deps.LateSharedLibs = append(deps.LateSharedLibs,
1116 linker.Properties.System_shared_libs...)
1117 } else if !ctx.sdk() {
1118 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1119 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001120 }
Colin Cross577f6e42015-03-27 18:23:34 -07001121
Colin Crossca860ac2016-01-04 14:34:37 -08001122 if ctx.sdk() {
1123 version := ctx.sdkVersion()
1124 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001125 "ndk_libc."+version,
1126 "ndk_libm."+version,
1127 )
1128 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001129 }
1130
Colin Crossca860ac2016-01-04 14:34:37 -08001131 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001132}
1133
Colin Crossca860ac2016-01-04 14:34:37 -08001134func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1135 toolchain := ctx.toolchain()
1136
1137 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1138
1139 if !ctx.noDefaultCompilerFlags() {
1140 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1141 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1142 }
1143
1144 if flags.Clang {
1145 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1146 } else {
1147 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1148 }
1149
1150 if ctx.Host() {
1151 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1152 }
1153 }
1154
Dan Willemsene7174922016-03-30 17:33:52 -07001155 if flags.Clang {
1156 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1157 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001158 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1159 }
1160
1161 return flags
1162}
1163
1164func (linker *baseLinker) static() bool {
1165 return linker.dynamicProperties.VariantIsStatic
1166}
1167
1168func (linker *baseLinker) staticBinary() bool {
1169 return linker.dynamicProperties.VariantIsStaticBinary
1170}
1171
1172func (linker *baseLinker) setStatic(static bool) {
1173 linker.dynamicProperties.VariantIsStatic = static
1174}
1175
1176type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001177 // Returns true if the build options for the module have selected a static or shared build
1178 buildStatic() bool
1179 buildShared() bool
1180
1181 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001182 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001183
Colin Cross18b6dc52015-04-28 13:20:37 -07001184 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001185 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001186
1187 // Returns whether a module is a static binary
1188 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001189}
1190
Colin Crossca860ac2016-01-04 14:34:37 -08001191type exportedFlagsProducer interface {
Colin Cross28344522015-04-22 13:07:53 -07001192 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001193}
1194
Colin Crossca860ac2016-01-04 14:34:37 -08001195type baseInstaller struct {
1196 Properties InstallerProperties
1197
1198 dir string
1199 dir64 string
1200 data bool
1201
Colin Crossa2344662016-03-24 13:14:12 -07001202 path common.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001203}
1204
1205var _ installer = (*baseInstaller)(nil)
1206
1207func (installer *baseInstaller) props() []interface{} {
1208 return []interface{}{&installer.Properties}
1209}
1210
1211func (installer *baseInstaller) install(ctx ModuleContext, file common.Path) {
1212 subDir := installer.dir
1213 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1214 subDir = installer.dir64
1215 }
1216 dir := common.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
1217 installer.path = ctx.InstallFile(dir, file)
1218}
1219
1220func (installer *baseInstaller) inData() bool {
1221 return installer.data
1222}
1223
Colin Cross3f40fa42015-01-30 17:27:36 -08001224//
1225// Combined static+shared libraries
1226//
1227
Colin Crossca860ac2016-01-04 14:34:37 -08001228type libraryCompiler struct {
1229 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001230
Colin Crossca860ac2016-01-04 14:34:37 -08001231 linker *libraryLinker
1232 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001233
Colin Crossca860ac2016-01-04 14:34:37 -08001234 // For reusing static library objects for shared library
1235 reuseFrom *libraryCompiler
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001236 reuseObjFiles common.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001237}
1238
Colin Crossca860ac2016-01-04 14:34:37 -08001239var _ compiler = (*libraryCompiler)(nil)
1240
1241func (library *libraryCompiler) props() []interface{} {
1242 props := library.baseCompiler.props()
1243 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001244}
1245
Colin Crossca860ac2016-01-04 14:34:37 -08001246func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1247 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001248
Dan Willemsen490fd492015-11-24 17:53:15 -08001249 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1250 // all code is position independent, and then those warnings get promoted to
1251 // errors.
1252 if ctx.HostType() != common.Windows {
1253 flags.CFlags = append(flags.CFlags, "-fPIC")
1254 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001255
Colin Crossca860ac2016-01-04 14:34:37 -08001256 if library.linker.static() {
1257 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001258 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001259 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001260 }
1261
Colin Crossca860ac2016-01-04 14:34:37 -08001262 return flags
1263}
1264
1265func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1266 var objFiles common.Paths
1267
1268 if library.reuseFrom != library && library.reuseFrom.Properties.Static.Cflags == nil &&
1269 library.Properties.Shared.Cflags == nil {
1270 objFiles = append(common.Paths(nil), library.reuseFrom.reuseObjFiles...)
1271 } else {
1272 objFiles = library.baseCompiler.compile(ctx, flags)
1273 library.reuseObjFiles = objFiles
1274 }
1275
1276 if library.linker.static() {
1277 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceStaticLibrary,
1278 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs)...)
1279 } else {
1280 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceSharedLibrary,
1281 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs)...)
1282 }
1283
1284 return objFiles
1285}
1286
1287type libraryLinker struct {
1288 baseLinker
1289
1290 Properties LibraryLinkerProperties
1291
1292 dynamicProperties struct {
1293 BuildStatic bool `blueprint:"mutated"`
1294 BuildShared bool `blueprint:"mutated"`
1295 }
1296
1297 exportFlags []string
1298
1299 // If we're used as a whole_static_lib, our missing dependencies need
1300 // to be given
1301 wholeStaticMissingDeps []string
1302
1303 // For whole_static_libs
1304 objFiles common.Paths
1305}
1306
1307var _ linker = (*libraryLinker)(nil)
1308var _ exportedFlagsProducer = (*libraryLinker)(nil)
1309
1310func (library *libraryLinker) props() []interface{} {
1311 props := library.baseLinker.props()
1312 return append(props, &library.Properties, &library.dynamicProperties)
1313}
1314
1315func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1316 flags = library.baseLinker.flags(ctx, flags)
1317
1318 flags.Nocrt = Bool(library.Properties.Nocrt)
1319
1320 if !library.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001321 libName := ctx.ModuleName()
1322 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1323 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001324 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001325 sharedFlag = "-shared"
1326 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001327 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001328 flags.LdFlags = append(flags.LdFlags,
1329 "-nostdlib",
1330 "-Wl,--gc-sections",
1331 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001332 }
Colin Cross97ba0732015-03-23 17:50:24 -07001333
Colin Cross0af4b842015-04-30 16:36:18 -07001334 if ctx.Darwin() {
1335 flags.LdFlags = append(flags.LdFlags,
1336 "-dynamiclib",
1337 "-single_module",
1338 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001339 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001340 )
1341 } else {
1342 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001343 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001344 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001345 )
1346 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001347 }
Colin Cross97ba0732015-03-23 17:50:24 -07001348
1349 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001350}
1351
Colin Crossca860ac2016-01-04 14:34:37 -08001352func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1353 deps = library.baseLinker.deps(ctx, deps)
1354 if library.static() {
1355 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1356 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1357 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1358 } else {
1359 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1360 if !ctx.sdk() {
1361 deps.CrtBegin = "crtbegin_so"
1362 deps.CrtEnd = "crtend_so"
1363 } else {
1364 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1365 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1366 }
1367 }
1368 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1369 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1370 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1371 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001372
Colin Crossca860ac2016-01-04 14:34:37 -08001373 return deps
1374}
Colin Cross3f40fa42015-01-30 17:27:36 -08001375
Colin Crossca860ac2016-01-04 14:34:37 -08001376func (library *libraryLinker) exportedFlags() []string {
1377 return library.exportFlags
1378}
1379
1380func (library *libraryLinker) linkStatic(ctx ModuleContext,
1381 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
1382
Colin Cross21b9a242015-03-24 14:15:58 -07001383 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Crossca860ac2016-01-04 14:34:37 -08001384 library.objFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001385
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001386 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001387
Colin Cross0af4b842015-04-30 16:36:18 -07001388 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001389 TransformDarwinObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001390 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001391 TransformObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001392 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001393
Colin Crossca860ac2016-01-04 14:34:37 -08001394 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001395
1396 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001397
1398 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001399}
1400
Colin Crossca860ac2016-01-04 14:34:37 -08001401func (library *libraryLinker) linkShared(ctx ModuleContext,
1402 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001403
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001404 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001405
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001406 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001407
Colin Crossca860ac2016-01-04 14:34:37 -08001408 versionScript := common.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1409 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1410 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1411 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001412 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001413 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001414 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001415 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001416 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001417 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001418 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1419 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001420 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001421 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1422 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001423 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001424 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1425 }
1426 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001427 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001428 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1429 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001430 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001431 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001432 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001433 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001434 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001435 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001437 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001438 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001439 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001440 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001441 }
Colin Crossaee540a2015-07-06 17:48:31 -07001442 }
1443
Colin Crossca860ac2016-01-04 14:34:37 -08001444 sharedLibs := deps.SharedLibs
1445 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001446
Colin Crossca860ac2016-01-04 14:34:37 -08001447 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1448 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
1449 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, flagsToBuilderFlags(flags), outputFile)
1450
1451 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001452}
1453
Colin Crossca860ac2016-01-04 14:34:37 -08001454func (library *libraryLinker) link(ctx ModuleContext,
1455 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001456
Colin Crossca860ac2016-01-04 14:34:37 -08001457 var out common.Path
1458 if library.static() {
1459 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001460 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001461 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001462 }
1463
Colin Crossca860ac2016-01-04 14:34:37 -08001464 includeDirs := common.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)
1465 library.exportFlags = []string{includeDirsToFlags(includeDirs)}
1466 library.exportFlags = append(library.exportFlags, deps.ReexportedCflags...)
1467
1468 return out
1469}
1470
1471func (library *libraryLinker) buildStatic() bool {
1472 return library.dynamicProperties.BuildStatic
1473}
1474
1475func (library *libraryLinker) buildShared() bool {
1476 return library.dynamicProperties.BuildShared
1477}
1478
1479func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1480 return library.wholeStaticMissingDeps
1481}
1482
1483type libraryInstaller struct {
1484 baseInstaller
1485
1486 linker *libraryLinker
1487}
1488
1489func (library *libraryInstaller) install(ctx ModuleContext, file common.Path) {
1490 if !library.linker.static() {
1491 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001492 }
1493}
1494
Colin Crossca860ac2016-01-04 14:34:37 -08001495func NewLibrary(hod common.HostOrDeviceSupported, shared, static bool) *Module {
1496 module := newModule(hod, common.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001497
Colin Crossca860ac2016-01-04 14:34:37 -08001498 linker := &libraryLinker{}
1499 linker.dynamicProperties.BuildShared = shared
1500 linker.dynamicProperties.BuildStatic = static
1501 module.linker = linker
1502
1503 module.compiler = &libraryCompiler{
1504 linker: linker,
1505 }
1506 module.installer = &libraryInstaller{
1507 baseInstaller: baseInstaller{
1508 dir: "lib",
1509 dir64: "lib64",
1510 },
1511 linker: linker,
Dan Albertc403f7c2015-03-18 14:01:18 -07001512 }
1513
Colin Crossca860ac2016-01-04 14:34:37 -08001514 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001515}
1516
Colin Crossca860ac2016-01-04 14:34:37 -08001517func libraryFactory() (blueprint.Module, []interface{}) {
1518 module := NewLibrary(common.HostAndDeviceSupported, true, true)
1519 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001520}
1521
Colin Cross3f40fa42015-01-30 17:27:36 -08001522//
1523// Objects (for crt*.o)
1524//
1525
Colin Crossca860ac2016-01-04 14:34:37 -08001526type objectLinker struct {
Dan Albertc3144b12015-04-28 18:17:56 -07001527}
1528
Colin Crossca860ac2016-01-04 14:34:37 -08001529func objectFactory() (blueprint.Module, []interface{}) {
1530 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
1531 module.compiler = &baseCompiler{}
1532 module.linker = &objectLinker{}
1533 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001534}
1535
Colin Crossca860ac2016-01-04 14:34:37 -08001536func (*objectLinker) props() []interface{} {
1537 return nil
Dan Albertc3144b12015-04-28 18:17:56 -07001538}
1539
Colin Crossca860ac2016-01-04 14:34:37 -08001540func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001541
Colin Crossca860ac2016-01-04 14:34:37 -08001542func (*objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross21b9a242015-03-24 14:15:58 -07001543 // object files can't have any dynamic dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08001544 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001545}
1546
Colin Crossca860ac2016-01-04 14:34:37 -08001547func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001548 if flags.Clang {
1549 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1550 } else {
1551 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1552 }
1553
Colin Crossca860ac2016-01-04 14:34:37 -08001554 return flags
1555}
1556
1557func (object *objectLinker) link(ctx ModuleContext,
1558 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001559
Colin Cross97ba0732015-03-23 17:50:24 -07001560 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001561
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001562 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001563 if len(objFiles) == 1 {
1564 outputFile = objFiles[0]
1565 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001566 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001567 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001568 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001569 }
1570
Colin Cross3f40fa42015-01-30 17:27:36 -08001571 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001572 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001573}
1574
Colin Cross3f40fa42015-01-30 17:27:36 -08001575//
1576// Executables
1577//
1578
Colin Crossca860ac2016-01-04 14:34:37 -08001579type binaryLinker struct {
1580 baseLinker
Colin Cross7d5136f2015-05-11 13:39:40 -07001581
Colin Crossca860ac2016-01-04 14:34:37 -08001582 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001583
Colin Crossca860ac2016-01-04 14:34:37 -08001584 hostToolPath common.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001585}
1586
Colin Crossca860ac2016-01-04 14:34:37 -08001587var _ linker = (*binaryLinker)(nil)
1588
1589func (binary *binaryLinker) props() []interface{} {
1590 return append(binary.baseLinker.props(), &binary.Properties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001591}
1592
Colin Crossca860ac2016-01-04 14:34:37 -08001593func (binary *binaryLinker) buildStatic() bool {
1594 return Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001595}
1596
Colin Crossca860ac2016-01-04 14:34:37 -08001597func (binary *binaryLinker) buildShared() bool {
1598 return !Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001599}
1600
Colin Crossca860ac2016-01-04 14:34:37 -08001601func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001602 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001603 if binary.Properties.Stem != "" {
1604 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001605 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001606
Colin Crossca860ac2016-01-04 14:34:37 -08001607 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001608}
1609
Colin Crossca860ac2016-01-04 14:34:37 -08001610func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1611 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001612 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001613 if !ctx.sdk() {
1614 if Bool(binary.Properties.Static_executable) {
1615 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001616 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001617 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001618 }
Colin Crossca860ac2016-01-04 14:34:37 -08001619 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001620 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001621 if Bool(binary.Properties.Static_executable) {
1622 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001623 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001624 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001625 }
Colin Crossca860ac2016-01-04 14:34:37 -08001626 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001627 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001628
Colin Crossca860ac2016-01-04 14:34:37 -08001629 if Bool(binary.Properties.Static_executable) {
1630 if inList("libc++_static", deps.StaticLibs) {
1631 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001632 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001633 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1634 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1635 // move them to the beginning of deps.LateStaticLibs
1636 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001637 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001638 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001639 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001640 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001641 }
Colin Crossca860ac2016-01-04 14:34:37 -08001642
1643 if !Bool(binary.Properties.Static_executable) && inList("libc", deps.StaticLibs) {
1644 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1645 "from static libs or set static_executable: true")
1646 }
1647 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001648}
1649
Colin Crossca860ac2016-01-04 14:34:37 -08001650func NewBinary(hod common.HostOrDeviceSupported) *Module {
1651 module := newModule(hod, common.MultilibFirst)
1652 module.compiler = &baseCompiler{}
1653 module.linker = &binaryLinker{}
1654 module.installer = &baseInstaller{
1655 dir: "bin",
1656 }
1657 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001658}
1659
Colin Crossca860ac2016-01-04 14:34:37 -08001660func binaryFactory() (blueprint.Module, []interface{}) {
1661 module := NewBinary(common.HostAndDeviceSupported)
1662 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001663}
1664
Colin Crossca860ac2016-01-04 14:34:37 -08001665func (binary *binaryLinker) ModifyProperties(ctx ModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001666 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001667 binary.Properties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001668 }
Colin Crossca860ac2016-01-04 14:34:37 -08001669 if Bool(binary.Properties.Static_executable) {
1670 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001671 }
1672}
1673
Colin Crossca860ac2016-01-04 14:34:37 -08001674func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1675 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001676
Dan Willemsen490fd492015-11-24 17:53:15 -08001677 if ctx.Host() {
1678 flags.LdFlags = append(flags.LdFlags, "-pie")
1679 if ctx.HostType() == common.Windows {
1680 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1681 }
1682 }
1683
1684 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1685 // all code is position independent, and then those warnings get promoted to
1686 // errors.
1687 if ctx.HostType() != common.Windows {
1688 flags.CFlags = append(flags.CFlags, "-fpie")
1689 }
Colin Cross97ba0732015-03-23 17:50:24 -07001690
Colin Crossf6566ed2015-03-24 11:13:38 -07001691 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001692 if Bool(binary.Properties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001693 // Clang driver needs -static to create static executable.
1694 // However, bionic/linker uses -shared to overwrite.
1695 // Linker for x86 targets does not allow coexistance of -static and -shared,
1696 // so we add -static only if -shared is not used.
1697 if !inList("-shared", flags.LdFlags) {
1698 flags.LdFlags = append(flags.LdFlags, "-static")
1699 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001700
Colin Crossed4cf0b2015-03-26 14:43:45 -07001701 flags.LdFlags = append(flags.LdFlags,
1702 "-nostdlib",
1703 "-Bstatic",
1704 "-Wl,--gc-sections",
1705 )
1706
1707 } else {
1708 linker := "/system/bin/linker"
1709 if flags.Toolchain.Is64Bit() {
Colin Crossca860ac2016-01-04 14:34:37 -08001710 linker += "64"
Colin Crossed4cf0b2015-03-26 14:43:45 -07001711 }
1712
1713 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001714 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001715 "-nostdlib",
1716 "-Bdynamic",
1717 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1718 "-Wl,--gc-sections",
1719 "-Wl,-z,nocopyreloc",
1720 )
1721 }
Colin Cross0af4b842015-04-30 16:36:18 -07001722 } else if ctx.Darwin() {
1723 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001724 }
1725
Colin Cross97ba0732015-03-23 17:50:24 -07001726 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001727}
1728
Colin Crossca860ac2016-01-04 14:34:37 -08001729func (binary *binaryLinker) link(ctx ModuleContext,
1730 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001731
Colin Crossca860ac2016-01-04 14:34:37 -08001732 outputFile := common.PathForModuleOut(ctx, binary.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
1733 if ctx.HostOrDevice().Host() {
1734 binary.hostToolPath = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001735 }
Colin Crossca860ac2016-01-04 14:34:37 -08001736 ret := outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001737
Colin Crossca860ac2016-01-04 14:34:37 -08001738 if binary.Properties.Prefix_symbols != "" {
Colin Crossbfae8852015-03-26 14:44:11 -07001739 afterPrefixSymbols := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001740 outputFile = common.PathForModuleOut(ctx, binary.getStem(ctx)+".intermediate")
1741 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1742 flagsToBuilderFlags(flags), afterPrefixSymbols)
Colin Crossbfae8852015-03-26 14:44:11 -07001743 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001744
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001745 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001746
Colin Crossca860ac2016-01-04 14:34:37 -08001747 sharedLibs := deps.SharedLibs
1748 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1749
1750 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001751 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossca860ac2016-01-04 14:34:37 -08001752 flagsToBuilderFlags(flags), outputFile)
1753
1754 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001755}
Colin Cross3f40fa42015-01-30 17:27:36 -08001756
Colin Crossca860ac2016-01-04 14:34:37 -08001757func (binary *binaryLinker) HostToolPath() common.OptionalPath {
1758 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001759}
1760
Colin Cross6362e272015-10-29 15:25:03 -07001761func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08001762 if m, ok := mctx.Module().(*Module); ok {
1763 if test, ok := m.linker.(*testLinker); ok {
1764 if Bool(test.Properties.Test_per_src) {
1765 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
1766 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1767 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1768 }
1769 tests := mctx.CreateLocalVariations(testNames...)
1770 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1771 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
1772 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
1773 }
Colin Cross6002e052015-09-16 16:00:08 -07001774 }
1775 }
1776 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001777}
1778
Colin Crossca860ac2016-01-04 14:34:37 -08001779type testLinker struct {
1780 binaryLinker
1781 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001782}
1783
Colin Crossca860ac2016-01-04 14:34:37 -08001784func (test *testLinker) props() []interface{} {
1785 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07001786}
1787
Colin Crossca860ac2016-01-04 14:34:37 -08001788func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
1789 flags = test.binaryLinker.flags(ctx, flags)
1790
1791 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001792 return flags
1793 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001794
Colin Cross97ba0732015-03-23 17:50:24 -07001795 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001796 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001797 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001798
1799 if ctx.HostType() == common.Windows {
1800 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1801 } else {
1802 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1803 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1804 }
1805 } else {
1806 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001807 }
1808
1809 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001810 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001811 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001812
Colin Cross21b9a242015-03-24 14:15:58 -07001813 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001814}
1815
Colin Crossca860ac2016-01-04 14:34:37 -08001816func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1817 if test.Properties.Gtest {
1818 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001819 }
Colin Crossca860ac2016-01-04 14:34:37 -08001820 deps = test.binaryLinker.deps(ctx, deps)
1821 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07001822}
1823
Colin Crossca860ac2016-01-04 14:34:37 -08001824type testInstaller struct {
1825 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08001826}
1827
Colin Crossca860ac2016-01-04 14:34:37 -08001828func (installer *testInstaller) install(ctx ModuleContext, file common.Path) {
1829 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
1830 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
1831 installer.baseInstaller.install(ctx, file)
1832}
1833
1834func NewTest(hod common.HostOrDeviceSupported) *Module {
1835 module := newModule(hod, common.MultilibBoth)
1836 module.compiler = &baseCompiler{}
1837 linker := &testLinker{}
1838 linker.Properties.Gtest = true
1839 module.linker = linker
1840 module.installer = &testInstaller{
1841 baseInstaller: baseInstaller{
1842 dir: "nativetest",
1843 dir64: "nativetest64",
1844 data: true,
1845 },
Dan Albertc403f7c2015-03-18 14:01:18 -07001846 }
Colin Crossca860ac2016-01-04 14:34:37 -08001847 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001848}
1849
Colin Crossca860ac2016-01-04 14:34:37 -08001850func testFactory() (blueprint.Module, []interface{}) {
1851 module := NewTest(common.HostAndDeviceSupported)
1852 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001853}
1854
Colin Crossca860ac2016-01-04 14:34:37 -08001855type benchmarkLinker struct {
1856 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07001857}
1858
Colin Crossca860ac2016-01-04 14:34:37 -08001859func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1860 deps = benchmark.binaryLinker.deps(ctx, deps)
1861 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
1862 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07001863}
1864
Colin Crossca860ac2016-01-04 14:34:37 -08001865func NewBenchmark(hod common.HostOrDeviceSupported) *Module {
1866 module := newModule(hod, common.MultilibFirst)
1867 module.compiler = &baseCompiler{}
1868 module.linker = &benchmarkLinker{}
1869 module.installer = &baseInstaller{
1870 dir: "nativetest",
1871 dir64: "nativetest64",
1872 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07001873 }
Colin Crossca860ac2016-01-04 14:34:37 -08001874 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07001875}
1876
Colin Crossca860ac2016-01-04 14:34:37 -08001877func benchmarkFactory() (blueprint.Module, []interface{}) {
1878 module := NewBenchmark(common.HostAndDeviceSupported)
1879 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001880}
1881
Colin Cross3f40fa42015-01-30 17:27:36 -08001882//
1883// Static library
1884//
1885
Colin Crossca860ac2016-01-04 14:34:37 -08001886func libraryStaticFactory() (blueprint.Module, []interface{}) {
1887 module := NewLibrary(common.HostAndDeviceSupported, false, true)
1888 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001889}
1890
1891//
1892// Shared libraries
1893//
1894
Colin Crossca860ac2016-01-04 14:34:37 -08001895func librarySharedFactory() (blueprint.Module, []interface{}) {
1896 module := NewLibrary(common.HostAndDeviceSupported, true, false)
1897 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001898}
1899
1900//
1901// Host static library
1902//
1903
Colin Crossca860ac2016-01-04 14:34:37 -08001904func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
1905 module := NewLibrary(common.HostSupported, false, true)
1906 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001907}
1908
1909//
1910// Host Shared libraries
1911//
1912
Colin Crossca860ac2016-01-04 14:34:37 -08001913func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
1914 module := NewLibrary(common.HostSupported, true, false)
1915 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001916}
1917
1918//
1919// Host Binaries
1920//
1921
Colin Crossca860ac2016-01-04 14:34:37 -08001922func binaryHostFactory() (blueprint.Module, []interface{}) {
1923 module := NewBinary(common.HostSupported)
1924 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001925}
1926
1927//
Colin Cross1f8f2342015-03-26 16:09:47 -07001928// Host Tests
1929//
1930
Colin Crossca860ac2016-01-04 14:34:37 -08001931func testHostFactory() (blueprint.Module, []interface{}) {
1932 module := NewTest(common.HostSupported)
1933 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07001934}
1935
1936//
Colin Cross2ba19d92015-05-07 15:44:20 -07001937// Host Benchmarks
1938//
1939
Colin Crossca860ac2016-01-04 14:34:37 -08001940func benchmarkHostFactory() (blueprint.Module, []interface{}) {
1941 module := NewBenchmark(common.HostSupported)
1942 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001943}
1944
1945//
Colin Crosscfad1192015-11-02 16:43:11 -08001946// Defaults
1947//
Colin Crossca860ac2016-01-04 14:34:37 -08001948type Defaults struct {
Colin Crosscfad1192015-11-02 16:43:11 -08001949 common.AndroidModuleBase
1950 common.DefaultsModule
1951}
1952
Colin Crossca860ac2016-01-04 14:34:37 -08001953func (*Defaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08001954}
1955
Colin Crossca860ac2016-01-04 14:34:37 -08001956func defaultsFactory() (blueprint.Module, []interface{}) {
1957 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08001958
1959 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08001960 &BaseProperties{},
1961 &BaseCompilerProperties{},
1962 &BaseLinkerProperties{},
1963 &LibraryCompilerProperties{},
1964 &LibraryLinkerProperties{},
1965 &BinaryLinkerProperties{},
1966 &TestLinkerProperties{},
1967 &UnusedProperties{},
1968 &StlProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001969 }
1970
Dan Willemsen218f6562015-07-08 18:13:11 -07001971 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1972 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001973
1974 return common.InitDefaultsModule(module, module, propertyStructs...)
1975}
1976
1977//
Colin Cross3f40fa42015-01-30 17:27:36 -08001978// Device libraries shipped with gcc
1979//
1980
Colin Crossca860ac2016-01-04 14:34:37 -08001981type toolchainLibraryLinker struct {
1982 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08001983}
1984
Colin Crossca860ac2016-01-04 14:34:37 -08001985var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
1986
1987func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001988 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08001989 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001990}
1991
Colin Crossca860ac2016-01-04 14:34:37 -08001992func (*toolchainLibraryLinker) buildStatic() bool {
1993 return true
1994}
Colin Cross3f40fa42015-01-30 17:27:36 -08001995
Colin Crossca860ac2016-01-04 14:34:37 -08001996func (*toolchainLibraryLinker) buildShared() bool {
1997 return false
1998}
1999
2000func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
2001 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2002 module.compiler = &baseCompiler{}
2003 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002004 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002005 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002006}
2007
Colin Crossca860ac2016-01-04 14:34:37 -08002008func (library *toolchainLibraryLinker) link(ctx ModuleContext,
2009 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002010
2011 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002012 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002013
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002014 if flags.Clang {
2015 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2016 }
2017
Colin Crossca860ac2016-01-04 14:34:37 -08002018 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002019
2020 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002021
Colin Crossca860ac2016-01-04 14:34:37 -08002022 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002023}
2024
Dan Albertbe961682015-03-18 23:38:50 -07002025// NDK prebuilt libraries.
2026//
2027// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2028// either (with the exception of the shared STLs, which are installed to the app's directory rather
2029// than to the system image).
2030
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002031func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
2032 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
2033 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07002034}
2035
Dan Albertc3144b12015-04-28 18:17:56 -07002036func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002037 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002038
2039 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2040 // We want to translate to just NAME.EXT
2041 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2042 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002043 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002044}
2045
Colin Crossca860ac2016-01-04 14:34:37 -08002046type ndkPrebuiltObjectLinker struct {
2047 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002048}
2049
Colin Crossca860ac2016-01-04 14:34:37 -08002050func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002051 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002052 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002053}
2054
Colin Crossca860ac2016-01-04 14:34:37 -08002055func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
2056 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2057 module.linker = &ndkPrebuiltObjectLinker{}
2058 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002059}
2060
Colin Crossca860ac2016-01-04 14:34:37 -08002061func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
2062 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002063 // A null build step, but it sets up the output path.
2064 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2065 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2066 }
2067
Colin Crossca860ac2016-01-04 14:34:37 -08002068 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002069}
2070
Colin Crossca860ac2016-01-04 14:34:37 -08002071type ndkPrebuiltLibraryLinker struct {
2072 libraryLinker
2073 Properties struct {
2074 Export_include_dirs []string `android:"arch_variant"`
2075 }
Dan Albertc3144b12015-04-28 18:17:56 -07002076}
2077
Colin Crossca860ac2016-01-04 14:34:37 -08002078var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2079var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002080
Colin Crossca860ac2016-01-04 14:34:37 -08002081func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
2082 return []interface{}{&ndk.Properties}
Dan Albertbe961682015-03-18 23:38:50 -07002083}
2084
Colin Crossca860ac2016-01-04 14:34:37 -08002085func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002086 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002087 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002088}
2089
Colin Crossca860ac2016-01-04 14:34:37 -08002090func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2091 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2092 linker := &ndkPrebuiltLibraryLinker{}
2093 linker.dynamicProperties.BuildShared = true
2094 module.linker = linker
2095 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002096}
2097
Colin Crossca860ac2016-01-04 14:34:37 -08002098func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
2099 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002100 // A null build step, but it sets up the output path.
2101 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2102 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2103 }
2104
Colin Crossca860ac2016-01-04 14:34:37 -08002105 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2106 ndk.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002107
Colin Crossca860ac2016-01-04 14:34:37 -08002108 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2109 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002110}
2111
2112// The NDK STLs are slightly different from the prebuilt system libraries:
2113// * Are not specific to each platform version.
2114// * The libraries are not in a predictable location for each STL.
2115
Colin Crossca860ac2016-01-04 14:34:37 -08002116type ndkPrebuiltStlLinker struct {
2117 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002118}
2119
Colin Crossca860ac2016-01-04 14:34:37 -08002120func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2121 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2122 linker := &ndkPrebuiltStlLinker{}
2123 linker.dynamicProperties.BuildShared = true
2124 module.linker = linker
2125 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002126}
2127
Colin Crossca860ac2016-01-04 14:34:37 -08002128func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2129 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2130 linker := &ndkPrebuiltStlLinker{}
2131 linker.dynamicProperties.BuildStatic = true
2132 module.linker = linker
2133 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002134}
2135
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002136func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002137 gccVersion := toolchain.GccVersion()
2138 var libDir string
2139 switch stl {
2140 case "libstlport":
2141 libDir = "cxx-stl/stlport/libs"
2142 case "libc++":
2143 libDir = "cxx-stl/llvm-libc++/libs"
2144 case "libgnustl":
2145 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2146 }
2147
2148 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002149 ndkSrcRoot := "prebuilts/ndk/current/sources"
2150 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002151 }
2152
2153 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002154 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002155}
2156
Colin Crossca860ac2016-01-04 14:34:37 -08002157func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
2158 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002159 // A null build step, but it sets up the output path.
2160 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2161 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2162 }
2163
Colin Crossca860ac2016-01-04 14:34:37 -08002164 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2165 ndk.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002166
2167 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002168 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002169 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002170 libExt = staticLibraryExtension
2171 }
2172
2173 stlName := strings.TrimSuffix(libName, "_shared")
2174 stlName = strings.TrimSuffix(stlName, "_static")
2175 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002176 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002177}
2178
Colin Cross6362e272015-10-29 15:25:03 -07002179func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002180 if m, ok := mctx.Module().(*Module); ok {
2181 if m.linker != nil {
2182 if linker, ok := m.linker.(baseLinkerInterface); ok {
2183 var modules []blueprint.Module
2184 if linker.buildStatic() && linker.buildShared() {
2185 modules = mctx.CreateLocalVariations("static", "shared")
2186 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
Colin Cross7b106e42016-03-25 17:31:43 -07002187 modules[0].(*Module).installer = nil
Colin Crossca860ac2016-01-04 14:34:37 -08002188 modules[1].(*Module).linker.(baseLinkerInterface).setStatic(false)
2189 } else if linker.buildStatic() {
2190 modules = mctx.CreateLocalVariations("static")
2191 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
Colin Cross7b106e42016-03-25 17:31:43 -07002192 modules[0].(*Module).installer = nil
Colin Crossca860ac2016-01-04 14:34:37 -08002193 } else if linker.buildShared() {
2194 modules = mctx.CreateLocalVariations("shared")
2195 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2196 } else {
2197 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2198 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002199
Colin Crossca860ac2016-01-04 14:34:37 -08002200 if _, ok := m.compiler.(*libraryCompiler); ok {
2201 reuseFrom := modules[0].(*Module).compiler.(*libraryCompiler)
2202 for _, m := range modules {
2203 m.(*Module).compiler.(*libraryCompiler).reuseFrom = reuseFrom
2204 }
2205 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002206 }
2207 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002208 }
2209}
Colin Cross74d1ec02015-04-28 13:30:13 -07002210
2211// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2212// modifies the slice contents in place, and returns a subslice of the original slice
2213func lastUniqueElements(list []string) []string {
2214 totalSkip := 0
2215 for i := len(list) - 1; i >= totalSkip; i-- {
2216 skip := 0
2217 for j := i - 1; j >= totalSkip; j-- {
2218 if list[i] == list[j] {
2219 skip++
2220 } else {
2221 list[j+skip] = list[j]
2222 }
2223 }
2224 totalSkip += skip
2225 }
2226 return list[totalSkip:]
2227}
Colin Cross06a931b2015-10-28 17:23:31 -07002228
2229var Bool = proptools.Bool