blob: d5f17a84ac952d1fd63792904adc4182ef0b19ca [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 Crossca860ac2016-01-04 14:34:37 -0800430}
431
432type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700433 // install to a subdirectory of the default install path for the module
434 Relative_install_path string
435}
436
Colin Cross665dce92016-04-28 14:50:03 -0700437type StripProperties struct {
438 Strip struct {
439 None bool
440 Keep_symbols bool
441 }
442}
443
Colin Crossca860ac2016-01-04 14:34:37 -0800444type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700445 Native_coverage *bool
446 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700447 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800448}
449
Colin Crossca860ac2016-01-04 14:34:37 -0800450type ModuleContextIntf interface {
451 module() *Module
452 static() bool
453 staticBinary() bool
454 clang() bool
455 toolchain() Toolchain
456 noDefaultCompilerFlags() bool
457 sdk() bool
458 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700459 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800460}
461
462type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700463 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800464 ModuleContextIntf
465}
466
467type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700468 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800469 ModuleContextIntf
470}
471
472type Customizer interface {
473 CustomizeProperties(BaseModuleContext)
474 Properties() []interface{}
475}
476
477type feature interface {
478 begin(ctx BaseModuleContext)
479 deps(ctx BaseModuleContext, deps Deps) Deps
480 flags(ctx ModuleContext, flags Flags) Flags
481 props() []interface{}
482}
483
484type compiler interface {
485 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700486 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800487}
488
489type linker interface {
490 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700491 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700492 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800493}
494
495type installer interface {
496 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700497 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800498 inData() bool
499}
500
Colin Crossc99deeb2016-04-11 15:06:20 -0700501type dependencyTag struct {
502 blueprint.BaseDependencyTag
503 name string
504 library bool
505}
506
507var (
508 sharedDepTag = dependencyTag{name: "shared", library: true}
509 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
510 staticDepTag = dependencyTag{name: "static", library: true}
511 lateStaticDepTag = dependencyTag{name: "late static", library: true}
512 wholeStaticDepTag = dependencyTag{name: "whole static", library: true}
Dan Willemsenb40aab62016-04-20 14:21:14 -0700513 genSourceDepTag = dependencyTag{name: "gen source"}
514 genHeaderDepTag = dependencyTag{name: "gen header"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700515 objDepTag = dependencyTag{name: "obj"}
516 crtBeginDepTag = dependencyTag{name: "crtbegin"}
517 crtEndDepTag = dependencyTag{name: "crtend"}
518 reuseObjTag = dependencyTag{name: "reuse objects"}
519)
520
Colin Crossca860ac2016-01-04 14:34:37 -0800521// Module contains the properties and members used by all C/C++ module types, and implements
522// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
523// to construct the output file. Behavior can be customized with a Customizer interface
524type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700525 android.ModuleBase
526 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700527
Colin Crossca860ac2016-01-04 14:34:37 -0800528 Properties BaseProperties
529 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700530
Colin Crossca860ac2016-01-04 14:34:37 -0800531 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700532 hod android.HostOrDeviceSupported
533 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700534
Colin Crossca860ac2016-01-04 14:34:37 -0800535 // delegates, initialize before calling Init
536 customizer Customizer
537 features []feature
538 compiler compiler
539 linker linker
540 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700541 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800542 sanitize *sanitize
543
544 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700545
Colin Cross635c3b02016-05-18 15:37:25 -0700546 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800547
548 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700549}
550
Colin Crossca860ac2016-01-04 14:34:37 -0800551func (c *Module) Init() (blueprint.Module, []interface{}) {
552 props := []interface{}{&c.Properties, &c.unused}
553 if c.customizer != nil {
554 props = append(props, c.customizer.Properties()...)
555 }
556 if c.compiler != nil {
557 props = append(props, c.compiler.props()...)
558 }
559 if c.linker != nil {
560 props = append(props, c.linker.props()...)
561 }
562 if c.installer != nil {
563 props = append(props, c.installer.props()...)
564 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700565 if c.stl != nil {
566 props = append(props, c.stl.props()...)
567 }
Colin Cross16b23492016-01-06 14:41:07 -0800568 if c.sanitize != nil {
569 props = append(props, c.sanitize.props()...)
570 }
Colin Crossca860ac2016-01-04 14:34:37 -0800571 for _, feature := range c.features {
572 props = append(props, feature.props()...)
573 }
Colin Crossc472d572015-03-17 15:06:21 -0700574
Colin Cross635c3b02016-05-18 15:37:25 -0700575 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700576
Colin Cross635c3b02016-05-18 15:37:25 -0700577 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700578}
579
Colin Crossca860ac2016-01-04 14:34:37 -0800580type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700581 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800582 moduleContextImpl
583}
584
585type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700586 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800587 moduleContextImpl
588}
589
590type moduleContextImpl struct {
591 mod *Module
592 ctx BaseModuleContext
593}
594
595func (ctx *moduleContextImpl) module() *Module {
596 return ctx.mod
597}
598
599func (ctx *moduleContextImpl) clang() bool {
600 return ctx.mod.clang(ctx.ctx)
601}
602
603func (ctx *moduleContextImpl) toolchain() Toolchain {
604 return ctx.mod.toolchain(ctx.ctx)
605}
606
607func (ctx *moduleContextImpl) static() bool {
608 if ctx.mod.linker == nil {
609 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
610 }
611 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
612 return linker.static()
613 } else {
614 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
615 }
616}
617
618func (ctx *moduleContextImpl) staticBinary() bool {
619 if ctx.mod.linker == nil {
620 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
621 }
622 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
623 return linker.staticBinary()
624 } else {
625 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
626 }
627}
628
629func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
630 return Bool(ctx.mod.Properties.No_default_compiler_flags)
631}
632
633func (ctx *moduleContextImpl) sdk() bool {
634 return ctx.mod.Properties.Sdk_version != ""
635}
636
637func (ctx *moduleContextImpl) sdkVersion() string {
638 return ctx.mod.Properties.Sdk_version
639}
640
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700641func (ctx *moduleContextImpl) selectedStl() string {
642 if stl := ctx.mod.stl; stl != nil {
643 return stl.Properties.SelectedStl
644 }
645 return ""
646}
647
Colin Cross635c3b02016-05-18 15:37:25 -0700648func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800649 return &Module{
650 hod: hod,
651 multilib: multilib,
652 }
653}
654
Colin Cross635c3b02016-05-18 15:37:25 -0700655func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800656 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700657 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800658 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800659 return module
660}
661
Colin Cross635c3b02016-05-18 15:37:25 -0700662func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800663 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700664 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800665 moduleContextImpl: moduleContextImpl{
666 mod: c,
667 },
668 }
669 ctx.ctx = ctx
670
671 flags := Flags{
672 Toolchain: c.toolchain(ctx),
673 Clang: c.clang(ctx),
674 }
Colin Crossca860ac2016-01-04 14:34:37 -0800675 if c.compiler != nil {
676 flags = c.compiler.flags(ctx, flags)
677 }
678 if c.linker != nil {
679 flags = c.linker.flags(ctx, flags)
680 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700681 if c.stl != nil {
682 flags = c.stl.flags(ctx, flags)
683 }
Colin Cross16b23492016-01-06 14:41:07 -0800684 if c.sanitize != nil {
685 flags = c.sanitize.flags(ctx, flags)
686 }
Colin Crossca860ac2016-01-04 14:34:37 -0800687 for _, feature := range c.features {
688 flags = feature.flags(ctx, flags)
689 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800690 if ctx.Failed() {
691 return
692 }
693
Colin Crossca860ac2016-01-04 14:34:37 -0800694 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
695 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
696 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800697
Colin Crossca860ac2016-01-04 14:34:37 -0800698 // Optimization to reduce size of build.ninja
699 // Replace the long list of flags for each file with a module-local variable
700 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
701 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
702 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
703 flags.CFlags = []string{"$cflags"}
704 flags.CppFlags = []string{"$cppflags"}
705 flags.AsFlags = []string{"$asflags"}
706
Colin Crossc99deeb2016-04-11 15:06:20 -0700707 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800708 if ctx.Failed() {
709 return
710 }
711
Colin Cross28344522015-04-22 13:07:53 -0700712 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700713
Colin Cross635c3b02016-05-18 15:37:25 -0700714 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800715 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700716 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800717 if ctx.Failed() {
718 return
719 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800720 }
721
Colin Crossca860ac2016-01-04 14:34:37 -0800722 if c.linker != nil {
723 outputFile := c.linker.link(ctx, flags, deps, objFiles)
724 if ctx.Failed() {
725 return
726 }
Colin Cross635c3b02016-05-18 15:37:25 -0700727 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700728
Colin Crossc99deeb2016-04-11 15:06:20 -0700729 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800730 c.installer.install(ctx, outputFile)
731 if ctx.Failed() {
732 return
733 }
734 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700735 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800736}
737
Colin Crossca860ac2016-01-04 14:34:37 -0800738func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
739 if c.cachedToolchain == nil {
740 arch := ctx.Arch()
741 hod := ctx.HostOrDevice()
742 ht := ctx.HostType()
743 factory := toolchainFactories[hod][ht][arch.ArchType]
744 if factory == nil {
745 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
746 return nil
747 }
748 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800749 }
Colin Crossca860ac2016-01-04 14:34:37 -0800750 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800751}
752
Colin Crossca860ac2016-01-04 14:34:37 -0800753func (c *Module) begin(ctx BaseModuleContext) {
754 if c.compiler != nil {
755 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700756 }
Colin Crossca860ac2016-01-04 14:34:37 -0800757 if c.linker != nil {
758 c.linker.begin(ctx)
759 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700760 if c.stl != nil {
761 c.stl.begin(ctx)
762 }
Colin Cross16b23492016-01-06 14:41:07 -0800763 if c.sanitize != nil {
764 c.sanitize.begin(ctx)
765 }
Colin Crossca860ac2016-01-04 14:34:37 -0800766 for _, feature := range c.features {
767 feature.begin(ctx)
768 }
769}
770
Colin Crossc99deeb2016-04-11 15:06:20 -0700771func (c *Module) deps(ctx BaseModuleContext) Deps {
772 deps := Deps{}
773
774 if c.compiler != nil {
775 deps = c.compiler.deps(ctx, deps)
776 }
777 if c.linker != nil {
778 deps = c.linker.deps(ctx, deps)
779 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700780 if c.stl != nil {
781 deps = c.stl.deps(ctx, deps)
782 }
Colin Cross16b23492016-01-06 14:41:07 -0800783 if c.sanitize != nil {
784 deps = c.sanitize.deps(ctx, deps)
785 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700786 for _, feature := range c.features {
787 deps = feature.deps(ctx, deps)
788 }
789
790 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
791 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
792 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
793 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
794 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
795
796 return deps
797}
798
Colin Cross635c3b02016-05-18 15:37:25 -0700799func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800800 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700801 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800802 moduleContextImpl: moduleContextImpl{
803 mod: c,
804 },
805 }
806 ctx.ctx = ctx
807
808 if c.customizer != nil {
809 c.customizer.CustomizeProperties(ctx)
810 }
811
812 c.begin(ctx)
813
Colin Crossc99deeb2016-04-11 15:06:20 -0700814 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800815
Colin Crossc99deeb2016-04-11 15:06:20 -0700816 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
817
818 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
819 deps.WholeStaticLibs...)
820
821 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticDepTag,
822 deps.StaticLibs...)
823
824 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
825 deps.LateStaticLibs...)
826
827 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedDepTag,
828 deps.SharedLibs...)
829
830 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
831 deps.LateSharedLibs...)
832
Dan Willemsenb40aab62016-04-20 14:21:14 -0700833 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
834 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
835
Colin Crossc99deeb2016-04-11 15:06:20 -0700836 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
837
838 if deps.CrtBegin != "" {
839 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800840 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700841 if deps.CrtEnd != "" {
842 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700843 }
Colin Cross6362e272015-10-29 15:25:03 -0700844}
Colin Cross21b9a242015-03-24 14:15:58 -0700845
Colin Cross635c3b02016-05-18 15:37:25 -0700846func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800847 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700848 c.depsMutator(ctx)
849 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800850}
851
Colin Crossca860ac2016-01-04 14:34:37 -0800852func (c *Module) clang(ctx BaseModuleContext) bool {
853 clang := Bool(c.Properties.Clang)
854
855 if c.Properties.Clang == nil {
856 if ctx.Host() {
857 clang = true
858 }
859
860 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
861 clang = true
862 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800863 }
Colin Cross28344522015-04-22 13:07:53 -0700864
Colin Crossca860ac2016-01-04 14:34:37 -0800865 if !c.toolchain(ctx).ClangSupported() {
866 clang = false
867 }
868
869 return clang
870}
871
Colin Crossc99deeb2016-04-11 15:06:20 -0700872// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700873func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800874 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800875
Colin Crossc99deeb2016-04-11 15:06:20 -0700876 ctx.VisitDirectDeps(func(m blueprint.Module) {
877 name := ctx.OtherModuleName(m)
878 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800879
Colin Cross635c3b02016-05-18 15:37:25 -0700880 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700881 if a == nil {
882 ctx.ModuleErrorf("module %q not an android module", name)
883 return
Colin Crossca860ac2016-01-04 14:34:37 -0800884 }
Colin Crossca860ac2016-01-04 14:34:37 -0800885
Colin Crossc99deeb2016-04-11 15:06:20 -0700886 c, _ := m.(*Module)
887 if c == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700888 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700889 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700890 case genSourceDepTag:
891 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
892 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
893 genRule.GeneratedSourceFiles()...)
894 } else {
895 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
896 }
897 case genHeaderDepTag:
898 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
899 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
900 genRule.GeneratedSourceFiles()...)
901 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700902 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700903 } else {
904 ctx.ModuleErrorf("module %q is not a genrule", name)
905 }
906 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700907 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800908 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700909 return
910 }
911
912 if !a.Enabled() {
913 ctx.ModuleErrorf("depends on disabled module %q", name)
914 return
915 }
916
917 if a.HostOrDevice() != ctx.HostOrDevice() {
918 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), name)
919 return
920 }
921
922 if !c.outputFile.Valid() {
923 ctx.ModuleErrorf("module %q missing output file", name)
924 return
925 }
926
927 if tag == reuseObjTag {
928 depPaths.ObjFiles = append(depPaths.ObjFiles,
929 c.compiler.(*libraryCompiler).reuseObjFiles...)
930 return
931 }
932
933 var cflags []string
934 if t, _ := tag.(dependencyTag); t.library {
935 if i, ok := c.linker.(exportedFlagsProducer); ok {
936 cflags = i.exportedFlags()
937 depPaths.Cflags = append(depPaths.Cflags, cflags...)
938 }
939 }
940
Colin Cross635c3b02016-05-18 15:37:25 -0700941 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700942
943 switch tag {
944 case sharedDepTag:
945 depPtr = &depPaths.SharedLibs
946 case lateSharedDepTag:
947 depPtr = &depPaths.LateSharedLibs
948 case staticDepTag:
949 depPtr = &depPaths.StaticLibs
950 case lateStaticDepTag:
951 depPtr = &depPaths.LateStaticLibs
952 case wholeStaticDepTag:
953 depPtr = &depPaths.WholeStaticLibs
954 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
955 staticLib, _ := c.linker.(*libraryLinker)
956 if staticLib == nil || !staticLib.static() {
957 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
958 return
959 }
960
961 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
962 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
963 for i := range missingDeps {
964 missingDeps[i] += postfix
965 }
966 ctx.AddMissingDependencies(missingDeps)
967 }
968 depPaths.WholeStaticLibObjFiles =
969 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
970 case objDepTag:
971 depPtr = &depPaths.ObjFiles
972 case crtBeginDepTag:
973 depPaths.CrtBegin = c.outputFile
974 case crtEndDepTag:
975 depPaths.CrtEnd = c.outputFile
976 default:
977 panic(fmt.Errorf("unknown dependency tag: %s", ctx.OtherModuleDependencyTag(m)))
978 }
979
980 if depPtr != nil {
981 *depPtr = append(*depPtr, c.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800982 }
983 })
984
985 return depPaths
986}
987
988func (c *Module) InstallInData() bool {
989 if c.installer == nil {
990 return false
991 }
992 return c.installer.inData()
993}
994
Colin Cross16b23492016-01-06 14:41:07 -0800995type appendVariantName interface {
996 appendVariantName(string)
997}
998
999func (c *Module) appendVariantName(name string) {
1000 if c.linker == nil {
1001 return
1002 }
1003
1004 if l, ok := c.linker.(appendVariantName); ok {
1005 l.appendVariantName(name)
1006 }
1007}
1008
Colin Crossca860ac2016-01-04 14:34:37 -08001009// Compiler
1010
1011type baseCompiler struct {
1012 Properties BaseCompilerProperties
1013}
1014
1015var _ compiler = (*baseCompiler)(nil)
1016
1017func (compiler *baseCompiler) props() []interface{} {
1018 return []interface{}{&compiler.Properties}
1019}
1020
Dan Willemsenb40aab62016-04-20 14:21:14 -07001021func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1022
1023func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1024 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1025 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1026
1027 return deps
1028}
Colin Crossca860ac2016-01-04 14:34:37 -08001029
1030// Create a Flags struct that collects the compile flags from global values,
1031// per-target values, module type values, and per-module Blueprints properties
1032func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1033 toolchain := ctx.toolchain()
1034
1035 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1036 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1037 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1038 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1039 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1040
Colin Cross28344522015-04-22 13:07:53 -07001041 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001042 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1043 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001044 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001045 includeDirsToFlags(localIncludeDirs),
1046 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001047
Colin Cross635c3b02016-05-18 15:37:25 -07001048 rootIncludeFiles := android.PathsForSource(ctx, compiler.Properties.Include_files)
1049 localIncludeFiles := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -07001050
1051 flags.GlobalFlags = append(flags.GlobalFlags,
1052 includeFilesToFlags(rootIncludeFiles),
1053 includeFilesToFlags(localIncludeFiles))
1054
Colin Crossca860ac2016-01-04 14:34:37 -08001055 if !ctx.noDefaultCompilerFlags() {
1056 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001057 flags.GlobalFlags = append(flags.GlobalFlags,
1058 "${commonGlobalIncludes}",
1059 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001060 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001061 }
1062
1063 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001064 "-I" + android.PathForModuleSrc(ctx).String(),
1065 "-I" + android.PathForModuleOut(ctx).String(),
1066 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001067 }...)
1068 }
1069
Colin Crossca860ac2016-01-04 14:34:37 -08001070 instructionSet := compiler.Properties.Instruction_set
1071 if flags.RequiredInstructionSet != "" {
1072 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001073 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001074 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1075 if flags.Clang {
1076 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1077 }
1078 if err != nil {
1079 ctx.ModuleErrorf("%s", err)
1080 }
1081
1082 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001083 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001084
Colin Cross97ba0732015-03-23 17:50:24 -07001085 if flags.Clang {
1086 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001087 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1088 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001089 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1090 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1091 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001092
1093 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001094 var gccPrefix string
1095 if !ctx.Darwin() {
1096 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1097 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001098
Colin Cross97ba0732015-03-23 17:50:24 -07001099 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1100 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1101 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001102 }
1103
Colin Crossca860ac2016-01-04 14:34:37 -08001104 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001105 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1106
Colin Cross97ba0732015-03-23 17:50:24 -07001107 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001108 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001109 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001110 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001111 toolchain.ClangCflags(),
1112 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001113 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001114
1115 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001116 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001117 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001118 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001119 toolchain.Cflags(),
1120 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001121 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001122 }
1123
Colin Cross7b66f152015-12-15 16:07:43 -08001124 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1125 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1126 }
1127
Colin Crossf6566ed2015-03-24 11:13:38 -07001128 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001129 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001130 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001131 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001132 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001133 }
1134 }
1135
Colin Cross97ba0732015-03-23 17:50:24 -07001136 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001137
Colin Cross97ba0732015-03-23 17:50:24 -07001138 if flags.Clang {
1139 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001140 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001141 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001142 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001143 }
1144
Colin Crossc4bde762015-11-23 16:11:30 -08001145 if flags.Clang {
1146 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1147 } else {
1148 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001149 }
1150
Colin Crossca860ac2016-01-04 14:34:37 -08001151 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001152 if ctx.Host() && !flags.Clang {
1153 // The host GCC doesn't support C++14 (and is deprecated, so likely
1154 // never will). Build these modules with C++11.
1155 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1156 } else {
1157 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1158 }
1159 }
1160
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001161 // We can enforce some rules more strictly in the code we own. strict
1162 // indicates if this is code that we can be stricter with. If we have
1163 // rules that we want to apply to *our* code (but maybe can't for
1164 // vendor/device specific things), we could extend this to be a ternary
1165 // value.
1166 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001167 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001168 strict = false
1169 }
1170
1171 // Can be used to make some annotations stricter for code we can fix
1172 // (such as when we mark functions as deprecated).
1173 if strict {
1174 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1175 }
1176
Colin Cross3f40fa42015-01-30 17:27:36 -08001177 return flags
1178}
1179
Colin Cross635c3b02016-05-18 15:37:25 -07001180func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001181 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001182 objFiles := compiler.compileObjs(ctx, flags, "",
1183 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1184 deps.GeneratedSources, deps.GeneratedHeaders)
1185
Colin Crossca860ac2016-01-04 14:34:37 -08001186 if ctx.Failed() {
1187 return nil
1188 }
1189
Colin Crossca860ac2016-01-04 14:34:37 -08001190 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001191}
1192
1193// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001194func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1195 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001196
Colin Crossca860ac2016-01-04 14:34:37 -08001197 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001198
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001199 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001200 inputFiles = append(inputFiles, extraSrcs...)
1201 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1202
1203 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001204 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001205
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001206 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001207}
1208
Colin Crossca860ac2016-01-04 14:34:37 -08001209// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1210type baseLinker struct {
1211 Properties BaseLinkerProperties
1212 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001213 VariantIsShared bool `blueprint:"mutated"`
1214 VariantIsStatic bool `blueprint:"mutated"`
1215 VariantIsStaticBinary bool `blueprint:"mutated"`
1216 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001217 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001218}
1219
Dan Willemsend30e6102016-03-30 17:35:50 -07001220func (linker *baseLinker) begin(ctx BaseModuleContext) {
1221 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001222 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001223 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001224 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001225 }
1226}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001227
Colin Crossca860ac2016-01-04 14:34:37 -08001228func (linker *baseLinker) props() []interface{} {
1229 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001230}
1231
Colin Crossca860ac2016-01-04 14:34:37 -08001232func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1233 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1234 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1235 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001236
Colin Cross74d1ec02015-04-28 13:30:13 -07001237 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001238 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001239 }
1240
Colin Crossf6566ed2015-03-24 11:13:38 -07001241 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001242 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001243 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1244 if !Bool(linker.Properties.No_libgcc) {
1245 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001246 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001247
Colin Crossca860ac2016-01-04 14:34:37 -08001248 if !linker.static() {
1249 if linker.Properties.System_shared_libs != nil {
1250 deps.LateSharedLibs = append(deps.LateSharedLibs,
1251 linker.Properties.System_shared_libs...)
1252 } else if !ctx.sdk() {
1253 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1254 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001255 }
Colin Cross577f6e42015-03-27 18:23:34 -07001256
Colin Crossca860ac2016-01-04 14:34:37 -08001257 if ctx.sdk() {
1258 version := ctx.sdkVersion()
1259 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001260 "ndk_libc."+version,
1261 "ndk_libm."+version,
1262 )
1263 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001264 }
1265
Colin Crossca860ac2016-01-04 14:34:37 -08001266 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001267}
1268
Colin Crossca860ac2016-01-04 14:34:37 -08001269func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1270 toolchain := ctx.toolchain()
1271
Colin Crossca860ac2016-01-04 14:34:37 -08001272 if !ctx.noDefaultCompilerFlags() {
1273 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1274 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1275 }
1276
1277 if flags.Clang {
1278 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1279 } else {
1280 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1281 }
1282
1283 if ctx.Host() {
1284 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1285 }
1286 }
1287
Dan Willemsen00ced762016-05-10 17:31:21 -07001288 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1289
Dan Willemsend30e6102016-03-30 17:35:50 -07001290 if ctx.Host() && !linker.static() {
1291 rpath_prefix := `\$$ORIGIN/`
1292 if ctx.Darwin() {
1293 rpath_prefix = "@loader_path/"
1294 }
1295
Colin Crossc99deeb2016-04-11 15:06:20 -07001296 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001297 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1298 }
1299 }
1300
Dan Willemsene7174922016-03-30 17:33:52 -07001301 if flags.Clang {
1302 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1303 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001304 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1305 }
1306
1307 return flags
1308}
1309
1310func (linker *baseLinker) static() bool {
1311 return linker.dynamicProperties.VariantIsStatic
1312}
1313
1314func (linker *baseLinker) staticBinary() bool {
1315 return linker.dynamicProperties.VariantIsStaticBinary
1316}
1317
1318func (linker *baseLinker) setStatic(static bool) {
1319 linker.dynamicProperties.VariantIsStatic = static
1320}
1321
Colin Cross16b23492016-01-06 14:41:07 -08001322func (linker *baseLinker) isDependencyRoot() bool {
1323 return false
1324}
1325
Colin Crossca860ac2016-01-04 14:34:37 -08001326type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001327 // Returns true if the build options for the module have selected a static or shared build
1328 buildStatic() bool
1329 buildShared() bool
1330
1331 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001332 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001333
Colin Cross18b6dc52015-04-28 13:20:37 -07001334 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001335 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001336
1337 // Returns whether a module is a static binary
1338 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001339
1340 // Returns true for dependency roots (binaries)
1341 // TODO(ccross): also handle dlopenable libraries
1342 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001343}
1344
Colin Crossca860ac2016-01-04 14:34:37 -08001345type baseInstaller struct {
1346 Properties InstallerProperties
1347
1348 dir string
1349 dir64 string
1350 data bool
1351
Colin Cross635c3b02016-05-18 15:37:25 -07001352 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001353}
1354
1355var _ installer = (*baseInstaller)(nil)
1356
1357func (installer *baseInstaller) props() []interface{} {
1358 return []interface{}{&installer.Properties}
1359}
1360
Colin Cross635c3b02016-05-18 15:37:25 -07001361func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001362 subDir := installer.dir
1363 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1364 subDir = installer.dir64
1365 }
Colin Cross635c3b02016-05-18 15:37:25 -07001366 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001367 installer.path = ctx.InstallFile(dir, file)
1368}
1369
1370func (installer *baseInstaller) inData() bool {
1371 return installer.data
1372}
1373
Colin Cross3f40fa42015-01-30 17:27:36 -08001374//
1375// Combined static+shared libraries
1376//
1377
Colin Cross919281a2016-04-05 16:42:05 -07001378type flagExporter struct {
1379 Properties FlagExporterProperties
1380
1381 flags []string
1382}
1383
1384func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001385 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1386 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001387}
1388
1389func (f *flagExporter) reexportFlags(flags []string) {
1390 f.flags = append(f.flags, flags...)
1391}
1392
1393func (f *flagExporter) exportedFlags() []string {
1394 return f.flags
1395}
1396
1397type exportedFlagsProducer interface {
1398 exportedFlags() []string
1399}
1400
1401var _ exportedFlagsProducer = (*flagExporter)(nil)
1402
Colin Crossca860ac2016-01-04 14:34:37 -08001403type libraryCompiler struct {
1404 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001405
Colin Crossca860ac2016-01-04 14:34:37 -08001406 linker *libraryLinker
1407 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001408
Colin Crossca860ac2016-01-04 14:34:37 -08001409 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001410 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001411}
1412
Colin Crossca860ac2016-01-04 14:34:37 -08001413var _ compiler = (*libraryCompiler)(nil)
1414
1415func (library *libraryCompiler) props() []interface{} {
1416 props := library.baseCompiler.props()
1417 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001418}
1419
Colin Crossca860ac2016-01-04 14:34:37 -08001420func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1421 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001422
Dan Willemsen490fd492015-11-24 17:53:15 -08001423 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1424 // all code is position independent, and then those warnings get promoted to
1425 // errors.
Colin Cross635c3b02016-05-18 15:37:25 -07001426 if ctx.HostType() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001427 flags.CFlags = append(flags.CFlags, "-fPIC")
1428 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001429
Colin Crossca860ac2016-01-04 14:34:37 -08001430 if library.linker.static() {
1431 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001432 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001433 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001434 }
1435
Colin Crossca860ac2016-01-04 14:34:37 -08001436 return flags
1437}
1438
Colin Cross635c3b02016-05-18 15:37:25 -07001439func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1440 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001441
Dan Willemsenb40aab62016-04-20 14:21:14 -07001442 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001443 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001444
1445 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001446 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001447 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1448 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001449 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001450 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001451 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1452 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001453 }
1454
1455 return objFiles
1456}
1457
1458type libraryLinker struct {
1459 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001460 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001461 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001462
1463 Properties LibraryLinkerProperties
1464
1465 dynamicProperties struct {
1466 BuildStatic bool `blueprint:"mutated"`
1467 BuildShared bool `blueprint:"mutated"`
1468 }
1469
Colin Crossca860ac2016-01-04 14:34:37 -08001470 // If we're used as a whole_static_lib, our missing dependencies need
1471 // to be given
1472 wholeStaticMissingDeps []string
1473
1474 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001475 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001476}
1477
1478var _ linker = (*libraryLinker)(nil)
Colin Cross16b23492016-01-06 14:41:07 -08001479var _ appendVariantName = (*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 Cross16b23492016-01-06 14:41:07 -08001670func (library *libraryLinker) appendVariantName(variant string) {
1671 library.Properties.VariantName += variant
1672}
1673
Colin Crossca860ac2016-01-04 14:34:37 -08001674type libraryInstaller struct {
1675 baseInstaller
1676
Colin Cross30d5f512016-05-03 18:02:42 -07001677 linker *libraryLinker
1678 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001679}
1680
Colin Cross635c3b02016-05-18 15:37:25 -07001681func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001682 if !library.linker.static() {
1683 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001684 }
1685}
1686
Colin Cross30d5f512016-05-03 18:02:42 -07001687func (library *libraryInstaller) inData() bool {
1688 return library.baseInstaller.inData() || library.sanitize.inData()
1689}
1690
Colin Cross635c3b02016-05-18 15:37:25 -07001691func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1692 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001693
Colin Crossca860ac2016-01-04 14:34:37 -08001694 linker := &libraryLinker{}
1695 linker.dynamicProperties.BuildShared = shared
1696 linker.dynamicProperties.BuildStatic = static
1697 module.linker = linker
1698
1699 module.compiler = &libraryCompiler{
1700 linker: linker,
1701 }
1702 module.installer = &libraryInstaller{
1703 baseInstaller: baseInstaller{
1704 dir: "lib",
1705 dir64: "lib64",
1706 },
Colin Cross30d5f512016-05-03 18:02:42 -07001707 linker: linker,
1708 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001709 }
1710
Colin Crossca860ac2016-01-04 14:34:37 -08001711 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001712}
1713
Colin Crossca860ac2016-01-04 14:34:37 -08001714func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001715 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001716 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001717}
1718
Colin Cross3f40fa42015-01-30 17:27:36 -08001719//
1720// Objects (for crt*.o)
1721//
1722
Colin Crossca860ac2016-01-04 14:34:37 -08001723type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001724 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001725}
1726
Colin Crossca860ac2016-01-04 14:34:37 -08001727func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001728 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001729 module.compiler = &baseCompiler{}
1730 module.linker = &objectLinker{}
1731 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001732}
1733
Colin Cross81413472016-04-11 14:37:39 -07001734func (object *objectLinker) props() []interface{} {
1735 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001736}
1737
Colin Crossca860ac2016-01-04 14:34:37 -08001738func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001739
Colin Cross81413472016-04-11 14:37:39 -07001740func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1741 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001742 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001743}
1744
Colin Crossca860ac2016-01-04 14:34:37 -08001745func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001746 if flags.Clang {
1747 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1748 } else {
1749 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1750 }
1751
Colin Crossca860ac2016-01-04 14:34:37 -08001752 return flags
1753}
1754
1755func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001756 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001757
Colin Cross97ba0732015-03-23 17:50:24 -07001758 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001759
Colin Cross635c3b02016-05-18 15:37:25 -07001760 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001761 if len(objFiles) == 1 {
1762 outputFile = objFiles[0]
1763 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001764 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001765 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001766 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001767 }
1768
Colin Cross3f40fa42015-01-30 17:27:36 -08001769 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001770 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001771}
1772
Colin Crossc99deeb2016-04-11 15:06:20 -07001773func (*objectLinker) installable() bool {
1774 return false
1775}
1776
Colin Cross3f40fa42015-01-30 17:27:36 -08001777//
1778// Executables
1779//
1780
Colin Crossca860ac2016-01-04 14:34:37 -08001781type binaryLinker struct {
1782 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001783 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001784
Colin Crossca860ac2016-01-04 14:34:37 -08001785 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001786
Colin Cross635c3b02016-05-18 15:37:25 -07001787 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001788}
1789
Colin Crossca860ac2016-01-04 14:34:37 -08001790var _ linker = (*binaryLinker)(nil)
1791
1792func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001793 return append(binary.baseLinker.props(),
1794 &binary.Properties,
1795 &binary.stripper.StripProperties)
1796
Colin Cross3f40fa42015-01-30 17:27:36 -08001797}
1798
Colin Crossca860ac2016-01-04 14:34:37 -08001799func (binary *binaryLinker) buildStatic() 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) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001804 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001805}
1806
Colin Crossca860ac2016-01-04 14:34:37 -08001807func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001808 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001809 if binary.Properties.Stem != "" {
1810 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001811 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001812
Colin Crossca860ac2016-01-04 14:34:37 -08001813 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001814}
1815
Colin Crossca860ac2016-01-04 14:34:37 -08001816func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1817 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001818 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001819 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001820 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001821 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001822 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001823 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001824 }
Colin Crossca860ac2016-01-04 14:34:37 -08001825 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001826 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001827 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001828 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001829 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001830 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001831 }
Colin Crossca860ac2016-01-04 14:34:37 -08001832 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001833 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001834
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001835 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001836 if inList("libc++_static", deps.StaticLibs) {
1837 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001838 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001839 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1840 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1841 // move them to the beginning of deps.LateStaticLibs
1842 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001843 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001844 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001845 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001846 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001847 }
Colin Crossca860ac2016-01-04 14:34:37 -08001848
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001849 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001850 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1851 "from static libs or set static_executable: true")
1852 }
1853 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001854}
1855
Colin Crossc99deeb2016-04-11 15:06:20 -07001856func (*binaryLinker) installable() bool {
1857 return true
1858}
1859
Colin Cross16b23492016-01-06 14:41:07 -08001860func (binary *binaryLinker) isDependencyRoot() bool {
1861 return true
1862}
1863
Colin Cross635c3b02016-05-18 15:37:25 -07001864func NewBinary(hod android.HostOrDeviceSupported) *Module {
1865 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001866 module.compiler = &baseCompiler{}
1867 module.linker = &binaryLinker{}
1868 module.installer = &baseInstaller{
1869 dir: "bin",
1870 }
1871 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001872}
1873
Colin Crossca860ac2016-01-04 14:34:37 -08001874func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001875 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001876 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001877}
1878
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001879func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1880 binary.baseLinker.begin(ctx)
1881
1882 static := Bool(binary.Properties.Static_executable)
1883 if ctx.Host() {
Colin Cross635c3b02016-05-18 15:37:25 -07001884 if ctx.HostType() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001885 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1886 static = true
1887 }
1888 } else {
1889 // Static executables are not supported on Darwin or Windows
1890 static = false
1891 }
Colin Cross0af4b842015-04-30 16:36:18 -07001892 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001893 if static {
1894 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001895 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001896 }
1897}
1898
Colin Crossca860ac2016-01-04 14:34:37 -08001899func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1900 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001901
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001902 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001903 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross635c3b02016-05-18 15:37:25 -07001904 if ctx.HostType() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001905 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1906 }
1907 }
1908
1909 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1910 // all code is position independent, and then those warnings get promoted to
1911 // errors.
Colin Cross635c3b02016-05-18 15:37:25 -07001912 if ctx.HostType() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001913 flags.CFlags = append(flags.CFlags, "-fpie")
1914 }
Colin Cross97ba0732015-03-23 17:50:24 -07001915
Colin Crossf6566ed2015-03-24 11:13:38 -07001916 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001917 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001918 // Clang driver needs -static to create static executable.
1919 // However, bionic/linker uses -shared to overwrite.
1920 // Linker for x86 targets does not allow coexistance of -static and -shared,
1921 // so we add -static only if -shared is not used.
1922 if !inList("-shared", flags.LdFlags) {
1923 flags.LdFlags = append(flags.LdFlags, "-static")
1924 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001925
Colin Crossed4cf0b2015-03-26 14:43:45 -07001926 flags.LdFlags = append(flags.LdFlags,
1927 "-nostdlib",
1928 "-Bstatic",
1929 "-Wl,--gc-sections",
1930 )
1931
1932 } else {
Colin Cross16b23492016-01-06 14:41:07 -08001933 if flags.DynamicLinker == "" {
1934 flags.DynamicLinker = "/system/bin/linker"
1935 if flags.Toolchain.Is64Bit() {
1936 flags.DynamicLinker += "64"
1937 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001938 }
1939
1940 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001941 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001942 "-nostdlib",
1943 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001944 "-Wl,--gc-sections",
1945 "-Wl,-z,nocopyreloc",
1946 )
1947 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001948 } else {
1949 if binary.staticBinary() {
1950 flags.LdFlags = append(flags.LdFlags, "-static")
1951 }
1952 if ctx.Darwin() {
1953 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
1954 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001955 }
1956
Colin Cross97ba0732015-03-23 17:50:24 -07001957 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001958}
1959
Colin Crossca860ac2016-01-04 14:34:37 -08001960func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001961 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001962
Colin Cross665dce92016-04-28 14:50:03 -07001963 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001964 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001965 ret := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001966 if ctx.HostOrDevice().Host() {
Colin Cross635c3b02016-05-18 15:37:25 -07001967 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001968 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001969
Colin Cross635c3b02016-05-18 15:37:25 -07001970 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001971
Colin Crossca860ac2016-01-04 14:34:37 -08001972 sharedLibs := deps.SharedLibs
1973 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1974
Colin Cross16b23492016-01-06 14:41:07 -08001975 if flags.DynamicLinker != "" {
1976 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
1977 }
1978
Colin Cross665dce92016-04-28 14:50:03 -07001979 builderFlags := flagsToBuilderFlags(flags)
1980
1981 if binary.stripper.needsStrip(ctx) {
1982 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001983 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001984 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1985 }
1986
1987 if binary.Properties.Prefix_symbols != "" {
1988 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001989 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001990 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1991 flagsToBuilderFlags(flags), afterPrefixSymbols)
1992 }
1993
Colin Crossca860ac2016-01-04 14:34:37 -08001994 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001995 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07001996 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001997
1998 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001999}
Colin Cross3f40fa42015-01-30 17:27:36 -08002000
Colin Cross635c3b02016-05-18 15:37:25 -07002001func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002002 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002003}
2004
Colin Cross665dce92016-04-28 14:50:03 -07002005type stripper struct {
2006 StripProperties StripProperties
2007}
2008
2009func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2010 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2011}
2012
Colin Cross635c3b02016-05-18 15:37:25 -07002013func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002014 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002015 if ctx.Darwin() {
2016 TransformDarwinStrip(ctx, in, out)
2017 } else {
2018 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2019 // TODO(ccross): don't add gnu debuglink for user builds
2020 flags.stripAddGnuDebuglink = true
2021 TransformStrip(ctx, in, out, flags)
2022 }
Colin Cross665dce92016-04-28 14:50:03 -07002023}
2024
Colin Cross635c3b02016-05-18 15:37:25 -07002025func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002026 if m, ok := mctx.Module().(*Module); ok {
2027 if test, ok := m.linker.(*testLinker); ok {
2028 if Bool(test.Properties.Test_per_src) {
2029 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2030 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2031 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2032 }
2033 tests := mctx.CreateLocalVariations(testNames...)
2034 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2035 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2036 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2037 }
Colin Cross6002e052015-09-16 16:00:08 -07002038 }
2039 }
2040 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002041}
2042
Colin Crossca860ac2016-01-04 14:34:37 -08002043type testLinker struct {
2044 binaryLinker
2045 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002046}
2047
Dan Willemsend30e6102016-03-30 17:35:50 -07002048func (test *testLinker) begin(ctx BaseModuleContext) {
2049 test.binaryLinker.begin(ctx)
2050
2051 runpath := "../../lib"
2052 if ctx.toolchain().Is64Bit() {
2053 runpath += "64"
2054 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002055 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002056}
2057
Colin Crossca860ac2016-01-04 14:34:37 -08002058func (test *testLinker) props() []interface{} {
2059 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002060}
2061
Colin Crossca860ac2016-01-04 14:34:37 -08002062func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2063 flags = test.binaryLinker.flags(ctx, flags)
2064
2065 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002066 return flags
2067 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002068
Colin Cross97ba0732015-03-23 17:50:24 -07002069 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002070 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002071 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002072
Dan Willemsen4a946832016-05-13 14:13:01 -07002073 switch ctx.HostType() {
Colin Cross635c3b02016-05-18 15:37:25 -07002074 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002075 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002076 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002077 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2078 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002079 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002080 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2081 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002082 }
2083 } else {
2084 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002085 }
2086
Colin Cross21b9a242015-03-24 14:15:58 -07002087 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002088}
2089
Colin Crossca860ac2016-01-04 14:34:37 -08002090func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2091 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002092 if ctx.sdk() && ctx.Device() {
2093 switch ctx.selectedStl() {
2094 case "ndk_libc++_shared", "ndk_libc++_static":
2095 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2096 case "ndk_libgnustl_static":
2097 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2098 default:
2099 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2100 }
2101 } else {
2102 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2103 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002104 }
Colin Crossca860ac2016-01-04 14:34:37 -08002105 deps = test.binaryLinker.deps(ctx, deps)
2106 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002107}
2108
Colin Crossca860ac2016-01-04 14:34:37 -08002109type testInstaller struct {
2110 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002111}
2112
Colin Cross635c3b02016-05-18 15:37:25 -07002113func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002114 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2115 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2116 installer.baseInstaller.install(ctx, file)
2117}
2118
Colin Cross635c3b02016-05-18 15:37:25 -07002119func NewTest(hod android.HostOrDeviceSupported) *Module {
2120 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002121 module.compiler = &baseCompiler{}
2122 linker := &testLinker{}
2123 linker.Properties.Gtest = true
2124 module.linker = linker
2125 module.installer = &testInstaller{
2126 baseInstaller: baseInstaller{
2127 dir: "nativetest",
2128 dir64: "nativetest64",
2129 data: true,
2130 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002131 }
Colin Crossca860ac2016-01-04 14:34:37 -08002132 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002133}
2134
Colin Crossca860ac2016-01-04 14:34:37 -08002135func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002136 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002137 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002138}
2139
Colin Crossca860ac2016-01-04 14:34:37 -08002140type benchmarkLinker struct {
2141 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002142}
2143
Colin Crossca860ac2016-01-04 14:34:37 -08002144func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2145 deps = benchmark.binaryLinker.deps(ctx, deps)
2146 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2147 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002148}
2149
Colin Cross635c3b02016-05-18 15:37:25 -07002150func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2151 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002152 module.compiler = &baseCompiler{}
2153 module.linker = &benchmarkLinker{}
2154 module.installer = &baseInstaller{
2155 dir: "nativetest",
2156 dir64: "nativetest64",
2157 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002158 }
Colin Crossca860ac2016-01-04 14:34:37 -08002159 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002160}
2161
Colin Crossca860ac2016-01-04 14:34:37 -08002162func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002163 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002164 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002165}
2166
Colin Cross3f40fa42015-01-30 17:27:36 -08002167//
2168// Static library
2169//
2170
Colin Crossca860ac2016-01-04 14:34:37 -08002171func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002172 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002173 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002174}
2175
2176//
2177// Shared libraries
2178//
2179
Colin Crossca860ac2016-01-04 14:34:37 -08002180func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002181 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002182 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002183}
2184
2185//
2186// Host static library
2187//
2188
Colin Crossca860ac2016-01-04 14:34:37 -08002189func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002190 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002191 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002192}
2193
2194//
2195// Host Shared libraries
2196//
2197
Colin Crossca860ac2016-01-04 14:34:37 -08002198func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002199 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002200 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002201}
2202
2203//
2204// Host Binaries
2205//
2206
Colin Crossca860ac2016-01-04 14:34:37 -08002207func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002208 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002209 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002210}
2211
2212//
Colin Cross1f8f2342015-03-26 16:09:47 -07002213// Host Tests
2214//
2215
Colin Crossca860ac2016-01-04 14:34:37 -08002216func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002217 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002218 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002219}
2220
2221//
Colin Cross2ba19d92015-05-07 15:44:20 -07002222// Host Benchmarks
2223//
2224
Colin Crossca860ac2016-01-04 14:34:37 -08002225func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002226 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002227 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002228}
2229
2230//
Colin Crosscfad1192015-11-02 16:43:11 -08002231// Defaults
2232//
Colin Crossca860ac2016-01-04 14:34:37 -08002233type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002234 android.ModuleBase
2235 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002236}
2237
Colin Cross635c3b02016-05-18 15:37:25 -07002238func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002239}
2240
Colin Crossca860ac2016-01-04 14:34:37 -08002241func defaultsFactory() (blueprint.Module, []interface{}) {
2242 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002243
2244 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002245 &BaseProperties{},
2246 &BaseCompilerProperties{},
2247 &BaseLinkerProperties{},
2248 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002249 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002250 &LibraryLinkerProperties{},
2251 &BinaryLinkerProperties{},
2252 &TestLinkerProperties{},
2253 &UnusedProperties{},
2254 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002255 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002256 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002257 }
2258
Colin Cross635c3b02016-05-18 15:37:25 -07002259 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2260 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002261
Colin Cross635c3b02016-05-18 15:37:25 -07002262 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002263}
2264
2265//
Colin Cross3f40fa42015-01-30 17:27:36 -08002266// Device libraries shipped with gcc
2267//
2268
Colin Crossca860ac2016-01-04 14:34:37 -08002269type toolchainLibraryLinker struct {
2270 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002271}
2272
Colin Crossca860ac2016-01-04 14:34:37 -08002273var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2274
2275func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002276 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002277 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002278}
2279
Colin Crossca860ac2016-01-04 14:34:37 -08002280func (*toolchainLibraryLinker) buildStatic() bool {
2281 return true
2282}
Colin Cross3f40fa42015-01-30 17:27:36 -08002283
Colin Crossca860ac2016-01-04 14:34:37 -08002284func (*toolchainLibraryLinker) buildShared() bool {
2285 return false
2286}
2287
2288func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002289 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002290 module.compiler = &baseCompiler{}
2291 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002292 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002293 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002294}
2295
Colin Crossca860ac2016-01-04 14:34:37 -08002296func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002297 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002298
2299 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002300 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002301
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002302 if flags.Clang {
2303 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2304 }
2305
Colin Crossca860ac2016-01-04 14:34:37 -08002306 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002307
2308 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002309
Colin Crossca860ac2016-01-04 14:34:37 -08002310 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002311}
2312
Colin Crossc99deeb2016-04-11 15:06:20 -07002313func (*toolchainLibraryLinker) installable() bool {
2314 return false
2315}
2316
Dan Albertbe961682015-03-18 23:38:50 -07002317// NDK prebuilt libraries.
2318//
2319// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2320// either (with the exception of the shared STLs, which are installed to the app's directory rather
2321// than to the system image).
2322
Colin Cross635c3b02016-05-18 15:37:25 -07002323func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002324 suffix := ""
2325 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2326 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002327 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002328 suffix = "64"
2329 }
Colin Cross635c3b02016-05-18 15:37:25 -07002330 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002331 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002332}
2333
Colin Cross635c3b02016-05-18 15:37:25 -07002334func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2335 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002336
2337 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2338 // We want to translate to just NAME.EXT
2339 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2340 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002341 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002342}
2343
Colin Crossca860ac2016-01-04 14:34:37 -08002344type ndkPrebuiltObjectLinker struct {
2345 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002346}
2347
Colin Crossca860ac2016-01-04 14:34:37 -08002348func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002349 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002350 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002351}
2352
Colin Crossca860ac2016-01-04 14:34:37 -08002353func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002354 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002355 module.linker = &ndkPrebuiltObjectLinker{}
2356 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002357}
2358
Colin Crossca860ac2016-01-04 14:34:37 -08002359func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002360 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002361 // A null build step, but it sets up the output path.
2362 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2363 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2364 }
2365
Colin Crossca860ac2016-01-04 14:34:37 -08002366 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002367}
2368
Colin Crossca860ac2016-01-04 14:34:37 -08002369type ndkPrebuiltLibraryLinker struct {
2370 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002371}
2372
Colin Crossca860ac2016-01-04 14:34:37 -08002373var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2374var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002375
Colin Crossca860ac2016-01-04 14:34:37 -08002376func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002377 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002378}
2379
Colin Crossca860ac2016-01-04 14:34:37 -08002380func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002381 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002382 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002383}
2384
Colin Crossca860ac2016-01-04 14:34:37 -08002385func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002386 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002387 linker := &ndkPrebuiltLibraryLinker{}
2388 linker.dynamicProperties.BuildShared = true
2389 module.linker = linker
2390 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002391}
2392
Colin Crossca860ac2016-01-04 14:34:37 -08002393func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002394 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002395 // A null build step, but it sets up the output path.
2396 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2397 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2398 }
2399
Colin Cross919281a2016-04-05 16:42:05 -07002400 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002401
Colin Crossca860ac2016-01-04 14:34:37 -08002402 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2403 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002404}
2405
2406// The NDK STLs are slightly different from the prebuilt system libraries:
2407// * Are not specific to each platform version.
2408// * The libraries are not in a predictable location for each STL.
2409
Colin Crossca860ac2016-01-04 14:34:37 -08002410type ndkPrebuiltStlLinker struct {
2411 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002412}
2413
Colin Crossca860ac2016-01-04 14:34:37 -08002414func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002415 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002416 linker := &ndkPrebuiltStlLinker{}
2417 linker.dynamicProperties.BuildShared = true
2418 module.linker = linker
2419 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002420}
2421
Colin Crossca860ac2016-01-04 14:34:37 -08002422func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002423 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002424 linker := &ndkPrebuiltStlLinker{}
2425 linker.dynamicProperties.BuildStatic = true
2426 module.linker = linker
2427 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002428}
2429
Colin Cross635c3b02016-05-18 15:37:25 -07002430func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002431 gccVersion := toolchain.GccVersion()
2432 var libDir string
2433 switch stl {
2434 case "libstlport":
2435 libDir = "cxx-stl/stlport/libs"
2436 case "libc++":
2437 libDir = "cxx-stl/llvm-libc++/libs"
2438 case "libgnustl":
2439 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2440 }
2441
2442 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002443 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002444 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002445 }
2446
2447 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002448 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002449}
2450
Colin Crossca860ac2016-01-04 14:34:37 -08002451func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002452 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002453 // A null build step, but it sets up the output path.
2454 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2455 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2456 }
2457
Colin Cross919281a2016-04-05 16:42:05 -07002458 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002459
2460 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002461 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002462 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002463 libExt = staticLibraryExtension
2464 }
2465
2466 stlName := strings.TrimSuffix(libName, "_shared")
2467 stlName = strings.TrimSuffix(stlName, "_static")
2468 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002469 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002470}
2471
Colin Cross635c3b02016-05-18 15:37:25 -07002472func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002473 if m, ok := mctx.Module().(*Module); ok {
2474 if m.linker != nil {
2475 if linker, ok := m.linker.(baseLinkerInterface); ok {
2476 var modules []blueprint.Module
2477 if linker.buildStatic() && linker.buildShared() {
2478 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002479 static := modules[0].(*Module)
2480 shared := modules[1].(*Module)
2481
2482 static.linker.(baseLinkerInterface).setStatic(true)
2483 shared.linker.(baseLinkerInterface).setStatic(false)
2484
2485 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2486 sharedCompiler := shared.compiler.(*libraryCompiler)
2487 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2488 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2489 // Optimize out compiling common .o files twice for static+shared libraries
2490 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2491 sharedCompiler.baseCompiler.Properties.Srcs = nil
2492 }
2493 }
Colin Crossca860ac2016-01-04 14:34:37 -08002494 } else if linker.buildStatic() {
2495 modules = mctx.CreateLocalVariations("static")
2496 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2497 } else if linker.buildShared() {
2498 modules = mctx.CreateLocalVariations("shared")
2499 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2500 } else {
2501 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2502 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002503 }
2504 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002505 }
2506}
Colin Cross74d1ec02015-04-28 13:30:13 -07002507
2508// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2509// modifies the slice contents in place, and returns a subslice of the original slice
2510func lastUniqueElements(list []string) []string {
2511 totalSkip := 0
2512 for i := len(list) - 1; i >= totalSkip; i-- {
2513 skip := 0
2514 for j := i - 1; j >= totalSkip; j-- {
2515 if list[i] == list[j] {
2516 skip++
2517 } else {
2518 list[j+skip] = list[j]
2519 }
2520 }
2521 totalSkip += skip
2522 }
2523 return list[totalSkip:]
2524}
Colin Cross06a931b2015-10-28 17:23:31 -07002525
2526var Bool = proptools.Bool