blob: 4e093ec587535c6b752b22395accb38a44441fc4 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080035 soong.RegisterModuleType("cc_library_static", libraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", librarySharedFactory)
37 soong.RegisterModuleType("cc_library", libraryFactory)
38 soong.RegisterModuleType("cc_object", objectFactory)
39 soong.RegisterModuleType("cc_binary", binaryFactory)
40 soong.RegisterModuleType("cc_test", testFactory)
41 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
42 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Crossca860ac2016-01-04 14:34:37 -080044 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070049
Colin Crossca860ac2016-01-04 14:34:37 -080050 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", testHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070055
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070059 android.RegisterBottomUpMutator("link", linkageMutator)
60 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080062
Colin Cross635c3b02016-05-18 15:37:25 -070063 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
64 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080065
Colin Cross635c3b02016-05-18 15:37:25 -070066 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
67 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Cross463a90e2015-06-17 14:20:06 -070068}
69
Colin Cross3f40fa42015-01-30 17:27:36 -080070var (
Colin Cross635c3b02016-05-18 15:37:25 -070071 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080072
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
Colin Cross3f40fa42015-01-30 17:27:36 -080074)
75
76// Flags used by lots of devices. Putting them in package static variables will save bytes in
77// build.ninja so they aren't repeated for every file
78var (
79 commonGlobalCflags = []string{
80 "-DANDROID",
81 "-fmessage-length=0",
82 "-W",
83 "-Wall",
84 "-Wno-unused",
85 "-Winit-self",
86 "-Wpointer-arith",
87
88 // COMMON_RELEASE_CFLAGS
89 "-DNDEBUG",
90 "-UDEBUG",
91 }
92
93 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080094 "-fdiagnostics-color",
95
Colin Cross3f40fa42015-01-30 17:27:36 -080096 // TARGET_ERROR_FLAGS
97 "-Werror=return-type",
98 "-Werror=non-virtual-dtor",
99 "-Werror=address",
100 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -0800101 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -0800102 }
103
104 hostGlobalCflags = []string{}
105
106 commonGlobalCppflags = []string{
107 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700108 }
109
Dan Willemsenbe03f342016-03-03 17:21:04 -0800110 noOverrideGlobalCflags = []string{
111 "-Werror=int-to-pointer-cast",
112 "-Werror=pointer-to-int-cast",
113 }
114
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700115 illegalFlags = []string{
116 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800117 }
Dan Willemsen97704ed2016-07-07 21:40:39 -0700118
119 ndkPrebuiltSharedLibs = []string{
120 "android",
121 "c",
122 "dl",
123 "EGL",
124 "GLESv1_CM",
125 "GLESv2",
126 "GLESv3",
127 "jnigraphics",
128 "log",
129 "mediandk",
130 "m",
131 "OpenMAXAL",
132 "OpenSLES",
133 "stdc++",
134 "vulkan",
135 "z",
136 }
137 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800138)
139
140func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700141 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700142 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
143 }
144
Colin Cross3f40fa42015-01-30 17:27:36 -0800145 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
146 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
147 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800148 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800149
150 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
151
152 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800153 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800154 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800155 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800156 pctx.StaticVariable("hostClangGlobalCflags",
157 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800158 pctx.StaticVariable("noOverrideClangGlobalCflags",
159 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
160
Tim Kilbournf2948142015-03-11 12:03:03 -0700161 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800162 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
164 // Everything in this list is a crime against abstraction and dependency tracking.
165 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800166 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167 []string{
168 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800169 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 "hardware/libhardware/include",
171 "hardware/libhardware_legacy/include",
172 "hardware/ril/include",
173 "libnativehelper/include",
174 "frameworks/native/include",
175 "frameworks/native/opengl/include",
176 "frameworks/av/include",
177 "frameworks/base/include",
178 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800179 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
180 // with this, since there is no associated library.
181 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
182 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800183
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700184 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
185 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700186 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700187 return override, nil
188 }
189 return "${clangDefaultBase}", nil
190 })
191 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700192 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700193 return override, nil
194 }
Stephen Hines369f0132016-04-26 14:34:07 -0700195 return "clang-2812033", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700196 })
Colin Cross16b23492016-01-06 14:41:07 -0800197 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
198 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800199}
200
Colin Crossca860ac2016-01-04 14:34:37 -0800201type Deps struct {
202 SharedLibs, LateSharedLibs []string
203 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700204
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700205 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
206
Colin Cross81413472016-04-11 14:37:39 -0700207 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208
Dan Willemsenb40aab62016-04-20 14:21:14 -0700209 GeneratedSources []string
210 GeneratedHeaders []string
211
Colin Cross97ba0732015-03-23 17:50:24 -0700212 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700213}
214
Colin Crossca860ac2016-01-04 14:34:37 -0800215type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700216 SharedLibs, LateSharedLibs android.Paths
217 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700218
Colin Cross635c3b02016-05-18 15:37:25 -0700219 ObjFiles android.Paths
220 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700221
Colin Cross635c3b02016-05-18 15:37:25 -0700222 GeneratedSources android.Paths
223 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700224
Dan Willemsen76f08272016-07-09 00:14:08 -0700225 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226
Colin Cross635c3b02016-05-18 15:37:25 -0700227 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700228}
229
Colin Crossca860ac2016-01-04 14:34:37 -0800230type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700231 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
232 AsFlags []string // Flags that apply to assembly source files
233 CFlags []string // Flags that apply to C and C++ source files
234 ConlyFlags []string // Flags that apply to C source files
235 CppFlags []string // Flags that apply to C++ source files
236 YaccFlags []string // Flags that apply to Yacc source files
237 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800238 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700239
240 Nocrt bool
241 Toolchain Toolchain
242 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800243
244 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800245 DynamicLinker string
246
Colin Cross635c3b02016-05-18 15:37:25 -0700247 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700248}
249
Colin Crossca860ac2016-01-04 14:34:37 -0800250type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700251 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700252 Srcs []string `android:"arch_variant"`
253
254 // list of source files that should not be used to build the C/C++ module.
255 // This is most useful in the arch/multilib variants to remove non-common files
256 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700257
258 // list of module-specific flags that will be used for C and C++ compiles.
259 Cflags []string `android:"arch_variant"`
260
261 // list of module-specific flags that will be used for C++ compiles
262 Cppflags []string `android:"arch_variant"`
263
264 // list of module-specific flags that will be used for C compiles
265 Conlyflags []string `android:"arch_variant"`
266
267 // list of module-specific flags that will be used for .S compiles
268 Asflags []string `android:"arch_variant"`
269
Colin Crossca860ac2016-01-04 14:34:37 -0800270 // list of module-specific flags that will be used for C and C++ compiles when
271 // compiling with clang
272 Clang_cflags []string `android:"arch_variant"`
273
274 // list of module-specific flags that will be used for .S compiles when
275 // compiling with clang
276 Clang_asflags []string `android:"arch_variant"`
277
Colin Cross7d5136f2015-05-11 13:39:40 -0700278 // list of module-specific flags that will be used for .y and .yy compiles
279 Yaccflags []string
280
Colin Cross7d5136f2015-05-11 13:39:40 -0700281 // the instruction set architecture to use to compile the C/C++
282 // module.
283 Instruction_set string `android:"arch_variant"`
284
285 // list of directories relative to the root of the source tree that will
286 // be added to the include path using -I.
287 // If possible, don't use this. If adding paths from the current directory use
288 // local_include_dirs, if adding paths from other modules use export_include_dirs in
289 // that module.
290 Include_dirs []string `android:"arch_variant"`
291
292 // list of directories relative to the Blueprints file that will
293 // be added to the include path using -I
294 Local_include_dirs []string `android:"arch_variant"`
295
Dan Willemsenb40aab62016-04-20 14:21:14 -0700296 // list of generated sources to compile. These are the names of gensrcs or
297 // genrule modules.
298 Generated_sources []string `android:"arch_variant"`
299
300 // list of generated headers to add to the include path. These are the names
301 // of genrule modules.
302 Generated_headers []string `android:"arch_variant"`
303
Colin Crossca860ac2016-01-04 14:34:37 -0800304 // pass -frtti instead of -fno-rtti
305 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700306
Colin Crossca860ac2016-01-04 14:34:37 -0800307 Debug, Release struct {
308 // list of module-specific flags that will be used for C and C++ compiles in debug or
309 // release builds
310 Cflags []string `android:"arch_variant"`
311 } `android:"arch_variant"`
312}
Colin Cross7d5136f2015-05-11 13:39:40 -0700313
Colin Crossca860ac2016-01-04 14:34:37 -0800314type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700315 // list of modules whose object files should be linked into this module
316 // in their entirety. For static library modules, all of the .o files from the intermediate
317 // directory of the dependency will be linked into this modules .a file. For a shared library,
318 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700319 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700320
321 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700322 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700323
324 // list of modules that should be dynamically linked into this module.
325 Shared_libs []string `android:"arch_variant"`
326
Colin Crossca860ac2016-01-04 14:34:37 -0800327 // list of module-specific flags that will be used for all link steps
328 Ldflags []string `android:"arch_variant"`
329
330 // don't insert default compiler flags into asflags, cflags,
331 // cppflags, conlyflags, ldflags, or include_dirs
332 No_default_compiler_flags *bool
333
334 // list of system libraries that will be dynamically linked to
335 // shared library and executable modules. If unset, generally defaults to libc
336 // and libm. Set to [] to prevent linking against libc and libm.
337 System_shared_libs []string
338
Colin Cross7d5136f2015-05-11 13:39:40 -0700339 // allow the module to contain undefined symbols. By default,
340 // modules cannot contain undefined symbols that are not satisified by their immediate
341 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
342 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700343 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700344
Dan Willemsend67be222015-09-16 15:19:33 -0700345 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700346 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700347
Colin Cross7d5136f2015-05-11 13:39:40 -0700348 // -l arguments to pass to linker for host-provided shared libraries
349 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700350
351 // list of shared libraries to re-export include directories from. Entries must be
352 // present in shared_libs.
353 Export_shared_lib_headers []string `android:"arch_variant"`
354
355 // list of static libraries to re-export include directories from. Entries must be
356 // present in static_libs.
357 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800358}
Colin Cross7d5136f2015-05-11 13:39:40 -0700359
Colin Crossca860ac2016-01-04 14:34:37 -0800360type LibraryCompilerProperties struct {
361 Static struct {
362 Srcs []string `android:"arch_variant"`
363 Exclude_srcs []string `android:"arch_variant"`
364 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700365 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800366 Shared struct {
367 Srcs []string `android:"arch_variant"`
368 Exclude_srcs []string `android:"arch_variant"`
369 Cflags []string `android:"arch_variant"`
370 } `android:"arch_variant"`
371}
372
Colin Cross919281a2016-04-05 16:42:05 -0700373type FlagExporterProperties struct {
374 // list of directories relative to the Blueprints file that will
375 // be added to the include path using -I for any module that links against this module
376 Export_include_dirs []string `android:"arch_variant"`
377}
378
Colin Crossca860ac2016-01-04 14:34:37 -0800379type LibraryLinkerProperties struct {
380 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700381 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800382 Whole_static_libs []string `android:"arch_variant"`
383 Static_libs []string `android:"arch_variant"`
384 Shared_libs []string `android:"arch_variant"`
385 } `android:"arch_variant"`
386 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700387 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800388 Whole_static_libs []string `android:"arch_variant"`
389 Static_libs []string `android:"arch_variant"`
390 Shared_libs []string `android:"arch_variant"`
391 } `android:"arch_variant"`
392
393 // local file name to pass to the linker as --version_script
394 Version_script *string `android:"arch_variant"`
395 // local file name to pass to the linker as -unexported_symbols_list
396 Unexported_symbols_list *string `android:"arch_variant"`
397 // local file name to pass to the linker as -force_symbols_not_weak_list
398 Force_symbols_not_weak_list *string `android:"arch_variant"`
399 // local file name to pass to the linker as -force_symbols_weak_list
400 Force_symbols_weak_list *string `android:"arch_variant"`
401
Colin Crossca860ac2016-01-04 14:34:37 -0800402 // don't link in crt_begin and crt_end. This flag should only be necessary for
403 // compiling crt or libc.
404 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800405
406 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800407}
408
409type BinaryLinkerProperties struct {
410 // compile executable with -static
411 Static_executable *bool
412
413 // set the name of the output
414 Stem string `android:"arch_variant"`
415
416 // append to the name of the output
417 Suffix string `android:"arch_variant"`
418
419 // if set, add an extra objcopy --prefix-symbols= step
420 Prefix_symbols string
421}
422
423type TestLinkerProperties struct {
424 // if set, build against the gtest library. Defaults to true.
425 Gtest bool
426
427 // Create a separate binary for each source file. Useful when there is
428 // global state that can not be torn down and reset between each test suite.
429 Test_per_src *bool
430}
431
Colin Cross81413472016-04-11 14:37:39 -0700432type ObjectLinkerProperties struct {
433 // names of other cc_object modules to link into this module using partial linking
434 Objs []string `android:"arch_variant"`
435}
436
Colin Crossca860ac2016-01-04 14:34:37 -0800437// Properties used to compile all C or C++ modules
438type BaseProperties struct {
439 // compile module with clang instead of gcc
440 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700441
442 // Minimum sdk version supported when compiling against the ndk
443 Sdk_version string
444
Colin Crossca860ac2016-01-04 14:34:37 -0800445 // don't insert default compiler flags into asflags, cflags,
446 // cppflags, conlyflags, ldflags, or include_dirs
447 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700448
449 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700450 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800451}
452
453type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700454 // install to a subdirectory of the default install path for the module
455 Relative_install_path string
456}
457
Colin Cross665dce92016-04-28 14:50:03 -0700458type StripProperties struct {
459 Strip struct {
460 None bool
461 Keep_symbols bool
462 }
463}
464
Colin Crossca860ac2016-01-04 14:34:37 -0800465type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700466 Native_coverage *bool
467 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700468 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800469}
470
Colin Crossca860ac2016-01-04 14:34:37 -0800471type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800472 static() bool
473 staticBinary() bool
474 clang() bool
475 toolchain() Toolchain
476 noDefaultCompilerFlags() bool
477 sdk() bool
478 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700479 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800480}
481
482type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700483 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800484 ModuleContextIntf
485}
486
487type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700488 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800489 ModuleContextIntf
490}
491
492type Customizer interface {
493 CustomizeProperties(BaseModuleContext)
494 Properties() []interface{}
495}
496
497type feature interface {
498 begin(ctx BaseModuleContext)
499 deps(ctx BaseModuleContext, deps Deps) Deps
500 flags(ctx ModuleContext, flags Flags) Flags
501 props() []interface{}
502}
503
504type compiler interface {
505 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700506 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800507}
508
509type linker interface {
510 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700511 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700512 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800513}
514
515type installer interface {
516 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700517 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800518 inData() bool
519}
520
Colin Crossc99deeb2016-04-11 15:06:20 -0700521type dependencyTag struct {
522 blueprint.BaseDependencyTag
523 name string
524 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700525
526 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700527}
528
529var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700530 sharedDepTag = dependencyTag{name: "shared", library: true}
531 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
532 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
533 staticDepTag = dependencyTag{name: "static", library: true}
534 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
535 lateStaticDepTag = dependencyTag{name: "late static", library: true}
536 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
537 genSourceDepTag = dependencyTag{name: "gen source"}
538 genHeaderDepTag = dependencyTag{name: "gen header"}
539 objDepTag = dependencyTag{name: "obj"}
540 crtBeginDepTag = dependencyTag{name: "crtbegin"}
541 crtEndDepTag = dependencyTag{name: "crtend"}
542 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700543)
544
Colin Crossca860ac2016-01-04 14:34:37 -0800545// Module contains the properties and members used by all C/C++ module types, and implements
546// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
547// to construct the output file. Behavior can be customized with a Customizer interface
548type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700549 android.ModuleBase
550 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700551
Colin Crossca860ac2016-01-04 14:34:37 -0800552 Properties BaseProperties
553 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700554
Colin Crossca860ac2016-01-04 14:34:37 -0800555 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700556 hod android.HostOrDeviceSupported
557 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700558
Colin Crossca860ac2016-01-04 14:34:37 -0800559 // delegates, initialize before calling Init
560 customizer Customizer
561 features []feature
562 compiler compiler
563 linker linker
564 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700565 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800566 sanitize *sanitize
567
568 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700569
Colin Cross635c3b02016-05-18 15:37:25 -0700570 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800571
572 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700573}
574
Colin Crossca860ac2016-01-04 14:34:37 -0800575func (c *Module) Init() (blueprint.Module, []interface{}) {
576 props := []interface{}{&c.Properties, &c.unused}
577 if c.customizer != nil {
578 props = append(props, c.customizer.Properties()...)
579 }
580 if c.compiler != nil {
581 props = append(props, c.compiler.props()...)
582 }
583 if c.linker != nil {
584 props = append(props, c.linker.props()...)
585 }
586 if c.installer != nil {
587 props = append(props, c.installer.props()...)
588 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700589 if c.stl != nil {
590 props = append(props, c.stl.props()...)
591 }
Colin Cross16b23492016-01-06 14:41:07 -0800592 if c.sanitize != nil {
593 props = append(props, c.sanitize.props()...)
594 }
Colin Crossca860ac2016-01-04 14:34:37 -0800595 for _, feature := range c.features {
596 props = append(props, feature.props()...)
597 }
Colin Crossc472d572015-03-17 15:06:21 -0700598
Colin Cross635c3b02016-05-18 15:37:25 -0700599 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700600
Colin Cross635c3b02016-05-18 15:37:25 -0700601 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700602}
603
Colin Crossca860ac2016-01-04 14:34:37 -0800604type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700605 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800606 moduleContextImpl
607}
608
609type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700610 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800611 moduleContextImpl
612}
613
614type moduleContextImpl struct {
615 mod *Module
616 ctx BaseModuleContext
617}
618
Colin Crossca860ac2016-01-04 14:34:37 -0800619func (ctx *moduleContextImpl) clang() bool {
620 return ctx.mod.clang(ctx.ctx)
621}
622
623func (ctx *moduleContextImpl) toolchain() Toolchain {
624 return ctx.mod.toolchain(ctx.ctx)
625}
626
627func (ctx *moduleContextImpl) static() bool {
628 if ctx.mod.linker == nil {
629 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
630 }
631 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
632 return linker.static()
633 } else {
634 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
635 }
636}
637
638func (ctx *moduleContextImpl) staticBinary() bool {
639 if ctx.mod.linker == nil {
640 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
641 }
642 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
643 return linker.staticBinary()
644 } else {
645 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
646 }
647}
648
649func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
650 return Bool(ctx.mod.Properties.No_default_compiler_flags)
651}
652
653func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700654 if ctx.ctx.Device() {
655 return ctx.mod.Properties.Sdk_version != ""
656 }
657 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800658}
659
660func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700661 if ctx.ctx.Device() {
662 return ctx.mod.Properties.Sdk_version
663 }
664 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800665}
666
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700667func (ctx *moduleContextImpl) selectedStl() string {
668 if stl := ctx.mod.stl; stl != nil {
669 return stl.Properties.SelectedStl
670 }
671 return ""
672}
673
Colin Cross635c3b02016-05-18 15:37:25 -0700674func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800675 return &Module{
676 hod: hod,
677 multilib: multilib,
678 }
679}
680
Colin Cross635c3b02016-05-18 15:37:25 -0700681func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800682 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700683 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800684 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800685 return module
686}
687
Colin Cross635c3b02016-05-18 15:37:25 -0700688func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800689 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700690 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800691 moduleContextImpl: moduleContextImpl{
692 mod: c,
693 },
694 }
695 ctx.ctx = ctx
696
697 flags := Flags{
698 Toolchain: c.toolchain(ctx),
699 Clang: c.clang(ctx),
700 }
Colin Crossca860ac2016-01-04 14:34:37 -0800701 if c.compiler != nil {
702 flags = c.compiler.flags(ctx, flags)
703 }
704 if c.linker != nil {
705 flags = c.linker.flags(ctx, flags)
706 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700707 if c.stl != nil {
708 flags = c.stl.flags(ctx, flags)
709 }
Colin Cross16b23492016-01-06 14:41:07 -0800710 if c.sanitize != nil {
711 flags = c.sanitize.flags(ctx, flags)
712 }
Colin Crossca860ac2016-01-04 14:34:37 -0800713 for _, feature := range c.features {
714 flags = feature.flags(ctx, flags)
715 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800716 if ctx.Failed() {
717 return
718 }
719
Colin Crossca860ac2016-01-04 14:34:37 -0800720 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
721 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
722 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800723
Colin Crossca860ac2016-01-04 14:34:37 -0800724 // Optimization to reduce size of build.ninja
725 // Replace the long list of flags for each file with a module-local variable
726 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
727 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
728 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
729 flags.CFlags = []string{"$cflags"}
730 flags.CppFlags = []string{"$cppflags"}
731 flags.AsFlags = []string{"$asflags"}
732
Colin Crossc99deeb2016-04-11 15:06:20 -0700733 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800734 if ctx.Failed() {
735 return
736 }
737
Dan Willemsen76f08272016-07-09 00:14:08 -0700738 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700739
Colin Cross635c3b02016-05-18 15:37:25 -0700740 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800741 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700742 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800743 if ctx.Failed() {
744 return
745 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 }
747
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if c.linker != nil {
749 outputFile := c.linker.link(ctx, flags, deps, objFiles)
750 if ctx.Failed() {
751 return
752 }
Colin Cross635c3b02016-05-18 15:37:25 -0700753 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700754
Colin Crossc99deeb2016-04-11 15:06:20 -0700755 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800756 c.installer.install(ctx, outputFile)
757 if ctx.Failed() {
758 return
759 }
760 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700761 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800762}
763
Colin Crossca860ac2016-01-04 14:34:37 -0800764func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
765 if c.cachedToolchain == nil {
766 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700767 os := ctx.Os()
768 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800769 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700770 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800771 return nil
772 }
773 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 }
Colin Crossca860ac2016-01-04 14:34:37 -0800775 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800776}
777
Colin Crossca860ac2016-01-04 14:34:37 -0800778func (c *Module) begin(ctx BaseModuleContext) {
779 if c.compiler != nil {
780 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700781 }
Colin Crossca860ac2016-01-04 14:34:37 -0800782 if c.linker != nil {
783 c.linker.begin(ctx)
784 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700785 if c.stl != nil {
786 c.stl.begin(ctx)
787 }
Colin Cross16b23492016-01-06 14:41:07 -0800788 if c.sanitize != nil {
789 c.sanitize.begin(ctx)
790 }
Colin Crossca860ac2016-01-04 14:34:37 -0800791 for _, feature := range c.features {
792 feature.begin(ctx)
793 }
794}
795
Colin Crossc99deeb2016-04-11 15:06:20 -0700796func (c *Module) deps(ctx BaseModuleContext) Deps {
797 deps := Deps{}
798
799 if c.compiler != nil {
800 deps = c.compiler.deps(ctx, deps)
801 }
802 if c.linker != nil {
803 deps = c.linker.deps(ctx, deps)
804 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700805 if c.stl != nil {
806 deps = c.stl.deps(ctx, deps)
807 }
Colin Cross16b23492016-01-06 14:41:07 -0800808 if c.sanitize != nil {
809 deps = c.sanitize.deps(ctx, deps)
810 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700811 for _, feature := range c.features {
812 deps = feature.deps(ctx, deps)
813 }
814
815 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
816 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
817 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
818 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
819 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
820
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700821 for _, lib := range deps.ReexportSharedLibHeaders {
822 if !inList(lib, deps.SharedLibs) {
823 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
824 }
825 }
826
827 for _, lib := range deps.ReexportStaticLibHeaders {
828 if !inList(lib, deps.StaticLibs) {
829 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
830 }
831 }
832
Colin Crossc99deeb2016-04-11 15:06:20 -0700833 return deps
834}
835
Colin Cross635c3b02016-05-18 15:37:25 -0700836func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800837 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700838 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800839 moduleContextImpl: moduleContextImpl{
840 mod: c,
841 },
842 }
843 ctx.ctx = ctx
844
845 if c.customizer != nil {
846 c.customizer.CustomizeProperties(ctx)
847 }
848
849 c.begin(ctx)
850
Colin Crossc99deeb2016-04-11 15:06:20 -0700851 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800852
Dan Willemsen72d39932016-07-08 23:23:48 -0700853 c.Properties.AndroidMkSharedLibs = append([]string(nil), deps.SharedLibs...)
854
855 if ctx.sdk() {
856 version := "." + ctx.sdkVersion()
857
858 rewriteNdkLibs := func(list []string) []string {
859 for i, entry := range list {
860 if inList(entry, ndkPrebuiltSharedLibraries) {
861 list[i] = "ndk_" + entry + version
862 }
863 }
864 return list
865 }
866
867 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
868 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
869 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700870
871 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
872 deps.WholeStaticLibs...)
873
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700874 for _, lib := range deps.StaticLibs {
875 depTag := staticDepTag
876 if inList(lib, deps.ReexportStaticLibHeaders) {
877 depTag = staticExportDepTag
878 }
879 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
880 deps.StaticLibs...)
881 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700882
883 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
884 deps.LateStaticLibs...)
885
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700886 for _, lib := range deps.SharedLibs {
887 depTag := sharedDepTag
888 if inList(lib, deps.ReexportSharedLibHeaders) {
889 depTag = sharedExportDepTag
890 }
891 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
892 deps.SharedLibs...)
893 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700894
895 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
896 deps.LateSharedLibs...)
897
Colin Cross68861832016-07-08 10:41:41 -0700898 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
899 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700900
Colin Cross68861832016-07-08 10:41:41 -0700901 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700902
903 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700904 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800905 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700906 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700907 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700908 }
Colin Cross6362e272015-10-29 15:25:03 -0700909}
Colin Cross21b9a242015-03-24 14:15:58 -0700910
Colin Cross635c3b02016-05-18 15:37:25 -0700911func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800912 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700913 c.depsMutator(ctx)
914 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800915}
916
Colin Crossca860ac2016-01-04 14:34:37 -0800917func (c *Module) clang(ctx BaseModuleContext) bool {
918 clang := Bool(c.Properties.Clang)
919
920 if c.Properties.Clang == nil {
921 if ctx.Host() {
922 clang = true
923 }
924
925 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
926 clang = true
927 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800928 }
Colin Cross28344522015-04-22 13:07:53 -0700929
Colin Crossca860ac2016-01-04 14:34:37 -0800930 if !c.toolchain(ctx).ClangSupported() {
931 clang = false
932 }
933
934 return clang
935}
936
Colin Crossc99deeb2016-04-11 15:06:20 -0700937// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700938func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800939 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800940
Dan Willemsena96ff642016-06-07 12:34:45 -0700941 // Whether a module can link to another module, taking into
942 // account NDK linking.
943 linkTypeOk := func(from, to *Module) bool {
944 if from.Target().Os != android.Android {
945 // Host code is not restricted
946 return true
947 }
948 if from.Properties.Sdk_version == "" {
949 // Platform code can link to anything
950 return true
951 }
952 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
953 // These are always allowed
954 return true
955 }
956 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
957 // These are allowed, but don't set sdk_version
958 return true
959 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700960 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
961 // These are allowed, but don't set sdk_version
962 return true
963 }
964 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700965 }
966
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 ctx.VisitDirectDeps(func(m blueprint.Module) {
968 name := ctx.OtherModuleName(m)
969 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800970
Colin Cross635c3b02016-05-18 15:37:25 -0700971 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700972 if a == nil {
973 ctx.ModuleErrorf("module %q not an android module", name)
974 return
Colin Crossca860ac2016-01-04 14:34:37 -0800975 }
Colin Crossca860ac2016-01-04 14:34:37 -0800976
Dan Willemsena96ff642016-06-07 12:34:45 -0700977 cc, _ := m.(*Module)
978 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700979 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700980 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700981 case genSourceDepTag:
982 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
983 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
984 genRule.GeneratedSourceFiles()...)
985 } else {
986 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
987 }
988 case genHeaderDepTag:
989 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
990 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
991 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -0700992 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -0700993 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700994 } else {
995 ctx.ModuleErrorf("module %q is not a genrule", name)
996 }
997 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700998 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800999 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001000 return
1001 }
1002
1003 if !a.Enabled() {
1004 ctx.ModuleErrorf("depends on disabled module %q", name)
1005 return
1006 }
1007
Colin Crossa1ad8d12016-06-01 17:09:44 -07001008 if a.Target().Os != ctx.Os() {
1009 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1010 return
1011 }
1012
1013 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1014 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001015 return
1016 }
1017
Dan Willemsena96ff642016-06-07 12:34:45 -07001018 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001019 ctx.ModuleErrorf("module %q missing output file", name)
1020 return
1021 }
1022
1023 if tag == reuseObjTag {
1024 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001025 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001026 return
1027 }
1028
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001029 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001030 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001031 flags := i.exportedFlags()
1032 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001033
1034 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001035 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001036 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001037 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001038
1039 if !linkTypeOk(c, cc) {
1040 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1041 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001042 }
1043
Colin Cross635c3b02016-05-18 15:37:25 -07001044 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001045
1046 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001047 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001048 depPtr = &depPaths.SharedLibs
1049 case lateSharedDepTag:
1050 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001051 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001052 depPtr = &depPaths.StaticLibs
1053 case lateStaticDepTag:
1054 depPtr = &depPaths.LateStaticLibs
1055 case wholeStaticDepTag:
1056 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001057 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001058 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001059 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001060 return
1061 }
1062
1063 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1064 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1065 for i := range missingDeps {
1066 missingDeps[i] += postfix
1067 }
1068 ctx.AddMissingDependencies(missingDeps)
1069 }
1070 depPaths.WholeStaticLibObjFiles =
1071 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1072 case objDepTag:
1073 depPtr = &depPaths.ObjFiles
1074 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001075 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001076 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001077 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001078 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001079 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001080 }
1081
1082 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001083 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001084 }
1085 })
1086
1087 return depPaths
1088}
1089
1090func (c *Module) InstallInData() bool {
1091 if c.installer == nil {
1092 return false
1093 }
1094 return c.installer.inData()
1095}
1096
1097// Compiler
1098
1099type baseCompiler struct {
1100 Properties BaseCompilerProperties
1101}
1102
1103var _ compiler = (*baseCompiler)(nil)
1104
1105func (compiler *baseCompiler) props() []interface{} {
1106 return []interface{}{&compiler.Properties}
1107}
1108
Dan Willemsenb40aab62016-04-20 14:21:14 -07001109func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1110
1111func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1112 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1113 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1114
1115 return deps
1116}
Colin Crossca860ac2016-01-04 14:34:37 -08001117
1118// Create a Flags struct that collects the compile flags from global values,
1119// per-target values, module type values, and per-module Blueprints properties
1120func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1121 toolchain := ctx.toolchain()
1122
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001123 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1124 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1125 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1126 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1127
Colin Crossca860ac2016-01-04 14:34:37 -08001128 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1129 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1130 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1131 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1132 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1133
Colin Cross28344522015-04-22 13:07:53 -07001134 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001135 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1136 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001137 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001138 includeDirsToFlags(localIncludeDirs),
1139 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001140
Colin Crossca860ac2016-01-04 14:34:37 -08001141 if !ctx.noDefaultCompilerFlags() {
1142 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001143 flags.GlobalFlags = append(flags.GlobalFlags,
1144 "${commonGlobalIncludes}",
1145 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001146 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001147 }
1148
1149 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001150 "-I" + android.PathForModuleSrc(ctx).String(),
1151 "-I" + android.PathForModuleOut(ctx).String(),
1152 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001153 }...)
1154 }
1155
Colin Crossca860ac2016-01-04 14:34:37 -08001156 instructionSet := compiler.Properties.Instruction_set
1157 if flags.RequiredInstructionSet != "" {
1158 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001159 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001160 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1161 if flags.Clang {
1162 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1163 }
1164 if err != nil {
1165 ctx.ModuleErrorf("%s", err)
1166 }
1167
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001168 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1169
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001170 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001171 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001172
Colin Cross97ba0732015-03-23 17:50:24 -07001173 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001174 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1175 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1176
Colin Cross97ba0732015-03-23 17:50:24 -07001177 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001178 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1179 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001180 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1181 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1182 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001183
1184 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001185 var gccPrefix string
1186 if !ctx.Darwin() {
1187 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1188 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001189
Colin Cross97ba0732015-03-23 17:50:24 -07001190 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1191 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1192 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001193 }
1194
Colin Crossa1ad8d12016-06-01 17:09:44 -07001195 hod := "host"
1196 if ctx.Os().Class == android.Device {
1197 hod = "device"
1198 }
1199
Colin Crossca860ac2016-01-04 14:34:37 -08001200 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001201 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1202
Colin Cross97ba0732015-03-23 17:50:24 -07001203 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001204 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001205 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001206 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001207 toolchain.ClangCflags(),
1208 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001209 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001210
1211 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001212 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001213 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001214 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001215 toolchain.Cflags(),
1216 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001217 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001218 }
1219
Colin Cross7b66f152015-12-15 16:07:43 -08001220 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1221 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1222 }
1223
Colin Crossf6566ed2015-03-24 11:13:38 -07001224 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001225 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001226 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001227 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001228 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001229 }
1230 }
1231
Colin Cross97ba0732015-03-23 17:50:24 -07001232 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001233
Colin Cross97ba0732015-03-23 17:50:24 -07001234 if flags.Clang {
1235 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001236 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001237 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001238 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001239 }
1240
Colin Crossc4bde762015-11-23 16:11:30 -08001241 if flags.Clang {
1242 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1243 } else {
1244 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001245 }
1246
Colin Crossca860ac2016-01-04 14:34:37 -08001247 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001248 if ctx.Host() && !flags.Clang {
1249 // The host GCC doesn't support C++14 (and is deprecated, so likely
1250 // never will). Build these modules with C++11.
1251 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1252 } else {
1253 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1254 }
1255 }
1256
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001257 // We can enforce some rules more strictly in the code we own. strict
1258 // indicates if this is code that we can be stricter with. If we have
1259 // rules that we want to apply to *our* code (but maybe can't for
1260 // vendor/device specific things), we could extend this to be a ternary
1261 // value.
1262 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001263 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001264 strict = false
1265 }
1266
1267 // Can be used to make some annotations stricter for code we can fix
1268 // (such as when we mark functions as deprecated).
1269 if strict {
1270 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1271 }
1272
Colin Cross3f40fa42015-01-30 17:27:36 -08001273 return flags
1274}
1275
Colin Cross635c3b02016-05-18 15:37:25 -07001276func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001277 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001278 objFiles := compiler.compileObjs(ctx, flags, "",
1279 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1280 deps.GeneratedSources, deps.GeneratedHeaders)
1281
Colin Crossca860ac2016-01-04 14:34:37 -08001282 if ctx.Failed() {
1283 return nil
1284 }
1285
Colin Crossca860ac2016-01-04 14:34:37 -08001286 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001287}
1288
1289// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001290func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1291 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001292
Colin Crossca860ac2016-01-04 14:34:37 -08001293 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001294
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001295 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001296 inputFiles = append(inputFiles, extraSrcs...)
1297 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1298
1299 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001300 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001301
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001303}
1304
Colin Crossca860ac2016-01-04 14:34:37 -08001305// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1306type baseLinker struct {
1307 Properties BaseLinkerProperties
1308 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001309 VariantIsShared bool `blueprint:"mutated"`
1310 VariantIsStatic bool `blueprint:"mutated"`
1311 VariantIsStaticBinary bool `blueprint:"mutated"`
1312 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001313 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001314}
1315
Dan Willemsend30e6102016-03-30 17:35:50 -07001316func (linker *baseLinker) begin(ctx BaseModuleContext) {
1317 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001318 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001319 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001320 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001321 }
1322}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001323
Colin Crossca860ac2016-01-04 14:34:37 -08001324func (linker *baseLinker) props() []interface{} {
1325 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001326}
1327
Colin Crossca860ac2016-01-04 14:34:37 -08001328func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1329 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1330 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1331 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001332
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001333 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1334 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1335
Dan Willemsena96ff642016-06-07 12:34:45 -07001336 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001337 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001338 }
1339
Colin Crossf6566ed2015-03-24 11:13:38 -07001340 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001341 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001342 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1343 if !Bool(linker.Properties.No_libgcc) {
1344 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001345 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001346
Colin Crossca860ac2016-01-04 14:34:37 -08001347 if !linker.static() {
1348 if linker.Properties.System_shared_libs != nil {
1349 deps.LateSharedLibs = append(deps.LateSharedLibs,
1350 linker.Properties.System_shared_libs...)
1351 } else if !ctx.sdk() {
1352 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1353 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001354 }
Colin Cross577f6e42015-03-27 18:23:34 -07001355
Colin Crossca860ac2016-01-04 14:34:37 -08001356 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001357 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001358 "libc",
1359 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001360 )
1361 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001362 }
1363
Colin Crossca860ac2016-01-04 14:34:37 -08001364 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001365}
1366
Colin Crossca860ac2016-01-04 14:34:37 -08001367func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1368 toolchain := ctx.toolchain()
1369
Colin Crossca860ac2016-01-04 14:34:37 -08001370 if !ctx.noDefaultCompilerFlags() {
1371 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1372 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1373 }
1374
1375 if flags.Clang {
1376 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1377 } else {
1378 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1379 }
1380
1381 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001382 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1383
Colin Crossca860ac2016-01-04 14:34:37 -08001384 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1385 }
1386 }
1387
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001388 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1389
Dan Willemsen00ced762016-05-10 17:31:21 -07001390 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1391
Dan Willemsend30e6102016-03-30 17:35:50 -07001392 if ctx.Host() && !linker.static() {
1393 rpath_prefix := `\$$ORIGIN/`
1394 if ctx.Darwin() {
1395 rpath_prefix = "@loader_path/"
1396 }
1397
Colin Crossc99deeb2016-04-11 15:06:20 -07001398 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001399 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1400 }
1401 }
1402
Dan Willemsene7174922016-03-30 17:33:52 -07001403 if flags.Clang {
1404 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1405 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001406 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1407 }
1408
1409 return flags
1410}
1411
1412func (linker *baseLinker) static() bool {
1413 return linker.dynamicProperties.VariantIsStatic
1414}
1415
1416func (linker *baseLinker) staticBinary() bool {
1417 return linker.dynamicProperties.VariantIsStaticBinary
1418}
1419
1420func (linker *baseLinker) setStatic(static bool) {
1421 linker.dynamicProperties.VariantIsStatic = static
1422}
1423
Colin Cross16b23492016-01-06 14:41:07 -08001424func (linker *baseLinker) isDependencyRoot() bool {
1425 return false
1426}
1427
Colin Crossca860ac2016-01-04 14:34:37 -08001428type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001429 // Returns true if the build options for the module have selected a static or shared build
1430 buildStatic() bool
1431 buildShared() bool
1432
1433 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001434 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001435
Colin Cross18b6dc52015-04-28 13:20:37 -07001436 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001437 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001438
1439 // Returns whether a module is a static binary
1440 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001441
1442 // Returns true for dependency roots (binaries)
1443 // TODO(ccross): also handle dlopenable libraries
1444 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001445}
1446
Colin Crossca860ac2016-01-04 14:34:37 -08001447type baseInstaller struct {
1448 Properties InstallerProperties
1449
1450 dir string
1451 dir64 string
1452 data bool
1453
Colin Cross635c3b02016-05-18 15:37:25 -07001454 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001455}
1456
1457var _ installer = (*baseInstaller)(nil)
1458
1459func (installer *baseInstaller) props() []interface{} {
1460 return []interface{}{&installer.Properties}
1461}
1462
Colin Cross635c3b02016-05-18 15:37:25 -07001463func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001464 subDir := installer.dir
1465 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1466 subDir = installer.dir64
1467 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001468 if !ctx.Host() && !ctx.Arch().Native {
1469 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1470 }
Colin Cross635c3b02016-05-18 15:37:25 -07001471 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001472 installer.path = ctx.InstallFile(dir, file)
1473}
1474
1475func (installer *baseInstaller) inData() bool {
1476 return installer.data
1477}
1478
Colin Cross3f40fa42015-01-30 17:27:36 -08001479//
1480// Combined static+shared libraries
1481//
1482
Colin Cross919281a2016-04-05 16:42:05 -07001483type flagExporter struct {
1484 Properties FlagExporterProperties
1485
1486 flags []string
1487}
1488
1489func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001490 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1491 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001492}
1493
1494func (f *flagExporter) reexportFlags(flags []string) {
1495 f.flags = append(f.flags, flags...)
1496}
1497
1498func (f *flagExporter) exportedFlags() []string {
1499 return f.flags
1500}
1501
1502type exportedFlagsProducer interface {
1503 exportedFlags() []string
1504}
1505
1506var _ exportedFlagsProducer = (*flagExporter)(nil)
1507
Colin Crossca860ac2016-01-04 14:34:37 -08001508type libraryCompiler struct {
1509 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001510
Colin Crossca860ac2016-01-04 14:34:37 -08001511 linker *libraryLinker
1512 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001513
Colin Crossca860ac2016-01-04 14:34:37 -08001514 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001515 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001516}
1517
Colin Crossca860ac2016-01-04 14:34:37 -08001518var _ compiler = (*libraryCompiler)(nil)
1519
1520func (library *libraryCompiler) props() []interface{} {
1521 props := library.baseCompiler.props()
1522 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001523}
1524
Colin Crossca860ac2016-01-04 14:34:37 -08001525func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1526 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001527
Dan Willemsen490fd492015-11-24 17:53:15 -08001528 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1529 // all code is position independent, and then those warnings get promoted to
1530 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001531 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001532 flags.CFlags = append(flags.CFlags, "-fPIC")
1533 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001534
Colin Crossca860ac2016-01-04 14:34:37 -08001535 if library.linker.static() {
1536 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001537 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001538 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001539 }
1540
Colin Crossca860ac2016-01-04 14:34:37 -08001541 return flags
1542}
1543
Colin Cross635c3b02016-05-18 15:37:25 -07001544func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1545 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001546
Dan Willemsenb40aab62016-04-20 14:21:14 -07001547 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001548 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001549
1550 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001551 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001552 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1553 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001554 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001555 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001556 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1557 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001558 }
1559
1560 return objFiles
1561}
1562
1563type libraryLinker struct {
1564 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001565 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001566 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001567
1568 Properties LibraryLinkerProperties
1569
1570 dynamicProperties struct {
1571 BuildStatic bool `blueprint:"mutated"`
1572 BuildShared bool `blueprint:"mutated"`
1573 }
1574
Colin Crossca860ac2016-01-04 14:34:37 -08001575 // If we're used as a whole_static_lib, our missing dependencies need
1576 // to be given
1577 wholeStaticMissingDeps []string
1578
1579 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001580 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001581}
1582
1583var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001584
1585func (library *libraryLinker) props() []interface{} {
1586 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001587 return append(props,
1588 &library.Properties,
1589 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001590 &library.flagExporter.Properties,
1591 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001592}
1593
1594func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1595 flags = library.baseLinker.flags(ctx, flags)
1596
1597 flags.Nocrt = Bool(library.Properties.Nocrt)
1598
1599 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001600 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001601 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1602 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001603 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001604 sharedFlag = "-shared"
1605 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001606 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001607 flags.LdFlags = append(flags.LdFlags,
1608 "-nostdlib",
1609 "-Wl,--gc-sections",
1610 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001611 }
Colin Cross97ba0732015-03-23 17:50:24 -07001612
Colin Cross0af4b842015-04-30 16:36:18 -07001613 if ctx.Darwin() {
1614 flags.LdFlags = append(flags.LdFlags,
1615 "-dynamiclib",
1616 "-single_module",
1617 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001618 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001619 )
1620 } else {
1621 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001622 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001623 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001624 )
1625 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001626 }
Colin Cross97ba0732015-03-23 17:50:24 -07001627
1628 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001629}
1630
Colin Crossca860ac2016-01-04 14:34:37 -08001631func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1632 deps = library.baseLinker.deps(ctx, deps)
1633 if library.static() {
1634 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1635 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1636 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1637 } else {
1638 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1639 if !ctx.sdk() {
1640 deps.CrtBegin = "crtbegin_so"
1641 deps.CrtEnd = "crtend_so"
1642 } else {
1643 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1644 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1645 }
1646 }
1647 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1648 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1649 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1650 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001651
Colin Crossca860ac2016-01-04 14:34:37 -08001652 return deps
1653}
Colin Cross3f40fa42015-01-30 17:27:36 -08001654
Colin Crossca860ac2016-01-04 14:34:37 -08001655func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001656 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001657
Colin Cross635c3b02016-05-18 15:37:25 -07001658 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001659 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001660
Colin Cross635c3b02016-05-18 15:37:25 -07001661 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001662 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001663
Colin Cross0af4b842015-04-30 16:36:18 -07001664 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001665 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001666 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001667 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001668 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001669
Colin Crossca860ac2016-01-04 14:34:37 -08001670 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001671
1672 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001673
1674 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001675}
1676
Colin Crossca860ac2016-01-04 14:34:37 -08001677func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001678 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001679
Colin Cross635c3b02016-05-18 15:37:25 -07001680 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001681
Colin Cross635c3b02016-05-18 15:37:25 -07001682 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1683 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1684 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1685 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001686 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001687 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001688 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001689 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001690 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001691 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001692 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1693 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001694 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001695 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1696 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001697 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001698 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1699 }
1700 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001701 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001702 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1703 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001704 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001705 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001706 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001707 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001708 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001709 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001710 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001711 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001712 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001713 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001714 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001715 }
Colin Crossaee540a2015-07-06 17:48:31 -07001716 }
1717
Colin Cross665dce92016-04-28 14:50:03 -07001718 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001719 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001720 ret := outputFile
1721
1722 builderFlags := flagsToBuilderFlags(flags)
1723
1724 if library.stripper.needsStrip(ctx) {
1725 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001726 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001727 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1728 }
1729
Colin Crossca860ac2016-01-04 14:34:37 -08001730 sharedLibs := deps.SharedLibs
1731 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001732
Colin Crossca860ac2016-01-04 14:34:37 -08001733 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1734 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001735 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001736
Colin Cross665dce92016-04-28 14:50:03 -07001737 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001738}
1739
Colin Crossca860ac2016-01-04 14:34:37 -08001740func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001741 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001742
Colin Crossc99deeb2016-04-11 15:06:20 -07001743 objFiles = append(objFiles, deps.ObjFiles...)
1744
Colin Cross635c3b02016-05-18 15:37:25 -07001745 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001746 if library.static() {
1747 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001748 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001749 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001750 }
1751
Colin Cross919281a2016-04-05 16:42:05 -07001752 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001753 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001754
1755 return out
1756}
1757
1758func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001759 return library.dynamicProperties.BuildStatic &&
1760 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001761}
1762
1763func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001764 return library.dynamicProperties.BuildShared &&
1765 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001766}
1767
1768func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1769 return library.wholeStaticMissingDeps
1770}
1771
Colin Crossc99deeb2016-04-11 15:06:20 -07001772func (library *libraryLinker) installable() bool {
1773 return !library.static()
1774}
1775
Colin Crossca860ac2016-01-04 14:34:37 -08001776type libraryInstaller struct {
1777 baseInstaller
1778
Colin Cross30d5f512016-05-03 18:02:42 -07001779 linker *libraryLinker
1780 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001781}
1782
Colin Cross635c3b02016-05-18 15:37:25 -07001783func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001784 if !library.linker.static() {
1785 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001786 }
1787}
1788
Colin Cross30d5f512016-05-03 18:02:42 -07001789func (library *libraryInstaller) inData() bool {
1790 return library.baseInstaller.inData() || library.sanitize.inData()
1791}
1792
Colin Cross635c3b02016-05-18 15:37:25 -07001793func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1794 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001795
Colin Crossca860ac2016-01-04 14:34:37 -08001796 linker := &libraryLinker{}
1797 linker.dynamicProperties.BuildShared = shared
1798 linker.dynamicProperties.BuildStatic = static
1799 module.linker = linker
1800
1801 module.compiler = &libraryCompiler{
1802 linker: linker,
1803 }
1804 module.installer = &libraryInstaller{
1805 baseInstaller: baseInstaller{
1806 dir: "lib",
1807 dir64: "lib64",
1808 },
Colin Cross30d5f512016-05-03 18:02:42 -07001809 linker: linker,
1810 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001811 }
1812
Colin Crossca860ac2016-01-04 14:34:37 -08001813 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001814}
1815
Colin Crossca860ac2016-01-04 14:34:37 -08001816func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001817 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001818 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001819}
1820
Colin Cross3f40fa42015-01-30 17:27:36 -08001821//
1822// Objects (for crt*.o)
1823//
1824
Colin Crossca860ac2016-01-04 14:34:37 -08001825type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001826 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001827}
1828
Colin Crossca860ac2016-01-04 14:34:37 -08001829func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001830 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001831 module.compiler = &baseCompiler{}
1832 module.linker = &objectLinker{}
1833 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001834}
1835
Colin Cross81413472016-04-11 14:37:39 -07001836func (object *objectLinker) props() []interface{} {
1837 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001838}
1839
Colin Crossca860ac2016-01-04 14:34:37 -08001840func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001841
Colin Cross81413472016-04-11 14:37:39 -07001842func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1843 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001844 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001845}
1846
Colin Crossca860ac2016-01-04 14:34:37 -08001847func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001848 if flags.Clang {
1849 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1850 } else {
1851 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1852 }
1853
Colin Crossca860ac2016-01-04 14:34:37 -08001854 return flags
1855}
1856
1857func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001858 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001859
Colin Cross97ba0732015-03-23 17:50:24 -07001860 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001861
Colin Cross635c3b02016-05-18 15:37:25 -07001862 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001863 if len(objFiles) == 1 {
1864 outputFile = objFiles[0]
1865 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001866 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001867 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001868 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001869 }
1870
Colin Cross3f40fa42015-01-30 17:27:36 -08001871 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001872 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001873}
1874
Colin Crossc99deeb2016-04-11 15:06:20 -07001875func (*objectLinker) installable() bool {
1876 return false
1877}
1878
Colin Cross3f40fa42015-01-30 17:27:36 -08001879//
1880// Executables
1881//
1882
Colin Crossca860ac2016-01-04 14:34:37 -08001883type binaryLinker struct {
1884 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001885 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001886
Colin Crossca860ac2016-01-04 14:34:37 -08001887 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001888
Colin Cross635c3b02016-05-18 15:37:25 -07001889 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001890}
1891
Colin Crossca860ac2016-01-04 14:34:37 -08001892var _ linker = (*binaryLinker)(nil)
1893
1894func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001895 return append(binary.baseLinker.props(),
1896 &binary.Properties,
1897 &binary.stripper.StripProperties)
1898
Colin Cross3f40fa42015-01-30 17:27:36 -08001899}
1900
Colin Crossca860ac2016-01-04 14:34:37 -08001901func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001902 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001903}
1904
Colin Crossca860ac2016-01-04 14:34:37 -08001905func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001906 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001907}
1908
Colin Crossca860ac2016-01-04 14:34:37 -08001909func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001910 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001911 if binary.Properties.Stem != "" {
1912 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001913 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001914
Colin Crossca860ac2016-01-04 14:34:37 -08001915 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001916}
1917
Colin Crossca860ac2016-01-04 14:34:37 -08001918func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1919 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001920 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001921 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001922 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001923 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001924 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001925 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001926 }
Colin Crossca860ac2016-01-04 14:34:37 -08001927 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001928 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001929 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001930 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001931 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001932 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001933 }
Colin Crossca860ac2016-01-04 14:34:37 -08001934 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001935 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001936
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001937 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001938 if inList("libc++_static", deps.StaticLibs) {
1939 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001940 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001941 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1942 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1943 // move them to the beginning of deps.LateStaticLibs
1944 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001945 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001946 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001947 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001948 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001949 }
Colin Crossca860ac2016-01-04 14:34:37 -08001950
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001951 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001952 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1953 "from static libs or set static_executable: true")
1954 }
1955 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001956}
1957
Colin Crossc99deeb2016-04-11 15:06:20 -07001958func (*binaryLinker) installable() bool {
1959 return true
1960}
1961
Colin Cross16b23492016-01-06 14:41:07 -08001962func (binary *binaryLinker) isDependencyRoot() bool {
1963 return true
1964}
1965
Colin Cross635c3b02016-05-18 15:37:25 -07001966func NewBinary(hod android.HostOrDeviceSupported) *Module {
1967 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001968 module.compiler = &baseCompiler{}
1969 module.linker = &binaryLinker{}
1970 module.installer = &baseInstaller{
1971 dir: "bin",
1972 }
1973 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001974}
1975
Colin Crossca860ac2016-01-04 14:34:37 -08001976func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001977 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001978 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001979}
1980
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001981func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1982 binary.baseLinker.begin(ctx)
1983
1984 static := Bool(binary.Properties.Static_executable)
1985 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001986 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001987 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1988 static = true
1989 }
1990 } else {
1991 // Static executables are not supported on Darwin or Windows
1992 static = false
1993 }
Colin Cross0af4b842015-04-30 16:36:18 -07001994 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001995 if static {
1996 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001997 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001998 }
1999}
2000
Colin Crossca860ac2016-01-04 14:34:37 -08002001func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2002 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002003
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002004 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002005 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002006 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002007 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2008 }
2009 }
2010
2011 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2012 // all code is position independent, and then those warnings get promoted to
2013 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002014 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002015 flags.CFlags = append(flags.CFlags, "-fpie")
2016 }
Colin Cross97ba0732015-03-23 17:50:24 -07002017
Colin Crossf6566ed2015-03-24 11:13:38 -07002018 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002019 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002020 // Clang driver needs -static to create static executable.
2021 // However, bionic/linker uses -shared to overwrite.
2022 // Linker for x86 targets does not allow coexistance of -static and -shared,
2023 // so we add -static only if -shared is not used.
2024 if !inList("-shared", flags.LdFlags) {
2025 flags.LdFlags = append(flags.LdFlags, "-static")
2026 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002027
Colin Crossed4cf0b2015-03-26 14:43:45 -07002028 flags.LdFlags = append(flags.LdFlags,
2029 "-nostdlib",
2030 "-Bstatic",
2031 "-Wl,--gc-sections",
2032 )
2033
2034 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002035 if flags.DynamicLinker == "" {
2036 flags.DynamicLinker = "/system/bin/linker"
2037 if flags.Toolchain.Is64Bit() {
2038 flags.DynamicLinker += "64"
2039 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002040 }
2041
2042 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002043 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002044 "-nostdlib",
2045 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002046 "-Wl,--gc-sections",
2047 "-Wl,-z,nocopyreloc",
2048 )
2049 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002050 } else {
2051 if binary.staticBinary() {
2052 flags.LdFlags = append(flags.LdFlags, "-static")
2053 }
2054 if ctx.Darwin() {
2055 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2056 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002057 }
2058
Colin Cross97ba0732015-03-23 17:50:24 -07002059 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002060}
2061
Colin Crossca860ac2016-01-04 14:34:37 -08002062func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002063 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002064
Colin Cross665dce92016-04-28 14:50:03 -07002065 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002066 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002067 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002068 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002069 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002070 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002071
Colin Cross635c3b02016-05-18 15:37:25 -07002072 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002073
Colin Crossca860ac2016-01-04 14:34:37 -08002074 sharedLibs := deps.SharedLibs
2075 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2076
Colin Cross16b23492016-01-06 14:41:07 -08002077 if flags.DynamicLinker != "" {
2078 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2079 }
2080
Colin Cross665dce92016-04-28 14:50:03 -07002081 builderFlags := flagsToBuilderFlags(flags)
2082
2083 if binary.stripper.needsStrip(ctx) {
2084 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002085 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002086 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2087 }
2088
2089 if binary.Properties.Prefix_symbols != "" {
2090 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002091 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002092 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2093 flagsToBuilderFlags(flags), afterPrefixSymbols)
2094 }
2095
Colin Crossca860ac2016-01-04 14:34:37 -08002096 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002097 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002098 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002099
2100 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002101}
Colin Cross3f40fa42015-01-30 17:27:36 -08002102
Colin Cross635c3b02016-05-18 15:37:25 -07002103func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002104 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002105}
2106
Colin Cross665dce92016-04-28 14:50:03 -07002107type stripper struct {
2108 StripProperties StripProperties
2109}
2110
2111func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2112 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2113}
2114
Colin Cross635c3b02016-05-18 15:37:25 -07002115func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002116 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002117 if ctx.Darwin() {
2118 TransformDarwinStrip(ctx, in, out)
2119 } else {
2120 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2121 // TODO(ccross): don't add gnu debuglink for user builds
2122 flags.stripAddGnuDebuglink = true
2123 TransformStrip(ctx, in, out, flags)
2124 }
Colin Cross665dce92016-04-28 14:50:03 -07002125}
2126
Colin Cross635c3b02016-05-18 15:37:25 -07002127func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002128 if m, ok := mctx.Module().(*Module); ok {
2129 if test, ok := m.linker.(*testLinker); ok {
2130 if Bool(test.Properties.Test_per_src) {
2131 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2132 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2133 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2134 }
2135 tests := mctx.CreateLocalVariations(testNames...)
2136 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2137 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2138 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2139 }
Colin Cross6002e052015-09-16 16:00:08 -07002140 }
2141 }
2142 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002143}
2144
Colin Crossca860ac2016-01-04 14:34:37 -08002145type testLinker struct {
2146 binaryLinker
2147 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002148}
2149
Dan Willemsend30e6102016-03-30 17:35:50 -07002150func (test *testLinker) begin(ctx BaseModuleContext) {
2151 test.binaryLinker.begin(ctx)
2152
2153 runpath := "../../lib"
2154 if ctx.toolchain().Is64Bit() {
2155 runpath += "64"
2156 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002157 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002158}
2159
Colin Crossca860ac2016-01-04 14:34:37 -08002160func (test *testLinker) props() []interface{} {
2161 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002162}
2163
Colin Crossca860ac2016-01-04 14:34:37 -08002164func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2165 flags = test.binaryLinker.flags(ctx, flags)
2166
2167 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002168 return flags
2169 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002170
Colin Cross97ba0732015-03-23 17:50:24 -07002171 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002172 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002173 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002174
Colin Crossa1ad8d12016-06-01 17:09:44 -07002175 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002176 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002177 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002178 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002179 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2180 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002181 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002182 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2183 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002184 }
2185 } else {
2186 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002187 }
2188
Colin Cross21b9a242015-03-24 14:15:58 -07002189 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002190}
2191
Colin Crossca860ac2016-01-04 14:34:37 -08002192func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2193 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002194 if ctx.sdk() && ctx.Device() {
2195 switch ctx.selectedStl() {
2196 case "ndk_libc++_shared", "ndk_libc++_static":
2197 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2198 case "ndk_libgnustl_static":
2199 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2200 default:
2201 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2202 }
2203 } else {
2204 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2205 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002206 }
Colin Crossca860ac2016-01-04 14:34:37 -08002207 deps = test.binaryLinker.deps(ctx, deps)
2208 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002209}
2210
Colin Crossca860ac2016-01-04 14:34:37 -08002211type testInstaller struct {
2212 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002213}
2214
Colin Cross635c3b02016-05-18 15:37:25 -07002215func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002216 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2217 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2218 installer.baseInstaller.install(ctx, file)
2219}
2220
Colin Cross635c3b02016-05-18 15:37:25 -07002221func NewTest(hod android.HostOrDeviceSupported) *Module {
2222 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002223 module.compiler = &baseCompiler{}
2224 linker := &testLinker{}
2225 linker.Properties.Gtest = true
2226 module.linker = linker
2227 module.installer = &testInstaller{
2228 baseInstaller: baseInstaller{
2229 dir: "nativetest",
2230 dir64: "nativetest64",
2231 data: true,
2232 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002233 }
Colin Crossca860ac2016-01-04 14:34:37 -08002234 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002235}
2236
Colin Crossca860ac2016-01-04 14:34:37 -08002237func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002238 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002239 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002240}
2241
Colin Crossca860ac2016-01-04 14:34:37 -08002242type benchmarkLinker struct {
2243 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002244}
2245
Colin Crossca860ac2016-01-04 14:34:37 -08002246func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2247 deps = benchmark.binaryLinker.deps(ctx, deps)
2248 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2249 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002250}
2251
Colin Cross635c3b02016-05-18 15:37:25 -07002252func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2253 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002254 module.compiler = &baseCompiler{}
2255 module.linker = &benchmarkLinker{}
2256 module.installer = &baseInstaller{
2257 dir: "nativetest",
2258 dir64: "nativetest64",
2259 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002260 }
Colin Crossca860ac2016-01-04 14:34:37 -08002261 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002262}
2263
Colin Crossca860ac2016-01-04 14:34:37 -08002264func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002265 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002266 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002267}
2268
Colin Cross3f40fa42015-01-30 17:27:36 -08002269//
2270// Static library
2271//
2272
Colin Crossca860ac2016-01-04 14:34:37 -08002273func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002274 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002275 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002276}
2277
2278//
2279// Shared libraries
2280//
2281
Colin Crossca860ac2016-01-04 14:34:37 -08002282func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002283 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002284 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002285}
2286
2287//
2288// Host static library
2289//
2290
Colin Crossca860ac2016-01-04 14:34:37 -08002291func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002292 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002293 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002294}
2295
2296//
2297// Host Shared libraries
2298//
2299
Colin Crossca860ac2016-01-04 14:34:37 -08002300func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002301 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002302 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002303}
2304
2305//
2306// Host Binaries
2307//
2308
Colin Crossca860ac2016-01-04 14:34:37 -08002309func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002310 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002311 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002312}
2313
2314//
Colin Cross1f8f2342015-03-26 16:09:47 -07002315// Host Tests
2316//
2317
Colin Crossca860ac2016-01-04 14:34:37 -08002318func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002319 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002320 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002321}
2322
2323//
Colin Cross2ba19d92015-05-07 15:44:20 -07002324// Host Benchmarks
2325//
2326
Colin Crossca860ac2016-01-04 14:34:37 -08002327func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002328 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002329 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002330}
2331
2332//
Colin Crosscfad1192015-11-02 16:43:11 -08002333// Defaults
2334//
Colin Crossca860ac2016-01-04 14:34:37 -08002335type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002336 android.ModuleBase
2337 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002338}
2339
Colin Cross635c3b02016-05-18 15:37:25 -07002340func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002341}
2342
Colin Crossca860ac2016-01-04 14:34:37 -08002343func defaultsFactory() (blueprint.Module, []interface{}) {
2344 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002345
2346 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002347 &BaseProperties{},
2348 &BaseCompilerProperties{},
2349 &BaseLinkerProperties{},
2350 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002351 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002352 &LibraryLinkerProperties{},
2353 &BinaryLinkerProperties{},
2354 &TestLinkerProperties{},
2355 &UnusedProperties{},
2356 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002357 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002358 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002359 }
2360
Colin Cross635c3b02016-05-18 15:37:25 -07002361 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2362 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002363
Colin Cross635c3b02016-05-18 15:37:25 -07002364 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002365}
2366
2367//
Colin Cross3f40fa42015-01-30 17:27:36 -08002368// Device libraries shipped with gcc
2369//
2370
Colin Crossca860ac2016-01-04 14:34:37 -08002371type toolchainLibraryLinker struct {
2372 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002373}
2374
Colin Crossca860ac2016-01-04 14:34:37 -08002375var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2376
2377func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002378 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002379 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002380}
2381
Colin Crossca860ac2016-01-04 14:34:37 -08002382func (*toolchainLibraryLinker) buildStatic() bool {
2383 return true
2384}
Colin Cross3f40fa42015-01-30 17:27:36 -08002385
Colin Crossca860ac2016-01-04 14:34:37 -08002386func (*toolchainLibraryLinker) buildShared() bool {
2387 return false
2388}
2389
2390func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002391 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002392 module.compiler = &baseCompiler{}
2393 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002394 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002395 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002396}
2397
Colin Crossca860ac2016-01-04 14:34:37 -08002398func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002399 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002400
2401 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002402 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002403
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002404 if flags.Clang {
2405 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2406 }
2407
Colin Crossca860ac2016-01-04 14:34:37 -08002408 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002409
2410 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002411
Colin Crossca860ac2016-01-04 14:34:37 -08002412 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002413}
2414
Colin Crossc99deeb2016-04-11 15:06:20 -07002415func (*toolchainLibraryLinker) installable() bool {
2416 return false
2417}
2418
Dan Albertbe961682015-03-18 23:38:50 -07002419// NDK prebuilt libraries.
2420//
2421// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2422// either (with the exception of the shared STLs, which are installed to the app's directory rather
2423// than to the system image).
2424
Colin Cross635c3b02016-05-18 15:37:25 -07002425func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002426 suffix := ""
2427 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2428 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002429 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002430 suffix = "64"
2431 }
Colin Cross635c3b02016-05-18 15:37:25 -07002432 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002433 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002434}
2435
Colin Cross635c3b02016-05-18 15:37:25 -07002436func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2437 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002438
2439 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2440 // We want to translate to just NAME.EXT
2441 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2442 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002443 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002444}
2445
Colin Crossca860ac2016-01-04 14:34:37 -08002446type ndkPrebuiltObjectLinker struct {
2447 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002448}
2449
Colin Crossca860ac2016-01-04 14:34:37 -08002450func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002451 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002452 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002453}
2454
Colin Crossca860ac2016-01-04 14:34:37 -08002455func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002456 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002457 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002458 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002459 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002460}
2461
Colin Crossca860ac2016-01-04 14:34:37 -08002462func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002463 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002464 // A null build step, but it sets up the output path.
2465 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2466 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2467 }
2468
Colin Crossca860ac2016-01-04 14:34:37 -08002469 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002470}
2471
Colin Crossca860ac2016-01-04 14:34:37 -08002472type ndkPrebuiltLibraryLinker struct {
2473 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002474}
2475
Colin Crossca860ac2016-01-04 14:34:37 -08002476var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2477var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002478
Colin Crossca860ac2016-01-04 14:34:37 -08002479func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002480 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002481}
2482
Colin Crossca860ac2016-01-04 14:34:37 -08002483func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002484 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002485 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002486}
2487
Colin Crossca860ac2016-01-04 14:34:37 -08002488func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002489 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002490 linker := &ndkPrebuiltLibraryLinker{}
2491 linker.dynamicProperties.BuildShared = true
2492 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002493 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002494 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002495}
2496
Colin Crossca860ac2016-01-04 14:34:37 -08002497func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002498 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002499 // A null build step, but it sets up the output path.
2500 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2501 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2502 }
2503
Colin Cross919281a2016-04-05 16:42:05 -07002504 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002505
Colin Crossca860ac2016-01-04 14:34:37 -08002506 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2507 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002508}
2509
2510// The NDK STLs are slightly different from the prebuilt system libraries:
2511// * Are not specific to each platform version.
2512// * The libraries are not in a predictable location for each STL.
2513
Colin Crossca860ac2016-01-04 14:34:37 -08002514type ndkPrebuiltStlLinker struct {
2515 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002516}
2517
Colin Crossca860ac2016-01-04 14:34:37 -08002518func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002519 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002520 linker := &ndkPrebuiltStlLinker{}
2521 linker.dynamicProperties.BuildShared = true
2522 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002523 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002524 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002525}
2526
Colin Crossca860ac2016-01-04 14:34:37 -08002527func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002528 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002529 linker := &ndkPrebuiltStlLinker{}
2530 linker.dynamicProperties.BuildStatic = true
2531 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002532 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002533 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002534}
2535
Colin Cross635c3b02016-05-18 15:37:25 -07002536func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002537 gccVersion := toolchain.GccVersion()
2538 var libDir string
2539 switch stl {
2540 case "libstlport":
2541 libDir = "cxx-stl/stlport/libs"
2542 case "libc++":
2543 libDir = "cxx-stl/llvm-libc++/libs"
2544 case "libgnustl":
2545 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2546 }
2547
2548 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002549 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002550 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002551 }
2552
2553 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002554 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002555}
2556
Colin Crossca860ac2016-01-04 14:34:37 -08002557func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002558 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002559 // A null build step, but it sets up the output path.
2560 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2561 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2562 }
2563
Colin Cross919281a2016-04-05 16:42:05 -07002564 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002565
2566 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002567 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002568 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002569 libExt = staticLibraryExtension
2570 }
2571
2572 stlName := strings.TrimSuffix(libName, "_shared")
2573 stlName = strings.TrimSuffix(stlName, "_static")
2574 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002575 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002576}
2577
Colin Cross635c3b02016-05-18 15:37:25 -07002578func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002579 if m, ok := mctx.Module().(*Module); ok {
2580 if m.linker != nil {
2581 if linker, ok := m.linker.(baseLinkerInterface); ok {
2582 var modules []blueprint.Module
2583 if linker.buildStatic() && linker.buildShared() {
2584 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002585 static := modules[0].(*Module)
2586 shared := modules[1].(*Module)
2587
2588 static.linker.(baseLinkerInterface).setStatic(true)
2589 shared.linker.(baseLinkerInterface).setStatic(false)
2590
2591 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2592 sharedCompiler := shared.compiler.(*libraryCompiler)
2593 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2594 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2595 // Optimize out compiling common .o files twice for static+shared libraries
2596 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2597 sharedCompiler.baseCompiler.Properties.Srcs = nil
2598 }
2599 }
Colin Crossca860ac2016-01-04 14:34:37 -08002600 } else if linker.buildStatic() {
2601 modules = mctx.CreateLocalVariations("static")
2602 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2603 } else if linker.buildShared() {
2604 modules = mctx.CreateLocalVariations("shared")
2605 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2606 } else {
2607 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2608 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002609 }
2610 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002611 }
2612}
Colin Cross74d1ec02015-04-28 13:30:13 -07002613
2614// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2615// modifies the slice contents in place, and returns a subslice of the original slice
2616func lastUniqueElements(list []string) []string {
2617 totalSkip := 0
2618 for i := len(list) - 1; i >= totalSkip; i-- {
2619 skip := 0
2620 for j := i - 1; j >= totalSkip; j-- {
2621 if list[i] == list[j] {
2622 skip++
2623 } else {
2624 list[j+skip] = list[j]
2625 }
2626 }
2627 totalSkip += skip
2628 }
2629 return list[totalSkip:]
2630}
Colin Cross06a931b2015-10-28 17:23:31 -07002631
2632var Bool = proptools.Bool