blob: 15b8cb72108640fe68abbeceb5aaf798c1a24894 [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 }
Dan Willemsen97704ed2016-07-07 21:40:39 -0700118
119 ndkPrebuiltSharedLibs = []string{
120 "android",
121 "c",
122 "dl",
123 "EGL",
124 "GLESv1_CM",
125 "GLESv2",
126 "GLESv3",
127 "jnigraphics",
128 "log",
129 "mediandk",
130 "m",
131 "OpenMAXAL",
132 "OpenSLES",
133 "stdc++",
134 "vulkan",
135 "z",
136 }
137 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800138)
139
140func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700141 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700142 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
143 }
144
Colin Cross3f40fa42015-01-30 17:27:36 -0800145 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
146 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
147 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800148 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800149
150 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
151
152 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800153 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800154 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800155 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800156 pctx.StaticVariable("hostClangGlobalCflags",
157 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800158 pctx.StaticVariable("noOverrideClangGlobalCflags",
159 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
160
Tim Kilbournf2948142015-03-11 12:03:03 -0700161 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800162 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
164 // Everything in this list is a crime against abstraction and dependency tracking.
165 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800166 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700167 []string{
168 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800169 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 "hardware/libhardware/include",
171 "hardware/libhardware_legacy/include",
172 "hardware/ril/include",
173 "libnativehelper/include",
174 "frameworks/native/include",
175 "frameworks/native/opengl/include",
176 "frameworks/av/include",
177 "frameworks/base/include",
178 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800179 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
180 // with this, since there is no associated library.
181 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
182 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800183
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700184 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
185 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700186 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700187 return override, nil
188 }
189 return "${clangDefaultBase}", nil
190 })
191 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700192 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700193 return override, nil
194 }
Stephen Hines369f0132016-04-26 14:34:07 -0700195 return "clang-2812033", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700196 })
Colin Cross16b23492016-01-06 14:41:07 -0800197 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
198 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800199}
200
Colin Crossca860ac2016-01-04 14:34:37 -0800201type Deps struct {
202 SharedLibs, LateSharedLibs []string
203 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700204
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700205 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
206
Colin Cross81413472016-04-11 14:37:39 -0700207 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700208
Dan Willemsenb40aab62016-04-20 14:21:14 -0700209 GeneratedSources []string
210 GeneratedHeaders []string
211
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700212 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700213
Colin Cross97ba0732015-03-23 17:50:24 -0700214 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700215}
216
Colin Crossca860ac2016-01-04 14:34:37 -0800217type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700218 SharedLibs, LateSharedLibs android.Paths
219 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700220
Colin Cross635c3b02016-05-18 15:37:25 -0700221 ObjFiles android.Paths
222 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700223
Colin Cross635c3b02016-05-18 15:37:25 -0700224 GeneratedSources android.Paths
225 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700226
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700227 Cflags, ReexportedCflags []string
228
Colin Cross635c3b02016-05-18 15:37:25 -0700229 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700230}
231
Colin Crossca860ac2016-01-04 14:34:37 -0800232type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700233 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
234 AsFlags []string // Flags that apply to assembly source files
235 CFlags []string // Flags that apply to C and C++ source files
236 ConlyFlags []string // Flags that apply to C source files
237 CppFlags []string // Flags that apply to C++ source files
238 YaccFlags []string // Flags that apply to Yacc source files
239 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800240 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700241
242 Nocrt bool
243 Toolchain Toolchain
244 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800245
246 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800247 DynamicLinker string
248
Colin Cross635c3b02016-05-18 15:37:25 -0700249 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700250}
251
Colin Crossca860ac2016-01-04 14:34:37 -0800252type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700253 // 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 -0700254 Srcs []string `android:"arch_variant"`
255
256 // list of source files that should not be used to build the C/C++ module.
257 // This is most useful in the arch/multilib variants to remove non-common files
258 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700259
260 // list of module-specific flags that will be used for C and C++ compiles.
261 Cflags []string `android:"arch_variant"`
262
263 // list of module-specific flags that will be used for C++ compiles
264 Cppflags []string `android:"arch_variant"`
265
266 // list of module-specific flags that will be used for C compiles
267 Conlyflags []string `android:"arch_variant"`
268
269 // list of module-specific flags that will be used for .S compiles
270 Asflags []string `android:"arch_variant"`
271
Colin Crossca860ac2016-01-04 14:34:37 -0800272 // list of module-specific flags that will be used for C and C++ compiles when
273 // compiling with clang
274 Clang_cflags []string `android:"arch_variant"`
275
276 // list of module-specific flags that will be used for .S compiles when
277 // compiling with clang
278 Clang_asflags []string `android:"arch_variant"`
279
Colin Cross7d5136f2015-05-11 13:39:40 -0700280 // list of module-specific flags that will be used for .y and .yy compiles
281 Yaccflags []string
282
Colin Cross7d5136f2015-05-11 13:39:40 -0700283 // the instruction set architecture to use to compile the C/C++
284 // module.
285 Instruction_set string `android:"arch_variant"`
286
287 // list of directories relative to the root of the source tree that will
288 // be added to the include path using -I.
289 // If possible, don't use this. If adding paths from the current directory use
290 // local_include_dirs, if adding paths from other modules use export_include_dirs in
291 // that module.
292 Include_dirs []string `android:"arch_variant"`
293
294 // list of directories relative to the Blueprints file that will
295 // be added to the include path using -I
296 Local_include_dirs []string `android:"arch_variant"`
297
Dan Willemsenb40aab62016-04-20 14:21:14 -0700298 // list of generated sources to compile. These are the names of gensrcs or
299 // genrule modules.
300 Generated_sources []string `android:"arch_variant"`
301
302 // list of generated headers to add to the include path. These are the names
303 // of genrule modules.
304 Generated_headers []string `android:"arch_variant"`
305
Colin Crossca860ac2016-01-04 14:34:37 -0800306 // pass -frtti instead of -fno-rtti
307 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700308
Colin Crossca860ac2016-01-04 14:34:37 -0800309 Debug, Release struct {
310 // list of module-specific flags that will be used for C and C++ compiles in debug or
311 // release builds
312 Cflags []string `android:"arch_variant"`
313 } `android:"arch_variant"`
314}
Colin Cross7d5136f2015-05-11 13:39:40 -0700315
Colin Crossca860ac2016-01-04 14:34:37 -0800316type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700317 // list of modules whose object files should be linked into this module
318 // in their entirety. For static library modules, all of the .o files from the intermediate
319 // directory of the dependency will be linked into this modules .a file. For a shared library,
320 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700321 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700322
323 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700324 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700325
326 // list of modules that should be dynamically linked into this module.
327 Shared_libs []string `android:"arch_variant"`
328
Colin Crossca860ac2016-01-04 14:34:37 -0800329 // list of module-specific flags that will be used for all link steps
330 Ldflags []string `android:"arch_variant"`
331
332 // don't insert default compiler flags into asflags, cflags,
333 // cppflags, conlyflags, ldflags, or include_dirs
334 No_default_compiler_flags *bool
335
336 // list of system libraries that will be dynamically linked to
337 // shared library and executable modules. If unset, generally defaults to libc
338 // and libm. Set to [] to prevent linking against libc and libm.
339 System_shared_libs []string
340
Colin Cross7d5136f2015-05-11 13:39:40 -0700341 // allow the module to contain undefined symbols. By default,
342 // modules cannot contain undefined symbols that are not satisified by their immediate
343 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
344 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700345 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700346
Dan Willemsend67be222015-09-16 15:19:33 -0700347 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700348 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700349
Colin Cross7d5136f2015-05-11 13:39:40 -0700350 // -l arguments to pass to linker for host-provided shared libraries
351 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700352
353 // list of shared libraries to re-export include directories from. Entries must be
354 // present in shared_libs.
355 Export_shared_lib_headers []string `android:"arch_variant"`
356
357 // list of static libraries to re-export include directories from. Entries must be
358 // present in static_libs.
359 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800360}
Colin Cross7d5136f2015-05-11 13:39:40 -0700361
Colin Crossca860ac2016-01-04 14:34:37 -0800362type LibraryCompilerProperties struct {
363 Static struct {
364 Srcs []string `android:"arch_variant"`
365 Exclude_srcs []string `android:"arch_variant"`
366 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700367 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800368 Shared struct {
369 Srcs []string `android:"arch_variant"`
370 Exclude_srcs []string `android:"arch_variant"`
371 Cflags []string `android:"arch_variant"`
372 } `android:"arch_variant"`
373}
374
Colin Cross919281a2016-04-05 16:42:05 -0700375type FlagExporterProperties struct {
376 // list of directories relative to the Blueprints file that will
377 // be added to the include path using -I for any module that links against this module
378 Export_include_dirs []string `android:"arch_variant"`
379}
380
Colin Crossca860ac2016-01-04 14:34:37 -0800381type LibraryLinkerProperties struct {
382 Static struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700383 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800384 Whole_static_libs []string `android:"arch_variant"`
385 Static_libs []string `android:"arch_variant"`
386 Shared_libs []string `android:"arch_variant"`
387 } `android:"arch_variant"`
388 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700389 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800390 Whole_static_libs []string `android:"arch_variant"`
391 Static_libs []string `android:"arch_variant"`
392 Shared_libs []string `android:"arch_variant"`
393 } `android:"arch_variant"`
394
395 // local file name to pass to the linker as --version_script
396 Version_script *string `android:"arch_variant"`
397 // local file name to pass to the linker as -unexported_symbols_list
398 Unexported_symbols_list *string `android:"arch_variant"`
399 // local file name to pass to the linker as -force_symbols_not_weak_list
400 Force_symbols_not_weak_list *string `android:"arch_variant"`
401 // local file name to pass to the linker as -force_symbols_weak_list
402 Force_symbols_weak_list *string `android:"arch_variant"`
403
Colin Crossca860ac2016-01-04 14:34:37 -0800404 // don't link in crt_begin and crt_end. This flag should only be necessary for
405 // compiling crt or libc.
406 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800407
408 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800409}
410
411type BinaryLinkerProperties struct {
412 // compile executable with -static
413 Static_executable *bool
414
415 // set the name of the output
416 Stem string `android:"arch_variant"`
417
418 // append to the name of the output
419 Suffix string `android:"arch_variant"`
420
421 // if set, add an extra objcopy --prefix-symbols= step
422 Prefix_symbols string
423}
424
425type TestLinkerProperties struct {
426 // if set, build against the gtest library. Defaults to true.
427 Gtest bool
428
429 // Create a separate binary for each source file. Useful when there is
430 // global state that can not be torn down and reset between each test suite.
431 Test_per_src *bool
432}
433
Colin Cross81413472016-04-11 14:37:39 -0700434type ObjectLinkerProperties struct {
435 // names of other cc_object modules to link into this module using partial linking
436 Objs []string `android:"arch_variant"`
437}
438
Colin Crossca860ac2016-01-04 14:34:37 -0800439// Properties used to compile all C or C++ modules
440type BaseProperties struct {
441 // compile module with clang instead of gcc
442 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700443
444 // Minimum sdk version supported when compiling against the ndk
445 Sdk_version string
446
Colin Crossca860ac2016-01-04 14:34:37 -0800447 // don't insert default compiler flags into asflags, cflags,
448 // cppflags, conlyflags, ldflags, or include_dirs
449 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700450
451 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700452 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800453}
454
455type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700456 // install to a subdirectory of the default install path for the module
457 Relative_install_path string
458}
459
Colin Cross665dce92016-04-28 14:50:03 -0700460type StripProperties struct {
461 Strip struct {
462 None bool
463 Keep_symbols bool
464 }
465}
466
Colin Crossca860ac2016-01-04 14:34:37 -0800467type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700468 Native_coverage *bool
469 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700470 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800471}
472
Colin Crossca860ac2016-01-04 14:34:37 -0800473type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800474 static() bool
475 staticBinary() bool
476 clang() bool
477 toolchain() Toolchain
478 noDefaultCompilerFlags() bool
479 sdk() bool
480 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700481 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800482}
483
484type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700485 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800486 ModuleContextIntf
487}
488
489type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700490 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800491 ModuleContextIntf
492}
493
494type Customizer interface {
495 CustomizeProperties(BaseModuleContext)
496 Properties() []interface{}
497}
498
499type feature interface {
500 begin(ctx BaseModuleContext)
501 deps(ctx BaseModuleContext, deps Deps) Deps
502 flags(ctx ModuleContext, flags Flags) Flags
503 props() []interface{}
504}
505
506type compiler interface {
507 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700508 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800509}
510
511type linker interface {
512 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700513 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700514 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800515}
516
517type installer interface {
518 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700519 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800520 inData() bool
521}
522
Colin Crossc99deeb2016-04-11 15:06:20 -0700523type dependencyTag struct {
524 blueprint.BaseDependencyTag
525 name string
526 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700527
528 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700529}
530
531var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700532 sharedDepTag = dependencyTag{name: "shared", library: true}
533 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
534 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
535 staticDepTag = dependencyTag{name: "static", library: true}
536 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
537 lateStaticDepTag = dependencyTag{name: "late static", library: true}
538 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
539 genSourceDepTag = dependencyTag{name: "gen source"}
540 genHeaderDepTag = dependencyTag{name: "gen header"}
541 objDepTag = dependencyTag{name: "obj"}
542 crtBeginDepTag = dependencyTag{name: "crtbegin"}
543 crtEndDepTag = dependencyTag{name: "crtend"}
544 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700545)
546
Colin Crossca860ac2016-01-04 14:34:37 -0800547// Module contains the properties and members used by all C/C++ module types, and implements
548// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
549// to construct the output file. Behavior can be customized with a Customizer interface
550type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700551 android.ModuleBase
552 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700553
Colin Crossca860ac2016-01-04 14:34:37 -0800554 Properties BaseProperties
555 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700556
Colin Crossca860ac2016-01-04 14:34:37 -0800557 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700558 hod android.HostOrDeviceSupported
559 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700560
Colin Crossca860ac2016-01-04 14:34:37 -0800561 // delegates, initialize before calling Init
562 customizer Customizer
563 features []feature
564 compiler compiler
565 linker linker
566 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700567 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800568 sanitize *sanitize
569
570 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700571
Colin Cross635c3b02016-05-18 15:37:25 -0700572 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800573
574 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700575}
576
Colin Crossca860ac2016-01-04 14:34:37 -0800577func (c *Module) Init() (blueprint.Module, []interface{}) {
578 props := []interface{}{&c.Properties, &c.unused}
579 if c.customizer != nil {
580 props = append(props, c.customizer.Properties()...)
581 }
582 if c.compiler != nil {
583 props = append(props, c.compiler.props()...)
584 }
585 if c.linker != nil {
586 props = append(props, c.linker.props()...)
587 }
588 if c.installer != nil {
589 props = append(props, c.installer.props()...)
590 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700591 if c.stl != nil {
592 props = append(props, c.stl.props()...)
593 }
Colin Cross16b23492016-01-06 14:41:07 -0800594 if c.sanitize != nil {
595 props = append(props, c.sanitize.props()...)
596 }
Colin Crossca860ac2016-01-04 14:34:37 -0800597 for _, feature := range c.features {
598 props = append(props, feature.props()...)
599 }
Colin Crossc472d572015-03-17 15:06:21 -0700600
Colin Cross635c3b02016-05-18 15:37:25 -0700601 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700602
Colin Cross635c3b02016-05-18 15:37:25 -0700603 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700604}
605
Colin Crossca860ac2016-01-04 14:34:37 -0800606type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700607 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800608 moduleContextImpl
609}
610
611type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700612 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800613 moduleContextImpl
614}
615
616type moduleContextImpl struct {
617 mod *Module
618 ctx BaseModuleContext
619}
620
Colin Crossca860ac2016-01-04 14:34:37 -0800621func (ctx *moduleContextImpl) clang() bool {
622 return ctx.mod.clang(ctx.ctx)
623}
624
625func (ctx *moduleContextImpl) toolchain() Toolchain {
626 return ctx.mod.toolchain(ctx.ctx)
627}
628
629func (ctx *moduleContextImpl) static() bool {
630 if ctx.mod.linker == nil {
631 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
632 }
633 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
634 return linker.static()
635 } else {
636 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
637 }
638}
639
640func (ctx *moduleContextImpl) staticBinary() bool {
641 if ctx.mod.linker == nil {
642 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
643 }
644 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
645 return linker.staticBinary()
646 } else {
647 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
648 }
649}
650
651func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
652 return Bool(ctx.mod.Properties.No_default_compiler_flags)
653}
654
655func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700656 if ctx.ctx.Device() {
657 return ctx.mod.Properties.Sdk_version != ""
658 }
659 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800660}
661
662func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700663 if ctx.ctx.Device() {
664 return ctx.mod.Properties.Sdk_version
665 }
666 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800667}
668
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700669func (ctx *moduleContextImpl) selectedStl() string {
670 if stl := ctx.mod.stl; stl != nil {
671 return stl.Properties.SelectedStl
672 }
673 return ""
674}
675
Colin Cross635c3b02016-05-18 15:37:25 -0700676func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800677 return &Module{
678 hod: hod,
679 multilib: multilib,
680 }
681}
682
Colin Cross635c3b02016-05-18 15:37:25 -0700683func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800684 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700685 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800686 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800687 return module
688}
689
Colin Cross635c3b02016-05-18 15:37:25 -0700690func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800691 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700692 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800693 moduleContextImpl: moduleContextImpl{
694 mod: c,
695 },
696 }
697 ctx.ctx = ctx
698
699 flags := Flags{
700 Toolchain: c.toolchain(ctx),
701 Clang: c.clang(ctx),
702 }
Colin Crossca860ac2016-01-04 14:34:37 -0800703 if c.compiler != nil {
704 flags = c.compiler.flags(ctx, flags)
705 }
706 if c.linker != nil {
707 flags = c.linker.flags(ctx, flags)
708 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700709 if c.stl != nil {
710 flags = c.stl.flags(ctx, flags)
711 }
Colin Cross16b23492016-01-06 14:41:07 -0800712 if c.sanitize != nil {
713 flags = c.sanitize.flags(ctx, flags)
714 }
Colin Crossca860ac2016-01-04 14:34:37 -0800715 for _, feature := range c.features {
716 flags = feature.flags(ctx, flags)
717 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800718 if ctx.Failed() {
719 return
720 }
721
Colin Crossca860ac2016-01-04 14:34:37 -0800722 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
723 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
724 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800725
Colin Crossca860ac2016-01-04 14:34:37 -0800726 // Optimization to reduce size of build.ninja
727 // Replace the long list of flags for each file with a module-local variable
728 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
729 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
730 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
731 flags.CFlags = []string{"$cflags"}
732 flags.CppFlags = []string{"$cppflags"}
733 flags.AsFlags = []string{"$asflags"}
734
Colin Crossc99deeb2016-04-11 15:06:20 -0700735 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800736 if ctx.Failed() {
737 return
738 }
739
Colin Cross28344522015-04-22 13:07:53 -0700740 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700741
Colin Cross635c3b02016-05-18 15:37:25 -0700742 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800743 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700744 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800745 if ctx.Failed() {
746 return
747 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800748 }
749
Colin Crossca860ac2016-01-04 14:34:37 -0800750 if c.linker != nil {
751 outputFile := c.linker.link(ctx, flags, deps, objFiles)
752 if ctx.Failed() {
753 return
754 }
Colin Cross635c3b02016-05-18 15:37:25 -0700755 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700756
Colin Crossc99deeb2016-04-11 15:06:20 -0700757 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800758 c.installer.install(ctx, outputFile)
759 if ctx.Failed() {
760 return
761 }
762 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700763 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800764}
765
Colin Crossca860ac2016-01-04 14:34:37 -0800766func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
767 if c.cachedToolchain == nil {
768 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700769 os := ctx.Os()
770 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800771 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700772 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800773 return nil
774 }
775 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800776 }
Colin Crossca860ac2016-01-04 14:34:37 -0800777 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800778}
779
Colin Crossca860ac2016-01-04 14:34:37 -0800780func (c *Module) begin(ctx BaseModuleContext) {
781 if c.compiler != nil {
782 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700783 }
Colin Crossca860ac2016-01-04 14:34:37 -0800784 if c.linker != nil {
785 c.linker.begin(ctx)
786 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700787 if c.stl != nil {
788 c.stl.begin(ctx)
789 }
Colin Cross16b23492016-01-06 14:41:07 -0800790 if c.sanitize != nil {
791 c.sanitize.begin(ctx)
792 }
Colin Crossca860ac2016-01-04 14:34:37 -0800793 for _, feature := range c.features {
794 feature.begin(ctx)
795 }
796}
797
Colin Crossc99deeb2016-04-11 15:06:20 -0700798func (c *Module) deps(ctx BaseModuleContext) Deps {
799 deps := Deps{}
800
801 if c.compiler != nil {
802 deps = c.compiler.deps(ctx, deps)
803 }
804 if c.linker != nil {
805 deps = c.linker.deps(ctx, deps)
806 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700807 if c.stl != nil {
808 deps = c.stl.deps(ctx, deps)
809 }
Colin Cross16b23492016-01-06 14:41:07 -0800810 if c.sanitize != nil {
811 deps = c.sanitize.deps(ctx, deps)
812 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700813 for _, feature := range c.features {
814 deps = feature.deps(ctx, deps)
815 }
816
817 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
818 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
819 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
820 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
821 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
822
Dan Willemsen97704ed2016-07-07 21:40:39 -0700823 if ctx.sdk() {
824 version := "." + ctx.sdkVersion()
825
826 rewriteNdkLibs := func(list []string) []string {
827 for i, entry := range list {
828 if inList(entry, ndkPrebuiltSharedLibraries) {
829 list[i] = "ndk_" + entry + version
830 }
831 }
832 return list
833 }
834
835 deps.SharedLibs = rewriteNdkLibs(deps.SharedLibs)
836 deps.LateSharedLibs = rewriteNdkLibs(deps.LateSharedLibs)
837 }
838
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700839 for _, lib := range deps.ReexportSharedLibHeaders {
840 if !inList(lib, deps.SharedLibs) {
841 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
842 }
843 }
844
845 for _, lib := range deps.ReexportStaticLibHeaders {
846 if !inList(lib, deps.StaticLibs) {
847 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
848 }
849 }
850
Colin Crossc99deeb2016-04-11 15:06:20 -0700851 return deps
852}
853
Colin Cross635c3b02016-05-18 15:37:25 -0700854func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800855 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700856 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800857 moduleContextImpl: moduleContextImpl{
858 mod: c,
859 },
860 }
861 ctx.ctx = ctx
862
863 if c.customizer != nil {
864 c.customizer.CustomizeProperties(ctx)
865 }
866
867 c.begin(ctx)
868
Colin Crossc99deeb2016-04-11 15:06:20 -0700869 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800870
Colin Crossc99deeb2016-04-11 15:06:20 -0700871 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
872
873 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
874 deps.WholeStaticLibs...)
875
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700876 for _, lib := range deps.StaticLibs {
877 depTag := staticDepTag
878 if inList(lib, deps.ReexportStaticLibHeaders) {
879 depTag = staticExportDepTag
880 }
881 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
882 deps.StaticLibs...)
883 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700884
885 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
886 deps.LateStaticLibs...)
887
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700888 for _, lib := range deps.SharedLibs {
889 depTag := sharedDepTag
890 if inList(lib, deps.ReexportSharedLibHeaders) {
891 depTag = sharedExportDepTag
892 }
893 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
894 deps.SharedLibs...)
895 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700896
897 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
898 deps.LateSharedLibs...)
899
Colin Cross68861832016-07-08 10:41:41 -0700900 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
901 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700902
Colin Cross68861832016-07-08 10:41:41 -0700903 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700904
905 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700906 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800907 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700908 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700909 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700910 }
Colin Cross6362e272015-10-29 15:25:03 -0700911}
Colin Cross21b9a242015-03-24 14:15:58 -0700912
Colin Cross635c3b02016-05-18 15:37:25 -0700913func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800914 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700915 c.depsMutator(ctx)
916 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800917}
918
Colin Crossca860ac2016-01-04 14:34:37 -0800919func (c *Module) clang(ctx BaseModuleContext) bool {
920 clang := Bool(c.Properties.Clang)
921
922 if c.Properties.Clang == nil {
923 if ctx.Host() {
924 clang = true
925 }
926
927 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
928 clang = true
929 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800930 }
Colin Cross28344522015-04-22 13:07:53 -0700931
Colin Crossca860ac2016-01-04 14:34:37 -0800932 if !c.toolchain(ctx).ClangSupported() {
933 clang = false
934 }
935
936 return clang
937}
938
Colin Crossc99deeb2016-04-11 15:06:20 -0700939// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700940func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800941 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800942
Dan Willemsena96ff642016-06-07 12:34:45 -0700943 // Whether a module can link to another module, taking into
944 // account NDK linking.
945 linkTypeOk := func(from, to *Module) bool {
946 if from.Target().Os != android.Android {
947 // Host code is not restricted
948 return true
949 }
950 if from.Properties.Sdk_version == "" {
951 // Platform code can link to anything
952 return true
953 }
954 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
955 // These are always allowed
956 return true
957 }
958 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
959 // These are allowed, but don't set sdk_version
960 return true
961 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -0700962 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
963 // These are allowed, but don't set sdk_version
964 return true
965 }
966 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -0700967 }
968
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 ctx.VisitDirectDeps(func(m blueprint.Module) {
970 name := ctx.OtherModuleName(m)
971 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800972
Colin Cross635c3b02016-05-18 15:37:25 -0700973 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700974 if a == nil {
975 ctx.ModuleErrorf("module %q not an android module", name)
976 return
Colin Crossca860ac2016-01-04 14:34:37 -0800977 }
Colin Crossca860ac2016-01-04 14:34:37 -0800978
Dan Willemsena96ff642016-06-07 12:34:45 -0700979 cc, _ := m.(*Module)
980 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700981 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700982 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700983 case genSourceDepTag:
984 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
985 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
986 genRule.GeneratedSourceFiles()...)
987 } else {
988 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
989 }
990 case genHeaderDepTag:
991 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
992 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
993 genRule.GeneratedSourceFiles()...)
994 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700995 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700996 } else {
997 ctx.ModuleErrorf("module %q is not a genrule", name)
998 }
999 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001000 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001001 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001002 return
1003 }
1004
1005 if !a.Enabled() {
1006 ctx.ModuleErrorf("depends on disabled module %q", name)
1007 return
1008 }
1009
Colin Crossa1ad8d12016-06-01 17:09:44 -07001010 if a.Target().Os != ctx.Os() {
1011 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1012 return
1013 }
1014
1015 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1016 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001017 return
1018 }
1019
Dan Willemsena96ff642016-06-07 12:34:45 -07001020 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001021 ctx.ModuleErrorf("module %q missing output file", name)
1022 return
1023 }
1024
1025 if tag == reuseObjTag {
1026 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001027 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001028 return
1029 }
1030
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001031 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001032 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001033 cflags := i.exportedFlags()
Colin Crossc99deeb2016-04-11 15:06:20 -07001034 depPaths.Cflags = append(depPaths.Cflags, cflags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001035
1036 if t.reexportFlags {
1037 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
1038 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001039 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001040
1041 if !linkTypeOk(c, cc) {
1042 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1043 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001044 }
1045
Colin Cross635c3b02016-05-18 15:37:25 -07001046 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001047
1048 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001049 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001050 depPtr = &depPaths.SharedLibs
1051 case lateSharedDepTag:
1052 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001053 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001054 depPtr = &depPaths.StaticLibs
1055 case lateStaticDepTag:
1056 depPtr = &depPaths.LateStaticLibs
1057 case wholeStaticDepTag:
1058 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001059 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001060 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001061 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001062 return
1063 }
1064
1065 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1066 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1067 for i := range missingDeps {
1068 missingDeps[i] += postfix
1069 }
1070 ctx.AddMissingDependencies(missingDeps)
1071 }
1072 depPaths.WholeStaticLibObjFiles =
1073 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1074 case objDepTag:
1075 depPtr = &depPaths.ObjFiles
1076 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001077 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001078 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001079 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001080 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001081 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001082 }
1083
1084 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001085 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001086 }
1087 })
1088
1089 return depPaths
1090}
1091
1092func (c *Module) InstallInData() bool {
1093 if c.installer == nil {
1094 return false
1095 }
1096 return c.installer.inData()
1097}
1098
1099// Compiler
1100
1101type baseCompiler struct {
1102 Properties BaseCompilerProperties
1103}
1104
1105var _ compiler = (*baseCompiler)(nil)
1106
1107func (compiler *baseCompiler) props() []interface{} {
1108 return []interface{}{&compiler.Properties}
1109}
1110
Dan Willemsenb40aab62016-04-20 14:21:14 -07001111func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1112
1113func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1114 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1115 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1116
1117 return deps
1118}
Colin Crossca860ac2016-01-04 14:34:37 -08001119
1120// Create a Flags struct that collects the compile flags from global values,
1121// per-target values, module type values, and per-module Blueprints properties
1122func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1123 toolchain := ctx.toolchain()
1124
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001125 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1126 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1127 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1128 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1129
Colin Crossca860ac2016-01-04 14:34:37 -08001130 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1131 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1132 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1133 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1134 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1135
Colin Cross28344522015-04-22 13:07:53 -07001136 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001137 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1138 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001139 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001140 includeDirsToFlags(localIncludeDirs),
1141 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001142
Colin Crossca860ac2016-01-04 14:34:37 -08001143 if !ctx.noDefaultCompilerFlags() {
1144 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001145 flags.GlobalFlags = append(flags.GlobalFlags,
1146 "${commonGlobalIncludes}",
1147 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001148 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001149 }
1150
1151 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001152 "-I" + android.PathForModuleSrc(ctx).String(),
1153 "-I" + android.PathForModuleOut(ctx).String(),
1154 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001155 }...)
1156 }
1157
Colin Crossca860ac2016-01-04 14:34:37 -08001158 instructionSet := compiler.Properties.Instruction_set
1159 if flags.RequiredInstructionSet != "" {
1160 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001161 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001162 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1163 if flags.Clang {
1164 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1165 }
1166 if err != nil {
1167 ctx.ModuleErrorf("%s", err)
1168 }
1169
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001170 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1171
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001172 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001173 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001174
Colin Cross97ba0732015-03-23 17:50:24 -07001175 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001176 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1177 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1178
Colin Cross97ba0732015-03-23 17:50:24 -07001179 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001180 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1181 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001182 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1183 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1184 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001185
1186 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001187 var gccPrefix string
1188 if !ctx.Darwin() {
1189 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1190 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001191
Colin Cross97ba0732015-03-23 17:50:24 -07001192 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1193 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1194 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001195 }
1196
Colin Crossa1ad8d12016-06-01 17:09:44 -07001197 hod := "host"
1198 if ctx.Os().Class == android.Device {
1199 hod = "device"
1200 }
1201
Colin Crossca860ac2016-01-04 14:34:37 -08001202 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001203 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1204
Colin Cross97ba0732015-03-23 17:50:24 -07001205 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001206 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001207 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001208 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001209 toolchain.ClangCflags(),
1210 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001211 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001212
1213 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001214 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001215 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001216 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001217 toolchain.Cflags(),
1218 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001219 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 }
1221
Colin Cross7b66f152015-12-15 16:07:43 -08001222 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1223 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1224 }
1225
Colin Crossf6566ed2015-03-24 11:13:38 -07001226 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001227 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001228 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001229 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001230 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001231 }
1232 }
1233
Colin Cross97ba0732015-03-23 17:50:24 -07001234 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001235
Colin Cross97ba0732015-03-23 17:50:24 -07001236 if flags.Clang {
1237 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001238 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001239 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001240 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001241 }
1242
Colin Crossc4bde762015-11-23 16:11:30 -08001243 if flags.Clang {
1244 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1245 } else {
1246 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001247 }
1248
Colin Crossca860ac2016-01-04 14:34:37 -08001249 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001250 if ctx.Host() && !flags.Clang {
1251 // The host GCC doesn't support C++14 (and is deprecated, so likely
1252 // never will). Build these modules with C++11.
1253 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1254 } else {
1255 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1256 }
1257 }
1258
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001259 // We can enforce some rules more strictly in the code we own. strict
1260 // indicates if this is code that we can be stricter with. If we have
1261 // rules that we want to apply to *our* code (but maybe can't for
1262 // vendor/device specific things), we could extend this to be a ternary
1263 // value.
1264 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001265 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001266 strict = false
1267 }
1268
1269 // Can be used to make some annotations stricter for code we can fix
1270 // (such as when we mark functions as deprecated).
1271 if strict {
1272 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1273 }
1274
Colin Cross3f40fa42015-01-30 17:27:36 -08001275 return flags
1276}
1277
Colin Cross635c3b02016-05-18 15:37:25 -07001278func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001279 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001280 objFiles := compiler.compileObjs(ctx, flags, "",
1281 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1282 deps.GeneratedSources, deps.GeneratedHeaders)
1283
Colin Crossca860ac2016-01-04 14:34:37 -08001284 if ctx.Failed() {
1285 return nil
1286 }
1287
Colin Crossca860ac2016-01-04 14:34:37 -08001288 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001289}
1290
1291// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001292func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1293 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001294
Colin Crossca860ac2016-01-04 14:34:37 -08001295 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001296
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001297 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001298 inputFiles = append(inputFiles, extraSrcs...)
1299 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1300
1301 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001302 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001303
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001304 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001305}
1306
Colin Crossca860ac2016-01-04 14:34:37 -08001307// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1308type baseLinker struct {
1309 Properties BaseLinkerProperties
1310 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001311 VariantIsShared bool `blueprint:"mutated"`
1312 VariantIsStatic bool `blueprint:"mutated"`
1313 VariantIsStaticBinary bool `blueprint:"mutated"`
1314 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001315 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001316}
1317
Dan Willemsend30e6102016-03-30 17:35:50 -07001318func (linker *baseLinker) begin(ctx BaseModuleContext) {
1319 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001320 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001321 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001322 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001323 }
1324}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001325
Colin Crossca860ac2016-01-04 14:34:37 -08001326func (linker *baseLinker) props() []interface{} {
1327 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001328}
1329
Colin Crossca860ac2016-01-04 14:34:37 -08001330func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1331 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1332 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1333 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001334
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001335 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1336 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1337
Dan Willemsena96ff642016-06-07 12:34:45 -07001338 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001339 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001340 }
1341
Colin Crossf6566ed2015-03-24 11:13:38 -07001342 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001343 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001344 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1345 if !Bool(linker.Properties.No_libgcc) {
1346 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001347 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001348
Colin Crossca860ac2016-01-04 14:34:37 -08001349 if !linker.static() {
1350 if linker.Properties.System_shared_libs != nil {
1351 deps.LateSharedLibs = append(deps.LateSharedLibs,
1352 linker.Properties.System_shared_libs...)
1353 } else if !ctx.sdk() {
1354 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1355 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001356 }
Colin Cross577f6e42015-03-27 18:23:34 -07001357
Colin Crossca860ac2016-01-04 14:34:37 -08001358 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001359 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001360 "libc",
1361 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001362 )
1363 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001364 }
1365
Colin Crossca860ac2016-01-04 14:34:37 -08001366 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001367}
1368
Colin Crossca860ac2016-01-04 14:34:37 -08001369func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1370 toolchain := ctx.toolchain()
1371
Colin Crossca860ac2016-01-04 14:34:37 -08001372 if !ctx.noDefaultCompilerFlags() {
1373 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1374 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1375 }
1376
1377 if flags.Clang {
1378 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1379 } else {
1380 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1381 }
1382
1383 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001384 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1385
Colin Crossca860ac2016-01-04 14:34:37 -08001386 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1387 }
1388 }
1389
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001390 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1391
Dan Willemsen00ced762016-05-10 17:31:21 -07001392 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1393
Dan Willemsend30e6102016-03-30 17:35:50 -07001394 if ctx.Host() && !linker.static() {
1395 rpath_prefix := `\$$ORIGIN/`
1396 if ctx.Darwin() {
1397 rpath_prefix = "@loader_path/"
1398 }
1399
Colin Crossc99deeb2016-04-11 15:06:20 -07001400 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001401 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1402 }
1403 }
1404
Dan Willemsene7174922016-03-30 17:33:52 -07001405 if flags.Clang {
1406 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1407 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001408 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1409 }
1410
1411 return flags
1412}
1413
1414func (linker *baseLinker) static() bool {
1415 return linker.dynamicProperties.VariantIsStatic
1416}
1417
1418func (linker *baseLinker) staticBinary() bool {
1419 return linker.dynamicProperties.VariantIsStaticBinary
1420}
1421
1422func (linker *baseLinker) setStatic(static bool) {
1423 linker.dynamicProperties.VariantIsStatic = static
1424}
1425
Colin Cross16b23492016-01-06 14:41:07 -08001426func (linker *baseLinker) isDependencyRoot() bool {
1427 return false
1428}
1429
Colin Crossca860ac2016-01-04 14:34:37 -08001430type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001431 // Returns true if the build options for the module have selected a static or shared build
1432 buildStatic() bool
1433 buildShared() bool
1434
1435 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001436 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001437
Colin Cross18b6dc52015-04-28 13:20:37 -07001438 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001439 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001440
1441 // Returns whether a module is a static binary
1442 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001443
1444 // Returns true for dependency roots (binaries)
1445 // TODO(ccross): also handle dlopenable libraries
1446 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001447}
1448
Colin Crossca860ac2016-01-04 14:34:37 -08001449type baseInstaller struct {
1450 Properties InstallerProperties
1451
1452 dir string
1453 dir64 string
1454 data bool
1455
Colin Cross635c3b02016-05-18 15:37:25 -07001456 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001457}
1458
1459var _ installer = (*baseInstaller)(nil)
1460
1461func (installer *baseInstaller) props() []interface{} {
1462 return []interface{}{&installer.Properties}
1463}
1464
Colin Cross635c3b02016-05-18 15:37:25 -07001465func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001466 subDir := installer.dir
1467 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1468 subDir = installer.dir64
1469 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001470 if !ctx.Host() && !ctx.Arch().Native {
1471 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1472 }
Colin Cross635c3b02016-05-18 15:37:25 -07001473 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001474 installer.path = ctx.InstallFile(dir, file)
1475}
1476
1477func (installer *baseInstaller) inData() bool {
1478 return installer.data
1479}
1480
Colin Cross3f40fa42015-01-30 17:27:36 -08001481//
1482// Combined static+shared libraries
1483//
1484
Colin Cross919281a2016-04-05 16:42:05 -07001485type flagExporter struct {
1486 Properties FlagExporterProperties
1487
1488 flags []string
1489}
1490
1491func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001492 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1493 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001494}
1495
1496func (f *flagExporter) reexportFlags(flags []string) {
1497 f.flags = append(f.flags, flags...)
1498}
1499
1500func (f *flagExporter) exportedFlags() []string {
1501 return f.flags
1502}
1503
1504type exportedFlagsProducer interface {
1505 exportedFlags() []string
1506}
1507
1508var _ exportedFlagsProducer = (*flagExporter)(nil)
1509
Colin Crossca860ac2016-01-04 14:34:37 -08001510type libraryCompiler struct {
1511 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001512
Colin Crossca860ac2016-01-04 14:34:37 -08001513 linker *libraryLinker
1514 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001515
Colin Crossca860ac2016-01-04 14:34:37 -08001516 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001517 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001518}
1519
Colin Crossca860ac2016-01-04 14:34:37 -08001520var _ compiler = (*libraryCompiler)(nil)
1521
1522func (library *libraryCompiler) props() []interface{} {
1523 props := library.baseCompiler.props()
1524 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001525}
1526
Colin Crossca860ac2016-01-04 14:34:37 -08001527func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1528 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001529
Dan Willemsen490fd492015-11-24 17:53:15 -08001530 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1531 // all code is position independent, and then those warnings get promoted to
1532 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001533 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001534 flags.CFlags = append(flags.CFlags, "-fPIC")
1535 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001536
Colin Crossca860ac2016-01-04 14:34:37 -08001537 if library.linker.static() {
1538 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001539 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001540 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001541 }
1542
Colin Crossca860ac2016-01-04 14:34:37 -08001543 return flags
1544}
1545
Colin Cross635c3b02016-05-18 15:37:25 -07001546func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1547 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001548
Dan Willemsenb40aab62016-04-20 14:21:14 -07001549 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001550 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001551
1552 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001553 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001554 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1555 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001556 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001557 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001558 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1559 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001560 }
1561
1562 return objFiles
1563}
1564
1565type libraryLinker struct {
1566 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001567 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001568 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001569
1570 Properties LibraryLinkerProperties
1571
1572 dynamicProperties struct {
1573 BuildStatic bool `blueprint:"mutated"`
1574 BuildShared bool `blueprint:"mutated"`
1575 }
1576
Colin Crossca860ac2016-01-04 14:34:37 -08001577 // If we're used as a whole_static_lib, our missing dependencies need
1578 // to be given
1579 wholeStaticMissingDeps []string
1580
1581 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001582 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001583}
1584
1585var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001586
1587func (library *libraryLinker) props() []interface{} {
1588 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001589 return append(props,
1590 &library.Properties,
1591 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001592 &library.flagExporter.Properties,
1593 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001594}
1595
1596func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1597 flags = library.baseLinker.flags(ctx, flags)
1598
1599 flags.Nocrt = Bool(library.Properties.Nocrt)
1600
1601 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001602 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001603 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1604 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001605 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001606 sharedFlag = "-shared"
1607 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001608 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001609 flags.LdFlags = append(flags.LdFlags,
1610 "-nostdlib",
1611 "-Wl,--gc-sections",
1612 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001613 }
Colin Cross97ba0732015-03-23 17:50:24 -07001614
Colin Cross0af4b842015-04-30 16:36:18 -07001615 if ctx.Darwin() {
1616 flags.LdFlags = append(flags.LdFlags,
1617 "-dynamiclib",
1618 "-single_module",
1619 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001620 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001621 )
1622 } else {
1623 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001624 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001625 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001626 )
1627 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001628 }
Colin Cross97ba0732015-03-23 17:50:24 -07001629
1630 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001631}
1632
Colin Crossca860ac2016-01-04 14:34:37 -08001633func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1634 deps = library.baseLinker.deps(ctx, deps)
1635 if library.static() {
1636 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1637 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1638 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1639 } else {
1640 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1641 if !ctx.sdk() {
1642 deps.CrtBegin = "crtbegin_so"
1643 deps.CrtEnd = "crtend_so"
1644 } else {
1645 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1646 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1647 }
1648 }
1649 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1650 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1651 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1652 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001653
Colin Crossca860ac2016-01-04 14:34:37 -08001654 return deps
1655}
Colin Cross3f40fa42015-01-30 17:27:36 -08001656
Colin Crossca860ac2016-01-04 14:34:37 -08001657func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001658 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001659
Colin Cross635c3b02016-05-18 15:37:25 -07001660 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001661 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001662
Colin Cross635c3b02016-05-18 15:37:25 -07001663 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001664 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001665
Colin Cross0af4b842015-04-30 16:36:18 -07001666 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001667 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001668 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001669 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001670 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001671
Colin Crossca860ac2016-01-04 14:34:37 -08001672 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001673
1674 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001675
1676 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001677}
1678
Colin Crossca860ac2016-01-04 14:34:37 -08001679func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001680 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001681
Colin Cross635c3b02016-05-18 15:37:25 -07001682 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001683
Colin Cross635c3b02016-05-18 15:37:25 -07001684 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1685 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1686 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1687 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001688 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001689 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001690 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001691 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001692 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001693 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001694 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1695 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001696 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001697 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1698 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001699 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001700 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1701 }
1702 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001703 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001704 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1705 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001706 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001707 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001708 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001709 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001710 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001711 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001712 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001713 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001714 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001715 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001716 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001717 }
Colin Crossaee540a2015-07-06 17:48:31 -07001718 }
1719
Colin Cross665dce92016-04-28 14:50:03 -07001720 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001721 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001722 ret := outputFile
1723
1724 builderFlags := flagsToBuilderFlags(flags)
1725
1726 if library.stripper.needsStrip(ctx) {
1727 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001728 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001729 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1730 }
1731
Colin Crossca860ac2016-01-04 14:34:37 -08001732 sharedLibs := deps.SharedLibs
1733 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001734
Colin Crossca860ac2016-01-04 14:34:37 -08001735 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1736 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001737 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001738
Colin Cross665dce92016-04-28 14:50:03 -07001739 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001740}
1741
Colin Crossca860ac2016-01-04 14:34:37 -08001742func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001743 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001744
Colin Crossc99deeb2016-04-11 15:06:20 -07001745 objFiles = append(objFiles, deps.ObjFiles...)
1746
Colin Cross635c3b02016-05-18 15:37:25 -07001747 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001748 if library.static() {
1749 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001750 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001751 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001752 }
1753
Colin Cross919281a2016-04-05 16:42:05 -07001754 library.exportIncludes(ctx, "-I")
1755 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001756
1757 return out
1758}
1759
1760func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001761 return library.dynamicProperties.BuildStatic &&
1762 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001763}
1764
1765func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001766 return library.dynamicProperties.BuildShared &&
1767 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001768}
1769
1770func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1771 return library.wholeStaticMissingDeps
1772}
1773
Colin Crossc99deeb2016-04-11 15:06:20 -07001774func (library *libraryLinker) installable() bool {
1775 return !library.static()
1776}
1777
Colin Crossca860ac2016-01-04 14:34:37 -08001778type libraryInstaller struct {
1779 baseInstaller
1780
Colin Cross30d5f512016-05-03 18:02:42 -07001781 linker *libraryLinker
1782 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001783}
1784
Colin Cross635c3b02016-05-18 15:37:25 -07001785func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001786 if !library.linker.static() {
1787 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001788 }
1789}
1790
Colin Cross30d5f512016-05-03 18:02:42 -07001791func (library *libraryInstaller) inData() bool {
1792 return library.baseInstaller.inData() || library.sanitize.inData()
1793}
1794
Colin Cross635c3b02016-05-18 15:37:25 -07001795func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1796 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001797
Colin Crossca860ac2016-01-04 14:34:37 -08001798 linker := &libraryLinker{}
1799 linker.dynamicProperties.BuildShared = shared
1800 linker.dynamicProperties.BuildStatic = static
1801 module.linker = linker
1802
1803 module.compiler = &libraryCompiler{
1804 linker: linker,
1805 }
1806 module.installer = &libraryInstaller{
1807 baseInstaller: baseInstaller{
1808 dir: "lib",
1809 dir64: "lib64",
1810 },
Colin Cross30d5f512016-05-03 18:02:42 -07001811 linker: linker,
1812 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001813 }
1814
Colin Crossca860ac2016-01-04 14:34:37 -08001815 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001816}
1817
Colin Crossca860ac2016-01-04 14:34:37 -08001818func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001819 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001820 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001821}
1822
Colin Cross3f40fa42015-01-30 17:27:36 -08001823//
1824// Objects (for crt*.o)
1825//
1826
Colin Crossca860ac2016-01-04 14:34:37 -08001827type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001828 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001829}
1830
Colin Crossca860ac2016-01-04 14:34:37 -08001831func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001832 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001833 module.compiler = &baseCompiler{}
1834 module.linker = &objectLinker{}
1835 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001836}
1837
Colin Cross81413472016-04-11 14:37:39 -07001838func (object *objectLinker) props() []interface{} {
1839 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001840}
1841
Colin Crossca860ac2016-01-04 14:34:37 -08001842func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001843
Colin Cross81413472016-04-11 14:37:39 -07001844func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1845 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001846 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001847}
1848
Colin Crossca860ac2016-01-04 14:34:37 -08001849func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001850 if flags.Clang {
1851 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1852 } else {
1853 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1854 }
1855
Colin Crossca860ac2016-01-04 14:34:37 -08001856 return flags
1857}
1858
1859func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001860 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001861
Colin Cross97ba0732015-03-23 17:50:24 -07001862 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001863
Colin Cross635c3b02016-05-18 15:37:25 -07001864 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001865 if len(objFiles) == 1 {
1866 outputFile = objFiles[0]
1867 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001868 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001869 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001870 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001871 }
1872
Colin Cross3f40fa42015-01-30 17:27:36 -08001873 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001874 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001875}
1876
Colin Crossc99deeb2016-04-11 15:06:20 -07001877func (*objectLinker) installable() bool {
1878 return false
1879}
1880
Colin Cross3f40fa42015-01-30 17:27:36 -08001881//
1882// Executables
1883//
1884
Colin Crossca860ac2016-01-04 14:34:37 -08001885type binaryLinker struct {
1886 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001887 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001888
Colin Crossca860ac2016-01-04 14:34:37 -08001889 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001890
Colin Cross635c3b02016-05-18 15:37:25 -07001891 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001892}
1893
Colin Crossca860ac2016-01-04 14:34:37 -08001894var _ linker = (*binaryLinker)(nil)
1895
1896func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001897 return append(binary.baseLinker.props(),
1898 &binary.Properties,
1899 &binary.stripper.StripProperties)
1900
Colin Cross3f40fa42015-01-30 17:27:36 -08001901}
1902
Colin Crossca860ac2016-01-04 14:34:37 -08001903func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001904 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001905}
1906
Colin Crossca860ac2016-01-04 14:34:37 -08001907func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001908 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001909}
1910
Colin Crossca860ac2016-01-04 14:34:37 -08001911func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001912 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001913 if binary.Properties.Stem != "" {
1914 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001915 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001916
Colin Crossca860ac2016-01-04 14:34:37 -08001917 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001918}
1919
Colin Crossca860ac2016-01-04 14:34:37 -08001920func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1921 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001922 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001923 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001924 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001925 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001926 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001927 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001928 }
Colin Crossca860ac2016-01-04 14:34:37 -08001929 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001930 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001931 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001932 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001933 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001934 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001935 }
Colin Crossca860ac2016-01-04 14:34:37 -08001936 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001937 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001938
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001939 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001940 if inList("libc++_static", deps.StaticLibs) {
1941 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001942 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001943 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1944 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1945 // move them to the beginning of deps.LateStaticLibs
1946 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001947 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001948 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001949 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001950 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001951 }
Colin Crossca860ac2016-01-04 14:34:37 -08001952
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001953 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001954 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1955 "from static libs or set static_executable: true")
1956 }
1957 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001958}
1959
Colin Crossc99deeb2016-04-11 15:06:20 -07001960func (*binaryLinker) installable() bool {
1961 return true
1962}
1963
Colin Cross16b23492016-01-06 14:41:07 -08001964func (binary *binaryLinker) isDependencyRoot() bool {
1965 return true
1966}
1967
Colin Cross635c3b02016-05-18 15:37:25 -07001968func NewBinary(hod android.HostOrDeviceSupported) *Module {
1969 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001970 module.compiler = &baseCompiler{}
1971 module.linker = &binaryLinker{}
1972 module.installer = &baseInstaller{
1973 dir: "bin",
1974 }
1975 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001976}
1977
Colin Crossca860ac2016-01-04 14:34:37 -08001978func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001979 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001980 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001981}
1982
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001983func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1984 binary.baseLinker.begin(ctx)
1985
1986 static := Bool(binary.Properties.Static_executable)
1987 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001988 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001989 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1990 static = true
1991 }
1992 } else {
1993 // Static executables are not supported on Darwin or Windows
1994 static = false
1995 }
Colin Cross0af4b842015-04-30 16:36:18 -07001996 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001997 if static {
1998 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001999 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07002000 }
2001}
2002
Colin Crossca860ac2016-01-04 14:34:37 -08002003func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2004 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002005
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002006 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002007 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002008 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002009 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2010 }
2011 }
2012
2013 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2014 // all code is position independent, and then those warnings get promoted to
2015 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002016 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002017 flags.CFlags = append(flags.CFlags, "-fpie")
2018 }
Colin Cross97ba0732015-03-23 17:50:24 -07002019
Colin Crossf6566ed2015-03-24 11:13:38 -07002020 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002021 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002022 // Clang driver needs -static to create static executable.
2023 // However, bionic/linker uses -shared to overwrite.
2024 // Linker for x86 targets does not allow coexistance of -static and -shared,
2025 // so we add -static only if -shared is not used.
2026 if !inList("-shared", flags.LdFlags) {
2027 flags.LdFlags = append(flags.LdFlags, "-static")
2028 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002029
Colin Crossed4cf0b2015-03-26 14:43:45 -07002030 flags.LdFlags = append(flags.LdFlags,
2031 "-nostdlib",
2032 "-Bstatic",
2033 "-Wl,--gc-sections",
2034 )
2035
2036 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002037 if flags.DynamicLinker == "" {
2038 flags.DynamicLinker = "/system/bin/linker"
2039 if flags.Toolchain.Is64Bit() {
2040 flags.DynamicLinker += "64"
2041 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002042 }
2043
2044 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002045 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002046 "-nostdlib",
2047 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002048 "-Wl,--gc-sections",
2049 "-Wl,-z,nocopyreloc",
2050 )
2051 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002052 } else {
2053 if binary.staticBinary() {
2054 flags.LdFlags = append(flags.LdFlags, "-static")
2055 }
2056 if ctx.Darwin() {
2057 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2058 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002059 }
2060
Colin Cross97ba0732015-03-23 17:50:24 -07002061 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002062}
2063
Colin Crossca860ac2016-01-04 14:34:37 -08002064func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002065 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002066
Colin Cross665dce92016-04-28 14:50:03 -07002067 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002068 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002069 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002070 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002071 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002072 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002073
Colin Cross635c3b02016-05-18 15:37:25 -07002074 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002075
Colin Crossca860ac2016-01-04 14:34:37 -08002076 sharedLibs := deps.SharedLibs
2077 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2078
Colin Cross16b23492016-01-06 14:41:07 -08002079 if flags.DynamicLinker != "" {
2080 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2081 }
2082
Colin Cross665dce92016-04-28 14:50:03 -07002083 builderFlags := flagsToBuilderFlags(flags)
2084
2085 if binary.stripper.needsStrip(ctx) {
2086 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002087 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002088 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2089 }
2090
2091 if binary.Properties.Prefix_symbols != "" {
2092 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002093 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002094 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2095 flagsToBuilderFlags(flags), afterPrefixSymbols)
2096 }
2097
Colin Crossca860ac2016-01-04 14:34:37 -08002098 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002099 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002100 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002101
2102 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002103}
Colin Cross3f40fa42015-01-30 17:27:36 -08002104
Colin Cross635c3b02016-05-18 15:37:25 -07002105func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002106 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002107}
2108
Colin Cross665dce92016-04-28 14:50:03 -07002109type stripper struct {
2110 StripProperties StripProperties
2111}
2112
2113func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2114 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2115}
2116
Colin Cross635c3b02016-05-18 15:37:25 -07002117func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002118 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002119 if ctx.Darwin() {
2120 TransformDarwinStrip(ctx, in, out)
2121 } else {
2122 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2123 // TODO(ccross): don't add gnu debuglink for user builds
2124 flags.stripAddGnuDebuglink = true
2125 TransformStrip(ctx, in, out, flags)
2126 }
Colin Cross665dce92016-04-28 14:50:03 -07002127}
2128
Colin Cross635c3b02016-05-18 15:37:25 -07002129func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002130 if m, ok := mctx.Module().(*Module); ok {
2131 if test, ok := m.linker.(*testLinker); ok {
2132 if Bool(test.Properties.Test_per_src) {
2133 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2134 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2135 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2136 }
2137 tests := mctx.CreateLocalVariations(testNames...)
2138 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2139 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2140 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2141 }
Colin Cross6002e052015-09-16 16:00:08 -07002142 }
2143 }
2144 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002145}
2146
Colin Crossca860ac2016-01-04 14:34:37 -08002147type testLinker struct {
2148 binaryLinker
2149 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002150}
2151
Dan Willemsend30e6102016-03-30 17:35:50 -07002152func (test *testLinker) begin(ctx BaseModuleContext) {
2153 test.binaryLinker.begin(ctx)
2154
2155 runpath := "../../lib"
2156 if ctx.toolchain().Is64Bit() {
2157 runpath += "64"
2158 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002159 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002160}
2161
Colin Crossca860ac2016-01-04 14:34:37 -08002162func (test *testLinker) props() []interface{} {
2163 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002164}
2165
Colin Crossca860ac2016-01-04 14:34:37 -08002166func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2167 flags = test.binaryLinker.flags(ctx, flags)
2168
2169 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002170 return flags
2171 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002172
Colin Cross97ba0732015-03-23 17:50:24 -07002173 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002174 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002175 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002176
Colin Crossa1ad8d12016-06-01 17:09:44 -07002177 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002178 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002179 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002180 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002181 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2182 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002183 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002184 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2185 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002186 }
2187 } else {
2188 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002189 }
2190
Colin Cross21b9a242015-03-24 14:15:58 -07002191 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002192}
2193
Colin Crossca860ac2016-01-04 14:34:37 -08002194func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2195 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002196 if ctx.sdk() && ctx.Device() {
2197 switch ctx.selectedStl() {
2198 case "ndk_libc++_shared", "ndk_libc++_static":
2199 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2200 case "ndk_libgnustl_static":
2201 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2202 default:
2203 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2204 }
2205 } else {
2206 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2207 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002208 }
Colin Crossca860ac2016-01-04 14:34:37 -08002209 deps = test.binaryLinker.deps(ctx, deps)
2210 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002211}
2212
Colin Crossca860ac2016-01-04 14:34:37 -08002213type testInstaller struct {
2214 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002215}
2216
Colin Cross635c3b02016-05-18 15:37:25 -07002217func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002218 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2219 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2220 installer.baseInstaller.install(ctx, file)
2221}
2222
Colin Cross635c3b02016-05-18 15:37:25 -07002223func NewTest(hod android.HostOrDeviceSupported) *Module {
2224 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002225 module.compiler = &baseCompiler{}
2226 linker := &testLinker{}
2227 linker.Properties.Gtest = true
2228 module.linker = linker
2229 module.installer = &testInstaller{
2230 baseInstaller: baseInstaller{
2231 dir: "nativetest",
2232 dir64: "nativetest64",
2233 data: true,
2234 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002235 }
Colin Crossca860ac2016-01-04 14:34:37 -08002236 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002237}
2238
Colin Crossca860ac2016-01-04 14:34:37 -08002239func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002240 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002241 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002242}
2243
Colin Crossca860ac2016-01-04 14:34:37 -08002244type benchmarkLinker struct {
2245 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002246}
2247
Colin Crossca860ac2016-01-04 14:34:37 -08002248func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2249 deps = benchmark.binaryLinker.deps(ctx, deps)
2250 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2251 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002252}
2253
Colin Cross635c3b02016-05-18 15:37:25 -07002254func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2255 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002256 module.compiler = &baseCompiler{}
2257 module.linker = &benchmarkLinker{}
2258 module.installer = &baseInstaller{
2259 dir: "nativetest",
2260 dir64: "nativetest64",
2261 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002262 }
Colin Crossca860ac2016-01-04 14:34:37 -08002263 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002264}
2265
Colin Crossca860ac2016-01-04 14:34:37 -08002266func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002267 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002268 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002269}
2270
Colin Cross3f40fa42015-01-30 17:27:36 -08002271//
2272// Static library
2273//
2274
Colin Crossca860ac2016-01-04 14:34:37 -08002275func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002276 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002277 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002278}
2279
2280//
2281// Shared libraries
2282//
2283
Colin Crossca860ac2016-01-04 14:34:37 -08002284func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002285 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002286 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002287}
2288
2289//
2290// Host static library
2291//
2292
Colin Crossca860ac2016-01-04 14:34:37 -08002293func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002294 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002295 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002296}
2297
2298//
2299// Host Shared libraries
2300//
2301
Colin Crossca860ac2016-01-04 14:34:37 -08002302func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002303 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002304 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002305}
2306
2307//
2308// Host Binaries
2309//
2310
Colin Crossca860ac2016-01-04 14:34:37 -08002311func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002312 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002313 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002314}
2315
2316//
Colin Cross1f8f2342015-03-26 16:09:47 -07002317// Host Tests
2318//
2319
Colin Crossca860ac2016-01-04 14:34:37 -08002320func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002321 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002322 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002323}
2324
2325//
Colin Cross2ba19d92015-05-07 15:44:20 -07002326// Host Benchmarks
2327//
2328
Colin Crossca860ac2016-01-04 14:34:37 -08002329func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002330 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002331 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002332}
2333
2334//
Colin Crosscfad1192015-11-02 16:43:11 -08002335// Defaults
2336//
Colin Crossca860ac2016-01-04 14:34:37 -08002337type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002338 android.ModuleBase
2339 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002340}
2341
Colin Cross635c3b02016-05-18 15:37:25 -07002342func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002343}
2344
Colin Crossca860ac2016-01-04 14:34:37 -08002345func defaultsFactory() (blueprint.Module, []interface{}) {
2346 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002347
2348 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002349 &BaseProperties{},
2350 &BaseCompilerProperties{},
2351 &BaseLinkerProperties{},
2352 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002353 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002354 &LibraryLinkerProperties{},
2355 &BinaryLinkerProperties{},
2356 &TestLinkerProperties{},
2357 &UnusedProperties{},
2358 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002359 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002360 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002361 }
2362
Colin Cross635c3b02016-05-18 15:37:25 -07002363 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2364 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002365
Colin Cross635c3b02016-05-18 15:37:25 -07002366 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002367}
2368
2369//
Colin Cross3f40fa42015-01-30 17:27:36 -08002370// Device libraries shipped with gcc
2371//
2372
Colin Crossca860ac2016-01-04 14:34:37 -08002373type toolchainLibraryLinker struct {
2374 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002375}
2376
Colin Crossca860ac2016-01-04 14:34:37 -08002377var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2378
2379func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002380 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002381 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002382}
2383
Colin Crossca860ac2016-01-04 14:34:37 -08002384func (*toolchainLibraryLinker) buildStatic() bool {
2385 return true
2386}
Colin Cross3f40fa42015-01-30 17:27:36 -08002387
Colin Crossca860ac2016-01-04 14:34:37 -08002388func (*toolchainLibraryLinker) buildShared() bool {
2389 return false
2390}
2391
2392func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002393 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002394 module.compiler = &baseCompiler{}
2395 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002396 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002397 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002398}
2399
Colin Crossca860ac2016-01-04 14:34:37 -08002400func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002401 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002402
2403 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002404 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002405
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002406 if flags.Clang {
2407 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2408 }
2409
Colin Crossca860ac2016-01-04 14:34:37 -08002410 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002411
2412 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002413
Colin Crossca860ac2016-01-04 14:34:37 -08002414 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002415}
2416
Colin Crossc99deeb2016-04-11 15:06:20 -07002417func (*toolchainLibraryLinker) installable() bool {
2418 return false
2419}
2420
Dan Albertbe961682015-03-18 23:38:50 -07002421// NDK prebuilt libraries.
2422//
2423// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2424// either (with the exception of the shared STLs, which are installed to the app's directory rather
2425// than to the system image).
2426
Colin Cross635c3b02016-05-18 15:37:25 -07002427func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002428 suffix := ""
2429 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2430 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002431 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002432 suffix = "64"
2433 }
Colin Cross635c3b02016-05-18 15:37:25 -07002434 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002435 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002436}
2437
Colin Cross635c3b02016-05-18 15:37:25 -07002438func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2439 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002440
2441 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2442 // We want to translate to just NAME.EXT
2443 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2444 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002445 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002446}
2447
Colin Crossca860ac2016-01-04 14:34:37 -08002448type ndkPrebuiltObjectLinker struct {
2449 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002450}
2451
Colin Crossca860ac2016-01-04 14:34:37 -08002452func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002453 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002454 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002455}
2456
Colin Crossca860ac2016-01-04 14:34:37 -08002457func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002458 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002459 module.linker = &ndkPrebuiltObjectLinker{}
2460 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002461}
2462
Colin Crossca860ac2016-01-04 14:34:37 -08002463func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002464 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002465 // A null build step, but it sets up the output path.
2466 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2467 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2468 }
2469
Colin Crossca860ac2016-01-04 14:34:37 -08002470 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002471}
2472
Colin Crossca860ac2016-01-04 14:34:37 -08002473type ndkPrebuiltLibraryLinker struct {
2474 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002475}
2476
Colin Crossca860ac2016-01-04 14:34:37 -08002477var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2478var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002479
Colin Crossca860ac2016-01-04 14:34:37 -08002480func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002481 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002482}
2483
Colin Crossca860ac2016-01-04 14:34:37 -08002484func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002485 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002486 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002487}
2488
Colin Crossca860ac2016-01-04 14:34:37 -08002489func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002490 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002491 linker := &ndkPrebuiltLibraryLinker{}
2492 linker.dynamicProperties.BuildShared = true
2493 module.linker = linker
2494 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002495}
2496
Colin Crossca860ac2016-01-04 14:34:37 -08002497func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002498 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002499 // A null build step, but it sets up the output path.
2500 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2501 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2502 }
2503
Colin Cross919281a2016-04-05 16:42:05 -07002504 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002505
Colin Crossca860ac2016-01-04 14:34:37 -08002506 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2507 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002508}
2509
2510// The NDK STLs are slightly different from the prebuilt system libraries:
2511// * Are not specific to each platform version.
2512// * The libraries are not in a predictable location for each STL.
2513
Colin Crossca860ac2016-01-04 14:34:37 -08002514type ndkPrebuiltStlLinker struct {
2515 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002516}
2517
Colin Crossca860ac2016-01-04 14:34:37 -08002518func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002519 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002520 linker := &ndkPrebuiltStlLinker{}
2521 linker.dynamicProperties.BuildShared = true
2522 module.linker = linker
2523 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002524}
2525
Colin Crossca860ac2016-01-04 14:34:37 -08002526func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002527 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002528 linker := &ndkPrebuiltStlLinker{}
2529 linker.dynamicProperties.BuildStatic = true
2530 module.linker = linker
2531 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002532}
2533
Colin Cross635c3b02016-05-18 15:37:25 -07002534func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002535 gccVersion := toolchain.GccVersion()
2536 var libDir string
2537 switch stl {
2538 case "libstlport":
2539 libDir = "cxx-stl/stlport/libs"
2540 case "libc++":
2541 libDir = "cxx-stl/llvm-libc++/libs"
2542 case "libgnustl":
2543 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2544 }
2545
2546 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002547 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002548 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002549 }
2550
2551 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002552 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002553}
2554
Colin Crossca860ac2016-01-04 14:34:37 -08002555func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002556 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002557 // A null build step, but it sets up the output path.
2558 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2559 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2560 }
2561
Colin Cross919281a2016-04-05 16:42:05 -07002562 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002563
2564 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002565 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002566 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002567 libExt = staticLibraryExtension
2568 }
2569
2570 stlName := strings.TrimSuffix(libName, "_shared")
2571 stlName = strings.TrimSuffix(stlName, "_static")
2572 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002573 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002574}
2575
Colin Cross635c3b02016-05-18 15:37:25 -07002576func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002577 if m, ok := mctx.Module().(*Module); ok {
2578 if m.linker != nil {
2579 if linker, ok := m.linker.(baseLinkerInterface); ok {
2580 var modules []blueprint.Module
2581 if linker.buildStatic() && linker.buildShared() {
2582 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002583 static := modules[0].(*Module)
2584 shared := modules[1].(*Module)
2585
2586 static.linker.(baseLinkerInterface).setStatic(true)
2587 shared.linker.(baseLinkerInterface).setStatic(false)
2588
2589 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2590 sharedCompiler := shared.compiler.(*libraryCompiler)
2591 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2592 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2593 // Optimize out compiling common .o files twice for static+shared libraries
2594 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2595 sharedCompiler.baseCompiler.Properties.Srcs = nil
2596 }
2597 }
Colin Crossca860ac2016-01-04 14:34:37 -08002598 } else if linker.buildStatic() {
2599 modules = mctx.CreateLocalVariations("static")
2600 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2601 } else if linker.buildShared() {
2602 modules = mctx.CreateLocalVariations("shared")
2603 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2604 } else {
2605 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2606 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002607 }
2608 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002609 }
2610}
Colin Cross74d1ec02015-04-28 13:30:13 -07002611
2612// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2613// modifies the slice contents in place, and returns a subslice of the original slice
2614func lastUniqueElements(list []string) []string {
2615 totalSkip := 0
2616 for i := len(list) - 1; i >= totalSkip; i-- {
2617 skip := 0
2618 for j := i - 1; j >= totalSkip; j-- {
2619 if list[i] == list[j] {
2620 skip++
2621 } else {
2622 list[j+skip] = list[j]
2623 }
2624 }
2625 totalSkip += skip
2626 }
2627 return list[totalSkip:]
2628}
Colin Cross06a931b2015-10-28 17:23:31 -07002629
2630var Bool = proptools.Bool