blob: 66b7b4f253c8869eda6cc3c6aeb8beb1ceab70c7 [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 }
931 return from.Properties.Sdk_version != ""
932 }
933
Colin Crossc99deeb2016-04-11 15:06:20 -0700934 ctx.VisitDirectDeps(func(m blueprint.Module) {
935 name := ctx.OtherModuleName(m)
936 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800937
Colin Cross635c3b02016-05-18 15:37:25 -0700938 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700939 if a == nil {
940 ctx.ModuleErrorf("module %q not an android module", name)
941 return
Colin Crossca860ac2016-01-04 14:34:37 -0800942 }
Colin Crossca860ac2016-01-04 14:34:37 -0800943
Dan Willemsena96ff642016-06-07 12:34:45 -0700944 cc, _ := m.(*Module)
945 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700946 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700947 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700948 case genSourceDepTag:
949 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
950 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
951 genRule.GeneratedSourceFiles()...)
952 } else {
953 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
954 }
955 case genHeaderDepTag:
956 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
957 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
958 genRule.GeneratedSourceFiles()...)
959 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700960 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700961 } else {
962 ctx.ModuleErrorf("module %q is not a genrule", name)
963 }
964 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700965 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800966 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 return
968 }
969
970 if !a.Enabled() {
971 ctx.ModuleErrorf("depends on disabled module %q", name)
972 return
973 }
974
Colin Crossa1ad8d12016-06-01 17:09:44 -0700975 if a.Target().Os != ctx.Os() {
976 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
977 return
978 }
979
980 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
981 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700982 return
983 }
984
Dan Willemsena96ff642016-06-07 12:34:45 -0700985 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700986 ctx.ModuleErrorf("module %q missing output file", name)
987 return
988 }
989
990 if tag == reuseObjTag {
991 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700992 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700993 return
994 }
995
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700996 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700997 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700998 cflags := i.exportedFlags()
Colin Crossc99deeb2016-04-11 15:06:20 -0700999 depPaths.Cflags = append(depPaths.Cflags, cflags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001000
1001 if t.reexportFlags {
1002 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
1003 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001004 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001005
1006 if !linkTypeOk(c, cc) {
1007 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1008 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001009 }
1010
Colin Cross635c3b02016-05-18 15:37:25 -07001011 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001012
1013 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001014 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001015 depPtr = &depPaths.SharedLibs
1016 case lateSharedDepTag:
1017 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001018 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001019 depPtr = &depPaths.StaticLibs
1020 case lateStaticDepTag:
1021 depPtr = &depPaths.LateStaticLibs
1022 case wholeStaticDepTag:
1023 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001024 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001025 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001026 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001027 return
1028 }
1029
1030 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1031 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1032 for i := range missingDeps {
1033 missingDeps[i] += postfix
1034 }
1035 ctx.AddMissingDependencies(missingDeps)
1036 }
1037 depPaths.WholeStaticLibObjFiles =
1038 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1039 case objDepTag:
1040 depPtr = &depPaths.ObjFiles
1041 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001042 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001043 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001044 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001045 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001046 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001047 }
1048
1049 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001050 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001051 }
1052 })
1053
1054 return depPaths
1055}
1056
1057func (c *Module) InstallInData() bool {
1058 if c.installer == nil {
1059 return false
1060 }
1061 return c.installer.inData()
1062}
1063
1064// Compiler
1065
1066type baseCompiler struct {
1067 Properties BaseCompilerProperties
1068}
1069
1070var _ compiler = (*baseCompiler)(nil)
1071
1072func (compiler *baseCompiler) props() []interface{} {
1073 return []interface{}{&compiler.Properties}
1074}
1075
Dan Willemsenb40aab62016-04-20 14:21:14 -07001076func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1077
1078func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1079 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1080 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1081
1082 return deps
1083}
Colin Crossca860ac2016-01-04 14:34:37 -08001084
1085// Create a Flags struct that collects the compile flags from global values,
1086// per-target values, module type values, and per-module Blueprints properties
1087func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1088 toolchain := ctx.toolchain()
1089
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001090 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1091 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1092 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1093 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1094
Colin Crossca860ac2016-01-04 14:34:37 -08001095 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1096 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1097 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1098 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1099 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1100
Colin Cross28344522015-04-22 13:07:53 -07001101 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001102 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1103 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001104 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001105 includeDirsToFlags(localIncludeDirs),
1106 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001107
Colin Crossca860ac2016-01-04 14:34:37 -08001108 if !ctx.noDefaultCompilerFlags() {
1109 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001110 flags.GlobalFlags = append(flags.GlobalFlags,
1111 "${commonGlobalIncludes}",
1112 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001113 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001114 }
1115
1116 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001117 "-I" + android.PathForModuleSrc(ctx).String(),
1118 "-I" + android.PathForModuleOut(ctx).String(),
1119 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001120 }...)
1121 }
1122
Colin Crossca860ac2016-01-04 14:34:37 -08001123 instructionSet := compiler.Properties.Instruction_set
1124 if flags.RequiredInstructionSet != "" {
1125 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001126 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001127 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1128 if flags.Clang {
1129 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1130 }
1131 if err != nil {
1132 ctx.ModuleErrorf("%s", err)
1133 }
1134
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001135 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1136
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001137 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001138 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001139
Colin Cross97ba0732015-03-23 17:50:24 -07001140 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001141 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1142 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1143
Colin Cross97ba0732015-03-23 17:50:24 -07001144 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001145 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1146 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001147 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1148 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1149 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001150
1151 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001152 var gccPrefix string
1153 if !ctx.Darwin() {
1154 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1155 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001156
Colin Cross97ba0732015-03-23 17:50:24 -07001157 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1158 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1159 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 }
1161
Colin Crossa1ad8d12016-06-01 17:09:44 -07001162 hod := "host"
1163 if ctx.Os().Class == android.Device {
1164 hod = "device"
1165 }
1166
Colin Crossca860ac2016-01-04 14:34:37 -08001167 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001168 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1169
Colin Cross97ba0732015-03-23 17:50:24 -07001170 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001171 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001172 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001173 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001174 toolchain.ClangCflags(),
1175 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001176 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001177
1178 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001179 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001180 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001181 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001182 toolchain.Cflags(),
1183 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001184 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001185 }
1186
Colin Cross7b66f152015-12-15 16:07:43 -08001187 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1188 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1189 }
1190
Colin Crossf6566ed2015-03-24 11:13:38 -07001191 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001192 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001193 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001194 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001195 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001196 }
1197 }
1198
Colin Cross97ba0732015-03-23 17:50:24 -07001199 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001200
Colin Cross97ba0732015-03-23 17:50:24 -07001201 if flags.Clang {
1202 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001203 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001204 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001205 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001206 }
1207
Colin Crossc4bde762015-11-23 16:11:30 -08001208 if flags.Clang {
1209 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1210 } else {
1211 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001212 }
1213
Colin Crossca860ac2016-01-04 14:34:37 -08001214 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001215 if ctx.Host() && !flags.Clang {
1216 // The host GCC doesn't support C++14 (and is deprecated, so likely
1217 // never will). Build these modules with C++11.
1218 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1219 } else {
1220 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1221 }
1222 }
1223
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001224 // We can enforce some rules more strictly in the code we own. strict
1225 // indicates if this is code that we can be stricter with. If we have
1226 // rules that we want to apply to *our* code (but maybe can't for
1227 // vendor/device specific things), we could extend this to be a ternary
1228 // value.
1229 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001230 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001231 strict = false
1232 }
1233
1234 // Can be used to make some annotations stricter for code we can fix
1235 // (such as when we mark functions as deprecated).
1236 if strict {
1237 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1238 }
1239
Colin Cross3f40fa42015-01-30 17:27:36 -08001240 return flags
1241}
1242
Colin Cross635c3b02016-05-18 15:37:25 -07001243func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001244 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001245 objFiles := compiler.compileObjs(ctx, flags, "",
1246 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1247 deps.GeneratedSources, deps.GeneratedHeaders)
1248
Colin Crossca860ac2016-01-04 14:34:37 -08001249 if ctx.Failed() {
1250 return nil
1251 }
1252
Colin Crossca860ac2016-01-04 14:34:37 -08001253 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001254}
1255
1256// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001257func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1258 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001259
Colin Crossca860ac2016-01-04 14:34:37 -08001260 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001261
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001262 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001263 inputFiles = append(inputFiles, extraSrcs...)
1264 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1265
1266 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001267 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001268
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001269 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001270}
1271
Colin Crossca860ac2016-01-04 14:34:37 -08001272// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1273type baseLinker struct {
1274 Properties BaseLinkerProperties
1275 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001276 VariantIsShared bool `blueprint:"mutated"`
1277 VariantIsStatic bool `blueprint:"mutated"`
1278 VariantIsStaticBinary bool `blueprint:"mutated"`
1279 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001280 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001281}
1282
Dan Willemsend30e6102016-03-30 17:35:50 -07001283func (linker *baseLinker) begin(ctx BaseModuleContext) {
1284 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001285 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001286 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001287 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001288 }
1289}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001290
Colin Crossca860ac2016-01-04 14:34:37 -08001291func (linker *baseLinker) props() []interface{} {
1292 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001293}
1294
Colin Crossca860ac2016-01-04 14:34:37 -08001295func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1296 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1297 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1298 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001299
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001300 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1301 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1302
Dan Willemsena96ff642016-06-07 12:34:45 -07001303 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001304 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001305 }
1306
Colin Crossf6566ed2015-03-24 11:13:38 -07001307 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001308 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001309 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1310 if !Bool(linker.Properties.No_libgcc) {
1311 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001312 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001313
Colin Crossca860ac2016-01-04 14:34:37 -08001314 if !linker.static() {
1315 if linker.Properties.System_shared_libs != nil {
1316 deps.LateSharedLibs = append(deps.LateSharedLibs,
1317 linker.Properties.System_shared_libs...)
1318 } else if !ctx.sdk() {
1319 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1320 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001321 }
Colin Cross577f6e42015-03-27 18:23:34 -07001322
Colin Crossca860ac2016-01-04 14:34:37 -08001323 if ctx.sdk() {
1324 version := ctx.sdkVersion()
1325 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001326 "ndk_libc."+version,
1327 "ndk_libm."+version,
1328 )
1329 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001330 }
1331
Colin Crossca860ac2016-01-04 14:34:37 -08001332 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001333}
1334
Colin Crossca860ac2016-01-04 14:34:37 -08001335func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1336 toolchain := ctx.toolchain()
1337
Colin Crossca860ac2016-01-04 14:34:37 -08001338 if !ctx.noDefaultCompilerFlags() {
1339 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1340 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1341 }
1342
1343 if flags.Clang {
1344 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1345 } else {
1346 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1347 }
1348
1349 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001350 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1351
Colin Crossca860ac2016-01-04 14:34:37 -08001352 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1353 }
1354 }
1355
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001356 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1357
Dan Willemsen00ced762016-05-10 17:31:21 -07001358 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1359
Dan Willemsend30e6102016-03-30 17:35:50 -07001360 if ctx.Host() && !linker.static() {
1361 rpath_prefix := `\$$ORIGIN/`
1362 if ctx.Darwin() {
1363 rpath_prefix = "@loader_path/"
1364 }
1365
Colin Crossc99deeb2016-04-11 15:06:20 -07001366 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001367 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1368 }
1369 }
1370
Dan Willemsene7174922016-03-30 17:33:52 -07001371 if flags.Clang {
1372 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1373 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001374 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1375 }
1376
1377 return flags
1378}
1379
1380func (linker *baseLinker) static() bool {
1381 return linker.dynamicProperties.VariantIsStatic
1382}
1383
1384func (linker *baseLinker) staticBinary() bool {
1385 return linker.dynamicProperties.VariantIsStaticBinary
1386}
1387
1388func (linker *baseLinker) setStatic(static bool) {
1389 linker.dynamicProperties.VariantIsStatic = static
1390}
1391
Colin Cross16b23492016-01-06 14:41:07 -08001392func (linker *baseLinker) isDependencyRoot() bool {
1393 return false
1394}
1395
Colin Crossca860ac2016-01-04 14:34:37 -08001396type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001397 // Returns true if the build options for the module have selected a static or shared build
1398 buildStatic() bool
1399 buildShared() bool
1400
1401 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001402 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001403
Colin Cross18b6dc52015-04-28 13:20:37 -07001404 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001405 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001406
1407 // Returns whether a module is a static binary
1408 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001409
1410 // Returns true for dependency roots (binaries)
1411 // TODO(ccross): also handle dlopenable libraries
1412 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001413}
1414
Colin Crossca860ac2016-01-04 14:34:37 -08001415type baseInstaller struct {
1416 Properties InstallerProperties
1417
1418 dir string
1419 dir64 string
1420 data bool
1421
Colin Cross635c3b02016-05-18 15:37:25 -07001422 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001423}
1424
1425var _ installer = (*baseInstaller)(nil)
1426
1427func (installer *baseInstaller) props() []interface{} {
1428 return []interface{}{&installer.Properties}
1429}
1430
Colin Cross635c3b02016-05-18 15:37:25 -07001431func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001432 subDir := installer.dir
1433 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1434 subDir = installer.dir64
1435 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001436 if !ctx.Host() && !ctx.Arch().Native {
1437 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1438 }
Colin Cross635c3b02016-05-18 15:37:25 -07001439 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001440 installer.path = ctx.InstallFile(dir, file)
1441}
1442
1443func (installer *baseInstaller) inData() bool {
1444 return installer.data
1445}
1446
Colin Cross3f40fa42015-01-30 17:27:36 -08001447//
1448// Combined static+shared libraries
1449//
1450
Colin Cross919281a2016-04-05 16:42:05 -07001451type flagExporter struct {
1452 Properties FlagExporterProperties
1453
1454 flags []string
1455}
1456
1457func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001458 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1459 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001460}
1461
1462func (f *flagExporter) reexportFlags(flags []string) {
1463 f.flags = append(f.flags, flags...)
1464}
1465
1466func (f *flagExporter) exportedFlags() []string {
1467 return f.flags
1468}
1469
1470type exportedFlagsProducer interface {
1471 exportedFlags() []string
1472}
1473
1474var _ exportedFlagsProducer = (*flagExporter)(nil)
1475
Colin Crossca860ac2016-01-04 14:34:37 -08001476type libraryCompiler struct {
1477 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001478
Colin Crossca860ac2016-01-04 14:34:37 -08001479 linker *libraryLinker
1480 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001481
Colin Crossca860ac2016-01-04 14:34:37 -08001482 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001483 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001484}
1485
Colin Crossca860ac2016-01-04 14:34:37 -08001486var _ compiler = (*libraryCompiler)(nil)
1487
1488func (library *libraryCompiler) props() []interface{} {
1489 props := library.baseCompiler.props()
1490 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001491}
1492
Colin Crossca860ac2016-01-04 14:34:37 -08001493func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1494 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001495
Dan Willemsen490fd492015-11-24 17:53:15 -08001496 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1497 // all code is position independent, and then those warnings get promoted to
1498 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001499 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001500 flags.CFlags = append(flags.CFlags, "-fPIC")
1501 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001502
Colin Crossca860ac2016-01-04 14:34:37 -08001503 if library.linker.static() {
1504 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001505 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001506 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001507 }
1508
Colin Crossca860ac2016-01-04 14:34:37 -08001509 return flags
1510}
1511
Colin Cross635c3b02016-05-18 15:37:25 -07001512func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1513 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001514
Dan Willemsenb40aab62016-04-20 14:21:14 -07001515 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001516 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001517
1518 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001519 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001520 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1521 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001522 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001523 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001524 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1525 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001526 }
1527
1528 return objFiles
1529}
1530
1531type libraryLinker struct {
1532 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001533 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001534 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001535
1536 Properties LibraryLinkerProperties
1537
1538 dynamicProperties struct {
1539 BuildStatic bool `blueprint:"mutated"`
1540 BuildShared bool `blueprint:"mutated"`
1541 }
1542
Colin Crossca860ac2016-01-04 14:34:37 -08001543 // If we're used as a whole_static_lib, our missing dependencies need
1544 // to be given
1545 wholeStaticMissingDeps []string
1546
1547 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001548 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001549}
1550
1551var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001552
Dan Willemsenfed4d192016-07-06 21:48:39 -07001553func (library *libraryLinker) begin(ctx BaseModuleContext) {
1554 library.baseLinker.begin(ctx)
1555 if library.static() {
1556 if library.Properties.Static.Enabled != nil &&
1557 !*library.Properties.Static.Enabled {
1558 ctx.module().Disable()
1559 }
1560 } else {
1561 if library.Properties.Shared.Enabled != nil &&
1562 !*library.Properties.Shared.Enabled {
1563 ctx.module().Disable()
1564 }
1565 }
1566}
1567
Colin Crossca860ac2016-01-04 14:34:37 -08001568func (library *libraryLinker) props() []interface{} {
1569 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001570 return append(props,
1571 &library.Properties,
1572 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001573 &library.flagExporter.Properties,
1574 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001575}
1576
1577func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1578 flags = library.baseLinker.flags(ctx, flags)
1579
1580 flags.Nocrt = Bool(library.Properties.Nocrt)
1581
1582 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001583 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001584 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1585 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001586 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001587 sharedFlag = "-shared"
1588 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001589 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001590 flags.LdFlags = append(flags.LdFlags,
1591 "-nostdlib",
1592 "-Wl,--gc-sections",
1593 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001594 }
Colin Cross97ba0732015-03-23 17:50:24 -07001595
Colin Cross0af4b842015-04-30 16:36:18 -07001596 if ctx.Darwin() {
1597 flags.LdFlags = append(flags.LdFlags,
1598 "-dynamiclib",
1599 "-single_module",
1600 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001601 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001602 )
1603 } else {
1604 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001605 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001606 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001607 )
1608 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001609 }
Colin Cross97ba0732015-03-23 17:50:24 -07001610
1611 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001612}
1613
Colin Crossca860ac2016-01-04 14:34:37 -08001614func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1615 deps = library.baseLinker.deps(ctx, deps)
1616 if library.static() {
1617 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1618 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1619 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1620 } else {
1621 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1622 if !ctx.sdk() {
1623 deps.CrtBegin = "crtbegin_so"
1624 deps.CrtEnd = "crtend_so"
1625 } else {
1626 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1627 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1628 }
1629 }
1630 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1631 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1632 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1633 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001634
Colin Crossca860ac2016-01-04 14:34:37 -08001635 return deps
1636}
Colin Cross3f40fa42015-01-30 17:27:36 -08001637
Colin Crossca860ac2016-01-04 14:34:37 -08001638func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001639 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001640
Colin Cross635c3b02016-05-18 15:37:25 -07001641 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001642 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001643
Colin Cross635c3b02016-05-18 15:37:25 -07001644 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001645 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001646
Colin Cross0af4b842015-04-30 16:36:18 -07001647 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001648 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001649 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001650 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001651 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001652
Colin Crossca860ac2016-01-04 14:34:37 -08001653 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001654
1655 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001656
1657 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001658}
1659
Colin Crossca860ac2016-01-04 14:34:37 -08001660func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001661 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001662
Colin Cross635c3b02016-05-18 15:37:25 -07001663 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001664
Colin Cross635c3b02016-05-18 15:37:25 -07001665 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1666 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1667 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1668 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001669 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001670 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001671 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001672 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001673 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001674 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001675 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1676 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001677 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001678 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1679 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001680 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001681 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1682 }
1683 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001684 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001685 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1686 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001687 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001688 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001689 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001690 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001691 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001692 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001693 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001694 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001696 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001697 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001698 }
Colin Crossaee540a2015-07-06 17:48:31 -07001699 }
1700
Colin Cross665dce92016-04-28 14:50:03 -07001701 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001702 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001703 ret := outputFile
1704
1705 builderFlags := flagsToBuilderFlags(flags)
1706
1707 if library.stripper.needsStrip(ctx) {
1708 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001709 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001710 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1711 }
1712
Colin Crossca860ac2016-01-04 14:34:37 -08001713 sharedLibs := deps.SharedLibs
1714 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001715
Colin Crossca860ac2016-01-04 14:34:37 -08001716 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1717 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001718 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001719
Colin Cross665dce92016-04-28 14:50:03 -07001720 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001721}
1722
Colin Crossca860ac2016-01-04 14:34:37 -08001723func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001724 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001725
Colin Crossc99deeb2016-04-11 15:06:20 -07001726 objFiles = append(objFiles, deps.ObjFiles...)
1727
Colin Cross635c3b02016-05-18 15:37:25 -07001728 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001729 if library.static() {
1730 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001731 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001732 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001733 }
1734
Colin Cross919281a2016-04-05 16:42:05 -07001735 library.exportIncludes(ctx, "-I")
1736 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001737
1738 return out
1739}
1740
1741func (library *libraryLinker) buildStatic() bool {
1742 return library.dynamicProperties.BuildStatic
1743}
1744
1745func (library *libraryLinker) buildShared() bool {
1746 return library.dynamicProperties.BuildShared
1747}
1748
1749func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1750 return library.wholeStaticMissingDeps
1751}
1752
Colin Crossc99deeb2016-04-11 15:06:20 -07001753func (library *libraryLinker) installable() bool {
1754 return !library.static()
1755}
1756
Colin Crossca860ac2016-01-04 14:34:37 -08001757type libraryInstaller struct {
1758 baseInstaller
1759
Colin Cross30d5f512016-05-03 18:02:42 -07001760 linker *libraryLinker
1761 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001762}
1763
Colin Cross635c3b02016-05-18 15:37:25 -07001764func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001765 if !library.linker.static() {
1766 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001767 }
1768}
1769
Colin Cross30d5f512016-05-03 18:02:42 -07001770func (library *libraryInstaller) inData() bool {
1771 return library.baseInstaller.inData() || library.sanitize.inData()
1772}
1773
Colin Cross635c3b02016-05-18 15:37:25 -07001774func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1775 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001776
Colin Crossca860ac2016-01-04 14:34:37 -08001777 linker := &libraryLinker{}
1778 linker.dynamicProperties.BuildShared = shared
1779 linker.dynamicProperties.BuildStatic = static
1780 module.linker = linker
1781
1782 module.compiler = &libraryCompiler{
1783 linker: linker,
1784 }
1785 module.installer = &libraryInstaller{
1786 baseInstaller: baseInstaller{
1787 dir: "lib",
1788 dir64: "lib64",
1789 },
Colin Cross30d5f512016-05-03 18:02:42 -07001790 linker: linker,
1791 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001792 }
1793
Colin Crossca860ac2016-01-04 14:34:37 -08001794 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001795}
1796
Colin Crossca860ac2016-01-04 14:34:37 -08001797func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001798 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001799 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001800}
1801
Colin Cross3f40fa42015-01-30 17:27:36 -08001802//
1803// Objects (for crt*.o)
1804//
1805
Colin Crossca860ac2016-01-04 14:34:37 -08001806type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001807 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001808}
1809
Colin Crossca860ac2016-01-04 14:34:37 -08001810func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001811 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001812 module.compiler = &baseCompiler{}
1813 module.linker = &objectLinker{}
1814 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001815}
1816
Colin Cross81413472016-04-11 14:37:39 -07001817func (object *objectLinker) props() []interface{} {
1818 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001819}
1820
Colin Crossca860ac2016-01-04 14:34:37 -08001821func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001822
Colin Cross81413472016-04-11 14:37:39 -07001823func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1824 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001825 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001826}
1827
Colin Crossca860ac2016-01-04 14:34:37 -08001828func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001829 if flags.Clang {
1830 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1831 } else {
1832 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1833 }
1834
Colin Crossca860ac2016-01-04 14:34:37 -08001835 return flags
1836}
1837
1838func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001839 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001840
Colin Cross97ba0732015-03-23 17:50:24 -07001841 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001842
Colin Cross635c3b02016-05-18 15:37:25 -07001843 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001844 if len(objFiles) == 1 {
1845 outputFile = objFiles[0]
1846 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001847 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001848 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001849 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001850 }
1851
Colin Cross3f40fa42015-01-30 17:27:36 -08001852 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001853 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001854}
1855
Colin Crossc99deeb2016-04-11 15:06:20 -07001856func (*objectLinker) installable() bool {
1857 return false
1858}
1859
Colin Cross3f40fa42015-01-30 17:27:36 -08001860//
1861// Executables
1862//
1863
Colin Crossca860ac2016-01-04 14:34:37 -08001864type binaryLinker struct {
1865 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001866 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001867
Colin Crossca860ac2016-01-04 14:34:37 -08001868 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001869
Colin Cross635c3b02016-05-18 15:37:25 -07001870 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001871}
1872
Colin Crossca860ac2016-01-04 14:34:37 -08001873var _ linker = (*binaryLinker)(nil)
1874
1875func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001876 return append(binary.baseLinker.props(),
1877 &binary.Properties,
1878 &binary.stripper.StripProperties)
1879
Colin Cross3f40fa42015-01-30 17:27:36 -08001880}
1881
Colin Crossca860ac2016-01-04 14:34:37 -08001882func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001883 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001884}
1885
Colin Crossca860ac2016-01-04 14:34:37 -08001886func (binary *binaryLinker) buildShared() 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) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001891 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001892 if binary.Properties.Stem != "" {
1893 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001894 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001895
Colin Crossca860ac2016-01-04 14:34:37 -08001896 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001897}
1898
Colin Crossca860ac2016-01-04 14:34:37 -08001899func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1900 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001901 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001902 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001903 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001904 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001905 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001906 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001907 }
Colin Crossca860ac2016-01-04 14:34:37 -08001908 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001909 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001910 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001911 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001912 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001913 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001914 }
Colin Crossca860ac2016-01-04 14:34:37 -08001915 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001916 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001917
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001918 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001919 if inList("libc++_static", deps.StaticLibs) {
1920 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001921 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001922 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1923 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1924 // move them to the beginning of deps.LateStaticLibs
1925 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001926 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001927 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001928 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001929 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001930 }
Colin Crossca860ac2016-01-04 14:34:37 -08001931
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001932 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001933 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1934 "from static libs or set static_executable: true")
1935 }
1936 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001937}
1938
Colin Crossc99deeb2016-04-11 15:06:20 -07001939func (*binaryLinker) installable() bool {
1940 return true
1941}
1942
Colin Cross16b23492016-01-06 14:41:07 -08001943func (binary *binaryLinker) isDependencyRoot() bool {
1944 return true
1945}
1946
Colin Cross635c3b02016-05-18 15:37:25 -07001947func NewBinary(hod android.HostOrDeviceSupported) *Module {
1948 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001949 module.compiler = &baseCompiler{}
1950 module.linker = &binaryLinker{}
1951 module.installer = &baseInstaller{
1952 dir: "bin",
1953 }
1954 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001955}
1956
Colin Crossca860ac2016-01-04 14:34:37 -08001957func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001958 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001959 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001960}
1961
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001962func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1963 binary.baseLinker.begin(ctx)
1964
1965 static := Bool(binary.Properties.Static_executable)
1966 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001967 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001968 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1969 static = true
1970 }
1971 } else {
1972 // Static executables are not supported on Darwin or Windows
1973 static = false
1974 }
Colin Cross0af4b842015-04-30 16:36:18 -07001975 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001976 if static {
1977 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001978 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001979 }
1980}
1981
Colin Crossca860ac2016-01-04 14:34:37 -08001982func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1983 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001984
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001985 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001986 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07001987 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001988 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1989 }
1990 }
1991
1992 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1993 // all code is position independent, and then those warnings get promoted to
1994 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001995 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001996 flags.CFlags = append(flags.CFlags, "-fpie")
1997 }
Colin Cross97ba0732015-03-23 17:50:24 -07001998
Colin Crossf6566ed2015-03-24 11:13:38 -07001999 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002000 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002001 // Clang driver needs -static to create static executable.
2002 // However, bionic/linker uses -shared to overwrite.
2003 // Linker for x86 targets does not allow coexistance of -static and -shared,
2004 // so we add -static only if -shared is not used.
2005 if !inList("-shared", flags.LdFlags) {
2006 flags.LdFlags = append(flags.LdFlags, "-static")
2007 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002008
Colin Crossed4cf0b2015-03-26 14:43:45 -07002009 flags.LdFlags = append(flags.LdFlags,
2010 "-nostdlib",
2011 "-Bstatic",
2012 "-Wl,--gc-sections",
2013 )
2014
2015 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002016 if flags.DynamicLinker == "" {
2017 flags.DynamicLinker = "/system/bin/linker"
2018 if flags.Toolchain.Is64Bit() {
2019 flags.DynamicLinker += "64"
2020 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002021 }
2022
2023 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002024 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002025 "-nostdlib",
2026 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002027 "-Wl,--gc-sections",
2028 "-Wl,-z,nocopyreloc",
2029 )
2030 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002031 } else {
2032 if binary.staticBinary() {
2033 flags.LdFlags = append(flags.LdFlags, "-static")
2034 }
2035 if ctx.Darwin() {
2036 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2037 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002038 }
2039
Colin Cross97ba0732015-03-23 17:50:24 -07002040 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002041}
2042
Colin Crossca860ac2016-01-04 14:34:37 -08002043func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002044 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002045
Colin Cross665dce92016-04-28 14:50:03 -07002046 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002047 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002048 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002049 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002050 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002051 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002052
Colin Cross635c3b02016-05-18 15:37:25 -07002053 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002054
Colin Crossca860ac2016-01-04 14:34:37 -08002055 sharedLibs := deps.SharedLibs
2056 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2057
Colin Cross16b23492016-01-06 14:41:07 -08002058 if flags.DynamicLinker != "" {
2059 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2060 }
2061
Colin Cross665dce92016-04-28 14:50:03 -07002062 builderFlags := flagsToBuilderFlags(flags)
2063
2064 if binary.stripper.needsStrip(ctx) {
2065 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002066 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002067 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2068 }
2069
2070 if binary.Properties.Prefix_symbols != "" {
2071 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002072 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002073 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2074 flagsToBuilderFlags(flags), afterPrefixSymbols)
2075 }
2076
Colin Crossca860ac2016-01-04 14:34:37 -08002077 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002078 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002079 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002080
2081 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002082}
Colin Cross3f40fa42015-01-30 17:27:36 -08002083
Colin Cross635c3b02016-05-18 15:37:25 -07002084func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002085 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002086}
2087
Colin Cross665dce92016-04-28 14:50:03 -07002088type stripper struct {
2089 StripProperties StripProperties
2090}
2091
2092func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2093 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2094}
2095
Colin Cross635c3b02016-05-18 15:37:25 -07002096func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002097 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002098 if ctx.Darwin() {
2099 TransformDarwinStrip(ctx, in, out)
2100 } else {
2101 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2102 // TODO(ccross): don't add gnu debuglink for user builds
2103 flags.stripAddGnuDebuglink = true
2104 TransformStrip(ctx, in, out, flags)
2105 }
Colin Cross665dce92016-04-28 14:50:03 -07002106}
2107
Colin Cross635c3b02016-05-18 15:37:25 -07002108func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002109 if m, ok := mctx.Module().(*Module); ok {
2110 if test, ok := m.linker.(*testLinker); ok {
2111 if Bool(test.Properties.Test_per_src) {
2112 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2113 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2114 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2115 }
2116 tests := mctx.CreateLocalVariations(testNames...)
2117 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2118 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2119 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2120 }
Colin Cross6002e052015-09-16 16:00:08 -07002121 }
2122 }
2123 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002124}
2125
Colin Crossca860ac2016-01-04 14:34:37 -08002126type testLinker struct {
2127 binaryLinker
2128 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002129}
2130
Dan Willemsend30e6102016-03-30 17:35:50 -07002131func (test *testLinker) begin(ctx BaseModuleContext) {
2132 test.binaryLinker.begin(ctx)
2133
2134 runpath := "../../lib"
2135 if ctx.toolchain().Is64Bit() {
2136 runpath += "64"
2137 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002138 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002139}
2140
Colin Crossca860ac2016-01-04 14:34:37 -08002141func (test *testLinker) props() []interface{} {
2142 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002143}
2144
Colin Crossca860ac2016-01-04 14:34:37 -08002145func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2146 flags = test.binaryLinker.flags(ctx, flags)
2147
2148 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002149 return flags
2150 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002151
Colin Cross97ba0732015-03-23 17:50:24 -07002152 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002153 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002154 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002155
Colin Crossa1ad8d12016-06-01 17:09:44 -07002156 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002157 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002158 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002159 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002160 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2161 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002162 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002163 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2164 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002165 }
2166 } else {
2167 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002168 }
2169
Colin Cross21b9a242015-03-24 14:15:58 -07002170 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002171}
2172
Colin Crossca860ac2016-01-04 14:34:37 -08002173func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2174 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002175 if ctx.sdk() && ctx.Device() {
2176 switch ctx.selectedStl() {
2177 case "ndk_libc++_shared", "ndk_libc++_static":
2178 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2179 case "ndk_libgnustl_static":
2180 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2181 default:
2182 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2183 }
2184 } else {
2185 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2186 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002187 }
Colin Crossca860ac2016-01-04 14:34:37 -08002188 deps = test.binaryLinker.deps(ctx, deps)
2189 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002190}
2191
Colin Crossca860ac2016-01-04 14:34:37 -08002192type testInstaller struct {
2193 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002194}
2195
Colin Cross635c3b02016-05-18 15:37:25 -07002196func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002197 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2198 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2199 installer.baseInstaller.install(ctx, file)
2200}
2201
Colin Cross635c3b02016-05-18 15:37:25 -07002202func NewTest(hod android.HostOrDeviceSupported) *Module {
2203 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002204 module.compiler = &baseCompiler{}
2205 linker := &testLinker{}
2206 linker.Properties.Gtest = true
2207 module.linker = linker
2208 module.installer = &testInstaller{
2209 baseInstaller: baseInstaller{
2210 dir: "nativetest",
2211 dir64: "nativetest64",
2212 data: true,
2213 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002214 }
Colin Crossca860ac2016-01-04 14:34:37 -08002215 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002216}
2217
Colin Crossca860ac2016-01-04 14:34:37 -08002218func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002219 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002220 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002221}
2222
Colin Crossca860ac2016-01-04 14:34:37 -08002223type benchmarkLinker struct {
2224 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002225}
2226
Colin Crossca860ac2016-01-04 14:34:37 -08002227func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2228 deps = benchmark.binaryLinker.deps(ctx, deps)
2229 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2230 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002231}
2232
Colin Cross635c3b02016-05-18 15:37:25 -07002233func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2234 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002235 module.compiler = &baseCompiler{}
2236 module.linker = &benchmarkLinker{}
2237 module.installer = &baseInstaller{
2238 dir: "nativetest",
2239 dir64: "nativetest64",
2240 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002241 }
Colin Crossca860ac2016-01-04 14:34:37 -08002242 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002243}
2244
Colin Crossca860ac2016-01-04 14:34:37 -08002245func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002246 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002247 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002248}
2249
Colin Cross3f40fa42015-01-30 17:27:36 -08002250//
2251// Static library
2252//
2253
Colin Crossca860ac2016-01-04 14:34:37 -08002254func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002255 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002256 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002257}
2258
2259//
2260// Shared libraries
2261//
2262
Colin Crossca860ac2016-01-04 14:34:37 -08002263func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002264 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002265 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002266}
2267
2268//
2269// Host static library
2270//
2271
Colin Crossca860ac2016-01-04 14:34:37 -08002272func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002273 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002274 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002275}
2276
2277//
2278// Host Shared libraries
2279//
2280
Colin Crossca860ac2016-01-04 14:34:37 -08002281func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002282 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002283 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002284}
2285
2286//
2287// Host Binaries
2288//
2289
Colin Crossca860ac2016-01-04 14:34:37 -08002290func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002291 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002292 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002293}
2294
2295//
Colin Cross1f8f2342015-03-26 16:09:47 -07002296// Host Tests
2297//
2298
Colin Crossca860ac2016-01-04 14:34:37 -08002299func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002300 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002301 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002302}
2303
2304//
Colin Cross2ba19d92015-05-07 15:44:20 -07002305// Host Benchmarks
2306//
2307
Colin Crossca860ac2016-01-04 14:34:37 -08002308func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002309 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002310 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002311}
2312
2313//
Colin Crosscfad1192015-11-02 16:43:11 -08002314// Defaults
2315//
Colin Crossca860ac2016-01-04 14:34:37 -08002316type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002317 android.ModuleBase
2318 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002319}
2320
Colin Cross635c3b02016-05-18 15:37:25 -07002321func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002322}
2323
Colin Crossca860ac2016-01-04 14:34:37 -08002324func defaultsFactory() (blueprint.Module, []interface{}) {
2325 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002326
2327 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002328 &BaseProperties{},
2329 &BaseCompilerProperties{},
2330 &BaseLinkerProperties{},
2331 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002332 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002333 &LibraryLinkerProperties{},
2334 &BinaryLinkerProperties{},
2335 &TestLinkerProperties{},
2336 &UnusedProperties{},
2337 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002338 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002339 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002340 }
2341
Colin Cross635c3b02016-05-18 15:37:25 -07002342 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2343 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002344
Colin Cross635c3b02016-05-18 15:37:25 -07002345 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002346}
2347
2348//
Colin Cross3f40fa42015-01-30 17:27:36 -08002349// Device libraries shipped with gcc
2350//
2351
Colin Crossca860ac2016-01-04 14:34:37 -08002352type toolchainLibraryLinker struct {
2353 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002354}
2355
Colin Crossca860ac2016-01-04 14:34:37 -08002356var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2357
2358func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002359 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002360 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002361}
2362
Colin Crossca860ac2016-01-04 14:34:37 -08002363func (*toolchainLibraryLinker) buildStatic() bool {
2364 return true
2365}
Colin Cross3f40fa42015-01-30 17:27:36 -08002366
Colin Crossca860ac2016-01-04 14:34:37 -08002367func (*toolchainLibraryLinker) buildShared() bool {
2368 return false
2369}
2370
2371func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002372 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002373 module.compiler = &baseCompiler{}
2374 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002375 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002376 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002377}
2378
Colin Crossca860ac2016-01-04 14:34:37 -08002379func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002380 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002381
2382 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002383 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002384
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002385 if flags.Clang {
2386 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2387 }
2388
Colin Crossca860ac2016-01-04 14:34:37 -08002389 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002390
2391 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002392
Colin Crossca860ac2016-01-04 14:34:37 -08002393 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002394}
2395
Colin Crossc99deeb2016-04-11 15:06:20 -07002396func (*toolchainLibraryLinker) installable() bool {
2397 return false
2398}
2399
Dan Albertbe961682015-03-18 23:38:50 -07002400// NDK prebuilt libraries.
2401//
2402// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2403// either (with the exception of the shared STLs, which are installed to the app's directory rather
2404// than to the system image).
2405
Colin Cross635c3b02016-05-18 15:37:25 -07002406func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002407 suffix := ""
2408 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2409 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002410 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002411 suffix = "64"
2412 }
Colin Cross635c3b02016-05-18 15:37:25 -07002413 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002414 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002415}
2416
Colin Cross635c3b02016-05-18 15:37:25 -07002417func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2418 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002419
2420 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2421 // We want to translate to just NAME.EXT
2422 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2423 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002424 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002425}
2426
Colin Crossca860ac2016-01-04 14:34:37 -08002427type ndkPrebuiltObjectLinker struct {
2428 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002429}
2430
Colin Crossca860ac2016-01-04 14:34:37 -08002431func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002432 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002433 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002434}
2435
Colin Crossca860ac2016-01-04 14:34:37 -08002436func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002437 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002438 module.linker = &ndkPrebuiltObjectLinker{}
2439 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002440}
2441
Colin Crossca860ac2016-01-04 14:34:37 -08002442func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002443 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002444 // A null build step, but it sets up the output path.
2445 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2446 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2447 }
2448
Colin Crossca860ac2016-01-04 14:34:37 -08002449 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002450}
2451
Colin Crossca860ac2016-01-04 14:34:37 -08002452type ndkPrebuiltLibraryLinker struct {
2453 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002454}
2455
Colin Crossca860ac2016-01-04 14:34:37 -08002456var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2457var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002458
Colin Crossca860ac2016-01-04 14:34:37 -08002459func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002460 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002461}
2462
Colin Crossca860ac2016-01-04 14:34:37 -08002463func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002464 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002465 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002466}
2467
Colin Crossca860ac2016-01-04 14:34:37 -08002468func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002469 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002470 linker := &ndkPrebuiltLibraryLinker{}
2471 linker.dynamicProperties.BuildShared = true
2472 module.linker = linker
2473 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002474}
2475
Colin Crossca860ac2016-01-04 14:34:37 -08002476func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002477 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002478 // A null build step, but it sets up the output path.
2479 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2480 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2481 }
2482
Colin Cross919281a2016-04-05 16:42:05 -07002483 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002484
Colin Crossca860ac2016-01-04 14:34:37 -08002485 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2486 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002487}
2488
2489// The NDK STLs are slightly different from the prebuilt system libraries:
2490// * Are not specific to each platform version.
2491// * The libraries are not in a predictable location for each STL.
2492
Colin Crossca860ac2016-01-04 14:34:37 -08002493type ndkPrebuiltStlLinker struct {
2494 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002495}
2496
Colin Crossca860ac2016-01-04 14:34:37 -08002497func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002498 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002499 linker := &ndkPrebuiltStlLinker{}
2500 linker.dynamicProperties.BuildShared = true
2501 module.linker = linker
2502 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002503}
2504
Colin Crossca860ac2016-01-04 14:34:37 -08002505func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002506 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002507 linker := &ndkPrebuiltStlLinker{}
2508 linker.dynamicProperties.BuildStatic = true
2509 module.linker = linker
2510 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002511}
2512
Colin Cross635c3b02016-05-18 15:37:25 -07002513func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002514 gccVersion := toolchain.GccVersion()
2515 var libDir string
2516 switch stl {
2517 case "libstlport":
2518 libDir = "cxx-stl/stlport/libs"
2519 case "libc++":
2520 libDir = "cxx-stl/llvm-libc++/libs"
2521 case "libgnustl":
2522 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2523 }
2524
2525 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002526 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002527 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002528 }
2529
2530 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002531 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002532}
2533
Colin Crossca860ac2016-01-04 14:34:37 -08002534func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002535 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002536 // A null build step, but it sets up the output path.
2537 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2538 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2539 }
2540
Colin Cross919281a2016-04-05 16:42:05 -07002541 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002542
2543 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002544 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002545 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002546 libExt = staticLibraryExtension
2547 }
2548
2549 stlName := strings.TrimSuffix(libName, "_shared")
2550 stlName = strings.TrimSuffix(stlName, "_static")
2551 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002552 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002553}
2554
Colin Cross635c3b02016-05-18 15:37:25 -07002555func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002556 if m, ok := mctx.Module().(*Module); ok {
2557 if m.linker != nil {
2558 if linker, ok := m.linker.(baseLinkerInterface); ok {
2559 var modules []blueprint.Module
2560 if linker.buildStatic() && linker.buildShared() {
2561 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002562 static := modules[0].(*Module)
2563 shared := modules[1].(*Module)
2564
2565 static.linker.(baseLinkerInterface).setStatic(true)
2566 shared.linker.(baseLinkerInterface).setStatic(false)
2567
2568 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2569 sharedCompiler := shared.compiler.(*libraryCompiler)
2570 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2571 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2572 // Optimize out compiling common .o files twice for static+shared libraries
2573 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2574 sharedCompiler.baseCompiler.Properties.Srcs = nil
2575 }
2576 }
Colin Crossca860ac2016-01-04 14:34:37 -08002577 } else if linker.buildStatic() {
2578 modules = mctx.CreateLocalVariations("static")
2579 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2580 } else if linker.buildShared() {
2581 modules = mctx.CreateLocalVariations("shared")
2582 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2583 } else {
2584 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2585 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002586 }
2587 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002588 }
2589}
Colin Cross74d1ec02015-04-28 13:30:13 -07002590
2591// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2592// modifies the slice contents in place, and returns a subslice of the original slice
2593func lastUniqueElements(list []string) []string {
2594 totalSkip := 0
2595 for i := len(list) - 1; i >= totalSkip; i-- {
2596 skip := 0
2597 for j := i - 1; j >= totalSkip; j-- {
2598 if list[i] == list[j] {
2599 skip++
2600 } else {
2601 list[j+skip] = list[j]
2602 }
2603 }
2604 totalSkip += skip
2605 }
2606 return list[totalSkip:]
2607}
Colin Cross06a931b2015-10-28 17:23:31 -07002608
2609var Bool = proptools.Bool