blob: dd9a831845cf87c3184b5a10cfb7a45fe3d0dc97 [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 Crossa1ad8d12016-06-01 17:09:44 -0700121 if android.BuildOs == 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
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700185 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
186
Colin Cross81413472016-04-11 14:37:39 -0700187 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700188
Dan Willemsenb40aab62016-04-20 14:21:14 -0700189 GeneratedSources []string
190 GeneratedHeaders []string
191
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700192 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700193
Colin Cross97ba0732015-03-23 17:50:24 -0700194 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700195}
196
Colin Crossca860ac2016-01-04 14:34:37 -0800197type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700198 SharedLibs, LateSharedLibs android.Paths
199 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700200
Colin Cross635c3b02016-05-18 15:37:25 -0700201 ObjFiles android.Paths
202 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700203
Colin Cross635c3b02016-05-18 15:37:25 -0700204 GeneratedSources android.Paths
205 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700206
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700207 Cflags, ReexportedCflags []string
208
Colin Cross635c3b02016-05-18 15:37:25 -0700209 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700210}
211
Colin Crossca860ac2016-01-04 14:34:37 -0800212type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700213 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
214 AsFlags []string // Flags that apply to assembly source files
215 CFlags []string // Flags that apply to C and C++ source files
216 ConlyFlags []string // Flags that apply to C source files
217 CppFlags []string // Flags that apply to C++ source files
218 YaccFlags []string // Flags that apply to Yacc source files
219 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800220 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700221
222 Nocrt bool
223 Toolchain Toolchain
224 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800225
226 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800227 DynamicLinker string
228
Colin Cross635c3b02016-05-18 15:37:25 -0700229 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700230}
231
Colin Crossca860ac2016-01-04 14:34:37 -0800232type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700233 // 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 -0700234 Srcs []string `android:"arch_variant"`
235
236 // list of source files that should not be used to build the C/C++ module.
237 // This is most useful in the arch/multilib variants to remove non-common files
238 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700239
240 // list of module-specific flags that will be used for C and C++ compiles.
241 Cflags []string `android:"arch_variant"`
242
243 // list of module-specific flags that will be used for C++ compiles
244 Cppflags []string `android:"arch_variant"`
245
246 // list of module-specific flags that will be used for C compiles
247 Conlyflags []string `android:"arch_variant"`
248
249 // list of module-specific flags that will be used for .S compiles
250 Asflags []string `android:"arch_variant"`
251
Colin Crossca860ac2016-01-04 14:34:37 -0800252 // list of module-specific flags that will be used for C and C++ compiles when
253 // compiling with clang
254 Clang_cflags []string `android:"arch_variant"`
255
256 // list of module-specific flags that will be used for .S compiles when
257 // compiling with clang
258 Clang_asflags []string `android:"arch_variant"`
259
Colin Cross7d5136f2015-05-11 13:39:40 -0700260 // list of module-specific flags that will be used for .y and .yy compiles
261 Yaccflags []string
262
Colin Cross7d5136f2015-05-11 13:39:40 -0700263 // the instruction set architecture to use to compile the C/C++
264 // module.
265 Instruction_set string `android:"arch_variant"`
266
267 // list of directories relative to the root of the source tree that will
268 // be added to the include path using -I.
269 // If possible, don't use this. If adding paths from the current directory use
270 // local_include_dirs, if adding paths from other modules use export_include_dirs in
271 // that module.
272 Include_dirs []string `android:"arch_variant"`
273
274 // list of directories relative to the Blueprints file that will
275 // be added to the include path using -I
276 Local_include_dirs []string `android:"arch_variant"`
277
Dan Willemsenb40aab62016-04-20 14:21:14 -0700278 // list of generated sources to compile. These are the names of gensrcs or
279 // genrule modules.
280 Generated_sources []string `android:"arch_variant"`
281
282 // list of generated headers to add to the include path. These are the names
283 // of genrule modules.
284 Generated_headers []string `android:"arch_variant"`
285
Colin Crossca860ac2016-01-04 14:34:37 -0800286 // pass -frtti instead of -fno-rtti
287 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700288
Colin Crossca860ac2016-01-04 14:34:37 -0800289 Debug, Release struct {
290 // list of module-specific flags that will be used for C and C++ compiles in debug or
291 // release builds
292 Cflags []string `android:"arch_variant"`
293 } `android:"arch_variant"`
294}
Colin Cross7d5136f2015-05-11 13:39:40 -0700295
Colin Crossca860ac2016-01-04 14:34:37 -0800296type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700297 // list of modules whose object files should be linked into this module
298 // in their entirety. For static library modules, all of the .o files from the intermediate
299 // directory of the dependency will be linked into this modules .a file. For a shared library,
300 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700301 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700302
303 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700304 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700305
306 // list of modules that should be dynamically linked into this module.
307 Shared_libs []string `android:"arch_variant"`
308
Colin Crossca860ac2016-01-04 14:34:37 -0800309 // list of module-specific flags that will be used for all link steps
310 Ldflags []string `android:"arch_variant"`
311
312 // don't insert default compiler flags into asflags, cflags,
313 // cppflags, conlyflags, ldflags, or include_dirs
314 No_default_compiler_flags *bool
315
316 // list of system libraries that will be dynamically linked to
317 // shared library and executable modules. If unset, generally defaults to libc
318 // and libm. Set to [] to prevent linking against libc and libm.
319 System_shared_libs []string
320
Colin Cross7d5136f2015-05-11 13:39:40 -0700321 // allow the module to contain undefined symbols. By default,
322 // modules cannot contain undefined symbols that are not satisified by their immediate
323 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
324 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700325 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700326
Dan Willemsend67be222015-09-16 15:19:33 -0700327 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700328 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700329
Colin Cross7d5136f2015-05-11 13:39:40 -0700330 // -l arguments to pass to linker for host-provided shared libraries
331 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700332
333 // list of shared libraries to re-export include directories from. Entries must be
334 // present in shared_libs.
335 Export_shared_lib_headers []string `android:"arch_variant"`
336
337 // list of static libraries to re-export include directories from. Entries must be
338 // present in static_libs.
339 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800340}
Colin Cross7d5136f2015-05-11 13:39:40 -0700341
Colin Crossca860ac2016-01-04 14:34:37 -0800342type LibraryCompilerProperties struct {
343 Static struct {
344 Srcs []string `android:"arch_variant"`
345 Exclude_srcs []string `android:"arch_variant"`
346 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700347 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800348 Shared struct {
349 Srcs []string `android:"arch_variant"`
350 Exclude_srcs []string `android:"arch_variant"`
351 Cflags []string `android:"arch_variant"`
352 } `android:"arch_variant"`
353}
354
Colin Cross919281a2016-04-05 16:42:05 -0700355type FlagExporterProperties struct {
356 // list of directories relative to the Blueprints file that will
357 // be added to the include path using -I for any module that links against this module
358 Export_include_dirs []string `android:"arch_variant"`
359}
360
Colin Crossca860ac2016-01-04 14:34:37 -0800361type LibraryLinkerProperties struct {
362 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700363 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800364 Whole_static_libs []string `android:"arch_variant"`
365 Static_libs []string `android:"arch_variant"`
366 Shared_libs []string `android:"arch_variant"`
367 } `android:"arch_variant"`
368 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700369 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800370 Whole_static_libs []string `android:"arch_variant"`
371 Static_libs []string `android:"arch_variant"`
372 Shared_libs []string `android:"arch_variant"`
373 } `android:"arch_variant"`
374
375 // local file name to pass to the linker as --version_script
376 Version_script *string `android:"arch_variant"`
377 // local file name to pass to the linker as -unexported_symbols_list
378 Unexported_symbols_list *string `android:"arch_variant"`
379 // local file name to pass to the linker as -force_symbols_not_weak_list
380 Force_symbols_not_weak_list *string `android:"arch_variant"`
381 // local file name to pass to the linker as -force_symbols_weak_list
382 Force_symbols_weak_list *string `android:"arch_variant"`
383
Colin Crossca860ac2016-01-04 14:34:37 -0800384 // don't link in crt_begin and crt_end. This flag should only be necessary for
385 // compiling crt or libc.
386 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800387
388 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800389}
390
391type BinaryLinkerProperties struct {
392 // compile executable with -static
393 Static_executable *bool
394
395 // set the name of the output
396 Stem string `android:"arch_variant"`
397
398 // append to the name of the output
399 Suffix string `android:"arch_variant"`
400
401 // if set, add an extra objcopy --prefix-symbols= step
402 Prefix_symbols string
403}
404
405type TestLinkerProperties struct {
406 // if set, build against the gtest library. Defaults to true.
407 Gtest bool
408
409 // Create a separate binary for each source file. Useful when there is
410 // global state that can not be torn down and reset between each test suite.
411 Test_per_src *bool
412}
413
Colin Cross81413472016-04-11 14:37:39 -0700414type ObjectLinkerProperties struct {
415 // names of other cc_object modules to link into this module using partial linking
416 Objs []string `android:"arch_variant"`
417}
418
Colin Crossca860ac2016-01-04 14:34:37 -0800419// Properties used to compile all C or C++ modules
420type BaseProperties struct {
421 // compile module with clang instead of gcc
422 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700423
424 // Minimum sdk version supported when compiling against the ndk
425 Sdk_version string
426
Colin Crossca860ac2016-01-04 14:34:37 -0800427 // don't insert default compiler flags into asflags, cflags,
428 // cppflags, conlyflags, ldflags, or include_dirs
429 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700430
431 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700432 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800433}
434
435type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700436 // install to a subdirectory of the default install path for the module
437 Relative_install_path string
438}
439
Colin Cross665dce92016-04-28 14:50:03 -0700440type StripProperties struct {
441 Strip struct {
442 None bool
443 Keep_symbols bool
444 }
445}
446
Colin Crossca860ac2016-01-04 14:34:37 -0800447type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700448 Native_coverage *bool
449 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700450 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800451}
452
Colin Crossca860ac2016-01-04 14:34:37 -0800453type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800454 static() bool
455 staticBinary() bool
456 clang() bool
457 toolchain() Toolchain
458 noDefaultCompilerFlags() bool
459 sdk() bool
460 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700461 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800462}
463
464type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700465 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800466 ModuleContextIntf
467}
468
469type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700470 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800471 ModuleContextIntf
472}
473
474type Customizer interface {
475 CustomizeProperties(BaseModuleContext)
476 Properties() []interface{}
477}
478
479type feature interface {
480 begin(ctx BaseModuleContext)
481 deps(ctx BaseModuleContext, deps Deps) Deps
482 flags(ctx ModuleContext, flags Flags) Flags
483 props() []interface{}
484}
485
486type compiler interface {
487 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700488 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800489}
490
491type linker interface {
492 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700493 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700494 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800495}
496
497type installer interface {
498 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700499 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800500 inData() bool
501}
502
Colin Crossc99deeb2016-04-11 15:06:20 -0700503type dependencyTag struct {
504 blueprint.BaseDependencyTag
505 name string
506 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700507
508 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700509}
510
511var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700512 sharedDepTag = dependencyTag{name: "shared", library: true}
513 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
514 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
515 staticDepTag = dependencyTag{name: "static", library: true}
516 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
517 lateStaticDepTag = dependencyTag{name: "late static", library: true}
518 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
519 genSourceDepTag = dependencyTag{name: "gen source"}
520 genHeaderDepTag = dependencyTag{name: "gen header"}
521 objDepTag = dependencyTag{name: "obj"}
522 crtBeginDepTag = dependencyTag{name: "crtbegin"}
523 crtEndDepTag = dependencyTag{name: "crtend"}
524 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700525)
526
Colin Crossca860ac2016-01-04 14:34:37 -0800527// Module contains the properties and members used by all C/C++ module types, and implements
528// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
529// to construct the output file. Behavior can be customized with a Customizer interface
530type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700531 android.ModuleBase
532 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700533
Colin Crossca860ac2016-01-04 14:34:37 -0800534 Properties BaseProperties
535 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700536
Colin Crossca860ac2016-01-04 14:34:37 -0800537 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700538 hod android.HostOrDeviceSupported
539 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700540
Colin Crossca860ac2016-01-04 14:34:37 -0800541 // delegates, initialize before calling Init
542 customizer Customizer
543 features []feature
544 compiler compiler
545 linker linker
546 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700547 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800548 sanitize *sanitize
549
550 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700551
Colin Cross635c3b02016-05-18 15:37:25 -0700552 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800553
554 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700555}
556
Colin Crossca860ac2016-01-04 14:34:37 -0800557func (c *Module) Init() (blueprint.Module, []interface{}) {
558 props := []interface{}{&c.Properties, &c.unused}
559 if c.customizer != nil {
560 props = append(props, c.customizer.Properties()...)
561 }
562 if c.compiler != nil {
563 props = append(props, c.compiler.props()...)
564 }
565 if c.linker != nil {
566 props = append(props, c.linker.props()...)
567 }
568 if c.installer != nil {
569 props = append(props, c.installer.props()...)
570 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700571 if c.stl != nil {
572 props = append(props, c.stl.props()...)
573 }
Colin Cross16b23492016-01-06 14:41:07 -0800574 if c.sanitize != nil {
575 props = append(props, c.sanitize.props()...)
576 }
Colin Crossca860ac2016-01-04 14:34:37 -0800577 for _, feature := range c.features {
578 props = append(props, feature.props()...)
579 }
Colin Crossc472d572015-03-17 15:06:21 -0700580
Colin Cross635c3b02016-05-18 15:37:25 -0700581 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700582
Colin Cross635c3b02016-05-18 15:37:25 -0700583 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700584}
585
Colin Crossca860ac2016-01-04 14:34:37 -0800586type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700587 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800588 moduleContextImpl
589}
590
591type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700592 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800593 moduleContextImpl
594}
595
596type moduleContextImpl struct {
597 mod *Module
598 ctx BaseModuleContext
599}
600
Colin Crossca860ac2016-01-04 14:34:37 -0800601func (ctx *moduleContextImpl) clang() bool {
602 return ctx.mod.clang(ctx.ctx)
603}
604
605func (ctx *moduleContextImpl) toolchain() Toolchain {
606 return ctx.mod.toolchain(ctx.ctx)
607}
608
609func (ctx *moduleContextImpl) static() bool {
610 if ctx.mod.linker == nil {
611 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
612 }
613 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
614 return linker.static()
615 } else {
616 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
617 }
618}
619
620func (ctx *moduleContextImpl) staticBinary() bool {
621 if ctx.mod.linker == nil {
622 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
623 }
624 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
625 return linker.staticBinary()
626 } else {
627 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
628 }
629}
630
631func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
632 return Bool(ctx.mod.Properties.No_default_compiler_flags)
633}
634
635func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700636 if ctx.ctx.Device() {
637 return ctx.mod.Properties.Sdk_version != ""
638 }
639 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800640}
641
642func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700643 if ctx.ctx.Device() {
644 return ctx.mod.Properties.Sdk_version
645 }
646 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800647}
648
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700649func (ctx *moduleContextImpl) selectedStl() string {
650 if stl := ctx.mod.stl; stl != nil {
651 return stl.Properties.SelectedStl
652 }
653 return ""
654}
655
Colin Cross635c3b02016-05-18 15:37:25 -0700656func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800657 return &Module{
658 hod: hod,
659 multilib: multilib,
660 }
661}
662
Colin Cross635c3b02016-05-18 15:37:25 -0700663func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800664 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700665 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800666 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800667 return module
668}
669
Colin Cross635c3b02016-05-18 15:37:25 -0700670func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800671 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700672 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800673 moduleContextImpl: moduleContextImpl{
674 mod: c,
675 },
676 }
677 ctx.ctx = ctx
678
679 flags := Flags{
680 Toolchain: c.toolchain(ctx),
681 Clang: c.clang(ctx),
682 }
Colin Crossca860ac2016-01-04 14:34:37 -0800683 if c.compiler != nil {
684 flags = c.compiler.flags(ctx, flags)
685 }
686 if c.linker != nil {
687 flags = c.linker.flags(ctx, flags)
688 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700689 if c.stl != nil {
690 flags = c.stl.flags(ctx, flags)
691 }
Colin Cross16b23492016-01-06 14:41:07 -0800692 if c.sanitize != nil {
693 flags = c.sanitize.flags(ctx, flags)
694 }
Colin Crossca860ac2016-01-04 14:34:37 -0800695 for _, feature := range c.features {
696 flags = feature.flags(ctx, flags)
697 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800698 if ctx.Failed() {
699 return
700 }
701
Colin Crossca860ac2016-01-04 14:34:37 -0800702 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
703 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
704 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800705
Colin Crossca860ac2016-01-04 14:34:37 -0800706 // Optimization to reduce size of build.ninja
707 // Replace the long list of flags for each file with a module-local variable
708 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
709 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
710 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
711 flags.CFlags = []string{"$cflags"}
712 flags.CppFlags = []string{"$cppflags"}
713 flags.AsFlags = []string{"$asflags"}
714
Colin Crossc99deeb2016-04-11 15:06:20 -0700715 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800716 if ctx.Failed() {
717 return
718 }
719
Colin Cross28344522015-04-22 13:07:53 -0700720 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700721
Colin Cross635c3b02016-05-18 15:37:25 -0700722 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800723 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700724 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800725 if ctx.Failed() {
726 return
727 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800728 }
729
Colin Crossca860ac2016-01-04 14:34:37 -0800730 if c.linker != nil {
731 outputFile := c.linker.link(ctx, flags, deps, objFiles)
732 if ctx.Failed() {
733 return
734 }
Colin Cross635c3b02016-05-18 15:37:25 -0700735 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700736
Colin Crossc99deeb2016-04-11 15:06:20 -0700737 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800738 c.installer.install(ctx, outputFile)
739 if ctx.Failed() {
740 return
741 }
742 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700743 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800744}
745
Colin Crossca860ac2016-01-04 14:34:37 -0800746func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
747 if c.cachedToolchain == nil {
748 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700749 os := ctx.Os()
750 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800751 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700752 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800753 return nil
754 }
755 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800756 }
Colin Crossca860ac2016-01-04 14:34:37 -0800757 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800758}
759
Colin Crossca860ac2016-01-04 14:34:37 -0800760func (c *Module) begin(ctx BaseModuleContext) {
761 if c.compiler != nil {
762 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700763 }
Colin Crossca860ac2016-01-04 14:34:37 -0800764 if c.linker != nil {
765 c.linker.begin(ctx)
766 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700767 if c.stl != nil {
768 c.stl.begin(ctx)
769 }
Colin Cross16b23492016-01-06 14:41:07 -0800770 if c.sanitize != nil {
771 c.sanitize.begin(ctx)
772 }
Colin Crossca860ac2016-01-04 14:34:37 -0800773 for _, feature := range c.features {
774 feature.begin(ctx)
775 }
776}
777
Colin Crossc99deeb2016-04-11 15:06:20 -0700778func (c *Module) deps(ctx BaseModuleContext) Deps {
779 deps := Deps{}
780
781 if c.compiler != nil {
782 deps = c.compiler.deps(ctx, deps)
783 }
784 if c.linker != nil {
785 deps = c.linker.deps(ctx, deps)
786 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700787 if c.stl != nil {
788 deps = c.stl.deps(ctx, deps)
789 }
Colin Cross16b23492016-01-06 14:41:07 -0800790 if c.sanitize != nil {
791 deps = c.sanitize.deps(ctx, deps)
792 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700793 for _, feature := range c.features {
794 deps = feature.deps(ctx, deps)
795 }
796
797 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
798 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
799 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
800 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
801 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
802
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700803 for _, lib := range deps.ReexportSharedLibHeaders {
804 if !inList(lib, deps.SharedLibs) {
805 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
806 }
807 }
808
809 for _, lib := range deps.ReexportStaticLibHeaders {
810 if !inList(lib, deps.StaticLibs) {
811 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
812 }
813 }
814
Colin Crossc99deeb2016-04-11 15:06:20 -0700815 return deps
816}
817
Colin Cross635c3b02016-05-18 15:37:25 -0700818func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800819 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700820 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800821 moduleContextImpl: moduleContextImpl{
822 mod: c,
823 },
824 }
825 ctx.ctx = ctx
826
827 if c.customizer != nil {
828 c.customizer.CustomizeProperties(ctx)
829 }
830
831 c.begin(ctx)
832
Colin Crossc99deeb2016-04-11 15:06:20 -0700833 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800834
Colin Crossc99deeb2016-04-11 15:06:20 -0700835 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
836
837 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
838 deps.WholeStaticLibs...)
839
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700840 for _, lib := range deps.StaticLibs {
841 depTag := staticDepTag
842 if inList(lib, deps.ReexportStaticLibHeaders) {
843 depTag = staticExportDepTag
844 }
845 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
846 deps.StaticLibs...)
847 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700848
849 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
850 deps.LateStaticLibs...)
851
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700852 for _, lib := range deps.SharedLibs {
853 depTag := sharedDepTag
854 if inList(lib, deps.ReexportSharedLibHeaders) {
855 depTag = sharedExportDepTag
856 }
857 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
858 deps.SharedLibs...)
859 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700860
861 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
862 deps.LateSharedLibs...)
863
Colin Cross68861832016-07-08 10:41:41 -0700864 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
865 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700866
Colin Cross68861832016-07-08 10:41:41 -0700867 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700868
869 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700870 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800871 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700873 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700874 }
Colin Cross6362e272015-10-29 15:25:03 -0700875}
Colin Cross21b9a242015-03-24 14:15:58 -0700876
Colin Cross635c3b02016-05-18 15:37:25 -0700877func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800878 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700879 c.depsMutator(ctx)
880 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800881}
882
Colin Crossca860ac2016-01-04 14:34:37 -0800883func (c *Module) clang(ctx BaseModuleContext) bool {
884 clang := Bool(c.Properties.Clang)
885
886 if c.Properties.Clang == nil {
887 if ctx.Host() {
888 clang = true
889 }
890
891 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
892 clang = true
893 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800894 }
Colin Cross28344522015-04-22 13:07:53 -0700895
Colin Crossca860ac2016-01-04 14:34:37 -0800896 if !c.toolchain(ctx).ClangSupported() {
897 clang = false
898 }
899
900 return clang
901}
902
Colin Crossc99deeb2016-04-11 15:06:20 -0700903// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700904func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800905 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800906
Dan Willemsena96ff642016-06-07 12:34:45 -0700907 // Whether a module can link to another module, taking into
908 // account NDK linking.
909 linkTypeOk := func(from, to *Module) bool {
910 if from.Target().Os != android.Android {
911 // Host code is not restricted
912 return true
913 }
914 if from.Properties.Sdk_version == "" {
915 // Platform code can link to anything
916 return true
917 }
918 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
919 // These are always allowed
920 return true
921 }
922 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
923 // These are allowed, but don't set sdk_version
924 return true
925 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700926 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
927 // These are allowed, but don't set sdk_version
928 return true
929 }
930 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700931 }
932
Colin Crossc99deeb2016-04-11 15:06:20 -0700933 ctx.VisitDirectDeps(func(m blueprint.Module) {
934 name := ctx.OtherModuleName(m)
935 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800936
Colin Cross635c3b02016-05-18 15:37:25 -0700937 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700938 if a == nil {
939 ctx.ModuleErrorf("module %q not an android module", name)
940 return
Colin Crossca860ac2016-01-04 14:34:37 -0800941 }
Colin Crossca860ac2016-01-04 14:34:37 -0800942
Dan Willemsena96ff642016-06-07 12:34:45 -0700943 cc, _ := m.(*Module)
944 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700945 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700946 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700947 case genSourceDepTag:
948 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
949 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
950 genRule.GeneratedSourceFiles()...)
951 } else {
952 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
953 }
954 case genHeaderDepTag:
955 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
956 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
957 genRule.GeneratedSourceFiles()...)
958 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700959 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700960 } else {
961 ctx.ModuleErrorf("module %q is not a genrule", name)
962 }
963 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700964 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800965 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700966 return
967 }
968
969 if !a.Enabled() {
970 ctx.ModuleErrorf("depends on disabled module %q", name)
971 return
972 }
973
Colin Crossa1ad8d12016-06-01 17:09:44 -0700974 if a.Target().Os != ctx.Os() {
975 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
976 return
977 }
978
979 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
980 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700981 return
982 }
983
Dan Willemsena96ff642016-06-07 12:34:45 -0700984 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700985 ctx.ModuleErrorf("module %q missing output file", name)
986 return
987 }
988
989 if tag == reuseObjTag {
990 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700991 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700992 return
993 }
994
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700995 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700996 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700997 cflags := i.exportedFlags()
Colin Crossc99deeb2016-04-11 15:06:20 -0700998 depPaths.Cflags = append(depPaths.Cflags, cflags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700999
1000 if t.reexportFlags {
1001 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
1002 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001003 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001004
1005 if !linkTypeOk(c, cc) {
1006 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1007 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001008 }
1009
Colin Cross635c3b02016-05-18 15:37:25 -07001010 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001011
1012 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001013 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001014 depPtr = &depPaths.SharedLibs
1015 case lateSharedDepTag:
1016 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001017 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001018 depPtr = &depPaths.StaticLibs
1019 case lateStaticDepTag:
1020 depPtr = &depPaths.LateStaticLibs
1021 case wholeStaticDepTag:
1022 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001023 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001024 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001025 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001026 return
1027 }
1028
1029 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1030 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1031 for i := range missingDeps {
1032 missingDeps[i] += postfix
1033 }
1034 ctx.AddMissingDependencies(missingDeps)
1035 }
1036 depPaths.WholeStaticLibObjFiles =
1037 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1038 case objDepTag:
1039 depPtr = &depPaths.ObjFiles
1040 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001041 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001042 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001043 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001044 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001045 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001046 }
1047
1048 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001049 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001050 }
1051 })
1052
1053 return depPaths
1054}
1055
1056func (c *Module) InstallInData() bool {
1057 if c.installer == nil {
1058 return false
1059 }
1060 return c.installer.inData()
1061}
1062
1063// Compiler
1064
1065type baseCompiler struct {
1066 Properties BaseCompilerProperties
1067}
1068
1069var _ compiler = (*baseCompiler)(nil)
1070
1071func (compiler *baseCompiler) props() []interface{} {
1072 return []interface{}{&compiler.Properties}
1073}
1074
Dan Willemsenb40aab62016-04-20 14:21:14 -07001075func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1076
1077func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1078 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1079 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1080
1081 return deps
1082}
Colin Crossca860ac2016-01-04 14:34:37 -08001083
1084// Create a Flags struct that collects the compile flags from global values,
1085// per-target values, module type values, and per-module Blueprints properties
1086func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1087 toolchain := ctx.toolchain()
1088
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001089 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1090 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1091 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1092 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1093
Colin Crossca860ac2016-01-04 14:34:37 -08001094 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1095 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1096 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1097 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1098 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1099
Colin Cross28344522015-04-22 13:07:53 -07001100 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001101 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1102 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001103 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001104 includeDirsToFlags(localIncludeDirs),
1105 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001106
Colin Crossca860ac2016-01-04 14:34:37 -08001107 if !ctx.noDefaultCompilerFlags() {
1108 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001109 flags.GlobalFlags = append(flags.GlobalFlags,
1110 "${commonGlobalIncludes}",
1111 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001112 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001113 }
1114
1115 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001116 "-I" + android.PathForModuleSrc(ctx).String(),
1117 "-I" + android.PathForModuleOut(ctx).String(),
1118 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001119 }...)
1120 }
1121
Colin Crossca860ac2016-01-04 14:34:37 -08001122 instructionSet := compiler.Properties.Instruction_set
1123 if flags.RequiredInstructionSet != "" {
1124 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001125 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001126 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1127 if flags.Clang {
1128 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1129 }
1130 if err != nil {
1131 ctx.ModuleErrorf("%s", err)
1132 }
1133
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001134 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1135
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001136 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001137 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001138
Colin Cross97ba0732015-03-23 17:50:24 -07001139 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001140 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1141 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1142
Colin Cross97ba0732015-03-23 17:50:24 -07001143 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001144 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1145 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001146 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1147 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1148 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001149
1150 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001151 var gccPrefix string
1152 if !ctx.Darwin() {
1153 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1154 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001155
Colin Cross97ba0732015-03-23 17:50:24 -07001156 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1157 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1158 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001159 }
1160
Colin Crossa1ad8d12016-06-01 17:09:44 -07001161 hod := "host"
1162 if ctx.Os().Class == android.Device {
1163 hod = "device"
1164 }
1165
Colin Crossca860ac2016-01-04 14:34:37 -08001166 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001167 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1168
Colin Cross97ba0732015-03-23 17:50:24 -07001169 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001170 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001171 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001172 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001173 toolchain.ClangCflags(),
1174 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001175 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001176
1177 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001178 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001179 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001180 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001181 toolchain.Cflags(),
1182 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001183 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001184 }
1185
Colin Cross7b66f152015-12-15 16:07:43 -08001186 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1187 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1188 }
1189
Colin Crossf6566ed2015-03-24 11:13:38 -07001190 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001191 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001192 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001193 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001194 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001195 }
1196 }
1197
Colin Cross97ba0732015-03-23 17:50:24 -07001198 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001199
Colin Cross97ba0732015-03-23 17:50:24 -07001200 if flags.Clang {
1201 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001202 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001203 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001204 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001205 }
1206
Colin Crossc4bde762015-11-23 16:11:30 -08001207 if flags.Clang {
1208 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1209 } else {
1210 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001211 }
1212
Colin Crossca860ac2016-01-04 14:34:37 -08001213 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001214 if ctx.Host() && !flags.Clang {
1215 // The host GCC doesn't support C++14 (and is deprecated, so likely
1216 // never will). Build these modules with C++11.
1217 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1218 } else {
1219 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1220 }
1221 }
1222
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001223 // We can enforce some rules more strictly in the code we own. strict
1224 // indicates if this is code that we can be stricter with. If we have
1225 // rules that we want to apply to *our* code (but maybe can't for
1226 // vendor/device specific things), we could extend this to be a ternary
1227 // value.
1228 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001229 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001230 strict = false
1231 }
1232
1233 // Can be used to make some annotations stricter for code we can fix
1234 // (such as when we mark functions as deprecated).
1235 if strict {
1236 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1237 }
1238
Colin Cross3f40fa42015-01-30 17:27:36 -08001239 return flags
1240}
1241
Colin Cross635c3b02016-05-18 15:37:25 -07001242func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001243 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001244 objFiles := compiler.compileObjs(ctx, flags, "",
1245 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1246 deps.GeneratedSources, deps.GeneratedHeaders)
1247
Colin Crossca860ac2016-01-04 14:34:37 -08001248 if ctx.Failed() {
1249 return nil
1250 }
1251
Colin Crossca860ac2016-01-04 14:34:37 -08001252 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001253}
1254
1255// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001256func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1257 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001258
Colin Crossca860ac2016-01-04 14:34:37 -08001259 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001260
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001261 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001262 inputFiles = append(inputFiles, extraSrcs...)
1263 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1264
1265 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001266 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001267
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001268 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001269}
1270
Colin Crossca860ac2016-01-04 14:34:37 -08001271// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1272type baseLinker struct {
1273 Properties BaseLinkerProperties
1274 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001275 VariantIsShared bool `blueprint:"mutated"`
1276 VariantIsStatic bool `blueprint:"mutated"`
1277 VariantIsStaticBinary bool `blueprint:"mutated"`
1278 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001279 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001280}
1281
Dan Willemsend30e6102016-03-30 17:35:50 -07001282func (linker *baseLinker) begin(ctx BaseModuleContext) {
1283 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001284 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001285 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001286 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001287 }
1288}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001289
Colin Crossca860ac2016-01-04 14:34:37 -08001290func (linker *baseLinker) props() []interface{} {
1291 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001292}
1293
Colin Crossca860ac2016-01-04 14:34:37 -08001294func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1295 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1296 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1297 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001298
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001299 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1300 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1301
Dan Willemsena96ff642016-06-07 12:34:45 -07001302 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001303 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001304 }
1305
Colin Crossf6566ed2015-03-24 11:13:38 -07001306 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001307 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001308 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1309 if !Bool(linker.Properties.No_libgcc) {
1310 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001311 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001312
Colin Crossca860ac2016-01-04 14:34:37 -08001313 if !linker.static() {
1314 if linker.Properties.System_shared_libs != nil {
1315 deps.LateSharedLibs = append(deps.LateSharedLibs,
1316 linker.Properties.System_shared_libs...)
1317 } else if !ctx.sdk() {
1318 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1319 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001320 }
Colin Cross577f6e42015-03-27 18:23:34 -07001321
Colin Crossca860ac2016-01-04 14:34:37 -08001322 if ctx.sdk() {
1323 version := ctx.sdkVersion()
1324 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001325 "ndk_libc."+version,
1326 "ndk_libm."+version,
1327 )
1328 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001329 }
1330
Colin Crossca860ac2016-01-04 14:34:37 -08001331 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001332}
1333
Colin Crossca860ac2016-01-04 14:34:37 -08001334func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1335 toolchain := ctx.toolchain()
1336
Colin Crossca860ac2016-01-04 14:34:37 -08001337 if !ctx.noDefaultCompilerFlags() {
1338 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1339 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1340 }
1341
1342 if flags.Clang {
1343 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1344 } else {
1345 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1346 }
1347
1348 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001349 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1350
Colin Crossca860ac2016-01-04 14:34:37 -08001351 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1352 }
1353 }
1354
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001355 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1356
Dan Willemsen00ced762016-05-10 17:31:21 -07001357 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1358
Dan Willemsend30e6102016-03-30 17:35:50 -07001359 if ctx.Host() && !linker.static() {
1360 rpath_prefix := `\$$ORIGIN/`
1361 if ctx.Darwin() {
1362 rpath_prefix = "@loader_path/"
1363 }
1364
Colin Crossc99deeb2016-04-11 15:06:20 -07001365 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001366 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1367 }
1368 }
1369
Dan Willemsene7174922016-03-30 17:33:52 -07001370 if flags.Clang {
1371 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1372 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001373 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1374 }
1375
1376 return flags
1377}
1378
1379func (linker *baseLinker) static() bool {
1380 return linker.dynamicProperties.VariantIsStatic
1381}
1382
1383func (linker *baseLinker) staticBinary() bool {
1384 return linker.dynamicProperties.VariantIsStaticBinary
1385}
1386
1387func (linker *baseLinker) setStatic(static bool) {
1388 linker.dynamicProperties.VariantIsStatic = static
1389}
1390
Colin Cross16b23492016-01-06 14:41:07 -08001391func (linker *baseLinker) isDependencyRoot() bool {
1392 return false
1393}
1394
Colin Crossca860ac2016-01-04 14:34:37 -08001395type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001396 // Returns true if the build options for the module have selected a static or shared build
1397 buildStatic() bool
1398 buildShared() bool
1399
1400 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001401 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001402
Colin Cross18b6dc52015-04-28 13:20:37 -07001403 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001404 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001405
1406 // Returns whether a module is a static binary
1407 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001408
1409 // Returns true for dependency roots (binaries)
1410 // TODO(ccross): also handle dlopenable libraries
1411 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001412}
1413
Colin Crossca860ac2016-01-04 14:34:37 -08001414type baseInstaller struct {
1415 Properties InstallerProperties
1416
1417 dir string
1418 dir64 string
1419 data bool
1420
Colin Cross635c3b02016-05-18 15:37:25 -07001421 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001422}
1423
1424var _ installer = (*baseInstaller)(nil)
1425
1426func (installer *baseInstaller) props() []interface{} {
1427 return []interface{}{&installer.Properties}
1428}
1429
Colin Cross635c3b02016-05-18 15:37:25 -07001430func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001431 subDir := installer.dir
1432 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1433 subDir = installer.dir64
1434 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001435 if !ctx.Host() && !ctx.Arch().Native {
1436 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1437 }
Colin Cross635c3b02016-05-18 15:37:25 -07001438 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001439 installer.path = ctx.InstallFile(dir, file)
1440}
1441
1442func (installer *baseInstaller) inData() bool {
1443 return installer.data
1444}
1445
Colin Cross3f40fa42015-01-30 17:27:36 -08001446//
1447// Combined static+shared libraries
1448//
1449
Colin Cross919281a2016-04-05 16:42:05 -07001450type flagExporter struct {
1451 Properties FlagExporterProperties
1452
1453 flags []string
1454}
1455
1456func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001457 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1458 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001459}
1460
1461func (f *flagExporter) reexportFlags(flags []string) {
1462 f.flags = append(f.flags, flags...)
1463}
1464
1465func (f *flagExporter) exportedFlags() []string {
1466 return f.flags
1467}
1468
1469type exportedFlagsProducer interface {
1470 exportedFlags() []string
1471}
1472
1473var _ exportedFlagsProducer = (*flagExporter)(nil)
1474
Colin Crossca860ac2016-01-04 14:34:37 -08001475type libraryCompiler struct {
1476 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001477
Colin Crossca860ac2016-01-04 14:34:37 -08001478 linker *libraryLinker
1479 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001480
Colin Crossca860ac2016-01-04 14:34:37 -08001481 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001482 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001483}
1484
Colin Crossca860ac2016-01-04 14:34:37 -08001485var _ compiler = (*libraryCompiler)(nil)
1486
1487func (library *libraryCompiler) props() []interface{} {
1488 props := library.baseCompiler.props()
1489 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001490}
1491
Colin Crossca860ac2016-01-04 14:34:37 -08001492func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1493 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001494
Dan Willemsen490fd492015-11-24 17:53:15 -08001495 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1496 // all code is position independent, and then those warnings get promoted to
1497 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001498 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001499 flags.CFlags = append(flags.CFlags, "-fPIC")
1500 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001501
Colin Crossca860ac2016-01-04 14:34:37 -08001502 if library.linker.static() {
1503 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001504 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001505 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001506 }
1507
Colin Crossca860ac2016-01-04 14:34:37 -08001508 return flags
1509}
1510
Colin Cross635c3b02016-05-18 15:37:25 -07001511func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1512 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001513
Dan Willemsenb40aab62016-04-20 14:21:14 -07001514 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001515 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001516
1517 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001518 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001519 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1520 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001521 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001522 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001523 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1524 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001525 }
1526
1527 return objFiles
1528}
1529
1530type libraryLinker struct {
1531 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001532 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001533 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001534
1535 Properties LibraryLinkerProperties
1536
1537 dynamicProperties struct {
1538 BuildStatic bool `blueprint:"mutated"`
1539 BuildShared bool `blueprint:"mutated"`
1540 }
1541
Colin Crossca860ac2016-01-04 14:34:37 -08001542 // If we're used as a whole_static_lib, our missing dependencies need
1543 // to be given
1544 wholeStaticMissingDeps []string
1545
1546 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001547 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001548}
1549
1550var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001551
1552func (library *libraryLinker) props() []interface{} {
1553 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001554 return append(props,
1555 &library.Properties,
1556 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001557 &library.flagExporter.Properties,
1558 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001559}
1560
1561func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1562 flags = library.baseLinker.flags(ctx, flags)
1563
1564 flags.Nocrt = Bool(library.Properties.Nocrt)
1565
1566 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001567 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001568 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1569 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001570 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001571 sharedFlag = "-shared"
1572 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001573 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001574 flags.LdFlags = append(flags.LdFlags,
1575 "-nostdlib",
1576 "-Wl,--gc-sections",
1577 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001578 }
Colin Cross97ba0732015-03-23 17:50:24 -07001579
Colin Cross0af4b842015-04-30 16:36:18 -07001580 if ctx.Darwin() {
1581 flags.LdFlags = append(flags.LdFlags,
1582 "-dynamiclib",
1583 "-single_module",
1584 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001585 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001586 )
1587 } else {
1588 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001589 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001590 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001591 )
1592 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001593 }
Colin Cross97ba0732015-03-23 17:50:24 -07001594
1595 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001596}
1597
Colin Crossca860ac2016-01-04 14:34:37 -08001598func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1599 deps = library.baseLinker.deps(ctx, deps)
1600 if library.static() {
1601 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1602 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1603 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1604 } else {
1605 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1606 if !ctx.sdk() {
1607 deps.CrtBegin = "crtbegin_so"
1608 deps.CrtEnd = "crtend_so"
1609 } else {
1610 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1611 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1612 }
1613 }
1614 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1615 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1616 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1617 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001618
Colin Crossca860ac2016-01-04 14:34:37 -08001619 return deps
1620}
Colin Cross3f40fa42015-01-30 17:27:36 -08001621
Colin Crossca860ac2016-01-04 14:34:37 -08001622func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001623 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001624
Colin Cross635c3b02016-05-18 15:37:25 -07001625 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001626 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001627
Colin Cross635c3b02016-05-18 15:37:25 -07001628 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001629 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001630
Colin Cross0af4b842015-04-30 16:36:18 -07001631 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001632 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001633 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001634 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001635 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001636
Colin Crossca860ac2016-01-04 14:34:37 -08001637 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001638
1639 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001640
1641 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001642}
1643
Colin Crossca860ac2016-01-04 14:34:37 -08001644func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001645 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001646
Colin Cross635c3b02016-05-18 15:37:25 -07001647 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001648
Colin Cross635c3b02016-05-18 15:37:25 -07001649 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1650 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1651 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1652 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001653 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001654 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001655 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001656 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001657 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001658 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001659 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1660 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001661 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001662 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1663 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001664 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001665 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1666 }
1667 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001668 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001669 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1670 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001671 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001672 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001673 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001674 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001675 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001676 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001677 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001678 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001679 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001680 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001681 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001682 }
Colin Crossaee540a2015-07-06 17:48:31 -07001683 }
1684
Colin Cross665dce92016-04-28 14:50:03 -07001685 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001686 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001687 ret := outputFile
1688
1689 builderFlags := flagsToBuilderFlags(flags)
1690
1691 if library.stripper.needsStrip(ctx) {
1692 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001693 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001694 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1695 }
1696
Colin Crossca860ac2016-01-04 14:34:37 -08001697 sharedLibs := deps.SharedLibs
1698 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001699
Colin Crossca860ac2016-01-04 14:34:37 -08001700 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1701 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001702 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001703
Colin Cross665dce92016-04-28 14:50:03 -07001704 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001705}
1706
Colin Crossca860ac2016-01-04 14:34:37 -08001707func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001708 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001709
Colin Crossc99deeb2016-04-11 15:06:20 -07001710 objFiles = append(objFiles, deps.ObjFiles...)
1711
Colin Cross635c3b02016-05-18 15:37:25 -07001712 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001713 if library.static() {
1714 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001715 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001716 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001717 }
1718
Colin Cross919281a2016-04-05 16:42:05 -07001719 library.exportIncludes(ctx, "-I")
1720 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001721
1722 return out
1723}
1724
1725func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001726 return library.dynamicProperties.BuildStatic &&
1727 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001728}
1729
1730func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001731 return library.dynamicProperties.BuildShared &&
1732 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001733}
1734
1735func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1736 return library.wholeStaticMissingDeps
1737}
1738
Colin Crossc99deeb2016-04-11 15:06:20 -07001739func (library *libraryLinker) installable() bool {
1740 return !library.static()
1741}
1742
Colin Crossca860ac2016-01-04 14:34:37 -08001743type libraryInstaller struct {
1744 baseInstaller
1745
Colin Cross30d5f512016-05-03 18:02:42 -07001746 linker *libraryLinker
1747 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001748}
1749
Colin Cross635c3b02016-05-18 15:37:25 -07001750func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001751 if !library.linker.static() {
1752 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001753 }
1754}
1755
Colin Cross30d5f512016-05-03 18:02:42 -07001756func (library *libraryInstaller) inData() bool {
1757 return library.baseInstaller.inData() || library.sanitize.inData()
1758}
1759
Colin Cross635c3b02016-05-18 15:37:25 -07001760func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1761 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001762
Colin Crossca860ac2016-01-04 14:34:37 -08001763 linker := &libraryLinker{}
1764 linker.dynamicProperties.BuildShared = shared
1765 linker.dynamicProperties.BuildStatic = static
1766 module.linker = linker
1767
1768 module.compiler = &libraryCompiler{
1769 linker: linker,
1770 }
1771 module.installer = &libraryInstaller{
1772 baseInstaller: baseInstaller{
1773 dir: "lib",
1774 dir64: "lib64",
1775 },
Colin Cross30d5f512016-05-03 18:02:42 -07001776 linker: linker,
1777 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001778 }
1779
Colin Crossca860ac2016-01-04 14:34:37 -08001780 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001781}
1782
Colin Crossca860ac2016-01-04 14:34:37 -08001783func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001784 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001785 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001786}
1787
Colin Cross3f40fa42015-01-30 17:27:36 -08001788//
1789// Objects (for crt*.o)
1790//
1791
Colin Crossca860ac2016-01-04 14:34:37 -08001792type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001793 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001794}
1795
Colin Crossca860ac2016-01-04 14:34:37 -08001796func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001797 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001798 module.compiler = &baseCompiler{}
1799 module.linker = &objectLinker{}
1800 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001801}
1802
Colin Cross81413472016-04-11 14:37:39 -07001803func (object *objectLinker) props() []interface{} {
1804 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001805}
1806
Colin Crossca860ac2016-01-04 14:34:37 -08001807func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001808
Colin Cross81413472016-04-11 14:37:39 -07001809func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1810 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001811 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001812}
1813
Colin Crossca860ac2016-01-04 14:34:37 -08001814func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001815 if flags.Clang {
1816 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1817 } else {
1818 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1819 }
1820
Colin Crossca860ac2016-01-04 14:34:37 -08001821 return flags
1822}
1823
1824func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001825 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001826
Colin Cross97ba0732015-03-23 17:50:24 -07001827 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001828
Colin Cross635c3b02016-05-18 15:37:25 -07001829 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001830 if len(objFiles) == 1 {
1831 outputFile = objFiles[0]
1832 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001833 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001834 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001835 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001836 }
1837
Colin Cross3f40fa42015-01-30 17:27:36 -08001838 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001839 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001840}
1841
Colin Crossc99deeb2016-04-11 15:06:20 -07001842func (*objectLinker) installable() bool {
1843 return false
1844}
1845
Colin Cross3f40fa42015-01-30 17:27:36 -08001846//
1847// Executables
1848//
1849
Colin Crossca860ac2016-01-04 14:34:37 -08001850type binaryLinker struct {
1851 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001852 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001853
Colin Crossca860ac2016-01-04 14:34:37 -08001854 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001855
Colin Cross635c3b02016-05-18 15:37:25 -07001856 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001857}
1858
Colin Crossca860ac2016-01-04 14:34:37 -08001859var _ linker = (*binaryLinker)(nil)
1860
1861func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001862 return append(binary.baseLinker.props(),
1863 &binary.Properties,
1864 &binary.stripper.StripProperties)
1865
Colin Cross3f40fa42015-01-30 17:27:36 -08001866}
1867
Colin Crossca860ac2016-01-04 14:34:37 -08001868func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001869 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001870}
1871
Colin Crossca860ac2016-01-04 14:34:37 -08001872func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001873 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001874}
1875
Colin Crossca860ac2016-01-04 14:34:37 -08001876func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001877 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001878 if binary.Properties.Stem != "" {
1879 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001880 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001881
Colin Crossca860ac2016-01-04 14:34:37 -08001882 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001883}
1884
Colin Crossca860ac2016-01-04 14:34:37 -08001885func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1886 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001887 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001888 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001889 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001890 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001891 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001892 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001893 }
Colin Crossca860ac2016-01-04 14:34:37 -08001894 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001895 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001896 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001897 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001898 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001899 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001900 }
Colin Crossca860ac2016-01-04 14:34:37 -08001901 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001902 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001903
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001904 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001905 if inList("libc++_static", deps.StaticLibs) {
1906 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001907 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001908 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1909 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1910 // move them to the beginning of deps.LateStaticLibs
1911 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001912 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001913 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001914 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001915 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001916 }
Colin Crossca860ac2016-01-04 14:34:37 -08001917
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001918 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001919 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1920 "from static libs or set static_executable: true")
1921 }
1922 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001923}
1924
Colin Crossc99deeb2016-04-11 15:06:20 -07001925func (*binaryLinker) installable() bool {
1926 return true
1927}
1928
Colin Cross16b23492016-01-06 14:41:07 -08001929func (binary *binaryLinker) isDependencyRoot() bool {
1930 return true
1931}
1932
Colin Cross635c3b02016-05-18 15:37:25 -07001933func NewBinary(hod android.HostOrDeviceSupported) *Module {
1934 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001935 module.compiler = &baseCompiler{}
1936 module.linker = &binaryLinker{}
1937 module.installer = &baseInstaller{
1938 dir: "bin",
1939 }
1940 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001941}
1942
Colin Crossca860ac2016-01-04 14:34:37 -08001943func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001944 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001945 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001946}
1947
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001948func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1949 binary.baseLinker.begin(ctx)
1950
1951 static := Bool(binary.Properties.Static_executable)
1952 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001953 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001954 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1955 static = true
1956 }
1957 } else {
1958 // Static executables are not supported on Darwin or Windows
1959 static = false
1960 }
Colin Cross0af4b842015-04-30 16:36:18 -07001961 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001962 if static {
1963 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001964 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001965 }
1966}
1967
Colin Crossca860ac2016-01-04 14:34:37 -08001968func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1969 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001970
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001971 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001972 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07001973 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001974 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1975 }
1976 }
1977
1978 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1979 // all code is position independent, and then those warnings get promoted to
1980 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001981 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001982 flags.CFlags = append(flags.CFlags, "-fpie")
1983 }
Colin Cross97ba0732015-03-23 17:50:24 -07001984
Colin Crossf6566ed2015-03-24 11:13:38 -07001985 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001986 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001987 // Clang driver needs -static to create static executable.
1988 // However, bionic/linker uses -shared to overwrite.
1989 // Linker for x86 targets does not allow coexistance of -static and -shared,
1990 // so we add -static only if -shared is not used.
1991 if !inList("-shared", flags.LdFlags) {
1992 flags.LdFlags = append(flags.LdFlags, "-static")
1993 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001994
Colin Crossed4cf0b2015-03-26 14:43:45 -07001995 flags.LdFlags = append(flags.LdFlags,
1996 "-nostdlib",
1997 "-Bstatic",
1998 "-Wl,--gc-sections",
1999 )
2000
2001 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002002 if flags.DynamicLinker == "" {
2003 flags.DynamicLinker = "/system/bin/linker"
2004 if flags.Toolchain.Is64Bit() {
2005 flags.DynamicLinker += "64"
2006 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002007 }
2008
2009 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002010 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002011 "-nostdlib",
2012 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002013 "-Wl,--gc-sections",
2014 "-Wl,-z,nocopyreloc",
2015 )
2016 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002017 } else {
2018 if binary.staticBinary() {
2019 flags.LdFlags = append(flags.LdFlags, "-static")
2020 }
2021 if ctx.Darwin() {
2022 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2023 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002024 }
2025
Colin Cross97ba0732015-03-23 17:50:24 -07002026 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002027}
2028
Colin Crossca860ac2016-01-04 14:34:37 -08002029func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002030 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002031
Colin Cross665dce92016-04-28 14:50:03 -07002032 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002033 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002034 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002035 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002036 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002037 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002038
Colin Cross635c3b02016-05-18 15:37:25 -07002039 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002040
Colin Crossca860ac2016-01-04 14:34:37 -08002041 sharedLibs := deps.SharedLibs
2042 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2043
Colin Cross16b23492016-01-06 14:41:07 -08002044 if flags.DynamicLinker != "" {
2045 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2046 }
2047
Colin Cross665dce92016-04-28 14:50:03 -07002048 builderFlags := flagsToBuilderFlags(flags)
2049
2050 if binary.stripper.needsStrip(ctx) {
2051 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002052 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002053 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2054 }
2055
2056 if binary.Properties.Prefix_symbols != "" {
2057 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002058 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002059 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2060 flagsToBuilderFlags(flags), afterPrefixSymbols)
2061 }
2062
Colin Crossca860ac2016-01-04 14:34:37 -08002063 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002064 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002065 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002066
2067 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002068}
Colin Cross3f40fa42015-01-30 17:27:36 -08002069
Colin Cross635c3b02016-05-18 15:37:25 -07002070func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002071 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002072}
2073
Colin Cross665dce92016-04-28 14:50:03 -07002074type stripper struct {
2075 StripProperties StripProperties
2076}
2077
2078func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2079 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2080}
2081
Colin Cross635c3b02016-05-18 15:37:25 -07002082func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002083 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002084 if ctx.Darwin() {
2085 TransformDarwinStrip(ctx, in, out)
2086 } else {
2087 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2088 // TODO(ccross): don't add gnu debuglink for user builds
2089 flags.stripAddGnuDebuglink = true
2090 TransformStrip(ctx, in, out, flags)
2091 }
Colin Cross665dce92016-04-28 14:50:03 -07002092}
2093
Colin Cross635c3b02016-05-18 15:37:25 -07002094func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002095 if m, ok := mctx.Module().(*Module); ok {
2096 if test, ok := m.linker.(*testLinker); ok {
2097 if Bool(test.Properties.Test_per_src) {
2098 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2099 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2100 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2101 }
2102 tests := mctx.CreateLocalVariations(testNames...)
2103 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2104 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2105 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2106 }
Colin Cross6002e052015-09-16 16:00:08 -07002107 }
2108 }
2109 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002110}
2111
Colin Crossca860ac2016-01-04 14:34:37 -08002112type testLinker struct {
2113 binaryLinker
2114 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002115}
2116
Dan Willemsend30e6102016-03-30 17:35:50 -07002117func (test *testLinker) begin(ctx BaseModuleContext) {
2118 test.binaryLinker.begin(ctx)
2119
2120 runpath := "../../lib"
2121 if ctx.toolchain().Is64Bit() {
2122 runpath += "64"
2123 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002124 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002125}
2126
Colin Crossca860ac2016-01-04 14:34:37 -08002127func (test *testLinker) props() []interface{} {
2128 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002129}
2130
Colin Crossca860ac2016-01-04 14:34:37 -08002131func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2132 flags = test.binaryLinker.flags(ctx, flags)
2133
2134 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002135 return flags
2136 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002137
Colin Cross97ba0732015-03-23 17:50:24 -07002138 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002139 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002140 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002141
Colin Crossa1ad8d12016-06-01 17:09:44 -07002142 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002143 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002144 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002145 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002146 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2147 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002148 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002149 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2150 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002151 }
2152 } else {
2153 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002154 }
2155
Colin Cross21b9a242015-03-24 14:15:58 -07002156 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002157}
2158
Colin Crossca860ac2016-01-04 14:34:37 -08002159func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2160 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002161 if ctx.sdk() && ctx.Device() {
2162 switch ctx.selectedStl() {
2163 case "ndk_libc++_shared", "ndk_libc++_static":
2164 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2165 case "ndk_libgnustl_static":
2166 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2167 default:
2168 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2169 }
2170 } else {
2171 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2172 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002173 }
Colin Crossca860ac2016-01-04 14:34:37 -08002174 deps = test.binaryLinker.deps(ctx, deps)
2175 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002176}
2177
Colin Crossca860ac2016-01-04 14:34:37 -08002178type testInstaller struct {
2179 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002180}
2181
Colin Cross635c3b02016-05-18 15:37:25 -07002182func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002183 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2184 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2185 installer.baseInstaller.install(ctx, file)
2186}
2187
Colin Cross635c3b02016-05-18 15:37:25 -07002188func NewTest(hod android.HostOrDeviceSupported) *Module {
2189 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002190 module.compiler = &baseCompiler{}
2191 linker := &testLinker{}
2192 linker.Properties.Gtest = true
2193 module.linker = linker
2194 module.installer = &testInstaller{
2195 baseInstaller: baseInstaller{
2196 dir: "nativetest",
2197 dir64: "nativetest64",
2198 data: true,
2199 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002200 }
Colin Crossca860ac2016-01-04 14:34:37 -08002201 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002202}
2203
Colin Crossca860ac2016-01-04 14:34:37 -08002204func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002205 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002206 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002207}
2208
Colin Crossca860ac2016-01-04 14:34:37 -08002209type benchmarkLinker struct {
2210 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002211}
2212
Colin Crossca860ac2016-01-04 14:34:37 -08002213func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2214 deps = benchmark.binaryLinker.deps(ctx, deps)
2215 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2216 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002217}
2218
Colin Cross635c3b02016-05-18 15:37:25 -07002219func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2220 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002221 module.compiler = &baseCompiler{}
2222 module.linker = &benchmarkLinker{}
2223 module.installer = &baseInstaller{
2224 dir: "nativetest",
2225 dir64: "nativetest64",
2226 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002227 }
Colin Crossca860ac2016-01-04 14:34:37 -08002228 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002229}
2230
Colin Crossca860ac2016-01-04 14:34:37 -08002231func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002232 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002233 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002234}
2235
Colin Cross3f40fa42015-01-30 17:27:36 -08002236//
2237// Static library
2238//
2239
Colin Crossca860ac2016-01-04 14:34:37 -08002240func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002241 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002242 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002243}
2244
2245//
2246// Shared libraries
2247//
2248
Colin Crossca860ac2016-01-04 14:34:37 -08002249func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002250 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002251 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002252}
2253
2254//
2255// Host static library
2256//
2257
Colin Crossca860ac2016-01-04 14:34:37 -08002258func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002259 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002260 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002261}
2262
2263//
2264// Host Shared libraries
2265//
2266
Colin Crossca860ac2016-01-04 14:34:37 -08002267func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002268 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002269 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002270}
2271
2272//
2273// Host Binaries
2274//
2275
Colin Crossca860ac2016-01-04 14:34:37 -08002276func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002277 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002278 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002279}
2280
2281//
Colin Cross1f8f2342015-03-26 16:09:47 -07002282// Host Tests
2283//
2284
Colin Crossca860ac2016-01-04 14:34:37 -08002285func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002286 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002287 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002288}
2289
2290//
Colin Cross2ba19d92015-05-07 15:44:20 -07002291// Host Benchmarks
2292//
2293
Colin Crossca860ac2016-01-04 14:34:37 -08002294func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002295 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002296 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002297}
2298
2299//
Colin Crosscfad1192015-11-02 16:43:11 -08002300// Defaults
2301//
Colin Crossca860ac2016-01-04 14:34:37 -08002302type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002303 android.ModuleBase
2304 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002305}
2306
Colin Cross635c3b02016-05-18 15:37:25 -07002307func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002308}
2309
Colin Crossca860ac2016-01-04 14:34:37 -08002310func defaultsFactory() (blueprint.Module, []interface{}) {
2311 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002312
2313 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002314 &BaseProperties{},
2315 &BaseCompilerProperties{},
2316 &BaseLinkerProperties{},
2317 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002318 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002319 &LibraryLinkerProperties{},
2320 &BinaryLinkerProperties{},
2321 &TestLinkerProperties{},
2322 &UnusedProperties{},
2323 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002324 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002325 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002326 }
2327
Colin Cross635c3b02016-05-18 15:37:25 -07002328 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2329 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002330
Colin Cross635c3b02016-05-18 15:37:25 -07002331 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002332}
2333
2334//
Colin Cross3f40fa42015-01-30 17:27:36 -08002335// Device libraries shipped with gcc
2336//
2337
Colin Crossca860ac2016-01-04 14:34:37 -08002338type toolchainLibraryLinker struct {
2339 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002340}
2341
Colin Crossca860ac2016-01-04 14:34:37 -08002342var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2343
2344func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002345 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002346 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002347}
2348
Colin Crossca860ac2016-01-04 14:34:37 -08002349func (*toolchainLibraryLinker) buildStatic() bool {
2350 return true
2351}
Colin Cross3f40fa42015-01-30 17:27:36 -08002352
Colin Crossca860ac2016-01-04 14:34:37 -08002353func (*toolchainLibraryLinker) buildShared() bool {
2354 return false
2355}
2356
2357func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002358 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002359 module.compiler = &baseCompiler{}
2360 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002361 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002362 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002363}
2364
Colin Crossca860ac2016-01-04 14:34:37 -08002365func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002366 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002367
2368 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002369 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002370
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002371 if flags.Clang {
2372 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2373 }
2374
Colin Crossca860ac2016-01-04 14:34:37 -08002375 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002376
2377 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002378
Colin Crossca860ac2016-01-04 14:34:37 -08002379 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002380}
2381
Colin Crossc99deeb2016-04-11 15:06:20 -07002382func (*toolchainLibraryLinker) installable() bool {
2383 return false
2384}
2385
Dan Albertbe961682015-03-18 23:38:50 -07002386// NDK prebuilt libraries.
2387//
2388// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2389// either (with the exception of the shared STLs, which are installed to the app's directory rather
2390// than to the system image).
2391
Colin Cross635c3b02016-05-18 15:37:25 -07002392func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002393 suffix := ""
2394 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2395 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002396 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002397 suffix = "64"
2398 }
Colin Cross635c3b02016-05-18 15:37:25 -07002399 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002400 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002401}
2402
Colin Cross635c3b02016-05-18 15:37:25 -07002403func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2404 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002405
2406 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2407 // We want to translate to just NAME.EXT
2408 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2409 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002410 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002411}
2412
Colin Crossca860ac2016-01-04 14:34:37 -08002413type ndkPrebuiltObjectLinker struct {
2414 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002415}
2416
Colin Crossca860ac2016-01-04 14:34:37 -08002417func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002418 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002419 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002420}
2421
Colin Crossca860ac2016-01-04 14:34:37 -08002422func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002423 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002424 module.linker = &ndkPrebuiltObjectLinker{}
2425 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002426}
2427
Colin Crossca860ac2016-01-04 14:34:37 -08002428func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002429 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002430 // A null build step, but it sets up the output path.
2431 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2432 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2433 }
2434
Colin Crossca860ac2016-01-04 14:34:37 -08002435 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002436}
2437
Colin Crossca860ac2016-01-04 14:34:37 -08002438type ndkPrebuiltLibraryLinker struct {
2439 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002440}
2441
Colin Crossca860ac2016-01-04 14:34:37 -08002442var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2443var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002444
Colin Crossca860ac2016-01-04 14:34:37 -08002445func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002446 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002447}
2448
Colin Crossca860ac2016-01-04 14:34:37 -08002449func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002450 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002451 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002452}
2453
Colin Crossca860ac2016-01-04 14:34:37 -08002454func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002455 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002456 linker := &ndkPrebuiltLibraryLinker{}
2457 linker.dynamicProperties.BuildShared = true
2458 module.linker = linker
2459 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002460}
2461
Colin Crossca860ac2016-01-04 14:34:37 -08002462func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002463 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002464 // A null build step, but it sets up the output path.
2465 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2466 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2467 }
2468
Colin Cross919281a2016-04-05 16:42:05 -07002469 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002470
Colin Crossca860ac2016-01-04 14:34:37 -08002471 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2472 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002473}
2474
2475// The NDK STLs are slightly different from the prebuilt system libraries:
2476// * Are not specific to each platform version.
2477// * The libraries are not in a predictable location for each STL.
2478
Colin Crossca860ac2016-01-04 14:34:37 -08002479type ndkPrebuiltStlLinker struct {
2480 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002481}
2482
Colin Crossca860ac2016-01-04 14:34:37 -08002483func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002484 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002485 linker := &ndkPrebuiltStlLinker{}
2486 linker.dynamicProperties.BuildShared = true
2487 module.linker = linker
2488 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002489}
2490
Colin Crossca860ac2016-01-04 14:34:37 -08002491func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002492 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002493 linker := &ndkPrebuiltStlLinker{}
2494 linker.dynamicProperties.BuildStatic = true
2495 module.linker = linker
2496 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002497}
2498
Colin Cross635c3b02016-05-18 15:37:25 -07002499func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002500 gccVersion := toolchain.GccVersion()
2501 var libDir string
2502 switch stl {
2503 case "libstlport":
2504 libDir = "cxx-stl/stlport/libs"
2505 case "libc++":
2506 libDir = "cxx-stl/llvm-libc++/libs"
2507 case "libgnustl":
2508 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2509 }
2510
2511 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002512 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002513 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002514 }
2515
2516 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002517 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002518}
2519
Colin Crossca860ac2016-01-04 14:34:37 -08002520func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002521 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002522 // A null build step, but it sets up the output path.
2523 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2524 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2525 }
2526
Colin Cross919281a2016-04-05 16:42:05 -07002527 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002528
2529 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002530 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002531 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002532 libExt = staticLibraryExtension
2533 }
2534
2535 stlName := strings.TrimSuffix(libName, "_shared")
2536 stlName = strings.TrimSuffix(stlName, "_static")
2537 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002538 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002539}
2540
Colin Cross635c3b02016-05-18 15:37:25 -07002541func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002542 if m, ok := mctx.Module().(*Module); ok {
2543 if m.linker != nil {
2544 if linker, ok := m.linker.(baseLinkerInterface); ok {
2545 var modules []blueprint.Module
2546 if linker.buildStatic() && linker.buildShared() {
2547 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002548 static := modules[0].(*Module)
2549 shared := modules[1].(*Module)
2550
2551 static.linker.(baseLinkerInterface).setStatic(true)
2552 shared.linker.(baseLinkerInterface).setStatic(false)
2553
2554 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2555 sharedCompiler := shared.compiler.(*libraryCompiler)
2556 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2557 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2558 // Optimize out compiling common .o files twice for static+shared libraries
2559 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2560 sharedCompiler.baseCompiler.Properties.Srcs = nil
2561 }
2562 }
Colin Crossca860ac2016-01-04 14:34:37 -08002563 } else if linker.buildStatic() {
2564 modules = mctx.CreateLocalVariations("static")
2565 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2566 } else if linker.buildShared() {
2567 modules = mctx.CreateLocalVariations("shared")
2568 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2569 } else {
2570 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2571 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002572 }
2573 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002574 }
2575}
Colin Cross74d1ec02015-04-28 13:30:13 -07002576
2577// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2578// modifies the slice contents in place, and returns a subslice of the original slice
2579func lastUniqueElements(list []string) []string {
2580 totalSkip := 0
2581 for i := len(list) - 1; i >= totalSkip; i-- {
2582 skip := 0
2583 for j := i - 1; j >= totalSkip; j-- {
2584 if list[i] == list[j] {
2585 skip++
2586 } else {
2587 list[j+skip] = list[j]
2588 }
2589 }
2590 totalSkip += skip
2591 }
2592 return list[totalSkip:]
2593}
Colin Cross06a931b2015-10-28 17:23:31 -07002594
2595var Bool = proptools.Bool