blob: 417dc0d0ff8f8d25bc22febcbe88024547b283ed [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
Dan Willemsenb40aab62016-04-20 14:21:14 -0700180 GeneratedSources []string
181 GeneratedHeaders []string
182
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700183 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700184
Colin Cross97ba0732015-03-23 17:50:24 -0700185 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700186}
187
Colin Crossca860ac2016-01-04 14:34:37 -0800188type PathDeps struct {
189 SharedLibs, LateSharedLibs common.Paths
190 StaticLibs, LateStaticLibs, WholeStaticLibs common.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700191
192 ObjFiles common.Paths
193 WholeStaticLibObjFiles common.Paths
194
Dan Willemsenb40aab62016-04-20 14:21:14 -0700195 GeneratedSources common.Paths
196 GeneratedHeaders common.Paths
197
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198 Cflags, ReexportedCflags []string
199
200 CrtBegin, CrtEnd common.OptionalPath
201}
202
Colin Crossca860ac2016-01-04 14:34:37 -0800203type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700204 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
205 AsFlags []string // Flags that apply to assembly source files
206 CFlags []string // Flags that apply to C and C++ source files
207 ConlyFlags []string // Flags that apply to C source files
208 CppFlags []string // Flags that apply to C++ source files
209 YaccFlags []string // Flags that apply to Yacc source files
210 LdFlags []string // Flags that apply to linker command lines
211
212 Nocrt bool
213 Toolchain Toolchain
214 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800215
216 RequiredInstructionSet string
Colin Crossc472d572015-03-17 15:06:21 -0700217}
218
Colin Crossca860ac2016-01-04 14:34:37 -0800219type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700220 // 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 -0700221 Srcs []string `android:"arch_variant"`
222
223 // list of source files that should not be used to build the C/C++ module.
224 // This is most useful in the arch/multilib variants to remove non-common files
225 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700226
227 // list of module-specific flags that will be used for C and C++ compiles.
228 Cflags []string `android:"arch_variant"`
229
230 // list of module-specific flags that will be used for C++ compiles
231 Cppflags []string `android:"arch_variant"`
232
233 // list of module-specific flags that will be used for C compiles
234 Conlyflags []string `android:"arch_variant"`
235
236 // list of module-specific flags that will be used for .S compiles
237 Asflags []string `android:"arch_variant"`
238
Colin Crossca860ac2016-01-04 14:34:37 -0800239 // list of module-specific flags that will be used for C and C++ compiles when
240 // compiling with clang
241 Clang_cflags []string `android:"arch_variant"`
242
243 // list of module-specific flags that will be used for .S compiles when
244 // compiling with clang
245 Clang_asflags []string `android:"arch_variant"`
246
Colin Cross7d5136f2015-05-11 13:39:40 -0700247 // list of module-specific flags that will be used for .y and .yy compiles
248 Yaccflags []string
249
Colin Cross7d5136f2015-05-11 13:39:40 -0700250 // the instruction set architecture to use to compile the C/C++
251 // module.
252 Instruction_set string `android:"arch_variant"`
253
254 // list of directories relative to the root of the source tree that will
255 // be added to the include path using -I.
256 // If possible, don't use this. If adding paths from the current directory use
257 // local_include_dirs, if adding paths from other modules use export_include_dirs in
258 // that module.
259 Include_dirs []string `android:"arch_variant"`
260
Colin Cross39d97f22015-09-14 12:30:50 -0700261 // list of files relative to the root of the source tree that will be included
262 // using -include.
263 // If possible, don't use this.
264 Include_files []string `android:"arch_variant"`
265
Colin Cross7d5136f2015-05-11 13:39:40 -0700266 // list of directories relative to the Blueprints file that will
267 // be added to the include path using -I
268 Local_include_dirs []string `android:"arch_variant"`
269
Colin Cross39d97f22015-09-14 12:30:50 -0700270 // list of files relative to the Blueprints file that will be included
271 // using -include.
272 // If possible, don't use this.
273 Local_include_files []string `android:"arch_variant"`
274
Dan Willemsenb40aab62016-04-20 14:21:14 -0700275 // list of generated sources to compile. These are the names of gensrcs or
276 // genrule modules.
277 Generated_sources []string `android:"arch_variant"`
278
279 // list of generated headers to add to the include path. These are the names
280 // of genrule modules.
281 Generated_headers []string `android:"arch_variant"`
282
Colin Crossca860ac2016-01-04 14:34:37 -0800283 // pass -frtti instead of -fno-rtti
284 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700285
Colin Crossca860ac2016-01-04 14:34:37 -0800286 Debug, Release struct {
287 // list of module-specific flags that will be used for C and C++ compiles in debug or
288 // release builds
289 Cflags []string `android:"arch_variant"`
290 } `android:"arch_variant"`
291}
Colin Cross7d5136f2015-05-11 13:39:40 -0700292
Colin Crossca860ac2016-01-04 14:34:37 -0800293type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700294 // list of modules whose object files should be linked into this module
295 // in their entirety. For static library modules, all of the .o files from the intermediate
296 // directory of the dependency will be linked into this modules .a file. For a shared library,
297 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
298 Whole_static_libs []string `android:"arch_variant"`
299
300 // list of modules that should be statically linked into this module.
301 Static_libs []string `android:"arch_variant"`
302
303 // list of modules that should be dynamically linked into this module.
304 Shared_libs []string `android:"arch_variant"`
305
Colin Crossca860ac2016-01-04 14:34:37 -0800306 // list of module-specific flags that will be used for all link steps
307 Ldflags []string `android:"arch_variant"`
308
309 // don't insert default compiler flags into asflags, cflags,
310 // cppflags, conlyflags, ldflags, or include_dirs
311 No_default_compiler_flags *bool
312
313 // list of system libraries that will be dynamically linked to
314 // shared library and executable modules. If unset, generally defaults to libc
315 // and libm. Set to [] to prevent linking against libc and libm.
316 System_shared_libs []string
317
Colin Cross7d5136f2015-05-11 13:39:40 -0700318 // allow the module to contain undefined symbols. By default,
319 // modules cannot contain undefined symbols that are not satisified by their immediate
320 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
321 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700322 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700323
Dan Willemsend67be222015-09-16 15:19:33 -0700324 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700325 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700326
Colin Cross7d5136f2015-05-11 13:39:40 -0700327 // -l arguments to pass to linker for host-provided shared libraries
328 Host_ldlibs []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800329}
Colin Cross7d5136f2015-05-11 13:39:40 -0700330
Colin Crossca860ac2016-01-04 14:34:37 -0800331type LibraryCompilerProperties struct {
332 Static struct {
333 Srcs []string `android:"arch_variant"`
334 Exclude_srcs []string `android:"arch_variant"`
335 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700336 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800337 Shared struct {
338 Srcs []string `android:"arch_variant"`
339 Exclude_srcs []string `android:"arch_variant"`
340 Cflags []string `android:"arch_variant"`
341 } `android:"arch_variant"`
342}
343
Colin Cross919281a2016-04-05 16:42:05 -0700344type FlagExporterProperties struct {
345 // list of directories relative to the Blueprints file that will
346 // be added to the include path using -I for any module that links against this module
347 Export_include_dirs []string `android:"arch_variant"`
348}
349
Colin Crossca860ac2016-01-04 14:34:37 -0800350type LibraryLinkerProperties struct {
351 Static struct {
352 Whole_static_libs []string `android:"arch_variant"`
353 Static_libs []string `android:"arch_variant"`
354 Shared_libs []string `android:"arch_variant"`
355 } `android:"arch_variant"`
356 Shared struct {
357 Whole_static_libs []string `android:"arch_variant"`
358 Static_libs []string `android:"arch_variant"`
359 Shared_libs []string `android:"arch_variant"`
360 } `android:"arch_variant"`
361
362 // local file name to pass to the linker as --version_script
363 Version_script *string `android:"arch_variant"`
364 // local file name to pass to the linker as -unexported_symbols_list
365 Unexported_symbols_list *string `android:"arch_variant"`
366 // local file name to pass to the linker as -force_symbols_not_weak_list
367 Force_symbols_not_weak_list *string `android:"arch_variant"`
368 // local file name to pass to the linker as -force_symbols_weak_list
369 Force_symbols_weak_list *string `android:"arch_variant"`
370
Colin Crossca860ac2016-01-04 14:34:37 -0800371 // don't link in crt_begin and crt_end. This flag should only be necessary for
372 // compiling crt or libc.
373 Nocrt *bool `android:"arch_variant"`
374}
375
376type BinaryLinkerProperties struct {
377 // compile executable with -static
378 Static_executable *bool
379
380 // set the name of the output
381 Stem string `android:"arch_variant"`
382
383 // append to the name of the output
384 Suffix string `android:"arch_variant"`
385
386 // if set, add an extra objcopy --prefix-symbols= step
387 Prefix_symbols string
388}
389
390type TestLinkerProperties struct {
391 // if set, build against the gtest library. Defaults to true.
392 Gtest bool
393
394 // Create a separate binary for each source file. Useful when there is
395 // global state that can not be torn down and reset between each test suite.
396 Test_per_src *bool
397}
398
Colin Cross81413472016-04-11 14:37:39 -0700399type ObjectLinkerProperties struct {
400 // names of other cc_object modules to link into this module using partial linking
401 Objs []string `android:"arch_variant"`
402}
403
Colin Crossca860ac2016-01-04 14:34:37 -0800404// Properties used to compile all C or C++ modules
405type BaseProperties struct {
406 // compile module with clang instead of gcc
407 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700408
409 // Minimum sdk version supported when compiling against the ndk
410 Sdk_version string
411
Colin Crossca860ac2016-01-04 14:34:37 -0800412 // don't insert default compiler flags into asflags, cflags,
413 // cppflags, conlyflags, ldflags, or include_dirs
414 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700415
416 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800417}
418
419type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700420 // install to a subdirectory of the default install path for the module
421 Relative_install_path string
422}
423
Colin Crossca860ac2016-01-04 14:34:37 -0800424type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700425 Native_coverage *bool
426 Required []string
427 Strip string
428 Tags []string
429 Sanitize struct {
430 Never bool `android:"arch_variant"`
431 Address bool `android:"arch_variant"`
432 Thread bool `android:"arch_variant"`
433 Undefined bool `android:"arch_variant"`
434 All_undefined bool `android:"arch_variant"`
435 Misc_undefined []string `android:"arch_variant"`
436 Coverage bool `android:"arch_variant"`
437 Recover []string
438 } `android:"arch_variant"`
Colin Crosscfad1192015-11-02 16:43:11 -0800439}
440
Colin Crossca860ac2016-01-04 14:34:37 -0800441type ModuleContextIntf interface {
442 module() *Module
443 static() bool
444 staticBinary() bool
445 clang() bool
446 toolchain() Toolchain
447 noDefaultCompilerFlags() bool
448 sdk() bool
449 sdkVersion() string
450}
451
452type ModuleContext interface {
453 common.AndroidModuleContext
454 ModuleContextIntf
455}
456
457type BaseModuleContext interface {
458 common.AndroidBaseContext
459 ModuleContextIntf
460}
461
462type Customizer interface {
463 CustomizeProperties(BaseModuleContext)
464 Properties() []interface{}
465}
466
467type feature interface {
468 begin(ctx BaseModuleContext)
469 deps(ctx BaseModuleContext, deps Deps) Deps
470 flags(ctx ModuleContext, flags Flags) Flags
471 props() []interface{}
472}
473
474type compiler interface {
475 feature
Dan Willemsenb40aab62016-04-20 14:21:14 -0700476 compile(ctx ModuleContext, flags Flags, deps PathDeps) common.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800477}
478
479type linker interface {
480 feature
481 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles common.Paths) common.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700482 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800483}
484
485type installer interface {
486 props() []interface{}
487 install(ctx ModuleContext, path common.Path)
488 inData() bool
489}
490
Colin Crossc99deeb2016-04-11 15:06:20 -0700491type dependencyTag struct {
492 blueprint.BaseDependencyTag
493 name string
494 library bool
495}
496
497var (
498 sharedDepTag = dependencyTag{name: "shared", library: true}
499 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
500 staticDepTag = dependencyTag{name: "static", library: true}
501 lateStaticDepTag = dependencyTag{name: "late static", library: true}
502 wholeStaticDepTag = dependencyTag{name: "whole static", library: true}
Dan Willemsenb40aab62016-04-20 14:21:14 -0700503 genSourceDepTag = dependencyTag{name: "gen source"}
504 genHeaderDepTag = dependencyTag{name: "gen header"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700505 objDepTag = dependencyTag{name: "obj"}
506 crtBeginDepTag = dependencyTag{name: "crtbegin"}
507 crtEndDepTag = dependencyTag{name: "crtend"}
508 reuseObjTag = dependencyTag{name: "reuse objects"}
509)
510
Colin Crossca860ac2016-01-04 14:34:37 -0800511// Module contains the properties and members used by all C/C++ module types, and implements
512// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
513// to construct the output file. Behavior can be customized with a Customizer interface
514type Module struct {
Colin Crossc472d572015-03-17 15:06:21 -0700515 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800516 common.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700517
Colin Crossca860ac2016-01-04 14:34:37 -0800518 Properties BaseProperties
519 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700520
Colin Crossca860ac2016-01-04 14:34:37 -0800521 // initialize before calling Init
522 hod common.HostOrDeviceSupported
523 multilib common.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700524
Colin Crossca860ac2016-01-04 14:34:37 -0800525 // delegates, initialize before calling Init
526 customizer Customizer
527 features []feature
528 compiler compiler
529 linker linker
530 installer installer
Colin Cross74d1ec02015-04-28 13:30:13 -0700531
Colin Crossca860ac2016-01-04 14:34:37 -0800532 outputFile common.OptionalPath
533
534 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700535}
536
Colin Crossca860ac2016-01-04 14:34:37 -0800537func (c *Module) Init() (blueprint.Module, []interface{}) {
538 props := []interface{}{&c.Properties, &c.unused}
539 if c.customizer != nil {
540 props = append(props, c.customizer.Properties()...)
541 }
542 if c.compiler != nil {
543 props = append(props, c.compiler.props()...)
544 }
545 if c.linker != nil {
546 props = append(props, c.linker.props()...)
547 }
548 if c.installer != nil {
549 props = append(props, c.installer.props()...)
550 }
551 for _, feature := range c.features {
552 props = append(props, feature.props()...)
553 }
Colin Crossc472d572015-03-17 15:06:21 -0700554
Colin Crossca860ac2016-01-04 14:34:37 -0800555 _, props = common.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700556
Colin Crossca860ac2016-01-04 14:34:37 -0800557 return common.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700558}
559
Colin Crossca860ac2016-01-04 14:34:37 -0800560type baseModuleContext struct {
561 common.AndroidBaseContext
562 moduleContextImpl
563}
564
565type moduleContext struct {
566 common.AndroidModuleContext
567 moduleContextImpl
568}
569
570type moduleContextImpl struct {
571 mod *Module
572 ctx BaseModuleContext
573}
574
575func (ctx *moduleContextImpl) module() *Module {
576 return ctx.mod
577}
578
579func (ctx *moduleContextImpl) clang() bool {
580 return ctx.mod.clang(ctx.ctx)
581}
582
583func (ctx *moduleContextImpl) toolchain() Toolchain {
584 return ctx.mod.toolchain(ctx.ctx)
585}
586
587func (ctx *moduleContextImpl) static() bool {
588 if ctx.mod.linker == nil {
589 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
590 }
591 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
592 return linker.static()
593 } else {
594 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
595 }
596}
597
598func (ctx *moduleContextImpl) staticBinary() bool {
599 if ctx.mod.linker == nil {
600 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
601 }
602 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
603 return linker.staticBinary()
604 } else {
605 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
606 }
607}
608
609func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
610 return Bool(ctx.mod.Properties.No_default_compiler_flags)
611}
612
613func (ctx *moduleContextImpl) sdk() bool {
614 return ctx.mod.Properties.Sdk_version != ""
615}
616
617func (ctx *moduleContextImpl) sdkVersion() string {
618 return ctx.mod.Properties.Sdk_version
619}
620
621func newBaseModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
622 return &Module{
623 hod: hod,
624 multilib: multilib,
625 }
626}
627
628func newModule(hod common.HostOrDeviceSupported, multilib common.Multilib) *Module {
629 module := newBaseModule(hod, multilib)
630 module.features = []feature{
631 &stlFeature{},
632 }
633 return module
634}
635
636func (c *Module) GenerateAndroidBuildActions(actx common.AndroidModuleContext) {
637 ctx := &moduleContext{
638 AndroidModuleContext: actx,
639 moduleContextImpl: moduleContextImpl{
640 mod: c,
641 },
642 }
643 ctx.ctx = ctx
644
645 flags := Flags{
646 Toolchain: c.toolchain(ctx),
647 Clang: c.clang(ctx),
648 }
649
650 if c.compiler != nil {
651 flags = c.compiler.flags(ctx, flags)
652 }
653 if c.linker != nil {
654 flags = c.linker.flags(ctx, flags)
655 }
656 for _, feature := range c.features {
657 flags = feature.flags(ctx, flags)
658 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800659 if ctx.Failed() {
660 return
661 }
662
Colin Crossca860ac2016-01-04 14:34:37 -0800663 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
664 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
665 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800666
Colin Crossca860ac2016-01-04 14:34:37 -0800667 // Optimization to reduce size of build.ninja
668 // Replace the long list of flags for each file with a module-local variable
669 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
670 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
671 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
672 flags.CFlags = []string{"$cflags"}
673 flags.CppFlags = []string{"$cppflags"}
674 flags.AsFlags = []string{"$asflags"}
675
Colin Crossc99deeb2016-04-11 15:06:20 -0700676 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800677 if ctx.Failed() {
678 return
679 }
680
Colin Cross28344522015-04-22 13:07:53 -0700681 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700682
Colin Crossca860ac2016-01-04 14:34:37 -0800683 var objFiles common.Paths
684 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700685 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800686 if ctx.Failed() {
687 return
688 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800689 }
690
Colin Crossca860ac2016-01-04 14:34:37 -0800691 if c.linker != nil {
692 outputFile := c.linker.link(ctx, flags, deps, objFiles)
693 if ctx.Failed() {
694 return
695 }
696 c.outputFile = common.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700697
Colin Crossc99deeb2016-04-11 15:06:20 -0700698 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800699 c.installer.install(ctx, outputFile)
700 if ctx.Failed() {
701 return
702 }
703 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700704 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800705}
706
Colin Crossca860ac2016-01-04 14:34:37 -0800707func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
708 if c.cachedToolchain == nil {
709 arch := ctx.Arch()
710 hod := ctx.HostOrDevice()
711 ht := ctx.HostType()
712 factory := toolchainFactories[hod][ht][arch.ArchType]
713 if factory == nil {
714 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
715 return nil
716 }
717 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800718 }
Colin Crossca860ac2016-01-04 14:34:37 -0800719 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800720}
721
Colin Crossca860ac2016-01-04 14:34:37 -0800722func (c *Module) begin(ctx BaseModuleContext) {
723 if c.compiler != nil {
724 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700725 }
Colin Crossca860ac2016-01-04 14:34:37 -0800726 if c.linker != nil {
727 c.linker.begin(ctx)
728 }
729 for _, feature := range c.features {
730 feature.begin(ctx)
731 }
732}
733
Colin Crossc99deeb2016-04-11 15:06:20 -0700734func (c *Module) deps(ctx BaseModuleContext) Deps {
735 deps := Deps{}
736
737 if c.compiler != nil {
738 deps = c.compiler.deps(ctx, deps)
739 }
740 if c.linker != nil {
741 deps = c.linker.deps(ctx, deps)
742 }
743 for _, feature := range c.features {
744 deps = feature.deps(ctx, deps)
745 }
746
747 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
748 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
749 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
750 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
751 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
752
753 return deps
754}
755
Colin Crossca860ac2016-01-04 14:34:37 -0800756func (c *Module) depsMutator(actx common.AndroidBottomUpMutatorContext) {
757 ctx := &baseModuleContext{
758 AndroidBaseContext: actx,
759 moduleContextImpl: moduleContextImpl{
760 mod: c,
761 },
762 }
763 ctx.ctx = ctx
764
765 if c.customizer != nil {
766 c.customizer.CustomizeProperties(ctx)
767 }
768
769 c.begin(ctx)
770
Colin Crossc99deeb2016-04-11 15:06:20 -0700771 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800772
Colin Crossc99deeb2016-04-11 15:06:20 -0700773 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
774
775 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
776 deps.WholeStaticLibs...)
777
778 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticDepTag,
779 deps.StaticLibs...)
780
781 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
782 deps.LateStaticLibs...)
783
784 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedDepTag,
785 deps.SharedLibs...)
786
787 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
788 deps.LateSharedLibs...)
789
Dan Willemsenb40aab62016-04-20 14:21:14 -0700790 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
791 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
792
Colin Crossc99deeb2016-04-11 15:06:20 -0700793 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
794
795 if deps.CrtBegin != "" {
796 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800797 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700798 if deps.CrtEnd != "" {
799 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700800 }
Colin Cross6362e272015-10-29 15:25:03 -0700801}
Colin Cross21b9a242015-03-24 14:15:58 -0700802
Colin Cross6362e272015-10-29 15:25:03 -0700803func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800804 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700805 c.depsMutator(ctx)
806 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800807}
808
Colin Crossca860ac2016-01-04 14:34:37 -0800809func (c *Module) clang(ctx BaseModuleContext) bool {
810 clang := Bool(c.Properties.Clang)
811
812 if c.Properties.Clang == nil {
813 if ctx.Host() {
814 clang = true
815 }
816
817 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
818 clang = true
819 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800820 }
Colin Cross28344522015-04-22 13:07:53 -0700821
Colin Crossca860ac2016-01-04 14:34:37 -0800822 if !c.toolchain(ctx).ClangSupported() {
823 clang = false
824 }
825
826 return clang
827}
828
Colin Crossc99deeb2016-04-11 15:06:20 -0700829// Convert dependencies to paths. Returns a PathDeps containing paths
830func (c *Module) depsToPaths(ctx common.AndroidModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800831 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800832
Colin Crossc99deeb2016-04-11 15:06:20 -0700833 ctx.VisitDirectDeps(func(m blueprint.Module) {
834 name := ctx.OtherModuleName(m)
835 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800836
Colin Crossc99deeb2016-04-11 15:06:20 -0700837 a, _ := m.(common.AndroidModule)
838 if a == nil {
839 ctx.ModuleErrorf("module %q not an android module", name)
840 return
Colin Crossca860ac2016-01-04 14:34:37 -0800841 }
Colin Crossca860ac2016-01-04 14:34:37 -0800842
Colin Crossc99deeb2016-04-11 15:06:20 -0700843 c, _ := m.(*Module)
844 if c == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700845 switch tag {
846 case common.DefaultsDepTag:
847 case genSourceDepTag:
848 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
849 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
850 genRule.GeneratedSourceFiles()...)
851 } else {
852 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
853 }
854 case genHeaderDepTag:
855 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
856 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
857 genRule.GeneratedSourceFiles()...)
858 depPaths.Cflags = append(depPaths.Cflags,
859 includeDirsToFlags(common.Paths{genRule.GeneratedHeaderDir()}))
860 } else {
861 ctx.ModuleErrorf("module %q is not a genrule", name)
862 }
863 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700864 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800865 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700866 return
867 }
868
869 if !a.Enabled() {
870 ctx.ModuleErrorf("depends on disabled module %q", name)
871 return
872 }
873
874 if a.HostOrDevice() != ctx.HostOrDevice() {
875 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), name)
876 return
877 }
878
879 if !c.outputFile.Valid() {
880 ctx.ModuleErrorf("module %q missing output file", name)
881 return
882 }
883
884 if tag == reuseObjTag {
885 depPaths.ObjFiles = append(depPaths.ObjFiles,
886 c.compiler.(*libraryCompiler).reuseObjFiles...)
887 return
888 }
889
890 var cflags []string
891 if t, _ := tag.(dependencyTag); t.library {
892 if i, ok := c.linker.(exportedFlagsProducer); ok {
893 cflags = i.exportedFlags()
894 depPaths.Cflags = append(depPaths.Cflags, cflags...)
895 }
896 }
897
898 var depPtr *common.Paths
899
900 switch tag {
901 case sharedDepTag:
902 depPtr = &depPaths.SharedLibs
903 case lateSharedDepTag:
904 depPtr = &depPaths.LateSharedLibs
905 case staticDepTag:
906 depPtr = &depPaths.StaticLibs
907 case lateStaticDepTag:
908 depPtr = &depPaths.LateStaticLibs
909 case wholeStaticDepTag:
910 depPtr = &depPaths.WholeStaticLibs
911 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
912 staticLib, _ := c.linker.(*libraryLinker)
913 if staticLib == nil || !staticLib.static() {
914 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
915 return
916 }
917
918 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
919 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
920 for i := range missingDeps {
921 missingDeps[i] += postfix
922 }
923 ctx.AddMissingDependencies(missingDeps)
924 }
925 depPaths.WholeStaticLibObjFiles =
926 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
927 case objDepTag:
928 depPtr = &depPaths.ObjFiles
929 case crtBeginDepTag:
930 depPaths.CrtBegin = c.outputFile
931 case crtEndDepTag:
932 depPaths.CrtEnd = c.outputFile
933 default:
934 panic(fmt.Errorf("unknown dependency tag: %s", ctx.OtherModuleDependencyTag(m)))
935 }
936
937 if depPtr != nil {
938 *depPtr = append(*depPtr, c.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800939 }
940 })
941
942 return depPaths
943}
944
945func (c *Module) InstallInData() bool {
946 if c.installer == nil {
947 return false
948 }
949 return c.installer.inData()
950}
951
952// Compiler
953
954type baseCompiler struct {
955 Properties BaseCompilerProperties
956}
957
958var _ compiler = (*baseCompiler)(nil)
959
960func (compiler *baseCompiler) props() []interface{} {
961 return []interface{}{&compiler.Properties}
962}
963
Dan Willemsenb40aab62016-04-20 14:21:14 -0700964func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
965
966func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
967 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
968 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
969
970 return deps
971}
Colin Crossca860ac2016-01-04 14:34:37 -0800972
973// Create a Flags struct that collects the compile flags from global values,
974// per-target values, module type values, and per-module Blueprints properties
975func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
976 toolchain := ctx.toolchain()
977
978 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
979 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
980 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
981 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
982 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
983
Colin Cross28344522015-04-22 13:07:53 -0700984 // Include dir cflags
Colin Crossca860ac2016-01-04 14:34:37 -0800985 rootIncludeDirs := common.PathsForSource(ctx, compiler.Properties.Include_dirs)
986 localIncludeDirs := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700987 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700988 includeDirsToFlags(localIncludeDirs),
989 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700990
Colin Crossca860ac2016-01-04 14:34:37 -0800991 rootIncludeFiles := common.PathsForSource(ctx, compiler.Properties.Include_files)
992 localIncludeFiles := common.PathsForModuleSrc(ctx, compiler.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700993
994 flags.GlobalFlags = append(flags.GlobalFlags,
995 includeFilesToFlags(rootIncludeFiles),
996 includeFilesToFlags(localIncludeFiles))
997
Colin Crossca860ac2016-01-04 14:34:37 -0800998 if !ctx.noDefaultCompilerFlags() {
999 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001000 flags.GlobalFlags = append(flags.GlobalFlags,
1001 "${commonGlobalIncludes}",
1002 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001003 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001004 }
1005
1006 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001007 "-I" + common.PathForModuleSrc(ctx).String(),
1008 "-I" + common.PathForModuleOut(ctx).String(),
1009 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001010 }...)
1011 }
1012
Colin Crossca860ac2016-01-04 14:34:37 -08001013 instructionSet := compiler.Properties.Instruction_set
1014 if flags.RequiredInstructionSet != "" {
1015 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001016 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001017 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1018 if flags.Clang {
1019 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1020 }
1021 if err != nil {
1022 ctx.ModuleErrorf("%s", err)
1023 }
1024
1025 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001026 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001027
Colin Cross97ba0732015-03-23 17:50:24 -07001028 if flags.Clang {
1029 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001030 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1031 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001032 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1033 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1034 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001035
1036 target := "-target " + toolchain.ClangTriple()
1037 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1038
Colin Cross97ba0732015-03-23 17:50:24 -07001039 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1040 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1041 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001042 }
1043
Colin Crossca860ac2016-01-04 14:34:37 -08001044 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001045 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1046
Colin Cross97ba0732015-03-23 17:50:24 -07001047 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001048 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001049 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001050 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001051 toolchain.ClangCflags(),
1052 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001053 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001054
1055 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001056 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001057 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001058 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001059 toolchain.Cflags(),
1060 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001061 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001062 }
1063
Colin Cross7b66f152015-12-15 16:07:43 -08001064 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1065 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1066 }
1067
Colin Crossf6566ed2015-03-24 11:13:38 -07001068 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001069 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001070 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001071 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001072 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001073 }
1074 }
1075
Colin Cross97ba0732015-03-23 17:50:24 -07001076 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001077
Colin Cross97ba0732015-03-23 17:50:24 -07001078 if flags.Clang {
1079 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001080 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001081 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001082 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001083 }
1084
Colin Crossc4bde762015-11-23 16:11:30 -08001085 if flags.Clang {
1086 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1087 } else {
1088 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001089 }
1090
Colin Crossca860ac2016-01-04 14:34:37 -08001091 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001092 if ctx.Host() && !flags.Clang {
1093 // The host GCC doesn't support C++14 (and is deprecated, so likely
1094 // never will). Build these modules with C++11.
1095 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1096 } else {
1097 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1098 }
1099 }
1100
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001101 // We can enforce some rules more strictly in the code we own. strict
1102 // indicates if this is code that we can be stricter with. If we have
1103 // rules that we want to apply to *our* code (but maybe can't for
1104 // vendor/device specific things), we could extend this to be a ternary
1105 // value.
1106 strict := true
1107 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
1108 strict = false
1109 }
1110
1111 // Can be used to make some annotations stricter for code we can fix
1112 // (such as when we mark functions as deprecated).
1113 if strict {
1114 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1115 }
1116
Colin Cross3f40fa42015-01-30 17:27:36 -08001117 return flags
1118}
1119
Dan Willemsenb40aab62016-04-20 14:21:14 -07001120func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) common.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001121 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001122 objFiles := compiler.compileObjs(ctx, flags, "",
1123 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1124 deps.GeneratedSources, deps.GeneratedHeaders)
1125
Colin Crossca860ac2016-01-04 14:34:37 -08001126 if ctx.Failed() {
1127 return nil
1128 }
1129
Colin Crossca860ac2016-01-04 14:34:37 -08001130 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001131}
1132
1133// Compile a list of source files into objects a specified subdirectory
Colin Crossca860ac2016-01-04 14:34:37 -08001134func (compiler *baseCompiler) compileObjs(ctx common.AndroidModuleContext, flags Flags,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001135 subdir string, srcFiles, excludes []string, extraSrcs, deps common.Paths) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001136
Colin Crossca860ac2016-01-04 14:34:37 -08001137 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001138
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001139 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001140 inputFiles = append(inputFiles, extraSrcs...)
1141 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1142
1143 deps = append(deps, gendeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001144
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001145 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001146}
1147
Colin Crossca860ac2016-01-04 14:34:37 -08001148// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1149type baseLinker struct {
1150 Properties BaseLinkerProperties
1151 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001152 VariantIsShared bool `blueprint:"mutated"`
1153 VariantIsStatic bool `blueprint:"mutated"`
1154 VariantIsStaticBinary bool `blueprint:"mutated"`
1155 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001156 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001157}
1158
Dan Willemsend30e6102016-03-30 17:35:50 -07001159func (linker *baseLinker) begin(ctx BaseModuleContext) {
1160 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001161 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001162 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001163 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001164 }
1165}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001166
Colin Crossca860ac2016-01-04 14:34:37 -08001167func (linker *baseLinker) props() []interface{} {
1168 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001169}
1170
Colin Crossca860ac2016-01-04 14:34:37 -08001171func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1172 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1173 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1174 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001175
Colin Cross74d1ec02015-04-28 13:30:13 -07001176 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001177 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001178 }
1179
Colin Crossf6566ed2015-03-24 11:13:38 -07001180 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001181 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001182 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1183 if !Bool(linker.Properties.No_libgcc) {
1184 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001185 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001186
Colin Crossca860ac2016-01-04 14:34:37 -08001187 if !linker.static() {
1188 if linker.Properties.System_shared_libs != nil {
1189 deps.LateSharedLibs = append(deps.LateSharedLibs,
1190 linker.Properties.System_shared_libs...)
1191 } else if !ctx.sdk() {
1192 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1193 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001194 }
Colin Cross577f6e42015-03-27 18:23:34 -07001195
Colin Crossca860ac2016-01-04 14:34:37 -08001196 if ctx.sdk() {
1197 version := ctx.sdkVersion()
1198 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001199 "ndk_libc."+version,
1200 "ndk_libm."+version,
1201 )
1202 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001203 }
1204
Colin Crossca860ac2016-01-04 14:34:37 -08001205 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001206}
1207
Colin Crossca860ac2016-01-04 14:34:37 -08001208func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1209 toolchain := ctx.toolchain()
1210
1211 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1212
1213 if !ctx.noDefaultCompilerFlags() {
1214 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1215 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1216 }
1217
1218 if flags.Clang {
1219 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1220 } else {
1221 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1222 }
1223
1224 if ctx.Host() {
1225 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1226 }
1227 }
1228
Dan Willemsend30e6102016-03-30 17:35:50 -07001229 if ctx.Host() && !linker.static() {
1230 rpath_prefix := `\$$ORIGIN/`
1231 if ctx.Darwin() {
1232 rpath_prefix = "@loader_path/"
1233 }
1234
Colin Crossc99deeb2016-04-11 15:06:20 -07001235 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001236 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1237 }
1238 }
1239
Dan Willemsene7174922016-03-30 17:33:52 -07001240 if flags.Clang {
1241 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1242 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001243 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1244 }
1245
1246 return flags
1247}
1248
1249func (linker *baseLinker) static() bool {
1250 return linker.dynamicProperties.VariantIsStatic
1251}
1252
1253func (linker *baseLinker) staticBinary() bool {
1254 return linker.dynamicProperties.VariantIsStaticBinary
1255}
1256
1257func (linker *baseLinker) setStatic(static bool) {
1258 linker.dynamicProperties.VariantIsStatic = static
1259}
1260
1261type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001262 // Returns true if the build options for the module have selected a static or shared build
1263 buildStatic() bool
1264 buildShared() bool
1265
1266 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001267 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001268
Colin Cross18b6dc52015-04-28 13:20:37 -07001269 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001270 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001271
1272 // Returns whether a module is a static binary
1273 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001274}
1275
Colin Crossca860ac2016-01-04 14:34:37 -08001276type baseInstaller struct {
1277 Properties InstallerProperties
1278
1279 dir string
1280 dir64 string
1281 data bool
1282
Colin Crossa2344662016-03-24 13:14:12 -07001283 path common.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001284}
1285
1286var _ installer = (*baseInstaller)(nil)
1287
1288func (installer *baseInstaller) props() []interface{} {
1289 return []interface{}{&installer.Properties}
1290}
1291
1292func (installer *baseInstaller) install(ctx ModuleContext, file common.Path) {
1293 subDir := installer.dir
1294 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1295 subDir = installer.dir64
1296 }
1297 dir := common.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
1298 installer.path = ctx.InstallFile(dir, file)
1299}
1300
1301func (installer *baseInstaller) inData() bool {
1302 return installer.data
1303}
1304
Colin Cross3f40fa42015-01-30 17:27:36 -08001305//
1306// Combined static+shared libraries
1307//
1308
Colin Cross919281a2016-04-05 16:42:05 -07001309type flagExporter struct {
1310 Properties FlagExporterProperties
1311
1312 flags []string
1313}
1314
1315func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
1316 includeDirs := common.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1317 f.flags = append(f.flags, common.JoinWithPrefix(includeDirs.Strings(), inc))
1318}
1319
1320func (f *flagExporter) reexportFlags(flags []string) {
1321 f.flags = append(f.flags, flags...)
1322}
1323
1324func (f *flagExporter) exportedFlags() []string {
1325 return f.flags
1326}
1327
1328type exportedFlagsProducer interface {
1329 exportedFlags() []string
1330}
1331
1332var _ exportedFlagsProducer = (*flagExporter)(nil)
1333
Colin Crossca860ac2016-01-04 14:34:37 -08001334type libraryCompiler struct {
1335 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001336
Colin Crossca860ac2016-01-04 14:34:37 -08001337 linker *libraryLinker
1338 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001339
Colin Crossca860ac2016-01-04 14:34:37 -08001340 // For reusing static library objects for shared library
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001341 reuseObjFiles common.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001342}
1343
Colin Crossca860ac2016-01-04 14:34:37 -08001344var _ compiler = (*libraryCompiler)(nil)
1345
1346func (library *libraryCompiler) props() []interface{} {
1347 props := library.baseCompiler.props()
1348 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001349}
1350
Colin Crossca860ac2016-01-04 14:34:37 -08001351func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1352 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001353
Dan Willemsen490fd492015-11-24 17:53:15 -08001354 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1355 // all code is position independent, and then those warnings get promoted to
1356 // errors.
1357 if ctx.HostType() != common.Windows {
1358 flags.CFlags = append(flags.CFlags, "-fPIC")
1359 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001360
Colin Crossca860ac2016-01-04 14:34:37 -08001361 if library.linker.static() {
1362 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001363 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001364 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001365 }
1366
Colin Crossca860ac2016-01-04 14:34:37 -08001367 return flags
1368}
1369
Dan Willemsenb40aab62016-04-20 14:21:14 -07001370func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) common.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001371 var objFiles common.Paths
1372
Dan Willemsenb40aab62016-04-20 14:21:14 -07001373 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001374 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001375
1376 if library.linker.static() {
1377 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001378 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1379 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001380 } else {
1381 objFiles = append(objFiles, library.compileObjs(ctx, flags, common.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001382 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1383 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001384 }
1385
1386 return objFiles
1387}
1388
1389type libraryLinker struct {
1390 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001391 flagExporter
Colin Crossca860ac2016-01-04 14:34:37 -08001392
1393 Properties LibraryLinkerProperties
1394
1395 dynamicProperties struct {
1396 BuildStatic bool `blueprint:"mutated"`
1397 BuildShared bool `blueprint:"mutated"`
1398 }
1399
Colin Crossca860ac2016-01-04 14:34:37 -08001400 // If we're used as a whole_static_lib, our missing dependencies need
1401 // to be given
1402 wholeStaticMissingDeps []string
1403
1404 // For whole_static_libs
1405 objFiles common.Paths
1406}
1407
1408var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001409
1410func (library *libraryLinker) props() []interface{} {
1411 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001412 return append(props,
1413 &library.Properties,
1414 &library.dynamicProperties,
1415 &library.flagExporter.Properties)
Colin Crossca860ac2016-01-04 14:34:37 -08001416}
1417
1418func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1419 flags = library.baseLinker.flags(ctx, flags)
1420
1421 flags.Nocrt = Bool(library.Properties.Nocrt)
1422
1423 if !library.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001424 libName := ctx.ModuleName()
1425 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1426 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001427 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001428 sharedFlag = "-shared"
1429 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001430 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001431 flags.LdFlags = append(flags.LdFlags,
1432 "-nostdlib",
1433 "-Wl,--gc-sections",
1434 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001435 }
Colin Cross97ba0732015-03-23 17:50:24 -07001436
Colin Cross0af4b842015-04-30 16:36:18 -07001437 if ctx.Darwin() {
1438 flags.LdFlags = append(flags.LdFlags,
1439 "-dynamiclib",
1440 "-single_module",
1441 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001442 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001443 )
1444 } else {
1445 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001446 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001447 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001448 )
1449 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001450 }
Colin Cross97ba0732015-03-23 17:50:24 -07001451
1452 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001453}
1454
Colin Crossca860ac2016-01-04 14:34:37 -08001455func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1456 deps = library.baseLinker.deps(ctx, deps)
1457 if library.static() {
1458 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1459 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1460 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1461 } else {
1462 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1463 if !ctx.sdk() {
1464 deps.CrtBegin = "crtbegin_so"
1465 deps.CrtEnd = "crtend_so"
1466 } else {
1467 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1468 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1469 }
1470 }
1471 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1472 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1473 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1474 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001475
Colin Crossca860ac2016-01-04 14:34:37 -08001476 return deps
1477}
Colin Cross3f40fa42015-01-30 17:27:36 -08001478
Colin Crossca860ac2016-01-04 14:34:37 -08001479func (library *libraryLinker) linkStatic(ctx ModuleContext,
1480 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
1481
Colin Cross21b9a242015-03-24 14:15:58 -07001482 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Crossca860ac2016-01-04 14:34:37 -08001483 library.objFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001484
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001485 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001486
Colin Cross0af4b842015-04-30 16:36:18 -07001487 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001488 TransformDarwinObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001489 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001490 TransformObjToStaticLib(ctx, objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001491 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001492
Colin Crossca860ac2016-01-04 14:34:37 -08001493 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001494
1495 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001496
1497 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001498}
1499
Colin Crossca860ac2016-01-04 14:34:37 -08001500func (library *libraryLinker) linkShared(ctx ModuleContext,
1501 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001502
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001503 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001504
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001505 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001506
Colin Crossca860ac2016-01-04 14:34:37 -08001507 versionScript := common.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1508 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1509 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1510 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001511 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001512 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001513 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001514 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001515 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001516 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001517 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1518 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001519 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001520 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1521 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001522 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001523 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1524 }
1525 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001526 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001527 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1528 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001529 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001530 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001531 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001532 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001533 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001534 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001535 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001536 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001537 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001538 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001539 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001540 }
Colin Crossaee540a2015-07-06 17:48:31 -07001541 }
1542
Colin Crossca860ac2016-01-04 14:34:37 -08001543 sharedLibs := deps.SharedLibs
1544 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001545
Colin Crossca860ac2016-01-04 14:34:37 -08001546 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1547 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
1548 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, flagsToBuilderFlags(flags), outputFile)
1549
1550 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001551}
1552
Colin Crossca860ac2016-01-04 14:34:37 -08001553func (library *libraryLinker) link(ctx ModuleContext,
1554 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001555
Colin Crossc99deeb2016-04-11 15:06:20 -07001556 objFiles = append(objFiles, deps.ObjFiles...)
1557
Colin Crossca860ac2016-01-04 14:34:37 -08001558 var out common.Path
1559 if library.static() {
1560 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001561 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001562 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001563 }
1564
Colin Cross919281a2016-04-05 16:42:05 -07001565 library.exportIncludes(ctx, "-I")
1566 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001567
1568 return out
1569}
1570
1571func (library *libraryLinker) buildStatic() bool {
1572 return library.dynamicProperties.BuildStatic
1573}
1574
1575func (library *libraryLinker) buildShared() bool {
1576 return library.dynamicProperties.BuildShared
1577}
1578
1579func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1580 return library.wholeStaticMissingDeps
1581}
1582
Colin Crossc99deeb2016-04-11 15:06:20 -07001583func (library *libraryLinker) installable() bool {
1584 return !library.static()
1585}
1586
Colin Crossca860ac2016-01-04 14:34:37 -08001587type libraryInstaller struct {
1588 baseInstaller
1589
1590 linker *libraryLinker
1591}
1592
1593func (library *libraryInstaller) install(ctx ModuleContext, file common.Path) {
1594 if !library.linker.static() {
1595 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001596 }
1597}
1598
Colin Crossca860ac2016-01-04 14:34:37 -08001599func NewLibrary(hod common.HostOrDeviceSupported, shared, static bool) *Module {
1600 module := newModule(hod, common.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001601
Colin Crossca860ac2016-01-04 14:34:37 -08001602 linker := &libraryLinker{}
1603 linker.dynamicProperties.BuildShared = shared
1604 linker.dynamicProperties.BuildStatic = static
1605 module.linker = linker
1606
1607 module.compiler = &libraryCompiler{
1608 linker: linker,
1609 }
1610 module.installer = &libraryInstaller{
1611 baseInstaller: baseInstaller{
1612 dir: "lib",
1613 dir64: "lib64",
1614 },
1615 linker: linker,
Dan Albertc403f7c2015-03-18 14:01:18 -07001616 }
1617
Colin Crossca860ac2016-01-04 14:34:37 -08001618 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001619}
1620
Colin Crossca860ac2016-01-04 14:34:37 -08001621func libraryFactory() (blueprint.Module, []interface{}) {
1622 module := NewLibrary(common.HostAndDeviceSupported, true, true)
1623 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001624}
1625
Colin Cross3f40fa42015-01-30 17:27:36 -08001626//
1627// Objects (for crt*.o)
1628//
1629
Colin Crossca860ac2016-01-04 14:34:37 -08001630type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001631 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001632}
1633
Colin Crossca860ac2016-01-04 14:34:37 -08001634func objectFactory() (blueprint.Module, []interface{}) {
1635 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
1636 module.compiler = &baseCompiler{}
1637 module.linker = &objectLinker{}
1638 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001639}
1640
Colin Cross81413472016-04-11 14:37:39 -07001641func (object *objectLinker) props() []interface{} {
1642 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001643}
1644
Colin Crossca860ac2016-01-04 14:34:37 -08001645func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001646
Colin Cross81413472016-04-11 14:37:39 -07001647func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1648 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001649 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001650}
1651
Colin Crossca860ac2016-01-04 14:34:37 -08001652func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001653 if flags.Clang {
1654 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1655 } else {
1656 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1657 }
1658
Colin Crossca860ac2016-01-04 14:34:37 -08001659 return flags
1660}
1661
1662func (object *objectLinker) link(ctx ModuleContext,
1663 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001664
Colin Cross97ba0732015-03-23 17:50:24 -07001665 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001666
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001667 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001668 if len(objFiles) == 1 {
1669 outputFile = objFiles[0]
1670 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001671 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001672 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001673 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001674 }
1675
Colin Cross3f40fa42015-01-30 17:27:36 -08001676 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001677 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001678}
1679
Colin Crossc99deeb2016-04-11 15:06:20 -07001680func (*objectLinker) installable() bool {
1681 return false
1682}
1683
Colin Cross3f40fa42015-01-30 17:27:36 -08001684//
1685// Executables
1686//
1687
Colin Crossca860ac2016-01-04 14:34:37 -08001688type binaryLinker struct {
1689 baseLinker
Colin Cross7d5136f2015-05-11 13:39:40 -07001690
Colin Crossca860ac2016-01-04 14:34:37 -08001691 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001692
Colin Crossca860ac2016-01-04 14:34:37 -08001693 hostToolPath common.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001694}
1695
Colin Crossca860ac2016-01-04 14:34:37 -08001696var _ linker = (*binaryLinker)(nil)
1697
1698func (binary *binaryLinker) props() []interface{} {
1699 return append(binary.baseLinker.props(), &binary.Properties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001700}
1701
Colin Crossca860ac2016-01-04 14:34:37 -08001702func (binary *binaryLinker) buildStatic() bool {
1703 return Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001704}
1705
Colin Crossca860ac2016-01-04 14:34:37 -08001706func (binary *binaryLinker) buildShared() bool {
1707 return !Bool(binary.Properties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001708}
1709
Colin Crossca860ac2016-01-04 14:34:37 -08001710func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001711 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001712 if binary.Properties.Stem != "" {
1713 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001714 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001715
Colin Crossca860ac2016-01-04 14:34:37 -08001716 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001717}
1718
Colin Crossca860ac2016-01-04 14:34:37 -08001719func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1720 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001721 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001722 if !ctx.sdk() {
1723 if Bool(binary.Properties.Static_executable) {
1724 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001725 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001726 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001727 }
Colin Crossca860ac2016-01-04 14:34:37 -08001728 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001729 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001730 if Bool(binary.Properties.Static_executable) {
1731 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001732 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001733 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001734 }
Colin Crossca860ac2016-01-04 14:34:37 -08001735 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001736 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001737
Colin Crossca860ac2016-01-04 14:34:37 -08001738 if Bool(binary.Properties.Static_executable) {
1739 if inList("libc++_static", deps.StaticLibs) {
1740 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001741 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001742 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1743 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1744 // move them to the beginning of deps.LateStaticLibs
1745 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001746 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001747 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001748 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001749 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001750 }
Colin Crossca860ac2016-01-04 14:34:37 -08001751
1752 if !Bool(binary.Properties.Static_executable) && inList("libc", deps.StaticLibs) {
1753 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1754 "from static libs or set static_executable: true")
1755 }
1756 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001757}
1758
Colin Crossc99deeb2016-04-11 15:06:20 -07001759func (*binaryLinker) installable() bool {
1760 return true
1761}
1762
Colin Crossca860ac2016-01-04 14:34:37 -08001763func NewBinary(hod common.HostOrDeviceSupported) *Module {
1764 module := newModule(hod, common.MultilibFirst)
1765 module.compiler = &baseCompiler{}
1766 module.linker = &binaryLinker{}
1767 module.installer = &baseInstaller{
1768 dir: "bin",
1769 }
1770 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001771}
1772
Colin Crossca860ac2016-01-04 14:34:37 -08001773func binaryFactory() (blueprint.Module, []interface{}) {
1774 module := NewBinary(common.HostAndDeviceSupported)
1775 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001776}
1777
Colin Crossca860ac2016-01-04 14:34:37 -08001778func (binary *binaryLinker) ModifyProperties(ctx ModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001779 if ctx.Darwin() {
Colin Crossca860ac2016-01-04 14:34:37 -08001780 binary.Properties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001781 }
Colin Crossca860ac2016-01-04 14:34:37 -08001782 if Bool(binary.Properties.Static_executable) {
1783 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001784 }
1785}
1786
Colin Crossca860ac2016-01-04 14:34:37 -08001787func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1788 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001789
Dan Willemsen490fd492015-11-24 17:53:15 -08001790 if ctx.Host() {
1791 flags.LdFlags = append(flags.LdFlags, "-pie")
1792 if ctx.HostType() == common.Windows {
1793 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1794 }
1795 }
1796
1797 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1798 // all code is position independent, and then those warnings get promoted to
1799 // errors.
1800 if ctx.HostType() != common.Windows {
1801 flags.CFlags = append(flags.CFlags, "-fpie")
1802 }
Colin Cross97ba0732015-03-23 17:50:24 -07001803
Colin Crossf6566ed2015-03-24 11:13:38 -07001804 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001805 if Bool(binary.Properties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001806 // Clang driver needs -static to create static executable.
1807 // However, bionic/linker uses -shared to overwrite.
1808 // Linker for x86 targets does not allow coexistance of -static and -shared,
1809 // so we add -static only if -shared is not used.
1810 if !inList("-shared", flags.LdFlags) {
1811 flags.LdFlags = append(flags.LdFlags, "-static")
1812 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001813
Colin Crossed4cf0b2015-03-26 14:43:45 -07001814 flags.LdFlags = append(flags.LdFlags,
1815 "-nostdlib",
1816 "-Bstatic",
1817 "-Wl,--gc-sections",
1818 )
1819
1820 } else {
1821 linker := "/system/bin/linker"
1822 if flags.Toolchain.Is64Bit() {
Colin Crossca860ac2016-01-04 14:34:37 -08001823 linker += "64"
Colin Crossed4cf0b2015-03-26 14:43:45 -07001824 }
1825
1826 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001827 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001828 "-nostdlib",
1829 "-Bdynamic",
1830 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1831 "-Wl,--gc-sections",
1832 "-Wl,-z,nocopyreloc",
1833 )
1834 }
Colin Cross0af4b842015-04-30 16:36:18 -07001835 } else if ctx.Darwin() {
1836 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001837 }
1838
Colin Cross97ba0732015-03-23 17:50:24 -07001839 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001840}
1841
Colin Crossca860ac2016-01-04 14:34:37 -08001842func (binary *binaryLinker) link(ctx ModuleContext,
1843 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001844
Colin Crossca860ac2016-01-04 14:34:37 -08001845 outputFile := common.PathForModuleOut(ctx, binary.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
1846 if ctx.HostOrDevice().Host() {
1847 binary.hostToolPath = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001848 }
Colin Crossca860ac2016-01-04 14:34:37 -08001849 ret := outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001850
Colin Crossca860ac2016-01-04 14:34:37 -08001851 if binary.Properties.Prefix_symbols != "" {
Colin Crossbfae8852015-03-26 14:44:11 -07001852 afterPrefixSymbols := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001853 outputFile = common.PathForModuleOut(ctx, binary.getStem(ctx)+".intermediate")
1854 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1855 flagsToBuilderFlags(flags), afterPrefixSymbols)
Colin Crossbfae8852015-03-26 14:44:11 -07001856 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001857
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001858 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001859
Colin Crossca860ac2016-01-04 14:34:37 -08001860 sharedLibs := deps.SharedLibs
1861 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1862
1863 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001864 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Crossca860ac2016-01-04 14:34:37 -08001865 flagsToBuilderFlags(flags), outputFile)
1866
1867 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001868}
Colin Cross3f40fa42015-01-30 17:27:36 -08001869
Colin Crossca860ac2016-01-04 14:34:37 -08001870func (binary *binaryLinker) HostToolPath() common.OptionalPath {
1871 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001872}
1873
Colin Cross6362e272015-10-29 15:25:03 -07001874func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08001875 if m, ok := mctx.Module().(*Module); ok {
1876 if test, ok := m.linker.(*testLinker); ok {
1877 if Bool(test.Properties.Test_per_src) {
1878 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
1879 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1880 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1881 }
1882 tests := mctx.CreateLocalVariations(testNames...)
1883 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
1884 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
1885 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
1886 }
Colin Cross6002e052015-09-16 16:00:08 -07001887 }
1888 }
1889 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001890}
1891
Colin Crossca860ac2016-01-04 14:34:37 -08001892type testLinker struct {
1893 binaryLinker
1894 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001895}
1896
Dan Willemsend30e6102016-03-30 17:35:50 -07001897func (test *testLinker) begin(ctx BaseModuleContext) {
1898 test.binaryLinker.begin(ctx)
1899
1900 runpath := "../../lib"
1901 if ctx.toolchain().Is64Bit() {
1902 runpath += "64"
1903 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001904 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07001905}
1906
Colin Crossca860ac2016-01-04 14:34:37 -08001907func (test *testLinker) props() []interface{} {
1908 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07001909}
1910
Colin Crossca860ac2016-01-04 14:34:37 -08001911func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
1912 flags = test.binaryLinker.flags(ctx, flags)
1913
1914 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001915 return flags
1916 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001917
Colin Cross97ba0732015-03-23 17:50:24 -07001918 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001919 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001920 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001921
1922 if ctx.HostType() == common.Windows {
1923 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1924 } else {
1925 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1926 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1927 }
1928 } else {
1929 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001930 }
1931
1932 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001933 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001934 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001935
Colin Cross21b9a242015-03-24 14:15:58 -07001936 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001937}
1938
Colin Crossca860ac2016-01-04 14:34:37 -08001939func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1940 if test.Properties.Gtest {
1941 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001942 }
Colin Crossca860ac2016-01-04 14:34:37 -08001943 deps = test.binaryLinker.deps(ctx, deps)
1944 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07001945}
1946
Colin Crossca860ac2016-01-04 14:34:37 -08001947type testInstaller struct {
1948 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08001949}
1950
Colin Crossca860ac2016-01-04 14:34:37 -08001951func (installer *testInstaller) install(ctx ModuleContext, file common.Path) {
1952 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
1953 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
1954 installer.baseInstaller.install(ctx, file)
1955}
1956
1957func NewTest(hod common.HostOrDeviceSupported) *Module {
1958 module := newModule(hod, common.MultilibBoth)
1959 module.compiler = &baseCompiler{}
1960 linker := &testLinker{}
1961 linker.Properties.Gtest = true
1962 module.linker = linker
1963 module.installer = &testInstaller{
1964 baseInstaller: baseInstaller{
1965 dir: "nativetest",
1966 dir64: "nativetest64",
1967 data: true,
1968 },
Dan Albertc403f7c2015-03-18 14:01:18 -07001969 }
Colin Crossca860ac2016-01-04 14:34:37 -08001970 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001971}
1972
Colin Crossca860ac2016-01-04 14:34:37 -08001973func testFactory() (blueprint.Module, []interface{}) {
1974 module := NewTest(common.HostAndDeviceSupported)
1975 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001976}
1977
Colin Crossca860ac2016-01-04 14:34:37 -08001978type benchmarkLinker struct {
1979 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07001980}
1981
Colin Crossca860ac2016-01-04 14:34:37 -08001982func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1983 deps = benchmark.binaryLinker.deps(ctx, deps)
1984 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
1985 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07001986}
1987
Colin Crossca860ac2016-01-04 14:34:37 -08001988func NewBenchmark(hod common.HostOrDeviceSupported) *Module {
1989 module := newModule(hod, common.MultilibFirst)
1990 module.compiler = &baseCompiler{}
1991 module.linker = &benchmarkLinker{}
1992 module.installer = &baseInstaller{
1993 dir: "nativetest",
1994 dir64: "nativetest64",
1995 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07001996 }
Colin Crossca860ac2016-01-04 14:34:37 -08001997 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07001998}
1999
Colin Crossca860ac2016-01-04 14:34:37 -08002000func benchmarkFactory() (blueprint.Module, []interface{}) {
2001 module := NewBenchmark(common.HostAndDeviceSupported)
2002 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002003}
2004
Colin Cross3f40fa42015-01-30 17:27:36 -08002005//
2006// Static library
2007//
2008
Colin Crossca860ac2016-01-04 14:34:37 -08002009func libraryStaticFactory() (blueprint.Module, []interface{}) {
2010 module := NewLibrary(common.HostAndDeviceSupported, false, true)
2011 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002012}
2013
2014//
2015// Shared libraries
2016//
2017
Colin Crossca860ac2016-01-04 14:34:37 -08002018func librarySharedFactory() (blueprint.Module, []interface{}) {
2019 module := NewLibrary(common.HostAndDeviceSupported, true, false)
2020 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002021}
2022
2023//
2024// Host static library
2025//
2026
Colin Crossca860ac2016-01-04 14:34:37 -08002027func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
2028 module := NewLibrary(common.HostSupported, false, true)
2029 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002030}
2031
2032//
2033// Host Shared libraries
2034//
2035
Colin Crossca860ac2016-01-04 14:34:37 -08002036func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
2037 module := NewLibrary(common.HostSupported, true, false)
2038 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002039}
2040
2041//
2042// Host Binaries
2043//
2044
Colin Crossca860ac2016-01-04 14:34:37 -08002045func binaryHostFactory() (blueprint.Module, []interface{}) {
2046 module := NewBinary(common.HostSupported)
2047 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002048}
2049
2050//
Colin Cross1f8f2342015-03-26 16:09:47 -07002051// Host Tests
2052//
2053
Colin Crossca860ac2016-01-04 14:34:37 -08002054func testHostFactory() (blueprint.Module, []interface{}) {
2055 module := NewTest(common.HostSupported)
2056 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002057}
2058
2059//
Colin Cross2ba19d92015-05-07 15:44:20 -07002060// Host Benchmarks
2061//
2062
Colin Crossca860ac2016-01-04 14:34:37 -08002063func benchmarkHostFactory() (blueprint.Module, []interface{}) {
2064 module := NewBenchmark(common.HostSupported)
2065 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002066}
2067
2068//
Colin Crosscfad1192015-11-02 16:43:11 -08002069// Defaults
2070//
Colin Crossca860ac2016-01-04 14:34:37 -08002071type Defaults struct {
Colin Crosscfad1192015-11-02 16:43:11 -08002072 common.AndroidModuleBase
2073 common.DefaultsModule
2074}
2075
Colin Crossca860ac2016-01-04 14:34:37 -08002076func (*Defaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002077}
2078
Colin Crossca860ac2016-01-04 14:34:37 -08002079func defaultsFactory() (blueprint.Module, []interface{}) {
2080 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002081
2082 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002083 &BaseProperties{},
2084 &BaseCompilerProperties{},
2085 &BaseLinkerProperties{},
2086 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002087 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002088 &LibraryLinkerProperties{},
2089 &BinaryLinkerProperties{},
2090 &TestLinkerProperties{},
2091 &UnusedProperties{},
2092 &StlProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002093 }
2094
Dan Willemsen218f6562015-07-08 18:13:11 -07002095 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
2096 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002097
2098 return common.InitDefaultsModule(module, module, propertyStructs...)
2099}
2100
2101//
Colin Cross3f40fa42015-01-30 17:27:36 -08002102// Device libraries shipped with gcc
2103//
2104
Colin Crossca860ac2016-01-04 14:34:37 -08002105type toolchainLibraryLinker struct {
2106 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002107}
2108
Colin Crossca860ac2016-01-04 14:34:37 -08002109var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2110
2111func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002112 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002113 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002114}
2115
Colin Crossca860ac2016-01-04 14:34:37 -08002116func (*toolchainLibraryLinker) buildStatic() bool {
2117 return true
2118}
Colin Cross3f40fa42015-01-30 17:27:36 -08002119
Colin Crossca860ac2016-01-04 14:34:37 -08002120func (*toolchainLibraryLinker) buildShared() bool {
2121 return false
2122}
2123
2124func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
2125 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2126 module.compiler = &baseCompiler{}
2127 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002128 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002129 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002130}
2131
Colin Crossca860ac2016-01-04 14:34:37 -08002132func (library *toolchainLibraryLinker) link(ctx ModuleContext,
2133 flags Flags, deps PathDeps, objFiles common.Paths) common.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002134
2135 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002136 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002137
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002138 if flags.Clang {
2139 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2140 }
2141
Colin Crossca860ac2016-01-04 14:34:37 -08002142 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002143
2144 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002145
Colin Crossca860ac2016-01-04 14:34:37 -08002146 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002147}
2148
Colin Crossc99deeb2016-04-11 15:06:20 -07002149func (*toolchainLibraryLinker) installable() bool {
2150 return false
2151}
2152
Dan Albertbe961682015-03-18 23:38:50 -07002153// NDK prebuilt libraries.
2154//
2155// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2156// either (with the exception of the shared STLs, which are installed to the app's directory rather
2157// than to the system image).
2158
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002159func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
2160 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
2161 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07002162}
2163
Dan Albertc3144b12015-04-28 18:17:56 -07002164func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002165 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002166
2167 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2168 // We want to translate to just NAME.EXT
2169 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2170 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002171 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002172}
2173
Colin Crossca860ac2016-01-04 14:34:37 -08002174type ndkPrebuiltObjectLinker struct {
2175 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002176}
2177
Colin Crossca860ac2016-01-04 14:34:37 -08002178func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002179 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002180 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002181}
2182
Colin Crossca860ac2016-01-04 14:34:37 -08002183func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
2184 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2185 module.linker = &ndkPrebuiltObjectLinker{}
2186 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002187}
2188
Colin Crossca860ac2016-01-04 14:34:37 -08002189func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
2190 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002191 // A null build step, but it sets up the output path.
2192 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2193 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2194 }
2195
Colin Crossca860ac2016-01-04 14:34:37 -08002196 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002197}
2198
Colin Crossca860ac2016-01-04 14:34:37 -08002199type ndkPrebuiltLibraryLinker struct {
2200 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002201}
2202
Colin Crossca860ac2016-01-04 14:34:37 -08002203var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2204var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002205
Colin Crossca860ac2016-01-04 14:34:37 -08002206func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002207 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002208}
2209
Colin Crossca860ac2016-01-04 14:34:37 -08002210func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002211 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002212 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002213}
2214
Colin Crossca860ac2016-01-04 14:34:37 -08002215func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2216 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2217 linker := &ndkPrebuiltLibraryLinker{}
2218 linker.dynamicProperties.BuildShared = true
2219 module.linker = linker
2220 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002221}
2222
Colin Crossca860ac2016-01-04 14:34:37 -08002223func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
2224 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002225 // A null build step, but it sets up the output path.
2226 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2227 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2228 }
2229
Colin Cross919281a2016-04-05 16:42:05 -07002230 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002231
Colin Crossca860ac2016-01-04 14:34:37 -08002232 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2233 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002234}
2235
2236// The NDK STLs are slightly different from the prebuilt system libraries:
2237// * Are not specific to each platform version.
2238// * The libraries are not in a predictable location for each STL.
2239
Colin Crossca860ac2016-01-04 14:34:37 -08002240type ndkPrebuiltStlLinker struct {
2241 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002242}
2243
Colin Crossca860ac2016-01-04 14:34:37 -08002244func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2245 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2246 linker := &ndkPrebuiltStlLinker{}
2247 linker.dynamicProperties.BuildShared = true
2248 module.linker = linker
2249 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002250}
2251
Colin Crossca860ac2016-01-04 14:34:37 -08002252func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2253 module := newBaseModule(common.DeviceSupported, common.MultilibBoth)
2254 linker := &ndkPrebuiltStlLinker{}
2255 linker.dynamicProperties.BuildStatic = true
2256 module.linker = linker
2257 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002258}
2259
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002260func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002261 gccVersion := toolchain.GccVersion()
2262 var libDir string
2263 switch stl {
2264 case "libstlport":
2265 libDir = "cxx-stl/stlport/libs"
2266 case "libc++":
2267 libDir = "cxx-stl/llvm-libc++/libs"
2268 case "libgnustl":
2269 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2270 }
2271
2272 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002273 ndkSrcRoot := "prebuilts/ndk/current/sources"
2274 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002275 }
2276
2277 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002278 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002279}
2280
Colin Crossca860ac2016-01-04 14:34:37 -08002281func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
2282 deps PathDeps, objFiles common.Paths) common.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002283 // A null build step, but it sets up the output path.
2284 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2285 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2286 }
2287
Colin Cross919281a2016-04-05 16:42:05 -07002288 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002289
2290 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002291 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002292 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002293 libExt = staticLibraryExtension
2294 }
2295
2296 stlName := strings.TrimSuffix(libName, "_shared")
2297 stlName = strings.TrimSuffix(stlName, "_static")
2298 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002299 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002300}
2301
Colin Cross6362e272015-10-29 15:25:03 -07002302func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002303 if m, ok := mctx.Module().(*Module); ok {
2304 if m.linker != nil {
2305 if linker, ok := m.linker.(baseLinkerInterface); ok {
2306 var modules []blueprint.Module
2307 if linker.buildStatic() && linker.buildShared() {
2308 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002309 static := modules[0].(*Module)
2310 shared := modules[1].(*Module)
2311
2312 static.linker.(baseLinkerInterface).setStatic(true)
2313 shared.linker.(baseLinkerInterface).setStatic(false)
2314
2315 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2316 sharedCompiler := shared.compiler.(*libraryCompiler)
2317 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2318 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2319 // Optimize out compiling common .o files twice for static+shared libraries
2320 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2321 sharedCompiler.baseCompiler.Properties.Srcs = nil
2322 }
2323 }
Colin Crossca860ac2016-01-04 14:34:37 -08002324 } else if linker.buildStatic() {
2325 modules = mctx.CreateLocalVariations("static")
2326 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2327 } else if linker.buildShared() {
2328 modules = mctx.CreateLocalVariations("shared")
2329 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2330 } else {
2331 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2332 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002333 }
2334 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002335 }
2336}
Colin Cross74d1ec02015-04-28 13:30:13 -07002337
2338// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2339// modifies the slice contents in place, and returns a subslice of the original slice
2340func lastUniqueElements(list []string) []string {
2341 totalSkip := 0
2342 for i := len(list) - 1; i >= totalSkip; i-- {
2343 skip := 0
2344 for j := i - 1; j >= totalSkip; j-- {
2345 if list[i] == list[j] {
2346 skip++
2347 } else {
2348 list[j+skip] = list[j]
2349 }
2350 }
2351 totalSkip += skip
2352 }
2353 return list[totalSkip:]
2354}
Colin Cross06a931b2015-10-28 17:23:31 -07002355
2356var Bool = proptools.Bool