blob: f8723bb1389031f2a5a8c5df3ee63a514a6a0353 [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 Crossa89d2e12016-01-11 12:48:37 -0800359
360 // don't link in crt_begin and crt_end. This flag should only be necessary for
361 // compiling crt or libc.
362 Nocrt *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800363}
Colin Cross7d5136f2015-05-11 13:39:40 -0700364
Colin Crossca860ac2016-01-04 14:34:37 -0800365type LibraryCompilerProperties struct {
366 Static struct {
367 Srcs []string `android:"arch_variant"`
368 Exclude_srcs []string `android:"arch_variant"`
369 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700370 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800371 Shared struct {
372 Srcs []string `android:"arch_variant"`
373 Exclude_srcs []string `android:"arch_variant"`
374 Cflags []string `android:"arch_variant"`
375 } `android:"arch_variant"`
376}
377
Colin Cross919281a2016-04-05 16:42:05 -0700378type FlagExporterProperties struct {
379 // list of directories relative to the Blueprints file that will
380 // be added to the include path using -I for any module that links against this module
381 Export_include_dirs []string `android:"arch_variant"`
382}
383
Colin Crossca860ac2016-01-04 14:34:37 -0800384type LibraryLinkerProperties struct {
385 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700386 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800387 Whole_static_libs []string `android:"arch_variant"`
388 Static_libs []string `android:"arch_variant"`
389 Shared_libs []string `android:"arch_variant"`
390 } `android:"arch_variant"`
391 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700392 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800393 Whole_static_libs []string `android:"arch_variant"`
394 Static_libs []string `android:"arch_variant"`
395 Shared_libs []string `android:"arch_variant"`
396 } `android:"arch_variant"`
397
398 // local file name to pass to the linker as --version_script
399 Version_script *string `android:"arch_variant"`
400 // local file name to pass to the linker as -unexported_symbols_list
401 Unexported_symbols_list *string `android:"arch_variant"`
402 // local file name to pass to the linker as -force_symbols_not_weak_list
403 Force_symbols_not_weak_list *string `android:"arch_variant"`
404 // local file name to pass to the linker as -force_symbols_weak_list
405 Force_symbols_weak_list *string `android:"arch_variant"`
406
Colin Cross16b23492016-01-06 14:41:07 -0800407 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
Colin Cross3854a602016-01-11 12:49:11 -0800457
458 // install symlinks to the module
459 Symlinks []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700460}
461
Colin Cross665dce92016-04-28 14:50:03 -0700462type StripProperties struct {
463 Strip struct {
464 None bool
465 Keep_symbols bool
466 }
467}
468
Colin Crossca860ac2016-01-04 14:34:37 -0800469type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700470 Native_coverage *bool
471 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700472 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800473}
474
Colin Crossca860ac2016-01-04 14:34:37 -0800475type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800476 static() bool
477 staticBinary() bool
478 clang() bool
479 toolchain() Toolchain
480 noDefaultCompilerFlags() bool
481 sdk() bool
482 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700483 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800484}
485
486type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700487 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800488 ModuleContextIntf
489}
490
491type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700492 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800493 ModuleContextIntf
494}
495
496type Customizer interface {
497 CustomizeProperties(BaseModuleContext)
498 Properties() []interface{}
499}
500
501type feature interface {
502 begin(ctx BaseModuleContext)
503 deps(ctx BaseModuleContext, deps Deps) Deps
504 flags(ctx ModuleContext, flags Flags) Flags
505 props() []interface{}
506}
507
508type compiler interface {
509 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700510 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800511}
512
513type linker interface {
514 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700515 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700516 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800517}
518
519type installer interface {
520 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700521 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800522 inData() bool
523}
524
Colin Crossc99deeb2016-04-11 15:06:20 -0700525type dependencyTag struct {
526 blueprint.BaseDependencyTag
527 name string
528 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700529
530 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700531}
532
533var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700534 sharedDepTag = dependencyTag{name: "shared", library: true}
535 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
536 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
537 staticDepTag = dependencyTag{name: "static", library: true}
538 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
539 lateStaticDepTag = dependencyTag{name: "late static", library: true}
540 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
541 genSourceDepTag = dependencyTag{name: "gen source"}
542 genHeaderDepTag = dependencyTag{name: "gen header"}
543 objDepTag = dependencyTag{name: "obj"}
544 crtBeginDepTag = dependencyTag{name: "crtbegin"}
545 crtEndDepTag = dependencyTag{name: "crtend"}
546 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700547)
548
Colin Crossca860ac2016-01-04 14:34:37 -0800549// Module contains the properties and members used by all C/C++ module types, and implements
550// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
551// to construct the output file. Behavior can be customized with a Customizer interface
552type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700553 android.ModuleBase
554 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700555
Colin Crossca860ac2016-01-04 14:34:37 -0800556 Properties BaseProperties
557 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700558
Colin Crossca860ac2016-01-04 14:34:37 -0800559 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700560 hod android.HostOrDeviceSupported
561 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700562
Colin Crossca860ac2016-01-04 14:34:37 -0800563 // delegates, initialize before calling Init
564 customizer Customizer
565 features []feature
566 compiler compiler
567 linker linker
568 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700569 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800570 sanitize *sanitize
571
572 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700573
Colin Cross635c3b02016-05-18 15:37:25 -0700574 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800575
576 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700577}
578
Colin Crossca860ac2016-01-04 14:34:37 -0800579func (c *Module) Init() (blueprint.Module, []interface{}) {
580 props := []interface{}{&c.Properties, &c.unused}
581 if c.customizer != nil {
582 props = append(props, c.customizer.Properties()...)
583 }
584 if c.compiler != nil {
585 props = append(props, c.compiler.props()...)
586 }
587 if c.linker != nil {
588 props = append(props, c.linker.props()...)
589 }
590 if c.installer != nil {
591 props = append(props, c.installer.props()...)
592 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700593 if c.stl != nil {
594 props = append(props, c.stl.props()...)
595 }
Colin Cross16b23492016-01-06 14:41:07 -0800596 if c.sanitize != nil {
597 props = append(props, c.sanitize.props()...)
598 }
Colin Crossca860ac2016-01-04 14:34:37 -0800599 for _, feature := range c.features {
600 props = append(props, feature.props()...)
601 }
Colin Crossc472d572015-03-17 15:06:21 -0700602
Colin Cross635c3b02016-05-18 15:37:25 -0700603 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700604
Colin Cross635c3b02016-05-18 15:37:25 -0700605 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700606}
607
Colin Crossca860ac2016-01-04 14:34:37 -0800608type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700609 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800610 moduleContextImpl
611}
612
613type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700614 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800615 moduleContextImpl
616}
617
618type moduleContextImpl struct {
619 mod *Module
620 ctx BaseModuleContext
621}
622
Colin Crossca860ac2016-01-04 14:34:37 -0800623func (ctx *moduleContextImpl) clang() bool {
624 return ctx.mod.clang(ctx.ctx)
625}
626
627func (ctx *moduleContextImpl) toolchain() Toolchain {
628 return ctx.mod.toolchain(ctx.ctx)
629}
630
631func (ctx *moduleContextImpl) static() bool {
632 if ctx.mod.linker == nil {
633 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
634 }
635 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
636 return linker.static()
637 } else {
638 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
639 }
640}
641
642func (ctx *moduleContextImpl) staticBinary() bool {
643 if ctx.mod.linker == nil {
644 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
645 }
646 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
647 return linker.staticBinary()
648 } else {
649 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
650 }
651}
652
653func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
654 return Bool(ctx.mod.Properties.No_default_compiler_flags)
655}
656
657func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700658 if ctx.ctx.Device() {
659 return ctx.mod.Properties.Sdk_version != ""
660 }
661 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800662}
663
664func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700665 if ctx.ctx.Device() {
666 return ctx.mod.Properties.Sdk_version
667 }
668 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800669}
670
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700671func (ctx *moduleContextImpl) selectedStl() string {
672 if stl := ctx.mod.stl; stl != nil {
673 return stl.Properties.SelectedStl
674 }
675 return ""
676}
677
Colin Cross635c3b02016-05-18 15:37:25 -0700678func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800679 return &Module{
680 hod: hod,
681 multilib: multilib,
682 }
683}
684
Colin Cross635c3b02016-05-18 15:37:25 -0700685func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800686 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700687 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800688 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800689 return module
690}
691
Colin Cross635c3b02016-05-18 15:37:25 -0700692func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800693 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700694 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800695 moduleContextImpl: moduleContextImpl{
696 mod: c,
697 },
698 }
699 ctx.ctx = ctx
700
701 flags := Flags{
702 Toolchain: c.toolchain(ctx),
703 Clang: c.clang(ctx),
704 }
Colin Crossca860ac2016-01-04 14:34:37 -0800705 if c.compiler != nil {
706 flags = c.compiler.flags(ctx, flags)
707 }
708 if c.linker != nil {
709 flags = c.linker.flags(ctx, flags)
710 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700711 if c.stl != nil {
712 flags = c.stl.flags(ctx, flags)
713 }
Colin Cross16b23492016-01-06 14:41:07 -0800714 if c.sanitize != nil {
715 flags = c.sanitize.flags(ctx, flags)
716 }
Colin Crossca860ac2016-01-04 14:34:37 -0800717 for _, feature := range c.features {
718 flags = feature.flags(ctx, flags)
719 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800720 if ctx.Failed() {
721 return
722 }
723
Colin Crossca860ac2016-01-04 14:34:37 -0800724 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
725 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
726 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800727
Colin Crossca860ac2016-01-04 14:34:37 -0800728 // Optimization to reduce size of build.ninja
729 // Replace the long list of flags for each file with a module-local variable
730 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
731 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
732 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
733 flags.CFlags = []string{"$cflags"}
734 flags.CppFlags = []string{"$cppflags"}
735 flags.AsFlags = []string{"$asflags"}
736
Colin Crossc99deeb2016-04-11 15:06:20 -0700737 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800738 if ctx.Failed() {
739 return
740 }
741
Dan Willemsen76f08272016-07-09 00:14:08 -0700742 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700743
Colin Cross635c3b02016-05-18 15:37:25 -0700744 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800745 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700746 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800747 if ctx.Failed() {
748 return
749 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800750 }
751
Colin Crossca860ac2016-01-04 14:34:37 -0800752 if c.linker != nil {
753 outputFile := c.linker.link(ctx, flags, deps, objFiles)
754 if ctx.Failed() {
755 return
756 }
Colin Cross635c3b02016-05-18 15:37:25 -0700757 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700758
Colin Crossc99deeb2016-04-11 15:06:20 -0700759 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800760 c.installer.install(ctx, outputFile)
761 if ctx.Failed() {
762 return
763 }
764 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700765 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800766}
767
Colin Crossca860ac2016-01-04 14:34:37 -0800768func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
769 if c.cachedToolchain == nil {
770 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700771 os := ctx.Os()
772 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800773 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700774 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800775 return nil
776 }
777 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800778 }
Colin Crossca860ac2016-01-04 14:34:37 -0800779 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800780}
781
Colin Crossca860ac2016-01-04 14:34:37 -0800782func (c *Module) begin(ctx BaseModuleContext) {
783 if c.compiler != nil {
784 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700785 }
Colin Crossca860ac2016-01-04 14:34:37 -0800786 if c.linker != nil {
787 c.linker.begin(ctx)
788 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700789 if c.stl != nil {
790 c.stl.begin(ctx)
791 }
Colin Cross16b23492016-01-06 14:41:07 -0800792 if c.sanitize != nil {
793 c.sanitize.begin(ctx)
794 }
Colin Crossca860ac2016-01-04 14:34:37 -0800795 for _, feature := range c.features {
796 feature.begin(ctx)
797 }
798}
799
Colin Crossc99deeb2016-04-11 15:06:20 -0700800func (c *Module) deps(ctx BaseModuleContext) Deps {
801 deps := Deps{}
802
803 if c.compiler != nil {
804 deps = c.compiler.deps(ctx, deps)
805 }
806 if c.linker != nil {
807 deps = c.linker.deps(ctx, deps)
808 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700809 if c.stl != nil {
810 deps = c.stl.deps(ctx, deps)
811 }
Colin Cross16b23492016-01-06 14:41:07 -0800812 if c.sanitize != nil {
813 deps = c.sanitize.deps(ctx, deps)
814 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700815 for _, feature := range c.features {
816 deps = feature.deps(ctx, deps)
817 }
818
819 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
820 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
821 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
822 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
823 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
824
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700825 for _, lib := range deps.ReexportSharedLibHeaders {
826 if !inList(lib, deps.SharedLibs) {
827 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
828 }
829 }
830
831 for _, lib := range deps.ReexportStaticLibHeaders {
832 if !inList(lib, deps.StaticLibs) {
833 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
834 }
835 }
836
Colin Crossc99deeb2016-04-11 15:06:20 -0700837 return deps
838}
839
Colin Cross635c3b02016-05-18 15:37:25 -0700840func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800841 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700842 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800843 moduleContextImpl: moduleContextImpl{
844 mod: c,
845 },
846 }
847 ctx.ctx = ctx
848
849 if c.customizer != nil {
850 c.customizer.CustomizeProperties(ctx)
851 }
852
853 c.begin(ctx)
854
Colin Crossc99deeb2016-04-11 15:06:20 -0700855 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800856
Colin Crossb5bc4b42016-07-11 16:11:59 -0700857 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
858 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700859
860 if ctx.sdk() {
861 version := "." + ctx.sdkVersion()
862
863 rewriteNdkLibs := func(list []string) []string {
864 for i, entry := range list {
865 if inList(entry, ndkPrebuiltSharedLibraries) {
866 list[i] = "ndk_" + entry + version
867 }
868 }
869 return list
870 }
871
872 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
873 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
874 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700875
876 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
877 deps.WholeStaticLibs...)
878
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700879 for _, lib := range deps.StaticLibs {
880 depTag := staticDepTag
881 if inList(lib, deps.ReexportStaticLibHeaders) {
882 depTag = staticExportDepTag
883 }
Colin Cross15a0d462016-07-14 14:49:58 -0700884 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700885 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700886
887 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
888 deps.LateStaticLibs...)
889
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700890 for _, lib := range deps.SharedLibs {
891 depTag := sharedDepTag
892 if inList(lib, deps.ReexportSharedLibHeaders) {
893 depTag = sharedExportDepTag
894 }
Colin Cross15a0d462016-07-14 14:49:58 -0700895 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700896 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700897
898 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
899 deps.LateSharedLibs...)
900
Colin Cross68861832016-07-08 10:41:41 -0700901 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
902 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700903
Colin Cross68861832016-07-08 10:41:41 -0700904 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700905
906 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700907 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800908 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700909 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700910 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700911 }
Colin Cross6362e272015-10-29 15:25:03 -0700912}
Colin Cross21b9a242015-03-24 14:15:58 -0700913
Colin Cross635c3b02016-05-18 15:37:25 -0700914func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700915 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700916 c.depsMutator(ctx)
917 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800918}
919
Colin Crossca860ac2016-01-04 14:34:37 -0800920func (c *Module) clang(ctx BaseModuleContext) bool {
921 clang := Bool(c.Properties.Clang)
922
923 if c.Properties.Clang == nil {
924 if ctx.Host() {
925 clang = true
926 }
927
928 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
929 clang = true
930 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800931 }
Colin Cross28344522015-04-22 13:07:53 -0700932
Colin Crossca860ac2016-01-04 14:34:37 -0800933 if !c.toolchain(ctx).ClangSupported() {
934 clang = false
935 }
936
937 return clang
938}
939
Colin Crossc99deeb2016-04-11 15:06:20 -0700940// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700941func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800942 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800943
Dan Willemsena96ff642016-06-07 12:34:45 -0700944 // Whether a module can link to another module, taking into
945 // account NDK linking.
946 linkTypeOk := func(from, to *Module) bool {
947 if from.Target().Os != android.Android {
948 // Host code is not restricted
949 return true
950 }
951 if from.Properties.Sdk_version == "" {
952 // Platform code can link to anything
953 return true
954 }
955 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
956 // These are always allowed
957 return true
958 }
959 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
960 // These are allowed, but don't set sdk_version
961 return true
962 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700963 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
964 // These are allowed, but don't set sdk_version
965 return true
966 }
967 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700968 }
969
Colin Crossc99deeb2016-04-11 15:06:20 -0700970 ctx.VisitDirectDeps(func(m blueprint.Module) {
971 name := ctx.OtherModuleName(m)
972 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800973
Colin Cross635c3b02016-05-18 15:37:25 -0700974 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700975 if a == nil {
976 ctx.ModuleErrorf("module %q not an android module", name)
977 return
Colin Crossca860ac2016-01-04 14:34:37 -0800978 }
Colin Crossca860ac2016-01-04 14:34:37 -0800979
Dan Willemsena96ff642016-06-07 12:34:45 -0700980 cc, _ := m.(*Module)
981 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700982 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700983 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700984 case genSourceDepTag:
985 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
986 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
987 genRule.GeneratedSourceFiles()...)
988 } else {
989 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
990 }
991 case genHeaderDepTag:
992 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
993 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
994 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700995 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700996 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700997 } else {
998 ctx.ModuleErrorf("module %q is not a genrule", name)
999 }
1000 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001001 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001002 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001003 return
1004 }
1005
1006 if !a.Enabled() {
1007 ctx.ModuleErrorf("depends on disabled module %q", name)
1008 return
1009 }
1010
Colin Crossa1ad8d12016-06-01 17:09:44 -07001011 if a.Target().Os != ctx.Os() {
1012 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1013 return
1014 }
1015
1016 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1017 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001018 return
1019 }
1020
Dan Willemsena96ff642016-06-07 12:34:45 -07001021 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001022 ctx.ModuleErrorf("module %q missing output file", name)
1023 return
1024 }
1025
1026 if tag == reuseObjTag {
1027 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001028 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001029 return
1030 }
1031
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001032 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001033 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001034 flags := i.exportedFlags()
1035 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001036
1037 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001038 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001039 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001040 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001041
1042 if !linkTypeOk(c, cc) {
1043 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1044 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001045 }
1046
Colin Cross635c3b02016-05-18 15:37:25 -07001047 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001048
1049 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001050 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001051 depPtr = &depPaths.SharedLibs
1052 case lateSharedDepTag:
1053 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001054 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001055 depPtr = &depPaths.StaticLibs
1056 case lateStaticDepTag:
1057 depPtr = &depPaths.LateStaticLibs
1058 case wholeStaticDepTag:
1059 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -07001060 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -07001061 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001062 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001063 return
1064 }
1065
1066 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1067 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1068 for i := range missingDeps {
1069 missingDeps[i] += postfix
1070 }
1071 ctx.AddMissingDependencies(missingDeps)
1072 }
1073 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -07001074 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001075 case objDepTag:
1076 depPtr = &depPaths.ObjFiles
1077 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001078 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001079 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001080 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001081 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001082 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001083 }
1084
1085 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001086 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001087 }
1088 })
1089
1090 return depPaths
1091}
1092
1093func (c *Module) InstallInData() bool {
1094 if c.installer == nil {
1095 return false
1096 }
1097 return c.installer.inData()
1098}
1099
1100// Compiler
1101
1102type baseCompiler struct {
1103 Properties BaseCompilerProperties
1104}
1105
1106var _ compiler = (*baseCompiler)(nil)
1107
1108func (compiler *baseCompiler) props() []interface{} {
1109 return []interface{}{&compiler.Properties}
1110}
1111
Dan Willemsenb40aab62016-04-20 14:21:14 -07001112func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1113
1114func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1115 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1116 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1117
1118 return deps
1119}
Colin Crossca860ac2016-01-04 14:34:37 -08001120
1121// Create a Flags struct that collects the compile flags from global values,
1122// per-target values, module type values, and per-module Blueprints properties
1123func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1124 toolchain := ctx.toolchain()
1125
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001126 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1127 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1128 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1129 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1130
Colin Crossca860ac2016-01-04 14:34:37 -08001131 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1132 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1133 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1134 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1135 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1136
Colin Cross28344522015-04-22 13:07:53 -07001137 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001138 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1139 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001140 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001141 includeDirsToFlags(localIncludeDirs),
1142 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001143
Colin Crossca860ac2016-01-04 14:34:37 -08001144 if !ctx.noDefaultCompilerFlags() {
1145 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001146 flags.GlobalFlags = append(flags.GlobalFlags,
1147 "${commonGlobalIncludes}",
1148 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001149 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001150 }
1151
1152 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001153 "-I" + android.PathForModuleSrc(ctx).String(),
1154 "-I" + android.PathForModuleOut(ctx).String(),
1155 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001156 }...)
1157 }
1158
Colin Crossca860ac2016-01-04 14:34:37 -08001159 instructionSet := compiler.Properties.Instruction_set
1160 if flags.RequiredInstructionSet != "" {
1161 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001162 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001163 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1164 if flags.Clang {
1165 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1166 }
1167 if err != nil {
1168 ctx.ModuleErrorf("%s", err)
1169 }
1170
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001171 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1172
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001173 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001174 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001175
Colin Cross97ba0732015-03-23 17:50:24 -07001176 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001177 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1178 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1179
Colin Cross97ba0732015-03-23 17:50:24 -07001180 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001181 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1182 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001183 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1184 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1185 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001186
1187 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001188 var gccPrefix string
1189 if !ctx.Darwin() {
1190 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1191 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001192
Colin Cross97ba0732015-03-23 17:50:24 -07001193 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1194 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1195 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001196 }
1197
Colin Crossa1ad8d12016-06-01 17:09:44 -07001198 hod := "host"
1199 if ctx.Os().Class == android.Device {
1200 hod = "device"
1201 }
1202
Colin Crossca860ac2016-01-04 14:34:37 -08001203 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001204 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1205
Colin Cross97ba0732015-03-23 17:50:24 -07001206 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001207 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001208 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001209 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001210 toolchain.ClangCflags(),
1211 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001212 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001213
1214 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001215 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001216 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001217 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001218 toolchain.Cflags(),
1219 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001220 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001221 }
1222
Colin Cross7b66f152015-12-15 16:07:43 -08001223 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1224 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1225 }
1226
Colin Crossf6566ed2015-03-24 11:13:38 -07001227 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001228 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001229 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001230 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001231 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001232 }
1233 }
1234
Colin Cross97ba0732015-03-23 17:50:24 -07001235 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001236
Colin Cross97ba0732015-03-23 17:50:24 -07001237 if flags.Clang {
1238 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001239 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001240 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001241 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001242 }
1243
Colin Crossc4bde762015-11-23 16:11:30 -08001244 if flags.Clang {
1245 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1246 } else {
1247 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001248 }
1249
Colin Crossca860ac2016-01-04 14:34:37 -08001250 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001251 if ctx.Host() && !flags.Clang {
1252 // The host GCC doesn't support C++14 (and is deprecated, so likely
1253 // never will). Build these modules with C++11.
1254 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1255 } else {
1256 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1257 }
1258 }
1259
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001260 // We can enforce some rules more strictly in the code we own. strict
1261 // indicates if this is code that we can be stricter with. If we have
1262 // rules that we want to apply to *our* code (but maybe can't for
1263 // vendor/device specific things), we could extend this to be a ternary
1264 // value.
1265 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001266 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001267 strict = false
1268 }
1269
1270 // Can be used to make some annotations stricter for code we can fix
1271 // (such as when we mark functions as deprecated).
1272 if strict {
1273 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1274 }
1275
Colin Cross3f40fa42015-01-30 17:27:36 -08001276 return flags
1277}
1278
Colin Cross635c3b02016-05-18 15:37:25 -07001279func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001280 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001281 objFiles := compiler.compileObjs(ctx, flags, "",
1282 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1283 deps.GeneratedSources, deps.GeneratedHeaders)
1284
Colin Crossca860ac2016-01-04 14:34:37 -08001285 if ctx.Failed() {
1286 return nil
1287 }
1288
Colin Crossca860ac2016-01-04 14:34:37 -08001289 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001290}
1291
1292// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001293func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1294 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001295
Colin Crossca860ac2016-01-04 14:34:37 -08001296 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001297
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001298 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001299 inputFiles = append(inputFiles, extraSrcs...)
1300 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1301
1302 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001303 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001304
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001305 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001306}
1307
Colin Crossca860ac2016-01-04 14:34:37 -08001308// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1309type baseLinker struct {
1310 Properties BaseLinkerProperties
1311 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001312 VariantIsShared bool `blueprint:"mutated"`
1313 VariantIsStatic bool `blueprint:"mutated"`
1314 VariantIsStaticBinary bool `blueprint:"mutated"`
1315 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001316 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001317}
1318
Dan Willemsend30e6102016-03-30 17:35:50 -07001319func (linker *baseLinker) begin(ctx BaseModuleContext) {
1320 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001321 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001322 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001323 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001324 }
1325}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001326
Colin Crossca860ac2016-01-04 14:34:37 -08001327func (linker *baseLinker) props() []interface{} {
1328 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001329}
1330
Colin Crossca860ac2016-01-04 14:34:37 -08001331func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1332 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1333 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1334 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001335
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001336 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1337 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1338
Dan Willemsena96ff642016-06-07 12:34:45 -07001339 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001340 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001341 }
1342
Colin Crossf6566ed2015-03-24 11:13:38 -07001343 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001344 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001345 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1346 if !Bool(linker.Properties.No_libgcc) {
1347 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001348 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001349
Colin Crossca860ac2016-01-04 14:34:37 -08001350 if !linker.static() {
1351 if linker.Properties.System_shared_libs != nil {
1352 deps.LateSharedLibs = append(deps.LateSharedLibs,
1353 linker.Properties.System_shared_libs...)
1354 } else if !ctx.sdk() {
1355 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1356 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001357 }
Colin Cross577f6e42015-03-27 18:23:34 -07001358
Colin Crossca860ac2016-01-04 14:34:37 -08001359 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001360 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001361 "libc",
1362 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001363 )
1364 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001365 }
1366
Colin Crossca860ac2016-01-04 14:34:37 -08001367 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001368}
1369
Colin Crossca860ac2016-01-04 14:34:37 -08001370func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1371 toolchain := ctx.toolchain()
1372
Colin Crossa89d2e12016-01-11 12:48:37 -08001373 flags.Nocrt = Bool(linker.Properties.Nocrt)
1374
Colin Crossca860ac2016-01-04 14:34:37 -08001375 if !ctx.noDefaultCompilerFlags() {
1376 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1377 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1378 }
1379
1380 if flags.Clang {
1381 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1382 } else {
1383 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1384 }
1385
1386 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001387 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1388
Colin Crossca860ac2016-01-04 14:34:37 -08001389 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1390 }
1391 }
1392
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001393 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1394
Dan Willemsen00ced762016-05-10 17:31:21 -07001395 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1396
Dan Willemsend30e6102016-03-30 17:35:50 -07001397 if ctx.Host() && !linker.static() {
1398 rpath_prefix := `\$$ORIGIN/`
1399 if ctx.Darwin() {
1400 rpath_prefix = "@loader_path/"
1401 }
1402
Colin Crossc99deeb2016-04-11 15:06:20 -07001403 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001404 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1405 }
1406 }
1407
Dan Willemsene7174922016-03-30 17:33:52 -07001408 if flags.Clang {
1409 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1410 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001411 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1412 }
1413
1414 return flags
1415}
1416
1417func (linker *baseLinker) static() bool {
1418 return linker.dynamicProperties.VariantIsStatic
1419}
1420
1421func (linker *baseLinker) staticBinary() bool {
1422 return linker.dynamicProperties.VariantIsStaticBinary
1423}
1424
1425func (linker *baseLinker) setStatic(static bool) {
1426 linker.dynamicProperties.VariantIsStatic = static
1427}
1428
Colin Cross16b23492016-01-06 14:41:07 -08001429func (linker *baseLinker) isDependencyRoot() bool {
1430 return false
1431}
1432
Colin Crossca860ac2016-01-04 14:34:37 -08001433type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001434 // Returns true if the build options for the module have selected a static or shared build
1435 buildStatic() bool
1436 buildShared() bool
1437
1438 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001439 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001440
Colin Cross18b6dc52015-04-28 13:20:37 -07001441 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001442 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001443
1444 // Returns whether a module is a static binary
1445 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001446
1447 // Returns true for dependency roots (binaries)
1448 // TODO(ccross): also handle dlopenable libraries
1449 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001450}
1451
Colin Crossca860ac2016-01-04 14:34:37 -08001452type baseInstaller struct {
1453 Properties InstallerProperties
1454
1455 dir string
1456 dir64 string
1457 data bool
1458
Colin Cross635c3b02016-05-18 15:37:25 -07001459 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001460}
1461
1462var _ installer = (*baseInstaller)(nil)
1463
1464func (installer *baseInstaller) props() []interface{} {
1465 return []interface{}{&installer.Properties}
1466}
1467
Colin Cross635c3b02016-05-18 15:37:25 -07001468func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001469 subDir := installer.dir
1470 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1471 subDir = installer.dir64
1472 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001473 if !ctx.Host() && !ctx.Arch().Native {
1474 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1475 }
Colin Cross635c3b02016-05-18 15:37:25 -07001476 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001477 installer.path = ctx.InstallFile(dir, file)
Colin Cross3854a602016-01-11 12:49:11 -08001478 for _, symlink := range installer.Properties.Symlinks {
1479 ctx.InstallSymlink(dir, symlink, installer.path)
1480 }
Colin Crossca860ac2016-01-04 14:34:37 -08001481}
1482
1483func (installer *baseInstaller) inData() bool {
1484 return installer.data
1485}
1486
Colin Cross3f40fa42015-01-30 17:27:36 -08001487//
1488// Combined static+shared libraries
1489//
1490
Colin Cross919281a2016-04-05 16:42:05 -07001491type flagExporter struct {
1492 Properties FlagExporterProperties
1493
1494 flags []string
1495}
1496
1497func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001498 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001499 for _, dir := range includeDirs.Strings() {
Colin Crossf87b2612016-07-13 18:55:43 -07001500 f.flags = append(f.flags, inc+dir)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001501 }
Colin Cross919281a2016-04-05 16:42:05 -07001502}
1503
1504func (f *flagExporter) reexportFlags(flags []string) {
1505 f.flags = append(f.flags, flags...)
1506}
1507
1508func (f *flagExporter) exportedFlags() []string {
1509 return f.flags
1510}
1511
1512type exportedFlagsProducer interface {
1513 exportedFlags() []string
1514}
1515
1516var _ exportedFlagsProducer = (*flagExporter)(nil)
1517
Colin Crossca860ac2016-01-04 14:34:37 -08001518type libraryCompiler struct {
1519 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001520
Colin Crossca860ac2016-01-04 14:34:37 -08001521 linker *libraryLinker
1522 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001523
Colin Crossca860ac2016-01-04 14:34:37 -08001524 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001525 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001526}
1527
Colin Crossca860ac2016-01-04 14:34:37 -08001528var _ compiler = (*libraryCompiler)(nil)
1529
1530func (library *libraryCompiler) props() []interface{} {
1531 props := library.baseCompiler.props()
1532 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001533}
1534
Colin Crossca860ac2016-01-04 14:34:37 -08001535func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1536 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001537
Dan Willemsen490fd492015-11-24 17:53:15 -08001538 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1539 // all code is position independent, and then those warnings get promoted to
1540 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001541 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001542 flags.CFlags = append(flags.CFlags, "-fPIC")
1543 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001544
Colin Crossca860ac2016-01-04 14:34:37 -08001545 if library.linker.static() {
1546 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001547 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001548 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001549 }
1550
Colin Crossca860ac2016-01-04 14:34:37 -08001551 return flags
1552}
1553
Colin Cross635c3b02016-05-18 15:37:25 -07001554func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1555 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001556
Dan Willemsenb40aab62016-04-20 14:21:14 -07001557 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001558 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001559
1560 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001561 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001562 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1563 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001564 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001565 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001566 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1567 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001568 }
1569
1570 return objFiles
1571}
1572
1573type libraryLinker struct {
1574 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001575 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001576 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001577
1578 Properties LibraryLinkerProperties
1579
1580 dynamicProperties struct {
1581 BuildStatic bool `blueprint:"mutated"`
1582 BuildShared bool `blueprint:"mutated"`
1583 }
1584
Colin Crossca860ac2016-01-04 14:34:37 -08001585 // If we're used as a whole_static_lib, our missing dependencies need
1586 // to be given
1587 wholeStaticMissingDeps []string
1588
1589 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001590 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001591}
1592
1593var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001594
Colin Crossc7a38dc2016-07-12 13:13:09 -07001595type libraryInterface interface {
1596 getWholeStaticMissingDeps() []string
1597 static() bool
1598 objs() android.Paths
1599}
1600
Colin Crossca860ac2016-01-04 14:34:37 -08001601func (library *libraryLinker) props() []interface{} {
1602 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001603 return append(props,
1604 &library.Properties,
1605 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001606 &library.flagExporter.Properties,
1607 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001608}
1609
1610func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1611 flags = library.baseLinker.flags(ctx, flags)
1612
Colin Crossca860ac2016-01-04 14:34:37 -08001613 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001614 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001615 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1616 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001617 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001618 sharedFlag = "-shared"
1619 }
Colin Crossf87b2612016-07-13 18:55:43 -07001620 var f []string
Colin Crossf6566ed2015-03-24 11:13:38 -07001621 if ctx.Device() {
Colin Crossf87b2612016-07-13 18:55:43 -07001622 f = append(f,
Dan Willemsen99db8c32016-03-03 18:05:38 -08001623 "-nostdlib",
1624 "-Wl,--gc-sections",
1625 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001626 }
Colin Cross97ba0732015-03-23 17:50:24 -07001627
Colin Cross0af4b842015-04-30 16:36:18 -07001628 if ctx.Darwin() {
Colin Crossf87b2612016-07-13 18:55:43 -07001629 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001630 "-dynamiclib",
1631 "-single_module",
1632 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001633 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001634 )
1635 } else {
Colin Crossf87b2612016-07-13 18:55:43 -07001636 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001637 sharedFlag,
Colin Crossf87b2612016-07-13 18:55:43 -07001638 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
Colin Cross0af4b842015-04-30 16:36:18 -07001639 }
Colin Crossf87b2612016-07-13 18:55:43 -07001640
1641 flags.LdFlags = append(f, flags.LdFlags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001642 }
Colin Cross97ba0732015-03-23 17:50:24 -07001643
1644 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001645}
1646
Colin Crossca860ac2016-01-04 14:34:37 -08001647func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1648 deps = library.baseLinker.deps(ctx, deps)
1649 if library.static() {
1650 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1651 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1652 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1653 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001654 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
Colin Crossca860ac2016-01-04 14:34:37 -08001655 if !ctx.sdk() {
1656 deps.CrtBegin = "crtbegin_so"
1657 deps.CrtEnd = "crtend_so"
1658 } else {
1659 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1660 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1661 }
1662 }
1663 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1664 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1665 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1666 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001667
Colin Crossca860ac2016-01-04 14:34:37 -08001668 return deps
1669}
Colin Cross3f40fa42015-01-30 17:27:36 -08001670
Colin Crossca860ac2016-01-04 14:34:37 -08001671func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001672 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001673
Colin Cross635c3b02016-05-18 15:37:25 -07001674 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001675 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001676
Colin Cross635c3b02016-05-18 15:37:25 -07001677 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001678 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001679
Colin Cross0af4b842015-04-30 16:36:18 -07001680 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001681 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001682 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001683 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001684 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001685
Colin Crossca860ac2016-01-04 14:34:37 -08001686 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001687
1688 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001689
1690 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001691}
1692
Colin Crossca860ac2016-01-04 14:34:37 -08001693func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001694 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001695
Colin Cross635c3b02016-05-18 15:37:25 -07001696 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001697
Colin Cross635c3b02016-05-18 15:37:25 -07001698 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1699 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1700 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1701 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001702 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001703 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001704 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001705 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001706 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001707 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001708 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1709 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001710 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001711 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1712 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001714 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1715 }
1716 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001717 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001718 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1719 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001720 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001721 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001722 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001723 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001724 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001725 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001726 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001727 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001728 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001729 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001730 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001731 }
Colin Crossaee540a2015-07-06 17:48:31 -07001732 }
1733
Colin Cross665dce92016-04-28 14:50:03 -07001734 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001735 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001736 ret := outputFile
1737
1738 builderFlags := flagsToBuilderFlags(flags)
1739
1740 if library.stripper.needsStrip(ctx) {
1741 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001742 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001743 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1744 }
1745
Colin Crossca860ac2016-01-04 14:34:37 -08001746 sharedLibs := deps.SharedLibs
1747 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001748
Colin Crossca860ac2016-01-04 14:34:37 -08001749 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1750 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001751 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001752
Colin Cross665dce92016-04-28 14:50:03 -07001753 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001754}
1755
Colin Crossca860ac2016-01-04 14:34:37 -08001756func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001757 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001758
Colin Crossc99deeb2016-04-11 15:06:20 -07001759 objFiles = append(objFiles, deps.ObjFiles...)
1760
Colin Cross635c3b02016-05-18 15:37:25 -07001761 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001762 if library.static() {
1763 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001764 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001765 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001766 }
1767
Colin Cross919281a2016-04-05 16:42:05 -07001768 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001769 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001770
1771 return out
1772}
1773
1774func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001775 return library.dynamicProperties.BuildStatic &&
1776 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001777}
1778
1779func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001780 return library.dynamicProperties.BuildShared &&
1781 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001782}
1783
1784func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1785 return library.wholeStaticMissingDeps
1786}
1787
Colin Crossc99deeb2016-04-11 15:06:20 -07001788func (library *libraryLinker) installable() bool {
1789 return !library.static()
1790}
1791
Colin Crossc7a38dc2016-07-12 13:13:09 -07001792func (library *libraryLinker) objs() android.Paths {
1793 return library.objFiles
1794}
1795
Colin Crossca860ac2016-01-04 14:34:37 -08001796type libraryInstaller struct {
1797 baseInstaller
1798
Colin Cross30d5f512016-05-03 18:02:42 -07001799 linker *libraryLinker
1800 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001801}
1802
Colin Cross635c3b02016-05-18 15:37:25 -07001803func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001804 if !library.linker.static() {
1805 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001806 }
1807}
1808
Colin Cross30d5f512016-05-03 18:02:42 -07001809func (library *libraryInstaller) inData() bool {
1810 return library.baseInstaller.inData() || library.sanitize.inData()
1811}
1812
Colin Cross635c3b02016-05-18 15:37:25 -07001813func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1814 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001815
Colin Crossca860ac2016-01-04 14:34:37 -08001816 linker := &libraryLinker{}
1817 linker.dynamicProperties.BuildShared = shared
1818 linker.dynamicProperties.BuildStatic = static
1819 module.linker = linker
1820
1821 module.compiler = &libraryCompiler{
1822 linker: linker,
1823 }
1824 module.installer = &libraryInstaller{
1825 baseInstaller: baseInstaller{
1826 dir: "lib",
1827 dir64: "lib64",
1828 },
Colin Cross30d5f512016-05-03 18:02:42 -07001829 linker: linker,
1830 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001831 }
1832
Colin Crossca860ac2016-01-04 14:34:37 -08001833 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001834}
1835
Colin Crossca860ac2016-01-04 14:34:37 -08001836func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001837 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001838 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001839}
1840
Colin Cross3f40fa42015-01-30 17:27:36 -08001841//
1842// Objects (for crt*.o)
1843//
1844
Colin Crossca860ac2016-01-04 14:34:37 -08001845type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001846 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001847}
1848
Colin Crossca860ac2016-01-04 14:34:37 -08001849func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001850 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001851 module.compiler = &baseCompiler{}
1852 module.linker = &objectLinker{}
1853 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001854}
1855
Colin Cross81413472016-04-11 14:37:39 -07001856func (object *objectLinker) props() []interface{} {
1857 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001858}
1859
Colin Crossca860ac2016-01-04 14:34:37 -08001860func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001861
Colin Cross81413472016-04-11 14:37:39 -07001862func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1863 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001864 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001865}
1866
Colin Crossca860ac2016-01-04 14:34:37 -08001867func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001868 if flags.Clang {
1869 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1870 } else {
1871 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1872 }
1873
Colin Crossca860ac2016-01-04 14:34:37 -08001874 return flags
1875}
1876
1877func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001878 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001879
Colin Cross97ba0732015-03-23 17:50:24 -07001880 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001881
Colin Cross635c3b02016-05-18 15:37:25 -07001882 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001883 if len(objFiles) == 1 {
1884 outputFile = objFiles[0]
1885 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001886 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001887 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001888 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001889 }
1890
Colin Cross3f40fa42015-01-30 17:27:36 -08001891 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001892 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001893}
1894
Colin Crossc99deeb2016-04-11 15:06:20 -07001895func (*objectLinker) installable() bool {
1896 return false
1897}
1898
Colin Cross3f40fa42015-01-30 17:27:36 -08001899//
1900// Executables
1901//
1902
Colin Crossca860ac2016-01-04 14:34:37 -08001903type binaryLinker struct {
1904 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001905 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001906
Colin Crossca860ac2016-01-04 14:34:37 -08001907 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001908
Colin Cross635c3b02016-05-18 15:37:25 -07001909 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001910}
1911
Colin Crossca860ac2016-01-04 14:34:37 -08001912var _ linker = (*binaryLinker)(nil)
1913
1914func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001915 return append(binary.baseLinker.props(),
1916 &binary.Properties,
1917 &binary.stripper.StripProperties)
1918
Colin Cross3f40fa42015-01-30 17:27:36 -08001919}
1920
Colin Crossca860ac2016-01-04 14:34:37 -08001921func (binary *binaryLinker) buildStatic() 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) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001926 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001927}
1928
Colin Crossca860ac2016-01-04 14:34:37 -08001929func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001930 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001931 if binary.Properties.Stem != "" {
1932 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001933 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001934
Colin Crossca860ac2016-01-04 14:34:37 -08001935 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001936}
1937
Colin Crossca860ac2016-01-04 14:34:37 -08001938func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1939 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001940 if ctx.Device() {
Colin Crossa89d2e12016-01-11 12:48:37 -08001941 if !Bool(binary.baseLinker.Properties.Nocrt) {
1942 if !ctx.sdk() {
1943 if binary.buildStatic() {
1944 deps.CrtBegin = "crtbegin_static"
1945 } else {
1946 deps.CrtBegin = "crtbegin_dynamic"
1947 }
1948 deps.CrtEnd = "crtend_android"
Dan Albertc3144b12015-04-28 18:17:56 -07001949 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001950 if binary.buildStatic() {
1951 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
1952 } else {
1953 if Bool(binary.Properties.Static_executable) {
1954 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
1955 } else {
1956 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
1957 }
1958 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
1959 }
Dan Albertc3144b12015-04-28 18:17:56 -07001960 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001961 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001962
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001963 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001964 if inList("libc++_static", deps.StaticLibs) {
1965 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001966 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001967 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1968 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1969 // move them to the beginning of deps.LateStaticLibs
1970 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001971 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001972 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001973 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001974 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001975 }
Colin Crossca860ac2016-01-04 14:34:37 -08001976
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001977 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001978 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1979 "from static libs or set static_executable: true")
1980 }
1981 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001982}
1983
Colin Crossc99deeb2016-04-11 15:06:20 -07001984func (*binaryLinker) installable() bool {
1985 return true
1986}
1987
Colin Cross16b23492016-01-06 14:41:07 -08001988func (binary *binaryLinker) isDependencyRoot() bool {
1989 return true
1990}
1991
Colin Cross635c3b02016-05-18 15:37:25 -07001992func NewBinary(hod android.HostOrDeviceSupported) *Module {
1993 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001994 module.compiler = &baseCompiler{}
1995 module.linker = &binaryLinker{}
1996 module.installer = &baseInstaller{
1997 dir: "bin",
1998 }
1999 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08002000}
2001
Colin Crossca860ac2016-01-04 14:34:37 -08002002func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002003 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002004 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002005}
2006
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002007func (binary *binaryLinker) begin(ctx BaseModuleContext) {
2008 binary.baseLinker.begin(ctx)
2009
2010 static := Bool(binary.Properties.Static_executable)
2011 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07002012 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002013 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
2014 static = true
2015 }
2016 } else {
2017 // Static executables are not supported on Darwin or Windows
2018 static = false
2019 }
Colin Cross0af4b842015-04-30 16:36:18 -07002020 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002021 if static {
2022 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08002023 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07002024 }
2025}
2026
Colin Crossca860ac2016-01-04 14:34:37 -08002027func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2028 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002029
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002030 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002031 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002032 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002033 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2034 }
2035 }
2036
2037 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2038 // all code is position independent, and then those warnings get promoted to
2039 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002040 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002041 flags.CFlags = append(flags.CFlags, "-fpie")
2042 }
Colin Cross97ba0732015-03-23 17:50:24 -07002043
Colin Crossf6566ed2015-03-24 11:13:38 -07002044 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002045 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002046 // Clang driver needs -static to create static executable.
2047 // However, bionic/linker uses -shared to overwrite.
2048 // Linker for x86 targets does not allow coexistance of -static and -shared,
2049 // so we add -static only if -shared is not used.
2050 if !inList("-shared", flags.LdFlags) {
2051 flags.LdFlags = append(flags.LdFlags, "-static")
2052 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002053
Colin Crossed4cf0b2015-03-26 14:43:45 -07002054 flags.LdFlags = append(flags.LdFlags,
2055 "-nostdlib",
2056 "-Bstatic",
2057 "-Wl,--gc-sections",
2058 )
2059
2060 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002061 if flags.DynamicLinker == "" {
2062 flags.DynamicLinker = "/system/bin/linker"
2063 if flags.Toolchain.Is64Bit() {
2064 flags.DynamicLinker += "64"
2065 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002066 }
2067
2068 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002069 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002070 "-nostdlib",
2071 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002072 "-Wl,--gc-sections",
2073 "-Wl,-z,nocopyreloc",
2074 )
2075 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002076 } else {
2077 if binary.staticBinary() {
2078 flags.LdFlags = append(flags.LdFlags, "-static")
2079 }
2080 if ctx.Darwin() {
2081 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2082 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002083 }
2084
Colin Cross97ba0732015-03-23 17:50:24 -07002085 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002086}
2087
Colin Crossca860ac2016-01-04 14:34:37 -08002088func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002089 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002090
Colin Cross665dce92016-04-28 14:50:03 -07002091 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002092 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002093 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002094 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002095 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002096 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002097
Colin Cross635c3b02016-05-18 15:37:25 -07002098 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002099
Colin Crossca860ac2016-01-04 14:34:37 -08002100 sharedLibs := deps.SharedLibs
2101 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2102
Colin Cross16b23492016-01-06 14:41:07 -08002103 if flags.DynamicLinker != "" {
2104 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2105 }
2106
Colin Cross665dce92016-04-28 14:50:03 -07002107 builderFlags := flagsToBuilderFlags(flags)
2108
2109 if binary.stripper.needsStrip(ctx) {
2110 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002111 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002112 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2113 }
2114
2115 if binary.Properties.Prefix_symbols != "" {
2116 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002117 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002118 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2119 flagsToBuilderFlags(flags), afterPrefixSymbols)
2120 }
2121
Colin Crossca860ac2016-01-04 14:34:37 -08002122 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002123 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002124 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002125
2126 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002127}
Colin Cross3f40fa42015-01-30 17:27:36 -08002128
Colin Cross635c3b02016-05-18 15:37:25 -07002129func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002130 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002131}
2132
Colin Cross665dce92016-04-28 14:50:03 -07002133type stripper struct {
2134 StripProperties StripProperties
2135}
2136
2137func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2138 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2139}
2140
Colin Cross635c3b02016-05-18 15:37:25 -07002141func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002142 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002143 if ctx.Darwin() {
2144 TransformDarwinStrip(ctx, in, out)
2145 } else {
2146 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2147 // TODO(ccross): don't add gnu debuglink for user builds
2148 flags.stripAddGnuDebuglink = true
2149 TransformStrip(ctx, in, out, flags)
2150 }
Colin Cross665dce92016-04-28 14:50:03 -07002151}
2152
Colin Cross635c3b02016-05-18 15:37:25 -07002153func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002154 if m, ok := mctx.Module().(*Module); ok {
Colin Crossc7a38dc2016-07-12 13:13:09 -07002155 if test, ok := m.linker.(*testBinaryLinker); ok {
2156 if Bool(test.testLinker.Properties.Test_per_src) {
Colin Crossca860ac2016-01-04 14:34:37 -08002157 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2158 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2159 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2160 }
2161 tests := mctx.CreateLocalVariations(testNames...)
2162 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2163 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002164 tests[i].(*Module).linker.(*testBinaryLinker).binaryLinker.Properties.Stem = testNames[i]
Colin Crossca860ac2016-01-04 14:34:37 -08002165 }
Colin Cross6002e052015-09-16 16:00:08 -07002166 }
2167 }
2168 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002169}
2170
Colin Crossca860ac2016-01-04 14:34:37 -08002171type testLinker struct {
Colin Crossca860ac2016-01-04 14:34:37 -08002172 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002173}
2174
Colin Crossca860ac2016-01-04 14:34:37 -08002175func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -08002176 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002177 return flags
2178 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002179
Colin Cross97ba0732015-03-23 17:50:24 -07002180 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002181 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002182 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002183
Colin Crossa1ad8d12016-06-01 17:09:44 -07002184 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002185 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002186 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002187 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002188 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2189 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002190 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002191 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2192 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002193 }
2194 } else {
2195 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002196 }
2197
Colin Cross21b9a242015-03-24 14:15:58 -07002198 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002199}
2200
Colin Crossca860ac2016-01-04 14:34:37 -08002201func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2202 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002203 if ctx.sdk() && ctx.Device() {
2204 switch ctx.selectedStl() {
2205 case "ndk_libc++_shared", "ndk_libc++_static":
2206 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2207 case "ndk_libgnustl_static":
2208 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2209 default:
2210 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2211 }
2212 } else {
2213 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2214 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002215 }
Colin Crossc7a38dc2016-07-12 13:13:09 -07002216 return deps
2217}
2218
2219type testBinaryLinker struct {
2220 testLinker
2221 binaryLinker
2222}
2223
2224func (test *testBinaryLinker) begin(ctx BaseModuleContext) {
2225 test.binaryLinker.begin(ctx)
2226 runpath := "../../lib"
2227 if ctx.toolchain().Is64Bit() {
2228 runpath += "64"
2229 }
2230 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
2231}
2232
2233func (test *testBinaryLinker) props() []interface{} {
2234 return append(test.binaryLinker.props(), &test.testLinker.Properties)
2235}
2236
2237func (test *testBinaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2238 flags = test.binaryLinker.flags(ctx, flags)
2239 flags = test.testLinker.flags(ctx, flags)
2240 return flags
2241}
2242
2243func (test *testBinaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2244 deps = test.testLinker.deps(ctx, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08002245 deps = test.binaryLinker.deps(ctx, deps)
2246 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002247}
2248
Colin Crossc7a38dc2016-07-12 13:13:09 -07002249type testLibraryLinker struct {
2250 testLinker
2251 *libraryLinker
2252}
2253
2254func (test *testLibraryLinker) props() []interface{} {
2255 return append(test.libraryLinker.props(), &test.testLinker.Properties)
2256}
2257
2258func (test *testLibraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2259 flags = test.libraryLinker.flags(ctx, flags)
2260 flags = test.testLinker.flags(ctx, flags)
2261 return flags
2262}
2263
2264func (test *testLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2265 deps = test.testLinker.deps(ctx, deps)
2266 deps = test.libraryLinker.deps(ctx, deps)
2267 return deps
2268}
2269
Colin Crossca860ac2016-01-04 14:34:37 -08002270type testInstaller struct {
2271 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002272}
2273
Colin Cross635c3b02016-05-18 15:37:25 -07002274func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002275 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2276 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2277 installer.baseInstaller.install(ctx, file)
2278}
2279
Colin Cross635c3b02016-05-18 15:37:25 -07002280func NewTest(hod android.HostOrDeviceSupported) *Module {
2281 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002282 module.compiler = &baseCompiler{}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002283 linker := &testBinaryLinker{}
2284 linker.testLinker.Properties.Gtest = true
Colin Crossca860ac2016-01-04 14:34:37 -08002285 module.linker = linker
2286 module.installer = &testInstaller{
2287 baseInstaller: baseInstaller{
2288 dir: "nativetest",
2289 dir64: "nativetest64",
2290 data: true,
2291 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002292 }
Colin Crossca860ac2016-01-04 14:34:37 -08002293 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002294}
2295
Colin Crossca860ac2016-01-04 14:34:37 -08002296func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002297 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002298 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002299}
2300
Colin Crossc7a38dc2016-07-12 13:13:09 -07002301func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
2302 module := NewLibrary(android.HostAndDeviceSupported, false, true)
2303 linker := &testLibraryLinker{
2304 libraryLinker: module.linker.(*libraryLinker),
2305 }
2306 linker.testLinker.Properties.Gtest = true
2307 module.linker = linker
2308 module.installer = &testInstaller{
2309 baseInstaller: baseInstaller{
2310 dir: "nativetest",
2311 dir64: "nativetest64",
2312 data: true,
2313 },
2314 }
2315 return module
2316}
2317
2318func testLibraryFactory() (blueprint.Module, []interface{}) {
2319 module := NewTestLibrary(android.HostAndDeviceSupported)
2320 return module.Init()
2321}
2322
Colin Crossca860ac2016-01-04 14:34:37 -08002323type benchmarkLinker struct {
Colin Crossaa3bf372016-07-14 10:27:10 -07002324 testBinaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002325}
2326
Colin Crossca860ac2016-01-04 14:34:37 -08002327func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossaa3bf372016-07-14 10:27:10 -07002328 deps = benchmark.testBinaryLinker.deps(ctx, deps)
Colin Cross26832742016-07-11 14:57:56 -07002329 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
Colin Crossca860ac2016-01-04 14:34:37 -08002330 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002331}
2332
Colin Cross635c3b02016-05-18 15:37:25 -07002333func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2334 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002335 module.compiler = &baseCompiler{}
2336 module.linker = &benchmarkLinker{}
Colin Cross624b8ed2016-07-11 17:20:09 -07002337 module.installer = &testInstaller{
2338 baseInstaller: baseInstaller{
2339 dir: "nativetest",
2340 dir64: "nativetest64",
2341 data: true,
2342 },
Colin Cross2ba19d92015-05-07 15:44:20 -07002343 }
Colin Crossca860ac2016-01-04 14:34:37 -08002344 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002345}
2346
Colin Crossca860ac2016-01-04 14:34:37 -08002347func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002348 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002349 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002350}
2351
Colin Cross3f40fa42015-01-30 17:27:36 -08002352//
2353// Static library
2354//
2355
Colin Crossca860ac2016-01-04 14:34:37 -08002356func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002357 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002358 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002359}
2360
2361//
2362// Shared libraries
2363//
2364
Colin Crossca860ac2016-01-04 14:34:37 -08002365func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002366 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002367 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002368}
2369
2370//
2371// Host static library
2372//
2373
Colin Crossca860ac2016-01-04 14:34:37 -08002374func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002375 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002376 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002377}
2378
2379//
2380// Host Shared libraries
2381//
2382
Colin Crossca860ac2016-01-04 14:34:37 -08002383func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002384 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002385 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002386}
2387
2388//
2389// Host Binaries
2390//
2391
Colin Crossca860ac2016-01-04 14:34:37 -08002392func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002393 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002394 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002395}
2396
2397//
Colin Cross1f8f2342015-03-26 16:09:47 -07002398// Host Tests
2399//
2400
Colin Crossca860ac2016-01-04 14:34:37 -08002401func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002402 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002403 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002404}
2405
2406//
Colin Cross2ba19d92015-05-07 15:44:20 -07002407// Host Benchmarks
2408//
2409
Colin Crossca860ac2016-01-04 14:34:37 -08002410func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002411 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002412 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002413}
2414
2415//
Colin Crosscfad1192015-11-02 16:43:11 -08002416// Defaults
2417//
Colin Crossca860ac2016-01-04 14:34:37 -08002418type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002419 android.ModuleBase
2420 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002421}
2422
Colin Cross635c3b02016-05-18 15:37:25 -07002423func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002424}
2425
Colin Crossca860ac2016-01-04 14:34:37 -08002426func defaultsFactory() (blueprint.Module, []interface{}) {
2427 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002428
2429 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002430 &BaseProperties{},
2431 &BaseCompilerProperties{},
2432 &BaseLinkerProperties{},
2433 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002434 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002435 &LibraryLinkerProperties{},
2436 &BinaryLinkerProperties{},
2437 &TestLinkerProperties{},
2438 &UnusedProperties{},
2439 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002440 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002441 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002442 }
2443
Colin Cross635c3b02016-05-18 15:37:25 -07002444 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2445 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002446
Colin Cross635c3b02016-05-18 15:37:25 -07002447 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002448}
2449
2450//
Colin Cross3f40fa42015-01-30 17:27:36 -08002451// Device libraries shipped with gcc
2452//
2453
Colin Crossca860ac2016-01-04 14:34:37 -08002454type toolchainLibraryLinker struct {
2455 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002456}
2457
Colin Crossca860ac2016-01-04 14:34:37 -08002458var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2459
2460func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002461 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002462 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002463}
2464
Colin Crossca860ac2016-01-04 14:34:37 -08002465func (*toolchainLibraryLinker) buildStatic() bool {
2466 return true
2467}
Colin Cross3f40fa42015-01-30 17:27:36 -08002468
Colin Crossca860ac2016-01-04 14:34:37 -08002469func (*toolchainLibraryLinker) buildShared() bool {
2470 return false
2471}
2472
2473func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002474 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002475 module.compiler = &baseCompiler{}
2476 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002477 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002478 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002479}
2480
Colin Crossca860ac2016-01-04 14:34:37 -08002481func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002482 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002483
2484 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002485 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002486
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002487 if flags.Clang {
2488 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2489 }
2490
Colin Crossca860ac2016-01-04 14:34:37 -08002491 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002492
2493 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002494
Colin Crossca860ac2016-01-04 14:34:37 -08002495 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002496}
2497
Colin Crossc99deeb2016-04-11 15:06:20 -07002498func (*toolchainLibraryLinker) installable() bool {
2499 return false
2500}
2501
Dan Albertbe961682015-03-18 23:38:50 -07002502// NDK prebuilt libraries.
2503//
2504// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2505// either (with the exception of the shared STLs, which are installed to the app's directory rather
2506// than to the system image).
2507
Colin Cross635c3b02016-05-18 15:37:25 -07002508func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002509 suffix := ""
2510 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2511 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002512 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002513 suffix = "64"
2514 }
Colin Cross635c3b02016-05-18 15:37:25 -07002515 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002516 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002517}
2518
Colin Cross635c3b02016-05-18 15:37:25 -07002519func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2520 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002521
2522 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2523 // We want to translate to just NAME.EXT
2524 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2525 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002526 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002527}
2528
Colin Crossca860ac2016-01-04 14:34:37 -08002529type ndkPrebuiltObjectLinker struct {
2530 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002531}
2532
Colin Crossca860ac2016-01-04 14:34:37 -08002533func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002534 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002535 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002536}
2537
Colin Crossca860ac2016-01-04 14:34:37 -08002538func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002539 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002540 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002541 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002542 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002543}
2544
Colin Crossca860ac2016-01-04 14:34:37 -08002545func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002546 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002547 // A null build step, but it sets up the output path.
2548 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2549 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2550 }
2551
Colin Crossca860ac2016-01-04 14:34:37 -08002552 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002553}
2554
Colin Crossca860ac2016-01-04 14:34:37 -08002555type ndkPrebuiltLibraryLinker struct {
2556 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002557}
2558
Colin Crossca860ac2016-01-04 14:34:37 -08002559var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2560var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002561
Colin Crossca860ac2016-01-04 14:34:37 -08002562func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002563 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002564}
2565
Colin Crossca860ac2016-01-04 14:34:37 -08002566func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002567 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002568 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002569}
2570
Colin Crossca860ac2016-01-04 14:34:37 -08002571func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002572 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002573 linker := &ndkPrebuiltLibraryLinker{}
2574 linker.dynamicProperties.BuildShared = true
2575 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002576 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002577 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002578}
2579
Colin Crossca860ac2016-01-04 14:34:37 -08002580func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002581 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002582 // A null build step, but it sets up the output path.
2583 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2584 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2585 }
2586
Colin Cross919281a2016-04-05 16:42:05 -07002587 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002588
Colin Crossca860ac2016-01-04 14:34:37 -08002589 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2590 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002591}
2592
2593// The NDK STLs are slightly different from the prebuilt system libraries:
2594// * Are not specific to each platform version.
2595// * The libraries are not in a predictable location for each STL.
2596
Colin Crossca860ac2016-01-04 14:34:37 -08002597type ndkPrebuiltStlLinker struct {
2598 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002599}
2600
Colin Crossca860ac2016-01-04 14:34:37 -08002601func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002602 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002603 linker := &ndkPrebuiltStlLinker{}
2604 linker.dynamicProperties.BuildShared = true
2605 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002606 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002607 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002608}
2609
Colin Crossca860ac2016-01-04 14:34:37 -08002610func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002611 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002612 linker := &ndkPrebuiltStlLinker{}
2613 linker.dynamicProperties.BuildStatic = true
2614 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002615 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002616 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002617}
2618
Colin Cross635c3b02016-05-18 15:37:25 -07002619func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002620 gccVersion := toolchain.GccVersion()
2621 var libDir string
2622 switch stl {
2623 case "libstlport":
2624 libDir = "cxx-stl/stlport/libs"
2625 case "libc++":
2626 libDir = "cxx-stl/llvm-libc++/libs"
2627 case "libgnustl":
2628 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2629 }
2630
2631 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002632 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002633 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002634 }
2635
2636 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002637 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002638}
2639
Colin Crossca860ac2016-01-04 14:34:37 -08002640func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002641 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002642 // A null build step, but it sets up the output path.
2643 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2644 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2645 }
2646
Colin Cross919281a2016-04-05 16:42:05 -07002647 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002648
2649 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002650 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002651 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002652 libExt = staticLibraryExtension
2653 }
2654
2655 stlName := strings.TrimSuffix(libName, "_shared")
2656 stlName = strings.TrimSuffix(stlName, "_static")
2657 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002658 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002659}
2660
Colin Cross635c3b02016-05-18 15:37:25 -07002661func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002662 if m, ok := mctx.Module().(*Module); ok {
2663 if m.linker != nil {
2664 if linker, ok := m.linker.(baseLinkerInterface); ok {
2665 var modules []blueprint.Module
2666 if linker.buildStatic() && linker.buildShared() {
2667 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002668 static := modules[0].(*Module)
2669 shared := modules[1].(*Module)
2670
2671 static.linker.(baseLinkerInterface).setStatic(true)
2672 shared.linker.(baseLinkerInterface).setStatic(false)
2673
2674 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2675 sharedCompiler := shared.compiler.(*libraryCompiler)
2676 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2677 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2678 // Optimize out compiling common .o files twice for static+shared libraries
2679 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2680 sharedCompiler.baseCompiler.Properties.Srcs = nil
2681 }
2682 }
Colin Crossca860ac2016-01-04 14:34:37 -08002683 } else if linker.buildStatic() {
2684 modules = mctx.CreateLocalVariations("static")
2685 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2686 } else if linker.buildShared() {
2687 modules = mctx.CreateLocalVariations("shared")
2688 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2689 } else {
2690 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2691 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002692 }
2693 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002694 }
2695}
Colin Cross74d1ec02015-04-28 13:30:13 -07002696
2697// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2698// modifies the slice contents in place, and returns a subslice of the original slice
2699func lastUniqueElements(list []string) []string {
2700 totalSkip := 0
2701 for i := len(list) - 1; i >= totalSkip; i-- {
2702 skip := 0
2703 for j := i - 1; j >= totalSkip; j-- {
2704 if list[i] == list[j] {
2705 skip++
2706 } else {
2707 list[j+skip] = list[j]
2708 }
2709 }
2710 totalSkip += skip
2711 }
2712 return list[totalSkip:]
2713}
Colin Cross06a931b2015-10-28 17:23:31 -07002714
2715var Bool = proptools.Bool