blob: a828d10727e390cfc1081375cf6a7137f509bbdb [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 }
118)
119
120func init() {
Colin Cross635c3b02016-05-18 15:37:25 -0700121 if android.CurrentHostType() == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700122 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
123 }
124
Colin Cross3f40fa42015-01-30 17:27:36 -0800125 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
126 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
127 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800128 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800129
130 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
131
132 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800133 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800134 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800135 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800136 pctx.StaticVariable("hostClangGlobalCflags",
137 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800138 pctx.StaticVariable("noOverrideClangGlobalCflags",
139 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
140
Tim Kilbournf2948142015-03-11 12:03:03 -0700141 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800142 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800143
144 // Everything in this list is a crime against abstraction and dependency tracking.
145 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800146 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700147 []string{
148 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800149 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700150 "hardware/libhardware/include",
151 "hardware/libhardware_legacy/include",
152 "hardware/ril/include",
153 "libnativehelper/include",
154 "frameworks/native/include",
155 "frameworks/native/opengl/include",
156 "frameworks/av/include",
157 "frameworks/base/include",
158 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800159 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
160 // with this, since there is no associated library.
161 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
162 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700164 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
165 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700166 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700167 return override, nil
168 }
169 return "${clangDefaultBase}", nil
170 })
171 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700172 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700173 return override, nil
174 }
Stephen Hines369f0132016-04-26 14:34:07 -0700175 return "clang-2812033", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700176 })
Colin Cross16b23492016-01-06 14:41:07 -0800177 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
178 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800179}
180
Colin Crossca860ac2016-01-04 14:34:37 -0800181type Deps struct {
182 SharedLibs, LateSharedLibs []string
183 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700184
Colin Cross81413472016-04-11 14:37:39 -0700185 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700186
Dan Willemsenb40aab62016-04-20 14:21:14 -0700187 GeneratedSources []string
188 GeneratedHeaders []string
189
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700191
Colin Cross97ba0732015-03-23 17:50:24 -0700192 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700193}
194
Colin Crossca860ac2016-01-04 14:34:37 -0800195type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700196 SharedLibs, LateSharedLibs android.Paths
197 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198
Colin Cross635c3b02016-05-18 15:37:25 -0700199 ObjFiles android.Paths
200 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700201
Colin Cross635c3b02016-05-18 15:37:25 -0700202 GeneratedSources android.Paths
203 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700204
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700205 Cflags, ReexportedCflags []string
206
Colin Cross635c3b02016-05-18 15:37:25 -0700207 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208}
209
Colin Crossca860ac2016-01-04 14:34:37 -0800210type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700211 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
212 AsFlags []string // Flags that apply to assembly source files
213 CFlags []string // Flags that apply to C and C++ source files
214 ConlyFlags []string // Flags that apply to C source files
215 CppFlags []string // Flags that apply to C++ source files
216 YaccFlags []string // Flags that apply to Yacc source files
217 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800218 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700219
220 Nocrt bool
221 Toolchain Toolchain
222 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800223
224 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800225 DynamicLinker string
226
Colin Cross635c3b02016-05-18 15:37:25 -0700227 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700228}
229
Colin Crossca860ac2016-01-04 14:34:37 -0800230type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700231 // 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 -0700232 Srcs []string `android:"arch_variant"`
233
234 // list of source files that should not be used to build the C/C++ module.
235 // This is most useful in the arch/multilib variants to remove non-common files
236 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700237
238 // list of module-specific flags that will be used for C and C++ compiles.
239 Cflags []string `android:"arch_variant"`
240
241 // list of module-specific flags that will be used for C++ compiles
242 Cppflags []string `android:"arch_variant"`
243
244 // list of module-specific flags that will be used for C compiles
245 Conlyflags []string `android:"arch_variant"`
246
247 // list of module-specific flags that will be used for .S compiles
248 Asflags []string `android:"arch_variant"`
249
Colin Crossca860ac2016-01-04 14:34:37 -0800250 // list of module-specific flags that will be used for C and C++ compiles when
251 // compiling with clang
252 Clang_cflags []string `android:"arch_variant"`
253
254 // list of module-specific flags that will be used for .S compiles when
255 // compiling with clang
256 Clang_asflags []string `android:"arch_variant"`
257
Colin Cross7d5136f2015-05-11 13:39:40 -0700258 // list of module-specific flags that will be used for .y and .yy compiles
259 Yaccflags []string
260
Colin Cross7d5136f2015-05-11 13:39:40 -0700261 // the instruction set architecture to use to compile the C/C++
262 // module.
263 Instruction_set string `android:"arch_variant"`
264
265 // list of directories relative to the root of the source tree that will
266 // be added to the include path using -I.
267 // If possible, don't use this. If adding paths from the current directory use
268 // local_include_dirs, if adding paths from other modules use export_include_dirs in
269 // that module.
270 Include_dirs []string `android:"arch_variant"`
271
Colin Cross39d97f22015-09-14 12:30:50 -0700272 // list of files relative to the root of the source tree that will be included
273 // using -include.
274 // If possible, don't use this.
275 Include_files []string `android:"arch_variant"`
276
Colin Cross7d5136f2015-05-11 13:39:40 -0700277 // list of directories relative to the Blueprints file that will
278 // be added to the include path using -I
279 Local_include_dirs []string `android:"arch_variant"`
280
Colin Cross39d97f22015-09-14 12:30:50 -0700281 // list of files relative to the Blueprints file that will be included
282 // using -include.
283 // If possible, don't use this.
284 Local_include_files []string `android:"arch_variant"`
285
Dan Willemsenb40aab62016-04-20 14:21:14 -0700286 // list of generated sources to compile. These are the names of gensrcs or
287 // genrule modules.
288 Generated_sources []string `android:"arch_variant"`
289
290 // list of generated headers to add to the include path. These are the names
291 // of genrule modules.
292 Generated_headers []string `android:"arch_variant"`
293
Colin Crossca860ac2016-01-04 14:34:37 -0800294 // pass -frtti instead of -fno-rtti
295 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700296
Colin Crossca860ac2016-01-04 14:34:37 -0800297 Debug, Release struct {
298 // list of module-specific flags that will be used for C and C++ compiles in debug or
299 // release builds
300 Cflags []string `android:"arch_variant"`
301 } `android:"arch_variant"`
302}
Colin Cross7d5136f2015-05-11 13:39:40 -0700303
Colin Crossca860ac2016-01-04 14:34:37 -0800304type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700305 // list of modules whose object files should be linked into this module
306 // in their entirety. For static library modules, all of the .o files from the intermediate
307 // directory of the dependency will be linked into this modules .a file. For a shared library,
308 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700309 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700310
311 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700312 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700313
314 // list of modules that should be dynamically linked into this module.
315 Shared_libs []string `android:"arch_variant"`
316
Colin Crossca860ac2016-01-04 14:34:37 -0800317 // list of module-specific flags that will be used for all link steps
318 Ldflags []string `android:"arch_variant"`
319
320 // don't insert default compiler flags into asflags, cflags,
321 // cppflags, conlyflags, ldflags, or include_dirs
322 No_default_compiler_flags *bool
323
324 // list of system libraries that will be dynamically linked to
325 // shared library and executable modules. If unset, generally defaults to libc
326 // and libm. Set to [] to prevent linking against libc and libm.
327 System_shared_libs []string
328
Colin Cross7d5136f2015-05-11 13:39:40 -0700329 // allow the module to contain undefined symbols. By default,
330 // modules cannot contain undefined symbols that are not satisified by their immediate
331 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
332 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700333 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700334
Dan Willemsend67be222015-09-16 15:19:33 -0700335 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700336 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700337
Colin Cross7d5136f2015-05-11 13:39:40 -0700338 // -l arguments to pass to linker for host-provided shared libraries
339 Host_ldlibs []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800340}
Colin Cross7d5136f2015-05-11 13:39:40 -0700341
Colin Crossca860ac2016-01-04 14:34:37 -0800342type LibraryCompilerProperties struct {
343 Static struct {
344 Srcs []string `android:"arch_variant"`
345 Exclude_srcs []string `android:"arch_variant"`
346 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700347 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800348 Shared struct {
349 Srcs []string `android:"arch_variant"`
350 Exclude_srcs []string `android:"arch_variant"`
351 Cflags []string `android:"arch_variant"`
352 } `android:"arch_variant"`
353}
354
Colin Cross919281a2016-04-05 16:42:05 -0700355type FlagExporterProperties struct {
356 // list of directories relative to the Blueprints file that will
357 // be added to the include path using -I for any module that links against this module
358 Export_include_dirs []string `android:"arch_variant"`
359}
360
Colin Crossca860ac2016-01-04 14:34:37 -0800361type LibraryLinkerProperties struct {
362 Static struct {
363 Whole_static_libs []string `android:"arch_variant"`
364 Static_libs []string `android:"arch_variant"`
365 Shared_libs []string `android:"arch_variant"`
366 } `android:"arch_variant"`
367 Shared struct {
368 Whole_static_libs []string `android:"arch_variant"`
369 Static_libs []string `android:"arch_variant"`
370 Shared_libs []string `android:"arch_variant"`
371 } `android:"arch_variant"`
372
373 // local file name to pass to the linker as --version_script
374 Version_script *string `android:"arch_variant"`
375 // local file name to pass to the linker as -unexported_symbols_list
376 Unexported_symbols_list *string `android:"arch_variant"`
377 // local file name to pass to the linker as -force_symbols_not_weak_list
378 Force_symbols_not_weak_list *string `android:"arch_variant"`
379 // local file name to pass to the linker as -force_symbols_weak_list
380 Force_symbols_weak_list *string `android:"arch_variant"`
381
Colin Crossca860ac2016-01-04 14:34:37 -0800382 // don't link in crt_begin and crt_end. This flag should only be necessary for
383 // compiling crt or libc.
384 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800385
386 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800387}
388
389type BinaryLinkerProperties struct {
390 // compile executable with -static
391 Static_executable *bool
392
393 // set the name of the output
394 Stem string `android:"arch_variant"`
395
396 // append to the name of the output
397 Suffix string `android:"arch_variant"`
398
399 // if set, add an extra objcopy --prefix-symbols= step
400 Prefix_symbols string
401}
402
403type TestLinkerProperties struct {
404 // if set, build against the gtest library. Defaults to true.
405 Gtest bool
406
407 // Create a separate binary for each source file. Useful when there is
408 // global state that can not be torn down and reset between each test suite.
409 Test_per_src *bool
410}
411
Colin Cross81413472016-04-11 14:37:39 -0700412type ObjectLinkerProperties struct {
413 // names of other cc_object modules to link into this module using partial linking
414 Objs []string `android:"arch_variant"`
415}
416
Colin Crossca860ac2016-01-04 14:34:37 -0800417// Properties used to compile all C or C++ modules
418type BaseProperties struct {
419 // compile module with clang instead of gcc
420 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700421
422 // Minimum sdk version supported when compiling against the ndk
423 Sdk_version string
424
Colin Crossca860ac2016-01-04 14:34:37 -0800425 // don't insert default compiler flags into asflags, cflags,
426 // cppflags, conlyflags, ldflags, or include_dirs
427 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700428
429 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700430 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800431}
432
433type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700434 // install to a subdirectory of the default install path for the module
435 Relative_install_path string
436}
437
Colin Cross665dce92016-04-28 14:50:03 -0700438type StripProperties struct {
439 Strip struct {
440 None bool
441 Keep_symbols bool
442 }
443}
444
Colin Crossca860ac2016-01-04 14:34:37 -0800445type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700446 Native_coverage *bool
447 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700448 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800449}
450
Colin Crossca860ac2016-01-04 14:34:37 -0800451type ModuleContextIntf interface {
452 module() *Module
453 static() bool
454 staticBinary() bool
455 clang() bool
456 toolchain() Toolchain
457 noDefaultCompilerFlags() bool
458 sdk() bool
459 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700460 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800461}
462
463type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700464 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800465 ModuleContextIntf
466}
467
468type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700469 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800470 ModuleContextIntf
471}
472
473type Customizer interface {
474 CustomizeProperties(BaseModuleContext)
475 Properties() []interface{}
476}
477
478type feature interface {
479 begin(ctx BaseModuleContext)
480 deps(ctx BaseModuleContext, deps Deps) Deps
481 flags(ctx ModuleContext, flags Flags) Flags
482 props() []interface{}
483}
484
485type compiler interface {
486 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700487 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800488}
489
490type linker interface {
491 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700492 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700493 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800494}
495
496type installer interface {
497 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700498 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800499 inData() bool
500}
501
Colin Crossc99deeb2016-04-11 15:06:20 -0700502type dependencyTag struct {
503 blueprint.BaseDependencyTag
504 name string
505 library bool
506}
507
508var (
509 sharedDepTag = dependencyTag{name: "shared", library: true}
510 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
511 staticDepTag = dependencyTag{name: "static", library: true}
512 lateStaticDepTag = dependencyTag{name: "late static", library: true}
513 wholeStaticDepTag = dependencyTag{name: "whole static", library: true}
Dan Willemsenb40aab62016-04-20 14:21:14 -0700514 genSourceDepTag = dependencyTag{name: "gen source"}
515 genHeaderDepTag = dependencyTag{name: "gen header"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700516 objDepTag = dependencyTag{name: "obj"}
517 crtBeginDepTag = dependencyTag{name: "crtbegin"}
518 crtEndDepTag = dependencyTag{name: "crtend"}
519 reuseObjTag = dependencyTag{name: "reuse objects"}
520)
521
Colin Crossca860ac2016-01-04 14:34:37 -0800522// Module contains the properties and members used by all C/C++ module types, and implements
523// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
524// to construct the output file. Behavior can be customized with a Customizer interface
525type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700526 android.ModuleBase
527 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700528
Colin Crossca860ac2016-01-04 14:34:37 -0800529 Properties BaseProperties
530 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700531
Colin Crossca860ac2016-01-04 14:34:37 -0800532 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700533 hod android.HostOrDeviceSupported
534 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700535
Colin Crossca860ac2016-01-04 14:34:37 -0800536 // delegates, initialize before calling Init
537 customizer Customizer
538 features []feature
539 compiler compiler
540 linker linker
541 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700542 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800543 sanitize *sanitize
544
545 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700546
Colin Cross635c3b02016-05-18 15:37:25 -0700547 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800548
549 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700550}
551
Colin Crossca860ac2016-01-04 14:34:37 -0800552func (c *Module) Init() (blueprint.Module, []interface{}) {
553 props := []interface{}{&c.Properties, &c.unused}
554 if c.customizer != nil {
555 props = append(props, c.customizer.Properties()...)
556 }
557 if c.compiler != nil {
558 props = append(props, c.compiler.props()...)
559 }
560 if c.linker != nil {
561 props = append(props, c.linker.props()...)
562 }
563 if c.installer != nil {
564 props = append(props, c.installer.props()...)
565 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700566 if c.stl != nil {
567 props = append(props, c.stl.props()...)
568 }
Colin Cross16b23492016-01-06 14:41:07 -0800569 if c.sanitize != nil {
570 props = append(props, c.sanitize.props()...)
571 }
Colin Crossca860ac2016-01-04 14:34:37 -0800572 for _, feature := range c.features {
573 props = append(props, feature.props()...)
574 }
Colin Crossc472d572015-03-17 15:06:21 -0700575
Colin Cross635c3b02016-05-18 15:37:25 -0700576 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700577
Colin Cross635c3b02016-05-18 15:37:25 -0700578 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700579}
580
Colin Crossca860ac2016-01-04 14:34:37 -0800581type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700582 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800583 moduleContextImpl
584}
585
586type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700587 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800588 moduleContextImpl
589}
590
591type moduleContextImpl struct {
592 mod *Module
593 ctx BaseModuleContext
594}
595
596func (ctx *moduleContextImpl) module() *Module {
597 return ctx.mod
598}
599
600func (ctx *moduleContextImpl) clang() bool {
601 return ctx.mod.clang(ctx.ctx)
602}
603
604func (ctx *moduleContextImpl) toolchain() Toolchain {
605 return ctx.mod.toolchain(ctx.ctx)
606}
607
608func (ctx *moduleContextImpl) static() bool {
609 if ctx.mod.linker == nil {
610 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
611 }
612 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
613 return linker.static()
614 } else {
615 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
616 }
617}
618
619func (ctx *moduleContextImpl) staticBinary() bool {
620 if ctx.mod.linker == nil {
621 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
622 }
623 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
624 return linker.staticBinary()
625 } else {
626 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
627 }
628}
629
630func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
631 return Bool(ctx.mod.Properties.No_default_compiler_flags)
632}
633
634func (ctx *moduleContextImpl) sdk() bool {
635 return ctx.mod.Properties.Sdk_version != ""
636}
637
638func (ctx *moduleContextImpl) sdkVersion() string {
639 return ctx.mod.Properties.Sdk_version
640}
641
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700642func (ctx *moduleContextImpl) selectedStl() string {
643 if stl := ctx.mod.stl; stl != nil {
644 return stl.Properties.SelectedStl
645 }
646 return ""
647}
648
Colin Cross635c3b02016-05-18 15:37:25 -0700649func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800650 return &Module{
651 hod: hod,
652 multilib: multilib,
653 }
654}
655
Colin Cross635c3b02016-05-18 15:37:25 -0700656func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800657 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700658 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800659 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800660 return module
661}
662
Colin Cross635c3b02016-05-18 15:37:25 -0700663func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800664 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700665 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800666 moduleContextImpl: moduleContextImpl{
667 mod: c,
668 },
669 }
670 ctx.ctx = ctx
671
672 flags := Flags{
673 Toolchain: c.toolchain(ctx),
674 Clang: c.clang(ctx),
675 }
Colin Crossca860ac2016-01-04 14:34:37 -0800676 if c.compiler != nil {
677 flags = c.compiler.flags(ctx, flags)
678 }
679 if c.linker != nil {
680 flags = c.linker.flags(ctx, flags)
681 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700682 if c.stl != nil {
683 flags = c.stl.flags(ctx, flags)
684 }
Colin Cross16b23492016-01-06 14:41:07 -0800685 if c.sanitize != nil {
686 flags = c.sanitize.flags(ctx, flags)
687 }
Colin Crossca860ac2016-01-04 14:34:37 -0800688 for _, feature := range c.features {
689 flags = feature.flags(ctx, flags)
690 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800691 if ctx.Failed() {
692 return
693 }
694
Colin Crossca860ac2016-01-04 14:34:37 -0800695 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
696 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
697 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800698
Colin Crossca860ac2016-01-04 14:34:37 -0800699 // Optimization to reduce size of build.ninja
700 // Replace the long list of flags for each file with a module-local variable
701 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
702 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
703 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
704 flags.CFlags = []string{"$cflags"}
705 flags.CppFlags = []string{"$cppflags"}
706 flags.AsFlags = []string{"$asflags"}
707
Colin Crossc99deeb2016-04-11 15:06:20 -0700708 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800709 if ctx.Failed() {
710 return
711 }
712
Colin Cross28344522015-04-22 13:07:53 -0700713 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700714
Colin Cross635c3b02016-05-18 15:37:25 -0700715 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800716 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700717 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800718 if ctx.Failed() {
719 return
720 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800721 }
722
Colin Crossca860ac2016-01-04 14:34:37 -0800723 if c.linker != nil {
724 outputFile := c.linker.link(ctx, flags, deps, objFiles)
725 if ctx.Failed() {
726 return
727 }
Colin Cross635c3b02016-05-18 15:37:25 -0700728 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700729
Colin Crossc99deeb2016-04-11 15:06:20 -0700730 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800731 c.installer.install(ctx, outputFile)
732 if ctx.Failed() {
733 return
734 }
735 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700736 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800737}
738
Colin Crossca860ac2016-01-04 14:34:37 -0800739func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
740 if c.cachedToolchain == nil {
741 arch := ctx.Arch()
742 hod := ctx.HostOrDevice()
743 ht := ctx.HostType()
744 factory := toolchainFactories[hod][ht][arch.ArchType]
745 if factory == nil {
746 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
747 return nil
748 }
749 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800750 }
Colin Crossca860ac2016-01-04 14:34:37 -0800751 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800752}
753
Colin Crossca860ac2016-01-04 14:34:37 -0800754func (c *Module) begin(ctx BaseModuleContext) {
755 if c.compiler != nil {
756 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700757 }
Colin Crossca860ac2016-01-04 14:34:37 -0800758 if c.linker != nil {
759 c.linker.begin(ctx)
760 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700761 if c.stl != nil {
762 c.stl.begin(ctx)
763 }
Colin Cross16b23492016-01-06 14:41:07 -0800764 if c.sanitize != nil {
765 c.sanitize.begin(ctx)
766 }
Colin Crossca860ac2016-01-04 14:34:37 -0800767 for _, feature := range c.features {
768 feature.begin(ctx)
769 }
770}
771
Colin Crossc99deeb2016-04-11 15:06:20 -0700772func (c *Module) deps(ctx BaseModuleContext) Deps {
773 deps := Deps{}
774
775 if c.compiler != nil {
776 deps = c.compiler.deps(ctx, deps)
777 }
778 if c.linker != nil {
779 deps = c.linker.deps(ctx, deps)
780 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700781 if c.stl != nil {
782 deps = c.stl.deps(ctx, deps)
783 }
Colin Cross16b23492016-01-06 14:41:07 -0800784 if c.sanitize != nil {
785 deps = c.sanitize.deps(ctx, deps)
786 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700787 for _, feature := range c.features {
788 deps = feature.deps(ctx, deps)
789 }
790
791 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
792 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
793 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
794 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
795 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
796
797 return deps
798}
799
Colin Cross635c3b02016-05-18 15:37:25 -0700800func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800801 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700802 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800803 moduleContextImpl: moduleContextImpl{
804 mod: c,
805 },
806 }
807 ctx.ctx = ctx
808
809 if c.customizer != nil {
810 c.customizer.CustomizeProperties(ctx)
811 }
812
813 c.begin(ctx)
814
Colin Crossc99deeb2016-04-11 15:06:20 -0700815 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800816
Colin Crossc99deeb2016-04-11 15:06:20 -0700817 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
818
819 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
820 deps.WholeStaticLibs...)
821
822 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticDepTag,
823 deps.StaticLibs...)
824
825 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
826 deps.LateStaticLibs...)
827
828 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedDepTag,
829 deps.SharedLibs...)
830
831 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
832 deps.LateSharedLibs...)
833
Dan Willemsenb40aab62016-04-20 14:21:14 -0700834 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
835 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
836
Colin Crossc99deeb2016-04-11 15:06:20 -0700837 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
838
839 if deps.CrtBegin != "" {
840 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800841 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700842 if deps.CrtEnd != "" {
843 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700844 }
Colin Cross6362e272015-10-29 15:25:03 -0700845}
Colin Cross21b9a242015-03-24 14:15:58 -0700846
Colin Cross635c3b02016-05-18 15:37:25 -0700847func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800848 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700849 c.depsMutator(ctx)
850 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800851}
852
Colin Crossca860ac2016-01-04 14:34:37 -0800853func (c *Module) clang(ctx BaseModuleContext) bool {
854 clang := Bool(c.Properties.Clang)
855
856 if c.Properties.Clang == nil {
857 if ctx.Host() {
858 clang = true
859 }
860
861 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
862 clang = true
863 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800864 }
Colin Cross28344522015-04-22 13:07:53 -0700865
Colin Crossca860ac2016-01-04 14:34:37 -0800866 if !c.toolchain(ctx).ClangSupported() {
867 clang = false
868 }
869
870 return clang
871}
872
Colin Crossc99deeb2016-04-11 15:06:20 -0700873// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700874func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800875 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800876
Colin Crossc99deeb2016-04-11 15:06:20 -0700877 ctx.VisitDirectDeps(func(m blueprint.Module) {
878 name := ctx.OtherModuleName(m)
879 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800880
Colin Cross635c3b02016-05-18 15:37:25 -0700881 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700882 if a == nil {
883 ctx.ModuleErrorf("module %q not an android module", name)
884 return
Colin Crossca860ac2016-01-04 14:34:37 -0800885 }
Colin Crossca860ac2016-01-04 14:34:37 -0800886
Colin Crossc99deeb2016-04-11 15:06:20 -0700887 c, _ := m.(*Module)
888 if c == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700889 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700890 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700891 case genSourceDepTag:
892 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
893 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
894 genRule.GeneratedSourceFiles()...)
895 } else {
896 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
897 }
898 case genHeaderDepTag:
899 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
900 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
901 genRule.GeneratedSourceFiles()...)
902 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700903 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700904 } else {
905 ctx.ModuleErrorf("module %q is not a genrule", name)
906 }
907 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800909 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700910 return
911 }
912
913 if !a.Enabled() {
914 ctx.ModuleErrorf("depends on disabled module %q", name)
915 return
916 }
917
918 if a.HostOrDevice() != ctx.HostOrDevice() {
919 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), name)
920 return
921 }
922
923 if !c.outputFile.Valid() {
924 ctx.ModuleErrorf("module %q missing output file", name)
925 return
926 }
927
928 if tag == reuseObjTag {
929 depPaths.ObjFiles = append(depPaths.ObjFiles,
930 c.compiler.(*libraryCompiler).reuseObjFiles...)
931 return
932 }
933
934 var cflags []string
935 if t, _ := tag.(dependencyTag); t.library {
936 if i, ok := c.linker.(exportedFlagsProducer); ok {
937 cflags = i.exportedFlags()
938 depPaths.Cflags = append(depPaths.Cflags, cflags...)
939 }
940 }
941
Colin Cross635c3b02016-05-18 15:37:25 -0700942 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700943
944 switch tag {
945 case sharedDepTag:
946 depPtr = &depPaths.SharedLibs
947 case lateSharedDepTag:
948 depPtr = &depPaths.LateSharedLibs
949 case staticDepTag:
950 depPtr = &depPaths.StaticLibs
951 case lateStaticDepTag:
952 depPtr = &depPaths.LateStaticLibs
953 case wholeStaticDepTag:
954 depPtr = &depPaths.WholeStaticLibs
955 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
956 staticLib, _ := c.linker.(*libraryLinker)
957 if staticLib == nil || !staticLib.static() {
958 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
959 return
960 }
961
962 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
963 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
964 for i := range missingDeps {
965 missingDeps[i] += postfix
966 }
967 ctx.AddMissingDependencies(missingDeps)
968 }
969 depPaths.WholeStaticLibObjFiles =
970 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
971 case objDepTag:
972 depPtr = &depPaths.ObjFiles
973 case crtBeginDepTag:
974 depPaths.CrtBegin = c.outputFile
975 case crtEndDepTag:
976 depPaths.CrtEnd = c.outputFile
977 default:
978 panic(fmt.Errorf("unknown dependency tag: %s", ctx.OtherModuleDependencyTag(m)))
979 }
980
981 if depPtr != nil {
982 *depPtr = append(*depPtr, c.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800983 }
984 })
985
986 return depPaths
987}
988
989func (c *Module) InstallInData() bool {
990 if c.installer == nil {
991 return false
992 }
993 return c.installer.inData()
994}
995
996// Compiler
997
998type baseCompiler struct {
999 Properties BaseCompilerProperties
1000}
1001
1002var _ compiler = (*baseCompiler)(nil)
1003
1004func (compiler *baseCompiler) props() []interface{} {
1005 return []interface{}{&compiler.Properties}
1006}
1007
Dan Willemsenb40aab62016-04-20 14:21:14 -07001008func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1009
1010func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1011 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1012 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1013
1014 return deps
1015}
Colin Crossca860ac2016-01-04 14:34:37 -08001016
1017// Create a Flags struct that collects the compile flags from global values,
1018// per-target values, module type values, and per-module Blueprints properties
1019func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1020 toolchain := ctx.toolchain()
1021
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001022 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1023 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1024 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1025 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1026
Colin Crossca860ac2016-01-04 14:34:37 -08001027 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1028 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1029 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1030 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1031 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1032
Colin Cross28344522015-04-22 13:07:53 -07001033 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001034 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1035 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001036 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001037 includeDirsToFlags(localIncludeDirs),
1038 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001039
Colin Cross635c3b02016-05-18 15:37:25 -07001040 rootIncludeFiles := android.PathsForSource(ctx, compiler.Properties.Include_files)
1041 localIncludeFiles := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -07001042
1043 flags.GlobalFlags = append(flags.GlobalFlags,
1044 includeFilesToFlags(rootIncludeFiles),
1045 includeFilesToFlags(localIncludeFiles))
1046
Colin Crossca860ac2016-01-04 14:34:37 -08001047 if !ctx.noDefaultCompilerFlags() {
1048 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001049 flags.GlobalFlags = append(flags.GlobalFlags,
1050 "${commonGlobalIncludes}",
1051 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001052 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001053 }
1054
1055 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001056 "-I" + android.PathForModuleSrc(ctx).String(),
1057 "-I" + android.PathForModuleOut(ctx).String(),
1058 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001059 }...)
1060 }
1061
Colin Crossca860ac2016-01-04 14:34:37 -08001062 instructionSet := compiler.Properties.Instruction_set
1063 if flags.RequiredInstructionSet != "" {
1064 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001065 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001066 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1067 if flags.Clang {
1068 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1069 }
1070 if err != nil {
1071 ctx.ModuleErrorf("%s", err)
1072 }
1073
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001074 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1075
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001076 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001077 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001078
Colin Cross97ba0732015-03-23 17:50:24 -07001079 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001080 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1081 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1082
Colin Cross97ba0732015-03-23 17:50:24 -07001083 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001084 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1085 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001086 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1087 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1088 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001089
1090 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001091 var gccPrefix string
1092 if !ctx.Darwin() {
1093 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1094 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001095
Colin Cross97ba0732015-03-23 17:50:24 -07001096 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1097 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1098 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001099 }
1100
Colin Crossca860ac2016-01-04 14:34:37 -08001101 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001102 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1103
Colin Cross97ba0732015-03-23 17:50:24 -07001104 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001105 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001106 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001107 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001108 toolchain.ClangCflags(),
1109 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001110 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001111
1112 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001113 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001114 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001115 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001116 toolchain.Cflags(),
1117 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001118 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001119 }
1120
Colin Cross7b66f152015-12-15 16:07:43 -08001121 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1122 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1123 }
1124
Colin Crossf6566ed2015-03-24 11:13:38 -07001125 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001126 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001127 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001128 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001129 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001130 }
1131 }
1132
Colin Cross97ba0732015-03-23 17:50:24 -07001133 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001134
Colin Cross97ba0732015-03-23 17:50:24 -07001135 if flags.Clang {
1136 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001137 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001138 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001139 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001140 }
1141
Colin Crossc4bde762015-11-23 16:11:30 -08001142 if flags.Clang {
1143 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1144 } else {
1145 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001146 }
1147
Colin Crossca860ac2016-01-04 14:34:37 -08001148 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001149 if ctx.Host() && !flags.Clang {
1150 // The host GCC doesn't support C++14 (and is deprecated, so likely
1151 // never will). Build these modules with C++11.
1152 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1153 } else {
1154 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1155 }
1156 }
1157
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001158 // We can enforce some rules more strictly in the code we own. strict
1159 // indicates if this is code that we can be stricter with. If we have
1160 // rules that we want to apply to *our* code (but maybe can't for
1161 // vendor/device specific things), we could extend this to be a ternary
1162 // value.
1163 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001164 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001165 strict = false
1166 }
1167
1168 // Can be used to make some annotations stricter for code we can fix
1169 // (such as when we mark functions as deprecated).
1170 if strict {
1171 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1172 }
1173
Colin Cross3f40fa42015-01-30 17:27:36 -08001174 return flags
1175}
1176
Colin Cross635c3b02016-05-18 15:37:25 -07001177func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001178 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001179 objFiles := compiler.compileObjs(ctx, flags, "",
1180 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1181 deps.GeneratedSources, deps.GeneratedHeaders)
1182
Colin Crossca860ac2016-01-04 14:34:37 -08001183 if ctx.Failed() {
1184 return nil
1185 }
1186
Colin Crossca860ac2016-01-04 14:34:37 -08001187 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001188}
1189
1190// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001191func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1192 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001193
Colin Crossca860ac2016-01-04 14:34:37 -08001194 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001195
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001196 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001197 inputFiles = append(inputFiles, extraSrcs...)
1198 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1199
1200 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001201 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001202
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001203 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001204}
1205
Colin Crossca860ac2016-01-04 14:34:37 -08001206// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1207type baseLinker struct {
1208 Properties BaseLinkerProperties
1209 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001210 VariantIsShared bool `blueprint:"mutated"`
1211 VariantIsStatic bool `blueprint:"mutated"`
1212 VariantIsStaticBinary bool `blueprint:"mutated"`
1213 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001214 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001215}
1216
Dan Willemsend30e6102016-03-30 17:35:50 -07001217func (linker *baseLinker) begin(ctx BaseModuleContext) {
1218 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001219 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001220 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001221 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001222 }
1223}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001224
Colin Crossca860ac2016-01-04 14:34:37 -08001225func (linker *baseLinker) props() []interface{} {
1226 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001227}
1228
Colin Crossca860ac2016-01-04 14:34:37 -08001229func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1230 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1231 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1232 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001233
Colin Cross74d1ec02015-04-28 13:30:13 -07001234 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001235 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001236 }
1237
Colin Crossf6566ed2015-03-24 11:13:38 -07001238 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001239 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001240 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1241 if !Bool(linker.Properties.No_libgcc) {
1242 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001243 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001244
Colin Crossca860ac2016-01-04 14:34:37 -08001245 if !linker.static() {
1246 if linker.Properties.System_shared_libs != nil {
1247 deps.LateSharedLibs = append(deps.LateSharedLibs,
1248 linker.Properties.System_shared_libs...)
1249 } else if !ctx.sdk() {
1250 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1251 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001252 }
Colin Cross577f6e42015-03-27 18:23:34 -07001253
Colin Crossca860ac2016-01-04 14:34:37 -08001254 if ctx.sdk() {
1255 version := ctx.sdkVersion()
1256 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001257 "ndk_libc."+version,
1258 "ndk_libm."+version,
1259 )
1260 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001261 }
1262
Colin Crossca860ac2016-01-04 14:34:37 -08001263 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001264}
1265
Colin Crossca860ac2016-01-04 14:34:37 -08001266func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1267 toolchain := ctx.toolchain()
1268
Colin Crossca860ac2016-01-04 14:34:37 -08001269 if !ctx.noDefaultCompilerFlags() {
1270 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1271 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1272 }
1273
1274 if flags.Clang {
1275 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1276 } else {
1277 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1278 }
1279
1280 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001281 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1282
Colin Crossca860ac2016-01-04 14:34:37 -08001283 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1284 }
1285 }
1286
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001287 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1288
Dan Willemsen00ced762016-05-10 17:31:21 -07001289 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1290
Dan Willemsend30e6102016-03-30 17:35:50 -07001291 if ctx.Host() && !linker.static() {
1292 rpath_prefix := `\$$ORIGIN/`
1293 if ctx.Darwin() {
1294 rpath_prefix = "@loader_path/"
1295 }
1296
Colin Crossc99deeb2016-04-11 15:06:20 -07001297 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001298 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1299 }
1300 }
1301
Dan Willemsene7174922016-03-30 17:33:52 -07001302 if flags.Clang {
1303 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1304 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001305 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1306 }
1307
1308 return flags
1309}
1310
1311func (linker *baseLinker) static() bool {
1312 return linker.dynamicProperties.VariantIsStatic
1313}
1314
1315func (linker *baseLinker) staticBinary() bool {
1316 return linker.dynamicProperties.VariantIsStaticBinary
1317}
1318
1319func (linker *baseLinker) setStatic(static bool) {
1320 linker.dynamicProperties.VariantIsStatic = static
1321}
1322
Colin Cross16b23492016-01-06 14:41:07 -08001323func (linker *baseLinker) isDependencyRoot() bool {
1324 return false
1325}
1326
Colin Crossca860ac2016-01-04 14:34:37 -08001327type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001328 // Returns true if the build options for the module have selected a static or shared build
1329 buildStatic() bool
1330 buildShared() bool
1331
1332 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001333 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001334
Colin Cross18b6dc52015-04-28 13:20:37 -07001335 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001336 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001337
1338 // Returns whether a module is a static binary
1339 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001340
1341 // Returns true for dependency roots (binaries)
1342 // TODO(ccross): also handle dlopenable libraries
1343 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001344}
1345
Colin Crossca860ac2016-01-04 14:34:37 -08001346type baseInstaller struct {
1347 Properties InstallerProperties
1348
1349 dir string
1350 dir64 string
1351 data bool
1352
Colin Cross635c3b02016-05-18 15:37:25 -07001353 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001354}
1355
1356var _ installer = (*baseInstaller)(nil)
1357
1358func (installer *baseInstaller) props() []interface{} {
1359 return []interface{}{&installer.Properties}
1360}
1361
Colin Cross635c3b02016-05-18 15:37:25 -07001362func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001363 subDir := installer.dir
1364 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1365 subDir = installer.dir64
1366 }
Colin Cross635c3b02016-05-18 15:37:25 -07001367 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001368 installer.path = ctx.InstallFile(dir, file)
1369}
1370
1371func (installer *baseInstaller) inData() bool {
1372 return installer.data
1373}
1374
Colin Cross3f40fa42015-01-30 17:27:36 -08001375//
1376// Combined static+shared libraries
1377//
1378
Colin Cross919281a2016-04-05 16:42:05 -07001379type flagExporter struct {
1380 Properties FlagExporterProperties
1381
1382 flags []string
1383}
1384
1385func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001386 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1387 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001388}
1389
1390func (f *flagExporter) reexportFlags(flags []string) {
1391 f.flags = append(f.flags, flags...)
1392}
1393
1394func (f *flagExporter) exportedFlags() []string {
1395 return f.flags
1396}
1397
1398type exportedFlagsProducer interface {
1399 exportedFlags() []string
1400}
1401
1402var _ exportedFlagsProducer = (*flagExporter)(nil)
1403
Colin Crossca860ac2016-01-04 14:34:37 -08001404type libraryCompiler struct {
1405 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001406
Colin Crossca860ac2016-01-04 14:34:37 -08001407 linker *libraryLinker
1408 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001409
Colin Crossca860ac2016-01-04 14:34:37 -08001410 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001411 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001412}
1413
Colin Crossca860ac2016-01-04 14:34:37 -08001414var _ compiler = (*libraryCompiler)(nil)
1415
1416func (library *libraryCompiler) props() []interface{} {
1417 props := library.baseCompiler.props()
1418 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001419}
1420
Colin Crossca860ac2016-01-04 14:34:37 -08001421func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1422 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001423
Dan Willemsen490fd492015-11-24 17:53:15 -08001424 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1425 // all code is position independent, and then those warnings get promoted to
1426 // errors.
Colin Cross635c3b02016-05-18 15:37:25 -07001427 if ctx.HostType() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001428 flags.CFlags = append(flags.CFlags, "-fPIC")
1429 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001430
Colin Crossca860ac2016-01-04 14:34:37 -08001431 if library.linker.static() {
1432 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001433 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001434 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001435 }
1436
Colin Crossca860ac2016-01-04 14:34:37 -08001437 return flags
1438}
1439
Colin Cross635c3b02016-05-18 15:37:25 -07001440func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1441 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001442
Dan Willemsenb40aab62016-04-20 14:21:14 -07001443 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001444 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001445
1446 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001447 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001448 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1449 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001450 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001451 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001452 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1453 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001454 }
1455
1456 return objFiles
1457}
1458
1459type libraryLinker struct {
1460 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001461 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001462 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001463
1464 Properties LibraryLinkerProperties
1465
1466 dynamicProperties struct {
1467 BuildStatic bool `blueprint:"mutated"`
1468 BuildShared bool `blueprint:"mutated"`
1469 }
1470
Colin Crossca860ac2016-01-04 14:34:37 -08001471 // If we're used as a whole_static_lib, our missing dependencies need
1472 // to be given
1473 wholeStaticMissingDeps []string
1474
1475 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001476 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001477}
1478
1479var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001480
1481func (library *libraryLinker) props() []interface{} {
1482 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001483 return append(props,
1484 &library.Properties,
1485 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001486 &library.flagExporter.Properties,
1487 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001488}
1489
1490func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1491 flags = library.baseLinker.flags(ctx, flags)
1492
1493 flags.Nocrt = Bool(library.Properties.Nocrt)
1494
1495 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001496 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001497 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1498 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001499 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001500 sharedFlag = "-shared"
1501 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001502 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001503 flags.LdFlags = append(flags.LdFlags,
1504 "-nostdlib",
1505 "-Wl,--gc-sections",
1506 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001507 }
Colin Cross97ba0732015-03-23 17:50:24 -07001508
Colin Cross0af4b842015-04-30 16:36:18 -07001509 if ctx.Darwin() {
1510 flags.LdFlags = append(flags.LdFlags,
1511 "-dynamiclib",
1512 "-single_module",
1513 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001514 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001515 )
1516 } else {
1517 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001518 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001519 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001520 )
1521 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001522 }
Colin Cross97ba0732015-03-23 17:50:24 -07001523
1524 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001525}
1526
Colin Crossca860ac2016-01-04 14:34:37 -08001527func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1528 deps = library.baseLinker.deps(ctx, deps)
1529 if library.static() {
1530 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1531 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1532 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1533 } else {
1534 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1535 if !ctx.sdk() {
1536 deps.CrtBegin = "crtbegin_so"
1537 deps.CrtEnd = "crtend_so"
1538 } else {
1539 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1540 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1541 }
1542 }
1543 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1544 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1545 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1546 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001547
Colin Crossca860ac2016-01-04 14:34:37 -08001548 return deps
1549}
Colin Cross3f40fa42015-01-30 17:27:36 -08001550
Colin Crossca860ac2016-01-04 14:34:37 -08001551func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001552 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001553
Colin Cross635c3b02016-05-18 15:37:25 -07001554 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001555 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001556
Colin Cross635c3b02016-05-18 15:37:25 -07001557 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001558 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001559
Colin Cross0af4b842015-04-30 16:36:18 -07001560 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001561 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001562 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001563 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001564 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001565
Colin Crossca860ac2016-01-04 14:34:37 -08001566 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001567
1568 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001569
1570 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001571}
1572
Colin Crossca860ac2016-01-04 14:34:37 -08001573func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001574 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001575
Colin Cross635c3b02016-05-18 15:37:25 -07001576 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001577
Colin Cross635c3b02016-05-18 15:37:25 -07001578 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1579 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1580 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1581 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001582 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001583 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001584 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001585 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001586 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001587 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001588 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1589 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001590 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001591 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1592 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001593 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001594 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1595 }
1596 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001597 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001598 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1599 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001600 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001601 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001602 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001603 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001604 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001605 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001606 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001607 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001608 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001609 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001610 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001611 }
Colin Crossaee540a2015-07-06 17:48:31 -07001612 }
1613
Colin Cross665dce92016-04-28 14:50:03 -07001614 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001615 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001616 ret := outputFile
1617
1618 builderFlags := flagsToBuilderFlags(flags)
1619
1620 if library.stripper.needsStrip(ctx) {
1621 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001622 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001623 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1624 }
1625
Colin Crossca860ac2016-01-04 14:34:37 -08001626 sharedLibs := deps.SharedLibs
1627 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001628
Colin Crossca860ac2016-01-04 14:34:37 -08001629 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1630 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001631 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001632
Colin Cross665dce92016-04-28 14:50:03 -07001633 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001634}
1635
Colin Crossca860ac2016-01-04 14:34:37 -08001636func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001637 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001638
Colin Crossc99deeb2016-04-11 15:06:20 -07001639 objFiles = append(objFiles, deps.ObjFiles...)
1640
Colin Cross635c3b02016-05-18 15:37:25 -07001641 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001642 if library.static() {
1643 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001644 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001645 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001646 }
1647
Colin Cross919281a2016-04-05 16:42:05 -07001648 library.exportIncludes(ctx, "-I")
1649 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001650
1651 return out
1652}
1653
1654func (library *libraryLinker) buildStatic() bool {
1655 return library.dynamicProperties.BuildStatic
1656}
1657
1658func (library *libraryLinker) buildShared() bool {
1659 return library.dynamicProperties.BuildShared
1660}
1661
1662func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1663 return library.wholeStaticMissingDeps
1664}
1665
Colin Crossc99deeb2016-04-11 15:06:20 -07001666func (library *libraryLinker) installable() bool {
1667 return !library.static()
1668}
1669
Colin Crossca860ac2016-01-04 14:34:37 -08001670type libraryInstaller struct {
1671 baseInstaller
1672
Colin Cross30d5f512016-05-03 18:02:42 -07001673 linker *libraryLinker
1674 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001675}
1676
Colin Cross635c3b02016-05-18 15:37:25 -07001677func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001678 if !library.linker.static() {
1679 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001680 }
1681}
1682
Colin Cross30d5f512016-05-03 18:02:42 -07001683func (library *libraryInstaller) inData() bool {
1684 return library.baseInstaller.inData() || library.sanitize.inData()
1685}
1686
Colin Cross635c3b02016-05-18 15:37:25 -07001687func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1688 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001689
Colin Crossca860ac2016-01-04 14:34:37 -08001690 linker := &libraryLinker{}
1691 linker.dynamicProperties.BuildShared = shared
1692 linker.dynamicProperties.BuildStatic = static
1693 module.linker = linker
1694
1695 module.compiler = &libraryCompiler{
1696 linker: linker,
1697 }
1698 module.installer = &libraryInstaller{
1699 baseInstaller: baseInstaller{
1700 dir: "lib",
1701 dir64: "lib64",
1702 },
Colin Cross30d5f512016-05-03 18:02:42 -07001703 linker: linker,
1704 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001705 }
1706
Colin Crossca860ac2016-01-04 14:34:37 -08001707 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001708}
1709
Colin Crossca860ac2016-01-04 14:34:37 -08001710func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001711 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001712 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001713}
1714
Colin Cross3f40fa42015-01-30 17:27:36 -08001715//
1716// Objects (for crt*.o)
1717//
1718
Colin Crossca860ac2016-01-04 14:34:37 -08001719type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001720 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001721}
1722
Colin Crossca860ac2016-01-04 14:34:37 -08001723func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001724 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001725 module.compiler = &baseCompiler{}
1726 module.linker = &objectLinker{}
1727 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001728}
1729
Colin Cross81413472016-04-11 14:37:39 -07001730func (object *objectLinker) props() []interface{} {
1731 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001732}
1733
Colin Crossca860ac2016-01-04 14:34:37 -08001734func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001735
Colin Cross81413472016-04-11 14:37:39 -07001736func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1737 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001738 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001739}
1740
Colin Crossca860ac2016-01-04 14:34:37 -08001741func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001742 if flags.Clang {
1743 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1744 } else {
1745 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1746 }
1747
Colin Crossca860ac2016-01-04 14:34:37 -08001748 return flags
1749}
1750
1751func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001752 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001753
Colin Cross97ba0732015-03-23 17:50:24 -07001754 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001755
Colin Cross635c3b02016-05-18 15:37:25 -07001756 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001757 if len(objFiles) == 1 {
1758 outputFile = objFiles[0]
1759 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001760 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001761 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001762 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001763 }
1764
Colin Cross3f40fa42015-01-30 17:27:36 -08001765 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001766 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001767}
1768
Colin Crossc99deeb2016-04-11 15:06:20 -07001769func (*objectLinker) installable() bool {
1770 return false
1771}
1772
Colin Cross3f40fa42015-01-30 17:27:36 -08001773//
1774// Executables
1775//
1776
Colin Crossca860ac2016-01-04 14:34:37 -08001777type binaryLinker struct {
1778 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001779 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001780
Colin Crossca860ac2016-01-04 14:34:37 -08001781 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001782
Colin Cross635c3b02016-05-18 15:37:25 -07001783 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001784}
1785
Colin Crossca860ac2016-01-04 14:34:37 -08001786var _ linker = (*binaryLinker)(nil)
1787
1788func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001789 return append(binary.baseLinker.props(),
1790 &binary.Properties,
1791 &binary.stripper.StripProperties)
1792
Colin Cross3f40fa42015-01-30 17:27:36 -08001793}
1794
Colin Crossca860ac2016-01-04 14:34:37 -08001795func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001796 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001797}
1798
Colin Crossca860ac2016-01-04 14:34:37 -08001799func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001800 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001801}
1802
Colin Crossca860ac2016-01-04 14:34:37 -08001803func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001804 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001805 if binary.Properties.Stem != "" {
1806 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001807 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001808
Colin Crossca860ac2016-01-04 14:34:37 -08001809 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001810}
1811
Colin Crossca860ac2016-01-04 14:34:37 -08001812func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1813 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001814 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001815 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001816 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001817 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001818 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001819 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001820 }
Colin Crossca860ac2016-01-04 14:34:37 -08001821 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001822 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001823 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001824 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001825 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001826 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001827 }
Colin Crossca860ac2016-01-04 14:34:37 -08001828 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001829 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001830
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001831 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001832 if inList("libc++_static", deps.StaticLibs) {
1833 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001834 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001835 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1836 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1837 // move them to the beginning of deps.LateStaticLibs
1838 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001839 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001840 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001841 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001842 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001843 }
Colin Crossca860ac2016-01-04 14:34:37 -08001844
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001845 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001846 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1847 "from static libs or set static_executable: true")
1848 }
1849 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001850}
1851
Colin Crossc99deeb2016-04-11 15:06:20 -07001852func (*binaryLinker) installable() bool {
1853 return true
1854}
1855
Colin Cross16b23492016-01-06 14:41:07 -08001856func (binary *binaryLinker) isDependencyRoot() bool {
1857 return true
1858}
1859
Colin Cross635c3b02016-05-18 15:37:25 -07001860func NewBinary(hod android.HostOrDeviceSupported) *Module {
1861 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001862 module.compiler = &baseCompiler{}
1863 module.linker = &binaryLinker{}
1864 module.installer = &baseInstaller{
1865 dir: "bin",
1866 }
1867 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001868}
1869
Colin Crossca860ac2016-01-04 14:34:37 -08001870func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001871 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001872 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001873}
1874
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001875func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1876 binary.baseLinker.begin(ctx)
1877
1878 static := Bool(binary.Properties.Static_executable)
1879 if ctx.Host() {
Colin Cross635c3b02016-05-18 15:37:25 -07001880 if ctx.HostType() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001881 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1882 static = true
1883 }
1884 } else {
1885 // Static executables are not supported on Darwin or Windows
1886 static = false
1887 }
Colin Cross0af4b842015-04-30 16:36:18 -07001888 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001889 if static {
1890 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001891 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001892 }
1893}
1894
Colin Crossca860ac2016-01-04 14:34:37 -08001895func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1896 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001897
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001898 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001899 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross635c3b02016-05-18 15:37:25 -07001900 if ctx.HostType() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001901 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1902 }
1903 }
1904
1905 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1906 // all code is position independent, and then those warnings get promoted to
1907 // errors.
Colin Cross635c3b02016-05-18 15:37:25 -07001908 if ctx.HostType() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001909 flags.CFlags = append(flags.CFlags, "-fpie")
1910 }
Colin Cross97ba0732015-03-23 17:50:24 -07001911
Colin Crossf6566ed2015-03-24 11:13:38 -07001912 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001913 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001914 // Clang driver needs -static to create static executable.
1915 // However, bionic/linker uses -shared to overwrite.
1916 // Linker for x86 targets does not allow coexistance of -static and -shared,
1917 // so we add -static only if -shared is not used.
1918 if !inList("-shared", flags.LdFlags) {
1919 flags.LdFlags = append(flags.LdFlags, "-static")
1920 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001921
Colin Crossed4cf0b2015-03-26 14:43:45 -07001922 flags.LdFlags = append(flags.LdFlags,
1923 "-nostdlib",
1924 "-Bstatic",
1925 "-Wl,--gc-sections",
1926 )
1927
1928 } else {
Colin Cross16b23492016-01-06 14:41:07 -08001929 if flags.DynamicLinker == "" {
1930 flags.DynamicLinker = "/system/bin/linker"
1931 if flags.Toolchain.Is64Bit() {
1932 flags.DynamicLinker += "64"
1933 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001934 }
1935
1936 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001937 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001938 "-nostdlib",
1939 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001940 "-Wl,--gc-sections",
1941 "-Wl,-z,nocopyreloc",
1942 )
1943 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001944 } else {
1945 if binary.staticBinary() {
1946 flags.LdFlags = append(flags.LdFlags, "-static")
1947 }
1948 if ctx.Darwin() {
1949 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
1950 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001951 }
1952
Colin Cross97ba0732015-03-23 17:50:24 -07001953 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001954}
1955
Colin Crossca860ac2016-01-04 14:34:37 -08001956func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001957 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001958
Colin Cross665dce92016-04-28 14:50:03 -07001959 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001960 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001961 ret := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001962 if ctx.HostOrDevice().Host() {
Colin Cross635c3b02016-05-18 15:37:25 -07001963 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001964 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001965
Colin Cross635c3b02016-05-18 15:37:25 -07001966 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001967
Colin Crossca860ac2016-01-04 14:34:37 -08001968 sharedLibs := deps.SharedLibs
1969 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1970
Colin Cross16b23492016-01-06 14:41:07 -08001971 if flags.DynamicLinker != "" {
1972 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
1973 }
1974
Colin Cross665dce92016-04-28 14:50:03 -07001975 builderFlags := flagsToBuilderFlags(flags)
1976
1977 if binary.stripper.needsStrip(ctx) {
1978 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001979 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001980 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1981 }
1982
1983 if binary.Properties.Prefix_symbols != "" {
1984 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001985 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001986 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1987 flagsToBuilderFlags(flags), afterPrefixSymbols)
1988 }
1989
Colin Crossca860ac2016-01-04 14:34:37 -08001990 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001991 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07001992 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001993
1994 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001995}
Colin Cross3f40fa42015-01-30 17:27:36 -08001996
Colin Cross635c3b02016-05-18 15:37:25 -07001997func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08001998 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001999}
2000
Colin Cross665dce92016-04-28 14:50:03 -07002001type stripper struct {
2002 StripProperties StripProperties
2003}
2004
2005func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2006 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2007}
2008
Colin Cross635c3b02016-05-18 15:37:25 -07002009func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002010 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002011 if ctx.Darwin() {
2012 TransformDarwinStrip(ctx, in, out)
2013 } else {
2014 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2015 // TODO(ccross): don't add gnu debuglink for user builds
2016 flags.stripAddGnuDebuglink = true
2017 TransformStrip(ctx, in, out, flags)
2018 }
Colin Cross665dce92016-04-28 14:50:03 -07002019}
2020
Colin Cross635c3b02016-05-18 15:37:25 -07002021func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002022 if m, ok := mctx.Module().(*Module); ok {
2023 if test, ok := m.linker.(*testLinker); ok {
2024 if Bool(test.Properties.Test_per_src) {
2025 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2026 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2027 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2028 }
2029 tests := mctx.CreateLocalVariations(testNames...)
2030 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2031 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2032 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2033 }
Colin Cross6002e052015-09-16 16:00:08 -07002034 }
2035 }
2036 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002037}
2038
Colin Crossca860ac2016-01-04 14:34:37 -08002039type testLinker struct {
2040 binaryLinker
2041 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002042}
2043
Dan Willemsend30e6102016-03-30 17:35:50 -07002044func (test *testLinker) begin(ctx BaseModuleContext) {
2045 test.binaryLinker.begin(ctx)
2046
2047 runpath := "../../lib"
2048 if ctx.toolchain().Is64Bit() {
2049 runpath += "64"
2050 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002051 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002052}
2053
Colin Crossca860ac2016-01-04 14:34:37 -08002054func (test *testLinker) props() []interface{} {
2055 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002056}
2057
Colin Crossca860ac2016-01-04 14:34:37 -08002058func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2059 flags = test.binaryLinker.flags(ctx, flags)
2060
2061 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002062 return flags
2063 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002064
Colin Cross97ba0732015-03-23 17:50:24 -07002065 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002066 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002067 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002068
Dan Willemsen4a946832016-05-13 14:13:01 -07002069 switch ctx.HostType() {
Colin Cross635c3b02016-05-18 15:37:25 -07002070 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002071 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002072 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002073 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2074 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002075 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002076 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2077 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002078 }
2079 } else {
2080 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002081 }
2082
Colin Cross21b9a242015-03-24 14:15:58 -07002083 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002084}
2085
Colin Crossca860ac2016-01-04 14:34:37 -08002086func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2087 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002088 if ctx.sdk() && ctx.Device() {
2089 switch ctx.selectedStl() {
2090 case "ndk_libc++_shared", "ndk_libc++_static":
2091 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2092 case "ndk_libgnustl_static":
2093 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2094 default:
2095 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2096 }
2097 } else {
2098 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2099 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002100 }
Colin Crossca860ac2016-01-04 14:34:37 -08002101 deps = test.binaryLinker.deps(ctx, deps)
2102 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002103}
2104
Colin Crossca860ac2016-01-04 14:34:37 -08002105type testInstaller struct {
2106 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002107}
2108
Colin Cross635c3b02016-05-18 15:37:25 -07002109func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002110 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2111 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2112 installer.baseInstaller.install(ctx, file)
2113}
2114
Colin Cross635c3b02016-05-18 15:37:25 -07002115func NewTest(hod android.HostOrDeviceSupported) *Module {
2116 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002117 module.compiler = &baseCompiler{}
2118 linker := &testLinker{}
2119 linker.Properties.Gtest = true
2120 module.linker = linker
2121 module.installer = &testInstaller{
2122 baseInstaller: baseInstaller{
2123 dir: "nativetest",
2124 dir64: "nativetest64",
2125 data: true,
2126 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002127 }
Colin Crossca860ac2016-01-04 14:34:37 -08002128 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002129}
2130
Colin Crossca860ac2016-01-04 14:34:37 -08002131func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002132 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002133 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002134}
2135
Colin Crossca860ac2016-01-04 14:34:37 -08002136type benchmarkLinker struct {
2137 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002138}
2139
Colin Crossca860ac2016-01-04 14:34:37 -08002140func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2141 deps = benchmark.binaryLinker.deps(ctx, deps)
2142 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2143 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002144}
2145
Colin Cross635c3b02016-05-18 15:37:25 -07002146func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2147 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002148 module.compiler = &baseCompiler{}
2149 module.linker = &benchmarkLinker{}
2150 module.installer = &baseInstaller{
2151 dir: "nativetest",
2152 dir64: "nativetest64",
2153 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002154 }
Colin Crossca860ac2016-01-04 14:34:37 -08002155 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002156}
2157
Colin Crossca860ac2016-01-04 14:34:37 -08002158func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002159 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002160 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002161}
2162
Colin Cross3f40fa42015-01-30 17:27:36 -08002163//
2164// Static library
2165//
2166
Colin Crossca860ac2016-01-04 14:34:37 -08002167func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002168 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002169 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002170}
2171
2172//
2173// Shared libraries
2174//
2175
Colin Crossca860ac2016-01-04 14:34:37 -08002176func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002177 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002178 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002179}
2180
2181//
2182// Host static library
2183//
2184
Colin Crossca860ac2016-01-04 14:34:37 -08002185func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002186 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002187 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002188}
2189
2190//
2191// Host Shared libraries
2192//
2193
Colin Crossca860ac2016-01-04 14:34:37 -08002194func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002195 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002196 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002197}
2198
2199//
2200// Host Binaries
2201//
2202
Colin Crossca860ac2016-01-04 14:34:37 -08002203func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002204 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002205 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002206}
2207
2208//
Colin Cross1f8f2342015-03-26 16:09:47 -07002209// Host Tests
2210//
2211
Colin Crossca860ac2016-01-04 14:34:37 -08002212func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002213 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002214 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002215}
2216
2217//
Colin Cross2ba19d92015-05-07 15:44:20 -07002218// Host Benchmarks
2219//
2220
Colin Crossca860ac2016-01-04 14:34:37 -08002221func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002222 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002223 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002224}
2225
2226//
Colin Crosscfad1192015-11-02 16:43:11 -08002227// Defaults
2228//
Colin Crossca860ac2016-01-04 14:34:37 -08002229type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002230 android.ModuleBase
2231 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002232}
2233
Colin Cross635c3b02016-05-18 15:37:25 -07002234func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002235}
2236
Colin Crossca860ac2016-01-04 14:34:37 -08002237func defaultsFactory() (blueprint.Module, []interface{}) {
2238 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002239
2240 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002241 &BaseProperties{},
2242 &BaseCompilerProperties{},
2243 &BaseLinkerProperties{},
2244 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002245 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002246 &LibraryLinkerProperties{},
2247 &BinaryLinkerProperties{},
2248 &TestLinkerProperties{},
2249 &UnusedProperties{},
2250 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002251 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002252 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002253 }
2254
Colin Cross635c3b02016-05-18 15:37:25 -07002255 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2256 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002257
Colin Cross635c3b02016-05-18 15:37:25 -07002258 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002259}
2260
2261//
Colin Cross3f40fa42015-01-30 17:27:36 -08002262// Device libraries shipped with gcc
2263//
2264
Colin Crossca860ac2016-01-04 14:34:37 -08002265type toolchainLibraryLinker struct {
2266 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002267}
2268
Colin Crossca860ac2016-01-04 14:34:37 -08002269var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2270
2271func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002272 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002273 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002274}
2275
Colin Crossca860ac2016-01-04 14:34:37 -08002276func (*toolchainLibraryLinker) buildStatic() bool {
2277 return true
2278}
Colin Cross3f40fa42015-01-30 17:27:36 -08002279
Colin Crossca860ac2016-01-04 14:34:37 -08002280func (*toolchainLibraryLinker) buildShared() bool {
2281 return false
2282}
2283
2284func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002285 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002286 module.compiler = &baseCompiler{}
2287 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002288 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002289 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002290}
2291
Colin Crossca860ac2016-01-04 14:34:37 -08002292func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002293 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002294
2295 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002296 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002297
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002298 if flags.Clang {
2299 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2300 }
2301
Colin Crossca860ac2016-01-04 14:34:37 -08002302 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002303
2304 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002305
Colin Crossca860ac2016-01-04 14:34:37 -08002306 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002307}
2308
Colin Crossc99deeb2016-04-11 15:06:20 -07002309func (*toolchainLibraryLinker) installable() bool {
2310 return false
2311}
2312
Dan Albertbe961682015-03-18 23:38:50 -07002313// NDK prebuilt libraries.
2314//
2315// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2316// either (with the exception of the shared STLs, which are installed to the app's directory rather
2317// than to the system image).
2318
Colin Cross635c3b02016-05-18 15:37:25 -07002319func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002320 suffix := ""
2321 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2322 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002323 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002324 suffix = "64"
2325 }
Colin Cross635c3b02016-05-18 15:37:25 -07002326 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002327 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002328}
2329
Colin Cross635c3b02016-05-18 15:37:25 -07002330func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2331 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002332
2333 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2334 // We want to translate to just NAME.EXT
2335 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2336 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002337 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002338}
2339
Colin Crossca860ac2016-01-04 14:34:37 -08002340type ndkPrebuiltObjectLinker struct {
2341 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002342}
2343
Colin Crossca860ac2016-01-04 14:34:37 -08002344func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002345 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002346 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002347}
2348
Colin Crossca860ac2016-01-04 14:34:37 -08002349func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002350 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002351 module.linker = &ndkPrebuiltObjectLinker{}
2352 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002353}
2354
Colin Crossca860ac2016-01-04 14:34:37 -08002355func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002356 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002357 // A null build step, but it sets up the output path.
2358 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2359 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2360 }
2361
Colin Crossca860ac2016-01-04 14:34:37 -08002362 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002363}
2364
Colin Crossca860ac2016-01-04 14:34:37 -08002365type ndkPrebuiltLibraryLinker struct {
2366 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002367}
2368
Colin Crossca860ac2016-01-04 14:34:37 -08002369var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2370var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002371
Colin Crossca860ac2016-01-04 14:34:37 -08002372func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002373 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002374}
2375
Colin Crossca860ac2016-01-04 14:34:37 -08002376func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002377 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002378 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002379}
2380
Colin Crossca860ac2016-01-04 14:34:37 -08002381func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002382 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002383 linker := &ndkPrebuiltLibraryLinker{}
2384 linker.dynamicProperties.BuildShared = true
2385 module.linker = linker
2386 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002387}
2388
Colin Crossca860ac2016-01-04 14:34:37 -08002389func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002390 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002391 // A null build step, but it sets up the output path.
2392 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2393 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2394 }
2395
Colin Cross919281a2016-04-05 16:42:05 -07002396 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002397
Colin Crossca860ac2016-01-04 14:34:37 -08002398 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2399 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002400}
2401
2402// The NDK STLs are slightly different from the prebuilt system libraries:
2403// * Are not specific to each platform version.
2404// * The libraries are not in a predictable location for each STL.
2405
Colin Crossca860ac2016-01-04 14:34:37 -08002406type ndkPrebuiltStlLinker struct {
2407 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002408}
2409
Colin Crossca860ac2016-01-04 14:34:37 -08002410func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002411 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002412 linker := &ndkPrebuiltStlLinker{}
2413 linker.dynamicProperties.BuildShared = true
2414 module.linker = linker
2415 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002416}
2417
Colin Crossca860ac2016-01-04 14:34:37 -08002418func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002419 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002420 linker := &ndkPrebuiltStlLinker{}
2421 linker.dynamicProperties.BuildStatic = true
2422 module.linker = linker
2423 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002424}
2425
Colin Cross635c3b02016-05-18 15:37:25 -07002426func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002427 gccVersion := toolchain.GccVersion()
2428 var libDir string
2429 switch stl {
2430 case "libstlport":
2431 libDir = "cxx-stl/stlport/libs"
2432 case "libc++":
2433 libDir = "cxx-stl/llvm-libc++/libs"
2434 case "libgnustl":
2435 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2436 }
2437
2438 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002439 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002440 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002441 }
2442
2443 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002444 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002445}
2446
Colin Crossca860ac2016-01-04 14:34:37 -08002447func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002448 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002449 // A null build step, but it sets up the output path.
2450 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2451 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2452 }
2453
Colin Cross919281a2016-04-05 16:42:05 -07002454 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002455
2456 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002457 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002458 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002459 libExt = staticLibraryExtension
2460 }
2461
2462 stlName := strings.TrimSuffix(libName, "_shared")
2463 stlName = strings.TrimSuffix(stlName, "_static")
2464 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002465 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002466}
2467
Colin Cross635c3b02016-05-18 15:37:25 -07002468func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002469 if m, ok := mctx.Module().(*Module); ok {
2470 if m.linker != nil {
2471 if linker, ok := m.linker.(baseLinkerInterface); ok {
2472 var modules []blueprint.Module
2473 if linker.buildStatic() && linker.buildShared() {
2474 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002475 static := modules[0].(*Module)
2476 shared := modules[1].(*Module)
2477
2478 static.linker.(baseLinkerInterface).setStatic(true)
2479 shared.linker.(baseLinkerInterface).setStatic(false)
2480
2481 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2482 sharedCompiler := shared.compiler.(*libraryCompiler)
2483 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2484 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2485 // Optimize out compiling common .o files twice for static+shared libraries
2486 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2487 sharedCompiler.baseCompiler.Properties.Srcs = nil
2488 }
2489 }
Colin Crossca860ac2016-01-04 14:34:37 -08002490 } else if linker.buildStatic() {
2491 modules = mctx.CreateLocalVariations("static")
2492 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2493 } else if linker.buildShared() {
2494 modules = mctx.CreateLocalVariations("shared")
2495 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2496 } else {
2497 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2498 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002499 }
2500 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002501 }
2502}
Colin Cross74d1ec02015-04-28 13:30:13 -07002503
2504// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2505// modifies the slice contents in place, and returns a subslice of the original slice
2506func lastUniqueElements(list []string) []string {
2507 totalSkip := 0
2508 for i := len(list) - 1; i >= totalSkip; i-- {
2509 skip := 0
2510 for j := i - 1; j >= totalSkip; j-- {
2511 if list[i] == list[j] {
2512 skip++
2513 } else {
2514 list[j+skip] = list[j]
2515 }
2516 }
2517 totalSkip += skip
2518 }
2519 return list[totalSkip:]
2520}
Colin Cross06a931b2015-10-28 17:23:31 -07002521
2522var Bool = proptools.Bool