blob: f169a385dad5648891c762ea8fe07cee8e569a69 [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
Dan Willemsen34cc69e2015-09-23 15:26:20 -070074 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
Colin Cross3f40fa42015-01-30 17:27:36 -080075)
76
77// Flags used by lots of devices. Putting them in package static variables will save bytes in
78// build.ninja so they aren't repeated for every file
79var (
80 commonGlobalCflags = []string{
81 "-DANDROID",
82 "-fmessage-length=0",
83 "-W",
84 "-Wall",
85 "-Wno-unused",
86 "-Winit-self",
87 "-Wpointer-arith",
88
89 // COMMON_RELEASE_CFLAGS
90 "-DNDEBUG",
91 "-UDEBUG",
92 }
93
94 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080095 "-fdiagnostics-color",
96
Colin Cross3f40fa42015-01-30 17:27:36 -080097 // TARGET_ERROR_FLAGS
98 "-Werror=return-type",
99 "-Werror=non-virtual-dtor",
100 "-Werror=address",
101 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -0800102 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -0800103 }
104
105 hostGlobalCflags = []string{}
106
107 commonGlobalCppflags = []string{
108 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700109 }
110
Dan Willemsenbe03f342016-03-03 17:21:04 -0800111 noOverrideGlobalCflags = []string{
112 "-Werror=int-to-pointer-cast",
113 "-Werror=pointer-to-int-cast",
114 }
115
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700116 illegalFlags = []string{
117 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800118 }
Dan Willemsen97704ed2016-07-07 21:40:39 -0700119
120 ndkPrebuiltSharedLibs = []string{
121 "android",
122 "c",
123 "dl",
124 "EGL",
125 "GLESv1_CM",
126 "GLESv2",
127 "GLESv3",
128 "jnigraphics",
129 "log",
130 "mediandk",
131 "m",
132 "OpenMAXAL",
133 "OpenSLES",
134 "stdc++",
135 "vulkan",
136 "z",
137 }
138 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800139)
140
141func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700142 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700143 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
144 }
145
Colin Cross3f40fa42015-01-30 17:27:36 -0800146 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
147 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
148 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800149 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800150
151 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
152
153 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800154 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800155 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800156 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800157 pctx.StaticVariable("hostClangGlobalCflags",
158 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800159 pctx.StaticVariable("noOverrideClangGlobalCflags",
160 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
161
Tim Kilbournf2948142015-03-11 12:03:03 -0700162 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800163 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800164
165 // Everything in this list is a crime against abstraction and dependency tracking.
166 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800167 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700168 []string{
169 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800170 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700171 "hardware/libhardware/include",
172 "hardware/libhardware_legacy/include",
173 "hardware/ril/include",
174 "libnativehelper/include",
175 "frameworks/native/include",
176 "frameworks/native/opengl/include",
177 "frameworks/av/include",
178 "frameworks/base/include",
179 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800180 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
181 // with this, since there is no associated library.
182 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
183 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800184
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700185 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
186 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700187 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700188 return override, nil
189 }
190 return "${clangDefaultBase}", nil
191 })
192 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700193 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700194 return override, nil
195 }
Stephen Hines369f0132016-04-26 14:34:07 -0700196 return "clang-2812033", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700197 })
Colin Cross16b23492016-01-06 14:41:07 -0800198 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
199 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800200}
201
Colin Crossca860ac2016-01-04 14:34:37 -0800202type Deps struct {
203 SharedLibs, LateSharedLibs []string
204 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700205
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700206 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
207
Colin Cross81413472016-04-11 14:37:39 -0700208 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700209
Dan Willemsenb40aab62016-04-20 14:21:14 -0700210 GeneratedSources []string
211 GeneratedHeaders []string
212
Colin Cross97ba0732015-03-23 17:50:24 -0700213 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700214}
215
Colin Crossca860ac2016-01-04 14:34:37 -0800216type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700217 SharedLibs, LateSharedLibs android.Paths
218 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700219
Colin Cross635c3b02016-05-18 15:37:25 -0700220 ObjFiles android.Paths
221 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700222
Colin Cross635c3b02016-05-18 15:37:25 -0700223 GeneratedSources android.Paths
224 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700225
Dan Willemsen76f08272016-07-09 00:14:08 -0700226 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700227
Colin Cross635c3b02016-05-18 15:37:25 -0700228 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700229}
230
Colin Crossca860ac2016-01-04 14:34:37 -0800231type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700232 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
233 AsFlags []string // Flags that apply to assembly source files
234 CFlags []string // Flags that apply to C and C++ source files
235 ConlyFlags []string // Flags that apply to C source files
236 CppFlags []string // Flags that apply to C++ source files
237 YaccFlags []string // Flags that apply to Yacc source files
238 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800239 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700240
241 Nocrt bool
242 Toolchain Toolchain
243 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800244
245 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800246 DynamicLinker string
247
Colin Cross635c3b02016-05-18 15:37:25 -0700248 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700249}
250
Colin Crossca860ac2016-01-04 14:34:37 -0800251type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700252 // 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 -0700253 Srcs []string `android:"arch_variant"`
254
255 // list of source files that should not be used to build the C/C++ module.
256 // This is most useful in the arch/multilib variants to remove non-common files
257 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700258
259 // list of module-specific flags that will be used for C and C++ compiles.
260 Cflags []string `android:"arch_variant"`
261
262 // list of module-specific flags that will be used for C++ compiles
263 Cppflags []string `android:"arch_variant"`
264
265 // list of module-specific flags that will be used for C compiles
266 Conlyflags []string `android:"arch_variant"`
267
268 // list of module-specific flags that will be used for .S compiles
269 Asflags []string `android:"arch_variant"`
270
Colin Crossca860ac2016-01-04 14:34:37 -0800271 // list of module-specific flags that will be used for C and C++ compiles when
272 // compiling with clang
273 Clang_cflags []string `android:"arch_variant"`
274
275 // list of module-specific flags that will be used for .S compiles when
276 // compiling with clang
277 Clang_asflags []string `android:"arch_variant"`
278
Colin Cross7d5136f2015-05-11 13:39:40 -0700279 // list of module-specific flags that will be used for .y and .yy compiles
280 Yaccflags []string
281
Colin Cross7d5136f2015-05-11 13:39:40 -0700282 // the instruction set architecture to use to compile the C/C++
283 // module.
284 Instruction_set string `android:"arch_variant"`
285
286 // list of directories relative to the root of the source tree that will
287 // be added to the include path using -I.
288 // If possible, don't use this. If adding paths from the current directory use
289 // local_include_dirs, if adding paths from other modules use export_include_dirs in
290 // that module.
291 Include_dirs []string `android:"arch_variant"`
292
293 // list of directories relative to the Blueprints file that will
294 // be added to the include path using -I
295 Local_include_dirs []string `android:"arch_variant"`
296
Dan Willemsenb40aab62016-04-20 14:21:14 -0700297 // list of generated sources to compile. These are the names of gensrcs or
298 // genrule modules.
299 Generated_sources []string `android:"arch_variant"`
300
301 // list of generated headers to add to the include path. These are the names
302 // of genrule modules.
303 Generated_headers []string `android:"arch_variant"`
304
Colin Crossca860ac2016-01-04 14:34:37 -0800305 // pass -frtti instead of -fno-rtti
306 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700307
Colin Crossca860ac2016-01-04 14:34:37 -0800308 Debug, Release struct {
309 // list of module-specific flags that will be used for C and C++ compiles in debug or
310 // release builds
311 Cflags []string `android:"arch_variant"`
312 } `android:"arch_variant"`
313}
Colin Cross7d5136f2015-05-11 13:39:40 -0700314
Colin Crossca860ac2016-01-04 14:34:37 -0800315type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700316 // list of modules whose object files should be linked into this module
317 // in their entirety. For static library modules, all of the .o files from the intermediate
318 // directory of the dependency will be linked into this modules .a file. For a shared library,
319 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700320 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700321
322 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700323 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700324
325 // list of modules that should be dynamically linked into this module.
326 Shared_libs []string `android:"arch_variant"`
327
Colin Crossca860ac2016-01-04 14:34:37 -0800328 // list of module-specific flags that will be used for all link steps
329 Ldflags []string `android:"arch_variant"`
330
331 // don't insert default compiler flags into asflags, cflags,
332 // cppflags, conlyflags, ldflags, or include_dirs
333 No_default_compiler_flags *bool
334
335 // list of system libraries that will be dynamically linked to
336 // shared library and executable modules. If unset, generally defaults to libc
337 // and libm. Set to [] to prevent linking against libc and libm.
338 System_shared_libs []string
339
Colin Cross7d5136f2015-05-11 13:39:40 -0700340 // allow the module to contain undefined symbols. By default,
341 // modules cannot contain undefined symbols that are not satisified by their immediate
342 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
343 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700344 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700345
Dan Willemsend67be222015-09-16 15:19:33 -0700346 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700347 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700348
Colin Cross7d5136f2015-05-11 13:39:40 -0700349 // -l arguments to pass to linker for host-provided shared libraries
350 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700351
352 // list of shared libraries to re-export include directories from. Entries must be
353 // present in shared_libs.
354 Export_shared_lib_headers []string `android:"arch_variant"`
355
356 // list of static libraries to re-export include directories from. Entries must be
357 // present in static_libs.
358 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800359}
Colin Cross7d5136f2015-05-11 13:39:40 -0700360
Colin Crossca860ac2016-01-04 14:34:37 -0800361type LibraryCompilerProperties struct {
362 Static struct {
363 Srcs []string `android:"arch_variant"`
364 Exclude_srcs []string `android:"arch_variant"`
365 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700366 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800367 Shared struct {
368 Srcs []string `android:"arch_variant"`
369 Exclude_srcs []string `android:"arch_variant"`
370 Cflags []string `android:"arch_variant"`
371 } `android:"arch_variant"`
372}
373
Colin Cross919281a2016-04-05 16:42:05 -0700374type FlagExporterProperties struct {
375 // list of directories relative to the Blueprints file that will
376 // be added to the include path using -I for any module that links against this module
377 Export_include_dirs []string `android:"arch_variant"`
378}
379
Colin Crossca860ac2016-01-04 14:34:37 -0800380type LibraryLinkerProperties struct {
381 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700382 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800383 Whole_static_libs []string `android:"arch_variant"`
384 Static_libs []string `android:"arch_variant"`
385 Shared_libs []string `android:"arch_variant"`
386 } `android:"arch_variant"`
387 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700388 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800389 Whole_static_libs []string `android:"arch_variant"`
390 Static_libs []string `android:"arch_variant"`
391 Shared_libs []string `android:"arch_variant"`
392 } `android:"arch_variant"`
393
394 // local file name to pass to the linker as --version_script
395 Version_script *string `android:"arch_variant"`
396 // local file name to pass to the linker as -unexported_symbols_list
397 Unexported_symbols_list *string `android:"arch_variant"`
398 // local file name to pass to the linker as -force_symbols_not_weak_list
399 Force_symbols_not_weak_list *string `android:"arch_variant"`
400 // local file name to pass to the linker as -force_symbols_weak_list
401 Force_symbols_weak_list *string `android:"arch_variant"`
402
Colin Crossca860ac2016-01-04 14:34:37 -0800403 // don't link in crt_begin and crt_end. This flag should only be necessary for
404 // compiling crt or libc.
405 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800406
407 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800408}
409
410type BinaryLinkerProperties struct {
411 // compile executable with -static
Dan Willemsen75ab8082016-07-12 15:36:34 -0700412 Static_executable *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800413
414 // set the name of the output
415 Stem string `android:"arch_variant"`
416
417 // append to the name of the output
418 Suffix string `android:"arch_variant"`
419
420 // if set, add an extra objcopy --prefix-symbols= step
421 Prefix_symbols string
422}
423
424type TestLinkerProperties struct {
425 // if set, build against the gtest library. Defaults to true.
426 Gtest bool
427
428 // Create a separate binary for each source file. Useful when there is
429 // global state that can not be torn down and reset between each test suite.
430 Test_per_src *bool
431}
432
Colin Cross81413472016-04-11 14:37:39 -0700433type ObjectLinkerProperties struct {
434 // names of other cc_object modules to link into this module using partial linking
435 Objs []string `android:"arch_variant"`
436}
437
Colin Crossca860ac2016-01-04 14:34:37 -0800438// Properties used to compile all C or C++ modules
439type BaseProperties struct {
440 // compile module with clang instead of gcc
441 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700442
443 // Minimum sdk version supported when compiling against the ndk
444 Sdk_version string
445
Colin Crossca860ac2016-01-04 14:34:37 -0800446 // don't insert default compiler flags into asflags, cflags,
447 // cppflags, conlyflags, ldflags, or include_dirs
448 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700449
450 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700451 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800452}
453
454type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700455 // install to a subdirectory of the default install path for the module
456 Relative_install_path string
457}
458
Colin Cross665dce92016-04-28 14:50:03 -0700459type StripProperties struct {
460 Strip struct {
461 None bool
462 Keep_symbols bool
463 }
464}
465
Colin Crossca860ac2016-01-04 14:34:37 -0800466type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700467 Native_coverage *bool
468 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700469 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800470}
471
Colin Crossca860ac2016-01-04 14:34:37 -0800472type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800473 static() bool
474 staticBinary() bool
475 clang() bool
476 toolchain() Toolchain
477 noDefaultCompilerFlags() bool
478 sdk() bool
479 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700480 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800481}
482
483type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700484 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800485 ModuleContextIntf
486}
487
488type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700489 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800490 ModuleContextIntf
491}
492
493type Customizer interface {
494 CustomizeProperties(BaseModuleContext)
495 Properties() []interface{}
496}
497
498type feature interface {
499 begin(ctx BaseModuleContext)
500 deps(ctx BaseModuleContext, deps Deps) Deps
501 flags(ctx ModuleContext, flags Flags) Flags
502 props() []interface{}
503}
504
505type compiler interface {
506 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700507 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800508}
509
510type linker interface {
511 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700512 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700513 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800514}
515
516type installer interface {
517 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700518 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800519 inData() bool
520}
521
Colin Crossc99deeb2016-04-11 15:06:20 -0700522type dependencyTag struct {
523 blueprint.BaseDependencyTag
524 name string
525 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700526
527 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700528}
529
530var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700531 sharedDepTag = dependencyTag{name: "shared", library: true}
532 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
533 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
534 staticDepTag = dependencyTag{name: "static", library: true}
535 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
536 lateStaticDepTag = dependencyTag{name: "late static", library: true}
537 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
538 genSourceDepTag = dependencyTag{name: "gen source"}
539 genHeaderDepTag = dependencyTag{name: "gen header"}
540 objDepTag = dependencyTag{name: "obj"}
541 crtBeginDepTag = dependencyTag{name: "crtbegin"}
542 crtEndDepTag = dependencyTag{name: "crtend"}
543 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700544)
545
Colin Crossca860ac2016-01-04 14:34:37 -0800546// Module contains the properties and members used by all C/C++ module types, and implements
547// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
548// to construct the output file. Behavior can be customized with a Customizer interface
549type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700550 android.ModuleBase
551 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700552
Colin Crossca860ac2016-01-04 14:34:37 -0800553 Properties BaseProperties
554 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700555
Colin Crossca860ac2016-01-04 14:34:37 -0800556 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700557 hod android.HostOrDeviceSupported
558 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700559
Colin Crossca860ac2016-01-04 14:34:37 -0800560 // delegates, initialize before calling Init
561 customizer Customizer
562 features []feature
563 compiler compiler
564 linker linker
565 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700566 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800567 sanitize *sanitize
568
569 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700570
Colin Cross635c3b02016-05-18 15:37:25 -0700571 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800572
573 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700574}
575
Colin Crossca860ac2016-01-04 14:34:37 -0800576func (c *Module) Init() (blueprint.Module, []interface{}) {
577 props := []interface{}{&c.Properties, &c.unused}
578 if c.customizer != nil {
579 props = append(props, c.customizer.Properties()...)
580 }
581 if c.compiler != nil {
582 props = append(props, c.compiler.props()...)
583 }
584 if c.linker != nil {
585 props = append(props, c.linker.props()...)
586 }
587 if c.installer != nil {
588 props = append(props, c.installer.props()...)
589 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700590 if c.stl != nil {
591 props = append(props, c.stl.props()...)
592 }
Colin Cross16b23492016-01-06 14:41:07 -0800593 if c.sanitize != nil {
594 props = append(props, c.sanitize.props()...)
595 }
Colin Crossca860ac2016-01-04 14:34:37 -0800596 for _, feature := range c.features {
597 props = append(props, feature.props()...)
598 }
Colin Crossc472d572015-03-17 15:06:21 -0700599
Colin Cross635c3b02016-05-18 15:37:25 -0700600 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700601
Colin Cross635c3b02016-05-18 15:37:25 -0700602 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700603}
604
Colin Crossca860ac2016-01-04 14:34:37 -0800605type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700606 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800607 moduleContextImpl
608}
609
610type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700611 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800612 moduleContextImpl
613}
614
615type moduleContextImpl struct {
616 mod *Module
617 ctx BaseModuleContext
618}
619
Colin Crossca860ac2016-01-04 14:34:37 -0800620func (ctx *moduleContextImpl) clang() bool {
621 return ctx.mod.clang(ctx.ctx)
622}
623
624func (ctx *moduleContextImpl) toolchain() Toolchain {
625 return ctx.mod.toolchain(ctx.ctx)
626}
627
628func (ctx *moduleContextImpl) static() bool {
629 if ctx.mod.linker == nil {
630 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
631 }
632 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
633 return linker.static()
634 } else {
635 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
636 }
637}
638
639func (ctx *moduleContextImpl) staticBinary() bool {
640 if ctx.mod.linker == nil {
641 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
642 }
643 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
644 return linker.staticBinary()
645 } else {
646 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
647 }
648}
649
650func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
651 return Bool(ctx.mod.Properties.No_default_compiler_flags)
652}
653
654func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700655 if ctx.ctx.Device() {
656 return ctx.mod.Properties.Sdk_version != ""
657 }
658 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800659}
660
661func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700662 if ctx.ctx.Device() {
663 return ctx.mod.Properties.Sdk_version
664 }
665 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800666}
667
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700668func (ctx *moduleContextImpl) selectedStl() string {
669 if stl := ctx.mod.stl; stl != nil {
670 return stl.Properties.SelectedStl
671 }
672 return ""
673}
674
Colin Cross635c3b02016-05-18 15:37:25 -0700675func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800676 return &Module{
677 hod: hod,
678 multilib: multilib,
679 }
680}
681
Colin Cross635c3b02016-05-18 15:37:25 -0700682func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800683 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700684 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800685 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800686 return module
687}
688
Colin Cross635c3b02016-05-18 15:37:25 -0700689func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800690 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700691 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800692 moduleContextImpl: moduleContextImpl{
693 mod: c,
694 },
695 }
696 ctx.ctx = ctx
697
698 flags := Flags{
699 Toolchain: c.toolchain(ctx),
700 Clang: c.clang(ctx),
701 }
Colin Crossca860ac2016-01-04 14:34:37 -0800702 if c.compiler != nil {
703 flags = c.compiler.flags(ctx, flags)
704 }
705 if c.linker != nil {
706 flags = c.linker.flags(ctx, flags)
707 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700708 if c.stl != nil {
709 flags = c.stl.flags(ctx, flags)
710 }
Colin Cross16b23492016-01-06 14:41:07 -0800711 if c.sanitize != nil {
712 flags = c.sanitize.flags(ctx, flags)
713 }
Colin Crossca860ac2016-01-04 14:34:37 -0800714 for _, feature := range c.features {
715 flags = feature.flags(ctx, flags)
716 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800717 if ctx.Failed() {
718 return
719 }
720
Colin Crossca860ac2016-01-04 14:34:37 -0800721 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
722 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
723 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800724
Colin Crossca860ac2016-01-04 14:34:37 -0800725 // Optimization to reduce size of build.ninja
726 // Replace the long list of flags for each file with a module-local variable
727 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
728 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
729 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
730 flags.CFlags = []string{"$cflags"}
731 flags.CppFlags = []string{"$cppflags"}
732 flags.AsFlags = []string{"$asflags"}
733
Colin Crossc99deeb2016-04-11 15:06:20 -0700734 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800735 if ctx.Failed() {
736 return
737 }
738
Dan Willemsen76f08272016-07-09 00:14:08 -0700739 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700740
Colin Cross635c3b02016-05-18 15:37:25 -0700741 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800742 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700743 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800744 if ctx.Failed() {
745 return
746 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800747 }
748
Colin Crossca860ac2016-01-04 14:34:37 -0800749 if c.linker != nil {
750 outputFile := c.linker.link(ctx, flags, deps, objFiles)
751 if ctx.Failed() {
752 return
753 }
Colin Cross635c3b02016-05-18 15:37:25 -0700754 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700755
Colin Crossc99deeb2016-04-11 15:06:20 -0700756 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800757 c.installer.install(ctx, outputFile)
758 if ctx.Failed() {
759 return
760 }
761 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700762 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800763}
764
Colin Crossca860ac2016-01-04 14:34:37 -0800765func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
766 if c.cachedToolchain == nil {
767 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700768 os := ctx.Os()
769 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800770 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700771 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800772 return nil
773 }
774 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800775 }
Colin Crossca860ac2016-01-04 14:34:37 -0800776 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800777}
778
Colin Crossca860ac2016-01-04 14:34:37 -0800779func (c *Module) begin(ctx BaseModuleContext) {
780 if c.compiler != nil {
781 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700782 }
Colin Crossca860ac2016-01-04 14:34:37 -0800783 if c.linker != nil {
784 c.linker.begin(ctx)
785 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700786 if c.stl != nil {
787 c.stl.begin(ctx)
788 }
Colin Cross16b23492016-01-06 14:41:07 -0800789 if c.sanitize != nil {
790 c.sanitize.begin(ctx)
791 }
Colin Crossca860ac2016-01-04 14:34:37 -0800792 for _, feature := range c.features {
793 feature.begin(ctx)
794 }
795}
796
Colin Crossc99deeb2016-04-11 15:06:20 -0700797func (c *Module) deps(ctx BaseModuleContext) Deps {
798 deps := Deps{}
799
800 if c.compiler != nil {
801 deps = c.compiler.deps(ctx, deps)
802 }
803 if c.linker != nil {
804 deps = c.linker.deps(ctx, deps)
805 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700806 if c.stl != nil {
807 deps = c.stl.deps(ctx, deps)
808 }
Colin Cross16b23492016-01-06 14:41:07 -0800809 if c.sanitize != nil {
810 deps = c.sanitize.deps(ctx, deps)
811 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700812 for _, feature := range c.features {
813 deps = feature.deps(ctx, deps)
814 }
815
816 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
817 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
818 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
819 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
820 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
821
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700822 for _, lib := range deps.ReexportSharedLibHeaders {
823 if !inList(lib, deps.SharedLibs) {
824 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
825 }
826 }
827
828 for _, lib := range deps.ReexportStaticLibHeaders {
829 if !inList(lib, deps.StaticLibs) {
830 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
831 }
832 }
833
Colin Crossc99deeb2016-04-11 15:06:20 -0700834 return deps
835}
836
Colin Cross635c3b02016-05-18 15:37:25 -0700837func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800838 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700839 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800840 moduleContextImpl: moduleContextImpl{
841 mod: c,
842 },
843 }
844 ctx.ctx = ctx
845
846 if c.customizer != nil {
847 c.customizer.CustomizeProperties(ctx)
848 }
849
850 c.begin(ctx)
851
Colin Crossc99deeb2016-04-11 15:06:20 -0700852 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800853
Colin Crossb5bc4b42016-07-11 16:11:59 -0700854 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
855 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700856
857 if ctx.sdk() {
858 version := "." + ctx.sdkVersion()
859
860 rewriteNdkLibs := func(list []string) []string {
861 for i, entry := range list {
862 if inList(entry, ndkPrebuiltSharedLibraries) {
863 list[i] = "ndk_" + entry + version
864 }
865 }
866 return list
867 }
868
869 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
870 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
871 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700872
873 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
874 deps.WholeStaticLibs...)
875
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700876 for _, lib := range deps.StaticLibs {
877 depTag := staticDepTag
878 if inList(lib, deps.ReexportStaticLibHeaders) {
879 depTag = staticExportDepTag
880 }
881 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
882 deps.StaticLibs...)
883 }
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 }
893 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
894 deps.SharedLibs...)
895 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700896
897 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
898 deps.LateSharedLibs...)
899
Colin Cross68861832016-07-08 10:41:41 -0700900 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
901 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700902
Colin Cross68861832016-07-08 10:41:41 -0700903 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700904
905 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700906 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800907 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700909 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700910 }
Colin Cross6362e272015-10-29 15:25:03 -0700911}
Colin Cross21b9a242015-03-24 14:15:58 -0700912
Colin Cross635c3b02016-05-18 15:37:25 -0700913func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700914 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700915 c.depsMutator(ctx)
916 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800917}
918
Colin Crossca860ac2016-01-04 14:34:37 -0800919func (c *Module) clang(ctx BaseModuleContext) bool {
920 clang := Bool(c.Properties.Clang)
921
922 if c.Properties.Clang == nil {
923 if ctx.Host() {
924 clang = true
925 }
926
927 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
928 clang = true
929 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800930 }
Colin Cross28344522015-04-22 13:07:53 -0700931
Colin Crossca860ac2016-01-04 14:34:37 -0800932 if !c.toolchain(ctx).ClangSupported() {
933 clang = false
934 }
935
936 return clang
937}
938
Colin Crossc99deeb2016-04-11 15:06:20 -0700939// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700940func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800941 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800942
Dan Willemsena96ff642016-06-07 12:34:45 -0700943 // Whether a module can link to another module, taking into
944 // account NDK linking.
945 linkTypeOk := func(from, to *Module) bool {
946 if from.Target().Os != android.Android {
947 // Host code is not restricted
948 return true
949 }
950 if from.Properties.Sdk_version == "" {
951 // Platform code can link to anything
952 return true
953 }
954 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
955 // These are always allowed
956 return true
957 }
958 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
959 // These are allowed, but don't set sdk_version
960 return true
961 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700962 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
963 // These are allowed, but don't set sdk_version
964 return true
965 }
966 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700967 }
968
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 ctx.VisitDirectDeps(func(m blueprint.Module) {
970 name := ctx.OtherModuleName(m)
971 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800972
Colin Cross635c3b02016-05-18 15:37:25 -0700973 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700974 if a == nil {
975 ctx.ModuleErrorf("module %q not an android module", name)
976 return
Colin Crossca860ac2016-01-04 14:34:37 -0800977 }
Colin Crossca860ac2016-01-04 14:34:37 -0800978
Dan Willemsena96ff642016-06-07 12:34:45 -0700979 cc, _ := m.(*Module)
980 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700981 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700982 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700983 case genSourceDepTag:
984 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
985 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
986 genRule.GeneratedSourceFiles()...)
987 } else {
988 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
989 }
990 case genHeaderDepTag:
991 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
992 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
993 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700994 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700995 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700996 } else {
997 ctx.ModuleErrorf("module %q is not a genrule", name)
998 }
999 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001000 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001001 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001002 return
1003 }
1004
1005 if !a.Enabled() {
1006 ctx.ModuleErrorf("depends on disabled module %q", name)
1007 return
1008 }
1009
Colin Crossa1ad8d12016-06-01 17:09:44 -07001010 if a.Target().Os != ctx.Os() {
1011 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1012 return
1013 }
1014
1015 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1016 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001017 return
1018 }
1019
Dan Willemsena96ff642016-06-07 12:34:45 -07001020 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001021 ctx.ModuleErrorf("module %q missing output file", name)
1022 return
1023 }
1024
1025 if tag == reuseObjTag {
1026 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001027 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001028 return
1029 }
1030
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001031 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001032 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001033 flags := i.exportedFlags()
1034 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001035
1036 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001037 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001038 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001039 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001040
1041 if !linkTypeOk(c, cc) {
1042 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1043 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001044 }
1045
Colin Cross635c3b02016-05-18 15:37:25 -07001046 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001047
1048 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001049 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001050 depPtr = &depPaths.SharedLibs
1051 case lateSharedDepTag:
1052 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001053 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001054 depPtr = &depPaths.StaticLibs
1055 case lateStaticDepTag:
1056 depPtr = &depPaths.LateStaticLibs
1057 case wholeStaticDepTag:
1058 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -07001059 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -07001060 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001061 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001062 return
1063 }
1064
1065 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1066 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1067 for i := range missingDeps {
1068 missingDeps[i] += postfix
1069 }
1070 ctx.AddMissingDependencies(missingDeps)
1071 }
1072 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -07001073 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001074 case objDepTag:
1075 depPtr = &depPaths.ObjFiles
1076 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001077 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001078 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001079 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001080 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001081 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001082 }
1083
1084 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001085 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001086 }
1087 })
1088
1089 return depPaths
1090}
1091
1092func (c *Module) InstallInData() bool {
1093 if c.installer == nil {
1094 return false
1095 }
1096 return c.installer.inData()
1097}
1098
1099// Compiler
1100
1101type baseCompiler struct {
1102 Properties BaseCompilerProperties
1103}
1104
1105var _ compiler = (*baseCompiler)(nil)
1106
1107func (compiler *baseCompiler) props() []interface{} {
1108 return []interface{}{&compiler.Properties}
1109}
1110
Dan Willemsenb40aab62016-04-20 14:21:14 -07001111func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1112
1113func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1114 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1115 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1116
1117 return deps
1118}
Colin Crossca860ac2016-01-04 14:34:37 -08001119
1120// Create a Flags struct that collects the compile flags from global values,
1121// per-target values, module type values, and per-module Blueprints properties
1122func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1123 toolchain := ctx.toolchain()
1124
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001125 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1126 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1127 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1128 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1129
Colin Crossca860ac2016-01-04 14:34:37 -08001130 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1131 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1132 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1133 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1134 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1135
Colin Cross28344522015-04-22 13:07:53 -07001136 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001137 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1138 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001139 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001140 includeDirsToFlags(localIncludeDirs),
1141 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001142
Colin Crossca860ac2016-01-04 14:34:37 -08001143 if !ctx.noDefaultCompilerFlags() {
1144 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001145 flags.GlobalFlags = append(flags.GlobalFlags,
1146 "${commonGlobalIncludes}",
1147 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001148 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001149 }
1150
1151 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001152 "-I" + android.PathForModuleSrc(ctx).String(),
1153 "-I" + android.PathForModuleOut(ctx).String(),
1154 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001155 }...)
1156 }
1157
Colin Crossca860ac2016-01-04 14:34:37 -08001158 instructionSet := compiler.Properties.Instruction_set
1159 if flags.RequiredInstructionSet != "" {
1160 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001161 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001162 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1163 if flags.Clang {
1164 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1165 }
1166 if err != nil {
1167 ctx.ModuleErrorf("%s", err)
1168 }
1169
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001170 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1171
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001172 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001173 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001174
Colin Cross97ba0732015-03-23 17:50:24 -07001175 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001176 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1177 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1178
Colin Cross97ba0732015-03-23 17:50:24 -07001179 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001180 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1181 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001182 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1183 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1184 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001185
1186 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001187 var gccPrefix string
1188 if !ctx.Darwin() {
1189 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1190 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001191
Colin Cross97ba0732015-03-23 17:50:24 -07001192 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1193 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1194 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001195 }
1196
Colin Crossa1ad8d12016-06-01 17:09:44 -07001197 hod := "host"
1198 if ctx.Os().Class == android.Device {
1199 hod = "device"
1200 }
1201
Colin Crossca860ac2016-01-04 14:34:37 -08001202 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001203 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1204
Colin Cross97ba0732015-03-23 17:50:24 -07001205 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001206 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001207 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001208 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001209 toolchain.ClangCflags(),
1210 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001211 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001212
1213 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001214 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001215 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001216 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001217 toolchain.Cflags(),
1218 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001219 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 }
1221
Colin Cross7b66f152015-12-15 16:07:43 -08001222 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1223 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1224 }
1225
Colin Crossf6566ed2015-03-24 11:13:38 -07001226 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001227 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001228 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001229 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001230 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001231 }
1232 }
1233
Colin Cross97ba0732015-03-23 17:50:24 -07001234 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001235
Colin Cross97ba0732015-03-23 17:50:24 -07001236 if flags.Clang {
1237 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001238 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001239 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001240 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001241 }
1242
Colin Crossc4bde762015-11-23 16:11:30 -08001243 if flags.Clang {
1244 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1245 } else {
1246 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001247 }
1248
Colin Crossca860ac2016-01-04 14:34:37 -08001249 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001250 if ctx.Host() && !flags.Clang {
1251 // The host GCC doesn't support C++14 (and is deprecated, so likely
1252 // never will). Build these modules with C++11.
1253 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1254 } else {
1255 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1256 }
1257 }
1258
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001259 // We can enforce some rules more strictly in the code we own. strict
1260 // indicates if this is code that we can be stricter with. If we have
1261 // rules that we want to apply to *our* code (but maybe can't for
1262 // vendor/device specific things), we could extend this to be a ternary
1263 // value.
1264 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001265 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001266 strict = false
1267 }
1268
1269 // Can be used to make some annotations stricter for code we can fix
1270 // (such as when we mark functions as deprecated).
1271 if strict {
1272 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1273 }
1274
Colin Cross3f40fa42015-01-30 17:27:36 -08001275 return flags
1276}
1277
Colin Cross635c3b02016-05-18 15:37:25 -07001278func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001279 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001280 objFiles := compiler.compileObjs(ctx, flags, "",
1281 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1282 deps.GeneratedSources, deps.GeneratedHeaders)
1283
Colin Crossca860ac2016-01-04 14:34:37 -08001284 if ctx.Failed() {
1285 return nil
1286 }
1287
Colin Crossca860ac2016-01-04 14:34:37 -08001288 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001289}
1290
1291// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001292func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1293 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001294
Colin Crossca860ac2016-01-04 14:34:37 -08001295 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001296
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001297 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001298 inputFiles = append(inputFiles, extraSrcs...)
1299 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1300
1301 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001302 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001303
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001304 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001305}
1306
Colin Crossca860ac2016-01-04 14:34:37 -08001307// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1308type baseLinker struct {
1309 Properties BaseLinkerProperties
1310 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001311 VariantIsShared bool `blueprint:"mutated"`
1312 VariantIsStatic bool `blueprint:"mutated"`
1313 VariantIsStaticBinary bool `blueprint:"mutated"`
1314 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001315 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001316}
1317
Dan Willemsend30e6102016-03-30 17:35:50 -07001318func (linker *baseLinker) begin(ctx BaseModuleContext) {
1319 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001320 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001321 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001322 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001323 }
1324}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001325
Colin Crossca860ac2016-01-04 14:34:37 -08001326func (linker *baseLinker) props() []interface{} {
1327 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001328}
1329
Colin Crossca860ac2016-01-04 14:34:37 -08001330func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1331 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1332 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1333 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001334
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001335 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1336 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1337
Dan Willemsena96ff642016-06-07 12:34:45 -07001338 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001339 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001340 }
1341
Colin Crossf6566ed2015-03-24 11:13:38 -07001342 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001343 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001344 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1345 if !Bool(linker.Properties.No_libgcc) {
1346 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001347 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001348
Colin Crossca860ac2016-01-04 14:34:37 -08001349 if !linker.static() {
1350 if linker.Properties.System_shared_libs != nil {
1351 deps.LateSharedLibs = append(deps.LateSharedLibs,
1352 linker.Properties.System_shared_libs...)
1353 } else if !ctx.sdk() {
1354 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1355 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001356 }
Colin Cross577f6e42015-03-27 18:23:34 -07001357
Colin Crossca860ac2016-01-04 14:34:37 -08001358 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001359 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001360 "libc",
1361 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001362 )
1363 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001364 }
1365
Colin Crossca860ac2016-01-04 14:34:37 -08001366 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001367}
1368
Colin Crossca860ac2016-01-04 14:34:37 -08001369func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1370 toolchain := ctx.toolchain()
1371
Colin Crossca860ac2016-01-04 14:34:37 -08001372 if !ctx.noDefaultCompilerFlags() {
1373 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1374 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1375 }
1376
1377 if flags.Clang {
1378 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1379 } else {
1380 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1381 }
1382
1383 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001384 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1385
Colin Crossca860ac2016-01-04 14:34:37 -08001386 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1387 }
1388 }
1389
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001390 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1391
Dan Willemsen00ced762016-05-10 17:31:21 -07001392 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1393
Dan Willemsend30e6102016-03-30 17:35:50 -07001394 if ctx.Host() && !linker.static() {
1395 rpath_prefix := `\$$ORIGIN/`
1396 if ctx.Darwin() {
1397 rpath_prefix = "@loader_path/"
1398 }
1399
Colin Crossc99deeb2016-04-11 15:06:20 -07001400 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001401 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1402 }
1403 }
1404
Dan Willemsene7174922016-03-30 17:33:52 -07001405 if flags.Clang {
1406 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1407 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001408 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1409 }
1410
1411 return flags
1412}
1413
1414func (linker *baseLinker) static() bool {
1415 return linker.dynamicProperties.VariantIsStatic
1416}
1417
1418func (linker *baseLinker) staticBinary() bool {
1419 return linker.dynamicProperties.VariantIsStaticBinary
1420}
1421
1422func (linker *baseLinker) setStatic(static bool) {
1423 linker.dynamicProperties.VariantIsStatic = static
1424}
1425
Colin Cross16b23492016-01-06 14:41:07 -08001426func (linker *baseLinker) isDependencyRoot() bool {
1427 return false
1428}
1429
Colin Crossca860ac2016-01-04 14:34:37 -08001430type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001431 // Returns true if the build options for the module have selected a static or shared build
1432 buildStatic() bool
1433 buildShared() bool
1434
1435 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001436 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001437
Colin Cross18b6dc52015-04-28 13:20:37 -07001438 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001439 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001440
1441 // Returns whether a module is a static binary
1442 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001443
1444 // Returns true for dependency roots (binaries)
1445 // TODO(ccross): also handle dlopenable libraries
1446 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001447}
1448
Colin Crossca860ac2016-01-04 14:34:37 -08001449type baseInstaller struct {
1450 Properties InstallerProperties
1451
1452 dir string
1453 dir64 string
1454 data bool
1455
Colin Cross635c3b02016-05-18 15:37:25 -07001456 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001457}
1458
1459var _ installer = (*baseInstaller)(nil)
1460
1461func (installer *baseInstaller) props() []interface{} {
1462 return []interface{}{&installer.Properties}
1463}
1464
Colin Cross635c3b02016-05-18 15:37:25 -07001465func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001466 subDir := installer.dir
1467 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1468 subDir = installer.dir64
1469 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001470 if !ctx.Host() && !ctx.Arch().Native {
1471 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1472 }
Colin Cross635c3b02016-05-18 15:37:25 -07001473 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001474 installer.path = ctx.InstallFile(dir, file)
1475}
1476
1477func (installer *baseInstaller) inData() bool {
1478 return installer.data
1479}
1480
Colin Cross3f40fa42015-01-30 17:27:36 -08001481//
1482// Combined static+shared libraries
1483//
1484
Colin Cross919281a2016-04-05 16:42:05 -07001485type flagExporter struct {
1486 Properties FlagExporterProperties
1487
1488 flags []string
1489}
1490
1491func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001492 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001493 for _, dir := range includeDirs.Strings() {
Colin Crossf87b2612016-07-13 18:55:43 -07001494 f.flags = append(f.flags, inc+dir)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001495 }
Colin Cross919281a2016-04-05 16:42:05 -07001496}
1497
1498func (f *flagExporter) reexportFlags(flags []string) {
1499 f.flags = append(f.flags, flags...)
1500}
1501
1502func (f *flagExporter) exportedFlags() []string {
1503 return f.flags
1504}
1505
1506type exportedFlagsProducer interface {
1507 exportedFlags() []string
1508}
1509
1510var _ exportedFlagsProducer = (*flagExporter)(nil)
1511
Colin Crossca860ac2016-01-04 14:34:37 -08001512type libraryCompiler struct {
1513 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001514
Colin Crossca860ac2016-01-04 14:34:37 -08001515 linker *libraryLinker
1516 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001517
Colin Crossca860ac2016-01-04 14:34:37 -08001518 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001519 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001520}
1521
Colin Crossca860ac2016-01-04 14:34:37 -08001522var _ compiler = (*libraryCompiler)(nil)
1523
1524func (library *libraryCompiler) props() []interface{} {
1525 props := library.baseCompiler.props()
1526 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001527}
1528
Colin Crossca860ac2016-01-04 14:34:37 -08001529func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1530 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001531
Dan Willemsen490fd492015-11-24 17:53:15 -08001532 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1533 // all code is position independent, and then those warnings get promoted to
1534 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001535 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001536 flags.CFlags = append(flags.CFlags, "-fPIC")
1537 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001538
Colin Crossca860ac2016-01-04 14:34:37 -08001539 if library.linker.static() {
1540 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001541 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001542 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001543 }
1544
Colin Crossca860ac2016-01-04 14:34:37 -08001545 return flags
1546}
1547
Colin Cross635c3b02016-05-18 15:37:25 -07001548func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1549 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001550
Dan Willemsenb40aab62016-04-20 14:21:14 -07001551 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001552 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001553
1554 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001555 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001556 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1557 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001558 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001559 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001560 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1561 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001562 }
1563
1564 return objFiles
1565}
1566
1567type libraryLinker struct {
1568 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001569 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001570 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001571
1572 Properties LibraryLinkerProperties
1573
1574 dynamicProperties struct {
1575 BuildStatic bool `blueprint:"mutated"`
1576 BuildShared bool `blueprint:"mutated"`
1577 }
1578
Colin Crossca860ac2016-01-04 14:34:37 -08001579 // If we're used as a whole_static_lib, our missing dependencies need
1580 // to be given
1581 wholeStaticMissingDeps []string
1582
1583 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001584 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001585}
1586
1587var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001588
Colin Crossc7a38dc2016-07-12 13:13:09 -07001589type libraryInterface interface {
1590 getWholeStaticMissingDeps() []string
1591 static() bool
1592 objs() android.Paths
1593}
1594
Colin Crossca860ac2016-01-04 14:34:37 -08001595func (library *libraryLinker) props() []interface{} {
1596 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001597 return append(props,
1598 &library.Properties,
1599 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001600 &library.flagExporter.Properties,
1601 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001602}
1603
1604func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1605 flags = library.baseLinker.flags(ctx, flags)
1606
1607 flags.Nocrt = Bool(library.Properties.Nocrt)
1608
1609 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001610 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001611 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1612 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001613 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001614 sharedFlag = "-shared"
1615 }
Colin Crossf87b2612016-07-13 18:55:43 -07001616 var f []string
Colin Crossf6566ed2015-03-24 11:13:38 -07001617 if ctx.Device() {
Colin Crossf87b2612016-07-13 18:55:43 -07001618 f = append(f,
Dan Willemsen99db8c32016-03-03 18:05:38 -08001619 "-nostdlib",
1620 "-Wl,--gc-sections",
1621 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001622 }
Colin Cross97ba0732015-03-23 17:50:24 -07001623
Colin Cross0af4b842015-04-30 16:36:18 -07001624 if ctx.Darwin() {
Colin Crossf87b2612016-07-13 18:55:43 -07001625 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001626 "-dynamiclib",
1627 "-single_module",
1628 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001629 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001630 )
1631 } else {
Colin Crossf87b2612016-07-13 18:55:43 -07001632 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001633 sharedFlag,
Colin Crossf87b2612016-07-13 18:55:43 -07001634 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
Colin Cross0af4b842015-04-30 16:36:18 -07001635 }
Colin Crossf87b2612016-07-13 18:55:43 -07001636
1637 flags.LdFlags = append(f, flags.LdFlags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001638 }
Colin Cross97ba0732015-03-23 17:50:24 -07001639
1640 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001641}
1642
Colin Crossca860ac2016-01-04 14:34:37 -08001643func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1644 deps = library.baseLinker.deps(ctx, deps)
1645 if library.static() {
1646 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1647 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1648 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1649 } else {
1650 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1651 if !ctx.sdk() {
1652 deps.CrtBegin = "crtbegin_so"
1653 deps.CrtEnd = "crtend_so"
1654 } else {
1655 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1656 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1657 }
1658 }
1659 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1660 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1661 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1662 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001663
Colin Crossca860ac2016-01-04 14:34:37 -08001664 return deps
1665}
Colin Cross3f40fa42015-01-30 17:27:36 -08001666
Colin Crossca860ac2016-01-04 14:34:37 -08001667func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001668 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001669
Colin Cross635c3b02016-05-18 15:37:25 -07001670 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001671 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001672
Colin Cross635c3b02016-05-18 15:37:25 -07001673 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001674 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001675
Colin Cross0af4b842015-04-30 16:36:18 -07001676 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001677 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001678 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001679 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001680 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001681
Colin Crossca860ac2016-01-04 14:34:37 -08001682 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001683
1684 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001685
1686 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001687}
1688
Colin Crossca860ac2016-01-04 14:34:37 -08001689func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001690 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001691
Colin Cross635c3b02016-05-18 15:37:25 -07001692 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001693
Colin Cross635c3b02016-05-18 15:37:25 -07001694 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1695 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1696 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1697 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001698 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001699 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001700 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001701 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001702 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001703 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001704 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1705 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001706 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001707 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1708 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001709 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001710 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1711 }
1712 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001714 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1715 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001716 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001717 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001718 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001719 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001720 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001721 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001722 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001723 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001724 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001725 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001726 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001727 }
Colin Crossaee540a2015-07-06 17:48:31 -07001728 }
1729
Colin Cross665dce92016-04-28 14:50:03 -07001730 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001731 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001732 ret := outputFile
1733
1734 builderFlags := flagsToBuilderFlags(flags)
1735
1736 if library.stripper.needsStrip(ctx) {
1737 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001738 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001739 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1740 }
1741
Colin Crossca860ac2016-01-04 14:34:37 -08001742 sharedLibs := deps.SharedLibs
1743 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001744
Colin Crossca860ac2016-01-04 14:34:37 -08001745 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1746 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001747 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001748
Colin Cross665dce92016-04-28 14:50:03 -07001749 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001750}
1751
Colin Crossca860ac2016-01-04 14:34:37 -08001752func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001753 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001754
Colin Crossc99deeb2016-04-11 15:06:20 -07001755 objFiles = append(objFiles, deps.ObjFiles...)
1756
Colin Cross635c3b02016-05-18 15:37:25 -07001757 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001758 if library.static() {
1759 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001760 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001761 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001762 }
1763
Colin Cross919281a2016-04-05 16:42:05 -07001764 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001765 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001766
1767 return out
1768}
1769
1770func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001771 return library.dynamicProperties.BuildStatic &&
1772 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001773}
1774
1775func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001776 return library.dynamicProperties.BuildShared &&
1777 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001778}
1779
1780func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1781 return library.wholeStaticMissingDeps
1782}
1783
Colin Crossc99deeb2016-04-11 15:06:20 -07001784func (library *libraryLinker) installable() bool {
1785 return !library.static()
1786}
1787
Colin Crossc7a38dc2016-07-12 13:13:09 -07001788func (library *libraryLinker) objs() android.Paths {
1789 return library.objFiles
1790}
1791
Colin Crossca860ac2016-01-04 14:34:37 -08001792type libraryInstaller struct {
1793 baseInstaller
1794
Colin Cross30d5f512016-05-03 18:02:42 -07001795 linker *libraryLinker
1796 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001797}
1798
Colin Cross635c3b02016-05-18 15:37:25 -07001799func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001800 if !library.linker.static() {
1801 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001802 }
1803}
1804
Colin Cross30d5f512016-05-03 18:02:42 -07001805func (library *libraryInstaller) inData() bool {
1806 return library.baseInstaller.inData() || library.sanitize.inData()
1807}
1808
Colin Cross635c3b02016-05-18 15:37:25 -07001809func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1810 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001811
Colin Crossca860ac2016-01-04 14:34:37 -08001812 linker := &libraryLinker{}
1813 linker.dynamicProperties.BuildShared = shared
1814 linker.dynamicProperties.BuildStatic = static
1815 module.linker = linker
1816
1817 module.compiler = &libraryCompiler{
1818 linker: linker,
1819 }
1820 module.installer = &libraryInstaller{
1821 baseInstaller: baseInstaller{
1822 dir: "lib",
1823 dir64: "lib64",
1824 },
Colin Cross30d5f512016-05-03 18:02:42 -07001825 linker: linker,
1826 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001827 }
1828
Colin Crossca860ac2016-01-04 14:34:37 -08001829 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001830}
1831
Colin Crossca860ac2016-01-04 14:34:37 -08001832func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001833 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001834 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001835}
1836
Colin Cross3f40fa42015-01-30 17:27:36 -08001837//
1838// Objects (for crt*.o)
1839//
1840
Colin Crossca860ac2016-01-04 14:34:37 -08001841type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001842 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001843}
1844
Colin Crossca860ac2016-01-04 14:34:37 -08001845func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001846 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001847 module.compiler = &baseCompiler{}
1848 module.linker = &objectLinker{}
1849 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001850}
1851
Colin Cross81413472016-04-11 14:37:39 -07001852func (object *objectLinker) props() []interface{} {
1853 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001854}
1855
Colin Crossca860ac2016-01-04 14:34:37 -08001856func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001857
Colin Cross81413472016-04-11 14:37:39 -07001858func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1859 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001860 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001861}
1862
Colin Crossca860ac2016-01-04 14:34:37 -08001863func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001864 if flags.Clang {
1865 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1866 } else {
1867 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1868 }
1869
Colin Crossca860ac2016-01-04 14:34:37 -08001870 return flags
1871}
1872
1873func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001874 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001875
Colin Cross97ba0732015-03-23 17:50:24 -07001876 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001877
Colin Cross635c3b02016-05-18 15:37:25 -07001878 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001879 if len(objFiles) == 1 {
1880 outputFile = objFiles[0]
1881 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001882 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001883 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001884 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001885 }
1886
Colin Cross3f40fa42015-01-30 17:27:36 -08001887 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001888 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001889}
1890
Colin Crossc99deeb2016-04-11 15:06:20 -07001891func (*objectLinker) installable() bool {
1892 return false
1893}
1894
Colin Cross3f40fa42015-01-30 17:27:36 -08001895//
1896// Executables
1897//
1898
Colin Crossca860ac2016-01-04 14:34:37 -08001899type binaryLinker struct {
1900 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001901 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001902
Colin Crossca860ac2016-01-04 14:34:37 -08001903 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001904
Colin Cross635c3b02016-05-18 15:37:25 -07001905 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001906}
1907
Colin Crossca860ac2016-01-04 14:34:37 -08001908var _ linker = (*binaryLinker)(nil)
1909
1910func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001911 return append(binary.baseLinker.props(),
1912 &binary.Properties,
1913 &binary.stripper.StripProperties)
1914
Colin Cross3f40fa42015-01-30 17:27:36 -08001915}
1916
Colin Crossca860ac2016-01-04 14:34:37 -08001917func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001918 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001919}
1920
Colin Crossca860ac2016-01-04 14:34:37 -08001921func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001922 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001923}
1924
Colin Crossca860ac2016-01-04 14:34:37 -08001925func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001926 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001927 if binary.Properties.Stem != "" {
1928 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001929 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001930
Colin Crossca860ac2016-01-04 14:34:37 -08001931 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001932}
1933
Colin Crossca860ac2016-01-04 14:34:37 -08001934func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1935 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001936 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001937 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001938 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001939 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001940 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001941 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001942 }
Colin Crossca860ac2016-01-04 14:34:37 -08001943 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001944 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001945 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001946 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001947 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001948 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001949 }
Colin Crossca860ac2016-01-04 14:34:37 -08001950 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001951 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001952
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001953 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001954 if inList("libc++_static", deps.StaticLibs) {
1955 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001956 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001957 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1958 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1959 // move them to the beginning of deps.LateStaticLibs
1960 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001961 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001962 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001963 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001964 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001965 }
Colin Crossca860ac2016-01-04 14:34:37 -08001966
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001967 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001968 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1969 "from static libs or set static_executable: true")
1970 }
1971 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001972}
1973
Colin Crossc99deeb2016-04-11 15:06:20 -07001974func (*binaryLinker) installable() bool {
1975 return true
1976}
1977
Colin Cross16b23492016-01-06 14:41:07 -08001978func (binary *binaryLinker) isDependencyRoot() bool {
1979 return true
1980}
1981
Colin Cross635c3b02016-05-18 15:37:25 -07001982func NewBinary(hod android.HostOrDeviceSupported) *Module {
1983 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001984 module.compiler = &baseCompiler{}
1985 module.linker = &binaryLinker{}
1986 module.installer = &baseInstaller{
1987 dir: "bin",
1988 }
1989 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001990}
1991
Colin Crossca860ac2016-01-04 14:34:37 -08001992func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001993 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001994 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001995}
1996
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001997func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1998 binary.baseLinker.begin(ctx)
1999
2000 static := Bool(binary.Properties.Static_executable)
2001 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07002002 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002003 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
2004 static = true
2005 }
2006 } else {
2007 // Static executables are not supported on Darwin or Windows
2008 static = false
2009 }
Colin Cross0af4b842015-04-30 16:36:18 -07002010 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002011 if static {
2012 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08002013 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07002014 }
2015}
2016
Colin Crossca860ac2016-01-04 14:34:37 -08002017func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2018 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002019
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002020 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002021 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002022 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002023 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2024 }
2025 }
2026
2027 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2028 // all code is position independent, and then those warnings get promoted to
2029 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002030 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002031 flags.CFlags = append(flags.CFlags, "-fpie")
2032 }
Colin Cross97ba0732015-03-23 17:50:24 -07002033
Colin Crossf6566ed2015-03-24 11:13:38 -07002034 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002035 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002036 // Clang driver needs -static to create static executable.
2037 // However, bionic/linker uses -shared to overwrite.
2038 // Linker for x86 targets does not allow coexistance of -static and -shared,
2039 // so we add -static only if -shared is not used.
2040 if !inList("-shared", flags.LdFlags) {
2041 flags.LdFlags = append(flags.LdFlags, "-static")
2042 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002043
Colin Crossed4cf0b2015-03-26 14:43:45 -07002044 flags.LdFlags = append(flags.LdFlags,
2045 "-nostdlib",
2046 "-Bstatic",
2047 "-Wl,--gc-sections",
2048 )
2049
2050 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002051 if flags.DynamicLinker == "" {
2052 flags.DynamicLinker = "/system/bin/linker"
2053 if flags.Toolchain.Is64Bit() {
2054 flags.DynamicLinker += "64"
2055 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002056 }
2057
2058 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002059 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002060 "-nostdlib",
2061 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002062 "-Wl,--gc-sections",
2063 "-Wl,-z,nocopyreloc",
2064 )
2065 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002066 } else {
2067 if binary.staticBinary() {
2068 flags.LdFlags = append(flags.LdFlags, "-static")
2069 }
2070 if ctx.Darwin() {
2071 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2072 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002073 }
2074
Colin Cross97ba0732015-03-23 17:50:24 -07002075 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002076}
2077
Colin Crossca860ac2016-01-04 14:34:37 -08002078func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002079 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002080
Colin Cross665dce92016-04-28 14:50:03 -07002081 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002082 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002083 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002084 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002085 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002086 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002087
Colin Cross635c3b02016-05-18 15:37:25 -07002088 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002089
Colin Crossca860ac2016-01-04 14:34:37 -08002090 sharedLibs := deps.SharedLibs
2091 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2092
Colin Cross16b23492016-01-06 14:41:07 -08002093 if flags.DynamicLinker != "" {
2094 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2095 }
2096
Colin Cross665dce92016-04-28 14:50:03 -07002097 builderFlags := flagsToBuilderFlags(flags)
2098
2099 if binary.stripper.needsStrip(ctx) {
2100 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002101 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002102 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2103 }
2104
2105 if binary.Properties.Prefix_symbols != "" {
2106 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002107 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002108 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2109 flagsToBuilderFlags(flags), afterPrefixSymbols)
2110 }
2111
Colin Crossca860ac2016-01-04 14:34:37 -08002112 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002113 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002114 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002115
2116 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002117}
Colin Cross3f40fa42015-01-30 17:27:36 -08002118
Colin Cross635c3b02016-05-18 15:37:25 -07002119func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002120 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002121}
2122
Colin Cross665dce92016-04-28 14:50:03 -07002123type stripper struct {
2124 StripProperties StripProperties
2125}
2126
2127func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2128 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2129}
2130
Colin Cross635c3b02016-05-18 15:37:25 -07002131func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002132 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002133 if ctx.Darwin() {
2134 TransformDarwinStrip(ctx, in, out)
2135 } else {
2136 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2137 // TODO(ccross): don't add gnu debuglink for user builds
2138 flags.stripAddGnuDebuglink = true
2139 TransformStrip(ctx, in, out, flags)
2140 }
Colin Cross665dce92016-04-28 14:50:03 -07002141}
2142
Colin Cross635c3b02016-05-18 15:37:25 -07002143func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002144 if m, ok := mctx.Module().(*Module); ok {
Colin Crossc7a38dc2016-07-12 13:13:09 -07002145 if test, ok := m.linker.(*testBinaryLinker); ok {
2146 if Bool(test.testLinker.Properties.Test_per_src) {
Colin Crossca860ac2016-01-04 14:34:37 -08002147 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2148 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2149 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2150 }
2151 tests := mctx.CreateLocalVariations(testNames...)
2152 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2153 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002154 tests[i].(*Module).linker.(*testBinaryLinker).binaryLinker.Properties.Stem = testNames[i]
Colin Crossca860ac2016-01-04 14:34:37 -08002155 }
Colin Cross6002e052015-09-16 16:00:08 -07002156 }
2157 }
2158 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002159}
2160
Colin Crossca860ac2016-01-04 14:34:37 -08002161type testLinker struct {
Colin Crossca860ac2016-01-04 14:34:37 -08002162 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002163}
2164
Colin Crossca860ac2016-01-04 14:34:37 -08002165func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -08002166 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002167 return flags
2168 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002169
Colin Cross97ba0732015-03-23 17:50:24 -07002170 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002171 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002172 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002173
Colin Crossa1ad8d12016-06-01 17:09:44 -07002174 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002175 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002176 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002177 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002178 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2179 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002180 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002181 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2182 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002183 }
2184 } else {
2185 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002186 }
2187
Colin Cross21b9a242015-03-24 14:15:58 -07002188 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002189}
2190
Colin Crossca860ac2016-01-04 14:34:37 -08002191func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2192 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002193 if ctx.sdk() && ctx.Device() {
2194 switch ctx.selectedStl() {
2195 case "ndk_libc++_shared", "ndk_libc++_static":
2196 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2197 case "ndk_libgnustl_static":
2198 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2199 default:
2200 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2201 }
2202 } else {
2203 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2204 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002205 }
Colin Crossc7a38dc2016-07-12 13:13:09 -07002206 return deps
2207}
2208
2209type testBinaryLinker struct {
2210 testLinker
2211 binaryLinker
2212}
2213
2214func (test *testBinaryLinker) begin(ctx BaseModuleContext) {
2215 test.binaryLinker.begin(ctx)
2216 runpath := "../../lib"
2217 if ctx.toolchain().Is64Bit() {
2218 runpath += "64"
2219 }
2220 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
2221}
2222
2223func (test *testBinaryLinker) props() []interface{} {
2224 return append(test.binaryLinker.props(), &test.testLinker.Properties)
2225}
2226
2227func (test *testBinaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2228 flags = test.binaryLinker.flags(ctx, flags)
2229 flags = test.testLinker.flags(ctx, flags)
2230 return flags
2231}
2232
2233func (test *testBinaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2234 deps = test.testLinker.deps(ctx, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08002235 deps = test.binaryLinker.deps(ctx, deps)
2236 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002237}
2238
Colin Crossc7a38dc2016-07-12 13:13:09 -07002239type testLibraryLinker struct {
2240 testLinker
2241 *libraryLinker
2242}
2243
2244func (test *testLibraryLinker) props() []interface{} {
2245 return append(test.libraryLinker.props(), &test.testLinker.Properties)
2246}
2247
2248func (test *testLibraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2249 flags = test.libraryLinker.flags(ctx, flags)
2250 flags = test.testLinker.flags(ctx, flags)
2251 return flags
2252}
2253
2254func (test *testLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2255 deps = test.testLinker.deps(ctx, deps)
2256 deps = test.libraryLinker.deps(ctx, deps)
2257 return deps
2258}
2259
Colin Crossca860ac2016-01-04 14:34:37 -08002260type testInstaller struct {
2261 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002262}
2263
Colin Cross635c3b02016-05-18 15:37:25 -07002264func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002265 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2266 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2267 installer.baseInstaller.install(ctx, file)
2268}
2269
Colin Cross635c3b02016-05-18 15:37:25 -07002270func NewTest(hod android.HostOrDeviceSupported) *Module {
2271 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002272 module.compiler = &baseCompiler{}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002273 linker := &testBinaryLinker{}
2274 linker.testLinker.Properties.Gtest = true
Colin Crossca860ac2016-01-04 14:34:37 -08002275 module.linker = linker
2276 module.installer = &testInstaller{
2277 baseInstaller: baseInstaller{
2278 dir: "nativetest",
2279 dir64: "nativetest64",
2280 data: true,
2281 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002282 }
Colin Crossca860ac2016-01-04 14:34:37 -08002283 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002284}
2285
Colin Crossca860ac2016-01-04 14:34:37 -08002286func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002287 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002288 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002289}
2290
Colin Crossc7a38dc2016-07-12 13:13:09 -07002291func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
2292 module := NewLibrary(android.HostAndDeviceSupported, false, true)
2293 linker := &testLibraryLinker{
2294 libraryLinker: module.linker.(*libraryLinker),
2295 }
2296 linker.testLinker.Properties.Gtest = true
2297 module.linker = linker
2298 module.installer = &testInstaller{
2299 baseInstaller: baseInstaller{
2300 dir: "nativetest",
2301 dir64: "nativetest64",
2302 data: true,
2303 },
2304 }
2305 return module
2306}
2307
2308func testLibraryFactory() (blueprint.Module, []interface{}) {
2309 module := NewTestLibrary(android.HostAndDeviceSupported)
2310 return module.Init()
2311}
2312
Colin Crossca860ac2016-01-04 14:34:37 -08002313type benchmarkLinker struct {
2314 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002315}
2316
Colin Crossca860ac2016-01-04 14:34:37 -08002317func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2318 deps = benchmark.binaryLinker.deps(ctx, deps)
Colin Cross26832742016-07-11 14:57:56 -07002319 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
Colin Crossca860ac2016-01-04 14:34:37 -08002320 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002321}
2322
Colin Cross635c3b02016-05-18 15:37:25 -07002323func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2324 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002325 module.compiler = &baseCompiler{}
2326 module.linker = &benchmarkLinker{}
Colin Cross624b8ed2016-07-11 17:20:09 -07002327 module.installer = &testInstaller{
2328 baseInstaller: baseInstaller{
2329 dir: "nativetest",
2330 dir64: "nativetest64",
2331 data: true,
2332 },
Colin Cross2ba19d92015-05-07 15:44:20 -07002333 }
Colin Crossca860ac2016-01-04 14:34:37 -08002334 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002335}
2336
Colin Crossca860ac2016-01-04 14:34:37 -08002337func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002338 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002339 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002340}
2341
Colin Cross3f40fa42015-01-30 17:27:36 -08002342//
2343// Static library
2344//
2345
Colin Crossca860ac2016-01-04 14:34:37 -08002346func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002347 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002348 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002349}
2350
2351//
2352// Shared libraries
2353//
2354
Colin Crossca860ac2016-01-04 14:34:37 -08002355func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002356 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002357 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002358}
2359
2360//
2361// Host static library
2362//
2363
Colin Crossca860ac2016-01-04 14:34:37 -08002364func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002365 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002366 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002367}
2368
2369//
2370// Host Shared libraries
2371//
2372
Colin Crossca860ac2016-01-04 14:34:37 -08002373func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002374 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002375 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002376}
2377
2378//
2379// Host Binaries
2380//
2381
Colin Crossca860ac2016-01-04 14:34:37 -08002382func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002383 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002384 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002385}
2386
2387//
Colin Cross1f8f2342015-03-26 16:09:47 -07002388// Host Tests
2389//
2390
Colin Crossca860ac2016-01-04 14:34:37 -08002391func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002392 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002393 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002394}
2395
2396//
Colin Cross2ba19d92015-05-07 15:44:20 -07002397// Host Benchmarks
2398//
2399
Colin Crossca860ac2016-01-04 14:34:37 -08002400func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002401 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002402 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002403}
2404
2405//
Colin Crosscfad1192015-11-02 16:43:11 -08002406// Defaults
2407//
Colin Crossca860ac2016-01-04 14:34:37 -08002408type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002409 android.ModuleBase
2410 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002411}
2412
Colin Cross635c3b02016-05-18 15:37:25 -07002413func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002414}
2415
Colin Crossca860ac2016-01-04 14:34:37 -08002416func defaultsFactory() (blueprint.Module, []interface{}) {
2417 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002418
2419 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002420 &BaseProperties{},
2421 &BaseCompilerProperties{},
2422 &BaseLinkerProperties{},
2423 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002424 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002425 &LibraryLinkerProperties{},
2426 &BinaryLinkerProperties{},
2427 &TestLinkerProperties{},
2428 &UnusedProperties{},
2429 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002430 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002431 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002432 }
2433
Colin Cross635c3b02016-05-18 15:37:25 -07002434 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2435 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002436
Colin Cross635c3b02016-05-18 15:37:25 -07002437 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002438}
2439
2440//
Colin Cross3f40fa42015-01-30 17:27:36 -08002441// Device libraries shipped with gcc
2442//
2443
Colin Crossca860ac2016-01-04 14:34:37 -08002444type toolchainLibraryLinker struct {
2445 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002446}
2447
Colin Crossca860ac2016-01-04 14:34:37 -08002448var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2449
2450func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002451 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002452 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002453}
2454
Colin Crossca860ac2016-01-04 14:34:37 -08002455func (*toolchainLibraryLinker) buildStatic() bool {
2456 return true
2457}
Colin Cross3f40fa42015-01-30 17:27:36 -08002458
Colin Crossca860ac2016-01-04 14:34:37 -08002459func (*toolchainLibraryLinker) buildShared() bool {
2460 return false
2461}
2462
2463func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002464 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002465 module.compiler = &baseCompiler{}
2466 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002467 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002468 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002469}
2470
Colin Crossca860ac2016-01-04 14:34:37 -08002471func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002472 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002473
2474 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002475 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002476
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002477 if flags.Clang {
2478 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2479 }
2480
Colin Crossca860ac2016-01-04 14:34:37 -08002481 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002482
2483 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002484
Colin Crossca860ac2016-01-04 14:34:37 -08002485 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002486}
2487
Colin Crossc99deeb2016-04-11 15:06:20 -07002488func (*toolchainLibraryLinker) installable() bool {
2489 return false
2490}
2491
Dan Albertbe961682015-03-18 23:38:50 -07002492// NDK prebuilt libraries.
2493//
2494// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2495// either (with the exception of the shared STLs, which are installed to the app's directory rather
2496// than to the system image).
2497
Colin Cross635c3b02016-05-18 15:37:25 -07002498func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002499 suffix := ""
2500 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2501 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002502 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002503 suffix = "64"
2504 }
Colin Cross635c3b02016-05-18 15:37:25 -07002505 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002506 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002507}
2508
Colin Cross635c3b02016-05-18 15:37:25 -07002509func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2510 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002511
2512 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2513 // We want to translate to just NAME.EXT
2514 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2515 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002516 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002517}
2518
Colin Crossca860ac2016-01-04 14:34:37 -08002519type ndkPrebuiltObjectLinker struct {
2520 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002521}
2522
Colin Crossca860ac2016-01-04 14:34:37 -08002523func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002524 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002525 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002526}
2527
Colin Crossca860ac2016-01-04 14:34:37 -08002528func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002529 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002530 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002531 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002532 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002533}
2534
Colin Crossca860ac2016-01-04 14:34:37 -08002535func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002536 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002537 // A null build step, but it sets up the output path.
2538 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2539 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2540 }
2541
Colin Crossca860ac2016-01-04 14:34:37 -08002542 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002543}
2544
Colin Crossca860ac2016-01-04 14:34:37 -08002545type ndkPrebuiltLibraryLinker struct {
2546 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002547}
2548
Colin Crossca860ac2016-01-04 14:34:37 -08002549var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2550var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002551
Colin Crossca860ac2016-01-04 14:34:37 -08002552func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002553 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002554}
2555
Colin Crossca860ac2016-01-04 14:34:37 -08002556func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002557 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002558 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002559}
2560
Colin Crossca860ac2016-01-04 14:34:37 -08002561func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002562 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002563 linker := &ndkPrebuiltLibraryLinker{}
2564 linker.dynamicProperties.BuildShared = true
2565 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002566 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002567 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002568}
2569
Colin Crossca860ac2016-01-04 14:34:37 -08002570func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002571 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002572 // A null build step, but it sets up the output path.
2573 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2574 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2575 }
2576
Colin Cross919281a2016-04-05 16:42:05 -07002577 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002578
Colin Crossca860ac2016-01-04 14:34:37 -08002579 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2580 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002581}
2582
2583// The NDK STLs are slightly different from the prebuilt system libraries:
2584// * Are not specific to each platform version.
2585// * The libraries are not in a predictable location for each STL.
2586
Colin Crossca860ac2016-01-04 14:34:37 -08002587type ndkPrebuiltStlLinker struct {
2588 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002589}
2590
Colin Crossca860ac2016-01-04 14:34:37 -08002591func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002592 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002593 linker := &ndkPrebuiltStlLinker{}
2594 linker.dynamicProperties.BuildShared = true
2595 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002596 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002597 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002598}
2599
Colin Crossca860ac2016-01-04 14:34:37 -08002600func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002601 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002602 linker := &ndkPrebuiltStlLinker{}
2603 linker.dynamicProperties.BuildStatic = true
2604 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002605 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002606 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002607}
2608
Colin Cross635c3b02016-05-18 15:37:25 -07002609func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002610 gccVersion := toolchain.GccVersion()
2611 var libDir string
2612 switch stl {
2613 case "libstlport":
2614 libDir = "cxx-stl/stlport/libs"
2615 case "libc++":
2616 libDir = "cxx-stl/llvm-libc++/libs"
2617 case "libgnustl":
2618 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2619 }
2620
2621 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002622 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002623 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002624 }
2625
2626 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002627 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002628}
2629
Colin Crossca860ac2016-01-04 14:34:37 -08002630func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002631 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002632 // A null build step, but it sets up the output path.
2633 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2634 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2635 }
2636
Colin Cross919281a2016-04-05 16:42:05 -07002637 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002638
2639 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002640 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002641 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002642 libExt = staticLibraryExtension
2643 }
2644
2645 stlName := strings.TrimSuffix(libName, "_shared")
2646 stlName = strings.TrimSuffix(stlName, "_static")
2647 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002648 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002649}
2650
Colin Cross635c3b02016-05-18 15:37:25 -07002651func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002652 if m, ok := mctx.Module().(*Module); ok {
2653 if m.linker != nil {
2654 if linker, ok := m.linker.(baseLinkerInterface); ok {
2655 var modules []blueprint.Module
2656 if linker.buildStatic() && linker.buildShared() {
2657 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002658 static := modules[0].(*Module)
2659 shared := modules[1].(*Module)
2660
2661 static.linker.(baseLinkerInterface).setStatic(true)
2662 shared.linker.(baseLinkerInterface).setStatic(false)
2663
2664 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2665 sharedCompiler := shared.compiler.(*libraryCompiler)
2666 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2667 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2668 // Optimize out compiling common .o files twice for static+shared libraries
2669 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2670 sharedCompiler.baseCompiler.Properties.Srcs = nil
2671 }
2672 }
Colin Crossca860ac2016-01-04 14:34:37 -08002673 } else if linker.buildStatic() {
2674 modules = mctx.CreateLocalVariations("static")
2675 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2676 } else if linker.buildShared() {
2677 modules = mctx.CreateLocalVariations("shared")
2678 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2679 } else {
2680 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2681 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002682 }
2683 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002684 }
2685}
Colin Cross74d1ec02015-04-28 13:30:13 -07002686
2687// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2688// modifies the slice contents in place, and returns a subslice of the original slice
2689func lastUniqueElements(list []string) []string {
2690 totalSkip := 0
2691 for i := len(list) - 1; i >= totalSkip; i-- {
2692 skip := 0
2693 for j := i - 1; j >= totalSkip; j-- {
2694 if list[i] == list[j] {
2695 skip++
2696 } else {
2697 list[j+skip] = list[j]
2698 }
2699 }
2700 totalSkip += skip
2701 }
2702 return list[totalSkip:]
2703}
Colin Cross06a931b2015-10-28 17:23:31 -07002704
2705var Bool = proptools.Bool