blob: acc807f46a70169f5c1d869ab5777af522614ab9 [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
Colin Cross16b23492016-01-06 14:41:07 -0800405 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800406}
407
408type BinaryLinkerProperties struct {
409 // compile executable with -static
Dan Willemsen75ab8082016-07-12 15:36:34 -0700410 Static_executable *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800411
412 // set the name of the output
413 Stem string `android:"arch_variant"`
414
415 // append to the name of the output
416 Suffix string `android:"arch_variant"`
417
418 // if set, add an extra objcopy --prefix-symbols= step
419 Prefix_symbols string
420}
421
422type TestLinkerProperties struct {
423 // if set, build against the gtest library. Defaults to true.
424 Gtest bool
425
426 // Create a separate binary for each source file. Useful when there is
427 // global state that can not be torn down and reset between each test suite.
428 Test_per_src *bool
429}
430
Colin Cross81413472016-04-11 14:37:39 -0700431type ObjectLinkerProperties struct {
432 // names of other cc_object modules to link into this module using partial linking
433 Objs []string `android:"arch_variant"`
434}
435
Colin Crossca860ac2016-01-04 14:34:37 -0800436// Properties used to compile all C or C++ modules
437type BaseProperties struct {
438 // compile module with clang instead of gcc
439 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700440
441 // Minimum sdk version supported when compiling against the ndk
442 Sdk_version string
443
Colin Crossca860ac2016-01-04 14:34:37 -0800444 // don't insert default compiler flags into asflags, cflags,
445 // cppflags, conlyflags, ldflags, or include_dirs
446 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700447
448 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700449 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800450}
451
452type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700453 // install to a subdirectory of the default install path for the module
454 Relative_install_path string
Colin Cross3854a602016-01-11 12:49:11 -0800455
456 // install symlinks to the module
457 Symlinks []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700458}
459
Colin Cross665dce92016-04-28 14:50:03 -0700460type StripProperties struct {
461 Strip struct {
462 None bool
463 Keep_symbols bool
464 }
465}
466
Colin Crossca860ac2016-01-04 14:34:37 -0800467type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700468 Native_coverage *bool
469 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700470 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800471}
472
Colin Crossca860ac2016-01-04 14:34:37 -0800473type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800474 static() bool
475 staticBinary() bool
476 clang() bool
477 toolchain() Toolchain
478 noDefaultCompilerFlags() bool
479 sdk() bool
480 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700481 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800482}
483
484type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700485 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800486 ModuleContextIntf
487}
488
489type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700490 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800491 ModuleContextIntf
492}
493
494type Customizer interface {
495 CustomizeProperties(BaseModuleContext)
496 Properties() []interface{}
497}
498
499type feature interface {
500 begin(ctx BaseModuleContext)
501 deps(ctx BaseModuleContext, deps Deps) Deps
502 flags(ctx ModuleContext, flags Flags) Flags
503 props() []interface{}
504}
505
506type compiler interface {
507 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700508 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800509}
510
511type linker interface {
512 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700513 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700514 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800515}
516
517type installer interface {
518 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700519 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800520 inData() bool
521}
522
Colin Crossc99deeb2016-04-11 15:06:20 -0700523type dependencyTag struct {
524 blueprint.BaseDependencyTag
525 name string
526 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700527
528 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700529}
530
531var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700532 sharedDepTag = dependencyTag{name: "shared", library: true}
533 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
534 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
535 staticDepTag = dependencyTag{name: "static", library: true}
536 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
537 lateStaticDepTag = dependencyTag{name: "late static", library: true}
538 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
539 genSourceDepTag = dependencyTag{name: "gen source"}
540 genHeaderDepTag = dependencyTag{name: "gen header"}
541 objDepTag = dependencyTag{name: "obj"}
542 crtBeginDepTag = dependencyTag{name: "crtbegin"}
543 crtEndDepTag = dependencyTag{name: "crtend"}
544 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700545)
546
Colin Crossca860ac2016-01-04 14:34:37 -0800547// Module contains the properties and members used by all C/C++ module types, and implements
548// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
549// to construct the output file. Behavior can be customized with a Customizer interface
550type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700551 android.ModuleBase
552 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700553
Colin Crossca860ac2016-01-04 14:34:37 -0800554 Properties BaseProperties
555 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700556
Colin Crossca860ac2016-01-04 14:34:37 -0800557 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700558 hod android.HostOrDeviceSupported
559 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700560
Colin Crossca860ac2016-01-04 14:34:37 -0800561 // delegates, initialize before calling Init
562 customizer Customizer
563 features []feature
564 compiler compiler
565 linker linker
566 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700567 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800568 sanitize *sanitize
569
570 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700571
Colin Cross635c3b02016-05-18 15:37:25 -0700572 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800573
574 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700575}
576
Colin Crossca860ac2016-01-04 14:34:37 -0800577func (c *Module) Init() (blueprint.Module, []interface{}) {
578 props := []interface{}{&c.Properties, &c.unused}
579 if c.customizer != nil {
580 props = append(props, c.customizer.Properties()...)
581 }
582 if c.compiler != nil {
583 props = append(props, c.compiler.props()...)
584 }
585 if c.linker != nil {
586 props = append(props, c.linker.props()...)
587 }
588 if c.installer != nil {
589 props = append(props, c.installer.props()...)
590 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700591 if c.stl != nil {
592 props = append(props, c.stl.props()...)
593 }
Colin Cross16b23492016-01-06 14:41:07 -0800594 if c.sanitize != nil {
595 props = append(props, c.sanitize.props()...)
596 }
Colin Crossca860ac2016-01-04 14:34:37 -0800597 for _, feature := range c.features {
598 props = append(props, feature.props()...)
599 }
Colin Crossc472d572015-03-17 15:06:21 -0700600
Colin Cross635c3b02016-05-18 15:37:25 -0700601 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700602
Colin Cross635c3b02016-05-18 15:37:25 -0700603 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700604}
605
Colin Crossca860ac2016-01-04 14:34:37 -0800606type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700607 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800608 moduleContextImpl
609}
610
611type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700612 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800613 moduleContextImpl
614}
615
616type moduleContextImpl struct {
617 mod *Module
618 ctx BaseModuleContext
619}
620
Colin Crossca860ac2016-01-04 14:34:37 -0800621func (ctx *moduleContextImpl) clang() bool {
622 return ctx.mod.clang(ctx.ctx)
623}
624
625func (ctx *moduleContextImpl) toolchain() Toolchain {
626 return ctx.mod.toolchain(ctx.ctx)
627}
628
629func (ctx *moduleContextImpl) static() bool {
630 if ctx.mod.linker == nil {
631 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
632 }
633 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
634 return linker.static()
635 } else {
636 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
637 }
638}
639
640func (ctx *moduleContextImpl) staticBinary() bool {
641 if ctx.mod.linker == nil {
642 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
643 }
644 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
645 return linker.staticBinary()
646 } else {
647 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
648 }
649}
650
651func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
652 return Bool(ctx.mod.Properties.No_default_compiler_flags)
653}
654
655func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700656 if ctx.ctx.Device() {
657 return ctx.mod.Properties.Sdk_version != ""
658 }
659 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800660}
661
662func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700663 if ctx.ctx.Device() {
664 return ctx.mod.Properties.Sdk_version
665 }
666 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800667}
668
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700669func (ctx *moduleContextImpl) selectedStl() string {
670 if stl := ctx.mod.stl; stl != nil {
671 return stl.Properties.SelectedStl
672 }
673 return ""
674}
675
Colin Cross635c3b02016-05-18 15:37:25 -0700676func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800677 return &Module{
678 hod: hod,
679 multilib: multilib,
680 }
681}
682
Colin Cross635c3b02016-05-18 15:37:25 -0700683func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800684 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700685 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800686 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800687 return module
688}
689
Colin Cross635c3b02016-05-18 15:37:25 -0700690func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800691 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700692 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800693 moduleContextImpl: moduleContextImpl{
694 mod: c,
695 },
696 }
697 ctx.ctx = ctx
698
699 flags := Flags{
700 Toolchain: c.toolchain(ctx),
701 Clang: c.clang(ctx),
702 }
Colin Crossca860ac2016-01-04 14:34:37 -0800703 if c.compiler != nil {
704 flags = c.compiler.flags(ctx, flags)
705 }
706 if c.linker != nil {
707 flags = c.linker.flags(ctx, flags)
708 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700709 if c.stl != nil {
710 flags = c.stl.flags(ctx, flags)
711 }
Colin Cross16b23492016-01-06 14:41:07 -0800712 if c.sanitize != nil {
713 flags = c.sanitize.flags(ctx, flags)
714 }
Colin Crossca860ac2016-01-04 14:34:37 -0800715 for _, feature := range c.features {
716 flags = feature.flags(ctx, flags)
717 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800718 if ctx.Failed() {
719 return
720 }
721
Colin Crossca860ac2016-01-04 14:34:37 -0800722 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
723 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
724 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800725
Colin Crossca860ac2016-01-04 14:34:37 -0800726 // Optimization to reduce size of build.ninja
727 // Replace the long list of flags for each file with a module-local variable
728 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
729 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
730 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
731 flags.CFlags = []string{"$cflags"}
732 flags.CppFlags = []string{"$cppflags"}
733 flags.AsFlags = []string{"$asflags"}
734
Colin Crossc99deeb2016-04-11 15:06:20 -0700735 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800736 if ctx.Failed() {
737 return
738 }
739
Dan Willemsen76f08272016-07-09 00:14:08 -0700740 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700741
Colin Cross635c3b02016-05-18 15:37:25 -0700742 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800743 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700744 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800745 if ctx.Failed() {
746 return
747 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800748 }
749
Colin Crossca860ac2016-01-04 14:34:37 -0800750 if c.linker != nil {
751 outputFile := c.linker.link(ctx, flags, deps, objFiles)
752 if ctx.Failed() {
753 return
754 }
Colin Cross635c3b02016-05-18 15:37:25 -0700755 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700756
Colin Crossc99deeb2016-04-11 15:06:20 -0700757 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800758 c.installer.install(ctx, outputFile)
759 if ctx.Failed() {
760 return
761 }
762 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700763 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800764}
765
Colin Crossca860ac2016-01-04 14:34:37 -0800766func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
767 if c.cachedToolchain == nil {
768 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700769 os := ctx.Os()
770 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800771 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700772 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800773 return nil
774 }
775 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800776 }
Colin Crossca860ac2016-01-04 14:34:37 -0800777 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800778}
779
Colin Crossca860ac2016-01-04 14:34:37 -0800780func (c *Module) begin(ctx BaseModuleContext) {
781 if c.compiler != nil {
782 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700783 }
Colin Crossca860ac2016-01-04 14:34:37 -0800784 if c.linker != nil {
785 c.linker.begin(ctx)
786 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700787 if c.stl != nil {
788 c.stl.begin(ctx)
789 }
Colin Cross16b23492016-01-06 14:41:07 -0800790 if c.sanitize != nil {
791 c.sanitize.begin(ctx)
792 }
Colin Crossca860ac2016-01-04 14:34:37 -0800793 for _, feature := range c.features {
794 feature.begin(ctx)
795 }
796}
797
Colin Crossc99deeb2016-04-11 15:06:20 -0700798func (c *Module) deps(ctx BaseModuleContext) Deps {
799 deps := Deps{}
800
801 if c.compiler != nil {
802 deps = c.compiler.deps(ctx, deps)
803 }
804 if c.linker != nil {
805 deps = c.linker.deps(ctx, deps)
806 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700807 if c.stl != nil {
808 deps = c.stl.deps(ctx, deps)
809 }
Colin Cross16b23492016-01-06 14:41:07 -0800810 if c.sanitize != nil {
811 deps = c.sanitize.deps(ctx, deps)
812 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700813 for _, feature := range c.features {
814 deps = feature.deps(ctx, deps)
815 }
816
817 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
818 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
819 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
820 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
821 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
822
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700823 for _, lib := range deps.ReexportSharedLibHeaders {
824 if !inList(lib, deps.SharedLibs) {
825 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
826 }
827 }
828
829 for _, lib := range deps.ReexportStaticLibHeaders {
830 if !inList(lib, deps.StaticLibs) {
831 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
832 }
833 }
834
Colin Crossc99deeb2016-04-11 15:06:20 -0700835 return deps
836}
837
Colin Cross635c3b02016-05-18 15:37:25 -0700838func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800839 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700840 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800841 moduleContextImpl: moduleContextImpl{
842 mod: c,
843 },
844 }
845 ctx.ctx = ctx
846
847 if c.customizer != nil {
848 c.customizer.CustomizeProperties(ctx)
849 }
850
851 c.begin(ctx)
852
Colin Crossc99deeb2016-04-11 15:06:20 -0700853 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800854
Colin Crossb5bc4b42016-07-11 16:11:59 -0700855 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
856 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700857
858 if ctx.sdk() {
859 version := "." + ctx.sdkVersion()
860
861 rewriteNdkLibs := func(list []string) []string {
862 for i, entry := range list {
863 if inList(entry, ndkPrebuiltSharedLibraries) {
864 list[i] = "ndk_" + entry + version
865 }
866 }
867 return list
868 }
869
870 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
871 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
872 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700873
874 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
875 deps.WholeStaticLibs...)
876
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700877 for _, lib := range deps.StaticLibs {
878 depTag := staticDepTag
879 if inList(lib, deps.ReexportStaticLibHeaders) {
880 depTag = staticExportDepTag
881 }
Colin Cross15a0d462016-07-14 14:49:58 -0700882 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700883 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700884
885 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
886 deps.LateStaticLibs...)
887
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700888 for _, lib := range deps.SharedLibs {
889 depTag := sharedDepTag
890 if inList(lib, deps.ReexportSharedLibHeaders) {
891 depTag = sharedExportDepTag
892 }
Colin Cross15a0d462016-07-14 14:49:58 -0700893 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700894 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700895
896 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
897 deps.LateSharedLibs...)
898
Colin Cross68861832016-07-08 10:41:41 -0700899 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
900 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700901
Colin Cross68861832016-07-08 10:41:41 -0700902 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700903
904 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700905 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800906 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700907 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700908 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700909 }
Colin Cross6362e272015-10-29 15:25:03 -0700910}
Colin Cross21b9a242015-03-24 14:15:58 -0700911
Colin Cross635c3b02016-05-18 15:37:25 -0700912func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700913 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700914 c.depsMutator(ctx)
915 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800916}
917
Colin Crossca860ac2016-01-04 14:34:37 -0800918func (c *Module) clang(ctx BaseModuleContext) bool {
919 clang := Bool(c.Properties.Clang)
920
921 if c.Properties.Clang == nil {
922 if ctx.Host() {
923 clang = true
924 }
925
926 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
927 clang = true
928 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800929 }
Colin Cross28344522015-04-22 13:07:53 -0700930
Colin Crossca860ac2016-01-04 14:34:37 -0800931 if !c.toolchain(ctx).ClangSupported() {
932 clang = false
933 }
934
935 return clang
936}
937
Colin Crossc99deeb2016-04-11 15:06:20 -0700938// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700939func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800940 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800941
Dan Willemsena96ff642016-06-07 12:34:45 -0700942 // Whether a module can link to another module, taking into
943 // account NDK linking.
944 linkTypeOk := func(from, to *Module) bool {
945 if from.Target().Os != android.Android {
946 // Host code is not restricted
947 return true
948 }
949 if from.Properties.Sdk_version == "" {
950 // Platform code can link to anything
951 return true
952 }
953 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
954 // These are always allowed
955 return true
956 }
957 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
958 // These are allowed, but don't set sdk_version
959 return true
960 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700961 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
962 // These are allowed, but don't set sdk_version
963 return true
964 }
965 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700966 }
967
Colin Crossc99deeb2016-04-11 15:06:20 -0700968 ctx.VisitDirectDeps(func(m blueprint.Module) {
969 name := ctx.OtherModuleName(m)
970 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800971
Colin Cross635c3b02016-05-18 15:37:25 -0700972 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700973 if a == nil {
974 ctx.ModuleErrorf("module %q not an android module", name)
975 return
Colin Crossca860ac2016-01-04 14:34:37 -0800976 }
Colin Crossca860ac2016-01-04 14:34:37 -0800977
Dan Willemsena96ff642016-06-07 12:34:45 -0700978 cc, _ := m.(*Module)
979 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700980 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700981 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700982 case genSourceDepTag:
983 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
984 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
985 genRule.GeneratedSourceFiles()...)
986 } else {
987 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
988 }
989 case genHeaderDepTag:
990 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
991 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
992 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700993 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700994 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700995 } else {
996 ctx.ModuleErrorf("module %q is not a genrule", name)
997 }
998 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700999 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001000 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001001 return
1002 }
1003
1004 if !a.Enabled() {
1005 ctx.ModuleErrorf("depends on disabled module %q", name)
1006 return
1007 }
1008
Colin Crossa1ad8d12016-06-01 17:09:44 -07001009 if a.Target().Os != ctx.Os() {
1010 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1011 return
1012 }
1013
1014 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1015 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001016 return
1017 }
1018
Dan Willemsena96ff642016-06-07 12:34:45 -07001019 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001020 ctx.ModuleErrorf("module %q missing output file", name)
1021 return
1022 }
1023
1024 if tag == reuseObjTag {
1025 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001026 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001027 return
1028 }
1029
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001030 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001031 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001032 flags := i.exportedFlags()
1033 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001034
1035 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001036 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001037 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001038 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001039
1040 if !linkTypeOk(c, cc) {
1041 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1042 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001043 }
1044
Colin Cross635c3b02016-05-18 15:37:25 -07001045 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001046
1047 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001048 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001049 depPtr = &depPaths.SharedLibs
1050 case lateSharedDepTag:
1051 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001052 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001053 depPtr = &depPaths.StaticLibs
1054 case lateStaticDepTag:
1055 depPtr = &depPaths.LateStaticLibs
1056 case wholeStaticDepTag:
1057 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -07001058 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -07001059 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001060 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001061 return
1062 }
1063
1064 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1065 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1066 for i := range missingDeps {
1067 missingDeps[i] += postfix
1068 }
1069 ctx.AddMissingDependencies(missingDeps)
1070 }
1071 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -07001072 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001073 case objDepTag:
1074 depPtr = &depPaths.ObjFiles
1075 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001076 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001077 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001078 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001079 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001080 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001081 }
1082
1083 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001084 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001085 }
1086 })
1087
1088 return depPaths
1089}
1090
1091func (c *Module) InstallInData() bool {
1092 if c.installer == nil {
1093 return false
1094 }
1095 return c.installer.inData()
1096}
1097
1098// Compiler
1099
1100type baseCompiler struct {
1101 Properties BaseCompilerProperties
1102}
1103
1104var _ compiler = (*baseCompiler)(nil)
1105
1106func (compiler *baseCompiler) props() []interface{} {
1107 return []interface{}{&compiler.Properties}
1108}
1109
Dan Willemsenb40aab62016-04-20 14:21:14 -07001110func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1111
1112func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1113 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1114 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1115
1116 return deps
1117}
Colin Crossca860ac2016-01-04 14:34:37 -08001118
1119// Create a Flags struct that collects the compile flags from global values,
1120// per-target values, module type values, and per-module Blueprints properties
1121func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1122 toolchain := ctx.toolchain()
1123
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001124 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1125 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1126 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1127 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1128
Colin Crossca860ac2016-01-04 14:34:37 -08001129 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1130 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1131 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1132 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1133 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1134
Colin Cross28344522015-04-22 13:07:53 -07001135 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001136 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1137 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001138 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001139 includeDirsToFlags(localIncludeDirs),
1140 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001141
Colin Crossca860ac2016-01-04 14:34:37 -08001142 if !ctx.noDefaultCompilerFlags() {
1143 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001144 flags.GlobalFlags = append(flags.GlobalFlags,
1145 "${commonGlobalIncludes}",
1146 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001147 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001148 }
1149
1150 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001151 "-I" + android.PathForModuleSrc(ctx).String(),
1152 "-I" + android.PathForModuleOut(ctx).String(),
1153 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001154 }...)
1155 }
1156
Colin Crossca860ac2016-01-04 14:34:37 -08001157 instructionSet := compiler.Properties.Instruction_set
1158 if flags.RequiredInstructionSet != "" {
1159 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001161 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1162 if flags.Clang {
1163 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1164 }
1165 if err != nil {
1166 ctx.ModuleErrorf("%s", err)
1167 }
1168
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001169 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1170
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001171 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001172 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001173
Colin Cross97ba0732015-03-23 17:50:24 -07001174 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001175 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1176 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1177
Colin Cross97ba0732015-03-23 17:50:24 -07001178 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001179 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1180 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001181 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1182 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1183 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001184
1185 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001186 var gccPrefix string
1187 if !ctx.Darwin() {
1188 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1189 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001190
Colin Cross97ba0732015-03-23 17:50:24 -07001191 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1192 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1193 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001194 }
1195
Colin Crossa1ad8d12016-06-01 17:09:44 -07001196 hod := "host"
1197 if ctx.Os().Class == android.Device {
1198 hod = "device"
1199 }
1200
Colin Crossca860ac2016-01-04 14:34:37 -08001201 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001202 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1203
Colin Cross97ba0732015-03-23 17:50:24 -07001204 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001205 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001206 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001207 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001208 toolchain.ClangCflags(),
1209 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001210 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001211
1212 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001213 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001214 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001215 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001216 toolchain.Cflags(),
1217 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001218 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001219 }
1220
Colin Cross7b66f152015-12-15 16:07:43 -08001221 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1222 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1223 }
1224
Colin Crossf6566ed2015-03-24 11:13:38 -07001225 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001226 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001227 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001228 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001229 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001230 }
1231 }
1232
Colin Cross97ba0732015-03-23 17:50:24 -07001233 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001234
Colin Cross97ba0732015-03-23 17:50:24 -07001235 if flags.Clang {
1236 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001237 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001238 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001239 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001240 }
1241
Colin Crossc4bde762015-11-23 16:11:30 -08001242 if flags.Clang {
1243 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1244 } else {
1245 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001246 }
1247
Colin Crossca860ac2016-01-04 14:34:37 -08001248 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001249 if ctx.Host() && !flags.Clang {
1250 // The host GCC doesn't support C++14 (and is deprecated, so likely
1251 // never will). Build these modules with C++11.
1252 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1253 } else {
1254 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1255 }
1256 }
1257
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001258 // We can enforce some rules more strictly in the code we own. strict
1259 // indicates if this is code that we can be stricter with. If we have
1260 // rules that we want to apply to *our* code (but maybe can't for
1261 // vendor/device specific things), we could extend this to be a ternary
1262 // value.
1263 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001264 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001265 strict = false
1266 }
1267
1268 // Can be used to make some annotations stricter for code we can fix
1269 // (such as when we mark functions as deprecated).
1270 if strict {
1271 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1272 }
1273
Colin Cross3f40fa42015-01-30 17:27:36 -08001274 return flags
1275}
1276
Colin Cross635c3b02016-05-18 15:37:25 -07001277func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001278 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001279 objFiles := compiler.compileObjs(ctx, flags, "",
1280 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1281 deps.GeneratedSources, deps.GeneratedHeaders)
1282
Colin Crossca860ac2016-01-04 14:34:37 -08001283 if ctx.Failed() {
1284 return nil
1285 }
1286
Colin Crossca860ac2016-01-04 14:34:37 -08001287 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001288}
1289
1290// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001291func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1292 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001293
Colin Crossca860ac2016-01-04 14:34:37 -08001294 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001295
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001296 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001297 inputFiles = append(inputFiles, extraSrcs...)
1298 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1299
1300 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001301 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001302
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001304}
1305
Colin Crossca860ac2016-01-04 14:34:37 -08001306// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1307type baseLinker struct {
1308 Properties BaseLinkerProperties
1309 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001310 VariantIsShared bool `blueprint:"mutated"`
1311 VariantIsStatic bool `blueprint:"mutated"`
1312 VariantIsStaticBinary bool `blueprint:"mutated"`
1313 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001314 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001315}
1316
Dan Willemsend30e6102016-03-30 17:35:50 -07001317func (linker *baseLinker) begin(ctx BaseModuleContext) {
1318 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001319 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001320 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001321 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001322 }
1323}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001324
Colin Crossca860ac2016-01-04 14:34:37 -08001325func (linker *baseLinker) props() []interface{} {
1326 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001327}
1328
Colin Crossca860ac2016-01-04 14:34:37 -08001329func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1330 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1331 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1332 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001333
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001334 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1335 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1336
Dan Willemsena96ff642016-06-07 12:34:45 -07001337 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Stephen Hines10347862016-07-18 15:54:54 -07001338 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001339 }
1340
Colin Crossf6566ed2015-03-24 11:13:38 -07001341 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001342 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001343 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1344 if !Bool(linker.Properties.No_libgcc) {
1345 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001346 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001347
Colin Crossca860ac2016-01-04 14:34:37 -08001348 if !linker.static() {
1349 if linker.Properties.System_shared_libs != nil {
1350 deps.LateSharedLibs = append(deps.LateSharedLibs,
1351 linker.Properties.System_shared_libs...)
1352 } else if !ctx.sdk() {
1353 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1354 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001355 }
Colin Cross577f6e42015-03-27 18:23:34 -07001356
Colin Crossca860ac2016-01-04 14:34:37 -08001357 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001358 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001359 "libc",
1360 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001361 )
1362 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001363 }
1364
Colin Crossca860ac2016-01-04 14:34:37 -08001365 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001366}
1367
Colin Crossca860ac2016-01-04 14:34:37 -08001368func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1369 toolchain := ctx.toolchain()
1370
Colin Crossa89d2e12016-01-11 12:48:37 -08001371 flags.Nocrt = Bool(linker.Properties.Nocrt)
1372
Colin Crossca860ac2016-01-04 14:34:37 -08001373 if !ctx.noDefaultCompilerFlags() {
1374 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1375 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1376 }
1377
1378 if flags.Clang {
1379 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1380 } else {
1381 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1382 }
1383
1384 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001385 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1386
Colin Crossca860ac2016-01-04 14:34:37 -08001387 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1388 }
1389 }
1390
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001391 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1392
Dan Willemsen00ced762016-05-10 17:31:21 -07001393 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1394
Dan Willemsend30e6102016-03-30 17:35:50 -07001395 if ctx.Host() && !linker.static() {
1396 rpath_prefix := `\$$ORIGIN/`
1397 if ctx.Darwin() {
1398 rpath_prefix = "@loader_path/"
1399 }
1400
Colin Crossc99deeb2016-04-11 15:06:20 -07001401 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001402 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1403 }
1404 }
1405
Dan Willemsene7174922016-03-30 17:33:52 -07001406 if flags.Clang {
1407 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1408 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001409 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1410 }
1411
1412 return flags
1413}
1414
1415func (linker *baseLinker) static() bool {
1416 return linker.dynamicProperties.VariantIsStatic
1417}
1418
1419func (linker *baseLinker) staticBinary() bool {
1420 return linker.dynamicProperties.VariantIsStaticBinary
1421}
1422
1423func (linker *baseLinker) setStatic(static bool) {
1424 linker.dynamicProperties.VariantIsStatic = static
1425}
1426
Colin Cross16b23492016-01-06 14:41:07 -08001427func (linker *baseLinker) isDependencyRoot() bool {
1428 return false
1429}
1430
Colin Crossca860ac2016-01-04 14:34:37 -08001431type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001432 // Returns true if the build options for the module have selected a static or shared build
1433 buildStatic() bool
1434 buildShared() bool
1435
1436 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001437 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001438
Colin Cross18b6dc52015-04-28 13:20:37 -07001439 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001440 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001441
1442 // Returns whether a module is a static binary
1443 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001444
1445 // Returns true for dependency roots (binaries)
1446 // TODO(ccross): also handle dlopenable libraries
1447 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001448}
1449
Colin Crossca860ac2016-01-04 14:34:37 -08001450type baseInstaller struct {
1451 Properties InstallerProperties
1452
1453 dir string
1454 dir64 string
1455 data bool
1456
Colin Cross635c3b02016-05-18 15:37:25 -07001457 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001458}
1459
1460var _ installer = (*baseInstaller)(nil)
1461
1462func (installer *baseInstaller) props() []interface{} {
1463 return []interface{}{&installer.Properties}
1464}
1465
Colin Cross635c3b02016-05-18 15:37:25 -07001466func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001467 subDir := installer.dir
1468 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1469 subDir = installer.dir64
1470 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001471 if !ctx.Host() && !ctx.Arch().Native {
1472 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1473 }
Colin Cross635c3b02016-05-18 15:37:25 -07001474 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001475 installer.path = ctx.InstallFile(dir, file)
Colin Cross3854a602016-01-11 12:49:11 -08001476 for _, symlink := range installer.Properties.Symlinks {
1477 ctx.InstallSymlink(dir, symlink, installer.path)
1478 }
Colin Crossca860ac2016-01-04 14:34:37 -08001479}
1480
1481func (installer *baseInstaller) inData() bool {
1482 return installer.data
1483}
1484
Colin Cross3f40fa42015-01-30 17:27:36 -08001485//
1486// Combined static+shared libraries
1487//
1488
Colin Cross919281a2016-04-05 16:42:05 -07001489type flagExporter struct {
1490 Properties FlagExporterProperties
1491
1492 flags []string
1493}
1494
1495func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001496 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001497 for _, dir := range includeDirs.Strings() {
Colin Crossf87b2612016-07-13 18:55:43 -07001498 f.flags = append(f.flags, inc+dir)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001499 }
Colin Cross919281a2016-04-05 16:42:05 -07001500}
1501
1502func (f *flagExporter) reexportFlags(flags []string) {
1503 f.flags = append(f.flags, flags...)
1504}
1505
1506func (f *flagExporter) exportedFlags() []string {
1507 return f.flags
1508}
1509
1510type exportedFlagsProducer interface {
1511 exportedFlags() []string
1512}
1513
1514var _ exportedFlagsProducer = (*flagExporter)(nil)
1515
Colin Crossca860ac2016-01-04 14:34:37 -08001516type libraryCompiler struct {
1517 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001518
Colin Crossca860ac2016-01-04 14:34:37 -08001519 linker *libraryLinker
1520 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001521
Colin Crossca860ac2016-01-04 14:34:37 -08001522 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001523 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001524}
1525
Colin Crossca860ac2016-01-04 14:34:37 -08001526var _ compiler = (*libraryCompiler)(nil)
1527
1528func (library *libraryCompiler) props() []interface{} {
1529 props := library.baseCompiler.props()
1530 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001531}
1532
Colin Crossca860ac2016-01-04 14:34:37 -08001533func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1534 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001535
Dan Willemsen490fd492015-11-24 17:53:15 -08001536 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1537 // all code is position independent, and then those warnings get promoted to
1538 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001539 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001540 flags.CFlags = append(flags.CFlags, "-fPIC")
1541 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001542
Colin Crossca860ac2016-01-04 14:34:37 -08001543 if library.linker.static() {
1544 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001545 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001546 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001547 }
1548
Colin Crossca860ac2016-01-04 14:34:37 -08001549 return flags
1550}
1551
Colin Cross635c3b02016-05-18 15:37:25 -07001552func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1553 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001554
Dan Willemsenb40aab62016-04-20 14:21:14 -07001555 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001556 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001557
1558 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001559 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001560 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1561 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001562 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001563 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001564 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1565 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001566 }
1567
1568 return objFiles
1569}
1570
1571type libraryLinker struct {
1572 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001573 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001574 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001575
1576 Properties LibraryLinkerProperties
1577
1578 dynamicProperties struct {
1579 BuildStatic bool `blueprint:"mutated"`
1580 BuildShared bool `blueprint:"mutated"`
1581 }
1582
Colin Crossca860ac2016-01-04 14:34:37 -08001583 // If we're used as a whole_static_lib, our missing dependencies need
1584 // to be given
1585 wholeStaticMissingDeps []string
1586
1587 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001588 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001589}
1590
1591var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001592
Colin Crossc7a38dc2016-07-12 13:13:09 -07001593type libraryInterface interface {
1594 getWholeStaticMissingDeps() []string
1595 static() bool
1596 objs() android.Paths
1597}
1598
Colin Crossca860ac2016-01-04 14:34:37 -08001599func (library *libraryLinker) props() []interface{} {
1600 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001601 return append(props,
1602 &library.Properties,
1603 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001604 &library.flagExporter.Properties,
1605 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001606}
1607
1608func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1609 flags = library.baseLinker.flags(ctx, flags)
1610
Colin Crossca860ac2016-01-04 14:34:37 -08001611 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001612 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001613 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1614 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001615 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001616 sharedFlag = "-shared"
1617 }
Colin Crossf87b2612016-07-13 18:55:43 -07001618 var f []string
Colin Crossf6566ed2015-03-24 11:13:38 -07001619 if ctx.Device() {
Colin Crossf87b2612016-07-13 18:55:43 -07001620 f = append(f,
Dan Willemsen99db8c32016-03-03 18:05:38 -08001621 "-nostdlib",
1622 "-Wl,--gc-sections",
1623 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001624 }
Colin Cross97ba0732015-03-23 17:50:24 -07001625
Colin Cross0af4b842015-04-30 16:36:18 -07001626 if ctx.Darwin() {
Colin Crossf87b2612016-07-13 18:55:43 -07001627 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001628 "-dynamiclib",
1629 "-single_module",
1630 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001631 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001632 )
1633 } else {
Colin Crossf87b2612016-07-13 18:55:43 -07001634 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001635 sharedFlag,
Colin Crossf87b2612016-07-13 18:55:43 -07001636 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
Colin Cross0af4b842015-04-30 16:36:18 -07001637 }
Colin Crossf87b2612016-07-13 18:55:43 -07001638
1639 flags.LdFlags = append(f, flags.LdFlags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001640 }
Colin Cross97ba0732015-03-23 17:50:24 -07001641
1642 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001643}
1644
Colin Crossca860ac2016-01-04 14:34:37 -08001645func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1646 deps = library.baseLinker.deps(ctx, deps)
1647 if library.static() {
1648 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1649 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1650 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1651 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001652 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
Colin Crossca860ac2016-01-04 14:34:37 -08001653 if !ctx.sdk() {
1654 deps.CrtBegin = "crtbegin_so"
1655 deps.CrtEnd = "crtend_so"
1656 } else {
1657 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1658 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1659 }
1660 }
1661 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1662 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1663 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1664 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001665
Colin Crossca860ac2016-01-04 14:34:37 -08001666 return deps
1667}
Colin Cross3f40fa42015-01-30 17:27:36 -08001668
Colin Crossca860ac2016-01-04 14:34:37 -08001669func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001670 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001671
Colin Cross635c3b02016-05-18 15:37:25 -07001672 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001673 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001674
Colin Cross635c3b02016-05-18 15:37:25 -07001675 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001676 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001677
Colin Cross0af4b842015-04-30 16:36:18 -07001678 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001679 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001680 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001681 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001682 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001683
Colin Crossca860ac2016-01-04 14:34:37 -08001684 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001685
1686 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001687
1688 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001689}
1690
Colin Crossca860ac2016-01-04 14:34:37 -08001691func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001692 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001693
Colin Cross635c3b02016-05-18 15:37:25 -07001694 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001695
Colin Cross635c3b02016-05-18 15:37:25 -07001696 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1697 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1698 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1699 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001700 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001701 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001702 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001703 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001704 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001705 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001706 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1707 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001708 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001709 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1710 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001711 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001712 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1713 }
1714 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001715 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001716 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1717 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001718 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001719 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001720 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001721 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001722 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001723 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001724 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001725 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001726 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001727 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001728 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001729 }
Colin Crossaee540a2015-07-06 17:48:31 -07001730 }
1731
Colin Cross665dce92016-04-28 14:50:03 -07001732 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001733 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001734 ret := outputFile
1735
1736 builderFlags := flagsToBuilderFlags(flags)
1737
1738 if library.stripper.needsStrip(ctx) {
1739 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001740 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001741 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1742 }
1743
Colin Crossca860ac2016-01-04 14:34:37 -08001744 sharedLibs := deps.SharedLibs
1745 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001746
Colin Crossca860ac2016-01-04 14:34:37 -08001747 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1748 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001749 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001750
Colin Cross665dce92016-04-28 14:50:03 -07001751 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001752}
1753
Colin Crossca860ac2016-01-04 14:34:37 -08001754func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001755 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001756
Colin Crossc99deeb2016-04-11 15:06:20 -07001757 objFiles = append(objFiles, deps.ObjFiles...)
1758
Colin Cross635c3b02016-05-18 15:37:25 -07001759 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001760 if library.static() {
1761 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001762 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001763 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001764 }
1765
Colin Cross919281a2016-04-05 16:42:05 -07001766 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001767 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001768
1769 return out
1770}
1771
1772func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001773 return library.dynamicProperties.BuildStatic &&
1774 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001775}
1776
1777func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001778 return library.dynamicProperties.BuildShared &&
1779 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001780}
1781
1782func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1783 return library.wholeStaticMissingDeps
1784}
1785
Colin Crossc99deeb2016-04-11 15:06:20 -07001786func (library *libraryLinker) installable() bool {
1787 return !library.static()
1788}
1789
Colin Crossc7a38dc2016-07-12 13:13:09 -07001790func (library *libraryLinker) objs() android.Paths {
1791 return library.objFiles
1792}
1793
Colin Crossca860ac2016-01-04 14:34:37 -08001794type libraryInstaller struct {
1795 baseInstaller
1796
Colin Cross30d5f512016-05-03 18:02:42 -07001797 linker *libraryLinker
1798 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001799}
1800
Colin Cross635c3b02016-05-18 15:37:25 -07001801func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001802 if !library.linker.static() {
1803 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001804 }
1805}
1806
Colin Cross30d5f512016-05-03 18:02:42 -07001807func (library *libraryInstaller) inData() bool {
1808 return library.baseInstaller.inData() || library.sanitize.inData()
1809}
1810
Colin Cross635c3b02016-05-18 15:37:25 -07001811func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1812 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001813
Colin Crossca860ac2016-01-04 14:34:37 -08001814 linker := &libraryLinker{}
1815 linker.dynamicProperties.BuildShared = shared
1816 linker.dynamicProperties.BuildStatic = static
1817 module.linker = linker
1818
1819 module.compiler = &libraryCompiler{
1820 linker: linker,
1821 }
1822 module.installer = &libraryInstaller{
1823 baseInstaller: baseInstaller{
1824 dir: "lib",
1825 dir64: "lib64",
1826 },
Colin Cross30d5f512016-05-03 18:02:42 -07001827 linker: linker,
1828 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001829 }
1830
Colin Crossca860ac2016-01-04 14:34:37 -08001831 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001832}
1833
Colin Crossca860ac2016-01-04 14:34:37 -08001834func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001835 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001836 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001837}
1838
Colin Cross3f40fa42015-01-30 17:27:36 -08001839//
1840// Objects (for crt*.o)
1841//
1842
Colin Crossca860ac2016-01-04 14:34:37 -08001843type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001844 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001845}
1846
Colin Crossca860ac2016-01-04 14:34:37 -08001847func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001848 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001849 module.compiler = &baseCompiler{}
1850 module.linker = &objectLinker{}
1851 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001852}
1853
Colin Cross81413472016-04-11 14:37:39 -07001854func (object *objectLinker) props() []interface{} {
1855 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001856}
1857
Colin Crossca860ac2016-01-04 14:34:37 -08001858func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001859
Colin Cross81413472016-04-11 14:37:39 -07001860func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1861 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001862 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001863}
1864
Colin Crossca860ac2016-01-04 14:34:37 -08001865func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001866 if flags.Clang {
1867 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1868 } else {
1869 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1870 }
1871
Colin Crossca860ac2016-01-04 14:34:37 -08001872 return flags
1873}
1874
1875func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001876 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001877
Colin Cross97ba0732015-03-23 17:50:24 -07001878 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001879
Colin Cross635c3b02016-05-18 15:37:25 -07001880 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001881 if len(objFiles) == 1 {
1882 outputFile = objFiles[0]
1883 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001884 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001885 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001886 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001887 }
1888
Colin Cross3f40fa42015-01-30 17:27:36 -08001889 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001890 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001891}
1892
Colin Crossc99deeb2016-04-11 15:06:20 -07001893func (*objectLinker) installable() bool {
1894 return false
1895}
1896
Colin Cross3f40fa42015-01-30 17:27:36 -08001897//
1898// Executables
1899//
1900
Colin Crossca860ac2016-01-04 14:34:37 -08001901type binaryLinker struct {
1902 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001903 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001904
Colin Crossca860ac2016-01-04 14:34:37 -08001905 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001906
Colin Cross635c3b02016-05-18 15:37:25 -07001907 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001908}
1909
Colin Crossca860ac2016-01-04 14:34:37 -08001910var _ linker = (*binaryLinker)(nil)
1911
1912func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001913 return append(binary.baseLinker.props(),
1914 &binary.Properties,
1915 &binary.stripper.StripProperties)
1916
Colin Cross3f40fa42015-01-30 17:27:36 -08001917}
1918
Colin Crossca860ac2016-01-04 14:34:37 -08001919func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001920 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001921}
1922
Colin Crossca860ac2016-01-04 14:34:37 -08001923func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001924 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001925}
1926
Colin Crossca860ac2016-01-04 14:34:37 -08001927func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001928 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001929 if binary.Properties.Stem != "" {
1930 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001931 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001932
Colin Crossca860ac2016-01-04 14:34:37 -08001933 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001934}
1935
Colin Crossca860ac2016-01-04 14:34:37 -08001936func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1937 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001938 if ctx.Device() {
Colin Crossa89d2e12016-01-11 12:48:37 -08001939 if !Bool(binary.baseLinker.Properties.Nocrt) {
1940 if !ctx.sdk() {
1941 if binary.buildStatic() {
1942 deps.CrtBegin = "crtbegin_static"
1943 } else {
1944 deps.CrtBegin = "crtbegin_dynamic"
1945 }
1946 deps.CrtEnd = "crtend_android"
Dan Albertc3144b12015-04-28 18:17:56 -07001947 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001948 if binary.buildStatic() {
1949 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
1950 } else {
1951 if Bool(binary.Properties.Static_executable) {
1952 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
1953 } else {
1954 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
1955 }
1956 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
1957 }
Dan Albertc3144b12015-04-28 18:17:56 -07001958 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001959 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001960
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001961 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001962 if inList("libc++_static", deps.StaticLibs) {
1963 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001964 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001965 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1966 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1967 // move them to the beginning of deps.LateStaticLibs
1968 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001969 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001970 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001971 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001972 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001973 }
Colin Crossca860ac2016-01-04 14:34:37 -08001974
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001975 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001976 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1977 "from static libs or set static_executable: true")
1978 }
1979 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001980}
1981
Colin Crossc99deeb2016-04-11 15:06:20 -07001982func (*binaryLinker) installable() bool {
1983 return true
1984}
1985
Colin Cross16b23492016-01-06 14:41:07 -08001986func (binary *binaryLinker) isDependencyRoot() bool {
1987 return true
1988}
1989
Colin Cross635c3b02016-05-18 15:37:25 -07001990func NewBinary(hod android.HostOrDeviceSupported) *Module {
1991 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001992 module.compiler = &baseCompiler{}
1993 module.linker = &binaryLinker{}
1994 module.installer = &baseInstaller{
1995 dir: "bin",
1996 }
1997 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001998}
1999
Colin Crossca860ac2016-01-04 14:34:37 -08002000func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002001 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002002 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002003}
2004
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002005func (binary *binaryLinker) begin(ctx BaseModuleContext) {
2006 binary.baseLinker.begin(ctx)
2007
2008 static := Bool(binary.Properties.Static_executable)
2009 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07002010 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002011 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
2012 static = true
2013 }
2014 } else {
2015 // Static executables are not supported on Darwin or Windows
2016 static = false
2017 }
Colin Cross0af4b842015-04-30 16:36:18 -07002018 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002019 if static {
2020 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08002021 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07002022 }
2023}
2024
Colin Crossca860ac2016-01-04 14:34:37 -08002025func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2026 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002027
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002028 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002029 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002030 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002031 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2032 }
2033 }
2034
2035 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2036 // all code is position independent, and then those warnings get promoted to
2037 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002038 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002039 flags.CFlags = append(flags.CFlags, "-fpie")
2040 }
Colin Cross97ba0732015-03-23 17:50:24 -07002041
Colin Crossf6566ed2015-03-24 11:13:38 -07002042 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002043 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002044 // Clang driver needs -static to create static executable.
2045 // However, bionic/linker uses -shared to overwrite.
2046 // Linker for x86 targets does not allow coexistance of -static and -shared,
2047 // so we add -static only if -shared is not used.
2048 if !inList("-shared", flags.LdFlags) {
2049 flags.LdFlags = append(flags.LdFlags, "-static")
2050 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002051
Colin Crossed4cf0b2015-03-26 14:43:45 -07002052 flags.LdFlags = append(flags.LdFlags,
2053 "-nostdlib",
2054 "-Bstatic",
2055 "-Wl,--gc-sections",
2056 )
2057
2058 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002059 if flags.DynamicLinker == "" {
2060 flags.DynamicLinker = "/system/bin/linker"
2061 if flags.Toolchain.Is64Bit() {
2062 flags.DynamicLinker += "64"
2063 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002064 }
2065
2066 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002067 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002068 "-nostdlib",
2069 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002070 "-Wl,--gc-sections",
2071 "-Wl,-z,nocopyreloc",
2072 )
2073 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002074 } else {
2075 if binary.staticBinary() {
2076 flags.LdFlags = append(flags.LdFlags, "-static")
2077 }
2078 if ctx.Darwin() {
2079 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2080 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002081 }
2082
Colin Cross97ba0732015-03-23 17:50:24 -07002083 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002084}
2085
Colin Crossca860ac2016-01-04 14:34:37 -08002086func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002087 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002088
Colin Cross665dce92016-04-28 14:50:03 -07002089 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002090 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002091 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002092 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002093 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002094 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002095
Colin Cross635c3b02016-05-18 15:37:25 -07002096 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002097
Colin Crossca860ac2016-01-04 14:34:37 -08002098 sharedLibs := deps.SharedLibs
2099 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2100
Colin Cross16b23492016-01-06 14:41:07 -08002101 if flags.DynamicLinker != "" {
2102 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2103 }
2104
Colin Cross665dce92016-04-28 14:50:03 -07002105 builderFlags := flagsToBuilderFlags(flags)
2106
2107 if binary.stripper.needsStrip(ctx) {
2108 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002109 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002110 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2111 }
2112
2113 if binary.Properties.Prefix_symbols != "" {
2114 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002115 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002116 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2117 flagsToBuilderFlags(flags), afterPrefixSymbols)
2118 }
2119
Colin Crossca860ac2016-01-04 14:34:37 -08002120 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002121 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002122 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002123
2124 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002125}
Colin Cross3f40fa42015-01-30 17:27:36 -08002126
Colin Cross635c3b02016-05-18 15:37:25 -07002127func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002128 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002129}
2130
Colin Cross665dce92016-04-28 14:50:03 -07002131type stripper struct {
2132 StripProperties StripProperties
2133}
2134
2135func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2136 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2137}
2138
Colin Cross635c3b02016-05-18 15:37:25 -07002139func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002140 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002141 if ctx.Darwin() {
2142 TransformDarwinStrip(ctx, in, out)
2143 } else {
2144 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2145 // TODO(ccross): don't add gnu debuglink for user builds
2146 flags.stripAddGnuDebuglink = true
2147 TransformStrip(ctx, in, out, flags)
2148 }
Colin Cross665dce92016-04-28 14:50:03 -07002149}
2150
Colin Cross635c3b02016-05-18 15:37:25 -07002151func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002152 if m, ok := mctx.Module().(*Module); ok {
Colin Crossc7a38dc2016-07-12 13:13:09 -07002153 if test, ok := m.linker.(*testBinaryLinker); ok {
2154 if Bool(test.testLinker.Properties.Test_per_src) {
Colin Crossca860ac2016-01-04 14:34:37 -08002155 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2156 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2157 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2158 }
2159 tests := mctx.CreateLocalVariations(testNames...)
2160 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2161 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002162 tests[i].(*Module).linker.(*testBinaryLinker).binaryLinker.Properties.Stem = testNames[i]
Colin Crossca860ac2016-01-04 14:34:37 -08002163 }
Colin Cross6002e052015-09-16 16:00:08 -07002164 }
2165 }
2166 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002167}
2168
Colin Crossca860ac2016-01-04 14:34:37 -08002169type testLinker struct {
Colin Crossca860ac2016-01-04 14:34:37 -08002170 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002171}
2172
Colin Crossca860ac2016-01-04 14:34:37 -08002173func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -08002174 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002175 return flags
2176 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002177
Colin Cross97ba0732015-03-23 17:50:24 -07002178 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002179 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002180 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002181
Colin Crossa1ad8d12016-06-01 17:09:44 -07002182 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002183 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002184 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002185 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002186 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2187 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002188 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002189 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2190 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002191 }
2192 } else {
2193 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002194 }
2195
Colin Cross21b9a242015-03-24 14:15:58 -07002196 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002197}
2198
Colin Crossca860ac2016-01-04 14:34:37 -08002199func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2200 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002201 if ctx.sdk() && ctx.Device() {
2202 switch ctx.selectedStl() {
2203 case "ndk_libc++_shared", "ndk_libc++_static":
2204 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2205 case "ndk_libgnustl_static":
2206 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2207 default:
2208 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2209 }
2210 } else {
2211 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2212 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002213 }
Colin Crossc7a38dc2016-07-12 13:13:09 -07002214 return deps
2215}
2216
2217type testBinaryLinker struct {
2218 testLinker
2219 binaryLinker
2220}
2221
2222func (test *testBinaryLinker) begin(ctx BaseModuleContext) {
2223 test.binaryLinker.begin(ctx)
2224 runpath := "../../lib"
2225 if ctx.toolchain().Is64Bit() {
2226 runpath += "64"
2227 }
2228 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
2229}
2230
2231func (test *testBinaryLinker) props() []interface{} {
2232 return append(test.binaryLinker.props(), &test.testLinker.Properties)
2233}
2234
2235func (test *testBinaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2236 flags = test.binaryLinker.flags(ctx, flags)
2237 flags = test.testLinker.flags(ctx, flags)
2238 return flags
2239}
2240
2241func (test *testBinaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2242 deps = test.testLinker.deps(ctx, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08002243 deps = test.binaryLinker.deps(ctx, deps)
2244 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002245}
2246
Colin Crossc7a38dc2016-07-12 13:13:09 -07002247type testLibraryLinker struct {
2248 testLinker
2249 *libraryLinker
2250}
2251
2252func (test *testLibraryLinker) props() []interface{} {
2253 return append(test.libraryLinker.props(), &test.testLinker.Properties)
2254}
2255
2256func (test *testLibraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2257 flags = test.libraryLinker.flags(ctx, flags)
2258 flags = test.testLinker.flags(ctx, flags)
2259 return flags
2260}
2261
2262func (test *testLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2263 deps = test.testLinker.deps(ctx, deps)
2264 deps = test.libraryLinker.deps(ctx, deps)
2265 return deps
2266}
2267
Colin Crossca860ac2016-01-04 14:34:37 -08002268type testInstaller struct {
2269 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002270}
2271
Colin Cross635c3b02016-05-18 15:37:25 -07002272func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002273 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2274 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2275 installer.baseInstaller.install(ctx, file)
2276}
2277
Colin Cross635c3b02016-05-18 15:37:25 -07002278func NewTest(hod android.HostOrDeviceSupported) *Module {
2279 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002280 module.compiler = &baseCompiler{}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002281 linker := &testBinaryLinker{}
2282 linker.testLinker.Properties.Gtest = true
Colin Crossca860ac2016-01-04 14:34:37 -08002283 module.linker = linker
2284 module.installer = &testInstaller{
2285 baseInstaller: baseInstaller{
2286 dir: "nativetest",
2287 dir64: "nativetest64",
2288 data: true,
2289 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002290 }
Colin Crossca860ac2016-01-04 14:34:37 -08002291 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002292}
2293
Colin Crossca860ac2016-01-04 14:34:37 -08002294func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002295 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002296 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002297}
2298
Colin Crossc7a38dc2016-07-12 13:13:09 -07002299func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
2300 module := NewLibrary(android.HostAndDeviceSupported, false, true)
2301 linker := &testLibraryLinker{
2302 libraryLinker: module.linker.(*libraryLinker),
2303 }
2304 linker.testLinker.Properties.Gtest = true
2305 module.linker = linker
2306 module.installer = &testInstaller{
2307 baseInstaller: baseInstaller{
2308 dir: "nativetest",
2309 dir64: "nativetest64",
2310 data: true,
2311 },
2312 }
2313 return module
2314}
2315
2316func testLibraryFactory() (blueprint.Module, []interface{}) {
2317 module := NewTestLibrary(android.HostAndDeviceSupported)
2318 return module.Init()
2319}
2320
Colin Crossca860ac2016-01-04 14:34:37 -08002321type benchmarkLinker struct {
Colin Crossaa3bf372016-07-14 10:27:10 -07002322 testBinaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002323}
2324
Colin Crossca860ac2016-01-04 14:34:37 -08002325func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossaa3bf372016-07-14 10:27:10 -07002326 deps = benchmark.testBinaryLinker.deps(ctx, deps)
Colin Cross26832742016-07-11 14:57:56 -07002327 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
Colin Crossca860ac2016-01-04 14:34:37 -08002328 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002329}
2330
Colin Cross635c3b02016-05-18 15:37:25 -07002331func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2332 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002333 module.compiler = &baseCompiler{}
2334 module.linker = &benchmarkLinker{}
Colin Cross624b8ed2016-07-11 17:20:09 -07002335 module.installer = &testInstaller{
2336 baseInstaller: baseInstaller{
2337 dir: "nativetest",
2338 dir64: "nativetest64",
2339 data: true,
2340 },
Colin Cross2ba19d92015-05-07 15:44:20 -07002341 }
Colin Crossca860ac2016-01-04 14:34:37 -08002342 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002343}
2344
Colin Crossca860ac2016-01-04 14:34:37 -08002345func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002346 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002347 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002348}
2349
Colin Cross3f40fa42015-01-30 17:27:36 -08002350//
2351// Static library
2352//
2353
Colin Crossca860ac2016-01-04 14:34:37 -08002354func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002355 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002356 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002357}
2358
2359//
2360// Shared libraries
2361//
2362
Colin Crossca860ac2016-01-04 14:34:37 -08002363func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002364 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002365 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002366}
2367
2368//
2369// Host static library
2370//
2371
Colin Crossca860ac2016-01-04 14:34:37 -08002372func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002373 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002374 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002375}
2376
2377//
2378// Host Shared libraries
2379//
2380
Colin Crossca860ac2016-01-04 14:34:37 -08002381func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002382 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002383 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002384}
2385
2386//
2387// Host Binaries
2388//
2389
Colin Crossca860ac2016-01-04 14:34:37 -08002390func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002391 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002392 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002393}
2394
2395//
Colin Cross1f8f2342015-03-26 16:09:47 -07002396// Host Tests
2397//
2398
Colin Crossca860ac2016-01-04 14:34:37 -08002399func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002400 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002401 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002402}
2403
2404//
Colin Cross2ba19d92015-05-07 15:44:20 -07002405// Host Benchmarks
2406//
2407
Colin Crossca860ac2016-01-04 14:34:37 -08002408func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002409 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002410 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002411}
2412
2413//
Colin Crosscfad1192015-11-02 16:43:11 -08002414// Defaults
2415//
Colin Crossca860ac2016-01-04 14:34:37 -08002416type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002417 android.ModuleBase
2418 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002419}
2420
Colin Cross635c3b02016-05-18 15:37:25 -07002421func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002422}
2423
Colin Crossca860ac2016-01-04 14:34:37 -08002424func defaultsFactory() (blueprint.Module, []interface{}) {
2425 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002426
2427 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002428 &BaseProperties{},
2429 &BaseCompilerProperties{},
2430 &BaseLinkerProperties{},
2431 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002432 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002433 &LibraryLinkerProperties{},
2434 &BinaryLinkerProperties{},
2435 &TestLinkerProperties{},
2436 &UnusedProperties{},
2437 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002438 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002439 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002440 }
2441
Colin Cross635c3b02016-05-18 15:37:25 -07002442 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2443 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002444
Colin Cross635c3b02016-05-18 15:37:25 -07002445 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002446}
2447
2448//
Colin Cross3f40fa42015-01-30 17:27:36 -08002449// Device libraries shipped with gcc
2450//
2451
Colin Crossca860ac2016-01-04 14:34:37 -08002452type toolchainLibraryLinker struct {
2453 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002454}
2455
Colin Crossca860ac2016-01-04 14:34:37 -08002456var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2457
2458func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002459 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002460 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002461}
2462
Colin Crossca860ac2016-01-04 14:34:37 -08002463func (*toolchainLibraryLinker) buildStatic() bool {
2464 return true
2465}
Colin Cross3f40fa42015-01-30 17:27:36 -08002466
Colin Crossca860ac2016-01-04 14:34:37 -08002467func (*toolchainLibraryLinker) buildShared() bool {
2468 return false
2469}
2470
2471func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002472 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002473 module.compiler = &baseCompiler{}
2474 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002475 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002476 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002477}
2478
Colin Crossca860ac2016-01-04 14:34:37 -08002479func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002480 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002481
2482 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002483 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002484
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002485 if flags.Clang {
2486 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2487 }
2488
Colin Crossca860ac2016-01-04 14:34:37 -08002489 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002490
2491 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002492
Colin Crossca860ac2016-01-04 14:34:37 -08002493 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002494}
2495
Colin Crossc99deeb2016-04-11 15:06:20 -07002496func (*toolchainLibraryLinker) installable() bool {
2497 return false
2498}
2499
Dan Albertbe961682015-03-18 23:38:50 -07002500// NDK prebuilt libraries.
2501//
2502// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2503// either (with the exception of the shared STLs, which are installed to the app's directory rather
2504// than to the system image).
2505
Colin Cross635c3b02016-05-18 15:37:25 -07002506func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002507 suffix := ""
2508 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2509 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002510 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002511 suffix = "64"
2512 }
Colin Cross635c3b02016-05-18 15:37:25 -07002513 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002514 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002515}
2516
Colin Cross635c3b02016-05-18 15:37:25 -07002517func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2518 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002519
2520 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2521 // We want to translate to just NAME.EXT
2522 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2523 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002524 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002525}
2526
Colin Crossca860ac2016-01-04 14:34:37 -08002527type ndkPrebuiltObjectLinker struct {
2528 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002529}
2530
Colin Crossca860ac2016-01-04 14:34:37 -08002531func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002532 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002533 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002534}
2535
Colin Crossca860ac2016-01-04 14:34:37 -08002536func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002537 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002538 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002539 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002540 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002541}
2542
Colin Crossca860ac2016-01-04 14:34:37 -08002543func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002544 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002545 // A null build step, but it sets up the output path.
2546 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2547 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2548 }
2549
Colin Crossca860ac2016-01-04 14:34:37 -08002550 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002551}
2552
Colin Crossca860ac2016-01-04 14:34:37 -08002553type ndkPrebuiltLibraryLinker struct {
2554 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002555}
2556
Colin Crossca860ac2016-01-04 14:34:37 -08002557var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2558var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002559
Colin Crossca860ac2016-01-04 14:34:37 -08002560func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002561 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002562}
2563
Colin Crossca860ac2016-01-04 14:34:37 -08002564func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002565 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002566 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002567}
2568
Colin Crossca860ac2016-01-04 14:34:37 -08002569func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002570 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002571 linker := &ndkPrebuiltLibraryLinker{}
2572 linker.dynamicProperties.BuildShared = true
2573 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002574 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002575 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002576}
2577
Colin Crossca860ac2016-01-04 14:34:37 -08002578func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002579 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002580 // A null build step, but it sets up the output path.
2581 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2582 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2583 }
2584
Colin Cross919281a2016-04-05 16:42:05 -07002585 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002586
Colin Crossca860ac2016-01-04 14:34:37 -08002587 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2588 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002589}
2590
2591// The NDK STLs are slightly different from the prebuilt system libraries:
2592// * Are not specific to each platform version.
2593// * The libraries are not in a predictable location for each STL.
2594
Colin Crossca860ac2016-01-04 14:34:37 -08002595type ndkPrebuiltStlLinker struct {
2596 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002597}
2598
Colin Crossca860ac2016-01-04 14:34:37 -08002599func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002600 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002601 linker := &ndkPrebuiltStlLinker{}
2602 linker.dynamicProperties.BuildShared = true
2603 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002604 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002605 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002606}
2607
Colin Crossca860ac2016-01-04 14:34:37 -08002608func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002609 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002610 linker := &ndkPrebuiltStlLinker{}
2611 linker.dynamicProperties.BuildStatic = true
2612 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002613 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002614 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002615}
2616
Colin Cross635c3b02016-05-18 15:37:25 -07002617func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002618 gccVersion := toolchain.GccVersion()
2619 var libDir string
2620 switch stl {
2621 case "libstlport":
2622 libDir = "cxx-stl/stlport/libs"
2623 case "libc++":
2624 libDir = "cxx-stl/llvm-libc++/libs"
2625 case "libgnustl":
2626 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2627 }
2628
2629 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002630 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002631 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002632 }
2633
2634 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002635 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002636}
2637
Colin Crossca860ac2016-01-04 14:34:37 -08002638func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002639 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002640 // A null build step, but it sets up the output path.
2641 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2642 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2643 }
2644
Colin Cross919281a2016-04-05 16:42:05 -07002645 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002646
2647 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002648 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002649 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002650 libExt = staticLibraryExtension
2651 }
2652
2653 stlName := strings.TrimSuffix(libName, "_shared")
2654 stlName = strings.TrimSuffix(stlName, "_static")
2655 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002656 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002657}
2658
Colin Cross635c3b02016-05-18 15:37:25 -07002659func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002660 if m, ok := mctx.Module().(*Module); ok {
2661 if m.linker != nil {
2662 if linker, ok := m.linker.(baseLinkerInterface); ok {
2663 var modules []blueprint.Module
2664 if linker.buildStatic() && linker.buildShared() {
2665 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002666 static := modules[0].(*Module)
2667 shared := modules[1].(*Module)
2668
2669 static.linker.(baseLinkerInterface).setStatic(true)
2670 shared.linker.(baseLinkerInterface).setStatic(false)
2671
2672 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2673 sharedCompiler := shared.compiler.(*libraryCompiler)
2674 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2675 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2676 // Optimize out compiling common .o files twice for static+shared libraries
2677 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2678 sharedCompiler.baseCompiler.Properties.Srcs = nil
2679 }
2680 }
Colin Crossca860ac2016-01-04 14:34:37 -08002681 } else if linker.buildStatic() {
2682 modules = mctx.CreateLocalVariations("static")
2683 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2684 } else if linker.buildShared() {
2685 modules = mctx.CreateLocalVariations("shared")
2686 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2687 } else {
2688 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2689 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002690 }
2691 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002692 }
2693}
Colin Cross74d1ec02015-04-28 13:30:13 -07002694
2695// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2696// modifies the slice contents in place, and returns a subslice of the original slice
2697func lastUniqueElements(list []string) []string {
2698 totalSkip := 0
2699 for i := len(list) - 1; i >= totalSkip; i-- {
2700 skip := 0
2701 for j := i - 1; j >= totalSkip; j-- {
2702 if list[i] == list[j] {
2703 skip++
2704 } else {
2705 list[j+skip] = list[j]
2706 }
2707 }
2708 totalSkip += skip
2709 }
2710 return list[totalSkip:]
2711}
Colin Cross06a931b2015-10-28 17:23:31 -07002712
2713var Bool = proptools.Bool