blob: 6054bc4c2124d9783376ca478c4ec012e02c9d01 [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)
Colin Crossc7a38dc2016-07-12 13:13:09 -070041 soong.RegisterModuleType("cc_test_library", testLibraryFactory)
Colin Crossca860ac2016-01-04 14:34:37 -080042 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
43 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070044
Colin Crossca860ac2016-01-04 14:34:37 -080045 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
47 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
48 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
49 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070050
Colin Crossca860ac2016-01-04 14:34:37 -080051 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
52 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
53 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
54 soong.RegisterModuleType("cc_test_host", testHostFactory)
55 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070056
57 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
58 // the Go initialization order because this package depends on common, so common's init
59 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070060 android.RegisterBottomUpMutator("link", linkageMutator)
61 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
62 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080063
Colin Cross635c3b02016-05-18 15:37:25 -070064 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
65 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080066
Colin Cross635c3b02016-05-18 15:37:25 -070067 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
68 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Cross463a90e2015-06-17 14:20:06 -070069}
70
Colin Cross3f40fa42015-01-30 17:27:36 -080071var (
Colin Cross635c3b02016-05-18 15:37:25 -070072 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080073)
74
75// Flags used by lots of devices. Putting them in package static variables will save bytes in
76// build.ninja so they aren't repeated for every file
77var (
78 commonGlobalCflags = []string{
79 "-DANDROID",
80 "-fmessage-length=0",
81 "-W",
82 "-Wall",
83 "-Wno-unused",
84 "-Winit-self",
85 "-Wpointer-arith",
86
87 // COMMON_RELEASE_CFLAGS
88 "-DNDEBUG",
89 "-UDEBUG",
90 }
91
92 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080093 "-fdiagnostics-color",
94
Colin Cross3f40fa42015-01-30 17:27:36 -080095 // TARGET_ERROR_FLAGS
96 "-Werror=return-type",
97 "-Werror=non-virtual-dtor",
98 "-Werror=address",
99 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -0800100 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -0800101 }
102
103 hostGlobalCflags = []string{}
104
105 commonGlobalCppflags = []string{
106 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700107 }
108
Dan Willemsenbe03f342016-03-03 17:21:04 -0800109 noOverrideGlobalCflags = []string{
110 "-Werror=int-to-pointer-cast",
111 "-Werror=pointer-to-int-cast",
112 }
113
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700114 illegalFlags = []string{
115 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800116 }
Dan Willemsen97704ed2016-07-07 21:40:39 -0700117
118 ndkPrebuiltSharedLibs = []string{
119 "android",
120 "c",
121 "dl",
122 "EGL",
123 "GLESv1_CM",
124 "GLESv2",
125 "GLESv3",
126 "jnigraphics",
127 "log",
128 "mediandk",
129 "m",
130 "OpenMAXAL",
131 "OpenSLES",
132 "stdc++",
133 "vulkan",
134 "z",
135 }
136 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800137)
138
139func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700140 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700141 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
142 }
143
Colin Cross3f40fa42015-01-30 17:27:36 -0800144 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
145 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
146 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800147 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800148
149 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
150
151 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800152 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800153 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800154 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800155 pctx.StaticVariable("hostClangGlobalCflags",
156 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800157 pctx.StaticVariable("noOverrideClangGlobalCflags",
158 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
159
Tim Kilbournf2948142015-03-11 12:03:03 -0700160 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800161 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800162
163 // Everything in this list is a crime against abstraction and dependency tracking.
164 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800165 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700166 []string{
167 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800168 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700169 "hardware/libhardware/include",
170 "hardware/libhardware_legacy/include",
171 "hardware/ril/include",
172 "libnativehelper/include",
173 "frameworks/native/include",
174 "frameworks/native/opengl/include",
175 "frameworks/av/include",
176 "frameworks/base/include",
177 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800178 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
179 // with this, since there is no associated library.
180 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
181 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800182
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700183 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
184 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700185 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700186 return override, nil
187 }
188 return "${clangDefaultBase}", nil
189 })
190 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700191 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700192 return override, nil
193 }
Pirama Arumuga Nainara17442b2016-06-28 11:00:12 -0700194 return "clang-3016494", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700195 })
Colin Cross16b23492016-01-06 14:41:07 -0800196 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
197 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800198}
199
Colin Crossca860ac2016-01-04 14:34:37 -0800200type Deps struct {
201 SharedLibs, LateSharedLibs []string
202 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700203
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700204 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
205
Colin Cross81413472016-04-11 14:37:39 -0700206 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700207
Dan Willemsenb40aab62016-04-20 14:21:14 -0700208 GeneratedSources []string
209 GeneratedHeaders []string
210
Colin Cross97ba0732015-03-23 17:50:24 -0700211 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700212}
213
Colin Crossca860ac2016-01-04 14:34:37 -0800214type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700215 SharedLibs, LateSharedLibs android.Paths
216 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700217
Colin Cross635c3b02016-05-18 15:37:25 -0700218 ObjFiles android.Paths
219 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700220
Colin Cross635c3b02016-05-18 15:37:25 -0700221 GeneratedSources android.Paths
222 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700223
Dan Willemsen76f08272016-07-09 00:14:08 -0700224 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700225
Colin Cross635c3b02016-05-18 15:37:25 -0700226 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700227}
228
Colin Crossca860ac2016-01-04 14:34:37 -0800229type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700230 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
231 AsFlags []string // Flags that apply to assembly source files
232 CFlags []string // Flags that apply to C and C++ source files
233 ConlyFlags []string // Flags that apply to C source files
234 CppFlags []string // Flags that apply to C++ source files
235 YaccFlags []string // Flags that apply to Yacc source files
236 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800237 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700238
239 Nocrt bool
240 Toolchain Toolchain
241 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800242
243 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800244 DynamicLinker string
245
Colin Cross635c3b02016-05-18 15:37:25 -0700246 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700247}
248
Colin Crossca860ac2016-01-04 14:34:37 -0800249type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700250 // 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 -0700251 Srcs []string `android:"arch_variant"`
252
253 // list of source files that should not be used to build the C/C++ module.
254 // This is most useful in the arch/multilib variants to remove non-common files
255 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700256
257 // list of module-specific flags that will be used for C and C++ compiles.
258 Cflags []string `android:"arch_variant"`
259
260 // list of module-specific flags that will be used for C++ compiles
261 Cppflags []string `android:"arch_variant"`
262
263 // list of module-specific flags that will be used for C compiles
264 Conlyflags []string `android:"arch_variant"`
265
266 // list of module-specific flags that will be used for .S compiles
267 Asflags []string `android:"arch_variant"`
268
Colin Crossca860ac2016-01-04 14:34:37 -0800269 // list of module-specific flags that will be used for C and C++ compiles when
270 // compiling with clang
271 Clang_cflags []string `android:"arch_variant"`
272
273 // list of module-specific flags that will be used for .S compiles when
274 // compiling with clang
275 Clang_asflags []string `android:"arch_variant"`
276
Colin Cross7d5136f2015-05-11 13:39:40 -0700277 // list of module-specific flags that will be used for .y and .yy compiles
278 Yaccflags []string
279
Colin Cross7d5136f2015-05-11 13:39:40 -0700280 // the instruction set architecture to use to compile the C/C++
281 // module.
282 Instruction_set string `android:"arch_variant"`
283
284 // list of directories relative to the root of the source tree that will
285 // be added to the include path using -I.
286 // If possible, don't use this. If adding paths from the current directory use
287 // local_include_dirs, if adding paths from other modules use export_include_dirs in
288 // that module.
289 Include_dirs []string `android:"arch_variant"`
290
291 // list of directories relative to the Blueprints file that will
292 // be added to the include path using -I
293 Local_include_dirs []string `android:"arch_variant"`
294
Dan Willemsenb40aab62016-04-20 14:21:14 -0700295 // list of generated sources to compile. These are the names of gensrcs or
296 // genrule modules.
297 Generated_sources []string `android:"arch_variant"`
298
299 // list of generated headers to add to the include path. These are the names
300 // of genrule modules.
301 Generated_headers []string `android:"arch_variant"`
302
Colin Crossca860ac2016-01-04 14:34:37 -0800303 // pass -frtti instead of -fno-rtti
304 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700305
Colin Crossca860ac2016-01-04 14:34:37 -0800306 Debug, Release struct {
307 // list of module-specific flags that will be used for C and C++ compiles in debug or
308 // release builds
309 Cflags []string `android:"arch_variant"`
310 } `android:"arch_variant"`
311}
Colin Cross7d5136f2015-05-11 13:39:40 -0700312
Colin Crossca860ac2016-01-04 14:34:37 -0800313type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700314 // list of modules whose object files should be linked into this module
315 // in their entirety. For static library modules, all of the .o files from the intermediate
316 // directory of the dependency will be linked into this modules .a file. For a shared library,
317 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700318 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700319
320 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700321 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700322
323 // list of modules that should be dynamically linked into this module.
324 Shared_libs []string `android:"arch_variant"`
325
Colin Crossca860ac2016-01-04 14:34:37 -0800326 // list of module-specific flags that will be used for all link steps
327 Ldflags []string `android:"arch_variant"`
328
329 // don't insert default compiler flags into asflags, cflags,
330 // cppflags, conlyflags, ldflags, or include_dirs
331 No_default_compiler_flags *bool
332
333 // list of system libraries that will be dynamically linked to
334 // shared library and executable modules. If unset, generally defaults to libc
335 // and libm. Set to [] to prevent linking against libc and libm.
336 System_shared_libs []string
337
Colin Cross7d5136f2015-05-11 13:39:40 -0700338 // allow the module to contain undefined symbols. By default,
339 // modules cannot contain undefined symbols that are not satisified by their immediate
340 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
341 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700342 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700343
Dan Willemsend67be222015-09-16 15:19:33 -0700344 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700345 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700346
Colin Cross7d5136f2015-05-11 13:39:40 -0700347 // -l arguments to pass to linker for host-provided shared libraries
348 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700349
350 // list of shared libraries to re-export include directories from. Entries must be
351 // present in shared_libs.
352 Export_shared_lib_headers []string `android:"arch_variant"`
353
354 // list of static libraries to re-export include directories from. Entries must be
355 // present in static_libs.
356 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossa89d2e12016-01-11 12:48:37 -0800357
358 // don't link in crt_begin and crt_end. This flag should only be necessary for
359 // compiling crt or libc.
360 Nocrt *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800361}
Colin Cross7d5136f2015-05-11 13:39:40 -0700362
Colin Crossca860ac2016-01-04 14:34:37 -0800363type LibraryCompilerProperties struct {
364 Static struct {
365 Srcs []string `android:"arch_variant"`
366 Exclude_srcs []string `android:"arch_variant"`
367 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700368 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800369 Shared struct {
370 Srcs []string `android:"arch_variant"`
371 Exclude_srcs []string `android:"arch_variant"`
372 Cflags []string `android:"arch_variant"`
373 } `android:"arch_variant"`
374}
375
Colin Cross919281a2016-04-05 16:42:05 -0700376type FlagExporterProperties struct {
377 // list of directories relative to the Blueprints file that will
378 // be added to the include path using -I for any module that links against this module
379 Export_include_dirs []string `android:"arch_variant"`
380}
381
Colin Crossca860ac2016-01-04 14:34:37 -0800382type LibraryLinkerProperties struct {
383 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700384 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800385 Whole_static_libs []string `android:"arch_variant"`
386 Static_libs []string `android:"arch_variant"`
387 Shared_libs []string `android:"arch_variant"`
388 } `android:"arch_variant"`
389 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700390 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800391 Whole_static_libs []string `android:"arch_variant"`
392 Static_libs []string `android:"arch_variant"`
393 Shared_libs []string `android:"arch_variant"`
394 } `android:"arch_variant"`
395
396 // local file name to pass to the linker as --version_script
397 Version_script *string `android:"arch_variant"`
398 // local file name to pass to the linker as -unexported_symbols_list
399 Unexported_symbols_list *string `android:"arch_variant"`
400 // local file name to pass to the linker as -force_symbols_not_weak_list
401 Force_symbols_not_weak_list *string `android:"arch_variant"`
402 // local file name to pass to the linker as -force_symbols_weak_list
403 Force_symbols_weak_list *string `android:"arch_variant"`
404
Dan Willemsen648c8ae2016-07-21 16:42:14 -0700405 // rename host libraries to prevent overlap with system installed libraries
406 Unique_host_soname *bool
407
Colin Cross16b23492016-01-06 14:41:07 -0800408 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800409}
410
411type BinaryLinkerProperties struct {
412 // compile executable with -static
Dan Willemsen75ab8082016-07-12 15:36:34 -0700413 Static_executable *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800414
415 // set the name of the output
416 Stem string `android:"arch_variant"`
417
418 // append to the name of the output
419 Suffix string `android:"arch_variant"`
420
421 // if set, add an extra objcopy --prefix-symbols= step
422 Prefix_symbols string
423}
424
425type TestLinkerProperties struct {
426 // if set, build against the gtest library. Defaults to true.
427 Gtest bool
428
429 // Create a separate binary for each source file. Useful when there is
430 // global state that can not be torn down and reset between each test suite.
431 Test_per_src *bool
432}
433
Colin Cross81413472016-04-11 14:37:39 -0700434type ObjectLinkerProperties struct {
435 // names of other cc_object modules to link into this module using partial linking
436 Objs []string `android:"arch_variant"`
437}
438
Colin Crossca860ac2016-01-04 14:34:37 -0800439// Properties used to compile all C or C++ modules
440type BaseProperties struct {
441 // compile module with clang instead of gcc
442 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700443
444 // Minimum sdk version supported when compiling against the ndk
445 Sdk_version string
446
Colin Crossca860ac2016-01-04 14:34:37 -0800447 // don't insert default compiler flags into asflags, cflags,
448 // cppflags, conlyflags, ldflags, or include_dirs
449 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700450
451 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700452 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800453}
454
455type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700456 // install to a subdirectory of the default install path for the module
457 Relative_install_path string
Colin Cross3854a602016-01-11 12:49:11 -0800458
459 // install symlinks to the module
460 Symlinks []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700461}
462
Colin Cross665dce92016-04-28 14:50:03 -0700463type StripProperties struct {
464 Strip struct {
465 None bool
466 Keep_symbols bool
467 }
468}
469
Colin Crossca860ac2016-01-04 14:34:37 -0800470type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700471 Native_coverage *bool
472 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700473 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800474}
475
Colin Crossca860ac2016-01-04 14:34:37 -0800476type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800477 static() bool
478 staticBinary() bool
479 clang() bool
480 toolchain() Toolchain
481 noDefaultCompilerFlags() bool
482 sdk() bool
483 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700484 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800485}
486
487type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700488 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800489 ModuleContextIntf
490}
491
492type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700493 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800494 ModuleContextIntf
495}
496
497type Customizer interface {
498 CustomizeProperties(BaseModuleContext)
499 Properties() []interface{}
500}
501
502type feature interface {
503 begin(ctx BaseModuleContext)
504 deps(ctx BaseModuleContext, deps Deps) Deps
505 flags(ctx ModuleContext, flags Flags) Flags
506 props() []interface{}
507}
508
509type compiler interface {
510 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700511 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800512}
513
514type linker interface {
515 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700516 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700517 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800518}
519
520type installer interface {
521 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700522 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800523 inData() bool
524}
525
Colin Crossc99deeb2016-04-11 15:06:20 -0700526type dependencyTag struct {
527 blueprint.BaseDependencyTag
528 name string
529 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700530
531 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700532}
533
534var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700535 sharedDepTag = dependencyTag{name: "shared", library: true}
536 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
537 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
538 staticDepTag = dependencyTag{name: "static", library: true}
539 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
540 lateStaticDepTag = dependencyTag{name: "late static", library: true}
541 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
542 genSourceDepTag = dependencyTag{name: "gen source"}
543 genHeaderDepTag = dependencyTag{name: "gen header"}
544 objDepTag = dependencyTag{name: "obj"}
545 crtBeginDepTag = dependencyTag{name: "crtbegin"}
546 crtEndDepTag = dependencyTag{name: "crtend"}
547 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700548)
549
Colin Crossca860ac2016-01-04 14:34:37 -0800550// Module contains the properties and members used by all C/C++ module types, and implements
551// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
552// to construct the output file. Behavior can be customized with a Customizer interface
553type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700554 android.ModuleBase
555 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700556
Colin Crossca860ac2016-01-04 14:34:37 -0800557 Properties BaseProperties
558 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700559
Colin Crossca860ac2016-01-04 14:34:37 -0800560 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700561 hod android.HostOrDeviceSupported
562 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700563
Colin Crossca860ac2016-01-04 14:34:37 -0800564 // delegates, initialize before calling Init
565 customizer Customizer
566 features []feature
567 compiler compiler
568 linker linker
569 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700570 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800571 sanitize *sanitize
572
573 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700574
Colin Cross635c3b02016-05-18 15:37:25 -0700575 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800576
577 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700578}
579
Colin Crossca860ac2016-01-04 14:34:37 -0800580func (c *Module) Init() (blueprint.Module, []interface{}) {
581 props := []interface{}{&c.Properties, &c.unused}
582 if c.customizer != nil {
583 props = append(props, c.customizer.Properties()...)
584 }
585 if c.compiler != nil {
586 props = append(props, c.compiler.props()...)
587 }
588 if c.linker != nil {
589 props = append(props, c.linker.props()...)
590 }
591 if c.installer != nil {
592 props = append(props, c.installer.props()...)
593 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700594 if c.stl != nil {
595 props = append(props, c.stl.props()...)
596 }
Colin Cross16b23492016-01-06 14:41:07 -0800597 if c.sanitize != nil {
598 props = append(props, c.sanitize.props()...)
599 }
Colin Crossca860ac2016-01-04 14:34:37 -0800600 for _, feature := range c.features {
601 props = append(props, feature.props()...)
602 }
Colin Crossc472d572015-03-17 15:06:21 -0700603
Colin Cross635c3b02016-05-18 15:37:25 -0700604 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700605
Colin Cross635c3b02016-05-18 15:37:25 -0700606 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700607}
608
Colin Crossca860ac2016-01-04 14:34:37 -0800609type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700610 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800611 moduleContextImpl
612}
613
614type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700615 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800616 moduleContextImpl
617}
618
619type moduleContextImpl struct {
620 mod *Module
621 ctx BaseModuleContext
622}
623
Colin Crossca860ac2016-01-04 14:34:37 -0800624func (ctx *moduleContextImpl) clang() bool {
625 return ctx.mod.clang(ctx.ctx)
626}
627
628func (ctx *moduleContextImpl) toolchain() Toolchain {
629 return ctx.mod.toolchain(ctx.ctx)
630}
631
632func (ctx *moduleContextImpl) static() bool {
633 if ctx.mod.linker == nil {
634 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
635 }
636 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
637 return linker.static()
638 } else {
639 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
640 }
641}
642
643func (ctx *moduleContextImpl) staticBinary() bool {
644 if ctx.mod.linker == nil {
645 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
646 }
647 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
648 return linker.staticBinary()
649 } else {
650 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
651 }
652}
653
654func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
655 return Bool(ctx.mod.Properties.No_default_compiler_flags)
656}
657
658func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700659 if ctx.ctx.Device() {
660 return ctx.mod.Properties.Sdk_version != ""
661 }
662 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800663}
664
665func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700666 if ctx.ctx.Device() {
667 return ctx.mod.Properties.Sdk_version
668 }
669 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800670}
671
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700672func (ctx *moduleContextImpl) selectedStl() string {
673 if stl := ctx.mod.stl; stl != nil {
674 return stl.Properties.SelectedStl
675 }
676 return ""
677}
678
Colin Cross635c3b02016-05-18 15:37:25 -0700679func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800680 return &Module{
681 hod: hod,
682 multilib: multilib,
683 }
684}
685
Colin Cross635c3b02016-05-18 15:37:25 -0700686func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800687 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700688 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800689 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800690 return module
691}
692
Colin Cross635c3b02016-05-18 15:37:25 -0700693func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800694 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700695 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800696 moduleContextImpl: moduleContextImpl{
697 mod: c,
698 },
699 }
700 ctx.ctx = ctx
701
702 flags := Flags{
703 Toolchain: c.toolchain(ctx),
704 Clang: c.clang(ctx),
705 }
Colin Crossca860ac2016-01-04 14:34:37 -0800706 if c.compiler != nil {
707 flags = c.compiler.flags(ctx, flags)
708 }
709 if c.linker != nil {
710 flags = c.linker.flags(ctx, flags)
711 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700712 if c.stl != nil {
713 flags = c.stl.flags(ctx, flags)
714 }
Colin Cross16b23492016-01-06 14:41:07 -0800715 if c.sanitize != nil {
716 flags = c.sanitize.flags(ctx, flags)
717 }
Colin Crossca860ac2016-01-04 14:34:37 -0800718 for _, feature := range c.features {
719 flags = feature.flags(ctx, flags)
720 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800721 if ctx.Failed() {
722 return
723 }
724
Colin Crossca860ac2016-01-04 14:34:37 -0800725 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
726 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
727 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800728
Colin Crossca860ac2016-01-04 14:34:37 -0800729 // Optimization to reduce size of build.ninja
730 // Replace the long list of flags for each file with a module-local variable
731 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
732 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
733 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
734 flags.CFlags = []string{"$cflags"}
735 flags.CppFlags = []string{"$cppflags"}
736 flags.AsFlags = []string{"$asflags"}
737
Colin Crossc99deeb2016-04-11 15:06:20 -0700738 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800739 if ctx.Failed() {
740 return
741 }
742
Dan Willemsen76f08272016-07-09 00:14:08 -0700743 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700744
Colin Cross635c3b02016-05-18 15:37:25 -0700745 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800746 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700747 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if ctx.Failed() {
749 return
750 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800751 }
752
Colin Crossca860ac2016-01-04 14:34:37 -0800753 if c.linker != nil {
754 outputFile := c.linker.link(ctx, flags, deps, objFiles)
755 if ctx.Failed() {
756 return
757 }
Colin Cross635c3b02016-05-18 15:37:25 -0700758 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700759
Colin Crossc99deeb2016-04-11 15:06:20 -0700760 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800761 c.installer.install(ctx, outputFile)
762 if ctx.Failed() {
763 return
764 }
765 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700766 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800767}
768
Colin Crossca860ac2016-01-04 14:34:37 -0800769func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
770 if c.cachedToolchain == nil {
771 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700772 os := ctx.Os()
773 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800774 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700775 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800776 return nil
777 }
778 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800779 }
Colin Crossca860ac2016-01-04 14:34:37 -0800780 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800781}
782
Colin Crossca860ac2016-01-04 14:34:37 -0800783func (c *Module) begin(ctx BaseModuleContext) {
784 if c.compiler != nil {
785 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700786 }
Colin Crossca860ac2016-01-04 14:34:37 -0800787 if c.linker != nil {
788 c.linker.begin(ctx)
789 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700790 if c.stl != nil {
791 c.stl.begin(ctx)
792 }
Colin Cross16b23492016-01-06 14:41:07 -0800793 if c.sanitize != nil {
794 c.sanitize.begin(ctx)
795 }
Colin Crossca860ac2016-01-04 14:34:37 -0800796 for _, feature := range c.features {
797 feature.begin(ctx)
798 }
799}
800
Colin Crossc99deeb2016-04-11 15:06:20 -0700801func (c *Module) deps(ctx BaseModuleContext) Deps {
802 deps := Deps{}
803
804 if c.compiler != nil {
805 deps = c.compiler.deps(ctx, deps)
806 }
807 if c.linker != nil {
808 deps = c.linker.deps(ctx, deps)
809 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700810 if c.stl != nil {
811 deps = c.stl.deps(ctx, deps)
812 }
Colin Cross16b23492016-01-06 14:41:07 -0800813 if c.sanitize != nil {
814 deps = c.sanitize.deps(ctx, deps)
815 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700816 for _, feature := range c.features {
817 deps = feature.deps(ctx, deps)
818 }
819
820 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
821 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
822 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
823 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
824 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
825
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700826 for _, lib := range deps.ReexportSharedLibHeaders {
827 if !inList(lib, deps.SharedLibs) {
828 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
829 }
830 }
831
832 for _, lib := range deps.ReexportStaticLibHeaders {
833 if !inList(lib, deps.StaticLibs) {
834 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
835 }
836 }
837
Colin Crossc99deeb2016-04-11 15:06:20 -0700838 return deps
839}
840
Colin Cross635c3b02016-05-18 15:37:25 -0700841func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800842 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700843 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800844 moduleContextImpl: moduleContextImpl{
845 mod: c,
846 },
847 }
848 ctx.ctx = ctx
849
850 if c.customizer != nil {
851 c.customizer.CustomizeProperties(ctx)
852 }
853
854 c.begin(ctx)
855
Colin Crossc99deeb2016-04-11 15:06:20 -0700856 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800857
Colin Crossb5bc4b42016-07-11 16:11:59 -0700858 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
859 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700860
861 if ctx.sdk() {
862 version := "." + ctx.sdkVersion()
863
864 rewriteNdkLibs := func(list []string) []string {
865 for i, entry := range list {
866 if inList(entry, ndkPrebuiltSharedLibraries) {
867 list[i] = "ndk_" + entry + version
868 }
869 }
870 return list
871 }
872
873 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
874 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
875 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700876
877 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
878 deps.WholeStaticLibs...)
879
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700880 for _, lib := range deps.StaticLibs {
881 depTag := staticDepTag
882 if inList(lib, deps.ReexportStaticLibHeaders) {
883 depTag = staticExportDepTag
884 }
Colin Cross15a0d462016-07-14 14:49:58 -0700885 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700886 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700887
888 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
889 deps.LateStaticLibs...)
890
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700891 for _, lib := range deps.SharedLibs {
892 depTag := sharedDepTag
893 if inList(lib, deps.ReexportSharedLibHeaders) {
894 depTag = sharedExportDepTag
895 }
Colin Cross15a0d462016-07-14 14:49:58 -0700896 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700897 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700898
899 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
900 deps.LateSharedLibs...)
901
Colin Cross68861832016-07-08 10:41:41 -0700902 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
903 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700904
Colin Cross68861832016-07-08 10:41:41 -0700905 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700906
907 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700908 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800909 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700910 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700911 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700912 }
Colin Cross6362e272015-10-29 15:25:03 -0700913}
Colin Cross21b9a242015-03-24 14:15:58 -0700914
Colin Cross635c3b02016-05-18 15:37:25 -0700915func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700916 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700917 c.depsMutator(ctx)
918 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800919}
920
Colin Crossca860ac2016-01-04 14:34:37 -0800921func (c *Module) clang(ctx BaseModuleContext) bool {
922 clang := Bool(c.Properties.Clang)
923
924 if c.Properties.Clang == nil {
925 if ctx.Host() {
926 clang = true
927 }
928
929 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
930 clang = true
931 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800932 }
Colin Cross28344522015-04-22 13:07:53 -0700933
Colin Crossca860ac2016-01-04 14:34:37 -0800934 if !c.toolchain(ctx).ClangSupported() {
935 clang = false
936 }
937
938 return clang
939}
940
Colin Crossc99deeb2016-04-11 15:06:20 -0700941// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700942func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800943 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800944
Dan Willemsena96ff642016-06-07 12:34:45 -0700945 // Whether a module can link to another module, taking into
946 // account NDK linking.
947 linkTypeOk := func(from, to *Module) bool {
948 if from.Target().Os != android.Android {
949 // Host code is not restricted
950 return true
951 }
952 if from.Properties.Sdk_version == "" {
953 // Platform code can link to anything
954 return true
955 }
956 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
957 // These are always allowed
958 return true
959 }
960 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
961 // These are allowed, but don't set sdk_version
962 return true
963 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700964 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
965 // These are allowed, but don't set sdk_version
966 return true
967 }
968 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700969 }
970
Colin Crossc99deeb2016-04-11 15:06:20 -0700971 ctx.VisitDirectDeps(func(m blueprint.Module) {
972 name := ctx.OtherModuleName(m)
973 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800974
Colin Cross635c3b02016-05-18 15:37:25 -0700975 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700976 if a == nil {
977 ctx.ModuleErrorf("module %q not an android module", name)
978 return
Colin Crossca860ac2016-01-04 14:34:37 -0800979 }
Colin Crossca860ac2016-01-04 14:34:37 -0800980
Dan Willemsena96ff642016-06-07 12:34:45 -0700981 cc, _ := m.(*Module)
982 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700983 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700984 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700985 case genSourceDepTag:
986 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
987 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
988 genRule.GeneratedSourceFiles()...)
989 } else {
990 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
991 }
992 case genHeaderDepTag:
993 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
994 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
995 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700996 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700997 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700998 } else {
999 ctx.ModuleErrorf("module %q is not a genrule", name)
1000 }
1001 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001002 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001003 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001004 return
1005 }
1006
1007 if !a.Enabled() {
1008 ctx.ModuleErrorf("depends on disabled module %q", name)
1009 return
1010 }
1011
Colin Crossa1ad8d12016-06-01 17:09:44 -07001012 if a.Target().Os != ctx.Os() {
1013 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1014 return
1015 }
1016
1017 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1018 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001019 return
1020 }
1021
Dan Willemsena96ff642016-06-07 12:34:45 -07001022 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001023 ctx.ModuleErrorf("module %q missing output file", name)
1024 return
1025 }
1026
1027 if tag == reuseObjTag {
1028 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001029 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001030 return
1031 }
1032
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001033 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001034 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001035 flags := i.exportedFlags()
1036 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001037
1038 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001039 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001040 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001041 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001042
1043 if !linkTypeOk(c, cc) {
1044 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1045 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001046 }
1047
Colin Cross635c3b02016-05-18 15:37:25 -07001048 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001049
1050 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001051 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001052 depPtr = &depPaths.SharedLibs
1053 case lateSharedDepTag:
1054 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001055 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001056 depPtr = &depPaths.StaticLibs
1057 case lateStaticDepTag:
1058 depPtr = &depPaths.LateStaticLibs
1059 case wholeStaticDepTag:
1060 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -07001061 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -07001062 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001063 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001064 return
1065 }
1066
1067 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1068 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1069 for i := range missingDeps {
1070 missingDeps[i] += postfix
1071 }
1072 ctx.AddMissingDependencies(missingDeps)
1073 }
1074 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -07001075 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001076 case objDepTag:
1077 depPtr = &depPaths.ObjFiles
1078 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001079 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001080 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001081 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001082 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001083 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001084 }
1085
1086 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001087 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001088 }
1089 })
1090
1091 return depPaths
1092}
1093
1094func (c *Module) InstallInData() bool {
1095 if c.installer == nil {
1096 return false
1097 }
1098 return c.installer.inData()
1099}
1100
1101// Compiler
1102
1103type baseCompiler struct {
1104 Properties BaseCompilerProperties
1105}
1106
1107var _ compiler = (*baseCompiler)(nil)
1108
1109func (compiler *baseCompiler) props() []interface{} {
1110 return []interface{}{&compiler.Properties}
1111}
1112
Dan Willemsenb40aab62016-04-20 14:21:14 -07001113func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1114
1115func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1116 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1117 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1118
1119 return deps
1120}
Colin Crossca860ac2016-01-04 14:34:37 -08001121
1122// Create a Flags struct that collects the compile flags from global values,
1123// per-target values, module type values, and per-module Blueprints properties
1124func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1125 toolchain := ctx.toolchain()
1126
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001127 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1128 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1129 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1130 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1131
Colin Crossca860ac2016-01-04 14:34:37 -08001132 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1133 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1134 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1135 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1136 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1137
Colin Cross28344522015-04-22 13:07:53 -07001138 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001139 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1140 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001141 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001142 includeDirsToFlags(localIncludeDirs),
1143 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001144
Colin Crossca860ac2016-01-04 14:34:37 -08001145 if !ctx.noDefaultCompilerFlags() {
1146 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001147 flags.GlobalFlags = append(flags.GlobalFlags,
1148 "${commonGlobalIncludes}",
1149 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001150 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001151 }
1152
1153 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001154 "-I" + android.PathForModuleSrc(ctx).String(),
1155 "-I" + android.PathForModuleOut(ctx).String(),
1156 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001157 }...)
1158 }
1159
Colin Crossca860ac2016-01-04 14:34:37 -08001160 instructionSet := compiler.Properties.Instruction_set
1161 if flags.RequiredInstructionSet != "" {
1162 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001163 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001164 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1165 if flags.Clang {
1166 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1167 }
1168 if err != nil {
1169 ctx.ModuleErrorf("%s", err)
1170 }
1171
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001172 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1173
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001174 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001175 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001176
Colin Cross97ba0732015-03-23 17:50:24 -07001177 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001178 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1179 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1180
Colin Cross97ba0732015-03-23 17:50:24 -07001181 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001182 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1183 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001184 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1185 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1186 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001187
1188 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001189 var gccPrefix string
1190 if !ctx.Darwin() {
1191 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1192 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001193
Colin Cross97ba0732015-03-23 17:50:24 -07001194 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1195 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1196 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001197 }
1198
Colin Crossa1ad8d12016-06-01 17:09:44 -07001199 hod := "host"
1200 if ctx.Os().Class == android.Device {
1201 hod = "device"
1202 }
1203
Colin Crossca860ac2016-01-04 14:34:37 -08001204 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001205 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1206
Colin Cross97ba0732015-03-23 17:50:24 -07001207 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001208 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001209 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001210 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001211 toolchain.ClangCflags(),
1212 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001213 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001214
1215 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001216 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001217 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001218 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001219 toolchain.Cflags(),
1220 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001221 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001222 }
1223
Colin Cross7b66f152015-12-15 16:07:43 -08001224 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1225 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1226 }
1227
Colin Crossf6566ed2015-03-24 11:13:38 -07001228 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001229 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001230 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001231 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001232 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001233 }
1234 }
1235
Colin Cross97ba0732015-03-23 17:50:24 -07001236 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001237
Colin Cross97ba0732015-03-23 17:50:24 -07001238 if flags.Clang {
1239 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001240 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001241 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001242 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001243 }
1244
Colin Crossc4bde762015-11-23 16:11:30 -08001245 if flags.Clang {
1246 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1247 } else {
1248 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001249 }
1250
Colin Crossca860ac2016-01-04 14:34:37 -08001251 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001252 if ctx.Host() && !flags.Clang {
1253 // The host GCC doesn't support C++14 (and is deprecated, so likely
1254 // never will). Build these modules with C++11.
1255 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1256 } else {
1257 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1258 }
1259 }
1260
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001261 // We can enforce some rules more strictly in the code we own. strict
1262 // indicates if this is code that we can be stricter with. If we have
1263 // rules that we want to apply to *our* code (but maybe can't for
1264 // vendor/device specific things), we could extend this to be a ternary
1265 // value.
1266 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001267 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001268 strict = false
1269 }
1270
1271 // Can be used to make some annotations stricter for code we can fix
1272 // (such as when we mark functions as deprecated).
1273 if strict {
1274 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1275 }
1276
Colin Cross3f40fa42015-01-30 17:27:36 -08001277 return flags
1278}
1279
Colin Cross635c3b02016-05-18 15:37:25 -07001280func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001281 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001282 objFiles := compiler.compileObjs(ctx, flags, "",
1283 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1284 deps.GeneratedSources, deps.GeneratedHeaders)
1285
Colin Crossca860ac2016-01-04 14:34:37 -08001286 if ctx.Failed() {
1287 return nil
1288 }
1289
Colin Crossca860ac2016-01-04 14:34:37 -08001290 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001291}
1292
1293// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001294func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1295 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001296
Colin Crossca860ac2016-01-04 14:34:37 -08001297 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001298
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001299 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001300 inputFiles = append(inputFiles, extraSrcs...)
1301 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1302
1303 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001304 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001305
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001306 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001307}
1308
Colin Crossca860ac2016-01-04 14:34:37 -08001309// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1310type baseLinker struct {
1311 Properties BaseLinkerProperties
1312 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001313 VariantIsShared bool `blueprint:"mutated"`
1314 VariantIsStatic bool `blueprint:"mutated"`
1315 VariantIsStaticBinary bool `blueprint:"mutated"`
1316 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001317 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001318}
1319
Dan Willemsend30e6102016-03-30 17:35:50 -07001320func (linker *baseLinker) begin(ctx BaseModuleContext) {
1321 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001322 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001323 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001324 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001325 }
1326}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001327
Colin Crossca860ac2016-01-04 14:34:37 -08001328func (linker *baseLinker) props() []interface{} {
1329 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001330}
1331
Colin Crossca860ac2016-01-04 14:34:37 -08001332func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1333 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1334 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1335 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001336
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001337 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1338 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1339
Dan Willemsena96ff642016-06-07 12:34:45 -07001340 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Stephen Hines10347862016-07-18 15:54:54 -07001341 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001342 }
1343
Colin Crossf6566ed2015-03-24 11:13:38 -07001344 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001345 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001346 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1347 if !Bool(linker.Properties.No_libgcc) {
1348 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001349 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001350
Colin Crossca860ac2016-01-04 14:34:37 -08001351 if !linker.static() {
1352 if linker.Properties.System_shared_libs != nil {
1353 deps.LateSharedLibs = append(deps.LateSharedLibs,
1354 linker.Properties.System_shared_libs...)
1355 } else if !ctx.sdk() {
1356 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1357 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001358 }
Colin Cross577f6e42015-03-27 18:23:34 -07001359
Colin Crossca860ac2016-01-04 14:34:37 -08001360 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001361 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001362 "libc",
1363 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001364 )
1365 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001366 }
1367
Colin Crossca860ac2016-01-04 14:34:37 -08001368 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001369}
1370
Colin Crossca860ac2016-01-04 14:34:37 -08001371func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1372 toolchain := ctx.toolchain()
1373
Colin Crossa89d2e12016-01-11 12:48:37 -08001374 flags.Nocrt = Bool(linker.Properties.Nocrt)
1375
Colin Crossca860ac2016-01-04 14:34:37 -08001376 if !ctx.noDefaultCompilerFlags() {
1377 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1378 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1379 }
1380
1381 if flags.Clang {
1382 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1383 } else {
1384 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1385 }
1386
1387 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001388 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1389
Colin Crossca860ac2016-01-04 14:34:37 -08001390 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1391 }
1392 }
1393
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001394 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1395
Dan Willemsen00ced762016-05-10 17:31:21 -07001396 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1397
Dan Willemsend30e6102016-03-30 17:35:50 -07001398 if ctx.Host() && !linker.static() {
1399 rpath_prefix := `\$$ORIGIN/`
1400 if ctx.Darwin() {
1401 rpath_prefix = "@loader_path/"
1402 }
1403
Colin Crossc99deeb2016-04-11 15:06:20 -07001404 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001405 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1406 }
1407 }
1408
Dan Willemsene7174922016-03-30 17:33:52 -07001409 if flags.Clang {
1410 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1411 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001412 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1413 }
1414
1415 return flags
1416}
1417
1418func (linker *baseLinker) static() bool {
1419 return linker.dynamicProperties.VariantIsStatic
1420}
1421
1422func (linker *baseLinker) staticBinary() bool {
1423 return linker.dynamicProperties.VariantIsStaticBinary
1424}
1425
1426func (linker *baseLinker) setStatic(static bool) {
1427 linker.dynamicProperties.VariantIsStatic = static
1428}
1429
Colin Cross16b23492016-01-06 14:41:07 -08001430func (linker *baseLinker) isDependencyRoot() bool {
1431 return false
1432}
1433
Colin Crossca860ac2016-01-04 14:34:37 -08001434type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001435 // Returns true if the build options for the module have selected a static or shared build
1436 buildStatic() bool
1437 buildShared() bool
1438
1439 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001440 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001441
Colin Cross18b6dc52015-04-28 13:20:37 -07001442 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001443 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001444
1445 // Returns whether a module is a static binary
1446 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001447
1448 // Returns true for dependency roots (binaries)
1449 // TODO(ccross): also handle dlopenable libraries
1450 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001451}
1452
Colin Crossca860ac2016-01-04 14:34:37 -08001453type baseInstaller struct {
1454 Properties InstallerProperties
1455
1456 dir string
1457 dir64 string
1458 data bool
1459
Colin Cross635c3b02016-05-18 15:37:25 -07001460 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001461}
1462
1463var _ installer = (*baseInstaller)(nil)
1464
1465func (installer *baseInstaller) props() []interface{} {
1466 return []interface{}{&installer.Properties}
1467}
1468
Colin Cross635c3b02016-05-18 15:37:25 -07001469func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001470 subDir := installer.dir
1471 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1472 subDir = installer.dir64
1473 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001474 if !ctx.Host() && !ctx.Arch().Native {
1475 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1476 }
Colin Cross635c3b02016-05-18 15:37:25 -07001477 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001478 installer.path = ctx.InstallFile(dir, file)
Colin Cross3854a602016-01-11 12:49:11 -08001479 for _, symlink := range installer.Properties.Symlinks {
1480 ctx.InstallSymlink(dir, symlink, installer.path)
1481 }
Colin Crossca860ac2016-01-04 14:34:37 -08001482}
1483
1484func (installer *baseInstaller) inData() bool {
1485 return installer.data
1486}
1487
Colin Cross3f40fa42015-01-30 17:27:36 -08001488//
1489// Combined static+shared libraries
1490//
1491
Colin Cross919281a2016-04-05 16:42:05 -07001492type flagExporter struct {
1493 Properties FlagExporterProperties
1494
1495 flags []string
1496}
1497
1498func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001499 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001500 for _, dir := range includeDirs.Strings() {
Colin Crossf87b2612016-07-13 18:55:43 -07001501 f.flags = append(f.flags, inc+dir)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001502 }
Colin Cross919281a2016-04-05 16:42:05 -07001503}
1504
1505func (f *flagExporter) reexportFlags(flags []string) {
1506 f.flags = append(f.flags, flags...)
1507}
1508
1509func (f *flagExporter) exportedFlags() []string {
1510 return f.flags
1511}
1512
1513type exportedFlagsProducer interface {
1514 exportedFlags() []string
1515}
1516
1517var _ exportedFlagsProducer = (*flagExporter)(nil)
1518
Colin Crossca860ac2016-01-04 14:34:37 -08001519type libraryCompiler struct {
1520 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001521
Colin Crossca860ac2016-01-04 14:34:37 -08001522 linker *libraryLinker
1523 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001524
Colin Crossca860ac2016-01-04 14:34:37 -08001525 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001526 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001527}
1528
Colin Crossca860ac2016-01-04 14:34:37 -08001529var _ compiler = (*libraryCompiler)(nil)
1530
1531func (library *libraryCompiler) props() []interface{} {
1532 props := library.baseCompiler.props()
1533 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001534}
1535
Colin Crossca860ac2016-01-04 14:34:37 -08001536func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1537 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001538
Dan Willemsen490fd492015-11-24 17:53:15 -08001539 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1540 // all code is position independent, and then those warnings get promoted to
1541 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001542 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001543 flags.CFlags = append(flags.CFlags, "-fPIC")
1544 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001545
Colin Crossca860ac2016-01-04 14:34:37 -08001546 if library.linker.static() {
1547 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001548 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001549 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001550 }
1551
Colin Crossca860ac2016-01-04 14:34:37 -08001552 return flags
1553}
1554
Colin Cross635c3b02016-05-18 15:37:25 -07001555func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1556 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001557
Dan Willemsenb40aab62016-04-20 14:21:14 -07001558 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001559 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001560
1561 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001562 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001563 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1564 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001565 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001566 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001567 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1568 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001569 }
1570
1571 return objFiles
1572}
1573
1574type libraryLinker struct {
1575 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001576 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001577 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001578
1579 Properties LibraryLinkerProperties
1580
1581 dynamicProperties struct {
1582 BuildStatic bool `blueprint:"mutated"`
1583 BuildShared bool `blueprint:"mutated"`
1584 }
1585
Colin Crossca860ac2016-01-04 14:34:37 -08001586 // If we're used as a whole_static_lib, our missing dependencies need
1587 // to be given
1588 wholeStaticMissingDeps []string
1589
1590 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001591 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001592}
1593
1594var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001595
Colin Crossc7a38dc2016-07-12 13:13:09 -07001596type libraryInterface interface {
1597 getWholeStaticMissingDeps() []string
1598 static() bool
1599 objs() android.Paths
1600}
1601
Colin Crossca860ac2016-01-04 14:34:37 -08001602func (library *libraryLinker) props() []interface{} {
1603 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001604 return append(props,
1605 &library.Properties,
1606 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001607 &library.flagExporter.Properties,
1608 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001609}
1610
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001611func (library *libraryLinker) getLibName(ctx ModuleContext) string {
1612 name := ctx.ModuleName()
1613
1614 if Bool(library.Properties.Unique_host_soname) {
1615 if !strings.HasSuffix(name, "-host") {
1616 name = name + "-host"
1617 }
1618 }
1619
1620 return name + library.Properties.VariantName
1621}
1622
Colin Crossca860ac2016-01-04 14:34:37 -08001623func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1624 flags = library.baseLinker.flags(ctx, flags)
1625
Colin Crossca860ac2016-01-04 14:34:37 -08001626 if !library.static() {
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001627 libName := library.getLibName(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -08001628 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1629 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001630 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001631 sharedFlag = "-shared"
1632 }
Colin Crossf87b2612016-07-13 18:55:43 -07001633 var f []string
Colin Crossf6566ed2015-03-24 11:13:38 -07001634 if ctx.Device() {
Colin Crossf87b2612016-07-13 18:55:43 -07001635 f = append(f,
Dan Willemsen99db8c32016-03-03 18:05:38 -08001636 "-nostdlib",
1637 "-Wl,--gc-sections",
1638 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001639 }
Colin Cross97ba0732015-03-23 17:50:24 -07001640
Colin Cross0af4b842015-04-30 16:36:18 -07001641 if ctx.Darwin() {
Colin Crossf87b2612016-07-13 18:55:43 -07001642 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001643 "-dynamiclib",
1644 "-single_module",
1645 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001646 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001647 )
1648 } else {
Colin Crossf87b2612016-07-13 18:55:43 -07001649 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001650 sharedFlag,
Colin Crossf87b2612016-07-13 18:55:43 -07001651 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
Colin Cross0af4b842015-04-30 16:36:18 -07001652 }
Colin Crossf87b2612016-07-13 18:55:43 -07001653
1654 flags.LdFlags = append(f, flags.LdFlags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001655 }
Colin Cross97ba0732015-03-23 17:50:24 -07001656
1657 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001658}
1659
Colin Crossca860ac2016-01-04 14:34:37 -08001660func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1661 deps = library.baseLinker.deps(ctx, deps)
1662 if library.static() {
1663 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1664 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1665 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1666 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001667 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
Colin Crossca860ac2016-01-04 14:34:37 -08001668 if !ctx.sdk() {
1669 deps.CrtBegin = "crtbegin_so"
1670 deps.CrtEnd = "crtend_so"
1671 } else {
1672 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1673 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1674 }
1675 }
1676 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1677 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1678 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1679 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001680
Colin Crossca860ac2016-01-04 14:34:37 -08001681 return deps
1682}
Colin Cross3f40fa42015-01-30 17:27:36 -08001683
Colin Crossca860ac2016-01-04 14:34:37 -08001684func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001685 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001686
Colin Cross635c3b02016-05-18 15:37:25 -07001687 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001688 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001689
Colin Cross635c3b02016-05-18 15:37:25 -07001690 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001691 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001692
Colin Cross0af4b842015-04-30 16:36:18 -07001693 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001694 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001695 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001696 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001697 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001698
Colin Crossca860ac2016-01-04 14:34:37 -08001699 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001700
1701 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001702
1703 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001704}
1705
Colin Crossca860ac2016-01-04 14:34:37 -08001706func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001707 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001708
Colin Cross635c3b02016-05-18 15:37:25 -07001709 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001710
Colin Cross635c3b02016-05-18 15:37:25 -07001711 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1712 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1713 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1714 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001715 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001716 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001717 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001718 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001719 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001720 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001721 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1722 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001723 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001724 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1725 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001726 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001727 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1728 }
1729 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001730 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001731 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1732 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001733 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001734 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001735 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001736 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001737 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001738 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001739 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001740 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001741 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001742 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001743 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001744 }
Colin Crossaee540a2015-07-06 17:48:31 -07001745 }
1746
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001747 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001748 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001749 ret := outputFile
1750
1751 builderFlags := flagsToBuilderFlags(flags)
1752
1753 if library.stripper.needsStrip(ctx) {
1754 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001755 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001756 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1757 }
1758
Colin Crossca860ac2016-01-04 14:34:37 -08001759 sharedLibs := deps.SharedLibs
1760 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001761
Colin Crossca860ac2016-01-04 14:34:37 -08001762 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1763 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001764 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001765
Colin Cross665dce92016-04-28 14:50:03 -07001766 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001767}
1768
Colin Crossca860ac2016-01-04 14:34:37 -08001769func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001770 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001771
Colin Crossc99deeb2016-04-11 15:06:20 -07001772 objFiles = append(objFiles, deps.ObjFiles...)
1773
Colin Cross635c3b02016-05-18 15:37:25 -07001774 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001775 if library.static() {
1776 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001777 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001778 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001779 }
1780
Colin Cross919281a2016-04-05 16:42:05 -07001781 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001782 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001783
1784 return out
1785}
1786
1787func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001788 return library.dynamicProperties.BuildStatic &&
1789 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001790}
1791
1792func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001793 return library.dynamicProperties.BuildShared &&
1794 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001795}
1796
1797func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1798 return library.wholeStaticMissingDeps
1799}
1800
Colin Crossc99deeb2016-04-11 15:06:20 -07001801func (library *libraryLinker) installable() bool {
1802 return !library.static()
1803}
1804
Colin Crossc7a38dc2016-07-12 13:13:09 -07001805func (library *libraryLinker) objs() android.Paths {
1806 return library.objFiles
1807}
1808
Colin Crossca860ac2016-01-04 14:34:37 -08001809type libraryInstaller struct {
1810 baseInstaller
1811
Colin Cross30d5f512016-05-03 18:02:42 -07001812 linker *libraryLinker
1813 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001814}
1815
Colin Cross635c3b02016-05-18 15:37:25 -07001816func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001817 if !library.linker.static() {
1818 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001819 }
1820}
1821
Colin Cross30d5f512016-05-03 18:02:42 -07001822func (library *libraryInstaller) inData() bool {
1823 return library.baseInstaller.inData() || library.sanitize.inData()
1824}
1825
Colin Cross635c3b02016-05-18 15:37:25 -07001826func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1827 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001828
Colin Crossca860ac2016-01-04 14:34:37 -08001829 linker := &libraryLinker{}
1830 linker.dynamicProperties.BuildShared = shared
1831 linker.dynamicProperties.BuildStatic = static
1832 module.linker = linker
1833
1834 module.compiler = &libraryCompiler{
1835 linker: linker,
1836 }
1837 module.installer = &libraryInstaller{
1838 baseInstaller: baseInstaller{
1839 dir: "lib",
1840 dir64: "lib64",
1841 },
Colin Cross30d5f512016-05-03 18:02:42 -07001842 linker: linker,
1843 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001844 }
1845
Colin Crossca860ac2016-01-04 14:34:37 -08001846 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001847}
1848
Colin Crossca860ac2016-01-04 14:34:37 -08001849func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001850 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001851 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001852}
1853
Colin Cross3f40fa42015-01-30 17:27:36 -08001854//
1855// Objects (for crt*.o)
1856//
1857
Colin Crossca860ac2016-01-04 14:34:37 -08001858type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001859 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001860}
1861
Colin Crossca860ac2016-01-04 14:34:37 -08001862func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001863 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001864 module.compiler = &baseCompiler{}
1865 module.linker = &objectLinker{}
1866 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001867}
1868
Colin Cross81413472016-04-11 14:37:39 -07001869func (object *objectLinker) props() []interface{} {
1870 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001871}
1872
Colin Crossca860ac2016-01-04 14:34:37 -08001873func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001874
Colin Cross81413472016-04-11 14:37:39 -07001875func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1876 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001877 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001878}
1879
Colin Crossca860ac2016-01-04 14:34:37 -08001880func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001881 if flags.Clang {
1882 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1883 } else {
1884 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1885 }
1886
Colin Crossca860ac2016-01-04 14:34:37 -08001887 return flags
1888}
1889
1890func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001891 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001892
Colin Cross97ba0732015-03-23 17:50:24 -07001893 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001894
Colin Cross635c3b02016-05-18 15:37:25 -07001895 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001896 if len(objFiles) == 1 {
1897 outputFile = objFiles[0]
1898 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001899 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001900 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001901 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001902 }
1903
Colin Cross3f40fa42015-01-30 17:27:36 -08001904 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001905 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001906}
1907
Colin Crossc99deeb2016-04-11 15:06:20 -07001908func (*objectLinker) installable() bool {
1909 return false
1910}
1911
Colin Cross3f40fa42015-01-30 17:27:36 -08001912//
1913// Executables
1914//
1915
Colin Crossca860ac2016-01-04 14:34:37 -08001916type binaryLinker struct {
1917 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001918 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001919
Colin Crossca860ac2016-01-04 14:34:37 -08001920 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001921
Colin Cross635c3b02016-05-18 15:37:25 -07001922 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001923}
1924
Colin Crossca860ac2016-01-04 14:34:37 -08001925var _ linker = (*binaryLinker)(nil)
1926
1927func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001928 return append(binary.baseLinker.props(),
1929 &binary.Properties,
1930 &binary.stripper.StripProperties)
1931
Colin Cross3f40fa42015-01-30 17:27:36 -08001932}
1933
Colin Crossca860ac2016-01-04 14:34:37 -08001934func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001935 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001936}
1937
Colin Crossca860ac2016-01-04 14:34:37 -08001938func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001939 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001940}
1941
Colin Crossca860ac2016-01-04 14:34:37 -08001942func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001943 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001944 if binary.Properties.Stem != "" {
1945 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001946 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001947
Colin Crossca860ac2016-01-04 14:34:37 -08001948 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001949}
1950
Colin Crossca860ac2016-01-04 14:34:37 -08001951func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1952 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001953 if ctx.Device() {
Colin Crossa89d2e12016-01-11 12:48:37 -08001954 if !Bool(binary.baseLinker.Properties.Nocrt) {
1955 if !ctx.sdk() {
1956 if binary.buildStatic() {
1957 deps.CrtBegin = "crtbegin_static"
1958 } else {
1959 deps.CrtBegin = "crtbegin_dynamic"
1960 }
1961 deps.CrtEnd = "crtend_android"
Dan Albertc3144b12015-04-28 18:17:56 -07001962 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001963 if binary.buildStatic() {
1964 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
1965 } else {
1966 if Bool(binary.Properties.Static_executable) {
1967 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
1968 } else {
1969 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
1970 }
1971 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
1972 }
Dan Albertc3144b12015-04-28 18:17:56 -07001973 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001974 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001975
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001976 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001977 if inList("libc++_static", deps.StaticLibs) {
1978 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001979 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001980 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1981 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1982 // move them to the beginning of deps.LateStaticLibs
1983 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001984 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001985 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001986 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001987 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001988 }
Colin Crossca860ac2016-01-04 14:34:37 -08001989
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001990 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001991 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1992 "from static libs or set static_executable: true")
1993 }
1994 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001995}
1996
Colin Crossc99deeb2016-04-11 15:06:20 -07001997func (*binaryLinker) installable() bool {
1998 return true
1999}
2000
Colin Cross16b23492016-01-06 14:41:07 -08002001func (binary *binaryLinker) isDependencyRoot() bool {
2002 return true
2003}
2004
Colin Cross635c3b02016-05-18 15:37:25 -07002005func NewBinary(hod android.HostOrDeviceSupported) *Module {
2006 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002007 module.compiler = &baseCompiler{}
2008 module.linker = &binaryLinker{}
2009 module.installer = &baseInstaller{
2010 dir: "bin",
2011 }
2012 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08002013}
2014
Colin Crossca860ac2016-01-04 14:34:37 -08002015func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002016 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002017 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002018}
2019
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002020func (binary *binaryLinker) begin(ctx BaseModuleContext) {
2021 binary.baseLinker.begin(ctx)
2022
2023 static := Bool(binary.Properties.Static_executable)
2024 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07002025 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002026 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
2027 static = true
2028 }
2029 } else {
2030 // Static executables are not supported on Darwin or Windows
2031 static = false
2032 }
Colin Cross0af4b842015-04-30 16:36:18 -07002033 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002034 if static {
2035 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08002036 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07002037 }
2038}
2039
Colin Crossca860ac2016-01-04 14:34:37 -08002040func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2041 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002042
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002043 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002044 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002045 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002046 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2047 }
2048 }
2049
2050 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2051 // all code is position independent, and then those warnings get promoted to
2052 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002053 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002054 flags.CFlags = append(flags.CFlags, "-fpie")
2055 }
Colin Cross97ba0732015-03-23 17:50:24 -07002056
Colin Crossf6566ed2015-03-24 11:13:38 -07002057 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002058 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002059 // Clang driver needs -static to create static executable.
2060 // However, bionic/linker uses -shared to overwrite.
2061 // Linker for x86 targets does not allow coexistance of -static and -shared,
2062 // so we add -static only if -shared is not used.
2063 if !inList("-shared", flags.LdFlags) {
2064 flags.LdFlags = append(flags.LdFlags, "-static")
2065 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002066
Colin Crossed4cf0b2015-03-26 14:43:45 -07002067 flags.LdFlags = append(flags.LdFlags,
2068 "-nostdlib",
2069 "-Bstatic",
2070 "-Wl,--gc-sections",
2071 )
2072
2073 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002074 if flags.DynamicLinker == "" {
2075 flags.DynamicLinker = "/system/bin/linker"
2076 if flags.Toolchain.Is64Bit() {
2077 flags.DynamicLinker += "64"
2078 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002079 }
2080
2081 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002082 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002083 "-nostdlib",
2084 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002085 "-Wl,--gc-sections",
2086 "-Wl,-z,nocopyreloc",
2087 )
2088 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002089 } else {
2090 if binary.staticBinary() {
2091 flags.LdFlags = append(flags.LdFlags, "-static")
2092 }
2093 if ctx.Darwin() {
2094 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2095 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002096 }
2097
Colin Cross97ba0732015-03-23 17:50:24 -07002098 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002099}
2100
Colin Crossca860ac2016-01-04 14:34:37 -08002101func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002102 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002103
Colin Cross665dce92016-04-28 14:50:03 -07002104 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002105 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002106 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002107 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002108 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002109 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002110
Colin Cross635c3b02016-05-18 15:37:25 -07002111 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002112
Colin Crossca860ac2016-01-04 14:34:37 -08002113 sharedLibs := deps.SharedLibs
2114 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2115
Colin Cross16b23492016-01-06 14:41:07 -08002116 if flags.DynamicLinker != "" {
2117 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2118 }
2119
Colin Cross665dce92016-04-28 14:50:03 -07002120 builderFlags := flagsToBuilderFlags(flags)
2121
2122 if binary.stripper.needsStrip(ctx) {
2123 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002124 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002125 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2126 }
2127
2128 if binary.Properties.Prefix_symbols != "" {
2129 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002130 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002131 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2132 flagsToBuilderFlags(flags), afterPrefixSymbols)
2133 }
2134
Colin Crossca860ac2016-01-04 14:34:37 -08002135 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002136 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002137 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002138
2139 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002140}
Colin Cross3f40fa42015-01-30 17:27:36 -08002141
Colin Cross635c3b02016-05-18 15:37:25 -07002142func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002143 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002144}
2145
Colin Cross665dce92016-04-28 14:50:03 -07002146type stripper struct {
2147 StripProperties StripProperties
2148}
2149
2150func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2151 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2152}
2153
Colin Cross635c3b02016-05-18 15:37:25 -07002154func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002155 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002156 if ctx.Darwin() {
2157 TransformDarwinStrip(ctx, in, out)
2158 } else {
2159 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2160 // TODO(ccross): don't add gnu debuglink for user builds
2161 flags.stripAddGnuDebuglink = true
2162 TransformStrip(ctx, in, out, flags)
2163 }
Colin Cross665dce92016-04-28 14:50:03 -07002164}
2165
Colin Cross635c3b02016-05-18 15:37:25 -07002166func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002167 if m, ok := mctx.Module().(*Module); ok {
Colin Crossc7a38dc2016-07-12 13:13:09 -07002168 if test, ok := m.linker.(*testBinaryLinker); ok {
2169 if Bool(test.testLinker.Properties.Test_per_src) {
Colin Crossca860ac2016-01-04 14:34:37 -08002170 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2171 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2172 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2173 }
2174 tests := mctx.CreateLocalVariations(testNames...)
2175 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2176 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002177 tests[i].(*Module).linker.(*testBinaryLinker).binaryLinker.Properties.Stem = testNames[i]
Colin Crossca860ac2016-01-04 14:34:37 -08002178 }
Colin Cross6002e052015-09-16 16:00:08 -07002179 }
2180 }
2181 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002182}
2183
Colin Crossca860ac2016-01-04 14:34:37 -08002184type testLinker struct {
Colin Crossca860ac2016-01-04 14:34:37 -08002185 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002186}
2187
Colin Crossca860ac2016-01-04 14:34:37 -08002188func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -08002189 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002190 return flags
2191 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002192
Colin Cross97ba0732015-03-23 17:50:24 -07002193 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002194 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002195 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002196
Colin Crossa1ad8d12016-06-01 17:09:44 -07002197 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002198 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002199 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002200 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002201 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2202 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002203 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002204 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2205 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002206 }
2207 } else {
2208 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002209 }
2210
Colin Cross21b9a242015-03-24 14:15:58 -07002211 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002212}
2213
Colin Crossca860ac2016-01-04 14:34:37 -08002214func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2215 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002216 if ctx.sdk() && ctx.Device() {
2217 switch ctx.selectedStl() {
2218 case "ndk_libc++_shared", "ndk_libc++_static":
2219 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2220 case "ndk_libgnustl_static":
2221 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2222 default:
2223 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2224 }
2225 } else {
2226 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2227 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002228 }
Colin Crossc7a38dc2016-07-12 13:13:09 -07002229 return deps
2230}
2231
2232type testBinaryLinker struct {
2233 testLinker
2234 binaryLinker
2235}
2236
2237func (test *testBinaryLinker) begin(ctx BaseModuleContext) {
2238 test.binaryLinker.begin(ctx)
2239 runpath := "../../lib"
2240 if ctx.toolchain().Is64Bit() {
2241 runpath += "64"
2242 }
2243 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
2244}
2245
2246func (test *testBinaryLinker) props() []interface{} {
2247 return append(test.binaryLinker.props(), &test.testLinker.Properties)
2248}
2249
2250func (test *testBinaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2251 flags = test.binaryLinker.flags(ctx, flags)
2252 flags = test.testLinker.flags(ctx, flags)
2253 return flags
2254}
2255
2256func (test *testBinaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2257 deps = test.testLinker.deps(ctx, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08002258 deps = test.binaryLinker.deps(ctx, deps)
2259 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002260}
2261
Colin Crossc7a38dc2016-07-12 13:13:09 -07002262type testLibraryLinker struct {
2263 testLinker
2264 *libraryLinker
2265}
2266
2267func (test *testLibraryLinker) props() []interface{} {
2268 return append(test.libraryLinker.props(), &test.testLinker.Properties)
2269}
2270
2271func (test *testLibraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2272 flags = test.libraryLinker.flags(ctx, flags)
2273 flags = test.testLinker.flags(ctx, flags)
2274 return flags
2275}
2276
2277func (test *testLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2278 deps = test.testLinker.deps(ctx, deps)
2279 deps = test.libraryLinker.deps(ctx, deps)
2280 return deps
2281}
2282
Colin Crossca860ac2016-01-04 14:34:37 -08002283type testInstaller struct {
2284 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002285}
2286
Colin Cross635c3b02016-05-18 15:37:25 -07002287func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002288 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2289 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2290 installer.baseInstaller.install(ctx, file)
2291}
2292
Colin Cross635c3b02016-05-18 15:37:25 -07002293func NewTest(hod android.HostOrDeviceSupported) *Module {
2294 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002295 module.compiler = &baseCompiler{}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002296 linker := &testBinaryLinker{}
2297 linker.testLinker.Properties.Gtest = true
Colin Crossca860ac2016-01-04 14:34:37 -08002298 module.linker = linker
2299 module.installer = &testInstaller{
2300 baseInstaller: baseInstaller{
2301 dir: "nativetest",
2302 dir64: "nativetest64",
2303 data: true,
2304 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002305 }
Colin Crossca860ac2016-01-04 14:34:37 -08002306 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002307}
2308
Colin Crossca860ac2016-01-04 14:34:37 -08002309func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002310 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002311 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002312}
2313
Colin Crossc7a38dc2016-07-12 13:13:09 -07002314func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
2315 module := NewLibrary(android.HostAndDeviceSupported, false, true)
2316 linker := &testLibraryLinker{
2317 libraryLinker: module.linker.(*libraryLinker),
2318 }
2319 linker.testLinker.Properties.Gtest = true
2320 module.linker = linker
2321 module.installer = &testInstaller{
2322 baseInstaller: baseInstaller{
2323 dir: "nativetest",
2324 dir64: "nativetest64",
2325 data: true,
2326 },
2327 }
2328 return module
2329}
2330
2331func testLibraryFactory() (blueprint.Module, []interface{}) {
2332 module := NewTestLibrary(android.HostAndDeviceSupported)
2333 return module.Init()
2334}
2335
Colin Crossca860ac2016-01-04 14:34:37 -08002336type benchmarkLinker struct {
Colin Crossaa3bf372016-07-14 10:27:10 -07002337 testBinaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002338}
2339
Colin Crossca860ac2016-01-04 14:34:37 -08002340func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossaa3bf372016-07-14 10:27:10 -07002341 deps = benchmark.testBinaryLinker.deps(ctx, deps)
Colin Cross26832742016-07-11 14:57:56 -07002342 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
Colin Crossca860ac2016-01-04 14:34:37 -08002343 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002344}
2345
Colin Cross635c3b02016-05-18 15:37:25 -07002346func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2347 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002348 module.compiler = &baseCompiler{}
2349 module.linker = &benchmarkLinker{}
Colin Cross624b8ed2016-07-11 17:20:09 -07002350 module.installer = &testInstaller{
2351 baseInstaller: baseInstaller{
2352 dir: "nativetest",
2353 dir64: "nativetest64",
2354 data: true,
2355 },
Colin Cross2ba19d92015-05-07 15:44:20 -07002356 }
Colin Crossca860ac2016-01-04 14:34:37 -08002357 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002358}
2359
Colin Crossca860ac2016-01-04 14:34:37 -08002360func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002361 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002362 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002363}
2364
Colin Cross3f40fa42015-01-30 17:27:36 -08002365//
2366// Static library
2367//
2368
Colin Crossca860ac2016-01-04 14:34:37 -08002369func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002370 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002371 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002372}
2373
2374//
2375// Shared libraries
2376//
2377
Colin Crossca860ac2016-01-04 14:34:37 -08002378func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002379 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002380 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002381}
2382
2383//
2384// Host static library
2385//
2386
Colin Crossca860ac2016-01-04 14:34:37 -08002387func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002388 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002389 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002390}
2391
2392//
2393// Host Shared libraries
2394//
2395
Colin Crossca860ac2016-01-04 14:34:37 -08002396func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002397 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002398 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002399}
2400
2401//
2402// Host Binaries
2403//
2404
Colin Crossca860ac2016-01-04 14:34:37 -08002405func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002406 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002407 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002408}
2409
2410//
Colin Cross1f8f2342015-03-26 16:09:47 -07002411// Host Tests
2412//
2413
Colin Crossca860ac2016-01-04 14:34:37 -08002414func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002415 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002416 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002417}
2418
2419//
Colin Cross2ba19d92015-05-07 15:44:20 -07002420// Host Benchmarks
2421//
2422
Colin Crossca860ac2016-01-04 14:34:37 -08002423func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002424 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002425 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002426}
2427
2428//
Colin Crosscfad1192015-11-02 16:43:11 -08002429// Defaults
2430//
Colin Crossca860ac2016-01-04 14:34:37 -08002431type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002432 android.ModuleBase
2433 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002434}
2435
Colin Cross635c3b02016-05-18 15:37:25 -07002436func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002437}
2438
Colin Crossca860ac2016-01-04 14:34:37 -08002439func defaultsFactory() (blueprint.Module, []interface{}) {
2440 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002441
2442 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002443 &BaseProperties{},
2444 &BaseCompilerProperties{},
2445 &BaseLinkerProperties{},
2446 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002447 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002448 &LibraryLinkerProperties{},
2449 &BinaryLinkerProperties{},
2450 &TestLinkerProperties{},
2451 &UnusedProperties{},
2452 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002453 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002454 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002455 }
2456
Colin Cross635c3b02016-05-18 15:37:25 -07002457 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2458 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002459
Colin Cross635c3b02016-05-18 15:37:25 -07002460 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002461}
2462
2463//
Colin Cross3f40fa42015-01-30 17:27:36 -08002464// Device libraries shipped with gcc
2465//
2466
Colin Crossca860ac2016-01-04 14:34:37 -08002467type toolchainLibraryLinker struct {
2468 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002469}
2470
Colin Crossca860ac2016-01-04 14:34:37 -08002471var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2472
2473func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002474 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002475 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002476}
2477
Colin Crossca860ac2016-01-04 14:34:37 -08002478func (*toolchainLibraryLinker) buildStatic() bool {
2479 return true
2480}
Colin Cross3f40fa42015-01-30 17:27:36 -08002481
Colin Crossca860ac2016-01-04 14:34:37 -08002482func (*toolchainLibraryLinker) buildShared() bool {
2483 return false
2484}
2485
2486func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002487 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002488 module.compiler = &baseCompiler{}
2489 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002490 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002491 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002492}
2493
Colin Crossca860ac2016-01-04 14:34:37 -08002494func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002495 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002496
2497 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002498 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002499
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002500 if flags.Clang {
2501 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2502 }
2503
Colin Crossca860ac2016-01-04 14:34:37 -08002504 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002505
2506 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002507
Colin Crossca860ac2016-01-04 14:34:37 -08002508 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002509}
2510
Colin Crossc99deeb2016-04-11 15:06:20 -07002511func (*toolchainLibraryLinker) installable() bool {
2512 return false
2513}
2514
Dan Albertbe961682015-03-18 23:38:50 -07002515// NDK prebuilt libraries.
2516//
2517// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2518// either (with the exception of the shared STLs, which are installed to the app's directory rather
2519// than to the system image).
2520
Colin Cross635c3b02016-05-18 15:37:25 -07002521func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002522 suffix := ""
2523 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2524 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002525 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002526 suffix = "64"
2527 }
Colin Cross635c3b02016-05-18 15:37:25 -07002528 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002529 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002530}
2531
Colin Cross635c3b02016-05-18 15:37:25 -07002532func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2533 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002534
2535 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2536 // We want to translate to just NAME.EXT
2537 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2538 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002539 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002540}
2541
Colin Crossca860ac2016-01-04 14:34:37 -08002542type ndkPrebuiltObjectLinker struct {
2543 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002544}
2545
Colin Crossca860ac2016-01-04 14:34:37 -08002546func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002547 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002548 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002549}
2550
Colin Crossca860ac2016-01-04 14:34:37 -08002551func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002552 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002553 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002554 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002555 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002556}
2557
Colin Crossca860ac2016-01-04 14:34:37 -08002558func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002559 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002560 // A null build step, but it sets up the output path.
2561 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2562 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2563 }
2564
Colin Crossca860ac2016-01-04 14:34:37 -08002565 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002566}
2567
Colin Crossca860ac2016-01-04 14:34:37 -08002568type ndkPrebuiltLibraryLinker struct {
2569 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002570}
2571
Colin Crossca860ac2016-01-04 14:34:37 -08002572var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2573var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002574
Colin Crossca860ac2016-01-04 14:34:37 -08002575func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002576 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002577}
2578
Colin Crossca860ac2016-01-04 14:34:37 -08002579func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002580 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002581 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002582}
2583
Colin Crossca860ac2016-01-04 14:34:37 -08002584func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002585 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002586 linker := &ndkPrebuiltLibraryLinker{}
2587 linker.dynamicProperties.BuildShared = true
2588 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002589 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002590 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002591}
2592
Colin Crossca860ac2016-01-04 14:34:37 -08002593func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002594 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002595 // A null build step, but it sets up the output path.
2596 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2597 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2598 }
2599
Colin Cross919281a2016-04-05 16:42:05 -07002600 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002601
Colin Crossca860ac2016-01-04 14:34:37 -08002602 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2603 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002604}
2605
2606// The NDK STLs are slightly different from the prebuilt system libraries:
2607// * Are not specific to each platform version.
2608// * The libraries are not in a predictable location for each STL.
2609
Colin Crossca860ac2016-01-04 14:34:37 -08002610type ndkPrebuiltStlLinker struct {
2611 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002612}
2613
Colin Crossca860ac2016-01-04 14:34:37 -08002614func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002615 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002616 linker := &ndkPrebuiltStlLinker{}
2617 linker.dynamicProperties.BuildShared = true
2618 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002619 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002620 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002621}
2622
Colin Crossca860ac2016-01-04 14:34:37 -08002623func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002624 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002625 linker := &ndkPrebuiltStlLinker{}
2626 linker.dynamicProperties.BuildStatic = true
2627 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002628 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002629 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002630}
2631
Colin Cross635c3b02016-05-18 15:37:25 -07002632func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002633 gccVersion := toolchain.GccVersion()
2634 var libDir string
2635 switch stl {
2636 case "libstlport":
2637 libDir = "cxx-stl/stlport/libs"
2638 case "libc++":
2639 libDir = "cxx-stl/llvm-libc++/libs"
2640 case "libgnustl":
2641 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2642 }
2643
2644 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002645 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002646 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002647 }
2648
2649 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002650 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002651}
2652
Colin Crossca860ac2016-01-04 14:34:37 -08002653func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002654 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002655 // A null build step, but it sets up the output path.
2656 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2657 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2658 }
2659
Colin Cross919281a2016-04-05 16:42:05 -07002660 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002661
2662 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002663 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002664 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002665 libExt = staticLibraryExtension
2666 }
2667
2668 stlName := strings.TrimSuffix(libName, "_shared")
2669 stlName = strings.TrimSuffix(stlName, "_static")
2670 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002671 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002672}
2673
Colin Cross635c3b02016-05-18 15:37:25 -07002674func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002675 if m, ok := mctx.Module().(*Module); ok {
2676 if m.linker != nil {
2677 if linker, ok := m.linker.(baseLinkerInterface); ok {
2678 var modules []blueprint.Module
2679 if linker.buildStatic() && linker.buildShared() {
2680 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002681 static := modules[0].(*Module)
2682 shared := modules[1].(*Module)
2683
2684 static.linker.(baseLinkerInterface).setStatic(true)
2685 shared.linker.(baseLinkerInterface).setStatic(false)
2686
2687 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2688 sharedCompiler := shared.compiler.(*libraryCompiler)
2689 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2690 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2691 // Optimize out compiling common .o files twice for static+shared libraries
2692 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2693 sharedCompiler.baseCompiler.Properties.Srcs = nil
2694 }
2695 }
Colin Crossca860ac2016-01-04 14:34:37 -08002696 } else if linker.buildStatic() {
2697 modules = mctx.CreateLocalVariations("static")
2698 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2699 } else if linker.buildShared() {
2700 modules = mctx.CreateLocalVariations("shared")
2701 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2702 } else {
2703 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2704 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002705 }
2706 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002707 }
2708}
Colin Cross74d1ec02015-04-28 13:30:13 -07002709
2710// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2711// modifies the slice contents in place, and returns a subslice of the original slice
2712func lastUniqueElements(list []string) []string {
2713 totalSkip := 0
2714 for i := len(list) - 1; i >= totalSkip; i-- {
2715 skip := 0
2716 for j := i - 1; j >= totalSkip; j-- {
2717 if list[i] == list[j] {
2718 skip++
2719 } else {
2720 list[j+skip] = list[j]
2721 }
2722 }
2723 totalSkip += skip
2724 }
2725 return list[totalSkip:]
2726}
Colin Cross06a931b2015-10-28 17:23:31 -07002727
2728var Bool = proptools.Bool