blob: 5adaf49f0edc3ed7956524d66fb1a3132abd5908 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080035 soong.RegisterModuleType("cc_library_static", libraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", librarySharedFactory)
37 soong.RegisterModuleType("cc_library", libraryFactory)
38 soong.RegisterModuleType("cc_object", objectFactory)
39 soong.RegisterModuleType("cc_binary", binaryFactory)
40 soong.RegisterModuleType("cc_test", testFactory)
41 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
42 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Crossca860ac2016-01-04 14:34:37 -080044 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070049
Colin Crossca860ac2016-01-04 14:34:37 -080050 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", testHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070055
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
Colin Cross3f40fa42015-01-30 17:27:36 -080068)
69
70// Flags used by lots of devices. Putting them in package static variables will save bytes in
71// build.ninja so they aren't repeated for every file
72var (
73 commonGlobalCflags = []string{
74 "-DANDROID",
75 "-fmessage-length=0",
76 "-W",
77 "-Wall",
78 "-Wno-unused",
79 "-Winit-self",
80 "-Wpointer-arith",
81
82 // COMMON_RELEASE_CFLAGS
83 "-DNDEBUG",
84 "-UDEBUG",
85 }
86
87 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080088 "-fdiagnostics-color",
89
Colin Cross3f40fa42015-01-30 17:27:36 -080090 // TARGET_ERROR_FLAGS
91 "-Werror=return-type",
92 "-Werror=non-virtual-dtor",
93 "-Werror=address",
94 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080095 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080096 }
97
98 hostGlobalCflags = []string{}
99
100 commonGlobalCppflags = []string{
101 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700102 }
103
Dan Willemsenbe03f342016-03-03 17:21:04 -0800104 noOverrideGlobalCflags = []string{
105 "-Werror=int-to-pointer-cast",
106 "-Werror=pointer-to-int-cast",
107 }
108
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700109 illegalFlags = []string{
110 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800111 }
112)
113
114func init() {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700115 if common.CurrentHostType() == common.Linux {
116 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
117 }
118
Colin Cross3f40fa42015-01-30 17:27:36 -0800119 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
120 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
121 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800122 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800123
124 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
125
126 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800127 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800129 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800130 pctx.StaticVariable("hostClangGlobalCflags",
131 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800132 pctx.StaticVariable("noOverrideClangGlobalCflags",
133 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
134
Tim Kilbournf2948142015-03-11 12:03:03 -0700135 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800136 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800137
138 // Everything in this list is a crime against abstraction and dependency tracking.
139 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800140 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700141 []string{
142 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800143 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700144 "hardware/libhardware/include",
145 "hardware/libhardware_legacy/include",
146 "hardware/ril/include",
147 "libnativehelper/include",
148 "frameworks/native/include",
149 "frameworks/native/opengl/include",
150 "frameworks/av/include",
151 "frameworks/base/include",
152 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800153 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
154 // with this, since there is no associated library.
155 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
156 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800157
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700158 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
159 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
160 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
161 return override, nil
162 }
163 return "${clangDefaultBase}", nil
164 })
165 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
166 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
167 return override, nil
168 }
Colin Cross7253e0b2016-03-21 15:12:34 -0700169 return "clang-2690385", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700170 })
171 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800172}
173
Colin Crossca860ac2016-01-04 14:34:37 -0800174type Deps struct {
175 SharedLibs, LateSharedLibs []string
176 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700177
Colin Cross81413472016-04-11 14:37:39 -0700178 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179
180 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700181
Colin Cross97ba0732015-03-23 17:50:24 -0700182 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700183}
184
Colin Crossca860ac2016-01-04 14:34:37 -0800185type PathDeps struct {
186 SharedLibs, LateSharedLibs common.Paths
187 StaticLibs, LateStaticLibs, WholeStaticLibs common.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700188
189 ObjFiles common.Paths
190 WholeStaticLibObjFiles common.Paths
191
192 Cflags, ReexportedCflags []string
193
194 CrtBegin, CrtEnd common.OptionalPath
195}
196
Colin Crossca860ac2016-01-04 14:34:37 -0800197type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700198 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
199 AsFlags []string // Flags that apply to assembly source files
200 CFlags []string // Flags that apply to C and C++ source files
201 ConlyFlags []string // Flags that apply to C source files
202 CppFlags []string // Flags that apply to C++ source files
203 YaccFlags []string // Flags that apply to Yacc source files
204 LdFlags []string // Flags that apply to linker command lines
205
206 Nocrt bool
207 Toolchain Toolchain
208 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800209
210 RequiredInstructionSet string
Colin Crossc472d572015-03-17 15:06:21 -0700211}
212
Colin Crossca860ac2016-01-04 14:34:37 -0800213type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700214 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700215 Srcs []string `android:"arch_variant"`
216
217 // list of source files that should not be used to build the C/C++ module.
218 // This is most useful in the arch/multilib variants to remove non-common files
219 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700220
221 // list of module-specific flags that will be used for C and C++ compiles.
222 Cflags []string `android:"arch_variant"`
223
224 // list of module-specific flags that will be used for C++ compiles
225 Cppflags []string `android:"arch_variant"`
226
227 // list of module-specific flags that will be used for C compiles
228 Conlyflags []string `android:"arch_variant"`
229
230 // list of module-specific flags that will be used for .S compiles
231 Asflags []string `android:"arch_variant"`
232
Colin Crossca860ac2016-01-04 14:34:37 -0800233 // list of module-specific flags that will be used for C and C++ compiles when
234 // compiling with clang
235 Clang_cflags []string `android:"arch_variant"`
236
237 // list of module-specific flags that will be used for .S compiles when
238 // compiling with clang
239 Clang_asflags []string `android:"arch_variant"`
240
Colin Cross7d5136f2015-05-11 13:39:40 -0700241 // list of module-specific flags that will be used for .y and .yy compiles
242 Yaccflags []string
243
Colin Cross7d5136f2015-05-11 13:39:40 -0700244 // the instruction set architecture to use to compile the C/C++
245 // module.
246 Instruction_set string `android:"arch_variant"`
247
248 // list of directories relative to the root of the source tree that will
249 // be added to the include path using -I.
250 // If possible, don't use this. If adding paths from the current directory use
251 // local_include_dirs, if adding paths from other modules use export_include_dirs in
252 // that module.
253 Include_dirs []string `android:"arch_variant"`
254
Colin Cross39d97f22015-09-14 12:30:50 -0700255 // list of files relative to the root of the source tree that will be included
256 // using -include.
257 // If possible, don't use this.
258 Include_files []string `android:"arch_variant"`
259
Colin Cross7d5136f2015-05-11 13:39:40 -0700260 // list of directories relative to the Blueprints file that will
261 // be added to the include path using -I
262 Local_include_dirs []string `android:"arch_variant"`
263
Colin Cross39d97f22015-09-14 12:30:50 -0700264 // list of files relative to the Blueprints file that will be included
265 // using -include.
266 // If possible, don't use this.
267 Local_include_files []string `android:"arch_variant"`
268
Colin Crossca860ac2016-01-04 14:34:37 -0800269 // pass -frtti instead of -fno-rtti
270 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700271
Colin Crossca860ac2016-01-04 14:34:37 -0800272 Debug, Release struct {
273 // list of module-specific flags that will be used for C and C++ compiles in debug or
274 // release builds
275 Cflags []string `android:"arch_variant"`
276 } `android:"arch_variant"`
277}
Colin Cross7d5136f2015-05-11 13:39:40 -0700278
Colin Crossca860ac2016-01-04 14:34:37 -0800279type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700280 // list of modules whose object files should be linked into this module
281 // in their entirety. For static library modules, all of the .o files from the intermediate
282 // directory of the dependency will be linked into this modules .a file. For a shared library,
283 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
284 Whole_static_libs []string `android:"arch_variant"`
285
286 // list of modules that should be statically linked into this module.
287 Static_libs []string `android:"arch_variant"`
288
289 // list of modules that should be dynamically linked into this module.
290 Shared_libs []string `android:"arch_variant"`
291
Colin Crossca860ac2016-01-04 14:34:37 -0800292 // list of module-specific flags that will be used for all link steps
293 Ldflags []string `android:"arch_variant"`
294
295 // don't insert default compiler flags into asflags, cflags,
296 // cppflags, conlyflags, ldflags, or include_dirs
297 No_default_compiler_flags *bool
298
299 // list of system libraries that will be dynamically linked to
300 // shared library and executable modules. If unset, generally defaults to libc
301 // and libm. Set to [] to prevent linking against libc and libm.
302 System_shared_libs []string
303
Colin Cross7d5136f2015-05-11 13:39:40 -0700304 // allow the module to contain undefined symbols. By default,
305 // modules cannot contain undefined symbols that are not satisified by their immediate
306 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
307 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700308 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700309
Dan Willemsend67be222015-09-16 15:19:33 -0700310 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700311 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700312
Colin Cross7d5136f2015-05-11 13:39:40 -0700313 // -l arguments to pass to linker for host-provided shared libraries
314 Host_ldlibs []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800315}
Colin Cross7d5136f2015-05-11 13:39:40 -0700316
Colin Crossca860ac2016-01-04 14:34:37 -0800317type LibraryCompilerProperties struct {
318 Static struct {
319 Srcs []string `android:"arch_variant"`
320 Exclude_srcs []string `android:"arch_variant"`
321 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700322 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800323 Shared struct {
324 Srcs []string `android:"arch_variant"`
325 Exclude_srcs []string `android:"arch_variant"`
326 Cflags []string `android:"arch_variant"`
327 } `android:"arch_variant"`
328}
329
330type LibraryLinkerProperties struct {
331 Static struct {
332 Whole_static_libs []string `android:"arch_variant"`
333 Static_libs []string `android:"arch_variant"`
334 Shared_libs []string `android:"arch_variant"`
335 } `android:"arch_variant"`
336 Shared struct {
337 Whole_static_libs []string `android:"arch_variant"`
338 Static_libs []string `android:"arch_variant"`
339 Shared_libs []string `android:"arch_variant"`
340 } `android:"arch_variant"`
341
342 // local file name to pass to the linker as --version_script
343 Version_script *string `android:"arch_variant"`
344 // local file name to pass to the linker as -unexported_symbols_list
345 Unexported_symbols_list *string `android:"arch_variant"`
346 // local file name to pass to the linker as -force_symbols_not_weak_list
347 Force_symbols_not_weak_list *string `android:"arch_variant"`
348 // local file name to pass to the linker as -force_symbols_weak_list
349 Force_symbols_weak_list *string `android:"arch_variant"`
350
351 // list of directories relative to the Blueprints file that will
352 // be added to the include path using -I for any module that links against this module
353 Export_include_dirs []string `android:"arch_variant"`
354
355 // don't link in crt_begin and crt_end. This flag should only be necessary for
356 // compiling crt or libc.
357 Nocrt *bool `android:"arch_variant"`
358}
359
360type BinaryLinkerProperties struct {
361 // compile executable with -static
362 Static_executable *bool
363
364 // set the name of the output
365 Stem string `android:"arch_variant"`
366
367 // append to the name of the output
368 Suffix string `android:"arch_variant"`
369
370 // if set, add an extra objcopy --prefix-symbols= step
371 Prefix_symbols string
372}
373
374type TestLinkerProperties struct {
375 // if set, build against the gtest library. Defaults to true.
376 Gtest bool
377
378 // Create a separate binary for each source file. Useful when there is
379 // global state that can not be torn down and reset between each test suite.
380 Test_per_src *bool
381}
382
Colin Cross81413472016-04-11 14:37:39 -0700383type ObjectLinkerProperties struct {
384 // names of other cc_object modules to link into this module using partial linking
385 Objs []string `android:"arch_variant"`
386}
387
Colin Crossca860ac2016-01-04 14:34:37 -0800388// Properties used to compile all C or C++ modules
389type BaseProperties struct {
390 // compile module with clang instead of gcc
391 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700392
393 // Minimum sdk version supported when compiling against the ndk
394 Sdk_version string
395
Colin Crossca860ac2016-01-04 14:34:37 -0800396 // don't insert default compiler flags into asflags, cflags,
397 // cppflags, conlyflags, ldflags, or include_dirs
398 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700399
400 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800401}
402
403type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700404 // install to a subdirectory of the default install path for the module
405 Relative_install_path string
406}
407
Colin Crossca860ac2016-01-04 14:34:37 -0800408type UnusedProperties struct {
Colin Crosscfad1192015-11-02 16:43:11 -0800409 Native_coverage *bool
410 Required []string
411 Sanitize []string `android:"arch_variant"`
412 Sanitize_recover []string
413 Strip string
414 Tags []string
415}
416
Colin Crossca860ac2016-01-04 14:34:37 -0800417type ModuleContextIntf interface {
418 module() *Module
419 static() bool
420 staticBinary() bool
421 clang() bool
422 toolchain() Toolchain
423 noDefaultCompilerFlags() bool
424 sdk() bool
425 sdkVersion() string
426}
427
428type ModuleContext interface {
429 common.AndroidModuleContext
430 ModuleContextIntf
431}
432
433type BaseModuleContext interface {
434 common.AndroidBaseContext
435 ModuleContextIntf
436}
437
438type Customizer interface {
439 CustomizeProperties(BaseModuleContext)
440 Properties() []interface{}
441}
442
443type feature interface {
444 begin(ctx BaseModuleContext)
445 deps(ctx BaseModuleContext, deps Deps) Deps
446 flags(ctx ModuleContext, flags Flags) Flags
447 props() []interface{}
448}
449
450type compiler interface {
451 feature
452 compile(ctx ModuleContext, flags Flags) common.Paths
453}
454
455type linker interface {
456 feature
457 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles common.Paths) common.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700458 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800459}
460
461type installer interface {
462 props() []interface{}
463 install(ctx ModuleContext, path common.Path)
464 inData() bool
465}
466
Colin Crossc99deeb2016-04-11 15:06:20 -0700467type dependencyTag struct {
468 blueprint.BaseDependencyTag
469 name string
470 library bool
471}
472
473var (
474 sharedDepTag = dependencyTag{name: "shared", library: true}
475 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
476 staticDepTag = dependencyTag{name: "static", library: true}
477 lateStaticDepTag = dependencyTag{name: "late static", library: true}
478 wholeStaticDepTag = dependencyTag{name: "whole static", library: true}
479 objDepTag = dependencyTag{name: "obj"}
480 crtBeginDepTag = dependencyTag{name: "crtbegin"}
481 crtEndDepTag = dependencyTag{name: "crtend"}
482 reuseObjTag = dependencyTag{name: "reuse objects"}
483)
484
Colin Crossca860ac2016-01-04 14:34:37 -0800485// Module contains the properties and members used by all C/C++ module types, and implements
486// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
487// to construct the output file. Behavior can be customized with a Customizer interface
488type Module struct {
Colin Crossc472d572015-03-17 15:06:21 -0700489 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800490 common.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700491
Colin Crossca860ac2016-01-04 14:34:37 -0800492 Properties BaseProperties
493 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700494
Colin Crossca860ac2016-01-04 14:34:37 -0800495 // initialize before calling Init
496 hod common.HostOrDeviceSupported
497 multilib common.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700498
Colin Crossca860ac2016-01-04 14:34:37 -0800499 // delegates, initialize before calling Init
500 customizer Customizer
501 features []feature
502 compiler compiler
503 linker linker
504 installer installer
Colin Cross74d1ec02015-04-28 13:30:13 -0700505
Colin Crossca860ac2016-01-04 14:34:37 -0800506 outputFile common.OptionalPath
507
508 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700509}
510
Colin Crossca860ac2016-01-04 14:34:37 -0800511func (c *Module) Init() (blueprint.Module, []interface{}) {
512 props := []interface{}{&c.Properties, &c.unused}
513 if c.customizer != nil {
514 props = append(props, c.customizer.Properties()...)
515 }
516 if c.compiler != nil {
517 props = append(props, c.compiler.props()...)
518 }
519 if c.linker != nil {
520 props = append(props, c.linker.props()...)
521 }
522 if c.installer != nil {
523 props = append(props, c.installer.props()...)
524 }
525 for _, feature := range c.features {
526 props = append(props, feature.props()...)
527 }
Colin Crossc472d572015-03-17 15:06:21 -0700528
Colin Crossca860ac2016-01-04 14:34:37 -0800529 _, props = common.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700530
Colin Crossca860ac2016-01-04 14:34:37 -0800531 return common.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700532}
533
Colin Crossca860ac2016-01-04 14:34:37 -0800534type baseModuleContext struct {
535 common.AndroidBaseContext
536 moduleContextImpl
537}
538
539type moduleContext struct {
540 common.AndroidModuleContext
541 moduleContextImpl
542}
543
544type moduleContextImpl struct {
545 mod *Module
546 ctx BaseModuleContext
547}
548
549func (ctx *moduleContextImpl) module() *Module {
550 return ctx.mod
551}
552
553func (ctx *moduleContextImpl) clang() bool {
554 return ctx.mod.clang(ctx.ctx)
555}
556
557func (ctx *moduleContextImpl) toolchain() Toolchain {
558 return ctx.mod.toolchain(ctx.ctx)
559}
560
561func (ctx *moduleContextImpl) static() bool {
562 if ctx.mod.linker == nil {
563 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
564 }
565 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
566 return linker.static()
567 } else {
568 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
569 }
570}
571
572func (ctx *moduleContextImpl) staticBinary() bool {
573 if ctx.mod.linker == nil {
574 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
575 }
576 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
577 return linker.staticBinary()
578 } else {
579 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
580 }
581}
582
583func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
584 return Bool(ctx.mod.Properties.No_default_compiler_flags)
585}
586
587func (ctx *moduleContextImpl) sdk() bool {
588 return ctx.mod.Properties.Sdk_version != ""
589}
590
591func (ctx *moduleContextImpl) sdkVersion() string {
592 return ctx.mod.Properties.Sdk_version
593}
594
595func newBaseModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
596 return &Module{
597 hod: hod,
598 multilib: multilib,
599 }
600}
601
602func newModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
603 module := newBaseModule(hod, multilib)
604 module.features = []feature{
605 &stlFeature{},
606 }
607 return module
608}
609
610func (c *Module) GenerateAndroidBuildActions(actx common.AndroidModuleContext) {
611 ctx := &moduleContext{
612 AndroidModuleContext: actx,
613 moduleContextImpl: moduleContextImpl{
614 mod: c,
615 },
616 }
617 ctx.ctx = ctx
618
619 flags := Flags{
620 Toolchain: c.toolchain(ctx),
621 Clang: c.clang(ctx),
622 }
623
624 if c.compiler != nil {
625 flags = c.compiler.flags(ctx, flags)
626 }
627 if c.linker != nil {
628 flags = c.linker.flags(ctx, flags)
629 }
630 for _, feature := range c.features {
631 flags = feature.flags(ctx, flags)
632 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800633 if ctx.Failed() {
634 return
635 }
636
Colin Crossca860ac2016-01-04 14:34:37 -0800637 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
638 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
639 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800640
Colin Crossca860ac2016-01-04 14:34:37 -0800641 // Optimization to reduce size of build.ninja
642 // Replace the long list of flags for each file with a module-local variable
643 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
644 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
645 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
646 flags.CFlags = []string{"$cflags"}
647 flags.CppFlags = []string{"$cppflags"}
648 flags.AsFlags = []string{"$asflags"}
649
Colin Crossc99deeb2016-04-11 15:06:20 -0700650 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800651 if ctx.Failed() {
652 return
653 }
654
Colin Cross28344522015-04-22 13:07:53 -0700655 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700656
Colin Crossca860ac2016-01-04 14:34:37 -0800657 var objFiles common.Paths
658 if c.compiler != nil {
659 objFiles = c.compiler.compile(ctx, flags)
660 if ctx.Failed() {
661 return
662 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800663 }
664
Colin Crossca860ac2016-01-04 14:34:37 -0800665 if c.linker != nil {
666 outputFile := c.linker.link(ctx, flags, deps, objFiles)
667 if ctx.Failed() {
668 return
669 }
670 c.outputFile = common.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700671
Colin Crossc99deeb2016-04-11 15:06:20 -0700672 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800673 c.installer.install(ctx, outputFile)
674 if ctx.Failed() {
675 return
676 }
677 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700678 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800679}
680
Colin Crossca860ac2016-01-04 14:34:37 -0800681func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
682 if c.cachedToolchain == nil {
683 arch := ctx.Arch()
684 hod := ctx.HostOrDevice()
685 ht := ctx.HostType()
686 factory := toolchainFactories[hod][ht][arch.ArchType]
687 if factory == nil {
688 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
689 return nil
690 }
691 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800692 }
Colin Crossca860ac2016-01-04 14:34:37 -0800693 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800694}
695
Colin Crossca860ac2016-01-04 14:34:37 -0800696func (c *Module) begin(ctx BaseModuleContext) {
697 if c.compiler != nil {
698 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700699 }
Colin Crossca860ac2016-01-04 14:34:37 -0800700 if c.linker != nil {
701 c.linker.begin(ctx)
702 }
703 for _, feature := range c.features {
704 feature.begin(ctx)
705 }
706}
707
Colin Crossc99deeb2016-04-11 15:06:20 -0700708func (c *Module) deps(ctx BaseModuleContext) Deps {
709 deps := Deps{}
710
711 if c.compiler != nil {
712 deps = c.compiler.deps(ctx, deps)
713 }
714 if c.linker != nil {
715 deps = c.linker.deps(ctx, deps)
716 }
717 for _, feature := range c.features {
718 deps = feature.deps(ctx, deps)
719 }
720
721 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
722 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
723 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
724 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
725 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
726
727 return deps
728}
729
Colin Crossca860ac2016-01-04 14:34:37 -0800730func (c *Module) depsMutator(actx common.AndroidBottomUpMutatorContext) {
731 ctx := &baseModuleContext{
732 AndroidBaseContext: actx,
733 moduleContextImpl: moduleContextImpl{
734 mod: c,
735 },
736 }
737 ctx.ctx = ctx
738
739 if c.customizer != nil {
740 c.customizer.CustomizeProperties(ctx)
741 }
742
743 c.begin(ctx)
744
Colin Crossc99deeb2016-04-11 15:06:20 -0700745 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800746
Colin Crossc99deeb2016-04-11 15:06:20 -0700747 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
748
749 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
750 deps.WholeStaticLibs...)
751
752 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticDepTag,
753 deps.StaticLibs...)
754
755 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
756 deps.LateStaticLibs...)
757
758 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedDepTag,
759 deps.SharedLibs...)
760
761 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
762 deps.LateSharedLibs...)
763
764 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
765
766 if deps.CrtBegin != "" {
767 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800768 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700769 if deps.CrtEnd != "" {
770 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700771 }
Colin Cross6362e272015-10-29 15:25:03 -0700772}
Colin Cross21b9a242015-03-24 14:15:58 -0700773
Colin Cross6362e272015-10-29 15:25:03 -0700774func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800775 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700776 c.depsMutator(ctx)
777 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800778}
779
Colin Crossca860ac2016-01-04 14:34:37 -0800780func (c *Module) clang(ctx BaseModuleContext) bool {
781 clang := Bool(c.Properties.Clang)
782
783 if c.Properties.Clang == nil {
784 if ctx.Host() {
785 clang = true
786 }
787
788 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
789 clang = true
790 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800791 }
Colin Cross28344522015-04-22 13:07:53 -0700792
Colin Crossca860ac2016-01-04 14:34:37 -0800793 if !c.toolchain(ctx).ClangSupported() {
794 clang = false
795 }
796
797 return clang
798}
799
Colin Crossc99deeb2016-04-11 15:06:20 -0700800// Convert dependencies to paths. Returns a PathDeps containing paths
801func (c *Module) depsToPaths(ctx common.AndroidModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800802 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800803
Colin Crossc99deeb2016-04-11 15:06:20 -0700804 ctx.VisitDirectDeps(func(m blueprint.Module) {
805 name := ctx.OtherModuleName(m)
806 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800807
Colin Crossc99deeb2016-04-11 15:06:20 -0700808 a, _ := m.(common.AndroidModule)
809 if a == nil {
810 ctx.ModuleErrorf("module %q not an android module", name)
811 return
Colin Crossca860ac2016-01-04 14:34:37 -0800812 }
Colin Crossca860ac2016-01-04 14:34:37 -0800813
Colin Crossc99deeb2016-04-11 15:06:20 -0700814 c, _ := m.(*Module)
815 if c == nil {
816 if tag != common.DefaultsDepTag {
817 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800818 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700819 return
820 }
821
822 if !a.Enabled() {
823 ctx.ModuleErrorf("depends on disabled module %q", name)
824 return
825 }
826
827 if a.HostOrDevice() != ctx.HostOrDevice() {
828 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), name)
829 return
830 }
831
832 if !c.outputFile.Valid() {
833 ctx.ModuleErrorf("module %q missing output file", name)
834 return
835 }
836
837 if tag == reuseObjTag {
838 depPaths.ObjFiles = append(depPaths.ObjFiles,
839 c.compiler.(*libraryCompiler).reuseObjFiles...)
840 return
841 }
842
843 var cflags []string
844 if t, _ := tag.(dependencyTag); t.library {
845 if i, ok := c.linker.(exportedFlagsProducer); ok {
846 cflags = i.exportedFlags()
847 depPaths.Cflags = append(depPaths.Cflags, cflags...)
848 }
849 }
850
851 var depPtr *common.Paths
852
853 switch tag {
854 case sharedDepTag:
855 depPtr = &depPaths.SharedLibs
856 case lateSharedDepTag:
857 depPtr = &depPaths.LateSharedLibs
858 case staticDepTag:
859 depPtr = &depPaths.StaticLibs
860 case lateStaticDepTag:
861 depPtr = &depPaths.LateStaticLibs
862 case wholeStaticDepTag:
863 depPtr = &depPaths.WholeStaticLibs
864 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
865 staticLib, _ := c.linker.(*libraryLinker)
866 if staticLib == nil || !staticLib.static() {
867 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
868 return
869 }
870
871 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
872 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
873 for i := range missingDeps {
874 missingDeps[i] += postfix
875 }
876 ctx.AddMissingDependencies(missingDeps)
877 }
878 depPaths.WholeStaticLibObjFiles =
879 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
880 case objDepTag:
881 depPtr = &depPaths.ObjFiles
882 case crtBeginDepTag:
883 depPaths.CrtBegin = c.outputFile
884 case crtEndDepTag:
885 depPaths.CrtEnd = c.outputFile
886 default:
887 panic(fmt.Errorf("unknown dependency tag: %s", ctx.OtherModuleDependencyTag(m)))
888 }
889
890 if depPtr != nil {
891 *depPtr = append(*depPtr, c.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800892 }
893 })
894
895 return depPaths
896}
897
898func (c *Module) InstallInData() bool {
899 if c.installer == nil {
900 return false
901 }
902 return c.installer.inData()
903}
904
905// Compiler
906
907type baseCompiler struct {
908 Properties BaseCompilerProperties
909}
910
911var _ compiler = (*baseCompiler)(nil)
912
913func (compiler *baseCompiler) props() []interface{} {
914 return []interface{}{&compiler.Properties}
915}
916
917func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
918func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps { return deps }
919
920// Create a Flags struct that collects the compile flags from global values,
921// per-target values, module type values, and per-module Blueprints properties
922func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
923 toolchain := ctx.toolchain()
924
925 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
926 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
927 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
928 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
929 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
930
Colin Cross28344522015-04-22 13:07:53 -0700931 // Include dir cflags
Colin Crossca860ac2016-01-04 14:34:37 -0800932 rootIncludeDirs := common.PathsForSource(ctx, compiler.Properties.Include_dirs)
933 localIncludeDirs := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700934 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700935 includeDirsToFlags(localIncludeDirs),
936 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700937
Colin Crossca860ac2016-01-04 14:34:37 -0800938 rootIncludeFiles := common.PathsForSource(ctx, compiler.Properties.Include_files)
939 localIncludeFiles := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700940
941 flags.GlobalFlags = append(flags.GlobalFlags,
942 includeFilesToFlags(rootIncludeFiles),
943 includeFilesToFlags(localIncludeFiles))
944
Colin Crossca860ac2016-01-04 14:34:37 -0800945 if !ctx.noDefaultCompilerFlags() {
946 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700947 flags.GlobalFlags = append(flags.GlobalFlags,
948 "${commonGlobalIncludes}",
949 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800950 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700951 }
952
953 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700954 "-I" + common.PathForModuleSrc(ctx).String(),
955 "-I" + common.PathForModuleOut(ctx).String(),
956 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700957 }...)
958 }
959
Colin Crossca860ac2016-01-04 14:34:37 -0800960 instructionSet := compiler.Properties.Instruction_set
961 if flags.RequiredInstructionSet != "" {
962 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -0800963 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800964 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
965 if flags.Clang {
966 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
967 }
968 if err != nil {
969 ctx.ModuleErrorf("%s", err)
970 }
971
972 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -0800973 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800974
Colin Cross97ba0732015-03-23 17:50:24 -0700975 if flags.Clang {
976 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -0800977 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
978 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700979 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
980 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
981 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800982
983 target := "-target " + toolchain.ClangTriple()
984 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
985
Colin Cross97ba0732015-03-23 17:50:24 -0700986 flags.CFlags = append(flags.CFlags, target, gccPrefix)
987 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
988 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800989 }
990
Colin Crossca860ac2016-01-04 14:34:37 -0800991 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -0700992 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
993
Colin Cross97ba0732015-03-23 17:50:24 -0700994 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800995 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700996 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700997 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800998 toolchain.ClangCflags(),
999 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001000 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001001
1002 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001003 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001004 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001005 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001006 toolchain.Cflags(),
1007 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001008 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001009 }
1010
Colin Cross7b66f152015-12-15 16:07:43 -08001011 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1012 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1013 }
1014
Colin Crossf6566ed2015-03-24 11:13:38 -07001015 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001016 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001017 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001018 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001019 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001020 }
1021 }
1022
Colin Cross97ba0732015-03-23 17:50:24 -07001023 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001024
Colin Cross97ba0732015-03-23 17:50:24 -07001025 if flags.Clang {
1026 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001027 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001028 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001029 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001030 }
1031
Colin Crossc4bde762015-11-23 16:11:30 -08001032 if flags.Clang {
1033 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1034 } else {
1035 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001036 }
1037
Colin Crossca860ac2016-01-04 14:34:37 -08001038 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001039 if ctx.Host() && !flags.Clang {
1040 // The host GCC doesn't support C++14 (and is deprecated, so likely
1041 // never will). Build these modules with C++11.
1042 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1043 } else {
1044 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1045 }
1046 }
1047
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001048 // We can enforce some rules more strictly in the code we own. strict
1049 // indicates if this is code that we can be stricter with. If we have
1050 // rules that we want to apply to *our* code (but maybe can't for
1051 // vendor/device specific things), we could extend this to be a ternary
1052 // value.
1053 strict := true
1054 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
1055 strict = false
1056 }
1057
1058 // Can be used to make some annotations stricter for code we can fix
1059 // (such as when we mark functions as deprecated).
1060 if strict {
1061 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1062 }
1063
Colin Cross3f40fa42015-01-30 17:27:36 -08001064 return flags
1065}
1066
Colin Crossca860ac2016-01-04 14:34:37 -08001067func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1068 // Compile files listed in c.Properties.Srcs into objects
1069 objFiles := compiler.compileObjs(ctx, flags, "", compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
1070 if ctx.Failed() {
1071 return nil
1072 }
1073
1074 var genSrcs common.Paths
1075 ctx.VisitDirectDeps(func(module blueprint.Module) {
1076 if gen, ok := module.(genrule.SourceFileGenerator); ok {
1077 genSrcs = append(genSrcs, gen.GeneratedSourceFiles()...)
1078 }
1079 })
1080
1081 if len(genSrcs) != 0 {
1082 genObjs := TransformSourceToObj(ctx, "", genSrcs, flagsToBuilderFlags(flags), nil)
1083 objFiles = append(objFiles, genObjs...)
1084 }
1085
1086 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001087}
1088
1089// Compile a list of source files into objects a specified subdirectory
Colin Crossca860ac2016-01-04 14:34:37 -08001090func (compiler *baseCompiler) compileObjs(ctx common.AndroidModuleContext, flags Flags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001091 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001092
Colin Crossca860ac2016-01-04 14:34:37 -08001093 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001094
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001095 inputFiles := ctx.ExpandSources(srcFiles, excludes)
1096 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001097
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001098 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001099}
1100
Colin Crossca860ac2016-01-04 14:34:37 -08001101// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1102type baseLinker struct {
1103 Properties BaseLinkerProperties
1104 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001105 VariantIsShared bool `blueprint:"mutated"`
1106 VariantIsStatic bool `blueprint:"mutated"`
1107 VariantIsStaticBinary bool `blueprint:"mutated"`
1108 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001109 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001110}
1111
Dan Willemsend30e6102016-03-30 17:35:50 -07001112func (linker *baseLinker) begin(ctx BaseModuleContext) {
1113 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001114 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001115 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001116 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001117 }
1118}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001119
Colin Crossca860ac2016-01-04 14:34:37 -08001120func (linker *baseLinker) props() []interface{} {
1121 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001122}
1123
Colin Crossca860ac2016-01-04 14:34:37 -08001124func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1125 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1126 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1127 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001128
Colin Cross74d1ec02015-04-28 13:30:13 -07001129 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001130 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001131 }
1132
Colin Crossf6566ed2015-03-24 11:13:38 -07001133 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001134 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001135 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1136 if !Bool(linker.Properties.No_libgcc) {
1137 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001138 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001139
Colin Crossca860ac2016-01-04 14:34:37 -08001140 if !linker.static() {
1141 if linker.Properties.System_shared_libs != nil {
1142 deps.LateSharedLibs = append(deps.LateSharedLibs,
1143 linker.Properties.System_shared_libs...)
1144 } else if !ctx.sdk() {
1145 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1146 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001147 }
Colin Cross577f6e42015-03-27 18:23:34 -07001148
Colin Crossca860ac2016-01-04 14:34:37 -08001149 if ctx.sdk() {
1150 version := ctx.sdkVersion()
1151 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001152 "ndk_libc."+version,
1153 "ndk_libm."+version,
1154 )
1155 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001156 }
1157
Colin Crossca860ac2016-01-04 14:34:37 -08001158 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001159}
1160
Colin Crossca860ac2016-01-04 14:34:37 -08001161func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1162 toolchain := ctx.toolchain()
1163
1164 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1165
1166 if !ctx.noDefaultCompilerFlags() {
1167 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1168 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1169 }
1170
1171 if flags.Clang {
1172 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1173 } else {
1174 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1175 }
1176
1177 if ctx.Host() {
1178 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1179 }
1180 }
1181
Dan Willemsend30e6102016-03-30 17:35:50 -07001182 if ctx.Host() && !linker.static() {
1183 rpath_prefix := `\$$ORIGIN/`
1184 if ctx.Darwin() {
1185 rpath_prefix = "@loader_path/"
1186 }
1187
Colin Crossc99deeb2016-04-11 15:06:20 -07001188 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001189 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1190 }
1191 }
1192
Dan Willemsene7174922016-03-30 17:33:52 -07001193 if flags.Clang {
1194 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1195 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001196 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1197 }
1198
1199 return flags
1200}
1201
1202func (linker *baseLinker) static() bool {
1203 return linker.dynamicProperties.VariantIsStatic
1204}
1205
1206func (linker *baseLinker) staticBinary() bool {
1207 return linker.dynamicProperties.VariantIsStaticBinary
1208}
1209
1210func (linker *baseLinker) setStatic(static bool) {
1211 linker.dynamicProperties.VariantIsStatic = static
1212}
1213
1214type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001215 // Returns true if the build options for the module have selected a static or shared build
1216 buildStatic() bool
1217 buildShared() bool
1218
1219 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001220 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001221
Colin Cross18b6dc52015-04-28 13:20:37 -07001222 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001223 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001224
1225 // Returns whether a module is a static binary
1226 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001227}
1228
Colin Crossca860ac2016-01-04 14:34:37 -08001229type exportedFlagsProducer interface {
Colin Cross28344522015-04-22 13:07:53 -07001230 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001231}
1232
Colin Crossca860ac2016-01-04 14:34:37 -08001233type baseInstaller struct {
1234 Properties InstallerProperties
1235
1236 dir string
1237 dir64 string
1238 data bool
1239
Colin Crossa2344662016-03-24 13:14:12 -07001240 path common.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001241}
1242
1243var _ installer = (*baseInstaller)(nil)
1244
1245func (installer *baseInstaller) props() []interface{} {
1246 return []interface{}{&installer.Properties}
1247}
1248
1249func (installer *baseInstaller) install(ctx ModuleContext, file common.Path) {
1250 subDir := installer.dir
1251 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1252 subDir = installer.dir64
1253 }
1254 dir := common.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
1255 installer.path = ctx.InstallFile(dir, file)
1256}
1257
1258func (installer *baseInstaller) inData() bool {
1259 return installer.data
1260}
1261
Colin Cross3f40fa42015-01-30 17:27:36 -08001262//
1263// Combined static+shared libraries
1264//
1265
Colin Crossca860ac2016-01-04 14:34:37 -08001266type libraryCompiler struct {
1267 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001268
Colin Crossca860ac2016-01-04 14:34:37 -08001269 linker *libraryLinker
1270 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001271
Colin Crossca860ac2016-01-04 14:34:37 -08001272 // For reusing static library objects for shared library
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001273 reuseObjFiles common.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001274}
1275
Colin Crossca860ac2016-01-04 14:34:37 -08001276var _ compiler = (*libraryCompiler)(nil)
1277
1278func (library *libraryCompiler) props() []interface{} {
1279 props := library.baseCompiler.props()
1280 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001281}
1282
Colin Crossca860ac2016-01-04 14:34:37 -08001283func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1284 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001285
Dan Willemsen490fd492015-11-24 17:53:15 -08001286 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1287 // all code is position independent, and then those warnings get promoted to
1288 // errors.
1289 if ctx.HostType() != common.Windows {
1290 flags.CFlags = append(flags.CFlags, "-fPIC")
1291 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001292
Colin Crossca860ac2016-01-04 14:34:37 -08001293 if library.linker.static() {
1294 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001295 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001296 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001297 }
1298
Colin Crossca860ac2016-01-04 14:34:37 -08001299 return flags
1300}
1301
1302func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags) common.Paths {
1303 var objFiles common.Paths
1304
Colin Crossc99deeb2016-04-11 15:06:20 -07001305 objFiles = library.baseCompiler.compile(ctx, flags)
1306 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001307
1308 if library.linker.static() {
1309 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceStaticLibrary,
1310 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs)...)
1311 } else {
1312 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceSharedLibrary,
1313 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs)...)
1314 }
1315
1316 return objFiles
1317}
1318
1319type libraryLinker struct {
1320 baseLinker
1321
1322 Properties LibraryLinkerProperties
1323
1324 dynamicProperties struct {
1325 BuildStatic bool `blueprint:"mutated"`
1326 BuildShared bool `blueprint:"mutated"`
1327 }
1328
1329 exportFlags []string
1330
1331 // If we're used as a whole_static_lib, our missing dependencies need
1332 // to be given
1333 wholeStaticMissingDeps []string
1334
1335 // For whole_static_libs
1336 objFiles common.Paths
1337}
1338
1339var _ linker = (*libraryLinker)(nil)
1340var _ exportedFlagsProducer = (*libraryLinker)(nil)
1341
1342func (library *libraryLinker) props() []interface{} {
1343 props := library.baseLinker.props()
1344 return append(props, &library.Properties, &library.dynamicProperties)
1345}
1346
1347func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1348 flags = library.baseLinker.flags(ctx, flags)
1349
1350 flags.Nocrt = Bool(library.Properties.Nocrt)
1351
1352 if !library.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001353 libName := ctx.ModuleName()
1354 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1355 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001356 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001357 sharedFlag = "-shared"
1358 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001359 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001360 flags.LdFlags = append(flags.LdFlags,
1361 "-nostdlib",
1362 "-Wl,--gc-sections",
1363 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001364 }
Colin Cross97ba0732015-03-23 17:50:24 -07001365
Colin Cross0af4b842015-04-30 16:36:18 -07001366 if ctx.Darwin() {
1367 flags.LdFlags = append(flags.LdFlags,
1368 "-dynamiclib",
1369 "-single_module",
1370 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001371 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001372 )
1373 } else {
1374 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001375 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001376 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001377 )
1378 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001379 }
Colin Cross97ba0732015-03-23 17:50:24 -07001380
1381 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001382}
1383
Colin Crossca860ac2016-01-04 14:34:37 -08001384func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1385 deps = library.baseLinker.deps(ctx, deps)
1386 if library.static() {
1387 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1388 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1389 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1390 } else {
1391 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1392 if !ctx.sdk() {
1393 deps.CrtBegin = "crtbegin_so"
1394 deps.CrtEnd = "crtend_so"
1395 } else {
1396 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1397 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1398 }
1399 }
1400 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1401 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1402 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1403 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001404
Colin Crossca860ac2016-01-04 14:34:37 -08001405 return deps
1406}
Colin Cross3f40fa42015-01-30 17:27:36 -08001407
Colin Crossca860ac2016-01-04 14:34:37 -08001408func (library *libraryLinker) exportedFlags() []string {
1409 return library.exportFlags
1410}
1411
1412func (library *libraryLinker) linkStatic(ctx ModuleContext,
1413 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
1414
Colin Cross21b9a242015-03-24 14:15:58 -07001415 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Crossca860ac2016-01-04 14:34:37 -08001416 library.objFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001417
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001418 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001419
Colin Cross0af4b842015-04-30 16:36:18 -07001420 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001421 TransformDarwinObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001422 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001423 TransformObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001424 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001425
Colin Crossca860ac2016-01-04 14:34:37 -08001426 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001427
1428 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001429
1430 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001431}
1432
Colin Crossca860ac2016-01-04 14:34:37 -08001433func (library *libraryLinker) linkShared(ctx ModuleContext,
1434 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001435
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001437
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001438 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001439
Colin Crossca860ac2016-01-04 14:34:37 -08001440 versionScript := common.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1441 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1442 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1443 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001444 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001445 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001446 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001447 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001448 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001449 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001450 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1451 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001452 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001453 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1454 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001455 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001456 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1457 }
1458 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001459 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001460 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1461 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001462 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001463 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001464 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001465 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001466 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001467 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001468 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001469 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001470 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001471 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001472 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001473 }
Colin Crossaee540a2015-07-06 17:48:31 -07001474 }
1475
Colin Crossca860ac2016-01-04 14:34:37 -08001476 sharedLibs := deps.SharedLibs
1477 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001478
Colin Crossca860ac2016-01-04 14:34:37 -08001479 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1480 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
1481 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, flagsToBuilderFlags(flags), outputFile)
1482
1483 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001484}
1485
Colin Crossca860ac2016-01-04 14:34:37 -08001486func (library *libraryLinker) link(ctx ModuleContext,
1487 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001488
Colin Crossc99deeb2016-04-11 15:06:20 -07001489 objFiles = append(objFiles, deps.ObjFiles...)
1490
Colin Crossca860ac2016-01-04 14:34:37 -08001491 var out common.Path
1492 if library.static() {
1493 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001494 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001495 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001496 }
1497
Colin Crossca860ac2016-01-04 14:34:37 -08001498 includeDirs := common.PathsForModuleSrc(ctx, library.Properties.Export_include_dirs)
1499 library.exportFlags = []string{includeDirsToFlags(includeDirs)}
1500 library.exportFlags = append(library.exportFlags, deps.ReexportedCflags...)
1501
1502 return out
1503}
1504
1505func (library *libraryLinker) buildStatic() bool {
1506 return library.dynamicProperties.BuildStatic
1507}
1508
1509func (library *libraryLinker) buildShared() bool {
1510 return library.dynamicProperties.BuildShared
1511}
1512
1513func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1514 return library.wholeStaticMissingDeps
1515}
1516
Colin Crossc99deeb2016-04-11 15:06:20 -07001517func (library *libraryLinker) installable() bool {
1518 return !library.static()
1519}
1520
Colin Crossca860ac2016-01-04 14:34:37 -08001521type libraryInstaller struct {
1522 baseInstaller
1523
1524 linker *libraryLinker
1525}
1526
1527func (library *libraryInstaller) install(ctx ModuleContext, file common.Path) {
1528 if !library.linker.static() {
1529 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001530 }
1531}
1532
Colin Crossca860ac2016-01-04 14:34:37 -08001533func NewLibrary(hod common.HostOrDeviceSupported, shared, static bool) *Module {
1534 module := newModule(hod, common.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001535
Colin Crossca860ac2016-01-04 14:34:37 -08001536 linker := &libraryLinker{}
1537 linker.dynamicProperties.BuildShared = shared
1538 linker.dynamicProperties.BuildStatic = static
1539 module.linker = linker
1540
1541 module.compiler = &libraryCompiler{
1542 linker: linker,
1543 }
1544 module.installer = &libraryInstaller{
1545 baseInstaller: baseInstaller{
1546 dir: "lib",
1547 dir64: "lib64",
1548 },
1549 linker: linker,
Dan Albertc403f7c2015-03-18 14:01:18 -07001550 }
1551
Colin Crossca860ac2016-01-04 14:34:37 -08001552 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001553}
1554
Colin Crossca860ac2016-01-04 14:34:37 -08001555func libraryFactory() (blueprint.Module, []interface{}) {
1556 module := NewLibrary(common.HostAndDeviceSupported, true, true)
1557 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001558}
1559
Colin Cross3f40fa42015-01-30 17:27:36 -08001560//
1561// Objects (for crt*.o)
1562//
1563
Colin Crossca860ac2016-01-04 14:34:37 -08001564type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001565 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001566}
1567
Colin Crossca860ac2016-01-04 14:34:37 -08001568func objectFactory() (blueprint.Module, []interface{}) {
1569 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
1570 module.compiler = &baseCompiler{}
1571 module.linker = &objectLinker{}
1572 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001573}
1574
Colin Cross81413472016-04-11 14:37:39 -07001575func (object *objectLinker) props() []interface{} {
1576 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001577}
1578
Colin Crossca860ac2016-01-04 14:34:37 -08001579func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001580
Colin Cross81413472016-04-11 14:37:39 -07001581func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1582 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001583 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001584}
1585
Colin Crossca860ac2016-01-04 14:34:37 -08001586func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001587 if flags.Clang {
1588 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1589 } else {
1590 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1591 }
1592
Colin Crossca860ac2016-01-04 14:34:37 -08001593 return flags
1594}
1595
1596func (object *objectLinker) link(ctx ModuleContext,
1597 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001598
Colin Cross97ba0732015-03-23 17:50:24 -07001599 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001600
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001601 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001602 if len(objFiles) == 1 {
1603 outputFile = objFiles[0]
1604 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001605 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001606 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001607 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001608 }
1609
Colin Cross3f40fa42015-01-30 17:27:36 -08001610 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001611 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001612}
1613
Colin Crossc99deeb2016-04-11 15:06:20 -07001614func (*objectLinker) installable() bool {
1615 return false
1616}
1617
Colin Cross3f40fa42015-01-30 17:27:36 -08001618//
1619// Executables
1620//
1621
Colin Crossca860ac2016-01-04 14:34:37 -08001622type binaryLinker struct {
1623 baseLinker
Colin Cross7d5136f2015-05-11 13:39:40 -07001624
Colin Crossca860ac2016-01-04 14:34:37 -08001625 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001626
Colin Crossca860ac2016-01-04 14:34:37 -08001627 hostToolPath common.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001628}
1629
Colin Crossca860ac2016-01-04 14:34:37 -08001630var _ linker = (*binaryLinker)(nil)
1631
1632func (binary *binaryLinker) props() []interface{} {
1633 return append(binary.baseLinker.props(), &binary.Properties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001634}
1635
Colin Crossca860ac2016-01-04 14:34:37 -08001636func (binary *binaryLinker) buildStatic() bool {
1637 return Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001638}
1639
Colin Crossca860ac2016-01-04 14:34:37 -08001640func (binary *binaryLinker) buildShared() bool {
1641 return !Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001642}
1643
Colin Crossca860ac2016-01-04 14:34:37 -08001644func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001645 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001646 if binary.Properties.Stem != "" {
1647 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001648 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001649
Colin Crossca860ac2016-01-04 14:34:37 -08001650 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001651}
1652
Colin Crossca860ac2016-01-04 14:34:37 -08001653func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1654 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001655 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001656 if !ctx.sdk() {
1657 if Bool(binary.Properties.Static_executable) {
1658 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001659 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001660 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001661 }
Colin Crossca860ac2016-01-04 14:34:37 -08001662 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001663 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001664 if Bool(binary.Properties.Static_executable) {
1665 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001666 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001667 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001668 }
Colin Crossca860ac2016-01-04 14:34:37 -08001669 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001670 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001671
Colin Crossca860ac2016-01-04 14:34:37 -08001672 if Bool(binary.Properties.Static_executable) {
1673 if inList("libc++_static", deps.StaticLibs) {
1674 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001675 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001676 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1677 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1678 // move them to the beginning of deps.LateStaticLibs
1679 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001680 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001681 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001682 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001683 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001684 }
Colin Crossca860ac2016-01-04 14:34:37 -08001685
1686 if !Bool(binary.Properties.Static_executable) && inList("libc", deps.StaticLibs) {
1687 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1688 "from static libs or set static_executable: true")
1689 }
1690 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001691}
1692
Colin Crossc99deeb2016-04-11 15:06:20 -07001693func (*binaryLinker) installable() bool {
1694 return true
1695}
1696
Colin Crossca860ac2016-01-04 14:34:37 -08001697func NewBinary(hod common.HostOrDeviceSupported) *Module {
1698 module := newModule(hod, common.MultilibFirst)
1699 module.compiler = &baseCompiler{}
1700 module.linker = &binaryLinker{}
1701 module.installer = &baseInstaller{
1702 dir: "bin",
1703 }
1704 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001705}
1706
Colin Crossca860ac2016-01-04 14:34:37 -08001707func binaryFactory() (blueprint.Module, []interface{}) {
1708 module := NewBinary(common.HostAndDeviceSupported)
1709 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001710}
1711
Colin Crossca860ac2016-01-04 14:34:37 -08001712func (binary *binaryLinker) ModifyProperties(ctx ModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001713 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001714 binary.Properties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001715 }
Colin Crossca860ac2016-01-04 14:34:37 -08001716 if Bool(binary.Properties.Static_executable) {
1717 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001718 }
1719}
1720
Colin Crossca860ac2016-01-04 14:34:37 -08001721func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1722 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001723
Dan Willemsen490fd492015-11-24 17:53:15 -08001724 if ctx.Host() {
1725 flags.LdFlags = append(flags.LdFlags, "-pie")
1726 if ctx.HostType() == common.Windows {
1727 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1728 }
1729 }
1730
1731 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1732 // all code is position independent, and then those warnings get promoted to
1733 // errors.
1734 if ctx.HostType() != common.Windows {
1735 flags.CFlags = append(flags.CFlags, "-fpie")
1736 }
Colin Cross97ba0732015-03-23 17:50:24 -07001737
Colin Crossf6566ed2015-03-24 11:13:38 -07001738 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001739 if Bool(binary.Properties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001740 // Clang driver needs -static to create static executable.
1741 // However, bionic/linker uses -shared to overwrite.
1742 // Linker for x86 targets does not allow coexistance of -static and -shared,
1743 // so we add -static only if -shared is not used.
1744 if !inList("-shared", flags.LdFlags) {
1745 flags.LdFlags = append(flags.LdFlags, "-static")
1746 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001747
Colin Crossed4cf0b2015-03-26 14:43:45 -07001748 flags.LdFlags = append(flags.LdFlags,
1749 "-nostdlib",
1750 "-Bstatic",
1751 "-Wl,--gc-sections",
1752 )
1753
1754 } else {
1755 linker := "/system/bin/linker"
1756 if flags.Toolchain.Is64Bit() {
Colin Crossca860ac2016-01-04 14:34:37 -08001757 linker += "64"
Colin Crossed4cf0b2015-03-26 14:43:45 -07001758 }
1759
1760 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001761 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001762 "-nostdlib",
1763 "-Bdynamic",
1764 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1765 "-Wl,--gc-sections",
1766 "-Wl,-z,nocopyreloc",
1767 )
1768 }
Colin Cross0af4b842015-04-30 16:36:18 -07001769 } else if ctx.Darwin() {
1770 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001771 }
1772
Colin Cross97ba0732015-03-23 17:50:24 -07001773 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001774}
1775
Colin Crossca860ac2016-01-04 14:34:37 -08001776func (binary *binaryLinker) link(ctx ModuleContext,
1777 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001778
Colin Crossca860ac2016-01-04 14:34:37 -08001779 outputFile := common.PathForModuleOut(ctx, binary.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
1780 if ctx.HostOrDevice().Host() {
1781 binary.hostToolPath = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001782 }
Colin Crossca860ac2016-01-04 14:34:37 -08001783 ret := outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001784
Colin Crossca860ac2016-01-04 14:34:37 -08001785 if binary.Properties.Prefix_symbols != "" {
Colin Crossbfae8852015-03-26 14:44:11 -07001786 afterPrefixSymbols := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001787 outputFile = common.PathForModuleOut(ctx, binary.getStem(ctx)+".intermediate")
1788 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1789 flagsToBuilderFlags(flags), afterPrefixSymbols)
Colin Crossbfae8852015-03-26 14:44:11 -07001790 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001791
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001792 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001793
Colin Crossca860ac2016-01-04 14:34:37 -08001794 sharedLibs := deps.SharedLibs
1795 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1796
1797 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001798 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossca860ac2016-01-04 14:34:37 -08001799 flagsToBuilderFlags(flags), outputFile)
1800
1801 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001802}
Colin Cross3f40fa42015-01-30 17:27:36 -08001803
Colin Crossca860ac2016-01-04 14:34:37 -08001804func (binary *binaryLinker) HostToolPath() common.OptionalPath {
1805 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001806}
1807
Colin Cross6362e272015-10-29 15:25:03 -07001808func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08001809 if m, ok := mctx.Module().(*Module); ok {
1810 if test, ok := m.linker.(*testLinker); ok {
1811 if Bool(test.Properties.Test_per_src) {
1812 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
1813 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1814 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1815 }
1816 tests := mctx.CreateLocalVariations(testNames...)
1817 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1818 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
1819 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
1820 }
Colin Cross6002e052015-09-16 16:00:08 -07001821 }
1822 }
1823 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001824}
1825
Colin Crossca860ac2016-01-04 14:34:37 -08001826type testLinker struct {
1827 binaryLinker
1828 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001829}
1830
Dan Willemsend30e6102016-03-30 17:35:50 -07001831func (test *testLinker) begin(ctx BaseModuleContext) {
1832 test.binaryLinker.begin(ctx)
1833
1834 runpath := "../../lib"
1835 if ctx.toolchain().Is64Bit() {
1836 runpath += "64"
1837 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001838 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07001839}
1840
Colin Crossca860ac2016-01-04 14:34:37 -08001841func (test *testLinker) props() []interface{} {
1842 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07001843}
1844
Colin Crossca860ac2016-01-04 14:34:37 -08001845func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
1846 flags = test.binaryLinker.flags(ctx, flags)
1847
1848 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001849 return flags
1850 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001851
Colin Cross97ba0732015-03-23 17:50:24 -07001852 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001853 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001854 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001855
1856 if ctx.HostType() == common.Windows {
1857 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1858 } else {
1859 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1860 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1861 }
1862 } else {
1863 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001864 }
1865
1866 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001867 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001868 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001869
Colin Cross21b9a242015-03-24 14:15:58 -07001870 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001871}
1872
Colin Crossca860ac2016-01-04 14:34:37 -08001873func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1874 if test.Properties.Gtest {
1875 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001876 }
Colin Crossca860ac2016-01-04 14:34:37 -08001877 deps = test.binaryLinker.deps(ctx, deps)
1878 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07001879}
1880
Colin Crossca860ac2016-01-04 14:34:37 -08001881type testInstaller struct {
1882 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08001883}
1884
Colin Crossca860ac2016-01-04 14:34:37 -08001885func (installer *testInstaller) install(ctx ModuleContext, file common.Path) {
1886 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
1887 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
1888 installer.baseInstaller.install(ctx, file)
1889}
1890
1891func NewTest(hod common.HostOrDeviceSupported) *Module {
1892 module := newModule(hod, common.MultilibBoth)
1893 module.compiler = &baseCompiler{}
1894 linker := &testLinker{}
1895 linker.Properties.Gtest = true
1896 module.linker = linker
1897 module.installer = &testInstaller{
1898 baseInstaller: baseInstaller{
1899 dir: "nativetest",
1900 dir64: "nativetest64",
1901 data: true,
1902 },
Dan Albertc403f7c2015-03-18 14:01:18 -07001903 }
Colin Crossca860ac2016-01-04 14:34:37 -08001904 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001905}
1906
Colin Crossca860ac2016-01-04 14:34:37 -08001907func testFactory() (blueprint.Module, []interface{}) {
1908 module := NewTest(common.HostAndDeviceSupported)
1909 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001910}
1911
Colin Crossca860ac2016-01-04 14:34:37 -08001912type benchmarkLinker struct {
1913 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07001914}
1915
Colin Crossca860ac2016-01-04 14:34:37 -08001916func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1917 deps = benchmark.binaryLinker.deps(ctx, deps)
1918 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
1919 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07001920}
1921
Colin Crossca860ac2016-01-04 14:34:37 -08001922func NewBenchmark(hod common.HostOrDeviceSupported) *Module {
1923 module := newModule(hod, common.MultilibFirst)
1924 module.compiler = &baseCompiler{}
1925 module.linker = &benchmarkLinker{}
1926 module.installer = &baseInstaller{
1927 dir: "nativetest",
1928 dir64: "nativetest64",
1929 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07001930 }
Colin Crossca860ac2016-01-04 14:34:37 -08001931 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07001932}
1933
Colin Crossca860ac2016-01-04 14:34:37 -08001934func benchmarkFactory() (blueprint.Module, []interface{}) {
1935 module := NewBenchmark(common.HostAndDeviceSupported)
1936 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07001937}
1938
Colin Cross3f40fa42015-01-30 17:27:36 -08001939//
1940// Static library
1941//
1942
Colin Crossca860ac2016-01-04 14:34:37 -08001943func libraryStaticFactory() (blueprint.Module, []interface{}) {
1944 module := NewLibrary(common.HostAndDeviceSupported, false, true)
1945 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001946}
1947
1948//
1949// Shared libraries
1950//
1951
Colin Crossca860ac2016-01-04 14:34:37 -08001952func librarySharedFactory() (blueprint.Module, []interface{}) {
1953 module := NewLibrary(common.HostAndDeviceSupported, true, false)
1954 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001955}
1956
1957//
1958// Host static library
1959//
1960
Colin Crossca860ac2016-01-04 14:34:37 -08001961func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
1962 module := NewLibrary(common.HostSupported, false, true)
1963 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001964}
1965
1966//
1967// Host Shared libraries
1968//
1969
Colin Crossca860ac2016-01-04 14:34:37 -08001970func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
1971 module := NewLibrary(common.HostSupported, true, false)
1972 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001973}
1974
1975//
1976// Host Binaries
1977//
1978
Colin Crossca860ac2016-01-04 14:34:37 -08001979func binaryHostFactory() (blueprint.Module, []interface{}) {
1980 module := NewBinary(common.HostSupported)
1981 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001982}
1983
1984//
Colin Cross1f8f2342015-03-26 16:09:47 -07001985// Host Tests
1986//
1987
Colin Crossca860ac2016-01-04 14:34:37 -08001988func testHostFactory() (blueprint.Module, []interface{}) {
1989 module := NewTest(common.HostSupported)
1990 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07001991}
1992
1993//
Colin Cross2ba19d92015-05-07 15:44:20 -07001994// Host Benchmarks
1995//
1996
Colin Crossca860ac2016-01-04 14:34:37 -08001997func benchmarkHostFactory() (blueprint.Module, []interface{}) {
1998 module := NewBenchmark(common.HostSupported)
1999 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002000}
2001
2002//
Colin Crosscfad1192015-11-02 16:43:11 -08002003// Defaults
2004//
Colin Crossca860ac2016-01-04 14:34:37 -08002005type Defaults struct {
Colin Crosscfad1192015-11-02 16:43:11 -08002006 common.AndroidModuleBase
2007 common.DefaultsModule
2008}
2009
Colin Crossca860ac2016-01-04 14:34:37 -08002010func (*Defaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002011}
2012
Colin Crossca860ac2016-01-04 14:34:37 -08002013func defaultsFactory() (blueprint.Module, []interface{}) {
2014 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002015
2016 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002017 &BaseProperties{},
2018 &BaseCompilerProperties{},
2019 &BaseLinkerProperties{},
2020 &LibraryCompilerProperties{},
2021 &LibraryLinkerProperties{},
2022 &BinaryLinkerProperties{},
2023 &TestLinkerProperties{},
2024 &UnusedProperties{},
2025 &StlProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002026 }
2027
Dan Willemsen218f6562015-07-08 18:13:11 -07002028 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
2029 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002030
2031 return common.InitDefaultsModule(module, module, propertyStructs...)
2032}
2033
2034//
Colin Cross3f40fa42015-01-30 17:27:36 -08002035// Device libraries shipped with gcc
2036//
2037
Colin Crossca860ac2016-01-04 14:34:37 -08002038type toolchainLibraryLinker struct {
2039 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002040}
2041
Colin Crossca860ac2016-01-04 14:34:37 -08002042var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2043
2044func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002045 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002046 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002047}
2048
Colin Crossca860ac2016-01-04 14:34:37 -08002049func (*toolchainLibraryLinker) buildStatic() bool {
2050 return true
2051}
Colin Cross3f40fa42015-01-30 17:27:36 -08002052
Colin Crossca860ac2016-01-04 14:34:37 -08002053func (*toolchainLibraryLinker) buildShared() bool {
2054 return false
2055}
2056
2057func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
2058 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2059 module.compiler = &baseCompiler{}
2060 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002061 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002062 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002063}
2064
Colin Crossca860ac2016-01-04 14:34:37 -08002065func (library *toolchainLibraryLinker) link(ctx ModuleContext,
2066 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002067
2068 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002069 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002070
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002071 if flags.Clang {
2072 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2073 }
2074
Colin Crossca860ac2016-01-04 14:34:37 -08002075 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002076
2077 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002078
Colin Crossca860ac2016-01-04 14:34:37 -08002079 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002080}
2081
Colin Crossc99deeb2016-04-11 15:06:20 -07002082func (*toolchainLibraryLinker) installable() bool {
2083 return false
2084}
2085
Dan Albertbe961682015-03-18 23:38:50 -07002086// NDK prebuilt libraries.
2087//
2088// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2089// either (with the exception of the shared STLs, which are installed to the app's directory rather
2090// than to the system image).
2091
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002092func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
2093 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
2094 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07002095}
2096
Dan Albertc3144b12015-04-28 18:17:56 -07002097func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002098 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002099
2100 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2101 // We want to translate to just NAME.EXT
2102 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2103 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002104 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002105}
2106
Colin Crossca860ac2016-01-04 14:34:37 -08002107type ndkPrebuiltObjectLinker struct {
2108 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002109}
2110
Colin Crossca860ac2016-01-04 14:34:37 -08002111func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002112 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002113 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002114}
2115
Colin Crossca860ac2016-01-04 14:34:37 -08002116func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
2117 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2118 module.linker = &ndkPrebuiltObjectLinker{}
2119 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002120}
2121
Colin Crossca860ac2016-01-04 14:34:37 -08002122func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
2123 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002124 // A null build step, but it sets up the output path.
2125 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2126 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2127 }
2128
Colin Crossca860ac2016-01-04 14:34:37 -08002129 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002130}
2131
Colin Crossca860ac2016-01-04 14:34:37 -08002132type ndkPrebuiltLibraryLinker struct {
2133 libraryLinker
2134 Properties struct {
2135 Export_include_dirs []string `android:"arch_variant"`
2136 }
Dan Albertc3144b12015-04-28 18:17:56 -07002137}
2138
Colin Crossca860ac2016-01-04 14:34:37 -08002139var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2140var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002141
Colin Crossca860ac2016-01-04 14:34:37 -08002142func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Crossc99deeb2016-04-11 15:06:20 -07002143 return append(ndk.libraryLinker.props(), &ndk.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002144}
2145
Colin Crossca860ac2016-01-04 14:34:37 -08002146func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002147 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002148 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002149}
2150
Colin Crossca860ac2016-01-04 14:34:37 -08002151func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2152 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2153 linker := &ndkPrebuiltLibraryLinker{}
2154 linker.dynamicProperties.BuildShared = true
2155 module.linker = linker
2156 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002157}
2158
Colin Crossca860ac2016-01-04 14:34:37 -08002159func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
2160 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002161 // A null build step, but it sets up the output path.
2162 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2163 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2164 }
2165
Colin Crossca860ac2016-01-04 14:34:37 -08002166 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2167 ndk.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002168
Colin Crossca860ac2016-01-04 14:34:37 -08002169 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2170 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002171}
2172
2173// The NDK STLs are slightly different from the prebuilt system libraries:
2174// * Are not specific to each platform version.
2175// * The libraries are not in a predictable location for each STL.
2176
Colin Crossca860ac2016-01-04 14:34:37 -08002177type ndkPrebuiltStlLinker struct {
2178 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002179}
2180
Colin Crossca860ac2016-01-04 14:34:37 -08002181func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2182 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2183 linker := &ndkPrebuiltStlLinker{}
2184 linker.dynamicProperties.BuildShared = true
2185 module.linker = linker
2186 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002187}
2188
Colin Crossca860ac2016-01-04 14:34:37 -08002189func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2190 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2191 linker := &ndkPrebuiltStlLinker{}
2192 linker.dynamicProperties.BuildStatic = true
2193 module.linker = linker
2194 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002195}
2196
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002197func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002198 gccVersion := toolchain.GccVersion()
2199 var libDir string
2200 switch stl {
2201 case "libstlport":
2202 libDir = "cxx-stl/stlport/libs"
2203 case "libc++":
2204 libDir = "cxx-stl/llvm-libc++/libs"
2205 case "libgnustl":
2206 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2207 }
2208
2209 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002210 ndkSrcRoot := "prebuilts/ndk/current/sources"
2211 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002212 }
2213
2214 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002215 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002216}
2217
Colin Crossca860ac2016-01-04 14:34:37 -08002218func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
2219 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002220 // A null build step, but it sets up the output path.
2221 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2222 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2223 }
2224
Colin Crossca860ac2016-01-04 14:34:37 -08002225 includeDirs := common.PathsForModuleSrc(ctx, ndk.Properties.Export_include_dirs)
2226 ndk.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002227
2228 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002229 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002230 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002231 libExt = staticLibraryExtension
2232 }
2233
2234 stlName := strings.TrimSuffix(libName, "_shared")
2235 stlName = strings.TrimSuffix(stlName, "_static")
2236 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002237 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002238}
2239
Colin Cross6362e272015-10-29 15:25:03 -07002240func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002241 if m, ok := mctx.Module().(*Module); ok {
2242 if m.linker != nil {
2243 if linker, ok := m.linker.(baseLinkerInterface); ok {
2244 var modules []blueprint.Module
2245 if linker.buildStatic() && linker.buildShared() {
2246 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002247 static := modules[0].(*Module)
2248 shared := modules[1].(*Module)
2249
2250 static.linker.(baseLinkerInterface).setStatic(true)
2251 shared.linker.(baseLinkerInterface).setStatic(false)
2252
2253 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2254 sharedCompiler := shared.compiler.(*libraryCompiler)
2255 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2256 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2257 // Optimize out compiling common .o files twice for static+shared libraries
2258 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2259 sharedCompiler.baseCompiler.Properties.Srcs = nil
2260 }
2261 }
Colin Crossca860ac2016-01-04 14:34:37 -08002262 } else if linker.buildStatic() {
2263 modules = mctx.CreateLocalVariations("static")
2264 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2265 } else if linker.buildShared() {
2266 modules = mctx.CreateLocalVariations("shared")
2267 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2268 } else {
2269 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2270 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002271 }
2272 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002273 }
2274}
Colin Cross74d1ec02015-04-28 13:30:13 -07002275
2276// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2277// modifies the slice contents in place, and returns a subslice of the original slice
2278func lastUniqueElements(list []string) []string {
2279 totalSkip := 0
2280 for i := len(list) - 1; i >= totalSkip; i-- {
2281 skip := 0
2282 for j := i - 1; j >= totalSkip; j-- {
2283 if list[i] == list[j] {
2284 skip++
2285 } else {
2286 list[j+skip] = list[j]
2287 }
2288 }
2289 totalSkip += skip
2290 }
2291 return list[totalSkip:]
2292}
Colin Cross06a931b2015-10-28 17:23:31 -07002293
2294var Bool = proptools.Bool