blob: 9cdd66c0e36ae11392a5df0c7bbff333e52ac74a [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
272 // list of directories relative to the Blueprints file that will
273 // be added to the include path using -I
274 Local_include_dirs []string `android:"arch_variant"`
275
Dan Willemsenb40aab62016-04-20 14:21:14 -0700276 // list of generated sources to compile. These are the names of gensrcs or
277 // genrule modules.
278 Generated_sources []string `android:"arch_variant"`
279
280 // list of generated headers to add to the include path. These are the names
281 // of genrule modules.
282 Generated_headers []string `android:"arch_variant"`
283
Colin Crossca860ac2016-01-04 14:34:37 -0800284 // pass -frtti instead of -fno-rtti
285 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700286
Colin Crossca860ac2016-01-04 14:34:37 -0800287 Debug, Release struct {
288 // list of module-specific flags that will be used for C and C++ compiles in debug or
289 // release builds
290 Cflags []string `android:"arch_variant"`
291 } `android:"arch_variant"`
292}
Colin Cross7d5136f2015-05-11 13:39:40 -0700293
Colin Crossca860ac2016-01-04 14:34:37 -0800294type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700295 // list of modules whose object files should be linked into this module
296 // in their entirety. For static library modules, all of the .o files from the intermediate
297 // directory of the dependency will be linked into this modules .a file. For a shared library,
298 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700299 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700300
301 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700302 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700303
304 // list of modules that should be dynamically linked into this module.
305 Shared_libs []string `android:"arch_variant"`
306
Colin Crossca860ac2016-01-04 14:34:37 -0800307 // list of module-specific flags that will be used for all link steps
308 Ldflags []string `android:"arch_variant"`
309
310 // don't insert default compiler flags into asflags, cflags,
311 // cppflags, conlyflags, ldflags, or include_dirs
312 No_default_compiler_flags *bool
313
314 // list of system libraries that will be dynamically linked to
315 // shared library and executable modules. If unset, generally defaults to libc
316 // and libm. Set to [] to prevent linking against libc and libm.
317 System_shared_libs []string
318
Colin Cross7d5136f2015-05-11 13:39:40 -0700319 // allow the module to contain undefined symbols. By default,
320 // modules cannot contain undefined symbols that are not satisified by their immediate
321 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
322 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700323 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700324
Dan Willemsend67be222015-09-16 15:19:33 -0700325 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700326 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700327
Colin Cross7d5136f2015-05-11 13:39:40 -0700328 // -l arguments to pass to linker for host-provided shared libraries
329 Host_ldlibs []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800330}
Colin Cross7d5136f2015-05-11 13:39:40 -0700331
Colin Crossca860ac2016-01-04 14:34:37 -0800332type LibraryCompilerProperties struct {
333 Static struct {
334 Srcs []string `android:"arch_variant"`
335 Exclude_srcs []string `android:"arch_variant"`
336 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700337 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800338 Shared struct {
339 Srcs []string `android:"arch_variant"`
340 Exclude_srcs []string `android:"arch_variant"`
341 Cflags []string `android:"arch_variant"`
342 } `android:"arch_variant"`
343}
344
Colin Cross919281a2016-04-05 16:42:05 -0700345type FlagExporterProperties struct {
346 // list of directories relative to the Blueprints file that will
347 // be added to the include path using -I for any module that links against this module
348 Export_include_dirs []string `android:"arch_variant"`
349}
350
Colin Crossca860ac2016-01-04 14:34:37 -0800351type LibraryLinkerProperties struct {
352 Static struct {
353 Whole_static_libs []string `android:"arch_variant"`
354 Static_libs []string `android:"arch_variant"`
355 Shared_libs []string `android:"arch_variant"`
356 } `android:"arch_variant"`
357 Shared struct {
358 Whole_static_libs []string `android:"arch_variant"`
359 Static_libs []string `android:"arch_variant"`
360 Shared_libs []string `android:"arch_variant"`
361 } `android:"arch_variant"`
362
363 // local file name to pass to the linker as --version_script
364 Version_script *string `android:"arch_variant"`
365 // local file name to pass to the linker as -unexported_symbols_list
366 Unexported_symbols_list *string `android:"arch_variant"`
367 // local file name to pass to the linker as -force_symbols_not_weak_list
368 Force_symbols_not_weak_list *string `android:"arch_variant"`
369 // local file name to pass to the linker as -force_symbols_weak_list
370 Force_symbols_weak_list *string `android:"arch_variant"`
371
Colin Crossca860ac2016-01-04 14:34:37 -0800372 // don't link in crt_begin and crt_end. This flag should only be necessary for
373 // compiling crt or libc.
374 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800375
376 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800377}
378
379type BinaryLinkerProperties struct {
380 // compile executable with -static
381 Static_executable *bool
382
383 // set the name of the output
384 Stem string `android:"arch_variant"`
385
386 // append to the name of the output
387 Suffix string `android:"arch_variant"`
388
389 // if set, add an extra objcopy --prefix-symbols= step
390 Prefix_symbols string
391}
392
393type TestLinkerProperties struct {
394 // if set, build against the gtest library. Defaults to true.
395 Gtest bool
396
397 // Create a separate binary for each source file. Useful when there is
398 // global state that can not be torn down and reset between each test suite.
399 Test_per_src *bool
400}
401
Colin Cross81413472016-04-11 14:37:39 -0700402type ObjectLinkerProperties struct {
403 // names of other cc_object modules to link into this module using partial linking
404 Objs []string `android:"arch_variant"`
405}
406
Colin Crossca860ac2016-01-04 14:34:37 -0800407// Properties used to compile all C or C++ modules
408type BaseProperties struct {
409 // compile module with clang instead of gcc
410 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700411
412 // Minimum sdk version supported when compiling against the ndk
413 Sdk_version string
414
Colin Crossca860ac2016-01-04 14:34:37 -0800415 // don't insert default compiler flags into asflags, cflags,
416 // cppflags, conlyflags, ldflags, or include_dirs
417 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700418
419 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700420 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800421}
422
423type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700424 // install to a subdirectory of the default install path for the module
425 Relative_install_path string
426}
427
Colin Cross665dce92016-04-28 14:50:03 -0700428type StripProperties struct {
429 Strip struct {
430 None bool
431 Keep_symbols bool
432 }
433}
434
Colin Crossca860ac2016-01-04 14:34:37 -0800435type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700436 Native_coverage *bool
437 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700438 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800439}
440
Colin Crossca860ac2016-01-04 14:34:37 -0800441type ModuleContextIntf interface {
442 module() *Module
443 static() bool
444 staticBinary() bool
445 clang() bool
446 toolchain() Toolchain
447 noDefaultCompilerFlags() bool
448 sdk() bool
449 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700450 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800451}
452
453type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700454 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800455 ModuleContextIntf
456}
457
458type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700459 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800460 ModuleContextIntf
461}
462
463type Customizer interface {
464 CustomizeProperties(BaseModuleContext)
465 Properties() []interface{}
466}
467
468type feature interface {
469 begin(ctx BaseModuleContext)
470 deps(ctx BaseModuleContext, deps Deps) Deps
471 flags(ctx ModuleContext, flags Flags) Flags
472 props() []interface{}
473}
474
475type compiler interface {
476 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700477 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800478}
479
480type linker interface {
481 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700482 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700483 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800484}
485
486type installer interface {
487 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700488 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800489 inData() bool
490}
491
Colin Crossc99deeb2016-04-11 15:06:20 -0700492type dependencyTag struct {
493 blueprint.BaseDependencyTag
494 name string
495 library bool
496}
497
498var (
499 sharedDepTag = dependencyTag{name: "shared", library: true}
500 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
501 staticDepTag = dependencyTag{name: "static", library: true}
502 lateStaticDepTag = dependencyTag{name: "late static", library: true}
503 wholeStaticDepTag = dependencyTag{name: "whole static", library: true}
Dan Willemsenb40aab62016-04-20 14:21:14 -0700504 genSourceDepTag = dependencyTag{name: "gen source"}
505 genHeaderDepTag = dependencyTag{name: "gen header"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700506 objDepTag = dependencyTag{name: "obj"}
507 crtBeginDepTag = dependencyTag{name: "crtbegin"}
508 crtEndDepTag = dependencyTag{name: "crtend"}
509 reuseObjTag = dependencyTag{name: "reuse objects"}
510)
511
Colin Crossca860ac2016-01-04 14:34:37 -0800512// Module contains the properties and members used by all C/C++ module types, and implements
513// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
514// to construct the output file. Behavior can be customized with a Customizer interface
515type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700516 android.ModuleBase
517 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700518
Colin Crossca860ac2016-01-04 14:34:37 -0800519 Properties BaseProperties
520 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700521
Colin Crossca860ac2016-01-04 14:34:37 -0800522 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700523 hod android.HostOrDeviceSupported
524 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700525
Colin Crossca860ac2016-01-04 14:34:37 -0800526 // delegates, initialize before calling Init
527 customizer Customizer
528 features []feature
529 compiler compiler
530 linker linker
531 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700532 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800533 sanitize *sanitize
534
535 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700536
Colin Cross635c3b02016-05-18 15:37:25 -0700537 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800538
539 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700540}
541
Colin Crossca860ac2016-01-04 14:34:37 -0800542func (c *Module) Init() (blueprint.Module, []interface{}) {
543 props := []interface{}{&c.Properties, &c.unused}
544 if c.customizer != nil {
545 props = append(props, c.customizer.Properties()...)
546 }
547 if c.compiler != nil {
548 props = append(props, c.compiler.props()...)
549 }
550 if c.linker != nil {
551 props = append(props, c.linker.props()...)
552 }
553 if c.installer != nil {
554 props = append(props, c.installer.props()...)
555 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700556 if c.stl != nil {
557 props = append(props, c.stl.props()...)
558 }
Colin Cross16b23492016-01-06 14:41:07 -0800559 if c.sanitize != nil {
560 props = append(props, c.sanitize.props()...)
561 }
Colin Crossca860ac2016-01-04 14:34:37 -0800562 for _, feature := range c.features {
563 props = append(props, feature.props()...)
564 }
Colin Crossc472d572015-03-17 15:06:21 -0700565
Colin Cross635c3b02016-05-18 15:37:25 -0700566 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700567
Colin Cross635c3b02016-05-18 15:37:25 -0700568 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700569}
570
Colin Crossca860ac2016-01-04 14:34:37 -0800571type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700572 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800573 moduleContextImpl
574}
575
576type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700577 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800578 moduleContextImpl
579}
580
581type moduleContextImpl struct {
582 mod *Module
583 ctx BaseModuleContext
584}
585
586func (ctx *moduleContextImpl) module() *Module {
587 return ctx.mod
588}
589
590func (ctx *moduleContextImpl) clang() bool {
591 return ctx.mod.clang(ctx.ctx)
592}
593
594func (ctx *moduleContextImpl) toolchain() Toolchain {
595 return ctx.mod.toolchain(ctx.ctx)
596}
597
598func (ctx *moduleContextImpl) static() bool {
599 if ctx.mod.linker == nil {
600 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
601 }
602 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
603 return linker.static()
604 } else {
605 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
606 }
607}
608
609func (ctx *moduleContextImpl) staticBinary() bool {
610 if ctx.mod.linker == nil {
611 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
612 }
613 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
614 return linker.staticBinary()
615 } else {
616 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
617 }
618}
619
620func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
621 return Bool(ctx.mod.Properties.No_default_compiler_flags)
622}
623
624func (ctx *moduleContextImpl) sdk() bool {
625 return ctx.mod.Properties.Sdk_version != ""
626}
627
628func (ctx *moduleContextImpl) sdkVersion() string {
629 return ctx.mod.Properties.Sdk_version
630}
631
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700632func (ctx *moduleContextImpl) selectedStl() string {
633 if stl := ctx.mod.stl; stl != nil {
634 return stl.Properties.SelectedStl
635 }
636 return ""
637}
638
Colin Cross635c3b02016-05-18 15:37:25 -0700639func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800640 return &Module{
641 hod: hod,
642 multilib: multilib,
643 }
644}
645
Colin Cross635c3b02016-05-18 15:37:25 -0700646func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800647 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700648 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800649 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800650 return module
651}
652
Colin Cross635c3b02016-05-18 15:37:25 -0700653func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800654 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700655 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800656 moduleContextImpl: moduleContextImpl{
657 mod: c,
658 },
659 }
660 ctx.ctx = ctx
661
662 flags := Flags{
663 Toolchain: c.toolchain(ctx),
664 Clang: c.clang(ctx),
665 }
Colin Crossca860ac2016-01-04 14:34:37 -0800666 if c.compiler != nil {
667 flags = c.compiler.flags(ctx, flags)
668 }
669 if c.linker != nil {
670 flags = c.linker.flags(ctx, flags)
671 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700672 if c.stl != nil {
673 flags = c.stl.flags(ctx, flags)
674 }
Colin Cross16b23492016-01-06 14:41:07 -0800675 if c.sanitize != nil {
676 flags = c.sanitize.flags(ctx, flags)
677 }
Colin Crossca860ac2016-01-04 14:34:37 -0800678 for _, feature := range c.features {
679 flags = feature.flags(ctx, flags)
680 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800681 if ctx.Failed() {
682 return
683 }
684
Colin Crossca860ac2016-01-04 14:34:37 -0800685 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
686 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
687 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800688
Colin Crossca860ac2016-01-04 14:34:37 -0800689 // Optimization to reduce size of build.ninja
690 // Replace the long list of flags for each file with a module-local variable
691 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
692 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
693 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
694 flags.CFlags = []string{"$cflags"}
695 flags.CppFlags = []string{"$cppflags"}
696 flags.AsFlags = []string{"$asflags"}
697
Colin Crossc99deeb2016-04-11 15:06:20 -0700698 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800699 if ctx.Failed() {
700 return
701 }
702
Colin Cross28344522015-04-22 13:07:53 -0700703 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700704
Colin Cross635c3b02016-05-18 15:37:25 -0700705 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800706 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700707 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800708 if ctx.Failed() {
709 return
710 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800711 }
712
Colin Crossca860ac2016-01-04 14:34:37 -0800713 if c.linker != nil {
714 outputFile := c.linker.link(ctx, flags, deps, objFiles)
715 if ctx.Failed() {
716 return
717 }
Colin Cross635c3b02016-05-18 15:37:25 -0700718 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700719
Colin Crossc99deeb2016-04-11 15:06:20 -0700720 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800721 c.installer.install(ctx, outputFile)
722 if ctx.Failed() {
723 return
724 }
725 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700726 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800727}
728
Colin Crossca860ac2016-01-04 14:34:37 -0800729func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
730 if c.cachedToolchain == nil {
731 arch := ctx.Arch()
732 hod := ctx.HostOrDevice()
733 ht := ctx.HostType()
734 factory := toolchainFactories[hod][ht][arch.ArchType]
735 if factory == nil {
736 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
737 return nil
738 }
739 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800740 }
Colin Crossca860ac2016-01-04 14:34:37 -0800741 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800742}
743
Colin Crossca860ac2016-01-04 14:34:37 -0800744func (c *Module) begin(ctx BaseModuleContext) {
745 if c.compiler != nil {
746 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700747 }
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if c.linker != nil {
749 c.linker.begin(ctx)
750 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700751 if c.stl != nil {
752 c.stl.begin(ctx)
753 }
Colin Cross16b23492016-01-06 14:41:07 -0800754 if c.sanitize != nil {
755 c.sanitize.begin(ctx)
756 }
Colin Crossca860ac2016-01-04 14:34:37 -0800757 for _, feature := range c.features {
758 feature.begin(ctx)
759 }
760}
761
Colin Crossc99deeb2016-04-11 15:06:20 -0700762func (c *Module) deps(ctx BaseModuleContext) Deps {
763 deps := Deps{}
764
765 if c.compiler != nil {
766 deps = c.compiler.deps(ctx, deps)
767 }
768 if c.linker != nil {
769 deps = c.linker.deps(ctx, deps)
770 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700771 if c.stl != nil {
772 deps = c.stl.deps(ctx, deps)
773 }
Colin Cross16b23492016-01-06 14:41:07 -0800774 if c.sanitize != nil {
775 deps = c.sanitize.deps(ctx, deps)
776 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700777 for _, feature := range c.features {
778 deps = feature.deps(ctx, deps)
779 }
780
781 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
782 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
783 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
784 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
785 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
786
787 return deps
788}
789
Colin Cross635c3b02016-05-18 15:37:25 -0700790func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800791 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700792 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800793 moduleContextImpl: moduleContextImpl{
794 mod: c,
795 },
796 }
797 ctx.ctx = ctx
798
799 if c.customizer != nil {
800 c.customizer.CustomizeProperties(ctx)
801 }
802
803 c.begin(ctx)
804
Colin Crossc99deeb2016-04-11 15:06:20 -0700805 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800806
Colin Crossc99deeb2016-04-11 15:06:20 -0700807 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
808
809 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
810 deps.WholeStaticLibs...)
811
812 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticDepTag,
813 deps.StaticLibs...)
814
815 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
816 deps.LateStaticLibs...)
817
818 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, sharedDepTag,
819 deps.SharedLibs...)
820
821 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
822 deps.LateSharedLibs...)
823
Dan Willemsenb40aab62016-04-20 14:21:14 -0700824 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
825 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
826
Colin Crossc99deeb2016-04-11 15:06:20 -0700827 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
828
829 if deps.CrtBegin != "" {
830 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800831 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700832 if deps.CrtEnd != "" {
833 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700834 }
Colin Cross6362e272015-10-29 15:25:03 -0700835}
Colin Cross21b9a242015-03-24 14:15:58 -0700836
Colin Cross635c3b02016-05-18 15:37:25 -0700837func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800838 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700839 c.depsMutator(ctx)
840 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800841}
842
Colin Crossca860ac2016-01-04 14:34:37 -0800843func (c *Module) clang(ctx BaseModuleContext) bool {
844 clang := Bool(c.Properties.Clang)
845
846 if c.Properties.Clang == nil {
847 if ctx.Host() {
848 clang = true
849 }
850
851 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
852 clang = true
853 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800854 }
Colin Cross28344522015-04-22 13:07:53 -0700855
Colin Crossca860ac2016-01-04 14:34:37 -0800856 if !c.toolchain(ctx).ClangSupported() {
857 clang = false
858 }
859
860 return clang
861}
862
Colin Crossc99deeb2016-04-11 15:06:20 -0700863// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700864func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800865 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800866
Colin Crossc99deeb2016-04-11 15:06:20 -0700867 ctx.VisitDirectDeps(func(m blueprint.Module) {
868 name := ctx.OtherModuleName(m)
869 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800870
Colin Cross635c3b02016-05-18 15:37:25 -0700871 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 if a == nil {
873 ctx.ModuleErrorf("module %q not an android module", name)
874 return
Colin Crossca860ac2016-01-04 14:34:37 -0800875 }
Colin Crossca860ac2016-01-04 14:34:37 -0800876
Colin Crossc99deeb2016-04-11 15:06:20 -0700877 c, _ := m.(*Module)
878 if c == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700879 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700880 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700881 case genSourceDepTag:
882 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
883 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
884 genRule.GeneratedSourceFiles()...)
885 } else {
886 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
887 }
888 case genHeaderDepTag:
889 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
890 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
891 genRule.GeneratedSourceFiles()...)
892 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700893 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700894 } else {
895 ctx.ModuleErrorf("module %q is not a genrule", name)
896 }
897 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700898 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800899 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700900 return
901 }
902
903 if !a.Enabled() {
904 ctx.ModuleErrorf("depends on disabled module %q", name)
905 return
906 }
907
908 if a.HostOrDevice() != ctx.HostOrDevice() {
909 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), name)
910 return
911 }
912
913 if !c.outputFile.Valid() {
914 ctx.ModuleErrorf("module %q missing output file", name)
915 return
916 }
917
918 if tag == reuseObjTag {
919 depPaths.ObjFiles = append(depPaths.ObjFiles,
920 c.compiler.(*libraryCompiler).reuseObjFiles...)
921 return
922 }
923
924 var cflags []string
925 if t, _ := tag.(dependencyTag); t.library {
926 if i, ok := c.linker.(exportedFlagsProducer); ok {
927 cflags = i.exportedFlags()
928 depPaths.Cflags = append(depPaths.Cflags, cflags...)
929 }
930 }
931
Colin Cross635c3b02016-05-18 15:37:25 -0700932 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700933
934 switch tag {
935 case sharedDepTag:
936 depPtr = &depPaths.SharedLibs
937 case lateSharedDepTag:
938 depPtr = &depPaths.LateSharedLibs
939 case staticDepTag:
940 depPtr = &depPaths.StaticLibs
941 case lateStaticDepTag:
942 depPtr = &depPaths.LateStaticLibs
943 case wholeStaticDepTag:
944 depPtr = &depPaths.WholeStaticLibs
945 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
946 staticLib, _ := c.linker.(*libraryLinker)
947 if staticLib == nil || !staticLib.static() {
948 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
949 return
950 }
951
952 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
953 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
954 for i := range missingDeps {
955 missingDeps[i] += postfix
956 }
957 ctx.AddMissingDependencies(missingDeps)
958 }
959 depPaths.WholeStaticLibObjFiles =
960 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
961 case objDepTag:
962 depPtr = &depPaths.ObjFiles
963 case crtBeginDepTag:
964 depPaths.CrtBegin = c.outputFile
965 case crtEndDepTag:
966 depPaths.CrtEnd = c.outputFile
967 default:
968 panic(fmt.Errorf("unknown dependency tag: %s", ctx.OtherModuleDependencyTag(m)))
969 }
970
971 if depPtr != nil {
972 *depPtr = append(*depPtr, c.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -0800973 }
974 })
975
976 return depPaths
977}
978
979func (c *Module) InstallInData() bool {
980 if c.installer == nil {
981 return false
982 }
983 return c.installer.inData()
984}
985
986// Compiler
987
988type baseCompiler struct {
989 Properties BaseCompilerProperties
990}
991
992var _ compiler = (*baseCompiler)(nil)
993
994func (compiler *baseCompiler) props() []interface{} {
995 return []interface{}{&compiler.Properties}
996}
997
Dan Willemsenb40aab62016-04-20 14:21:14 -0700998func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
999
1000func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1001 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1002 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1003
1004 return deps
1005}
Colin Crossca860ac2016-01-04 14:34:37 -08001006
1007// Create a Flags struct that collects the compile flags from global values,
1008// per-target values, module type values, and per-module Blueprints properties
1009func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1010 toolchain := ctx.toolchain()
1011
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001012 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1013 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1014 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1015 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1016
Colin Crossca860ac2016-01-04 14:34:37 -08001017 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1018 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1019 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1020 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1021 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1022
Colin Cross28344522015-04-22 13:07:53 -07001023 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001024 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1025 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001026 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001027 includeDirsToFlags(localIncludeDirs),
1028 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001029
Colin Crossca860ac2016-01-04 14:34:37 -08001030 if !ctx.noDefaultCompilerFlags() {
1031 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001032 flags.GlobalFlags = append(flags.GlobalFlags,
1033 "${commonGlobalIncludes}",
1034 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001035 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001036 }
1037
1038 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001039 "-I" + android.PathForModuleSrc(ctx).String(),
1040 "-I" + android.PathForModuleOut(ctx).String(),
1041 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001042 }...)
1043 }
1044
Colin Crossca860ac2016-01-04 14:34:37 -08001045 instructionSet := compiler.Properties.Instruction_set
1046 if flags.RequiredInstructionSet != "" {
1047 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001048 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001049 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1050 if flags.Clang {
1051 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1052 }
1053 if err != nil {
1054 ctx.ModuleErrorf("%s", err)
1055 }
1056
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001057 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1058
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001059 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001060 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001061
Colin Cross97ba0732015-03-23 17:50:24 -07001062 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001063 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1064 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1065
Colin Cross97ba0732015-03-23 17:50:24 -07001066 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001067 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1068 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001069 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1070 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1071 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001072
1073 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001074 var gccPrefix string
1075 if !ctx.Darwin() {
1076 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1077 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001078
Colin Cross97ba0732015-03-23 17:50:24 -07001079 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1080 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1081 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001082 }
1083
Colin Crossca860ac2016-01-04 14:34:37 -08001084 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001085 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1086
Colin Cross97ba0732015-03-23 17:50:24 -07001087 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001088 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001089 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001090 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001091 toolchain.ClangCflags(),
1092 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001093 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001094
1095 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001096 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001097 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001098 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001099 toolchain.Cflags(),
1100 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -07001101 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001102 }
1103
Colin Cross7b66f152015-12-15 16:07:43 -08001104 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1105 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1106 }
1107
Colin Crossf6566ed2015-03-24 11:13:38 -07001108 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001109 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001110 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001111 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001112 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001113 }
1114 }
1115
Colin Cross97ba0732015-03-23 17:50:24 -07001116 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001117
Colin Cross97ba0732015-03-23 17:50:24 -07001118 if flags.Clang {
1119 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001120 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001121 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001122 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001123 }
1124
Colin Crossc4bde762015-11-23 16:11:30 -08001125 if flags.Clang {
1126 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1127 } else {
1128 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001129 }
1130
Colin Crossca860ac2016-01-04 14:34:37 -08001131 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001132 if ctx.Host() && !flags.Clang {
1133 // The host GCC doesn't support C++14 (and is deprecated, so likely
1134 // never will). Build these modules with C++11.
1135 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1136 } else {
1137 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1138 }
1139 }
1140
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001141 // We can enforce some rules more strictly in the code we own. strict
1142 // indicates if this is code that we can be stricter with. If we have
1143 // rules that we want to apply to *our* code (but maybe can't for
1144 // vendor/device specific things), we could extend this to be a ternary
1145 // value.
1146 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001147 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001148 strict = false
1149 }
1150
1151 // Can be used to make some annotations stricter for code we can fix
1152 // (such as when we mark functions as deprecated).
1153 if strict {
1154 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1155 }
1156
Colin Cross3f40fa42015-01-30 17:27:36 -08001157 return flags
1158}
1159
Colin Cross635c3b02016-05-18 15:37:25 -07001160func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001161 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001162 objFiles := compiler.compileObjs(ctx, flags, "",
1163 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1164 deps.GeneratedSources, deps.GeneratedHeaders)
1165
Colin Crossca860ac2016-01-04 14:34:37 -08001166 if ctx.Failed() {
1167 return nil
1168 }
1169
Colin Crossca860ac2016-01-04 14:34:37 -08001170 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001171}
1172
1173// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001174func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1175 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001176
Colin Crossca860ac2016-01-04 14:34:37 -08001177 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001178
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001179 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001180 inputFiles = append(inputFiles, extraSrcs...)
1181 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1182
1183 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001184 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001185
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001186 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001187}
1188
Colin Crossca860ac2016-01-04 14:34:37 -08001189// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1190type baseLinker struct {
1191 Properties BaseLinkerProperties
1192 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001193 VariantIsShared bool `blueprint:"mutated"`
1194 VariantIsStatic bool `blueprint:"mutated"`
1195 VariantIsStaticBinary bool `blueprint:"mutated"`
1196 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001197 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001198}
1199
Dan Willemsend30e6102016-03-30 17:35:50 -07001200func (linker *baseLinker) begin(ctx BaseModuleContext) {
1201 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001202 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001203 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001204 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001205 }
1206}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001207
Colin Crossca860ac2016-01-04 14:34:37 -08001208func (linker *baseLinker) props() []interface{} {
1209 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001210}
1211
Colin Crossca860ac2016-01-04 14:34:37 -08001212func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1213 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1214 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1215 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001216
Colin Cross74d1ec02015-04-28 13:30:13 -07001217 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001218 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001219 }
1220
Colin Crossf6566ed2015-03-24 11:13:38 -07001221 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001222 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001223 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1224 if !Bool(linker.Properties.No_libgcc) {
1225 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001226 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001227
Colin Crossca860ac2016-01-04 14:34:37 -08001228 if !linker.static() {
1229 if linker.Properties.System_shared_libs != nil {
1230 deps.LateSharedLibs = append(deps.LateSharedLibs,
1231 linker.Properties.System_shared_libs...)
1232 } else if !ctx.sdk() {
1233 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1234 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001235 }
Colin Cross577f6e42015-03-27 18:23:34 -07001236
Colin Crossca860ac2016-01-04 14:34:37 -08001237 if ctx.sdk() {
1238 version := ctx.sdkVersion()
1239 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001240 "ndk_libc."+version,
1241 "ndk_libm."+version,
1242 )
1243 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001244 }
1245
Colin Crossca860ac2016-01-04 14:34:37 -08001246 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001247}
1248
Colin Crossca860ac2016-01-04 14:34:37 -08001249func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1250 toolchain := ctx.toolchain()
1251
Colin Crossca860ac2016-01-04 14:34:37 -08001252 if !ctx.noDefaultCompilerFlags() {
1253 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1254 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1255 }
1256
1257 if flags.Clang {
1258 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1259 } else {
1260 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1261 }
1262
1263 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001264 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1265
Colin Crossca860ac2016-01-04 14:34:37 -08001266 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1267 }
1268 }
1269
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001270 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1271
Dan Willemsen00ced762016-05-10 17:31:21 -07001272 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1273
Dan Willemsend30e6102016-03-30 17:35:50 -07001274 if ctx.Host() && !linker.static() {
1275 rpath_prefix := `\$$ORIGIN/`
1276 if ctx.Darwin() {
1277 rpath_prefix = "@loader_path/"
1278 }
1279
Colin Crossc99deeb2016-04-11 15:06:20 -07001280 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001281 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1282 }
1283 }
1284
Dan Willemsene7174922016-03-30 17:33:52 -07001285 if flags.Clang {
1286 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1287 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001288 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1289 }
1290
1291 return flags
1292}
1293
1294func (linker *baseLinker) static() bool {
1295 return linker.dynamicProperties.VariantIsStatic
1296}
1297
1298func (linker *baseLinker) staticBinary() bool {
1299 return linker.dynamicProperties.VariantIsStaticBinary
1300}
1301
1302func (linker *baseLinker) setStatic(static bool) {
1303 linker.dynamicProperties.VariantIsStatic = static
1304}
1305
Colin Cross16b23492016-01-06 14:41:07 -08001306func (linker *baseLinker) isDependencyRoot() bool {
1307 return false
1308}
1309
Colin Crossca860ac2016-01-04 14:34:37 -08001310type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001311 // Returns true if the build options for the module have selected a static or shared build
1312 buildStatic() bool
1313 buildShared() bool
1314
1315 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001316 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001317
Colin Cross18b6dc52015-04-28 13:20:37 -07001318 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001319 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001320
1321 // Returns whether a module is a static binary
1322 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001323
1324 // Returns true for dependency roots (binaries)
1325 // TODO(ccross): also handle dlopenable libraries
1326 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001327}
1328
Colin Crossca860ac2016-01-04 14:34:37 -08001329type baseInstaller struct {
1330 Properties InstallerProperties
1331
1332 dir string
1333 dir64 string
1334 data bool
1335
Colin Cross635c3b02016-05-18 15:37:25 -07001336 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001337}
1338
1339var _ installer = (*baseInstaller)(nil)
1340
1341func (installer *baseInstaller) props() []interface{} {
1342 return []interface{}{&installer.Properties}
1343}
1344
Colin Cross635c3b02016-05-18 15:37:25 -07001345func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001346 subDir := installer.dir
1347 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1348 subDir = installer.dir64
1349 }
Colin Cross635c3b02016-05-18 15:37:25 -07001350 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001351 installer.path = ctx.InstallFile(dir, file)
1352}
1353
1354func (installer *baseInstaller) inData() bool {
1355 return installer.data
1356}
1357
Colin Cross3f40fa42015-01-30 17:27:36 -08001358//
1359// Combined static+shared libraries
1360//
1361
Colin Cross919281a2016-04-05 16:42:05 -07001362type flagExporter struct {
1363 Properties FlagExporterProperties
1364
1365 flags []string
1366}
1367
1368func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001369 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1370 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001371}
1372
1373func (f *flagExporter) reexportFlags(flags []string) {
1374 f.flags = append(f.flags, flags...)
1375}
1376
1377func (f *flagExporter) exportedFlags() []string {
1378 return f.flags
1379}
1380
1381type exportedFlagsProducer interface {
1382 exportedFlags() []string
1383}
1384
1385var _ exportedFlagsProducer = (*flagExporter)(nil)
1386
Colin Crossca860ac2016-01-04 14:34:37 -08001387type libraryCompiler struct {
1388 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001389
Colin Crossca860ac2016-01-04 14:34:37 -08001390 linker *libraryLinker
1391 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001392
Colin Crossca860ac2016-01-04 14:34:37 -08001393 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001394 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001395}
1396
Colin Crossca860ac2016-01-04 14:34:37 -08001397var _ compiler = (*libraryCompiler)(nil)
1398
1399func (library *libraryCompiler) props() []interface{} {
1400 props := library.baseCompiler.props()
1401 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001402}
1403
Colin Crossca860ac2016-01-04 14:34:37 -08001404func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1405 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001406
Dan Willemsen490fd492015-11-24 17:53:15 -08001407 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1408 // all code is position independent, and then those warnings get promoted to
1409 // errors.
Colin Cross635c3b02016-05-18 15:37:25 -07001410 if ctx.HostType() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001411 flags.CFlags = append(flags.CFlags, "-fPIC")
1412 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001413
Colin Crossca860ac2016-01-04 14:34:37 -08001414 if library.linker.static() {
1415 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001416 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001417 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001418 }
1419
Colin Crossca860ac2016-01-04 14:34:37 -08001420 return flags
1421}
1422
Colin Cross635c3b02016-05-18 15:37:25 -07001423func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1424 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001425
Dan Willemsenb40aab62016-04-20 14:21:14 -07001426 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001427 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001428
1429 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001430 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001431 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1432 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001433 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001434 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001435 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1436 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001437 }
1438
1439 return objFiles
1440}
1441
1442type libraryLinker struct {
1443 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001444 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001445 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001446
1447 Properties LibraryLinkerProperties
1448
1449 dynamicProperties struct {
1450 BuildStatic bool `blueprint:"mutated"`
1451 BuildShared bool `blueprint:"mutated"`
1452 }
1453
Colin Crossca860ac2016-01-04 14:34:37 -08001454 // If we're used as a whole_static_lib, our missing dependencies need
1455 // to be given
1456 wholeStaticMissingDeps []string
1457
1458 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001459 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001460}
1461
1462var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001463
1464func (library *libraryLinker) props() []interface{} {
1465 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001466 return append(props,
1467 &library.Properties,
1468 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001469 &library.flagExporter.Properties,
1470 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001471}
1472
1473func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1474 flags = library.baseLinker.flags(ctx, flags)
1475
1476 flags.Nocrt = Bool(library.Properties.Nocrt)
1477
1478 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001479 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001480 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1481 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001482 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001483 sharedFlag = "-shared"
1484 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001485 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001486 flags.LdFlags = append(flags.LdFlags,
1487 "-nostdlib",
1488 "-Wl,--gc-sections",
1489 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001490 }
Colin Cross97ba0732015-03-23 17:50:24 -07001491
Colin Cross0af4b842015-04-30 16:36:18 -07001492 if ctx.Darwin() {
1493 flags.LdFlags = append(flags.LdFlags,
1494 "-dynamiclib",
1495 "-single_module",
1496 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001497 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001498 )
1499 } else {
1500 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001501 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001502 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001503 )
1504 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001505 }
Colin Cross97ba0732015-03-23 17:50:24 -07001506
1507 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001508}
1509
Colin Crossca860ac2016-01-04 14:34:37 -08001510func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1511 deps = library.baseLinker.deps(ctx, deps)
1512 if library.static() {
1513 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1514 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1515 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1516 } else {
1517 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1518 if !ctx.sdk() {
1519 deps.CrtBegin = "crtbegin_so"
1520 deps.CrtEnd = "crtend_so"
1521 } else {
1522 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1523 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1524 }
1525 }
1526 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1527 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1528 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1529 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001530
Colin Crossca860ac2016-01-04 14:34:37 -08001531 return deps
1532}
Colin Cross3f40fa42015-01-30 17:27:36 -08001533
Colin Crossca860ac2016-01-04 14:34:37 -08001534func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001535 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001536
Colin Cross635c3b02016-05-18 15:37:25 -07001537 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001538 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001539
Colin Cross635c3b02016-05-18 15:37:25 -07001540 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001541 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001542
Colin Cross0af4b842015-04-30 16:36:18 -07001543 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001544 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001545 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001546 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001547 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001548
Colin Crossca860ac2016-01-04 14:34:37 -08001549 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001550
1551 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001552
1553 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001554}
1555
Colin Crossca860ac2016-01-04 14:34:37 -08001556func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001557 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001558
Colin Cross635c3b02016-05-18 15:37:25 -07001559 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001560
Colin Cross635c3b02016-05-18 15:37:25 -07001561 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1562 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1563 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1564 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001565 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001566 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001567 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001568 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001569 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001570 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001571 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1572 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001573 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001574 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1575 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001576 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001577 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1578 }
1579 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001580 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001581 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1582 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001583 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001584 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001585 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001586 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001587 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001588 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001589 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001590 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001591 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001592 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001593 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001594 }
Colin Crossaee540a2015-07-06 17:48:31 -07001595 }
1596
Colin Cross665dce92016-04-28 14:50:03 -07001597 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001598 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001599 ret := outputFile
1600
1601 builderFlags := flagsToBuilderFlags(flags)
1602
1603 if library.stripper.needsStrip(ctx) {
1604 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001605 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001606 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1607 }
1608
Colin Crossca860ac2016-01-04 14:34:37 -08001609 sharedLibs := deps.SharedLibs
1610 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001611
Colin Crossca860ac2016-01-04 14:34:37 -08001612 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1613 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001614 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001615
Colin Cross665dce92016-04-28 14:50:03 -07001616 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001617}
1618
Colin Crossca860ac2016-01-04 14:34:37 -08001619func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001620 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001621
Colin Crossc99deeb2016-04-11 15:06:20 -07001622 objFiles = append(objFiles, deps.ObjFiles...)
1623
Colin Cross635c3b02016-05-18 15:37:25 -07001624 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001625 if library.static() {
1626 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001627 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001628 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001629 }
1630
Colin Cross919281a2016-04-05 16:42:05 -07001631 library.exportIncludes(ctx, "-I")
1632 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001633
1634 return out
1635}
1636
1637func (library *libraryLinker) buildStatic() bool {
1638 return library.dynamicProperties.BuildStatic
1639}
1640
1641func (library *libraryLinker) buildShared() bool {
1642 return library.dynamicProperties.BuildShared
1643}
1644
1645func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1646 return library.wholeStaticMissingDeps
1647}
1648
Colin Crossc99deeb2016-04-11 15:06:20 -07001649func (library *libraryLinker) installable() bool {
1650 return !library.static()
1651}
1652
Colin Crossca860ac2016-01-04 14:34:37 -08001653type libraryInstaller struct {
1654 baseInstaller
1655
Colin Cross30d5f512016-05-03 18:02:42 -07001656 linker *libraryLinker
1657 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001658}
1659
Colin Cross635c3b02016-05-18 15:37:25 -07001660func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001661 if !library.linker.static() {
1662 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001663 }
1664}
1665
Colin Cross30d5f512016-05-03 18:02:42 -07001666func (library *libraryInstaller) inData() bool {
1667 return library.baseInstaller.inData() || library.sanitize.inData()
1668}
1669
Colin Cross635c3b02016-05-18 15:37:25 -07001670func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1671 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001672
Colin Crossca860ac2016-01-04 14:34:37 -08001673 linker := &libraryLinker{}
1674 linker.dynamicProperties.BuildShared = shared
1675 linker.dynamicProperties.BuildStatic = static
1676 module.linker = linker
1677
1678 module.compiler = &libraryCompiler{
1679 linker: linker,
1680 }
1681 module.installer = &libraryInstaller{
1682 baseInstaller: baseInstaller{
1683 dir: "lib",
1684 dir64: "lib64",
1685 },
Colin Cross30d5f512016-05-03 18:02:42 -07001686 linker: linker,
1687 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001688 }
1689
Colin Crossca860ac2016-01-04 14:34:37 -08001690 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001691}
1692
Colin Crossca860ac2016-01-04 14:34:37 -08001693func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001694 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001695 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001696}
1697
Colin Cross3f40fa42015-01-30 17:27:36 -08001698//
1699// Objects (for crt*.o)
1700//
1701
Colin Crossca860ac2016-01-04 14:34:37 -08001702type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001703 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001704}
1705
Colin Crossca860ac2016-01-04 14:34:37 -08001706func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001707 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001708 module.compiler = &baseCompiler{}
1709 module.linker = &objectLinker{}
1710 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001711}
1712
Colin Cross81413472016-04-11 14:37:39 -07001713func (object *objectLinker) props() []interface{} {
1714 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001715}
1716
Colin Crossca860ac2016-01-04 14:34:37 -08001717func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001718
Colin Cross81413472016-04-11 14:37:39 -07001719func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1720 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001721 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001722}
1723
Colin Crossca860ac2016-01-04 14:34:37 -08001724func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001725 if flags.Clang {
1726 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1727 } else {
1728 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1729 }
1730
Colin Crossca860ac2016-01-04 14:34:37 -08001731 return flags
1732}
1733
1734func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001735 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001736
Colin Cross97ba0732015-03-23 17:50:24 -07001737 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001738
Colin Cross635c3b02016-05-18 15:37:25 -07001739 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001740 if len(objFiles) == 1 {
1741 outputFile = objFiles[0]
1742 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001743 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001744 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001745 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001746 }
1747
Colin Cross3f40fa42015-01-30 17:27:36 -08001748 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001749 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001750}
1751
Colin Crossc99deeb2016-04-11 15:06:20 -07001752func (*objectLinker) installable() bool {
1753 return false
1754}
1755
Colin Cross3f40fa42015-01-30 17:27:36 -08001756//
1757// Executables
1758//
1759
Colin Crossca860ac2016-01-04 14:34:37 -08001760type binaryLinker struct {
1761 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001762 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001763
Colin Crossca860ac2016-01-04 14:34:37 -08001764 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001765
Colin Cross635c3b02016-05-18 15:37:25 -07001766 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001767}
1768
Colin Crossca860ac2016-01-04 14:34:37 -08001769var _ linker = (*binaryLinker)(nil)
1770
1771func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001772 return append(binary.baseLinker.props(),
1773 &binary.Properties,
1774 &binary.stripper.StripProperties)
1775
Colin Cross3f40fa42015-01-30 17:27:36 -08001776}
1777
Colin Crossca860ac2016-01-04 14:34:37 -08001778func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001779 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001780}
1781
Colin Crossca860ac2016-01-04 14:34:37 -08001782func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001783 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001784}
1785
Colin Crossca860ac2016-01-04 14:34:37 -08001786func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001787 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001788 if binary.Properties.Stem != "" {
1789 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001790 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001791
Colin Crossca860ac2016-01-04 14:34:37 -08001792 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001793}
1794
Colin Crossca860ac2016-01-04 14:34:37 -08001795func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1796 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001797 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001798 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001799 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001800 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001801 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001802 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001803 }
Colin Crossca860ac2016-01-04 14:34:37 -08001804 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001805 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001806 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001807 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001808 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001809 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001810 }
Colin Crossca860ac2016-01-04 14:34:37 -08001811 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001812 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001813
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001814 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001815 if inList("libc++_static", deps.StaticLibs) {
1816 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001817 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001818 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1819 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1820 // move them to the beginning of deps.LateStaticLibs
1821 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001822 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001823 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001824 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001825 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001826 }
Colin Crossca860ac2016-01-04 14:34:37 -08001827
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001828 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001829 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1830 "from static libs or set static_executable: true")
1831 }
1832 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001833}
1834
Colin Crossc99deeb2016-04-11 15:06:20 -07001835func (*binaryLinker) installable() bool {
1836 return true
1837}
1838
Colin Cross16b23492016-01-06 14:41:07 -08001839func (binary *binaryLinker) isDependencyRoot() bool {
1840 return true
1841}
1842
Colin Cross635c3b02016-05-18 15:37:25 -07001843func NewBinary(hod android.HostOrDeviceSupported) *Module {
1844 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001845 module.compiler = &baseCompiler{}
1846 module.linker = &binaryLinker{}
1847 module.installer = &baseInstaller{
1848 dir: "bin",
1849 }
1850 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001851}
1852
Colin Crossca860ac2016-01-04 14:34:37 -08001853func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001854 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001855 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001856}
1857
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001858func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1859 binary.baseLinker.begin(ctx)
1860
1861 static := Bool(binary.Properties.Static_executable)
1862 if ctx.Host() {
Colin Cross635c3b02016-05-18 15:37:25 -07001863 if ctx.HostType() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001864 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1865 static = true
1866 }
1867 } else {
1868 // Static executables are not supported on Darwin or Windows
1869 static = false
1870 }
Colin Cross0af4b842015-04-30 16:36:18 -07001871 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001872 if static {
1873 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001874 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001875 }
1876}
1877
Colin Crossca860ac2016-01-04 14:34:37 -08001878func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1879 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001880
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001881 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001882 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Cross635c3b02016-05-18 15:37:25 -07001883 if ctx.HostType() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001884 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1885 }
1886 }
1887
1888 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1889 // all code is position independent, and then those warnings get promoted to
1890 // errors.
Colin Cross635c3b02016-05-18 15:37:25 -07001891 if ctx.HostType() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001892 flags.CFlags = append(flags.CFlags, "-fpie")
1893 }
Colin Cross97ba0732015-03-23 17:50:24 -07001894
Colin Crossf6566ed2015-03-24 11:13:38 -07001895 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001896 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001897 // Clang driver needs -static to create static executable.
1898 // However, bionic/linker uses -shared to overwrite.
1899 // Linker for x86 targets does not allow coexistance of -static and -shared,
1900 // so we add -static only if -shared is not used.
1901 if !inList("-shared", flags.LdFlags) {
1902 flags.LdFlags = append(flags.LdFlags, "-static")
1903 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001904
Colin Crossed4cf0b2015-03-26 14:43:45 -07001905 flags.LdFlags = append(flags.LdFlags,
1906 "-nostdlib",
1907 "-Bstatic",
1908 "-Wl,--gc-sections",
1909 )
1910
1911 } else {
Colin Cross16b23492016-01-06 14:41:07 -08001912 if flags.DynamicLinker == "" {
1913 flags.DynamicLinker = "/system/bin/linker"
1914 if flags.Toolchain.Is64Bit() {
1915 flags.DynamicLinker += "64"
1916 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001917 }
1918
1919 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001920 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001921 "-nostdlib",
1922 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001923 "-Wl,--gc-sections",
1924 "-Wl,-z,nocopyreloc",
1925 )
1926 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001927 } else {
1928 if binary.staticBinary() {
1929 flags.LdFlags = append(flags.LdFlags, "-static")
1930 }
1931 if ctx.Darwin() {
1932 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
1933 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001934 }
1935
Colin Cross97ba0732015-03-23 17:50:24 -07001936 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001937}
1938
Colin Crossca860ac2016-01-04 14:34:37 -08001939func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001940 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001941
Colin Cross665dce92016-04-28 14:50:03 -07001942 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001943 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001944 ret := outputFile
Colin Crossca860ac2016-01-04 14:34:37 -08001945 if ctx.HostOrDevice().Host() {
Colin Cross635c3b02016-05-18 15:37:25 -07001946 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001947 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001948
Colin Cross635c3b02016-05-18 15:37:25 -07001949 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001950
Colin Crossca860ac2016-01-04 14:34:37 -08001951 sharedLibs := deps.SharedLibs
1952 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
1953
Colin Cross16b23492016-01-06 14:41:07 -08001954 if flags.DynamicLinker != "" {
1955 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
1956 }
1957
Colin Cross665dce92016-04-28 14:50:03 -07001958 builderFlags := flagsToBuilderFlags(flags)
1959
1960 if binary.stripper.needsStrip(ctx) {
1961 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001962 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001963 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1964 }
1965
1966 if binary.Properties.Prefix_symbols != "" {
1967 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001968 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001969 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
1970 flagsToBuilderFlags(flags), afterPrefixSymbols)
1971 }
1972
Colin Crossca860ac2016-01-04 14:34:37 -08001973 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001974 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07001975 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001976
1977 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07001978}
Colin Cross3f40fa42015-01-30 17:27:36 -08001979
Colin Cross635c3b02016-05-18 15:37:25 -07001980func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08001981 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07001982}
1983
Colin Cross665dce92016-04-28 14:50:03 -07001984type stripper struct {
1985 StripProperties StripProperties
1986}
1987
1988func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
1989 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
1990}
1991
Colin Cross635c3b02016-05-18 15:37:25 -07001992func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07001993 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07001994 if ctx.Darwin() {
1995 TransformDarwinStrip(ctx, in, out)
1996 } else {
1997 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
1998 // TODO(ccross): don't add gnu debuglink for user builds
1999 flags.stripAddGnuDebuglink = true
2000 TransformStrip(ctx, in, out, flags)
2001 }
Colin Cross665dce92016-04-28 14:50:03 -07002002}
2003
Colin Cross635c3b02016-05-18 15:37:25 -07002004func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002005 if m, ok := mctx.Module().(*Module); ok {
2006 if test, ok := m.linker.(*testLinker); ok {
2007 if Bool(test.Properties.Test_per_src) {
2008 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2009 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2010 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2011 }
2012 tests := mctx.CreateLocalVariations(testNames...)
2013 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2014 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2015 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2016 }
Colin Cross6002e052015-09-16 16:00:08 -07002017 }
2018 }
2019 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002020}
2021
Colin Crossca860ac2016-01-04 14:34:37 -08002022type testLinker struct {
2023 binaryLinker
2024 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002025}
2026
Dan Willemsend30e6102016-03-30 17:35:50 -07002027func (test *testLinker) begin(ctx BaseModuleContext) {
2028 test.binaryLinker.begin(ctx)
2029
2030 runpath := "../../lib"
2031 if ctx.toolchain().Is64Bit() {
2032 runpath += "64"
2033 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002034 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002035}
2036
Colin Crossca860ac2016-01-04 14:34:37 -08002037func (test *testLinker) props() []interface{} {
2038 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002039}
2040
Colin Crossca860ac2016-01-04 14:34:37 -08002041func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2042 flags = test.binaryLinker.flags(ctx, flags)
2043
2044 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002045 return flags
2046 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002047
Colin Cross97ba0732015-03-23 17:50:24 -07002048 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002049 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002050 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002051
Dan Willemsen4a946832016-05-13 14:13:01 -07002052 switch ctx.HostType() {
Colin Cross635c3b02016-05-18 15:37:25 -07002053 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002054 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002055 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002056 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2057 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002058 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002059 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2060 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002061 }
2062 } else {
2063 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002064 }
2065
Colin Cross21b9a242015-03-24 14:15:58 -07002066 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002067}
2068
Colin Crossca860ac2016-01-04 14:34:37 -08002069func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2070 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002071 if ctx.sdk() && ctx.Device() {
2072 switch ctx.selectedStl() {
2073 case "ndk_libc++_shared", "ndk_libc++_static":
2074 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2075 case "ndk_libgnustl_static":
2076 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2077 default:
2078 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2079 }
2080 } else {
2081 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2082 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002083 }
Colin Crossca860ac2016-01-04 14:34:37 -08002084 deps = test.binaryLinker.deps(ctx, deps)
2085 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002086}
2087
Colin Crossca860ac2016-01-04 14:34:37 -08002088type testInstaller struct {
2089 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002090}
2091
Colin Cross635c3b02016-05-18 15:37:25 -07002092func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002093 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2094 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2095 installer.baseInstaller.install(ctx, file)
2096}
2097
Colin Cross635c3b02016-05-18 15:37:25 -07002098func NewTest(hod android.HostOrDeviceSupported) *Module {
2099 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002100 module.compiler = &baseCompiler{}
2101 linker := &testLinker{}
2102 linker.Properties.Gtest = true
2103 module.linker = linker
2104 module.installer = &testInstaller{
2105 baseInstaller: baseInstaller{
2106 dir: "nativetest",
2107 dir64: "nativetest64",
2108 data: true,
2109 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002110 }
Colin Crossca860ac2016-01-04 14:34:37 -08002111 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002112}
2113
Colin Crossca860ac2016-01-04 14:34:37 -08002114func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002115 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002116 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002117}
2118
Colin Crossca860ac2016-01-04 14:34:37 -08002119type benchmarkLinker struct {
2120 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002121}
2122
Colin Crossca860ac2016-01-04 14:34:37 -08002123func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2124 deps = benchmark.binaryLinker.deps(ctx, deps)
2125 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2126 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002127}
2128
Colin Cross635c3b02016-05-18 15:37:25 -07002129func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2130 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002131 module.compiler = &baseCompiler{}
2132 module.linker = &benchmarkLinker{}
2133 module.installer = &baseInstaller{
2134 dir: "nativetest",
2135 dir64: "nativetest64",
2136 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002137 }
Colin Crossca860ac2016-01-04 14:34:37 -08002138 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002139}
2140
Colin Crossca860ac2016-01-04 14:34:37 -08002141func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002142 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002143 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002144}
2145
Colin Cross3f40fa42015-01-30 17:27:36 -08002146//
2147// Static library
2148//
2149
Colin Crossca860ac2016-01-04 14:34:37 -08002150func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002151 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002152 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002153}
2154
2155//
2156// Shared libraries
2157//
2158
Colin Crossca860ac2016-01-04 14:34:37 -08002159func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002160 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002161 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002162}
2163
2164//
2165// Host static library
2166//
2167
Colin Crossca860ac2016-01-04 14:34:37 -08002168func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002169 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002170 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002171}
2172
2173//
2174// Host Shared libraries
2175//
2176
Colin Crossca860ac2016-01-04 14:34:37 -08002177func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002178 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002179 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002180}
2181
2182//
2183// Host Binaries
2184//
2185
Colin Crossca860ac2016-01-04 14:34:37 -08002186func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002187 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002188 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002189}
2190
2191//
Colin Cross1f8f2342015-03-26 16:09:47 -07002192// Host Tests
2193//
2194
Colin Crossca860ac2016-01-04 14:34:37 -08002195func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002196 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002197 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002198}
2199
2200//
Colin Cross2ba19d92015-05-07 15:44:20 -07002201// Host Benchmarks
2202//
2203
Colin Crossca860ac2016-01-04 14:34:37 -08002204func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002205 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002206 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002207}
2208
2209//
Colin Crosscfad1192015-11-02 16:43:11 -08002210// Defaults
2211//
Colin Crossca860ac2016-01-04 14:34:37 -08002212type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002213 android.ModuleBase
2214 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002215}
2216
Colin Cross635c3b02016-05-18 15:37:25 -07002217func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002218}
2219
Colin Crossca860ac2016-01-04 14:34:37 -08002220func defaultsFactory() (blueprint.Module, []interface{}) {
2221 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002222
2223 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002224 &BaseProperties{},
2225 &BaseCompilerProperties{},
2226 &BaseLinkerProperties{},
2227 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002228 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002229 &LibraryLinkerProperties{},
2230 &BinaryLinkerProperties{},
2231 &TestLinkerProperties{},
2232 &UnusedProperties{},
2233 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002234 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002235 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002236 }
2237
Colin Cross635c3b02016-05-18 15:37:25 -07002238 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2239 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002240
Colin Cross635c3b02016-05-18 15:37:25 -07002241 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002242}
2243
2244//
Colin Cross3f40fa42015-01-30 17:27:36 -08002245// Device libraries shipped with gcc
2246//
2247
Colin Crossca860ac2016-01-04 14:34:37 -08002248type toolchainLibraryLinker struct {
2249 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002250}
2251
Colin Crossca860ac2016-01-04 14:34:37 -08002252var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2253
2254func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002255 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002256 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002257}
2258
Colin Crossca860ac2016-01-04 14:34:37 -08002259func (*toolchainLibraryLinker) buildStatic() bool {
2260 return true
2261}
Colin Cross3f40fa42015-01-30 17:27:36 -08002262
Colin Crossca860ac2016-01-04 14:34:37 -08002263func (*toolchainLibraryLinker) buildShared() bool {
2264 return false
2265}
2266
2267func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002268 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002269 module.compiler = &baseCompiler{}
2270 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002271 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002272 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002273}
2274
Colin Crossca860ac2016-01-04 14:34:37 -08002275func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002276 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002277
2278 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002279 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002280
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002281 if flags.Clang {
2282 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2283 }
2284
Colin Crossca860ac2016-01-04 14:34:37 -08002285 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002286
2287 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002288
Colin Crossca860ac2016-01-04 14:34:37 -08002289 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002290}
2291
Colin Crossc99deeb2016-04-11 15:06:20 -07002292func (*toolchainLibraryLinker) installable() bool {
2293 return false
2294}
2295
Dan Albertbe961682015-03-18 23:38:50 -07002296// NDK prebuilt libraries.
2297//
2298// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2299// either (with the exception of the shared STLs, which are installed to the app's directory rather
2300// than to the system image).
2301
Colin Cross635c3b02016-05-18 15:37:25 -07002302func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002303 suffix := ""
2304 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2305 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002306 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002307 suffix = "64"
2308 }
Colin Cross635c3b02016-05-18 15:37:25 -07002309 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002310 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002311}
2312
Colin Cross635c3b02016-05-18 15:37:25 -07002313func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2314 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002315
2316 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2317 // We want to translate to just NAME.EXT
2318 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2319 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002320 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002321}
2322
Colin Crossca860ac2016-01-04 14:34:37 -08002323type ndkPrebuiltObjectLinker struct {
2324 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002325}
2326
Colin Crossca860ac2016-01-04 14:34:37 -08002327func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002328 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002329 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002330}
2331
Colin Crossca860ac2016-01-04 14:34:37 -08002332func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002333 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002334 module.linker = &ndkPrebuiltObjectLinker{}
2335 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002336}
2337
Colin Crossca860ac2016-01-04 14:34:37 -08002338func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002339 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002340 // A null build step, but it sets up the output path.
2341 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2342 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2343 }
2344
Colin Crossca860ac2016-01-04 14:34:37 -08002345 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002346}
2347
Colin Crossca860ac2016-01-04 14:34:37 -08002348type ndkPrebuiltLibraryLinker struct {
2349 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002350}
2351
Colin Crossca860ac2016-01-04 14:34:37 -08002352var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2353var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002354
Colin Crossca860ac2016-01-04 14:34:37 -08002355func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002356 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002357}
2358
Colin Crossca860ac2016-01-04 14:34:37 -08002359func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002360 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002361 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002362}
2363
Colin Crossca860ac2016-01-04 14:34:37 -08002364func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002365 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002366 linker := &ndkPrebuiltLibraryLinker{}
2367 linker.dynamicProperties.BuildShared = true
2368 module.linker = linker
2369 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002370}
2371
Colin Crossca860ac2016-01-04 14:34:37 -08002372func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002373 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002374 // A null build step, but it sets up the output path.
2375 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2376 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2377 }
2378
Colin Cross919281a2016-04-05 16:42:05 -07002379 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002380
Colin Crossca860ac2016-01-04 14:34:37 -08002381 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2382 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002383}
2384
2385// The NDK STLs are slightly different from the prebuilt system libraries:
2386// * Are not specific to each platform version.
2387// * The libraries are not in a predictable location for each STL.
2388
Colin Crossca860ac2016-01-04 14:34:37 -08002389type ndkPrebuiltStlLinker struct {
2390 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002391}
2392
Colin Crossca860ac2016-01-04 14:34:37 -08002393func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002394 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002395 linker := &ndkPrebuiltStlLinker{}
2396 linker.dynamicProperties.BuildShared = true
2397 module.linker = linker
2398 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002399}
2400
Colin Crossca860ac2016-01-04 14:34:37 -08002401func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002402 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002403 linker := &ndkPrebuiltStlLinker{}
2404 linker.dynamicProperties.BuildStatic = true
2405 module.linker = linker
2406 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002407}
2408
Colin Cross635c3b02016-05-18 15:37:25 -07002409func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002410 gccVersion := toolchain.GccVersion()
2411 var libDir string
2412 switch stl {
2413 case "libstlport":
2414 libDir = "cxx-stl/stlport/libs"
2415 case "libc++":
2416 libDir = "cxx-stl/llvm-libc++/libs"
2417 case "libgnustl":
2418 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2419 }
2420
2421 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002422 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002423 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002424 }
2425
2426 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002427 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002428}
2429
Colin Crossca860ac2016-01-04 14:34:37 -08002430func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002431 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002432 // A null build step, but it sets up the output path.
2433 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2434 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2435 }
2436
Colin Cross919281a2016-04-05 16:42:05 -07002437 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002438
2439 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002440 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002441 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002442 libExt = staticLibraryExtension
2443 }
2444
2445 stlName := strings.TrimSuffix(libName, "_shared")
2446 stlName = strings.TrimSuffix(stlName, "_static")
2447 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002448 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002449}
2450
Colin Cross635c3b02016-05-18 15:37:25 -07002451func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002452 if m, ok := mctx.Module().(*Module); ok {
2453 if m.linker != nil {
2454 if linker, ok := m.linker.(baseLinkerInterface); ok {
2455 var modules []blueprint.Module
2456 if linker.buildStatic() && linker.buildShared() {
2457 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002458 static := modules[0].(*Module)
2459 shared := modules[1].(*Module)
2460
2461 static.linker.(baseLinkerInterface).setStatic(true)
2462 shared.linker.(baseLinkerInterface).setStatic(false)
2463
2464 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2465 sharedCompiler := shared.compiler.(*libraryCompiler)
2466 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2467 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2468 // Optimize out compiling common .o files twice for static+shared libraries
2469 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2470 sharedCompiler.baseCompiler.Properties.Srcs = nil
2471 }
2472 }
Colin Crossca860ac2016-01-04 14:34:37 -08002473 } else if linker.buildStatic() {
2474 modules = mctx.CreateLocalVariations("static")
2475 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2476 } else if linker.buildShared() {
2477 modules = mctx.CreateLocalVariations("shared")
2478 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2479 } else {
2480 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2481 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002482 }
2483 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002484 }
2485}
Colin Cross74d1ec02015-04-28 13:30:13 -07002486
2487// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2488// modifies the slice contents in place, and returns a subslice of the original slice
2489func lastUniqueElements(list []string) []string {
2490 totalSkip := 0
2491 for i := len(list) - 1; i >= totalSkip; i-- {
2492 skip := 0
2493 for j := i - 1; j >= totalSkip; j-- {
2494 if list[i] == list[j] {
2495 skip++
2496 } else {
2497 list[j+skip] = list[j]
2498 }
2499 }
2500 totalSkip += skip
2501 }
2502 return list[totalSkip:]
2503}
Colin Cross06a931b2015-10-28 17:23:31 -07002504
2505var Bool = proptools.Bool