blob: 8206912b32cfd2b4436814b1e608a0723355b242 [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 {
454 module() *Module
455 static() bool
456 staticBinary() bool
457 clang() bool
458 toolchain() Toolchain
459 noDefaultCompilerFlags() bool
460 sdk() bool
461 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700462 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800463}
464
465type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700466 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800467 ModuleContextIntf
468}
469
470type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700471 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800472 ModuleContextIntf
473}
474
475type Customizer interface {
476 CustomizeProperties(BaseModuleContext)
477 Properties() []interface{}
478}
479
480type feature interface {
481 begin(ctx BaseModuleContext)
482 deps(ctx BaseModuleContext, deps Deps) Deps
483 flags(ctx ModuleContext, flags Flags) Flags
484 props() []interface{}
485}
486
487type compiler interface {
488 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700489 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800490}
491
492type linker interface {
493 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700494 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700495 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800496}
497
498type installer interface {
499 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700500 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800501 inData() bool
502}
503
Colin Crossc99deeb2016-04-11 15:06:20 -0700504type dependencyTag struct {
505 blueprint.BaseDependencyTag
506 name string
507 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700508
509 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700510}
511
512var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700513 sharedDepTag = dependencyTag{name: "shared", library: true}
514 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
515 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
516 staticDepTag = dependencyTag{name: "static", library: true}
517 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
518 lateStaticDepTag = dependencyTag{name: "late static", library: true}
519 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
520 genSourceDepTag = dependencyTag{name: "gen source"}
521 genHeaderDepTag = dependencyTag{name: "gen header"}
522 objDepTag = dependencyTag{name: "obj"}
523 crtBeginDepTag = dependencyTag{name: "crtbegin"}
524 crtEndDepTag = dependencyTag{name: "crtend"}
525 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700526)
527
Colin Crossca860ac2016-01-04 14:34:37 -0800528// Module contains the properties and members used by all C/C++ module types, and implements
529// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
530// to construct the output file. Behavior can be customized with a Customizer interface
531type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700532 android.ModuleBase
533 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700534
Colin Crossca860ac2016-01-04 14:34:37 -0800535 Properties BaseProperties
536 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700537
Colin Crossca860ac2016-01-04 14:34:37 -0800538 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700539 hod android.HostOrDeviceSupported
540 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700541
Colin Crossca860ac2016-01-04 14:34:37 -0800542 // delegates, initialize before calling Init
543 customizer Customizer
544 features []feature
545 compiler compiler
546 linker linker
547 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700548 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800549 sanitize *sanitize
550
551 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700552
Colin Cross635c3b02016-05-18 15:37:25 -0700553 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800554
555 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700556}
557
Colin Crossca860ac2016-01-04 14:34:37 -0800558func (c *Module) Init() (blueprint.Module, []interface{}) {
559 props := []interface{}{&c.Properties, &c.unused}
560 if c.customizer != nil {
561 props = append(props, c.customizer.Properties()...)
562 }
563 if c.compiler != nil {
564 props = append(props, c.compiler.props()...)
565 }
566 if c.linker != nil {
567 props = append(props, c.linker.props()...)
568 }
569 if c.installer != nil {
570 props = append(props, c.installer.props()...)
571 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700572 if c.stl != nil {
573 props = append(props, c.stl.props()...)
574 }
Colin Cross16b23492016-01-06 14:41:07 -0800575 if c.sanitize != nil {
576 props = append(props, c.sanitize.props()...)
577 }
Colin Crossca860ac2016-01-04 14:34:37 -0800578 for _, feature := range c.features {
579 props = append(props, feature.props()...)
580 }
Colin Crossc472d572015-03-17 15:06:21 -0700581
Colin Cross635c3b02016-05-18 15:37:25 -0700582 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700583
Colin Cross635c3b02016-05-18 15:37:25 -0700584 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700585}
586
Colin Crossca860ac2016-01-04 14:34:37 -0800587type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700588 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800589 moduleContextImpl
590}
591
592type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700593 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800594 moduleContextImpl
595}
596
597type moduleContextImpl struct {
598 mod *Module
599 ctx BaseModuleContext
600}
601
602func (ctx *moduleContextImpl) module() *Module {
603 return ctx.mod
604}
605
606func (ctx *moduleContextImpl) clang() bool {
607 return ctx.mod.clang(ctx.ctx)
608}
609
610func (ctx *moduleContextImpl) toolchain() Toolchain {
611 return ctx.mod.toolchain(ctx.ctx)
612}
613
614func (ctx *moduleContextImpl) static() bool {
615 if ctx.mod.linker == nil {
616 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
617 }
618 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
619 return linker.static()
620 } else {
621 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
622 }
623}
624
625func (ctx *moduleContextImpl) staticBinary() bool {
626 if ctx.mod.linker == nil {
627 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
628 }
629 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
630 return linker.staticBinary()
631 } else {
632 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
633 }
634}
635
636func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
637 return Bool(ctx.mod.Properties.No_default_compiler_flags)
638}
639
640func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700641 if ctx.ctx.Device() {
642 return ctx.mod.Properties.Sdk_version != ""
643 }
644 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800645}
646
647func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700648 if ctx.ctx.Device() {
649 return ctx.mod.Properties.Sdk_version
650 }
651 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800652}
653
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700654func (ctx *moduleContextImpl) selectedStl() string {
655 if stl := ctx.mod.stl; stl != nil {
656 return stl.Properties.SelectedStl
657 }
658 return ""
659}
660
Colin Cross635c3b02016-05-18 15:37:25 -0700661func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800662 return &Module{
663 hod: hod,
664 multilib: multilib,
665 }
666}
667
Colin Cross635c3b02016-05-18 15:37:25 -0700668func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800669 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700670 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800671 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800672 return module
673}
674
Colin Cross635c3b02016-05-18 15:37:25 -0700675func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800676 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700677 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800678 moduleContextImpl: moduleContextImpl{
679 mod: c,
680 },
681 }
682 ctx.ctx = ctx
683
684 flags := Flags{
685 Toolchain: c.toolchain(ctx),
686 Clang: c.clang(ctx),
687 }
Colin Crossca860ac2016-01-04 14:34:37 -0800688 if c.compiler != nil {
689 flags = c.compiler.flags(ctx, flags)
690 }
691 if c.linker != nil {
692 flags = c.linker.flags(ctx, flags)
693 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700694 if c.stl != nil {
695 flags = c.stl.flags(ctx, flags)
696 }
Colin Cross16b23492016-01-06 14:41:07 -0800697 if c.sanitize != nil {
698 flags = c.sanitize.flags(ctx, flags)
699 }
Colin Crossca860ac2016-01-04 14:34:37 -0800700 for _, feature := range c.features {
701 flags = feature.flags(ctx, flags)
702 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800703 if ctx.Failed() {
704 return
705 }
706
Colin Crossca860ac2016-01-04 14:34:37 -0800707 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
708 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
709 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800710
Colin Crossca860ac2016-01-04 14:34:37 -0800711 // Optimization to reduce size of build.ninja
712 // Replace the long list of flags for each file with a module-local variable
713 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
714 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
715 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
716 flags.CFlags = []string{"$cflags"}
717 flags.CppFlags = []string{"$cppflags"}
718 flags.AsFlags = []string{"$asflags"}
719
Colin Crossc99deeb2016-04-11 15:06:20 -0700720 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800721 if ctx.Failed() {
722 return
723 }
724
Colin Cross28344522015-04-22 13:07:53 -0700725 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700726
Colin Cross635c3b02016-05-18 15:37:25 -0700727 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800728 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700729 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800730 if ctx.Failed() {
731 return
732 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800733 }
734
Colin Crossca860ac2016-01-04 14:34:37 -0800735 if c.linker != nil {
736 outputFile := c.linker.link(ctx, flags, deps, objFiles)
737 if ctx.Failed() {
738 return
739 }
Colin Cross635c3b02016-05-18 15:37:25 -0700740 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700741
Colin Crossc99deeb2016-04-11 15:06:20 -0700742 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800743 c.installer.install(ctx, outputFile)
744 if ctx.Failed() {
745 return
746 }
747 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700748 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800749}
750
Colin Crossca860ac2016-01-04 14:34:37 -0800751func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
752 if c.cachedToolchain == nil {
753 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700754 os := ctx.Os()
755 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800756 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700757 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800758 return nil
759 }
760 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800761 }
Colin Crossca860ac2016-01-04 14:34:37 -0800762 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800763}
764
Colin Crossca860ac2016-01-04 14:34:37 -0800765func (c *Module) begin(ctx BaseModuleContext) {
766 if c.compiler != nil {
767 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700768 }
Colin Crossca860ac2016-01-04 14:34:37 -0800769 if c.linker != nil {
770 c.linker.begin(ctx)
771 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700772 if c.stl != nil {
773 c.stl.begin(ctx)
774 }
Colin Cross16b23492016-01-06 14:41:07 -0800775 if c.sanitize != nil {
776 c.sanitize.begin(ctx)
777 }
Colin Crossca860ac2016-01-04 14:34:37 -0800778 for _, feature := range c.features {
779 feature.begin(ctx)
780 }
781}
782
Colin Crossc99deeb2016-04-11 15:06:20 -0700783func (c *Module) deps(ctx BaseModuleContext) Deps {
784 deps := Deps{}
785
786 if c.compiler != nil {
787 deps = c.compiler.deps(ctx, deps)
788 }
789 if c.linker != nil {
790 deps = c.linker.deps(ctx, deps)
791 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700792 if c.stl != nil {
793 deps = c.stl.deps(ctx, deps)
794 }
Colin Cross16b23492016-01-06 14:41:07 -0800795 if c.sanitize != nil {
796 deps = c.sanitize.deps(ctx, deps)
797 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700798 for _, feature := range c.features {
799 deps = feature.deps(ctx, deps)
800 }
801
802 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
803 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
804 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
805 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
806 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
807
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700808 for _, lib := range deps.ReexportSharedLibHeaders {
809 if !inList(lib, deps.SharedLibs) {
810 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
811 }
812 }
813
814 for _, lib := range deps.ReexportStaticLibHeaders {
815 if !inList(lib, deps.StaticLibs) {
816 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
817 }
818 }
819
Colin Crossc99deeb2016-04-11 15:06:20 -0700820 return deps
821}
822
Colin Cross635c3b02016-05-18 15:37:25 -0700823func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800824 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700825 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800826 moduleContextImpl: moduleContextImpl{
827 mod: c,
828 },
829 }
830 ctx.ctx = ctx
831
832 if c.customizer != nil {
833 c.customizer.CustomizeProperties(ctx)
834 }
835
836 c.begin(ctx)
837
Colin Crossc99deeb2016-04-11 15:06:20 -0700838 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800839
Colin Crossc99deeb2016-04-11 15:06:20 -0700840 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
841
842 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
843 deps.WholeStaticLibs...)
844
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700845 for _, lib := range deps.StaticLibs {
846 depTag := staticDepTag
847 if inList(lib, deps.ReexportStaticLibHeaders) {
848 depTag = staticExportDepTag
849 }
850 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
851 deps.StaticLibs...)
852 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700853
854 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
855 deps.LateStaticLibs...)
856
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700857 for _, lib := range deps.SharedLibs {
858 depTag := sharedDepTag
859 if inList(lib, deps.ReexportSharedLibHeaders) {
860 depTag = sharedExportDepTag
861 }
862 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
863 deps.SharedLibs...)
864 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700865
866 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
867 deps.LateSharedLibs...)
868
Dan Willemsenb40aab62016-04-20 14:21:14 -0700869 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
870 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
871
Colin Crossc99deeb2016-04-11 15:06:20 -0700872 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
873
874 if deps.CrtBegin != "" {
875 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800876 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700877 if deps.CrtEnd != "" {
878 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700879 }
Colin Cross6362e272015-10-29 15:25:03 -0700880}
Colin Cross21b9a242015-03-24 14:15:58 -0700881
Colin Cross635c3b02016-05-18 15:37:25 -0700882func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800883 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700884 c.depsMutator(ctx)
885 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800886}
887
Colin Crossca860ac2016-01-04 14:34:37 -0800888func (c *Module) clang(ctx BaseModuleContext) bool {
889 clang := Bool(c.Properties.Clang)
890
891 if c.Properties.Clang == nil {
892 if ctx.Host() {
893 clang = true
894 }
895
896 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
897 clang = true
898 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800899 }
Colin Cross28344522015-04-22 13:07:53 -0700900
Colin Crossca860ac2016-01-04 14:34:37 -0800901 if !c.toolchain(ctx).ClangSupported() {
902 clang = false
903 }
904
905 return clang
906}
907
Colin Crossc99deeb2016-04-11 15:06:20 -0700908// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700909func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800910 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800911
Dan Willemsena96ff642016-06-07 12:34:45 -0700912 // Whether a module can link to another module, taking into
913 // account NDK linking.
914 linkTypeOk := func(from, to *Module) bool {
915 if from.Target().Os != android.Android {
916 // Host code is not restricted
917 return true
918 }
919 if from.Properties.Sdk_version == "" {
920 // Platform code can link to anything
921 return true
922 }
923 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
924 // These are always allowed
925 return true
926 }
927 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
928 // These are allowed, but don't set sdk_version
929 return true
930 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700931 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
932 // These are allowed, but don't set sdk_version
933 return true
934 }
935 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700936 }
937
Colin Crossc99deeb2016-04-11 15:06:20 -0700938 ctx.VisitDirectDeps(func(m blueprint.Module) {
939 name := ctx.OtherModuleName(m)
940 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800941
Colin Cross635c3b02016-05-18 15:37:25 -0700942 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700943 if a == nil {
944 ctx.ModuleErrorf("module %q not an android module", name)
945 return
Colin Crossca860ac2016-01-04 14:34:37 -0800946 }
Colin Crossca860ac2016-01-04 14:34:37 -0800947
Dan Willemsena96ff642016-06-07 12:34:45 -0700948 cc, _ := m.(*Module)
949 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700950 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700951 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700952 case genSourceDepTag:
953 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
954 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
955 genRule.GeneratedSourceFiles()...)
956 } else {
957 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
958 }
959 case genHeaderDepTag:
960 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
961 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
962 genRule.GeneratedSourceFiles()...)
963 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700964 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700965 } else {
966 ctx.ModuleErrorf("module %q is not a genrule", name)
967 }
968 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800970 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700971 return
972 }
973
974 if !a.Enabled() {
975 ctx.ModuleErrorf("depends on disabled module %q", name)
976 return
977 }
978
Colin Crossa1ad8d12016-06-01 17:09:44 -0700979 if a.Target().Os != ctx.Os() {
980 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
981 return
982 }
983
984 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
985 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700986 return
987 }
988
Dan Willemsena96ff642016-06-07 12:34:45 -0700989 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700990 ctx.ModuleErrorf("module %q missing output file", name)
991 return
992 }
993
994 if tag == reuseObjTag {
995 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700996 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700997 return
998 }
999
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001000 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001001 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001002 cflags := i.exportedFlags()
Colin Crossc99deeb2016-04-11 15:06:20 -07001003 depPaths.Cflags = append(depPaths.Cflags, cflags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001004
1005 if t.reexportFlags {
1006 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
1007 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001008 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001009
1010 if !linkTypeOk(c, cc) {
1011 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1012 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001013 }
1014
Colin Cross635c3b02016-05-18 15:37:25 -07001015 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001016
1017 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001018 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001019 depPtr = &depPaths.SharedLibs
1020 case lateSharedDepTag:
1021 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001022 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001023 depPtr = &depPaths.StaticLibs
1024 case lateStaticDepTag:
1025 depPtr = &depPaths.LateStaticLibs
1026 case wholeStaticDepTag:
1027 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001028 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001029 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001030 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001031 return
1032 }
1033
1034 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1035 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1036 for i := range missingDeps {
1037 missingDeps[i] += postfix
1038 }
1039 ctx.AddMissingDependencies(missingDeps)
1040 }
1041 depPaths.WholeStaticLibObjFiles =
1042 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1043 case objDepTag:
1044 depPtr = &depPaths.ObjFiles
1045 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001046 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001047 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001048 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001049 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001050 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001051 }
1052
1053 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001054 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001055 }
1056 })
1057
1058 return depPaths
1059}
1060
1061func (c *Module) InstallInData() bool {
1062 if c.installer == nil {
1063 return false
1064 }
1065 return c.installer.inData()
1066}
1067
1068// Compiler
1069
1070type baseCompiler struct {
1071 Properties BaseCompilerProperties
1072}
1073
1074var _ compiler = (*baseCompiler)(nil)
1075
1076func (compiler *baseCompiler) props() []interface{} {
1077 return []interface{}{&compiler.Properties}
1078}
1079
Dan Willemsenb40aab62016-04-20 14:21:14 -07001080func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1081
1082func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1083 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1084 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1085
1086 return deps
1087}
Colin Crossca860ac2016-01-04 14:34:37 -08001088
1089// Create a Flags struct that collects the compile flags from global values,
1090// per-target values, module type values, and per-module Blueprints properties
1091func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1092 toolchain := ctx.toolchain()
1093
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001094 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1095 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1096 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1097 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1098
Colin Crossca860ac2016-01-04 14:34:37 -08001099 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1100 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1101 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1102 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1103 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1104
Colin Cross28344522015-04-22 13:07:53 -07001105 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001106 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1107 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001108 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001109 includeDirsToFlags(localIncludeDirs),
1110 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001111
Colin Crossca860ac2016-01-04 14:34:37 -08001112 if !ctx.noDefaultCompilerFlags() {
1113 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001114 flags.GlobalFlags = append(flags.GlobalFlags,
1115 "${commonGlobalIncludes}",
1116 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001117 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001118 }
1119
1120 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001121 "-I" + android.PathForModuleSrc(ctx).String(),
1122 "-I" + android.PathForModuleOut(ctx).String(),
1123 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001124 }...)
1125 }
1126
Colin Crossca860ac2016-01-04 14:34:37 -08001127 instructionSet := compiler.Properties.Instruction_set
1128 if flags.RequiredInstructionSet != "" {
1129 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001130 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001131 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1132 if flags.Clang {
1133 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1134 }
1135 if err != nil {
1136 ctx.ModuleErrorf("%s", err)
1137 }
1138
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001139 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1140
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001141 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001142 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001143
Colin Cross97ba0732015-03-23 17:50:24 -07001144 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001145 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1146 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1147
Colin Cross97ba0732015-03-23 17:50:24 -07001148 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001149 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1150 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001151 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1152 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1153 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001154
1155 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001156 var gccPrefix string
1157 if !ctx.Darwin() {
1158 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1159 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001160
Colin Cross97ba0732015-03-23 17:50:24 -07001161 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1162 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1163 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001164 }
1165
Colin Crossa1ad8d12016-06-01 17:09:44 -07001166 hod := "host"
1167 if ctx.Os().Class == android.Device {
1168 hod = "device"
1169 }
1170
Colin Crossca860ac2016-01-04 14:34:37 -08001171 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001172 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1173
Colin Cross97ba0732015-03-23 17:50:24 -07001174 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001175 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001176 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001177 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001178 toolchain.ClangCflags(),
1179 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001180 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001181
1182 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001183 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001184 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001185 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001186 toolchain.Cflags(),
1187 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001188 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001189 }
1190
Colin Cross7b66f152015-12-15 16:07:43 -08001191 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1192 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1193 }
1194
Colin Crossf6566ed2015-03-24 11:13:38 -07001195 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001196 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001197 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001198 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001199 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001200 }
1201 }
1202
Colin Cross97ba0732015-03-23 17:50:24 -07001203 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001204
Colin Cross97ba0732015-03-23 17:50:24 -07001205 if flags.Clang {
1206 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001207 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001208 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001209 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001210 }
1211
Colin Crossc4bde762015-11-23 16:11:30 -08001212 if flags.Clang {
1213 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1214 } else {
1215 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001216 }
1217
Colin Crossca860ac2016-01-04 14:34:37 -08001218 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001219 if ctx.Host() && !flags.Clang {
1220 // The host GCC doesn't support C++14 (and is deprecated, so likely
1221 // never will). Build these modules with C++11.
1222 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1223 } else {
1224 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1225 }
1226 }
1227
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001228 // We can enforce some rules more strictly in the code we own. strict
1229 // indicates if this is code that we can be stricter with. If we have
1230 // rules that we want to apply to *our* code (but maybe can't for
1231 // vendor/device specific things), we could extend this to be a ternary
1232 // value.
1233 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001234 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001235 strict = false
1236 }
1237
1238 // Can be used to make some annotations stricter for code we can fix
1239 // (such as when we mark functions as deprecated).
1240 if strict {
1241 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1242 }
1243
Colin Cross3f40fa42015-01-30 17:27:36 -08001244 return flags
1245}
1246
Colin Cross635c3b02016-05-18 15:37:25 -07001247func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001248 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001249 objFiles := compiler.compileObjs(ctx, flags, "",
1250 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1251 deps.GeneratedSources, deps.GeneratedHeaders)
1252
Colin Crossca860ac2016-01-04 14:34:37 -08001253 if ctx.Failed() {
1254 return nil
1255 }
1256
Colin Crossca860ac2016-01-04 14:34:37 -08001257 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001258}
1259
1260// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001261func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1262 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001263
Colin Crossca860ac2016-01-04 14:34:37 -08001264 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001265
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001266 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001267 inputFiles = append(inputFiles, extraSrcs...)
1268 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1269
1270 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001271 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001272
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001273 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001274}
1275
Colin Crossca860ac2016-01-04 14:34:37 -08001276// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1277type baseLinker struct {
1278 Properties BaseLinkerProperties
1279 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001280 VariantIsShared bool `blueprint:"mutated"`
1281 VariantIsStatic bool `blueprint:"mutated"`
1282 VariantIsStaticBinary bool `blueprint:"mutated"`
1283 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001284 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001285}
1286
Dan Willemsend30e6102016-03-30 17:35:50 -07001287func (linker *baseLinker) begin(ctx BaseModuleContext) {
1288 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001289 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001290 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001291 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001292 }
1293}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001294
Colin Crossca860ac2016-01-04 14:34:37 -08001295func (linker *baseLinker) props() []interface{} {
1296 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001297}
1298
Colin Crossca860ac2016-01-04 14:34:37 -08001299func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1300 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1301 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1302 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001303
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001304 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1305 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1306
Dan Willemsena96ff642016-06-07 12:34:45 -07001307 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001308 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001309 }
1310
Colin Crossf6566ed2015-03-24 11:13:38 -07001311 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001312 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001313 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1314 if !Bool(linker.Properties.No_libgcc) {
1315 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001316 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001317
Colin Crossca860ac2016-01-04 14:34:37 -08001318 if !linker.static() {
1319 if linker.Properties.System_shared_libs != nil {
1320 deps.LateSharedLibs = append(deps.LateSharedLibs,
1321 linker.Properties.System_shared_libs...)
1322 } else if !ctx.sdk() {
1323 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1324 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001325 }
Colin Cross577f6e42015-03-27 18:23:34 -07001326
Colin Crossca860ac2016-01-04 14:34:37 -08001327 if ctx.sdk() {
1328 version := ctx.sdkVersion()
1329 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001330 "ndk_libc."+version,
1331 "ndk_libm."+version,
1332 )
1333 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001334 }
1335
Colin Crossca860ac2016-01-04 14:34:37 -08001336 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001337}
1338
Colin Crossca860ac2016-01-04 14:34:37 -08001339func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1340 toolchain := ctx.toolchain()
1341
Colin Crossca860ac2016-01-04 14:34:37 -08001342 if !ctx.noDefaultCompilerFlags() {
1343 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1344 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1345 }
1346
1347 if flags.Clang {
1348 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1349 } else {
1350 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1351 }
1352
1353 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001354 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1355
Colin Crossca860ac2016-01-04 14:34:37 -08001356 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1357 }
1358 }
1359
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001360 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1361
Dan Willemsen00ced762016-05-10 17:31:21 -07001362 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1363
Dan Willemsend30e6102016-03-30 17:35:50 -07001364 if ctx.Host() && !linker.static() {
1365 rpath_prefix := `\$$ORIGIN/`
1366 if ctx.Darwin() {
1367 rpath_prefix = "@loader_path/"
1368 }
1369
Colin Crossc99deeb2016-04-11 15:06:20 -07001370 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001371 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1372 }
1373 }
1374
Dan Willemsene7174922016-03-30 17:33:52 -07001375 if flags.Clang {
1376 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1377 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001378 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1379 }
1380
1381 return flags
1382}
1383
1384func (linker *baseLinker) static() bool {
1385 return linker.dynamicProperties.VariantIsStatic
1386}
1387
1388func (linker *baseLinker) staticBinary() bool {
1389 return linker.dynamicProperties.VariantIsStaticBinary
1390}
1391
1392func (linker *baseLinker) setStatic(static bool) {
1393 linker.dynamicProperties.VariantIsStatic = static
1394}
1395
Colin Cross16b23492016-01-06 14:41:07 -08001396func (linker *baseLinker) isDependencyRoot() bool {
1397 return false
1398}
1399
Colin Crossca860ac2016-01-04 14:34:37 -08001400type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001401 // Returns true if the build options for the module have selected a static or shared build
1402 buildStatic() bool
1403 buildShared() bool
1404
1405 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001406 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001407
Colin Cross18b6dc52015-04-28 13:20:37 -07001408 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001409 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001410
1411 // Returns whether a module is a static binary
1412 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001413
1414 // Returns true for dependency roots (binaries)
1415 // TODO(ccross): also handle dlopenable libraries
1416 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001417}
1418
Colin Crossca860ac2016-01-04 14:34:37 -08001419type baseInstaller struct {
1420 Properties InstallerProperties
1421
1422 dir string
1423 dir64 string
1424 data bool
1425
Colin Cross635c3b02016-05-18 15:37:25 -07001426 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001427}
1428
1429var _ installer = (*baseInstaller)(nil)
1430
1431func (installer *baseInstaller) props() []interface{} {
1432 return []interface{}{&installer.Properties}
1433}
1434
Colin Cross635c3b02016-05-18 15:37:25 -07001435func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001436 subDir := installer.dir
1437 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1438 subDir = installer.dir64
1439 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001440 if !ctx.Host() && !ctx.Arch().Native {
1441 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1442 }
Colin Cross635c3b02016-05-18 15:37:25 -07001443 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001444 installer.path = ctx.InstallFile(dir, file)
1445}
1446
1447func (installer *baseInstaller) inData() bool {
1448 return installer.data
1449}
1450
Colin Cross3f40fa42015-01-30 17:27:36 -08001451//
1452// Combined static+shared libraries
1453//
1454
Colin Cross919281a2016-04-05 16:42:05 -07001455type flagExporter struct {
1456 Properties FlagExporterProperties
1457
1458 flags []string
1459}
1460
1461func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001462 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1463 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001464}
1465
1466func (f *flagExporter) reexportFlags(flags []string) {
1467 f.flags = append(f.flags, flags...)
1468}
1469
1470func (f *flagExporter) exportedFlags() []string {
1471 return f.flags
1472}
1473
1474type exportedFlagsProducer interface {
1475 exportedFlags() []string
1476}
1477
1478var _ exportedFlagsProducer = (*flagExporter)(nil)
1479
Colin Crossca860ac2016-01-04 14:34:37 -08001480type libraryCompiler struct {
1481 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001482
Colin Crossca860ac2016-01-04 14:34:37 -08001483 linker *libraryLinker
1484 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001485
Colin Crossca860ac2016-01-04 14:34:37 -08001486 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001487 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001488}
1489
Colin Crossca860ac2016-01-04 14:34:37 -08001490var _ compiler = (*libraryCompiler)(nil)
1491
1492func (library *libraryCompiler) props() []interface{} {
1493 props := library.baseCompiler.props()
1494 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001495}
1496
Colin Crossca860ac2016-01-04 14:34:37 -08001497func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1498 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001499
Dan Willemsen490fd492015-11-24 17:53:15 -08001500 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1501 // all code is position independent, and then those warnings get promoted to
1502 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001503 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001504 flags.CFlags = append(flags.CFlags, "-fPIC")
1505 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001506
Colin Crossca860ac2016-01-04 14:34:37 -08001507 if library.linker.static() {
1508 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001509 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001510 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001511 }
1512
Colin Crossca860ac2016-01-04 14:34:37 -08001513 return flags
1514}
1515
Colin Cross635c3b02016-05-18 15:37:25 -07001516func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1517 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001518
Dan Willemsenb40aab62016-04-20 14:21:14 -07001519 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001520 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001521
1522 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001523 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001524 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1525 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001526 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001527 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001528 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1529 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001530 }
1531
1532 return objFiles
1533}
1534
1535type libraryLinker struct {
1536 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001537 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001538 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001539
1540 Properties LibraryLinkerProperties
1541
1542 dynamicProperties struct {
1543 BuildStatic bool `blueprint:"mutated"`
1544 BuildShared bool `blueprint:"mutated"`
1545 }
1546
Colin Crossca860ac2016-01-04 14:34:37 -08001547 // If we're used as a whole_static_lib, our missing dependencies need
1548 // to be given
1549 wholeStaticMissingDeps []string
1550
1551 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001552 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001553}
1554
1555var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001556
Dan Willemsenfed4d192016-07-06 21:48:39 -07001557func (library *libraryLinker) begin(ctx BaseModuleContext) {
1558 library.baseLinker.begin(ctx)
1559 if library.static() {
1560 if library.Properties.Static.Enabled != nil &&
1561 !*library.Properties.Static.Enabled {
1562 ctx.module().Disable()
1563 }
1564 } else {
1565 if library.Properties.Shared.Enabled != nil &&
1566 !*library.Properties.Shared.Enabled {
1567 ctx.module().Disable()
1568 }
1569 }
1570}
1571
Colin Crossca860ac2016-01-04 14:34:37 -08001572func (library *libraryLinker) props() []interface{} {
1573 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001574 return append(props,
1575 &library.Properties,
1576 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001577 &library.flagExporter.Properties,
1578 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001579}
1580
1581func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1582 flags = library.baseLinker.flags(ctx, flags)
1583
1584 flags.Nocrt = Bool(library.Properties.Nocrt)
1585
1586 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001587 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001588 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1589 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001590 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001591 sharedFlag = "-shared"
1592 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001593 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001594 flags.LdFlags = append(flags.LdFlags,
1595 "-nostdlib",
1596 "-Wl,--gc-sections",
1597 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001598 }
Colin Cross97ba0732015-03-23 17:50:24 -07001599
Colin Cross0af4b842015-04-30 16:36:18 -07001600 if ctx.Darwin() {
1601 flags.LdFlags = append(flags.LdFlags,
1602 "-dynamiclib",
1603 "-single_module",
1604 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001605 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001606 )
1607 } else {
1608 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001609 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001610 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001611 )
1612 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001613 }
Colin Cross97ba0732015-03-23 17:50:24 -07001614
1615 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001616}
1617
Colin Crossca860ac2016-01-04 14:34:37 -08001618func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1619 deps = library.baseLinker.deps(ctx, deps)
1620 if library.static() {
1621 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1622 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1623 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1624 } else {
1625 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1626 if !ctx.sdk() {
1627 deps.CrtBegin = "crtbegin_so"
1628 deps.CrtEnd = "crtend_so"
1629 } else {
1630 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1631 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1632 }
1633 }
1634 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1635 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1636 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1637 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001638
Colin Crossca860ac2016-01-04 14:34:37 -08001639 return deps
1640}
Colin Cross3f40fa42015-01-30 17:27:36 -08001641
Colin Crossca860ac2016-01-04 14:34:37 -08001642func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001643 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001644
Colin Cross635c3b02016-05-18 15:37:25 -07001645 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001646 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001647
Colin Cross635c3b02016-05-18 15:37:25 -07001648 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001649 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001650
Colin Cross0af4b842015-04-30 16:36:18 -07001651 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001652 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001653 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001654 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001655 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001656
Colin Crossca860ac2016-01-04 14:34:37 -08001657 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001658
1659 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001660
1661 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001662}
1663
Colin Crossca860ac2016-01-04 14:34:37 -08001664func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001665 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001666
Colin Cross635c3b02016-05-18 15:37:25 -07001667 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001668
Colin Cross635c3b02016-05-18 15:37:25 -07001669 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1670 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1671 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1672 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001673 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001674 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001675 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001676 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001677 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001678 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001679 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1680 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001681 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001682 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1683 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001684 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001685 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1686 }
1687 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001688 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001689 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1690 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001691 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001692 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001693 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001694 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001696 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001697 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001698 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001699 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001700 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001701 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001702 }
Colin Crossaee540a2015-07-06 17:48:31 -07001703 }
1704
Colin Cross665dce92016-04-28 14:50:03 -07001705 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001706 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001707 ret := outputFile
1708
1709 builderFlags := flagsToBuilderFlags(flags)
1710
1711 if library.stripper.needsStrip(ctx) {
1712 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001713 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001714 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1715 }
1716
Colin Crossca860ac2016-01-04 14:34:37 -08001717 sharedLibs := deps.SharedLibs
1718 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001719
Colin Crossca860ac2016-01-04 14:34:37 -08001720 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1721 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001722 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001723
Colin Cross665dce92016-04-28 14:50:03 -07001724 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001725}
1726
Colin Crossca860ac2016-01-04 14:34:37 -08001727func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001728 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001729
Colin Crossc99deeb2016-04-11 15:06:20 -07001730 objFiles = append(objFiles, deps.ObjFiles...)
1731
Colin Cross635c3b02016-05-18 15:37:25 -07001732 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001733 if library.static() {
1734 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001735 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001736 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001737 }
1738
Colin Cross919281a2016-04-05 16:42:05 -07001739 library.exportIncludes(ctx, "-I")
1740 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001741
1742 return out
1743}
1744
1745func (library *libraryLinker) buildStatic() bool {
1746 return library.dynamicProperties.BuildStatic
1747}
1748
1749func (library *libraryLinker) buildShared() bool {
1750 return library.dynamicProperties.BuildShared
1751}
1752
1753func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1754 return library.wholeStaticMissingDeps
1755}
1756
Colin Crossc99deeb2016-04-11 15:06:20 -07001757func (library *libraryLinker) installable() bool {
1758 return !library.static()
1759}
1760
Colin Crossca860ac2016-01-04 14:34:37 -08001761type libraryInstaller struct {
1762 baseInstaller
1763
Colin Cross30d5f512016-05-03 18:02:42 -07001764 linker *libraryLinker
1765 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001766}
1767
Colin Cross635c3b02016-05-18 15:37:25 -07001768func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001769 if !library.linker.static() {
1770 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001771 }
1772}
1773
Colin Cross30d5f512016-05-03 18:02:42 -07001774func (library *libraryInstaller) inData() bool {
1775 return library.baseInstaller.inData() || library.sanitize.inData()
1776}
1777
Colin Cross635c3b02016-05-18 15:37:25 -07001778func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1779 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001780
Colin Crossca860ac2016-01-04 14:34:37 -08001781 linker := &libraryLinker{}
1782 linker.dynamicProperties.BuildShared = shared
1783 linker.dynamicProperties.BuildStatic = static
1784 module.linker = linker
1785
1786 module.compiler = &libraryCompiler{
1787 linker: linker,
1788 }
1789 module.installer = &libraryInstaller{
1790 baseInstaller: baseInstaller{
1791 dir: "lib",
1792 dir64: "lib64",
1793 },
Colin Cross30d5f512016-05-03 18:02:42 -07001794 linker: linker,
1795 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001796 }
1797
Colin Crossca860ac2016-01-04 14:34:37 -08001798 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001799}
1800
Colin Crossca860ac2016-01-04 14:34:37 -08001801func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001802 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001803 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001804}
1805
Colin Cross3f40fa42015-01-30 17:27:36 -08001806//
1807// Objects (for crt*.o)
1808//
1809
Colin Crossca860ac2016-01-04 14:34:37 -08001810type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001811 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001812}
1813
Colin Crossca860ac2016-01-04 14:34:37 -08001814func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001815 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001816 module.compiler = &baseCompiler{}
1817 module.linker = &objectLinker{}
1818 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001819}
1820
Colin Cross81413472016-04-11 14:37:39 -07001821func (object *objectLinker) props() []interface{} {
1822 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001823}
1824
Colin Crossca860ac2016-01-04 14:34:37 -08001825func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001826
Colin Cross81413472016-04-11 14:37:39 -07001827func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1828 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001829 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001830}
1831
Colin Crossca860ac2016-01-04 14:34:37 -08001832func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001833 if flags.Clang {
1834 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1835 } else {
1836 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1837 }
1838
Colin Crossca860ac2016-01-04 14:34:37 -08001839 return flags
1840}
1841
1842func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001843 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001844
Colin Cross97ba0732015-03-23 17:50:24 -07001845 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001846
Colin Cross635c3b02016-05-18 15:37:25 -07001847 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001848 if len(objFiles) == 1 {
1849 outputFile = objFiles[0]
1850 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001851 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001852 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001853 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001854 }
1855
Colin Cross3f40fa42015-01-30 17:27:36 -08001856 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001857 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001858}
1859
Colin Crossc99deeb2016-04-11 15:06:20 -07001860func (*objectLinker) installable() bool {
1861 return false
1862}
1863
Colin Cross3f40fa42015-01-30 17:27:36 -08001864//
1865// Executables
1866//
1867
Colin Crossca860ac2016-01-04 14:34:37 -08001868type binaryLinker struct {
1869 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001870 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001871
Colin Crossca860ac2016-01-04 14:34:37 -08001872 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001873
Colin Cross635c3b02016-05-18 15:37:25 -07001874 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001875}
1876
Colin Crossca860ac2016-01-04 14:34:37 -08001877var _ linker = (*binaryLinker)(nil)
1878
1879func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001880 return append(binary.baseLinker.props(),
1881 &binary.Properties,
1882 &binary.stripper.StripProperties)
1883
Colin Cross3f40fa42015-01-30 17:27:36 -08001884}
1885
Colin Crossca860ac2016-01-04 14:34:37 -08001886func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001887 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001888}
1889
Colin Crossca860ac2016-01-04 14:34:37 -08001890func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001891 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001892}
1893
Colin Crossca860ac2016-01-04 14:34:37 -08001894func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001895 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001896 if binary.Properties.Stem != "" {
1897 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001898 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001899
Colin Crossca860ac2016-01-04 14:34:37 -08001900 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001901}
1902
Colin Crossca860ac2016-01-04 14:34:37 -08001903func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1904 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001905 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001906 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001907 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001908 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001909 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001910 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001911 }
Colin Crossca860ac2016-01-04 14:34:37 -08001912 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001913 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001914 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001915 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001916 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001917 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001918 }
Colin Crossca860ac2016-01-04 14:34:37 -08001919 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001920 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001921
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001922 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001923 if inList("libc++_static", deps.StaticLibs) {
1924 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001925 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001926 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1927 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1928 // move them to the beginning of deps.LateStaticLibs
1929 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001930 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001931 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001932 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001933 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001934 }
Colin Crossca860ac2016-01-04 14:34:37 -08001935
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001936 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001937 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1938 "from static libs or set static_executable: true")
1939 }
1940 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001941}
1942
Colin Crossc99deeb2016-04-11 15:06:20 -07001943func (*binaryLinker) installable() bool {
1944 return true
1945}
1946
Colin Cross16b23492016-01-06 14:41:07 -08001947func (binary *binaryLinker) isDependencyRoot() bool {
1948 return true
1949}
1950
Colin Cross635c3b02016-05-18 15:37:25 -07001951func NewBinary(hod android.HostOrDeviceSupported) *Module {
1952 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001953 module.compiler = &baseCompiler{}
1954 module.linker = &binaryLinker{}
1955 module.installer = &baseInstaller{
1956 dir: "bin",
1957 }
1958 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001959}
1960
Colin Crossca860ac2016-01-04 14:34:37 -08001961func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001962 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001963 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001964}
1965
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001966func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1967 binary.baseLinker.begin(ctx)
1968
1969 static := Bool(binary.Properties.Static_executable)
1970 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001971 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001972 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1973 static = true
1974 }
1975 } else {
1976 // Static executables are not supported on Darwin or Windows
1977 static = false
1978 }
Colin Cross0af4b842015-04-30 16:36:18 -07001979 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001980 if static {
1981 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001982 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001983 }
1984}
1985
Colin Crossca860ac2016-01-04 14:34:37 -08001986func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1987 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001988
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001989 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001990 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07001991 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001992 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1993 }
1994 }
1995
1996 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1997 // all code is position independent, and then those warnings get promoted to
1998 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001999 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002000 flags.CFlags = append(flags.CFlags, "-fpie")
2001 }
Colin Cross97ba0732015-03-23 17:50:24 -07002002
Colin Crossf6566ed2015-03-24 11:13:38 -07002003 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002004 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002005 // Clang driver needs -static to create static executable.
2006 // However, bionic/linker uses -shared to overwrite.
2007 // Linker for x86 targets does not allow coexistance of -static and -shared,
2008 // so we add -static only if -shared is not used.
2009 if !inList("-shared", flags.LdFlags) {
2010 flags.LdFlags = append(flags.LdFlags, "-static")
2011 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002012
Colin Crossed4cf0b2015-03-26 14:43:45 -07002013 flags.LdFlags = append(flags.LdFlags,
2014 "-nostdlib",
2015 "-Bstatic",
2016 "-Wl,--gc-sections",
2017 )
2018
2019 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002020 if flags.DynamicLinker == "" {
2021 flags.DynamicLinker = "/system/bin/linker"
2022 if flags.Toolchain.Is64Bit() {
2023 flags.DynamicLinker += "64"
2024 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002025 }
2026
2027 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002028 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002029 "-nostdlib",
2030 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002031 "-Wl,--gc-sections",
2032 "-Wl,-z,nocopyreloc",
2033 )
2034 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002035 } else {
2036 if binary.staticBinary() {
2037 flags.LdFlags = append(flags.LdFlags, "-static")
2038 }
2039 if ctx.Darwin() {
2040 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2041 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002042 }
2043
Colin Cross97ba0732015-03-23 17:50:24 -07002044 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002045}
2046
Colin Crossca860ac2016-01-04 14:34:37 -08002047func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002048 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002049
Colin Cross665dce92016-04-28 14:50:03 -07002050 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002051 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002052 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002053 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002054 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002055 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002056
Colin Cross635c3b02016-05-18 15:37:25 -07002057 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002058
Colin Crossca860ac2016-01-04 14:34:37 -08002059 sharedLibs := deps.SharedLibs
2060 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2061
Colin Cross16b23492016-01-06 14:41:07 -08002062 if flags.DynamicLinker != "" {
2063 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2064 }
2065
Colin Cross665dce92016-04-28 14:50:03 -07002066 builderFlags := flagsToBuilderFlags(flags)
2067
2068 if binary.stripper.needsStrip(ctx) {
2069 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002070 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002071 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2072 }
2073
2074 if binary.Properties.Prefix_symbols != "" {
2075 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002076 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002077 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2078 flagsToBuilderFlags(flags), afterPrefixSymbols)
2079 }
2080
Colin Crossca860ac2016-01-04 14:34:37 -08002081 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002082 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002083 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002084
2085 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002086}
Colin Cross3f40fa42015-01-30 17:27:36 -08002087
Colin Cross635c3b02016-05-18 15:37:25 -07002088func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002089 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002090}
2091
Colin Cross665dce92016-04-28 14:50:03 -07002092type stripper struct {
2093 StripProperties StripProperties
2094}
2095
2096func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2097 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2098}
2099
Colin Cross635c3b02016-05-18 15:37:25 -07002100func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002101 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002102 if ctx.Darwin() {
2103 TransformDarwinStrip(ctx, in, out)
2104 } else {
2105 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2106 // TODO(ccross): don't add gnu debuglink for user builds
2107 flags.stripAddGnuDebuglink = true
2108 TransformStrip(ctx, in, out, flags)
2109 }
Colin Cross665dce92016-04-28 14:50:03 -07002110}
2111
Colin Cross635c3b02016-05-18 15:37:25 -07002112func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002113 if m, ok := mctx.Module().(*Module); ok {
2114 if test, ok := m.linker.(*testLinker); ok {
2115 if Bool(test.Properties.Test_per_src) {
2116 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2117 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2118 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2119 }
2120 tests := mctx.CreateLocalVariations(testNames...)
2121 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2122 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2123 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2124 }
Colin Cross6002e052015-09-16 16:00:08 -07002125 }
2126 }
2127 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002128}
2129
Colin Crossca860ac2016-01-04 14:34:37 -08002130type testLinker struct {
2131 binaryLinker
2132 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002133}
2134
Dan Willemsend30e6102016-03-30 17:35:50 -07002135func (test *testLinker) begin(ctx BaseModuleContext) {
2136 test.binaryLinker.begin(ctx)
2137
2138 runpath := "../../lib"
2139 if ctx.toolchain().Is64Bit() {
2140 runpath += "64"
2141 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002142 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002143}
2144
Colin Crossca860ac2016-01-04 14:34:37 -08002145func (test *testLinker) props() []interface{} {
2146 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002147}
2148
Colin Crossca860ac2016-01-04 14:34:37 -08002149func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2150 flags = test.binaryLinker.flags(ctx, flags)
2151
2152 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002153 return flags
2154 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002155
Colin Cross97ba0732015-03-23 17:50:24 -07002156 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002157 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002158 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002159
Colin Crossa1ad8d12016-06-01 17:09:44 -07002160 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002161 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002162 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002163 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002164 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2165 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002166 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002167 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2168 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002169 }
2170 } else {
2171 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002172 }
2173
Colin Cross21b9a242015-03-24 14:15:58 -07002174 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002175}
2176
Colin Crossca860ac2016-01-04 14:34:37 -08002177func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2178 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002179 if ctx.sdk() && ctx.Device() {
2180 switch ctx.selectedStl() {
2181 case "ndk_libc++_shared", "ndk_libc++_static":
2182 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2183 case "ndk_libgnustl_static":
2184 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2185 default:
2186 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2187 }
2188 } else {
2189 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2190 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002191 }
Colin Crossca860ac2016-01-04 14:34:37 -08002192 deps = test.binaryLinker.deps(ctx, deps)
2193 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002194}
2195
Colin Crossca860ac2016-01-04 14:34:37 -08002196type testInstaller struct {
2197 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002198}
2199
Colin Cross635c3b02016-05-18 15:37:25 -07002200func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002201 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2202 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2203 installer.baseInstaller.install(ctx, file)
2204}
2205
Colin Cross635c3b02016-05-18 15:37:25 -07002206func NewTest(hod android.HostOrDeviceSupported) *Module {
2207 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002208 module.compiler = &baseCompiler{}
2209 linker := &testLinker{}
2210 linker.Properties.Gtest = true
2211 module.linker = linker
2212 module.installer = &testInstaller{
2213 baseInstaller: baseInstaller{
2214 dir: "nativetest",
2215 dir64: "nativetest64",
2216 data: true,
2217 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002218 }
Colin Crossca860ac2016-01-04 14:34:37 -08002219 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002220}
2221
Colin Crossca860ac2016-01-04 14:34:37 -08002222func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002223 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002224 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002225}
2226
Colin Crossca860ac2016-01-04 14:34:37 -08002227type benchmarkLinker struct {
2228 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002229}
2230
Colin Crossca860ac2016-01-04 14:34:37 -08002231func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2232 deps = benchmark.binaryLinker.deps(ctx, deps)
2233 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2234 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002235}
2236
Colin Cross635c3b02016-05-18 15:37:25 -07002237func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2238 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002239 module.compiler = &baseCompiler{}
2240 module.linker = &benchmarkLinker{}
2241 module.installer = &baseInstaller{
2242 dir: "nativetest",
2243 dir64: "nativetest64",
2244 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002245 }
Colin Crossca860ac2016-01-04 14:34:37 -08002246 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002247}
2248
Colin Crossca860ac2016-01-04 14:34:37 -08002249func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002250 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002251 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002252}
2253
Colin Cross3f40fa42015-01-30 17:27:36 -08002254//
2255// Static library
2256//
2257
Colin Crossca860ac2016-01-04 14:34:37 -08002258func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002259 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002260 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002261}
2262
2263//
2264// Shared libraries
2265//
2266
Colin Crossca860ac2016-01-04 14:34:37 -08002267func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002268 module := NewLibrary(android.HostAndDeviceSupported, 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 static library
2274//
2275
Colin Crossca860ac2016-01-04 14:34:37 -08002276func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002277 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002278 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002279}
2280
2281//
2282// Host Shared libraries
2283//
2284
Colin Crossca860ac2016-01-04 14:34:37 -08002285func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002286 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002287 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002288}
2289
2290//
2291// Host Binaries
2292//
2293
Colin Crossca860ac2016-01-04 14:34:37 -08002294func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002295 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002296 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002297}
2298
2299//
Colin Cross1f8f2342015-03-26 16:09:47 -07002300// Host Tests
2301//
2302
Colin Crossca860ac2016-01-04 14:34:37 -08002303func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002304 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002305 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002306}
2307
2308//
Colin Cross2ba19d92015-05-07 15:44:20 -07002309// Host Benchmarks
2310//
2311
Colin Crossca860ac2016-01-04 14:34:37 -08002312func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002313 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002314 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002315}
2316
2317//
Colin Crosscfad1192015-11-02 16:43:11 -08002318// Defaults
2319//
Colin Crossca860ac2016-01-04 14:34:37 -08002320type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002321 android.ModuleBase
2322 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002323}
2324
Colin Cross635c3b02016-05-18 15:37:25 -07002325func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002326}
2327
Colin Crossca860ac2016-01-04 14:34:37 -08002328func defaultsFactory() (blueprint.Module, []interface{}) {
2329 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002330
2331 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002332 &BaseProperties{},
2333 &BaseCompilerProperties{},
2334 &BaseLinkerProperties{},
2335 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002336 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002337 &LibraryLinkerProperties{},
2338 &BinaryLinkerProperties{},
2339 &TestLinkerProperties{},
2340 &UnusedProperties{},
2341 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002342 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002343 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002344 }
2345
Colin Cross635c3b02016-05-18 15:37:25 -07002346 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2347 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002348
Colin Cross635c3b02016-05-18 15:37:25 -07002349 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002350}
2351
2352//
Colin Cross3f40fa42015-01-30 17:27:36 -08002353// Device libraries shipped with gcc
2354//
2355
Colin Crossca860ac2016-01-04 14:34:37 -08002356type toolchainLibraryLinker struct {
2357 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002358}
2359
Colin Crossca860ac2016-01-04 14:34:37 -08002360var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2361
2362func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002363 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002364 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002365}
2366
Colin Crossca860ac2016-01-04 14:34:37 -08002367func (*toolchainLibraryLinker) buildStatic() bool {
2368 return true
2369}
Colin Cross3f40fa42015-01-30 17:27:36 -08002370
Colin Crossca860ac2016-01-04 14:34:37 -08002371func (*toolchainLibraryLinker) buildShared() bool {
2372 return false
2373}
2374
2375func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002376 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002377 module.compiler = &baseCompiler{}
2378 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002379 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002380 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002381}
2382
Colin Crossca860ac2016-01-04 14:34:37 -08002383func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002384 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002385
2386 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002387 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002388
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002389 if flags.Clang {
2390 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2391 }
2392
Colin Crossca860ac2016-01-04 14:34:37 -08002393 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002394
2395 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002396
Colin Crossca860ac2016-01-04 14:34:37 -08002397 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002398}
2399
Colin Crossc99deeb2016-04-11 15:06:20 -07002400func (*toolchainLibraryLinker) installable() bool {
2401 return false
2402}
2403
Dan Albertbe961682015-03-18 23:38:50 -07002404// NDK prebuilt libraries.
2405//
2406// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2407// either (with the exception of the shared STLs, which are installed to the app's directory rather
2408// than to the system image).
2409
Colin Cross635c3b02016-05-18 15:37:25 -07002410func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002411 suffix := ""
2412 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2413 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002414 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002415 suffix = "64"
2416 }
Colin Cross635c3b02016-05-18 15:37:25 -07002417 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002418 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002419}
2420
Colin Cross635c3b02016-05-18 15:37:25 -07002421func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2422 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002423
2424 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2425 // We want to translate to just NAME.EXT
2426 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2427 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002428 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002429}
2430
Colin Crossca860ac2016-01-04 14:34:37 -08002431type ndkPrebuiltObjectLinker struct {
2432 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002433}
2434
Colin Crossca860ac2016-01-04 14:34:37 -08002435func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002436 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002437 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002438}
2439
Colin Crossca860ac2016-01-04 14:34:37 -08002440func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002441 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002442 module.linker = &ndkPrebuiltObjectLinker{}
2443 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002444}
2445
Colin Crossca860ac2016-01-04 14:34:37 -08002446func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002447 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002448 // A null build step, but it sets up the output path.
2449 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2450 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2451 }
2452
Colin Crossca860ac2016-01-04 14:34:37 -08002453 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002454}
2455
Colin Crossca860ac2016-01-04 14:34:37 -08002456type ndkPrebuiltLibraryLinker struct {
2457 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002458}
2459
Colin Crossca860ac2016-01-04 14:34:37 -08002460var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2461var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002462
Colin Crossca860ac2016-01-04 14:34:37 -08002463func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002464 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002465}
2466
Colin Crossca860ac2016-01-04 14:34:37 -08002467func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002468 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002469 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002470}
2471
Colin Crossca860ac2016-01-04 14:34:37 -08002472func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002473 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002474 linker := &ndkPrebuiltLibraryLinker{}
2475 linker.dynamicProperties.BuildShared = true
2476 module.linker = linker
2477 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002478}
2479
Colin Crossca860ac2016-01-04 14:34:37 -08002480func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002481 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002482 // A null build step, but it sets up the output path.
2483 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2484 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2485 }
2486
Colin Cross919281a2016-04-05 16:42:05 -07002487 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002488
Colin Crossca860ac2016-01-04 14:34:37 -08002489 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2490 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002491}
2492
2493// The NDK STLs are slightly different from the prebuilt system libraries:
2494// * Are not specific to each platform version.
2495// * The libraries are not in a predictable location for each STL.
2496
Colin Crossca860ac2016-01-04 14:34:37 -08002497type ndkPrebuiltStlLinker struct {
2498 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002499}
2500
Colin Crossca860ac2016-01-04 14:34:37 -08002501func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002502 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002503 linker := &ndkPrebuiltStlLinker{}
2504 linker.dynamicProperties.BuildShared = true
2505 module.linker = linker
2506 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002507}
2508
Colin Crossca860ac2016-01-04 14:34:37 -08002509func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002510 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002511 linker := &ndkPrebuiltStlLinker{}
2512 linker.dynamicProperties.BuildStatic = true
2513 module.linker = linker
2514 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002515}
2516
Colin Cross635c3b02016-05-18 15:37:25 -07002517func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002518 gccVersion := toolchain.GccVersion()
2519 var libDir string
2520 switch stl {
2521 case "libstlport":
2522 libDir = "cxx-stl/stlport/libs"
2523 case "libc++":
2524 libDir = "cxx-stl/llvm-libc++/libs"
2525 case "libgnustl":
2526 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2527 }
2528
2529 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002530 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002531 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002532 }
2533
2534 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002535 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002536}
2537
Colin Crossca860ac2016-01-04 14:34:37 -08002538func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002539 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002540 // A null build step, but it sets up the output path.
2541 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2542 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2543 }
2544
Colin Cross919281a2016-04-05 16:42:05 -07002545 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002546
2547 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002548 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002549 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002550 libExt = staticLibraryExtension
2551 }
2552
2553 stlName := strings.TrimSuffix(libName, "_shared")
2554 stlName = strings.TrimSuffix(stlName, "_static")
2555 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002556 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002557}
2558
Colin Cross635c3b02016-05-18 15:37:25 -07002559func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002560 if m, ok := mctx.Module().(*Module); ok {
2561 if m.linker != nil {
2562 if linker, ok := m.linker.(baseLinkerInterface); ok {
2563 var modules []blueprint.Module
2564 if linker.buildStatic() && linker.buildShared() {
2565 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002566 static := modules[0].(*Module)
2567 shared := modules[1].(*Module)
2568
2569 static.linker.(baseLinkerInterface).setStatic(true)
2570 shared.linker.(baseLinkerInterface).setStatic(false)
2571
2572 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2573 sharedCompiler := shared.compiler.(*libraryCompiler)
2574 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2575 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2576 // Optimize out compiling common .o files twice for static+shared libraries
2577 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2578 sharedCompiler.baseCompiler.Properties.Srcs = nil
2579 }
2580 }
Colin Crossca860ac2016-01-04 14:34:37 -08002581 } else if linker.buildStatic() {
2582 modules = mctx.CreateLocalVariations("static")
2583 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2584 } else if linker.buildShared() {
2585 modules = mctx.CreateLocalVariations("shared")
2586 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2587 } else {
2588 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2589 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002590 }
2591 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002592 }
2593}
Colin Cross74d1ec02015-04-28 13:30:13 -07002594
2595// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2596// modifies the slice contents in place, and returns a subslice of the original slice
2597func lastUniqueElements(list []string) []string {
2598 totalSkip := 0
2599 for i := len(list) - 1; i >= totalSkip; i-- {
2600 skip := 0
2601 for j := i - 1; j >= totalSkip; j-- {
2602 if list[i] == list[j] {
2603 skip++
2604 } else {
2605 list[j+skip] = list[j]
2606 }
2607 }
2608 totalSkip += skip
2609 }
2610 return list[totalSkip:]
2611}
Colin Cross06a931b2015-10-28 17:23:31 -07002612
2613var Bool = proptools.Bool