blob: 64ac50f2e07f5f7c2912019c4840dde8aaa891ae [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 Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
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 Cross635c3b02016-05-18 15:37:25 -070059 android.RegisterBottomUpMutator("link", linkageMutator)
60 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080062
Colin Cross635c3b02016-05-18 15:37:25 -070063 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
64 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080065
Colin Cross635c3b02016-05-18 15:37:25 -070066 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
67 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Cross463a90e2015-06-17 14:20:06 -070068}
69
Colin Cross3f40fa42015-01-30 17:27:36 -080070var (
Colin Cross635c3b02016-05-18 15:37:25 -070071 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080072
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
Colin Cross3f40fa42015-01-30 17:27:36 -080074)
75
76// Flags used by lots of devices. Putting them in package static variables will save bytes in
77// build.ninja so they aren't repeated for every file
78var (
79 commonGlobalCflags = []string{
80 "-DANDROID",
81 "-fmessage-length=0",
82 "-W",
83 "-Wall",
84 "-Wno-unused",
85 "-Winit-self",
86 "-Wpointer-arith",
87
88 // COMMON_RELEASE_CFLAGS
89 "-DNDEBUG",
90 "-UDEBUG",
91 }
92
93 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080094 "-fdiagnostics-color",
95
Colin Cross3f40fa42015-01-30 17:27:36 -080096 // TARGET_ERROR_FLAGS
97 "-Werror=return-type",
98 "-Werror=non-virtual-dtor",
99 "-Werror=address",
100 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -0800101 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -0800102 }
103
104 hostGlobalCflags = []string{}
105
106 commonGlobalCppflags = []string{
107 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700108 }
109
Dan Willemsenbe03f342016-03-03 17:21:04 -0800110 noOverrideGlobalCflags = []string{
111 "-Werror=int-to-pointer-cast",
112 "-Werror=pointer-to-int-cast",
113 }
114
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700115 illegalFlags = []string{
116 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800117 }
Dan Willemsen97704ed2016-07-07 21:40:39 -0700118
119 ndkPrebuiltSharedLibs = []string{
120 "android",
121 "c",
122 "dl",
123 "EGL",
124 "GLESv1_CM",
125 "GLESv2",
126 "GLESv3",
127 "jnigraphics",
128 "log",
129 "mediandk",
130 "m",
131 "OpenMAXAL",
132 "OpenSLES",
133 "stdc++",
134 "vulkan",
135 "z",
136 }
137 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800138)
139
140func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700141 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700142 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
143 }
144
Colin Cross3f40fa42015-01-30 17:27:36 -0800145 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
146 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
147 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800148 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800149
150 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
151
152 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800153 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800154 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800155 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800156 pctx.StaticVariable("hostClangGlobalCflags",
157 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800158 pctx.StaticVariable("noOverrideClangGlobalCflags",
159 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
160
Tim Kilbournf2948142015-03-11 12:03:03 -0700161 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800162 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
164 // Everything in this list is a crime against abstraction and dependency tracking.
165 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800166 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167 []string{
168 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800169 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 "hardware/libhardware/include",
171 "hardware/libhardware_legacy/include",
172 "hardware/ril/include",
173 "libnativehelper/include",
174 "frameworks/native/include",
175 "frameworks/native/opengl/include",
176 "frameworks/av/include",
177 "frameworks/base/include",
178 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800179 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
180 // with this, since there is no associated library.
181 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
182 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800183
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700184 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
185 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700186 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700187 return override, nil
188 }
189 return "${clangDefaultBase}", nil
190 })
191 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700192 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700193 return override, nil
194 }
Stephen Hines369f0132016-04-26 14:34:07 -0700195 return "clang-2812033", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700196 })
Colin Cross16b23492016-01-06 14:41:07 -0800197 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
198 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800199}
200
Colin Crossca860ac2016-01-04 14:34:37 -0800201type Deps struct {
202 SharedLibs, LateSharedLibs []string
203 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700204
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700205 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
206
Colin Cross81413472016-04-11 14:37:39 -0700207 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208
Dan Willemsenb40aab62016-04-20 14:21:14 -0700209 GeneratedSources []string
210 GeneratedHeaders []string
211
Colin Cross97ba0732015-03-23 17:50:24 -0700212 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700213}
214
Colin Crossca860ac2016-01-04 14:34:37 -0800215type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700216 SharedLibs, LateSharedLibs android.Paths
217 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700218
Colin Cross635c3b02016-05-18 15:37:25 -0700219 ObjFiles android.Paths
220 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700221
Colin Cross635c3b02016-05-18 15:37:25 -0700222 GeneratedSources android.Paths
223 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700224
Dan Willemsen76f08272016-07-09 00:14:08 -0700225 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226
Colin Cross635c3b02016-05-18 15:37:25 -0700227 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700228}
229
Colin Crossca860ac2016-01-04 14:34:37 -0800230type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700231 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
232 AsFlags []string // Flags that apply to assembly source files
233 CFlags []string // Flags that apply to C and C++ source files
234 ConlyFlags []string // Flags that apply to C source files
235 CppFlags []string // Flags that apply to C++ source files
236 YaccFlags []string // Flags that apply to Yacc source files
237 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800238 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700239
240 Nocrt bool
241 Toolchain Toolchain
242 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800243
244 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800245 DynamicLinker string
246
Colin Cross635c3b02016-05-18 15:37:25 -0700247 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700248}
249
Colin Crossca860ac2016-01-04 14:34:37 -0800250type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700251 // 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 -0700252 Srcs []string `android:"arch_variant"`
253
254 // list of source files that should not be used to build the C/C++ module.
255 // This is most useful in the arch/multilib variants to remove non-common files
256 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700257
258 // list of module-specific flags that will be used for C and C++ compiles.
259 Cflags []string `android:"arch_variant"`
260
261 // list of module-specific flags that will be used for C++ compiles
262 Cppflags []string `android:"arch_variant"`
263
264 // list of module-specific flags that will be used for C compiles
265 Conlyflags []string `android:"arch_variant"`
266
267 // list of module-specific flags that will be used for .S compiles
268 Asflags []string `android:"arch_variant"`
269
Colin Crossca860ac2016-01-04 14:34:37 -0800270 // list of module-specific flags that will be used for C and C++ compiles when
271 // compiling with clang
272 Clang_cflags []string `android:"arch_variant"`
273
274 // list of module-specific flags that will be used for .S compiles when
275 // compiling with clang
276 Clang_asflags []string `android:"arch_variant"`
277
Colin Cross7d5136f2015-05-11 13:39:40 -0700278 // list of module-specific flags that will be used for .y and .yy compiles
279 Yaccflags []string
280
Colin Cross7d5136f2015-05-11 13:39:40 -0700281 // the instruction set architecture to use to compile the C/C++
282 // module.
283 Instruction_set string `android:"arch_variant"`
284
285 // list of directories relative to the root of the source tree that will
286 // be added to the include path using -I.
287 // If possible, don't use this. If adding paths from the current directory use
288 // local_include_dirs, if adding paths from other modules use export_include_dirs in
289 // that module.
290 Include_dirs []string `android:"arch_variant"`
291
292 // list of directories relative to the Blueprints file that will
293 // be added to the include path using -I
294 Local_include_dirs []string `android:"arch_variant"`
295
Dan Willemsenb40aab62016-04-20 14:21:14 -0700296 // list of generated sources to compile. These are the names of gensrcs or
297 // genrule modules.
298 Generated_sources []string `android:"arch_variant"`
299
300 // list of generated headers to add to the include path. These are the names
301 // of genrule modules.
302 Generated_headers []string `android:"arch_variant"`
303
Colin Crossca860ac2016-01-04 14:34:37 -0800304 // pass -frtti instead of -fno-rtti
305 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700306
Colin Crossca860ac2016-01-04 14:34:37 -0800307 Debug, Release struct {
308 // list of module-specific flags that will be used for C and C++ compiles in debug or
309 // release builds
310 Cflags []string `android:"arch_variant"`
311 } `android:"arch_variant"`
312}
Colin Cross7d5136f2015-05-11 13:39:40 -0700313
Colin Crossca860ac2016-01-04 14:34:37 -0800314type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700315 // list of modules whose object files should be linked into this module
316 // in their entirety. For static library modules, all of the .o files from the intermediate
317 // directory of the dependency will be linked into this modules .a file. For a shared library,
318 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700319 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700320
321 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700322 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700323
324 // list of modules that should be dynamically linked into this module.
325 Shared_libs []string `android:"arch_variant"`
326
Colin Crossca860ac2016-01-04 14:34:37 -0800327 // list of module-specific flags that will be used for all link steps
328 Ldflags []string `android:"arch_variant"`
329
330 // don't insert default compiler flags into asflags, cflags,
331 // cppflags, conlyflags, ldflags, or include_dirs
332 No_default_compiler_flags *bool
333
334 // list of system libraries that will be dynamically linked to
335 // shared library and executable modules. If unset, generally defaults to libc
336 // and libm. Set to [] to prevent linking against libc and libm.
337 System_shared_libs []string
338
Colin Cross7d5136f2015-05-11 13:39:40 -0700339 // allow the module to contain undefined symbols. By default,
340 // modules cannot contain undefined symbols that are not satisified by their immediate
341 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
342 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700343 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700344
Dan Willemsend67be222015-09-16 15:19:33 -0700345 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700346 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700347
Colin Cross7d5136f2015-05-11 13:39:40 -0700348 // -l arguments to pass to linker for host-provided shared libraries
349 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700350
351 // list of shared libraries to re-export include directories from. Entries must be
352 // present in shared_libs.
353 Export_shared_lib_headers []string `android:"arch_variant"`
354
355 // list of static libraries to re-export include directories from. Entries must be
356 // present in static_libs.
357 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800358}
Colin Cross7d5136f2015-05-11 13:39:40 -0700359
Colin Crossca860ac2016-01-04 14:34:37 -0800360type LibraryCompilerProperties struct {
361 Static struct {
362 Srcs []string `android:"arch_variant"`
363 Exclude_srcs []string `android:"arch_variant"`
364 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700365 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800366 Shared struct {
367 Srcs []string `android:"arch_variant"`
368 Exclude_srcs []string `android:"arch_variant"`
369 Cflags []string `android:"arch_variant"`
370 } `android:"arch_variant"`
371}
372
Colin Cross919281a2016-04-05 16:42:05 -0700373type FlagExporterProperties struct {
374 // list of directories relative to the Blueprints file that will
375 // be added to the include path using -I for any module that links against this module
376 Export_include_dirs []string `android:"arch_variant"`
377}
378
Colin Crossca860ac2016-01-04 14:34:37 -0800379type LibraryLinkerProperties struct {
380 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700381 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800382 Whole_static_libs []string `android:"arch_variant"`
383 Static_libs []string `android:"arch_variant"`
384 Shared_libs []string `android:"arch_variant"`
385 } `android:"arch_variant"`
386 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700387 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800388 Whole_static_libs []string `android:"arch_variant"`
389 Static_libs []string `android:"arch_variant"`
390 Shared_libs []string `android:"arch_variant"`
391 } `android:"arch_variant"`
392
393 // local file name to pass to the linker as --version_script
394 Version_script *string `android:"arch_variant"`
395 // local file name to pass to the linker as -unexported_symbols_list
396 Unexported_symbols_list *string `android:"arch_variant"`
397 // local file name to pass to the linker as -force_symbols_not_weak_list
398 Force_symbols_not_weak_list *string `android:"arch_variant"`
399 // local file name to pass to the linker as -force_symbols_weak_list
400 Force_symbols_weak_list *string `android:"arch_variant"`
401
Colin Crossca860ac2016-01-04 14:34:37 -0800402 // don't link in crt_begin and crt_end. This flag should only be necessary for
403 // compiling crt or libc.
404 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800405
406 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800407}
408
409type BinaryLinkerProperties struct {
410 // compile executable with -static
411 Static_executable *bool
412
413 // set the name of the output
414 Stem string `android:"arch_variant"`
415
416 // append to the name of the output
417 Suffix string `android:"arch_variant"`
418
419 // if set, add an extra objcopy --prefix-symbols= step
420 Prefix_symbols string
421}
422
423type TestLinkerProperties struct {
424 // if set, build against the gtest library. Defaults to true.
425 Gtest bool
426
427 // Create a separate binary for each source file. Useful when there is
428 // global state that can not be torn down and reset between each test suite.
429 Test_per_src *bool
430}
431
Colin Cross81413472016-04-11 14:37:39 -0700432type ObjectLinkerProperties struct {
433 // names of other cc_object modules to link into this module using partial linking
434 Objs []string `android:"arch_variant"`
435}
436
Colin Crossca860ac2016-01-04 14:34:37 -0800437// Properties used to compile all C or C++ modules
438type BaseProperties struct {
439 // compile module with clang instead of gcc
440 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700441
442 // Minimum sdk version supported when compiling against the ndk
443 Sdk_version string
444
Colin Crossca860ac2016-01-04 14:34:37 -0800445 // don't insert default compiler flags into asflags, cflags,
446 // cppflags, conlyflags, ldflags, or include_dirs
447 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700448
449 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700450 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800451}
452
453type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700454 // install to a subdirectory of the default install path for the module
455 Relative_install_path string
456}
457
Colin Cross665dce92016-04-28 14:50:03 -0700458type StripProperties struct {
459 Strip struct {
460 None bool
461 Keep_symbols bool
462 }
463}
464
Colin Crossca860ac2016-01-04 14:34:37 -0800465type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700466 Native_coverage *bool
467 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700468 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800469}
470
Colin Crossca860ac2016-01-04 14:34:37 -0800471type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800472 static() bool
473 staticBinary() bool
474 clang() bool
475 toolchain() Toolchain
476 noDefaultCompilerFlags() bool
477 sdk() bool
478 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700479 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800480}
481
482type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700483 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800484 ModuleContextIntf
485}
486
487type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700488 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800489 ModuleContextIntf
490}
491
492type Customizer interface {
493 CustomizeProperties(BaseModuleContext)
494 Properties() []interface{}
495}
496
497type feature interface {
498 begin(ctx BaseModuleContext)
499 deps(ctx BaseModuleContext, deps Deps) Deps
500 flags(ctx ModuleContext, flags Flags) Flags
501 props() []interface{}
502}
503
504type compiler interface {
505 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700506 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800507}
508
509type linker interface {
510 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700511 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700512 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800513}
514
515type installer interface {
516 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700517 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800518 inData() bool
519}
520
Colin Crossc99deeb2016-04-11 15:06:20 -0700521type dependencyTag struct {
522 blueprint.BaseDependencyTag
523 name string
524 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700525
526 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700527}
528
529var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700530 sharedDepTag = dependencyTag{name: "shared", library: true}
531 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
532 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
533 staticDepTag = dependencyTag{name: "static", library: true}
534 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
535 lateStaticDepTag = dependencyTag{name: "late static", library: true}
536 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
537 genSourceDepTag = dependencyTag{name: "gen source"}
538 genHeaderDepTag = dependencyTag{name: "gen header"}
539 objDepTag = dependencyTag{name: "obj"}
540 crtBeginDepTag = dependencyTag{name: "crtbegin"}
541 crtEndDepTag = dependencyTag{name: "crtend"}
542 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700543)
544
Colin Crossca860ac2016-01-04 14:34:37 -0800545// Module contains the properties and members used by all C/C++ module types, and implements
546// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
547// to construct the output file. Behavior can be customized with a Customizer interface
548type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700549 android.ModuleBase
550 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700551
Colin Crossca860ac2016-01-04 14:34:37 -0800552 Properties BaseProperties
553 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700554
Colin Crossca860ac2016-01-04 14:34:37 -0800555 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700556 hod android.HostOrDeviceSupported
557 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700558
Colin Crossca860ac2016-01-04 14:34:37 -0800559 // delegates, initialize before calling Init
560 customizer Customizer
561 features []feature
562 compiler compiler
563 linker linker
564 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700565 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800566 sanitize *sanitize
567
568 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700569
Colin Cross635c3b02016-05-18 15:37:25 -0700570 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800571
572 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700573}
574
Colin Crossca860ac2016-01-04 14:34:37 -0800575func (c *Module) Init() (blueprint.Module, []interface{}) {
576 props := []interface{}{&c.Properties, &c.unused}
577 if c.customizer != nil {
578 props = append(props, c.customizer.Properties()...)
579 }
580 if c.compiler != nil {
581 props = append(props, c.compiler.props()...)
582 }
583 if c.linker != nil {
584 props = append(props, c.linker.props()...)
585 }
586 if c.installer != nil {
587 props = append(props, c.installer.props()...)
588 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700589 if c.stl != nil {
590 props = append(props, c.stl.props()...)
591 }
Colin Cross16b23492016-01-06 14:41:07 -0800592 if c.sanitize != nil {
593 props = append(props, c.sanitize.props()...)
594 }
Colin Crossca860ac2016-01-04 14:34:37 -0800595 for _, feature := range c.features {
596 props = append(props, feature.props()...)
597 }
Colin Crossc472d572015-03-17 15:06:21 -0700598
Colin Cross635c3b02016-05-18 15:37:25 -0700599 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700600
Colin Cross635c3b02016-05-18 15:37:25 -0700601 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700602}
603
Colin Crossca860ac2016-01-04 14:34:37 -0800604type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700605 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800606 moduleContextImpl
607}
608
609type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700610 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800611 moduleContextImpl
612}
613
614type moduleContextImpl struct {
615 mod *Module
616 ctx BaseModuleContext
617}
618
Colin Crossca860ac2016-01-04 14:34:37 -0800619func (ctx *moduleContextImpl) clang() bool {
620 return ctx.mod.clang(ctx.ctx)
621}
622
623func (ctx *moduleContextImpl) toolchain() Toolchain {
624 return ctx.mod.toolchain(ctx.ctx)
625}
626
627func (ctx *moduleContextImpl) static() bool {
628 if ctx.mod.linker == nil {
629 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
630 }
631 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
632 return linker.static()
633 } else {
634 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
635 }
636}
637
638func (ctx *moduleContextImpl) staticBinary() bool {
639 if ctx.mod.linker == nil {
640 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
641 }
642 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
643 return linker.staticBinary()
644 } else {
645 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
646 }
647}
648
649func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
650 return Bool(ctx.mod.Properties.No_default_compiler_flags)
651}
652
653func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700654 if ctx.ctx.Device() {
655 return ctx.mod.Properties.Sdk_version != ""
656 }
657 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800658}
659
660func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700661 if ctx.ctx.Device() {
662 return ctx.mod.Properties.Sdk_version
663 }
664 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800665}
666
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700667func (ctx *moduleContextImpl) selectedStl() string {
668 if stl := ctx.mod.stl; stl != nil {
669 return stl.Properties.SelectedStl
670 }
671 return ""
672}
673
Colin Cross635c3b02016-05-18 15:37:25 -0700674func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800675 return &Module{
676 hod: hod,
677 multilib: multilib,
678 }
679}
680
Colin Cross635c3b02016-05-18 15:37:25 -0700681func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800682 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700683 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800684 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800685 return module
686}
687
Colin Cross635c3b02016-05-18 15:37:25 -0700688func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800689 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700690 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800691 moduleContextImpl: moduleContextImpl{
692 mod: c,
693 },
694 }
695 ctx.ctx = ctx
696
697 flags := Flags{
698 Toolchain: c.toolchain(ctx),
699 Clang: c.clang(ctx),
700 }
Colin Crossca860ac2016-01-04 14:34:37 -0800701 if c.compiler != nil {
702 flags = c.compiler.flags(ctx, flags)
703 }
704 if c.linker != nil {
705 flags = c.linker.flags(ctx, flags)
706 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700707 if c.stl != nil {
708 flags = c.stl.flags(ctx, flags)
709 }
Colin Cross16b23492016-01-06 14:41:07 -0800710 if c.sanitize != nil {
711 flags = c.sanitize.flags(ctx, flags)
712 }
Colin Crossca860ac2016-01-04 14:34:37 -0800713 for _, feature := range c.features {
714 flags = feature.flags(ctx, flags)
715 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800716 if ctx.Failed() {
717 return
718 }
719
Colin Crossca860ac2016-01-04 14:34:37 -0800720 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
721 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
722 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800723
Colin Crossca860ac2016-01-04 14:34:37 -0800724 // Optimization to reduce size of build.ninja
725 // Replace the long list of flags for each file with a module-local variable
726 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
727 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
728 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
729 flags.CFlags = []string{"$cflags"}
730 flags.CppFlags = []string{"$cppflags"}
731 flags.AsFlags = []string{"$asflags"}
732
Colin Crossc99deeb2016-04-11 15:06:20 -0700733 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800734 if ctx.Failed() {
735 return
736 }
737
Dan Willemsen76f08272016-07-09 00:14:08 -0700738 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700739
Colin Cross635c3b02016-05-18 15:37:25 -0700740 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800741 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700742 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800743 if ctx.Failed() {
744 return
745 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 }
747
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if c.linker != nil {
749 outputFile := c.linker.link(ctx, flags, deps, objFiles)
750 if ctx.Failed() {
751 return
752 }
Colin Cross635c3b02016-05-18 15:37:25 -0700753 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700754
Colin Crossc99deeb2016-04-11 15:06:20 -0700755 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800756 c.installer.install(ctx, outputFile)
757 if ctx.Failed() {
758 return
759 }
760 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700761 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800762}
763
Colin Crossca860ac2016-01-04 14:34:37 -0800764func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
765 if c.cachedToolchain == nil {
766 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700767 os := ctx.Os()
768 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800769 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700770 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800771 return nil
772 }
773 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 }
Colin Crossca860ac2016-01-04 14:34:37 -0800775 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800776}
777
Colin Crossca860ac2016-01-04 14:34:37 -0800778func (c *Module) begin(ctx BaseModuleContext) {
779 if c.compiler != nil {
780 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700781 }
Colin Crossca860ac2016-01-04 14:34:37 -0800782 if c.linker != nil {
783 c.linker.begin(ctx)
784 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700785 if c.stl != nil {
786 c.stl.begin(ctx)
787 }
Colin Cross16b23492016-01-06 14:41:07 -0800788 if c.sanitize != nil {
789 c.sanitize.begin(ctx)
790 }
Colin Crossca860ac2016-01-04 14:34:37 -0800791 for _, feature := range c.features {
792 feature.begin(ctx)
793 }
794}
795
Colin Crossc99deeb2016-04-11 15:06:20 -0700796func (c *Module) deps(ctx BaseModuleContext) Deps {
797 deps := Deps{}
798
799 if c.compiler != nil {
800 deps = c.compiler.deps(ctx, deps)
801 }
802 if c.linker != nil {
803 deps = c.linker.deps(ctx, deps)
804 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700805 if c.stl != nil {
806 deps = c.stl.deps(ctx, deps)
807 }
Colin Cross16b23492016-01-06 14:41:07 -0800808 if c.sanitize != nil {
809 deps = c.sanitize.deps(ctx, deps)
810 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700811 for _, feature := range c.features {
812 deps = feature.deps(ctx, deps)
813 }
814
815 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
816 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
817 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
818 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
819 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
820
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700821 for _, lib := range deps.ReexportSharedLibHeaders {
822 if !inList(lib, deps.SharedLibs) {
823 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
824 }
825 }
826
827 for _, lib := range deps.ReexportStaticLibHeaders {
828 if !inList(lib, deps.StaticLibs) {
829 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
830 }
831 }
832
Colin Crossc99deeb2016-04-11 15:06:20 -0700833 return deps
834}
835
Colin Cross635c3b02016-05-18 15:37:25 -0700836func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800837 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700838 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800839 moduleContextImpl: moduleContextImpl{
840 mod: c,
841 },
842 }
843 ctx.ctx = ctx
844
845 if c.customizer != nil {
846 c.customizer.CustomizeProperties(ctx)
847 }
848
849 c.begin(ctx)
850
Colin Crossc99deeb2016-04-11 15:06:20 -0700851 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800852
Colin Crossb5bc4b42016-07-11 16:11:59 -0700853 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
854 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700855
856 if ctx.sdk() {
857 version := "." + ctx.sdkVersion()
858
859 rewriteNdkLibs := func(list []string) []string {
860 for i, entry := range list {
861 if inList(entry, ndkPrebuiltSharedLibraries) {
862 list[i] = "ndk_" + entry + version
863 }
864 }
865 return list
866 }
867
868 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
869 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
870 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700871
872 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
873 deps.WholeStaticLibs...)
874
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700875 for _, lib := range deps.StaticLibs {
876 depTag := staticDepTag
877 if inList(lib, deps.ReexportStaticLibHeaders) {
878 depTag = staticExportDepTag
879 }
880 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
881 deps.StaticLibs...)
882 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700883
884 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
885 deps.LateStaticLibs...)
886
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700887 for _, lib := range deps.SharedLibs {
888 depTag := sharedDepTag
889 if inList(lib, deps.ReexportSharedLibHeaders) {
890 depTag = sharedExportDepTag
891 }
892 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
893 deps.SharedLibs...)
894 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700895
896 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
897 deps.LateSharedLibs...)
898
Colin Cross68861832016-07-08 10:41:41 -0700899 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
900 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700901
Colin Cross68861832016-07-08 10:41:41 -0700902 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700903
904 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700905 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800906 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700907 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700908 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700909 }
Colin Cross6362e272015-10-29 15:25:03 -0700910}
Colin Cross21b9a242015-03-24 14:15:58 -0700911
Colin Cross635c3b02016-05-18 15:37:25 -0700912func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700913 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700914 c.depsMutator(ctx)
915 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800916}
917
Colin Crossca860ac2016-01-04 14:34:37 -0800918func (c *Module) clang(ctx BaseModuleContext) bool {
919 clang := Bool(c.Properties.Clang)
920
921 if c.Properties.Clang == nil {
922 if ctx.Host() {
923 clang = true
924 }
925
926 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
927 clang = true
928 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800929 }
Colin Cross28344522015-04-22 13:07:53 -0700930
Colin Crossca860ac2016-01-04 14:34:37 -0800931 if !c.toolchain(ctx).ClangSupported() {
932 clang = false
933 }
934
935 return clang
936}
937
Colin Crossc99deeb2016-04-11 15:06:20 -0700938// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700939func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800940 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800941
Dan Willemsena96ff642016-06-07 12:34:45 -0700942 // Whether a module can link to another module, taking into
943 // account NDK linking.
944 linkTypeOk := func(from, to *Module) bool {
945 if from.Target().Os != android.Android {
946 // Host code is not restricted
947 return true
948 }
949 if from.Properties.Sdk_version == "" {
950 // Platform code can link to anything
951 return true
952 }
953 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
954 // These are always allowed
955 return true
956 }
957 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
958 // These are allowed, but don't set sdk_version
959 return true
960 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700961 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
962 // These are allowed, but don't set sdk_version
963 return true
964 }
965 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700966 }
967
Colin Crossc99deeb2016-04-11 15:06:20 -0700968 ctx.VisitDirectDeps(func(m blueprint.Module) {
969 name := ctx.OtherModuleName(m)
970 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800971
Colin Cross635c3b02016-05-18 15:37:25 -0700972 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700973 if a == nil {
974 ctx.ModuleErrorf("module %q not an android module", name)
975 return
Colin Crossca860ac2016-01-04 14:34:37 -0800976 }
Colin Crossca860ac2016-01-04 14:34:37 -0800977
Dan Willemsena96ff642016-06-07 12:34:45 -0700978 cc, _ := m.(*Module)
979 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700980 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700981 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700982 case genSourceDepTag:
983 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
984 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
985 genRule.GeneratedSourceFiles()...)
986 } else {
987 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
988 }
989 case genHeaderDepTag:
990 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
991 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
992 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700993 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700994 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700995 } else {
996 ctx.ModuleErrorf("module %q is not a genrule", name)
997 }
998 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700999 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001000 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001001 return
1002 }
1003
1004 if !a.Enabled() {
1005 ctx.ModuleErrorf("depends on disabled module %q", name)
1006 return
1007 }
1008
Colin Crossa1ad8d12016-06-01 17:09:44 -07001009 if a.Target().Os != ctx.Os() {
1010 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1011 return
1012 }
1013
1014 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1015 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001016 return
1017 }
1018
Dan Willemsena96ff642016-06-07 12:34:45 -07001019 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001020 ctx.ModuleErrorf("module %q missing output file", name)
1021 return
1022 }
1023
1024 if tag == reuseObjTag {
1025 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001026 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001027 return
1028 }
1029
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001030 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001031 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001032 flags := i.exportedFlags()
1033 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001034
1035 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001036 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001037 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001038 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001039
1040 if !linkTypeOk(c, cc) {
1041 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1042 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001043 }
1044
Colin Cross635c3b02016-05-18 15:37:25 -07001045 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001046
1047 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001048 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001049 depPtr = &depPaths.SharedLibs
1050 case lateSharedDepTag:
1051 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001052 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001053 depPtr = &depPaths.StaticLibs
1054 case lateStaticDepTag:
1055 depPtr = &depPaths.LateStaticLibs
1056 case wholeStaticDepTag:
1057 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001058 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001059 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001060 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001061 return
1062 }
1063
1064 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1065 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1066 for i := range missingDeps {
1067 missingDeps[i] += postfix
1068 }
1069 ctx.AddMissingDependencies(missingDeps)
1070 }
1071 depPaths.WholeStaticLibObjFiles =
1072 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1073 case objDepTag:
1074 depPtr = &depPaths.ObjFiles
1075 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001076 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001077 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001078 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001079 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001080 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001081 }
1082
1083 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001084 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001085 }
1086 })
1087
1088 return depPaths
1089}
1090
1091func (c *Module) InstallInData() bool {
1092 if c.installer == nil {
1093 return false
1094 }
1095 return c.installer.inData()
1096}
1097
1098// Compiler
1099
1100type baseCompiler struct {
1101 Properties BaseCompilerProperties
1102}
1103
1104var _ compiler = (*baseCompiler)(nil)
1105
1106func (compiler *baseCompiler) props() []interface{} {
1107 return []interface{}{&compiler.Properties}
1108}
1109
Dan Willemsenb40aab62016-04-20 14:21:14 -07001110func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1111
1112func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1113 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1114 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1115
1116 return deps
1117}
Colin Crossca860ac2016-01-04 14:34:37 -08001118
1119// Create a Flags struct that collects the compile flags from global values,
1120// per-target values, module type values, and per-module Blueprints properties
1121func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1122 toolchain := ctx.toolchain()
1123
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001124 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1125 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1126 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1127 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1128
Colin Crossca860ac2016-01-04 14:34:37 -08001129 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1130 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1131 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1132 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1133 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1134
Colin Cross28344522015-04-22 13:07:53 -07001135 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001136 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1137 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001138 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001139 includeDirsToFlags(localIncludeDirs),
1140 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001141
Colin Crossca860ac2016-01-04 14:34:37 -08001142 if !ctx.noDefaultCompilerFlags() {
1143 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001144 flags.GlobalFlags = append(flags.GlobalFlags,
1145 "${commonGlobalIncludes}",
1146 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001147 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001148 }
1149
1150 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001151 "-I" + android.PathForModuleSrc(ctx).String(),
1152 "-I" + android.PathForModuleOut(ctx).String(),
1153 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001154 }...)
1155 }
1156
Colin Crossca860ac2016-01-04 14:34:37 -08001157 instructionSet := compiler.Properties.Instruction_set
1158 if flags.RequiredInstructionSet != "" {
1159 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001161 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1162 if flags.Clang {
1163 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1164 }
1165 if err != nil {
1166 ctx.ModuleErrorf("%s", err)
1167 }
1168
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001169 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1170
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001171 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001172 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001173
Colin Cross97ba0732015-03-23 17:50:24 -07001174 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001175 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1176 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1177
Colin Cross97ba0732015-03-23 17:50:24 -07001178 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001179 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1180 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001181 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1182 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1183 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001184
1185 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001186 var gccPrefix string
1187 if !ctx.Darwin() {
1188 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1189 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001190
Colin Cross97ba0732015-03-23 17:50:24 -07001191 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1192 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1193 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001194 }
1195
Colin Crossa1ad8d12016-06-01 17:09:44 -07001196 hod := "host"
1197 if ctx.Os().Class == android.Device {
1198 hod = "device"
1199 }
1200
Colin Crossca860ac2016-01-04 14:34:37 -08001201 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001202 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1203
Colin Cross97ba0732015-03-23 17:50:24 -07001204 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001205 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001206 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001207 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001208 toolchain.ClangCflags(),
1209 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001210 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001211
1212 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001213 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001214 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001215 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001216 toolchain.Cflags(),
1217 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001218 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001219 }
1220
Colin Cross7b66f152015-12-15 16:07:43 -08001221 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1222 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1223 }
1224
Colin Crossf6566ed2015-03-24 11:13:38 -07001225 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001226 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001227 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001228 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001229 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001230 }
1231 }
1232
Colin Cross97ba0732015-03-23 17:50:24 -07001233 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001234
Colin Cross97ba0732015-03-23 17:50:24 -07001235 if flags.Clang {
1236 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001237 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001238 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001239 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001240 }
1241
Colin Crossc4bde762015-11-23 16:11:30 -08001242 if flags.Clang {
1243 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1244 } else {
1245 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001246 }
1247
Colin Crossca860ac2016-01-04 14:34:37 -08001248 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001249 if ctx.Host() && !flags.Clang {
1250 // The host GCC doesn't support C++14 (and is deprecated, so likely
1251 // never will). Build these modules with C++11.
1252 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1253 } else {
1254 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1255 }
1256 }
1257
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001258 // We can enforce some rules more strictly in the code we own. strict
1259 // indicates if this is code that we can be stricter with. If we have
1260 // rules that we want to apply to *our* code (but maybe can't for
1261 // vendor/device specific things), we could extend this to be a ternary
1262 // value.
1263 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001264 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001265 strict = false
1266 }
1267
1268 // Can be used to make some annotations stricter for code we can fix
1269 // (such as when we mark functions as deprecated).
1270 if strict {
1271 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1272 }
1273
Colin Cross3f40fa42015-01-30 17:27:36 -08001274 return flags
1275}
1276
Colin Cross635c3b02016-05-18 15:37:25 -07001277func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001278 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001279 objFiles := compiler.compileObjs(ctx, flags, "",
1280 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1281 deps.GeneratedSources, deps.GeneratedHeaders)
1282
Colin Crossca860ac2016-01-04 14:34:37 -08001283 if ctx.Failed() {
1284 return nil
1285 }
1286
Colin Crossca860ac2016-01-04 14:34:37 -08001287 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001288}
1289
1290// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001291func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1292 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001293
Colin Crossca860ac2016-01-04 14:34:37 -08001294 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001295
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001296 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001297 inputFiles = append(inputFiles, extraSrcs...)
1298 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1299
1300 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001301 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001302
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001304}
1305
Colin Crossca860ac2016-01-04 14:34:37 -08001306// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1307type baseLinker struct {
1308 Properties BaseLinkerProperties
1309 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001310 VariantIsShared bool `blueprint:"mutated"`
1311 VariantIsStatic bool `blueprint:"mutated"`
1312 VariantIsStaticBinary bool `blueprint:"mutated"`
1313 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001314 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001315}
1316
Dan Willemsend30e6102016-03-30 17:35:50 -07001317func (linker *baseLinker) begin(ctx BaseModuleContext) {
1318 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001319 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001320 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001321 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001322 }
1323}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001324
Colin Crossca860ac2016-01-04 14:34:37 -08001325func (linker *baseLinker) props() []interface{} {
1326 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001327}
1328
Colin Crossca860ac2016-01-04 14:34:37 -08001329func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1330 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1331 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1332 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001333
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001334 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1335 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1336
Dan Willemsena96ff642016-06-07 12:34:45 -07001337 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001338 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001339 }
1340
Colin Crossf6566ed2015-03-24 11:13:38 -07001341 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001342 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001343 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1344 if !Bool(linker.Properties.No_libgcc) {
1345 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001346 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001347
Colin Crossca860ac2016-01-04 14:34:37 -08001348 if !linker.static() {
1349 if linker.Properties.System_shared_libs != nil {
1350 deps.LateSharedLibs = append(deps.LateSharedLibs,
1351 linker.Properties.System_shared_libs...)
1352 } else if !ctx.sdk() {
1353 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1354 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001355 }
Colin Cross577f6e42015-03-27 18:23:34 -07001356
Colin Crossca860ac2016-01-04 14:34:37 -08001357 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001358 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001359 "libc",
1360 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001361 )
1362 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001363 }
1364
Colin Crossca860ac2016-01-04 14:34:37 -08001365 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001366}
1367
Colin Crossca860ac2016-01-04 14:34:37 -08001368func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1369 toolchain := ctx.toolchain()
1370
Colin Crossca860ac2016-01-04 14:34:37 -08001371 if !ctx.noDefaultCompilerFlags() {
1372 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1373 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1374 }
1375
1376 if flags.Clang {
1377 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1378 } else {
1379 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1380 }
1381
1382 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001383 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1384
Colin Crossca860ac2016-01-04 14:34:37 -08001385 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1386 }
1387 }
1388
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001389 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1390
Dan Willemsen00ced762016-05-10 17:31:21 -07001391 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1392
Dan Willemsend30e6102016-03-30 17:35:50 -07001393 if ctx.Host() && !linker.static() {
1394 rpath_prefix := `\$$ORIGIN/`
1395 if ctx.Darwin() {
1396 rpath_prefix = "@loader_path/"
1397 }
1398
Colin Crossc99deeb2016-04-11 15:06:20 -07001399 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001400 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1401 }
1402 }
1403
Dan Willemsene7174922016-03-30 17:33:52 -07001404 if flags.Clang {
1405 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1406 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001407 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1408 }
1409
1410 return flags
1411}
1412
1413func (linker *baseLinker) static() bool {
1414 return linker.dynamicProperties.VariantIsStatic
1415}
1416
1417func (linker *baseLinker) staticBinary() bool {
1418 return linker.dynamicProperties.VariantIsStaticBinary
1419}
1420
1421func (linker *baseLinker) setStatic(static bool) {
1422 linker.dynamicProperties.VariantIsStatic = static
1423}
1424
Colin Cross16b23492016-01-06 14:41:07 -08001425func (linker *baseLinker) isDependencyRoot() bool {
1426 return false
1427}
1428
Colin Crossca860ac2016-01-04 14:34:37 -08001429type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001430 // Returns true if the build options for the module have selected a static or shared build
1431 buildStatic() bool
1432 buildShared() bool
1433
1434 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001435 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001436
Colin Cross18b6dc52015-04-28 13:20:37 -07001437 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001438 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001439
1440 // Returns whether a module is a static binary
1441 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001442
1443 // Returns true for dependency roots (binaries)
1444 // TODO(ccross): also handle dlopenable libraries
1445 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001446}
1447
Colin Crossca860ac2016-01-04 14:34:37 -08001448type baseInstaller struct {
1449 Properties InstallerProperties
1450
1451 dir string
1452 dir64 string
1453 data bool
1454
Colin Cross635c3b02016-05-18 15:37:25 -07001455 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001456}
1457
1458var _ installer = (*baseInstaller)(nil)
1459
1460func (installer *baseInstaller) props() []interface{} {
1461 return []interface{}{&installer.Properties}
1462}
1463
Colin Cross635c3b02016-05-18 15:37:25 -07001464func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001465 subDir := installer.dir
1466 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1467 subDir = installer.dir64
1468 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001469 if !ctx.Host() && !ctx.Arch().Native {
1470 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1471 }
Colin Cross635c3b02016-05-18 15:37:25 -07001472 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001473 installer.path = ctx.InstallFile(dir, file)
1474}
1475
1476func (installer *baseInstaller) inData() bool {
1477 return installer.data
1478}
1479
Colin Cross3f40fa42015-01-30 17:27:36 -08001480//
1481// Combined static+shared libraries
1482//
1483
Colin Cross919281a2016-04-05 16:42:05 -07001484type flagExporter struct {
1485 Properties FlagExporterProperties
1486
1487 flags []string
1488}
1489
1490func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001491 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1492 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001493}
1494
1495func (f *flagExporter) reexportFlags(flags []string) {
1496 f.flags = append(f.flags, flags...)
1497}
1498
1499func (f *flagExporter) exportedFlags() []string {
1500 return f.flags
1501}
1502
1503type exportedFlagsProducer interface {
1504 exportedFlags() []string
1505}
1506
1507var _ exportedFlagsProducer = (*flagExporter)(nil)
1508
Colin Crossca860ac2016-01-04 14:34:37 -08001509type libraryCompiler struct {
1510 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001511
Colin Crossca860ac2016-01-04 14:34:37 -08001512 linker *libraryLinker
1513 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001514
Colin Crossca860ac2016-01-04 14:34:37 -08001515 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001516 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001517}
1518
Colin Crossca860ac2016-01-04 14:34:37 -08001519var _ compiler = (*libraryCompiler)(nil)
1520
1521func (library *libraryCompiler) props() []interface{} {
1522 props := library.baseCompiler.props()
1523 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001524}
1525
Colin Crossca860ac2016-01-04 14:34:37 -08001526func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1527 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001528
Dan Willemsen490fd492015-11-24 17:53:15 -08001529 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1530 // all code is position independent, and then those warnings get promoted to
1531 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001532 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001533 flags.CFlags = append(flags.CFlags, "-fPIC")
1534 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001535
Colin Crossca860ac2016-01-04 14:34:37 -08001536 if library.linker.static() {
1537 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001538 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001539 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001540 }
1541
Colin Crossca860ac2016-01-04 14:34:37 -08001542 return flags
1543}
1544
Colin Cross635c3b02016-05-18 15:37:25 -07001545func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1546 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001547
Dan Willemsenb40aab62016-04-20 14:21:14 -07001548 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001549 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001550
1551 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001552 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001553 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1554 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001555 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001556 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001557 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1558 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001559 }
1560
1561 return objFiles
1562}
1563
1564type libraryLinker struct {
1565 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001566 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001567 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001568
1569 Properties LibraryLinkerProperties
1570
1571 dynamicProperties struct {
1572 BuildStatic bool `blueprint:"mutated"`
1573 BuildShared bool `blueprint:"mutated"`
1574 }
1575
Colin Crossca860ac2016-01-04 14:34:37 -08001576 // If we're used as a whole_static_lib, our missing dependencies need
1577 // to be given
1578 wholeStaticMissingDeps []string
1579
1580 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001581 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001582}
1583
1584var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001585
1586func (library *libraryLinker) props() []interface{} {
1587 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001588 return append(props,
1589 &library.Properties,
1590 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001591 &library.flagExporter.Properties,
1592 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001593}
1594
1595func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1596 flags = library.baseLinker.flags(ctx, flags)
1597
1598 flags.Nocrt = Bool(library.Properties.Nocrt)
1599
1600 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001601 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001602 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1603 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001604 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001605 sharedFlag = "-shared"
1606 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001607 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001608 flags.LdFlags = append(flags.LdFlags,
1609 "-nostdlib",
1610 "-Wl,--gc-sections",
1611 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001612 }
Colin Cross97ba0732015-03-23 17:50:24 -07001613
Colin Cross0af4b842015-04-30 16:36:18 -07001614 if ctx.Darwin() {
1615 flags.LdFlags = append(flags.LdFlags,
1616 "-dynamiclib",
1617 "-single_module",
1618 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001619 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001620 )
1621 } else {
1622 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001623 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001624 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001625 )
1626 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001627 }
Colin Cross97ba0732015-03-23 17:50:24 -07001628
1629 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001630}
1631
Colin Crossca860ac2016-01-04 14:34:37 -08001632func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1633 deps = library.baseLinker.deps(ctx, deps)
1634 if library.static() {
1635 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1636 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1637 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1638 } else {
1639 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1640 if !ctx.sdk() {
1641 deps.CrtBegin = "crtbegin_so"
1642 deps.CrtEnd = "crtend_so"
1643 } else {
1644 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1645 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1646 }
1647 }
1648 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1649 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1650 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1651 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001652
Colin Crossca860ac2016-01-04 14:34:37 -08001653 return deps
1654}
Colin Cross3f40fa42015-01-30 17:27:36 -08001655
Colin Crossca860ac2016-01-04 14:34:37 -08001656func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001657 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001658
Colin Cross635c3b02016-05-18 15:37:25 -07001659 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001660 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001661
Colin Cross635c3b02016-05-18 15:37:25 -07001662 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001663 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001664
Colin Cross0af4b842015-04-30 16:36:18 -07001665 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001666 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001667 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001668 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001669 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001670
Colin Crossca860ac2016-01-04 14:34:37 -08001671 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001672
1673 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001674
1675 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001676}
1677
Colin Crossca860ac2016-01-04 14:34:37 -08001678func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001679 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001680
Colin Cross635c3b02016-05-18 15:37:25 -07001681 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001682
Colin Cross635c3b02016-05-18 15:37:25 -07001683 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1684 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1685 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1686 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001687 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001688 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001689 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001690 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001691 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001692 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001693 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1694 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001696 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1697 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001698 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001699 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1700 }
1701 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001702 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001703 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1704 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001705 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001706 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001707 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001708 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001709 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001710 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001711 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001712 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001714 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001715 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001716 }
Colin Crossaee540a2015-07-06 17:48:31 -07001717 }
1718
Colin Cross665dce92016-04-28 14:50:03 -07001719 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001720 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001721 ret := outputFile
1722
1723 builderFlags := flagsToBuilderFlags(flags)
1724
1725 if library.stripper.needsStrip(ctx) {
1726 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001727 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001728 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1729 }
1730
Colin Crossca860ac2016-01-04 14:34:37 -08001731 sharedLibs := deps.SharedLibs
1732 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001733
Colin Crossca860ac2016-01-04 14:34:37 -08001734 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1735 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001736 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001737
Colin Cross665dce92016-04-28 14:50:03 -07001738 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001739}
1740
Colin Crossca860ac2016-01-04 14:34:37 -08001741func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001742 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001743
Colin Crossc99deeb2016-04-11 15:06:20 -07001744 objFiles = append(objFiles, deps.ObjFiles...)
1745
Colin Cross635c3b02016-05-18 15:37:25 -07001746 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001747 if library.static() {
1748 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001749 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001750 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001751 }
1752
Colin Cross919281a2016-04-05 16:42:05 -07001753 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001754 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001755
1756 return out
1757}
1758
1759func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001760 return library.dynamicProperties.BuildStatic &&
1761 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001762}
1763
1764func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001765 return library.dynamicProperties.BuildShared &&
1766 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001767}
1768
1769func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1770 return library.wholeStaticMissingDeps
1771}
1772
Colin Crossc99deeb2016-04-11 15:06:20 -07001773func (library *libraryLinker) installable() bool {
1774 return !library.static()
1775}
1776
Colin Crossca860ac2016-01-04 14:34:37 -08001777type libraryInstaller struct {
1778 baseInstaller
1779
Colin Cross30d5f512016-05-03 18:02:42 -07001780 linker *libraryLinker
1781 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001782}
1783
Colin Cross635c3b02016-05-18 15:37:25 -07001784func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001785 if !library.linker.static() {
1786 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001787 }
1788}
1789
Colin Cross30d5f512016-05-03 18:02:42 -07001790func (library *libraryInstaller) inData() bool {
1791 return library.baseInstaller.inData() || library.sanitize.inData()
1792}
1793
Colin Cross635c3b02016-05-18 15:37:25 -07001794func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1795 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001796
Colin Crossca860ac2016-01-04 14:34:37 -08001797 linker := &libraryLinker{}
1798 linker.dynamicProperties.BuildShared = shared
1799 linker.dynamicProperties.BuildStatic = static
1800 module.linker = linker
1801
1802 module.compiler = &libraryCompiler{
1803 linker: linker,
1804 }
1805 module.installer = &libraryInstaller{
1806 baseInstaller: baseInstaller{
1807 dir: "lib",
1808 dir64: "lib64",
1809 },
Colin Cross30d5f512016-05-03 18:02:42 -07001810 linker: linker,
1811 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001812 }
1813
Colin Crossca860ac2016-01-04 14:34:37 -08001814 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001815}
1816
Colin Crossca860ac2016-01-04 14:34:37 -08001817func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001818 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001819 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001820}
1821
Colin Cross3f40fa42015-01-30 17:27:36 -08001822//
1823// Objects (for crt*.o)
1824//
1825
Colin Crossca860ac2016-01-04 14:34:37 -08001826type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001827 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001828}
1829
Colin Crossca860ac2016-01-04 14:34:37 -08001830func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001831 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001832 module.compiler = &baseCompiler{}
1833 module.linker = &objectLinker{}
1834 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001835}
1836
Colin Cross81413472016-04-11 14:37:39 -07001837func (object *objectLinker) props() []interface{} {
1838 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001839}
1840
Colin Crossca860ac2016-01-04 14:34:37 -08001841func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001842
Colin Cross81413472016-04-11 14:37:39 -07001843func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1844 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001845 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001846}
1847
Colin Crossca860ac2016-01-04 14:34:37 -08001848func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001849 if flags.Clang {
1850 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1851 } else {
1852 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1853 }
1854
Colin Crossca860ac2016-01-04 14:34:37 -08001855 return flags
1856}
1857
1858func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001859 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001860
Colin Cross97ba0732015-03-23 17:50:24 -07001861 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001862
Colin Cross635c3b02016-05-18 15:37:25 -07001863 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001864 if len(objFiles) == 1 {
1865 outputFile = objFiles[0]
1866 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001867 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001868 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001869 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001870 }
1871
Colin Cross3f40fa42015-01-30 17:27:36 -08001872 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001873 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001874}
1875
Colin Crossc99deeb2016-04-11 15:06:20 -07001876func (*objectLinker) installable() bool {
1877 return false
1878}
1879
Colin Cross3f40fa42015-01-30 17:27:36 -08001880//
1881// Executables
1882//
1883
Colin Crossca860ac2016-01-04 14:34:37 -08001884type binaryLinker struct {
1885 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001886 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001887
Colin Crossca860ac2016-01-04 14:34:37 -08001888 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001889
Colin Cross635c3b02016-05-18 15:37:25 -07001890 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001891}
1892
Colin Crossca860ac2016-01-04 14:34:37 -08001893var _ linker = (*binaryLinker)(nil)
1894
1895func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001896 return append(binary.baseLinker.props(),
1897 &binary.Properties,
1898 &binary.stripper.StripProperties)
1899
Colin Cross3f40fa42015-01-30 17:27:36 -08001900}
1901
Colin Crossca860ac2016-01-04 14:34:37 -08001902func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001903 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001904}
1905
Colin Crossca860ac2016-01-04 14:34:37 -08001906func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001907 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001908}
1909
Colin Crossca860ac2016-01-04 14:34:37 -08001910func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001911 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001912 if binary.Properties.Stem != "" {
1913 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001914 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001915
Colin Crossca860ac2016-01-04 14:34:37 -08001916 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001917}
1918
Colin Crossca860ac2016-01-04 14:34:37 -08001919func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1920 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001921 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001922 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001923 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001924 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001925 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001926 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001927 }
Colin Crossca860ac2016-01-04 14:34:37 -08001928 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001929 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001930 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001931 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001932 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001933 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001934 }
Colin Crossca860ac2016-01-04 14:34:37 -08001935 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001936 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001937
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001938 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001939 if inList("libc++_static", deps.StaticLibs) {
1940 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001941 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001942 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1943 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1944 // move them to the beginning of deps.LateStaticLibs
1945 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001946 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001947 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001948 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001949 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001950 }
Colin Crossca860ac2016-01-04 14:34:37 -08001951
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001952 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001953 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1954 "from static libs or set static_executable: true")
1955 }
1956 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001957}
1958
Colin Crossc99deeb2016-04-11 15:06:20 -07001959func (*binaryLinker) installable() bool {
1960 return true
1961}
1962
Colin Cross16b23492016-01-06 14:41:07 -08001963func (binary *binaryLinker) isDependencyRoot() bool {
1964 return true
1965}
1966
Colin Cross635c3b02016-05-18 15:37:25 -07001967func NewBinary(hod android.HostOrDeviceSupported) *Module {
1968 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001969 module.compiler = &baseCompiler{}
1970 module.linker = &binaryLinker{}
1971 module.installer = &baseInstaller{
1972 dir: "bin",
1973 }
1974 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001975}
1976
Colin Crossca860ac2016-01-04 14:34:37 -08001977func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001978 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001979 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001980}
1981
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001982func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1983 binary.baseLinker.begin(ctx)
1984
1985 static := Bool(binary.Properties.Static_executable)
1986 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001987 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001988 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1989 static = true
1990 }
1991 } else {
1992 // Static executables are not supported on Darwin or Windows
1993 static = false
1994 }
Colin Cross0af4b842015-04-30 16:36:18 -07001995 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001996 if static {
1997 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001998 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001999 }
2000}
2001
Colin Crossca860ac2016-01-04 14:34:37 -08002002func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2003 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002004
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002005 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002006 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002007 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002008 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2009 }
2010 }
2011
2012 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2013 // all code is position independent, and then those warnings get promoted to
2014 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002015 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002016 flags.CFlags = append(flags.CFlags, "-fpie")
2017 }
Colin Cross97ba0732015-03-23 17:50:24 -07002018
Colin Crossf6566ed2015-03-24 11:13:38 -07002019 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002020 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002021 // Clang driver needs -static to create static executable.
2022 // However, bionic/linker uses -shared to overwrite.
2023 // Linker for x86 targets does not allow coexistance of -static and -shared,
2024 // so we add -static only if -shared is not used.
2025 if !inList("-shared", flags.LdFlags) {
2026 flags.LdFlags = append(flags.LdFlags, "-static")
2027 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002028
Colin Crossed4cf0b2015-03-26 14:43:45 -07002029 flags.LdFlags = append(flags.LdFlags,
2030 "-nostdlib",
2031 "-Bstatic",
2032 "-Wl,--gc-sections",
2033 )
2034
2035 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002036 if flags.DynamicLinker == "" {
2037 flags.DynamicLinker = "/system/bin/linker"
2038 if flags.Toolchain.Is64Bit() {
2039 flags.DynamicLinker += "64"
2040 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002041 }
2042
2043 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002044 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002045 "-nostdlib",
2046 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002047 "-Wl,--gc-sections",
2048 "-Wl,-z,nocopyreloc",
2049 )
2050 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002051 } else {
2052 if binary.staticBinary() {
2053 flags.LdFlags = append(flags.LdFlags, "-static")
2054 }
2055 if ctx.Darwin() {
2056 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2057 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002058 }
2059
Colin Cross97ba0732015-03-23 17:50:24 -07002060 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002061}
2062
Colin Crossca860ac2016-01-04 14:34:37 -08002063func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002064 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002065
Colin Cross665dce92016-04-28 14:50:03 -07002066 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002067 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002068 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002069 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002070 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002071 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002072
Colin Cross635c3b02016-05-18 15:37:25 -07002073 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002074
Colin Crossca860ac2016-01-04 14:34:37 -08002075 sharedLibs := deps.SharedLibs
2076 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2077
Colin Cross16b23492016-01-06 14:41:07 -08002078 if flags.DynamicLinker != "" {
2079 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2080 }
2081
Colin Cross665dce92016-04-28 14:50:03 -07002082 builderFlags := flagsToBuilderFlags(flags)
2083
2084 if binary.stripper.needsStrip(ctx) {
2085 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002086 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002087 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2088 }
2089
2090 if binary.Properties.Prefix_symbols != "" {
2091 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002092 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002093 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2094 flagsToBuilderFlags(flags), afterPrefixSymbols)
2095 }
2096
Colin Crossca860ac2016-01-04 14:34:37 -08002097 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002098 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002099 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002100
2101 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002102}
Colin Cross3f40fa42015-01-30 17:27:36 -08002103
Colin Cross635c3b02016-05-18 15:37:25 -07002104func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002105 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002106}
2107
Colin Cross665dce92016-04-28 14:50:03 -07002108type stripper struct {
2109 StripProperties StripProperties
2110}
2111
2112func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2113 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2114}
2115
Colin Cross635c3b02016-05-18 15:37:25 -07002116func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002117 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002118 if ctx.Darwin() {
2119 TransformDarwinStrip(ctx, in, out)
2120 } else {
2121 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2122 // TODO(ccross): don't add gnu debuglink for user builds
2123 flags.stripAddGnuDebuglink = true
2124 TransformStrip(ctx, in, out, flags)
2125 }
Colin Cross665dce92016-04-28 14:50:03 -07002126}
2127
Colin Cross635c3b02016-05-18 15:37:25 -07002128func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002129 if m, ok := mctx.Module().(*Module); ok {
2130 if test, ok := m.linker.(*testLinker); ok {
2131 if Bool(test.Properties.Test_per_src) {
2132 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2133 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2134 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2135 }
2136 tests := mctx.CreateLocalVariations(testNames...)
2137 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2138 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2139 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2140 }
Colin Cross6002e052015-09-16 16:00:08 -07002141 }
2142 }
2143 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002144}
2145
Colin Crossca860ac2016-01-04 14:34:37 -08002146type testLinker struct {
2147 binaryLinker
2148 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002149}
2150
Dan Willemsend30e6102016-03-30 17:35:50 -07002151func (test *testLinker) begin(ctx BaseModuleContext) {
2152 test.binaryLinker.begin(ctx)
2153
2154 runpath := "../../lib"
2155 if ctx.toolchain().Is64Bit() {
2156 runpath += "64"
2157 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002158 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002159}
2160
Colin Crossca860ac2016-01-04 14:34:37 -08002161func (test *testLinker) props() []interface{} {
2162 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002163}
2164
Colin Crossca860ac2016-01-04 14:34:37 -08002165func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2166 flags = test.binaryLinker.flags(ctx, flags)
2167
2168 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002169 return flags
2170 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002171
Colin Cross97ba0732015-03-23 17:50:24 -07002172 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002173 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002174 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002175
Colin Crossa1ad8d12016-06-01 17:09:44 -07002176 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002177 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002178 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002179 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002180 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2181 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002182 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002183 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2184 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002185 }
2186 } else {
2187 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002188 }
2189
Colin Cross21b9a242015-03-24 14:15:58 -07002190 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002191}
2192
Colin Crossca860ac2016-01-04 14:34:37 -08002193func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2194 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002195 if ctx.sdk() && ctx.Device() {
2196 switch ctx.selectedStl() {
2197 case "ndk_libc++_shared", "ndk_libc++_static":
2198 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2199 case "ndk_libgnustl_static":
2200 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2201 default:
2202 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2203 }
2204 } else {
2205 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2206 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002207 }
Colin Crossca860ac2016-01-04 14:34:37 -08002208 deps = test.binaryLinker.deps(ctx, deps)
2209 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002210}
2211
Colin Crossca860ac2016-01-04 14:34:37 -08002212type testInstaller struct {
2213 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002214}
2215
Colin Cross635c3b02016-05-18 15:37:25 -07002216func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002217 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2218 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2219 installer.baseInstaller.install(ctx, file)
2220}
2221
Colin Cross635c3b02016-05-18 15:37:25 -07002222func NewTest(hod android.HostOrDeviceSupported) *Module {
2223 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002224 module.compiler = &baseCompiler{}
2225 linker := &testLinker{}
2226 linker.Properties.Gtest = true
2227 module.linker = linker
2228 module.installer = &testInstaller{
2229 baseInstaller: baseInstaller{
2230 dir: "nativetest",
2231 dir64: "nativetest64",
2232 data: true,
2233 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002234 }
Colin Crossca860ac2016-01-04 14:34:37 -08002235 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002236}
2237
Colin Crossca860ac2016-01-04 14:34:37 -08002238func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002239 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002240 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002241}
2242
Colin Crossca860ac2016-01-04 14:34:37 -08002243type benchmarkLinker struct {
2244 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002245}
2246
Colin Crossca860ac2016-01-04 14:34:37 -08002247func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2248 deps = benchmark.binaryLinker.deps(ctx, deps)
Colin Cross26832742016-07-11 14:57:56 -07002249 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
Colin Crossca860ac2016-01-04 14:34:37 -08002250 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002251}
2252
Colin Cross635c3b02016-05-18 15:37:25 -07002253func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2254 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002255 module.compiler = &baseCompiler{}
2256 module.linker = &benchmarkLinker{}
2257 module.installer = &baseInstaller{
2258 dir: "nativetest",
2259 dir64: "nativetest64",
2260 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002261 }
Colin Crossca860ac2016-01-04 14:34:37 -08002262 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002263}
2264
Colin Crossca860ac2016-01-04 14:34:37 -08002265func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002266 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002267 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002268}
2269
Colin Cross3f40fa42015-01-30 17:27:36 -08002270//
2271// Static library
2272//
2273
Colin Crossca860ac2016-01-04 14:34:37 -08002274func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002275 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002276 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002277}
2278
2279//
2280// Shared libraries
2281//
2282
Colin Crossca860ac2016-01-04 14:34:37 -08002283func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002284 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002285 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002286}
2287
2288//
2289// Host static library
2290//
2291
Colin Crossca860ac2016-01-04 14:34:37 -08002292func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002293 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002294 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002295}
2296
2297//
2298// Host Shared libraries
2299//
2300
Colin Crossca860ac2016-01-04 14:34:37 -08002301func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002302 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002303 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002304}
2305
2306//
2307// Host Binaries
2308//
2309
Colin Crossca860ac2016-01-04 14:34:37 -08002310func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002311 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002312 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002313}
2314
2315//
Colin Cross1f8f2342015-03-26 16:09:47 -07002316// Host Tests
2317//
2318
Colin Crossca860ac2016-01-04 14:34:37 -08002319func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002320 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002321 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002322}
2323
2324//
Colin Cross2ba19d92015-05-07 15:44:20 -07002325// Host Benchmarks
2326//
2327
Colin Crossca860ac2016-01-04 14:34:37 -08002328func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002329 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002330 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002331}
2332
2333//
Colin Crosscfad1192015-11-02 16:43:11 -08002334// Defaults
2335//
Colin Crossca860ac2016-01-04 14:34:37 -08002336type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002337 android.ModuleBase
2338 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002339}
2340
Colin Cross635c3b02016-05-18 15:37:25 -07002341func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002342}
2343
Colin Crossca860ac2016-01-04 14:34:37 -08002344func defaultsFactory() (blueprint.Module, []interface{}) {
2345 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002346
2347 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002348 &BaseProperties{},
2349 &BaseCompilerProperties{},
2350 &BaseLinkerProperties{},
2351 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002352 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002353 &LibraryLinkerProperties{},
2354 &BinaryLinkerProperties{},
2355 &TestLinkerProperties{},
2356 &UnusedProperties{},
2357 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002358 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002359 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002360 }
2361
Colin Cross635c3b02016-05-18 15:37:25 -07002362 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2363 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002364
Colin Cross635c3b02016-05-18 15:37:25 -07002365 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002366}
2367
2368//
Colin Cross3f40fa42015-01-30 17:27:36 -08002369// Device libraries shipped with gcc
2370//
2371
Colin Crossca860ac2016-01-04 14:34:37 -08002372type toolchainLibraryLinker struct {
2373 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002374}
2375
Colin Crossca860ac2016-01-04 14:34:37 -08002376var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2377
2378func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002379 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002380 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002381}
2382
Colin Crossca860ac2016-01-04 14:34:37 -08002383func (*toolchainLibraryLinker) buildStatic() bool {
2384 return true
2385}
Colin Cross3f40fa42015-01-30 17:27:36 -08002386
Colin Crossca860ac2016-01-04 14:34:37 -08002387func (*toolchainLibraryLinker) buildShared() bool {
2388 return false
2389}
2390
2391func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002392 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002393 module.compiler = &baseCompiler{}
2394 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002395 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002396 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002397}
2398
Colin Crossca860ac2016-01-04 14:34:37 -08002399func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002400 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002401
2402 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002403 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002404
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002405 if flags.Clang {
2406 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2407 }
2408
Colin Crossca860ac2016-01-04 14:34:37 -08002409 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002410
2411 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002412
Colin Crossca860ac2016-01-04 14:34:37 -08002413 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002414}
2415
Colin Crossc99deeb2016-04-11 15:06:20 -07002416func (*toolchainLibraryLinker) installable() bool {
2417 return false
2418}
2419
Dan Albertbe961682015-03-18 23:38:50 -07002420// NDK prebuilt libraries.
2421//
2422// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2423// either (with the exception of the shared STLs, which are installed to the app's directory rather
2424// than to the system image).
2425
Colin Cross635c3b02016-05-18 15:37:25 -07002426func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002427 suffix := ""
2428 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2429 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002430 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002431 suffix = "64"
2432 }
Colin Cross635c3b02016-05-18 15:37:25 -07002433 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002434 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002435}
2436
Colin Cross635c3b02016-05-18 15:37:25 -07002437func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2438 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002439
2440 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2441 // We want to translate to just NAME.EXT
2442 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2443 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002444 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002445}
2446
Colin Crossca860ac2016-01-04 14:34:37 -08002447type ndkPrebuiltObjectLinker struct {
2448 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002449}
2450
Colin Crossca860ac2016-01-04 14:34:37 -08002451func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002452 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002453 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002454}
2455
Colin Crossca860ac2016-01-04 14:34:37 -08002456func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002457 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002458 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002459 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002460 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002461}
2462
Colin Crossca860ac2016-01-04 14:34:37 -08002463func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002464 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002465 // A null build step, but it sets up the output path.
2466 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2467 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2468 }
2469
Colin Crossca860ac2016-01-04 14:34:37 -08002470 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002471}
2472
Colin Crossca860ac2016-01-04 14:34:37 -08002473type ndkPrebuiltLibraryLinker struct {
2474 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002475}
2476
Colin Crossca860ac2016-01-04 14:34:37 -08002477var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2478var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002479
Colin Crossca860ac2016-01-04 14:34:37 -08002480func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002481 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002482}
2483
Colin Crossca860ac2016-01-04 14:34:37 -08002484func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002485 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002486 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002487}
2488
Colin Crossca860ac2016-01-04 14:34:37 -08002489func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002490 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002491 linker := &ndkPrebuiltLibraryLinker{}
2492 linker.dynamicProperties.BuildShared = true
2493 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002494 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002495 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002496}
2497
Colin Crossca860ac2016-01-04 14:34:37 -08002498func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002499 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002500 // A null build step, but it sets up the output path.
2501 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2502 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2503 }
2504
Colin Cross919281a2016-04-05 16:42:05 -07002505 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002506
Colin Crossca860ac2016-01-04 14:34:37 -08002507 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2508 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002509}
2510
2511// The NDK STLs are slightly different from the prebuilt system libraries:
2512// * Are not specific to each platform version.
2513// * The libraries are not in a predictable location for each STL.
2514
Colin Crossca860ac2016-01-04 14:34:37 -08002515type ndkPrebuiltStlLinker struct {
2516 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002517}
2518
Colin Crossca860ac2016-01-04 14:34:37 -08002519func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002520 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002521 linker := &ndkPrebuiltStlLinker{}
2522 linker.dynamicProperties.BuildShared = true
2523 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002524 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002525 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002526}
2527
Colin Crossca860ac2016-01-04 14:34:37 -08002528func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002529 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002530 linker := &ndkPrebuiltStlLinker{}
2531 linker.dynamicProperties.BuildStatic = true
2532 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002533 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002534 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002535}
2536
Colin Cross635c3b02016-05-18 15:37:25 -07002537func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002538 gccVersion := toolchain.GccVersion()
2539 var libDir string
2540 switch stl {
2541 case "libstlport":
2542 libDir = "cxx-stl/stlport/libs"
2543 case "libc++":
2544 libDir = "cxx-stl/llvm-libc++/libs"
2545 case "libgnustl":
2546 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2547 }
2548
2549 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002550 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002551 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002552 }
2553
2554 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002555 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002556}
2557
Colin Crossca860ac2016-01-04 14:34:37 -08002558func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002559 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002560 // A null build step, but it sets up the output path.
2561 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2562 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2563 }
2564
Colin Cross919281a2016-04-05 16:42:05 -07002565 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002566
2567 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002568 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002569 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002570 libExt = staticLibraryExtension
2571 }
2572
2573 stlName := strings.TrimSuffix(libName, "_shared")
2574 stlName = strings.TrimSuffix(stlName, "_static")
2575 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002576 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002577}
2578
Colin Cross635c3b02016-05-18 15:37:25 -07002579func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002580 if m, ok := mctx.Module().(*Module); ok {
2581 if m.linker != nil {
2582 if linker, ok := m.linker.(baseLinkerInterface); ok {
2583 var modules []blueprint.Module
2584 if linker.buildStatic() && linker.buildShared() {
2585 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002586 static := modules[0].(*Module)
2587 shared := modules[1].(*Module)
2588
2589 static.linker.(baseLinkerInterface).setStatic(true)
2590 shared.linker.(baseLinkerInterface).setStatic(false)
2591
2592 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2593 sharedCompiler := shared.compiler.(*libraryCompiler)
2594 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2595 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2596 // Optimize out compiling common .o files twice for static+shared libraries
2597 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2598 sharedCompiler.baseCompiler.Properties.Srcs = nil
2599 }
2600 }
Colin Crossca860ac2016-01-04 14:34:37 -08002601 } else if linker.buildStatic() {
2602 modules = mctx.CreateLocalVariations("static")
2603 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2604 } else if linker.buildShared() {
2605 modules = mctx.CreateLocalVariations("shared")
2606 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2607 } else {
2608 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2609 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002610 }
2611 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002612 }
2613}
Colin Cross74d1ec02015-04-28 13:30:13 -07002614
2615// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2616// modifies the slice contents in place, and returns a subslice of the original slice
2617func lastUniqueElements(list []string) []string {
2618 totalSkip := 0
2619 for i := len(list) - 1; i >= totalSkip; i-- {
2620 skip := 0
2621 for j := i - 1; j >= totalSkip; j-- {
2622 if list[i] == list[j] {
2623 skip++
2624 } else {
2625 list[j+skip] = list[j]
2626 }
2627 }
2628 totalSkip += skip
2629 }
2630 return list[totalSkip:]
2631}
Colin Cross06a931b2015-10-28 17:23:31 -07002632
2633var Bool = proptools.Bool