blob: 12d9ed713df27c3fa51084de1968957cd4d09f53 [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)
Colin Crossc7a38dc2016-07-12 13:13:09 -070041 soong.RegisterModuleType("cc_test_library", testLibraryFactory)
Colin Crossca860ac2016-01-04 14:34:37 -080042 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
43 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070044
Colin Crossca860ac2016-01-04 14:34:37 -080045 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
47 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
48 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
49 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070050
Colin Crossca860ac2016-01-04 14:34:37 -080051 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
52 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
53 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
54 soong.RegisterModuleType("cc_test_host", testHostFactory)
55 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070056
57 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
58 // the Go initialization order because this package depends on common, so common's init
59 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070060 android.RegisterBottomUpMutator("link", linkageMutator)
Dan Albert914449f2016-06-17 16:45:24 -070061 android.RegisterBottomUpMutator("ndk_api", ndkApiMutator)
Colin Cross635c3b02016-05-18 15:37:25 -070062 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
63 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080064
Colin Cross635c3b02016-05-18 15:37:25 -070065 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
66 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080067
Colin Cross635c3b02016-05-18 15:37:25 -070068 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
69 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Cross463a90e2015-06-17 14:20:06 -070070}
71
Colin Cross3f40fa42015-01-30 17:27:36 -080072var (
Colin Cross635c3b02016-05-18 15:37:25 -070073 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Dan Albert914449f2016-06-17 16:45:24 -070074
75 // These libraries have migrated over to the new ndk_library, which is added
76 // as a variation dependency via depsMutator.
77 ndkMigratedLibs = []string{}
Colin Cross3f40fa42015-01-30 17:27:36 -080078)
79
80// Flags used by lots of devices. Putting them in package static variables will save bytes in
81// build.ninja so they aren't repeated for every file
82var (
83 commonGlobalCflags = []string{
84 "-DANDROID",
85 "-fmessage-length=0",
86 "-W",
87 "-Wall",
88 "-Wno-unused",
89 "-Winit-self",
90 "-Wpointer-arith",
91
92 // COMMON_RELEASE_CFLAGS
93 "-DNDEBUG",
94 "-UDEBUG",
95 }
96
97 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080098 "-fdiagnostics-color",
99
Colin Cross3f40fa42015-01-30 17:27:36 -0800100 // TARGET_ERROR_FLAGS
101 "-Werror=return-type",
102 "-Werror=non-virtual-dtor",
103 "-Werror=address",
104 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -0800105 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -0800106 }
107
108 hostGlobalCflags = []string{}
109
110 commonGlobalCppflags = []string{
111 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700112 }
113
Dan Willemsenbe03f342016-03-03 17:21:04 -0800114 noOverrideGlobalCflags = []string{
115 "-Werror=int-to-pointer-cast",
116 "-Werror=pointer-to-int-cast",
117 }
118
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700119 illegalFlags = []string{
120 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800121 }
Dan Willemsen97704ed2016-07-07 21:40:39 -0700122
123 ndkPrebuiltSharedLibs = []string{
124 "android",
125 "c",
126 "dl",
127 "EGL",
128 "GLESv1_CM",
129 "GLESv2",
130 "GLESv3",
131 "jnigraphics",
132 "log",
133 "mediandk",
134 "m",
135 "OpenMAXAL",
136 "OpenSLES",
137 "stdc++",
138 "vulkan",
139 "z",
140 }
141 ndkPrebuiltSharedLibraries = addPrefix(append([]string(nil), ndkPrebuiltSharedLibs...), "lib")
Colin Cross3f40fa42015-01-30 17:27:36 -0800142)
143
144func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700145 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700146 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
147 }
148
Colin Cross3f40fa42015-01-30 17:27:36 -0800149 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
150 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
151 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800152 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800153
154 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
155
156 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800157 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800158 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800159 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800160 pctx.StaticVariable("hostClangGlobalCflags",
161 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800162 pctx.StaticVariable("noOverrideClangGlobalCflags",
163 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
164
Tim Kilbournf2948142015-03-11 12:03:03 -0700165 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800166 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800167
168 // Everything in this list is a crime against abstraction and dependency tracking.
169 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800170 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700171 []string{
172 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800173 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700174 "hardware/libhardware/include",
175 "hardware/libhardware_legacy/include",
176 "hardware/ril/include",
177 "libnativehelper/include",
178 "frameworks/native/include",
179 "frameworks/native/opengl/include",
180 "frameworks/av/include",
181 "frameworks/base/include",
182 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800183 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
184 // with this, since there is no associated library.
185 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
186 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800187
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700188 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
189 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700190 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700191 return override, nil
192 }
193 return "${clangDefaultBase}", nil
194 })
195 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700196 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700197 return override, nil
198 }
Pirama Arumuga Nainara17442b2016-06-28 11:00:12 -0700199 return "clang-3016494", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700200 })
Colin Cross16b23492016-01-06 14:41:07 -0800201 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
202 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800203}
204
Colin Crossca860ac2016-01-04 14:34:37 -0800205type Deps struct {
206 SharedLibs, LateSharedLibs []string
207 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700208
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700209 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
210
Colin Cross81413472016-04-11 14:37:39 -0700211 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700212
Dan Willemsenb40aab62016-04-20 14:21:14 -0700213 GeneratedSources []string
214 GeneratedHeaders []string
215
Colin Cross97ba0732015-03-23 17:50:24 -0700216 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700217}
218
Colin Crossca860ac2016-01-04 14:34:37 -0800219type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700220 SharedLibs, LateSharedLibs android.Paths
221 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700222
Colin Cross635c3b02016-05-18 15:37:25 -0700223 ObjFiles android.Paths
224 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700225
Colin Cross635c3b02016-05-18 15:37:25 -0700226 GeneratedSources android.Paths
227 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700228
Dan Willemsen76f08272016-07-09 00:14:08 -0700229 Flags, ReexportedFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700230
Colin Cross635c3b02016-05-18 15:37:25 -0700231 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700232}
233
Colin Crossca860ac2016-01-04 14:34:37 -0800234type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700235 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
236 AsFlags []string // Flags that apply to assembly source files
237 CFlags []string // Flags that apply to C and C++ source files
238 ConlyFlags []string // Flags that apply to C source files
239 CppFlags []string // Flags that apply to C++ source files
240 YaccFlags []string // Flags that apply to Yacc source files
241 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800242 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700243
244 Nocrt bool
245 Toolchain Toolchain
246 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800247
248 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800249 DynamicLinker string
250
Colin Cross635c3b02016-05-18 15:37:25 -0700251 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700252}
253
Colin Crossca860ac2016-01-04 14:34:37 -0800254type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700255 // 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 -0700256 Srcs []string `android:"arch_variant"`
257
258 // list of source files that should not be used to build the C/C++ module.
259 // This is most useful in the arch/multilib variants to remove non-common files
260 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700261
262 // list of module-specific flags that will be used for C and C++ compiles.
263 Cflags []string `android:"arch_variant"`
264
265 // list of module-specific flags that will be used for C++ compiles
266 Cppflags []string `android:"arch_variant"`
267
268 // list of module-specific flags that will be used for C compiles
269 Conlyflags []string `android:"arch_variant"`
270
271 // list of module-specific flags that will be used for .S compiles
272 Asflags []string `android:"arch_variant"`
273
Colin Crossca860ac2016-01-04 14:34:37 -0800274 // list of module-specific flags that will be used for C and C++ compiles when
275 // compiling with clang
276 Clang_cflags []string `android:"arch_variant"`
277
278 // list of module-specific flags that will be used for .S compiles when
279 // compiling with clang
280 Clang_asflags []string `android:"arch_variant"`
281
Colin Cross7d5136f2015-05-11 13:39:40 -0700282 // list of module-specific flags that will be used for .y and .yy compiles
283 Yaccflags []string
284
Colin Cross7d5136f2015-05-11 13:39:40 -0700285 // the instruction set architecture to use to compile the C/C++
286 // module.
287 Instruction_set string `android:"arch_variant"`
288
289 // list of directories relative to the root of the source tree that will
290 // be added to the include path using -I.
291 // If possible, don't use this. If adding paths from the current directory use
292 // local_include_dirs, if adding paths from other modules use export_include_dirs in
293 // that module.
294 Include_dirs []string `android:"arch_variant"`
295
296 // list of directories relative to the Blueprints file that will
297 // be added to the include path using -I
298 Local_include_dirs []string `android:"arch_variant"`
299
Dan Willemsenb40aab62016-04-20 14:21:14 -0700300 // list of generated sources to compile. These are the names of gensrcs or
301 // genrule modules.
302 Generated_sources []string `android:"arch_variant"`
303
304 // list of generated headers to add to the include path. These are the names
305 // of genrule modules.
306 Generated_headers []string `android:"arch_variant"`
307
Colin Crossca860ac2016-01-04 14:34:37 -0800308 // pass -frtti instead of -fno-rtti
309 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700310
Colin Crossca860ac2016-01-04 14:34:37 -0800311 Debug, Release struct {
312 // list of module-specific flags that will be used for C and C++ compiles in debug or
313 // release builds
314 Cflags []string `android:"arch_variant"`
315 } `android:"arch_variant"`
316}
Colin Cross7d5136f2015-05-11 13:39:40 -0700317
Colin Crossca860ac2016-01-04 14:34:37 -0800318type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700319 // list of modules whose object files should be linked into this module
320 // in their entirety. For static library modules, all of the .o files from the intermediate
321 // directory of the dependency will be linked into this modules .a file. For a shared library,
322 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700323 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700324
325 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700326 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700327
328 // list of modules that should be dynamically linked into this module.
329 Shared_libs []string `android:"arch_variant"`
330
Colin Crossca860ac2016-01-04 14:34:37 -0800331 // list of module-specific flags that will be used for all link steps
332 Ldflags []string `android:"arch_variant"`
333
334 // don't insert default compiler flags into asflags, cflags,
335 // cppflags, conlyflags, ldflags, or include_dirs
336 No_default_compiler_flags *bool
337
338 // list of system libraries that will be dynamically linked to
339 // shared library and executable modules. If unset, generally defaults to libc
340 // and libm. Set to [] to prevent linking against libc and libm.
341 System_shared_libs []string
342
Colin Cross7d5136f2015-05-11 13:39:40 -0700343 // allow the module to contain undefined symbols. By default,
344 // modules cannot contain undefined symbols that are not satisified by their immediate
345 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
346 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700347 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700348
Dan Willemsend67be222015-09-16 15:19:33 -0700349 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700350 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700351
Colin Cross7d5136f2015-05-11 13:39:40 -0700352 // -l arguments to pass to linker for host-provided shared libraries
353 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700354
355 // list of shared libraries to re-export include directories from. Entries must be
356 // present in shared_libs.
357 Export_shared_lib_headers []string `android:"arch_variant"`
358
359 // list of static libraries to re-export include directories from. Entries must be
360 // present in static_libs.
361 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossa89d2e12016-01-11 12:48:37 -0800362
363 // don't link in crt_begin and crt_end. This flag should only be necessary for
364 // compiling crt or libc.
365 Nocrt *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800366}
Colin Cross7d5136f2015-05-11 13:39:40 -0700367
Colin Crossca860ac2016-01-04 14:34:37 -0800368type LibraryCompilerProperties struct {
369 Static struct {
370 Srcs []string `android:"arch_variant"`
371 Exclude_srcs []string `android:"arch_variant"`
372 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700373 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800374 Shared struct {
375 Srcs []string `android:"arch_variant"`
376 Exclude_srcs []string `android:"arch_variant"`
377 Cflags []string `android:"arch_variant"`
378 } `android:"arch_variant"`
379}
380
Colin Cross919281a2016-04-05 16:42:05 -0700381type FlagExporterProperties struct {
382 // list of directories relative to the Blueprints file that will
383 // be added to the include path using -I for any module that links against this module
384 Export_include_dirs []string `android:"arch_variant"`
385}
386
Colin Crossca860ac2016-01-04 14:34:37 -0800387type LibraryLinkerProperties struct {
388 Static 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 Shared struct {
Dan Willemsenfed4d192016-07-06 21:48:39 -0700395 Enabled *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800396 Whole_static_libs []string `android:"arch_variant"`
397 Static_libs []string `android:"arch_variant"`
398 Shared_libs []string `android:"arch_variant"`
399 } `android:"arch_variant"`
400
401 // local file name to pass to the linker as --version_script
402 Version_script *string `android:"arch_variant"`
403 // local file name to pass to the linker as -unexported_symbols_list
404 Unexported_symbols_list *string `android:"arch_variant"`
405 // local file name to pass to the linker as -force_symbols_not_weak_list
406 Force_symbols_not_weak_list *string `android:"arch_variant"`
407 // local file name to pass to the linker as -force_symbols_weak_list
408 Force_symbols_weak_list *string `android:"arch_variant"`
409
Dan Willemsen648c8ae2016-07-21 16:42:14 -0700410 // rename host libraries to prevent overlap with system installed libraries
411 Unique_host_soname *bool
412
Colin Cross16b23492016-01-06 14:41:07 -0800413 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800414}
415
416type BinaryLinkerProperties struct {
417 // compile executable with -static
Dan Willemsen75ab8082016-07-12 15:36:34 -0700418 Static_executable *bool `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800419
420 // set the name of the output
421 Stem string `android:"arch_variant"`
422
423 // append to the name of the output
424 Suffix string `android:"arch_variant"`
425
426 // if set, add an extra objcopy --prefix-symbols= step
427 Prefix_symbols string
428}
429
430type TestLinkerProperties struct {
431 // if set, build against the gtest library. Defaults to true.
432 Gtest bool
433
434 // Create a separate binary for each source file. Useful when there is
435 // global state that can not be torn down and reset between each test suite.
436 Test_per_src *bool
437}
438
Colin Cross81413472016-04-11 14:37:39 -0700439type ObjectLinkerProperties struct {
440 // names of other cc_object modules to link into this module using partial linking
441 Objs []string `android:"arch_variant"`
442}
443
Colin Crossca860ac2016-01-04 14:34:37 -0800444// Properties used to compile all C or C++ modules
445type BaseProperties struct {
446 // compile module with clang instead of gcc
447 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700448
449 // Minimum sdk version supported when compiling against the ndk
450 Sdk_version string
451
Colin Crossca860ac2016-01-04 14:34:37 -0800452 // don't insert default compiler flags into asflags, cflags,
453 // cppflags, conlyflags, ldflags, or include_dirs
454 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700455
456 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700457 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800458}
459
460type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700461 // install to a subdirectory of the default install path for the module
462 Relative_install_path string
Colin Cross3854a602016-01-11 12:49:11 -0800463
464 // install symlinks to the module
465 Symlinks []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700466}
467
Colin Cross665dce92016-04-28 14:50:03 -0700468type StripProperties struct {
469 Strip struct {
470 None bool
471 Keep_symbols bool
472 }
473}
474
Colin Crossca860ac2016-01-04 14:34:37 -0800475type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700476 Native_coverage *bool
477 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700478 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800479}
480
Colin Crossca860ac2016-01-04 14:34:37 -0800481type ModuleContextIntf interface {
Colin Crossca860ac2016-01-04 14:34:37 -0800482 static() bool
483 staticBinary() bool
484 clang() bool
485 toolchain() Toolchain
486 noDefaultCompilerFlags() bool
487 sdk() bool
488 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700489 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800490}
491
492type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700493 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800494 ModuleContextIntf
495}
496
497type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700498 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800499 ModuleContextIntf
500}
501
Colin Cross76fada02016-07-27 10:31:13 -0700502type CustomizerFlagsContext interface {
503 BaseModuleContext
504 AppendCflags(...string)
505 AppendLdflags(...string)
506 AppendAsflags(...string)
507}
508
Colin Crossca860ac2016-01-04 14:34:37 -0800509type Customizer interface {
Colin Cross76fada02016-07-27 10:31:13 -0700510 Flags(CustomizerFlagsContext)
Colin Crossca860ac2016-01-04 14:34:37 -0800511 Properties() []interface{}
512}
513
514type feature interface {
515 begin(ctx BaseModuleContext)
516 deps(ctx BaseModuleContext, deps Deps) Deps
517 flags(ctx ModuleContext, flags Flags) Flags
518 props() []interface{}
519}
520
521type compiler interface {
522 feature
Colin Cross76fada02016-07-27 10:31:13 -0700523 appendCflags([]string)
524 appendAsflags([]string)
Colin Cross635c3b02016-05-18 15:37:25 -0700525 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800526}
527
528type linker interface {
529 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700530 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Cross76fada02016-07-27 10:31:13 -0700531 appendLdflags([]string)
Colin Crossc99deeb2016-04-11 15:06:20 -0700532 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800533}
534
535type installer interface {
536 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700537 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800538 inData() bool
539}
540
Colin Crossc99deeb2016-04-11 15:06:20 -0700541type dependencyTag struct {
542 blueprint.BaseDependencyTag
543 name string
544 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700545
546 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700547}
548
549var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700550 sharedDepTag = dependencyTag{name: "shared", library: true}
551 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
552 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
553 staticDepTag = dependencyTag{name: "static", library: true}
554 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
555 lateStaticDepTag = dependencyTag{name: "late static", library: true}
556 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
557 genSourceDepTag = dependencyTag{name: "gen source"}
558 genHeaderDepTag = dependencyTag{name: "gen header"}
559 objDepTag = dependencyTag{name: "obj"}
560 crtBeginDepTag = dependencyTag{name: "crtbegin"}
561 crtEndDepTag = dependencyTag{name: "crtend"}
562 reuseObjTag = dependencyTag{name: "reuse objects"}
Dan Albert914449f2016-06-17 16:45:24 -0700563 ndkStubDepTag = dependencyTag{name: "ndk stub", library: true}
564 ndkLateStubDepTag = dependencyTag{name: "ndk late stub", library: true}
Colin Crossc99deeb2016-04-11 15:06:20 -0700565)
566
Colin Crossca860ac2016-01-04 14:34:37 -0800567// Module contains the properties and members used by all C/C++ module types, and implements
568// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
569// to construct the output file. Behavior can be customized with a Customizer interface
570type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700571 android.ModuleBase
572 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700573
Colin Crossca860ac2016-01-04 14:34:37 -0800574 Properties BaseProperties
575 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700576
Colin Crossca860ac2016-01-04 14:34:37 -0800577 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700578 hod android.HostOrDeviceSupported
579 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700580
Colin Crossca860ac2016-01-04 14:34:37 -0800581 // delegates, initialize before calling Init
Colin Cross76fada02016-07-27 10:31:13 -0700582 Customizer Customizer
Colin Crossca860ac2016-01-04 14:34:37 -0800583 features []feature
584 compiler compiler
585 linker linker
586 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700587 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800588 sanitize *sanitize
589
590 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700591
Colin Cross635c3b02016-05-18 15:37:25 -0700592 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800593
594 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700595}
596
Colin Crossca860ac2016-01-04 14:34:37 -0800597func (c *Module) Init() (blueprint.Module, []interface{}) {
598 props := []interface{}{&c.Properties, &c.unused}
Colin Cross76fada02016-07-27 10:31:13 -0700599 if c.Customizer != nil {
600 props = append(props, c.Customizer.Properties()...)
Colin Crossca860ac2016-01-04 14:34:37 -0800601 }
602 if c.compiler != nil {
603 props = append(props, c.compiler.props()...)
604 }
605 if c.linker != nil {
606 props = append(props, c.linker.props()...)
607 }
608 if c.installer != nil {
609 props = append(props, c.installer.props()...)
610 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700611 if c.stl != nil {
612 props = append(props, c.stl.props()...)
613 }
Colin Cross16b23492016-01-06 14:41:07 -0800614 if c.sanitize != nil {
615 props = append(props, c.sanitize.props()...)
616 }
Colin Crossca860ac2016-01-04 14:34:37 -0800617 for _, feature := range c.features {
618 props = append(props, feature.props()...)
619 }
Colin Crossc472d572015-03-17 15:06:21 -0700620
Colin Cross635c3b02016-05-18 15:37:25 -0700621 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700622
Colin Cross635c3b02016-05-18 15:37:25 -0700623 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700624}
625
Colin Crossca860ac2016-01-04 14:34:37 -0800626type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700627 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800628 moduleContextImpl
629}
630
631type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700632 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800633 moduleContextImpl
634}
635
636type moduleContextImpl struct {
637 mod *Module
638 ctx BaseModuleContext
639}
640
Colin Cross76fada02016-07-27 10:31:13 -0700641func (ctx *moduleContextImpl) AppendCflags(flags ...string) {
642 CheckBadCompilerFlags(ctx.ctx, "", flags)
643 ctx.mod.compiler.appendCflags(flags)
644}
645
646func (ctx *moduleContextImpl) AppendAsflags(flags ...string) {
647 CheckBadCompilerFlags(ctx.ctx, "", flags)
648 ctx.mod.compiler.appendAsflags(flags)
649}
650
651func (ctx *moduleContextImpl) AppendLdflags(flags ...string) {
652 CheckBadLinkerFlags(ctx.ctx, "", flags)
653 ctx.mod.linker.appendLdflags(flags)
654}
655
Colin Crossca860ac2016-01-04 14:34:37 -0800656func (ctx *moduleContextImpl) clang() bool {
657 return ctx.mod.clang(ctx.ctx)
658}
659
660func (ctx *moduleContextImpl) toolchain() Toolchain {
661 return ctx.mod.toolchain(ctx.ctx)
662}
663
664func (ctx *moduleContextImpl) static() bool {
665 if ctx.mod.linker == nil {
666 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
667 }
668 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
669 return linker.static()
670 } else {
671 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
672 }
673}
674
675func (ctx *moduleContextImpl) staticBinary() bool {
676 if ctx.mod.linker == nil {
677 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
678 }
679 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
680 return linker.staticBinary()
681 } else {
682 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
683 }
684}
685
686func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
687 return Bool(ctx.mod.Properties.No_default_compiler_flags)
688}
689
690func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700691 if ctx.ctx.Device() {
692 return ctx.mod.Properties.Sdk_version != ""
693 }
694 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800695}
696
697func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700698 if ctx.ctx.Device() {
699 return ctx.mod.Properties.Sdk_version
700 }
701 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800702}
703
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700704func (ctx *moduleContextImpl) selectedStl() string {
705 if stl := ctx.mod.stl; stl != nil {
706 return stl.Properties.SelectedStl
707 }
708 return ""
709}
710
Colin Cross635c3b02016-05-18 15:37:25 -0700711func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800712 return &Module{
713 hod: hod,
714 multilib: multilib,
715 }
716}
717
Colin Cross635c3b02016-05-18 15:37:25 -0700718func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800719 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700720 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800721 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800722 return module
723}
724
Colin Cross635c3b02016-05-18 15:37:25 -0700725func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800726 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700727 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800728 moduleContextImpl: moduleContextImpl{
729 mod: c,
730 },
731 }
732 ctx.ctx = ctx
733
Colin Cross76fada02016-07-27 10:31:13 -0700734 if c.Customizer != nil {
735 c.Customizer.Flags(ctx)
736 }
737
Colin Crossca860ac2016-01-04 14:34:37 -0800738 flags := Flags{
739 Toolchain: c.toolchain(ctx),
740 Clang: c.clang(ctx),
741 }
Colin Crossca860ac2016-01-04 14:34:37 -0800742 if c.compiler != nil {
743 flags = c.compiler.flags(ctx, flags)
744 }
745 if c.linker != nil {
746 flags = c.linker.flags(ctx, flags)
747 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700748 if c.stl != nil {
749 flags = c.stl.flags(ctx, flags)
750 }
Colin Cross16b23492016-01-06 14:41:07 -0800751 if c.sanitize != nil {
752 flags = c.sanitize.flags(ctx, flags)
753 }
Colin Crossca860ac2016-01-04 14:34:37 -0800754 for _, feature := range c.features {
755 flags = feature.flags(ctx, flags)
756 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800757 if ctx.Failed() {
758 return
759 }
760
Colin Crossca860ac2016-01-04 14:34:37 -0800761 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
762 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
763 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800764
Colin Crossca860ac2016-01-04 14:34:37 -0800765 // Optimization to reduce size of build.ninja
766 // Replace the long list of flags for each file with a module-local variable
767 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
768 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
769 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
770 flags.CFlags = []string{"$cflags"}
771 flags.CppFlags = []string{"$cppflags"}
772 flags.AsFlags = []string{"$asflags"}
773
Colin Crossc99deeb2016-04-11 15:06:20 -0700774 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800775 if ctx.Failed() {
776 return
777 }
778
Dan Willemsen76f08272016-07-09 00:14:08 -0700779 flags.GlobalFlags = append(flags.GlobalFlags, deps.Flags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700780
Colin Cross635c3b02016-05-18 15:37:25 -0700781 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800782 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700783 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800784 if ctx.Failed() {
785 return
786 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800787 }
788
Colin Crossca860ac2016-01-04 14:34:37 -0800789 if c.linker != nil {
790 outputFile := c.linker.link(ctx, flags, deps, objFiles)
791 if ctx.Failed() {
792 return
793 }
Colin Cross635c3b02016-05-18 15:37:25 -0700794 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700795
Colin Crossc99deeb2016-04-11 15:06:20 -0700796 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800797 c.installer.install(ctx, outputFile)
798 if ctx.Failed() {
799 return
800 }
801 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700802 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800803}
804
Colin Crossca860ac2016-01-04 14:34:37 -0800805func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
806 if c.cachedToolchain == nil {
807 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700808 os := ctx.Os()
809 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800810 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700811 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800812 return nil
813 }
814 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800815 }
Colin Crossca860ac2016-01-04 14:34:37 -0800816 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800817}
818
Colin Crossca860ac2016-01-04 14:34:37 -0800819func (c *Module) begin(ctx BaseModuleContext) {
820 if c.compiler != nil {
821 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700822 }
Colin Crossca860ac2016-01-04 14:34:37 -0800823 if c.linker != nil {
824 c.linker.begin(ctx)
825 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700826 if c.stl != nil {
827 c.stl.begin(ctx)
828 }
Colin Cross16b23492016-01-06 14:41:07 -0800829 if c.sanitize != nil {
830 c.sanitize.begin(ctx)
831 }
Colin Crossca860ac2016-01-04 14:34:37 -0800832 for _, feature := range c.features {
833 feature.begin(ctx)
834 }
835}
836
Colin Crossc99deeb2016-04-11 15:06:20 -0700837func (c *Module) deps(ctx BaseModuleContext) Deps {
838 deps := Deps{}
839
840 if c.compiler != nil {
841 deps = c.compiler.deps(ctx, deps)
842 }
843 if c.linker != nil {
844 deps = c.linker.deps(ctx, deps)
845 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700846 if c.stl != nil {
847 deps = c.stl.deps(ctx, deps)
848 }
Colin Cross16b23492016-01-06 14:41:07 -0800849 if c.sanitize != nil {
850 deps = c.sanitize.deps(ctx, deps)
851 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700852 for _, feature := range c.features {
853 deps = feature.deps(ctx, deps)
854 }
855
856 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
857 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
858 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
859 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
860 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
861
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700862 for _, lib := range deps.ReexportSharedLibHeaders {
863 if !inList(lib, deps.SharedLibs) {
864 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
865 }
866 }
867
868 for _, lib := range deps.ReexportStaticLibHeaders {
869 if !inList(lib, deps.StaticLibs) {
870 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
871 }
872 }
873
Colin Crossc99deeb2016-04-11 15:06:20 -0700874 return deps
875}
876
Colin Cross635c3b02016-05-18 15:37:25 -0700877func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800878 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700879 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800880 moduleContextImpl: moduleContextImpl{
881 mod: c,
882 },
883 }
884 ctx.ctx = ctx
885
Colin Crossca860ac2016-01-04 14:34:37 -0800886 c.begin(ctx)
887
Colin Crossc99deeb2016-04-11 15:06:20 -0700888 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800889
Colin Crossb5bc4b42016-07-11 16:11:59 -0700890 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.SharedLibs...)
891 c.Properties.AndroidMkSharedLibs = append(c.Properties.AndroidMkSharedLibs, deps.LateSharedLibs...)
Dan Willemsen72d39932016-07-08 23:23:48 -0700892
Dan Albert914449f2016-06-17 16:45:24 -0700893 variantNdkLibs := []string{}
894 variantLateNdkLibs := []string{}
Dan Willemsen72d39932016-07-08 23:23:48 -0700895 if ctx.sdk() {
Dan Albert914449f2016-06-17 16:45:24 -0700896 version := ctx.sdkVersion()
Dan Willemsen72d39932016-07-08 23:23:48 -0700897
Dan Albert914449f2016-06-17 16:45:24 -0700898 // Rewrites the names of shared libraries into the names of the NDK
899 // libraries where appropriate. This returns two slices.
900 //
901 // The first is a list of non-variant shared libraries (either rewritten
902 // NDK libraries to the modules in prebuilts/ndk, or not rewritten
903 // because they are not NDK libraries).
904 //
905 // The second is a list of ndk_library modules. These need to be
906 // separated because they are a variation dependency and must be added
907 // in a different manner.
908 rewriteNdkLibs := func(list []string) ([]string, []string) {
909 variantLibs := []string{}
910 nonvariantLibs := []string{}
911 for _, entry := range list {
Dan Willemsen72d39932016-07-08 23:23:48 -0700912 if inList(entry, ndkPrebuiltSharedLibraries) {
Dan Albert914449f2016-06-17 16:45:24 -0700913 if !inList(entry, ndkMigratedLibs) {
914 nonvariantLibs = append(nonvariantLibs, entry+".ndk."+version)
915 } else {
916 variantLibs = append(variantLibs, entry+ndkLibrarySuffix)
917 }
918 } else {
919 nonvariantLibs = append(variantLibs, entry)
Dan Willemsen72d39932016-07-08 23:23:48 -0700920 }
921 }
Dan Albert914449f2016-06-17 16:45:24 -0700922 return nonvariantLibs, variantLibs
Dan Willemsen72d39932016-07-08 23:23:48 -0700923 }
924
Dan Albert914449f2016-06-17 16:45:24 -0700925 deps.SharedLibs, variantNdkLibs = rewriteNdkLibs(deps.SharedLibs)
926 deps.LateSharedLibs, variantLateNdkLibs = rewriteNdkLibs(deps.LateSharedLibs)
Dan Willemsen72d39932016-07-08 23:23:48 -0700927 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700928
929 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
930 deps.WholeStaticLibs...)
931
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700932 for _, lib := range deps.StaticLibs {
933 depTag := staticDepTag
934 if inList(lib, deps.ReexportStaticLibHeaders) {
935 depTag = staticExportDepTag
936 }
Colin Cross15a0d462016-07-14 14:49:58 -0700937 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700938 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700939
940 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
941 deps.LateStaticLibs...)
942
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700943 for _, lib := range deps.SharedLibs {
944 depTag := sharedDepTag
945 if inList(lib, deps.ReexportSharedLibHeaders) {
946 depTag = sharedExportDepTag
947 }
Colin Cross15a0d462016-07-14 14:49:58 -0700948 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag, lib)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700949 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700950
951 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
952 deps.LateSharedLibs...)
953
Colin Cross68861832016-07-08 10:41:41 -0700954 actx.AddDependency(c, genSourceDepTag, deps.GeneratedSources...)
955 actx.AddDependency(c, genHeaderDepTag, deps.GeneratedHeaders...)
Dan Willemsenb40aab62016-04-20 14:21:14 -0700956
Colin Cross68861832016-07-08 10:41:41 -0700957 actx.AddDependency(c, objDepTag, deps.ObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700958
959 if deps.CrtBegin != "" {
Colin Cross68861832016-07-08 10:41:41 -0700960 actx.AddDependency(c, crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800961 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700962 if deps.CrtEnd != "" {
Colin Cross68861832016-07-08 10:41:41 -0700963 actx.AddDependency(c, crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700964 }
Dan Albert914449f2016-06-17 16:45:24 -0700965
966 version := ctx.sdkVersion()
967 actx.AddVariationDependencies([]blueprint.Variation{
968 {"ndk_api", version}, {"link", "shared"}}, ndkStubDepTag, variantNdkLibs...)
969 actx.AddVariationDependencies([]blueprint.Variation{
970 {"ndk_api", version}, {"link", "shared"}}, ndkLateStubDepTag, variantLateNdkLibs...)
Colin Cross6362e272015-10-29 15:25:03 -0700971}
Colin Cross21b9a242015-03-24 14:15:58 -0700972
Colin Cross635c3b02016-05-18 15:37:25 -0700973func depsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsen3f32f032016-07-11 14:36:48 -0700974 if c, ok := ctx.Module().(*Module); ok && c.Enabled() {
Colin Cross6362e272015-10-29 15:25:03 -0700975 c.depsMutator(ctx)
976 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800977}
978
Colin Crossca860ac2016-01-04 14:34:37 -0800979func (c *Module) clang(ctx BaseModuleContext) bool {
980 clang := Bool(c.Properties.Clang)
981
982 if c.Properties.Clang == nil {
983 if ctx.Host() {
984 clang = true
985 }
986
987 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
988 clang = true
989 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800990 }
Colin Cross28344522015-04-22 13:07:53 -0700991
Colin Crossca860ac2016-01-04 14:34:37 -0800992 if !c.toolchain(ctx).ClangSupported() {
993 clang = false
994 }
995
996 return clang
997}
998
Colin Crossc99deeb2016-04-11 15:06:20 -0700999// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -07001000func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -08001001 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -08001002
Dan Willemsena96ff642016-06-07 12:34:45 -07001003 // Whether a module can link to another module, taking into
1004 // account NDK linking.
1005 linkTypeOk := func(from, to *Module) bool {
1006 if from.Target().Os != android.Android {
1007 // Host code is not restricted
1008 return true
1009 }
1010 if from.Properties.Sdk_version == "" {
1011 // Platform code can link to anything
1012 return true
1013 }
1014 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
1015 // These are always allowed
1016 return true
1017 }
1018 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
1019 // These are allowed, but don't set sdk_version
1020 return true
1021 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -07001022 if _, ok := to.linker.(*ndkPrebuiltStlLinker); ok {
1023 // These are allowed, but don't set sdk_version
1024 return true
1025 }
Dan Albert914449f2016-06-17 16:45:24 -07001026 if _, ok := to.linker.(*stubLinker); ok {
1027 // These aren't real libraries, but are the stub shared libraries that are included in
1028 // the NDK.
1029 return true
1030 }
Dan Willemsen3c316bc2016-07-07 20:41:36 -07001031 return to.Properties.Sdk_version != ""
Dan Willemsena96ff642016-06-07 12:34:45 -07001032 }
1033
Colin Crossc99deeb2016-04-11 15:06:20 -07001034 ctx.VisitDirectDeps(func(m blueprint.Module) {
1035 name := ctx.OtherModuleName(m)
1036 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -08001037
Colin Cross635c3b02016-05-18 15:37:25 -07001038 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -07001039 if a == nil {
1040 ctx.ModuleErrorf("module %q not an android module", name)
1041 return
Colin Crossca860ac2016-01-04 14:34:37 -08001042 }
Colin Crossca860ac2016-01-04 14:34:37 -08001043
Dan Willemsena96ff642016-06-07 12:34:45 -07001044 cc, _ := m.(*Module)
1045 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -07001046 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -07001047 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -07001048 case genSourceDepTag:
1049 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
1050 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
1051 genRule.GeneratedSourceFiles()...)
1052 } else {
1053 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
1054 }
1055 case genHeaderDepTag:
1056 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
1057 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
1058 genRule.GeneratedSourceFiles()...)
Dan Willemsen76f08272016-07-09 00:14:08 -07001059 depPaths.Flags = append(depPaths.Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07001060 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -07001061 } else {
1062 ctx.ModuleErrorf("module %q is not a genrule", name)
1063 }
1064 default:
Colin Crossc99deeb2016-04-11 15:06:20 -07001065 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -08001066 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001067 return
1068 }
1069
1070 if !a.Enabled() {
1071 ctx.ModuleErrorf("depends on disabled module %q", name)
1072 return
1073 }
1074
Colin Crossa1ad8d12016-06-01 17:09:44 -07001075 if a.Target().Os != ctx.Os() {
1076 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
1077 return
1078 }
1079
1080 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
1081 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001082 return
1083 }
1084
Dan Willemsena96ff642016-06-07 12:34:45 -07001085 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001086 ctx.ModuleErrorf("module %q missing output file", name)
1087 return
1088 }
1089
1090 if tag == reuseObjTag {
1091 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -07001092 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001093 return
1094 }
1095
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001096 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -07001097 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen76f08272016-07-09 00:14:08 -07001098 flags := i.exportedFlags()
1099 depPaths.Flags = append(depPaths.Flags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001100
1101 if t.reexportFlags {
Dan Willemsen76f08272016-07-09 00:14:08 -07001102 depPaths.ReexportedFlags = append(depPaths.ReexportedFlags, flags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001103 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001104 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001105
1106 if !linkTypeOk(c, cc) {
1107 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1108 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001109 }
1110
Colin Cross635c3b02016-05-18 15:37:25 -07001111 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001112
1113 switch tag {
Dan Albert914449f2016-06-17 16:45:24 -07001114 case ndkStubDepTag, sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001115 depPtr = &depPaths.SharedLibs
Dan Albert914449f2016-06-17 16:45:24 -07001116 case lateSharedDepTag, ndkLateStubDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001117 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001118 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001119 depPtr = &depPaths.StaticLibs
1120 case lateStaticDepTag:
1121 depPtr = &depPaths.LateStaticLibs
1122 case wholeStaticDepTag:
1123 depPtr = &depPaths.WholeStaticLibs
Colin Crossc7a38dc2016-07-12 13:13:09 -07001124 staticLib, _ := cc.linker.(libraryInterface)
Colin Crossc99deeb2016-04-11 15:06:20 -07001125 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001126 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001127 return
1128 }
1129
1130 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1131 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1132 for i := range missingDeps {
1133 missingDeps[i] += postfix
1134 }
1135 ctx.AddMissingDependencies(missingDeps)
1136 }
1137 depPaths.WholeStaticLibObjFiles =
Colin Crossc7a38dc2016-07-12 13:13:09 -07001138 append(depPaths.WholeStaticLibObjFiles, staticLib.objs()...)
Colin Crossc99deeb2016-04-11 15:06:20 -07001139 case objDepTag:
1140 depPtr = &depPaths.ObjFiles
1141 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001142 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001143 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001144 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001145 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001146 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001147 }
1148
1149 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001150 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001151 }
1152 })
1153
1154 return depPaths
1155}
1156
1157func (c *Module) InstallInData() bool {
1158 if c.installer == nil {
1159 return false
1160 }
1161 return c.installer.inData()
1162}
1163
1164// Compiler
1165
1166type baseCompiler struct {
1167 Properties BaseCompilerProperties
1168}
1169
1170var _ compiler = (*baseCompiler)(nil)
1171
Colin Cross76fada02016-07-27 10:31:13 -07001172func (compiler *baseCompiler) appendCflags(flags []string) {
1173 compiler.Properties.Cflags = append(compiler.Properties.Cflags, flags...)
1174}
1175
1176func (compiler *baseCompiler) appendAsflags(flags []string) {
1177 compiler.Properties.Asflags = append(compiler.Properties.Asflags, flags...)
1178}
1179
Colin Crossca860ac2016-01-04 14:34:37 -08001180func (compiler *baseCompiler) props() []interface{} {
1181 return []interface{}{&compiler.Properties}
1182}
1183
Dan Willemsenb40aab62016-04-20 14:21:14 -07001184func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1185
1186func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1187 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1188 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1189
1190 return deps
1191}
Colin Crossca860ac2016-01-04 14:34:37 -08001192
1193// Create a Flags struct that collects the compile flags from global values,
1194// per-target values, module type values, and per-module Blueprints properties
1195func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1196 toolchain := ctx.toolchain()
1197
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001198 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1199 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1200 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1201 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1202
Colin Crossca860ac2016-01-04 14:34:37 -08001203 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1204 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1205 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1206 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1207 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1208
Colin Cross28344522015-04-22 13:07:53 -07001209 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001210 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1211 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001212 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001213 includeDirsToFlags(localIncludeDirs),
1214 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001215
Colin Crossca860ac2016-01-04 14:34:37 -08001216 if !ctx.noDefaultCompilerFlags() {
1217 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001218 flags.GlobalFlags = append(flags.GlobalFlags,
1219 "${commonGlobalIncludes}",
1220 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001221 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001222 }
1223
1224 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001225 "-I" + android.PathForModuleSrc(ctx).String(),
1226 "-I" + android.PathForModuleOut(ctx).String(),
1227 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001228 }...)
1229 }
1230
Dan Albert914449f2016-06-17 16:45:24 -07001231 if ctx.sdk() {
1232 // The NDK headers are installed to a common sysroot. While a more
1233 // typical Soong approach would be to only make the headers for the
1234 // library you're using available, we're trying to emulate the NDK
1235 // behavior here, and the NDK always has all the NDK headers available.
1236 flags.GlobalFlags = append(flags.GlobalFlags,
1237 "-isystem "+getCurrentIncludePath(ctx).String(),
1238 "-isystem "+getCurrentIncludePath(ctx).Join(ctx, toolchain.ClangTriple()).String())
1239
1240 // Traditionally this has come from android/api-level.h, but with the
1241 // libc headers unified it must be set by the build system since we
1242 // don't have per-API level copies of that header now.
1243 flags.GlobalFlags = append(flags.GlobalFlags,
1244 "-D__ANDROID_API__="+ctx.sdkVersion())
1245
1246 // Until the full NDK has been migrated to using ndk_headers, we still
1247 // need to add the legacy sysroot includes to get the full set of
1248 // headers.
1249 legacyIncludes := fmt.Sprintf(
1250 "prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/include",
1251 ctx.sdkVersion(), ctx.Arch().ArchType.String())
1252 flags.GlobalFlags = append(flags.GlobalFlags, "-isystem "+legacyIncludes)
1253 }
1254
Colin Crossca860ac2016-01-04 14:34:37 -08001255 instructionSet := compiler.Properties.Instruction_set
1256 if flags.RequiredInstructionSet != "" {
1257 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001258 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001259 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1260 if flags.Clang {
1261 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1262 }
1263 if err != nil {
1264 ctx.ModuleErrorf("%s", err)
1265 }
1266
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001267 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1268
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001269 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001270 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001271
Colin Cross97ba0732015-03-23 17:50:24 -07001272 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001273 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1274 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1275
Colin Cross97ba0732015-03-23 17:50:24 -07001276 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001277 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1278 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001279 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1280 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1281 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001282
1283 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001284 var gccPrefix string
1285 if !ctx.Darwin() {
1286 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1287 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001288
Colin Cross97ba0732015-03-23 17:50:24 -07001289 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1290 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1291 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001292 }
1293
Colin Crossa1ad8d12016-06-01 17:09:44 -07001294 hod := "host"
1295 if ctx.Os().Class == android.Device {
1296 hod = "device"
1297 }
1298
Colin Crossca860ac2016-01-04 14:34:37 -08001299 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001300 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1301
Colin Cross97ba0732015-03-23 17:50:24 -07001302 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001303 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001304 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001305 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001306 toolchain.ClangCflags(),
1307 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001308 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001309
1310 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001311 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001312 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001313 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001314 toolchain.Cflags(),
1315 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001316 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001317 }
1318
Colin Cross7b66f152015-12-15 16:07:43 -08001319 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1320 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1321 }
1322
Colin Crossf6566ed2015-03-24 11:13:38 -07001323 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001324 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001325 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001326 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001327 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001328 }
1329 }
1330
Colin Cross97ba0732015-03-23 17:50:24 -07001331 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001332
Colin Cross97ba0732015-03-23 17:50:24 -07001333 if flags.Clang {
1334 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001335 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001336 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001337 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001338 }
1339
Colin Crossc4bde762015-11-23 16:11:30 -08001340 if flags.Clang {
1341 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1342 } else {
1343 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001344 }
1345
Colin Crossca860ac2016-01-04 14:34:37 -08001346 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001347 if ctx.Host() && !flags.Clang {
1348 // The host GCC doesn't support C++14 (and is deprecated, so likely
1349 // never will). Build these modules with C++11.
1350 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1351 } else {
1352 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1353 }
1354 }
1355
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001356 // We can enforce some rules more strictly in the code we own. strict
1357 // indicates if this is code that we can be stricter with. If we have
1358 // rules that we want to apply to *our* code (but maybe can't for
1359 // vendor/device specific things), we could extend this to be a ternary
1360 // value.
1361 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001362 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001363 strict = false
1364 }
1365
1366 // Can be used to make some annotations stricter for code we can fix
1367 // (such as when we mark functions as deprecated).
1368 if strict {
1369 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1370 }
1371
Colin Cross3f40fa42015-01-30 17:27:36 -08001372 return flags
1373}
1374
Dan Albert914449f2016-06-17 16:45:24 -07001375func ndkPathDeps(ctx ModuleContext) android.Paths {
1376 if ctx.sdk() {
1377 // The NDK sysroot timestamp file depends on all the NDK sysroot files
1378 // (headers and libraries).
1379 return android.Paths{getNdkSysrootTimestampFile(ctx)}
1380 }
1381 return nil
1382}
1383
Colin Cross635c3b02016-05-18 15:37:25 -07001384func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Dan Albert914449f2016-06-17 16:45:24 -07001385 pathDeps := deps.GeneratedHeaders
1386 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001387 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001388 objFiles := compiler.compileObjs(ctx, flags, "",
1389 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
Dan Albert914449f2016-06-17 16:45:24 -07001390 deps.GeneratedSources, pathDeps)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001391
Colin Crossca860ac2016-01-04 14:34:37 -08001392 if ctx.Failed() {
1393 return nil
1394 }
1395
Colin Crossca860ac2016-01-04 14:34:37 -08001396 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001397}
1398
1399// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001400func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1401 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001402
Colin Crossca860ac2016-01-04 14:34:37 -08001403 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001404
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001405 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001406 inputFiles = append(inputFiles, extraSrcs...)
1407 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1408
1409 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001410 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001411
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001412 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001413}
1414
Colin Crossca860ac2016-01-04 14:34:37 -08001415// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1416type baseLinker struct {
1417 Properties BaseLinkerProperties
1418 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001419 VariantIsShared bool `blueprint:"mutated"`
1420 VariantIsStatic bool `blueprint:"mutated"`
1421 VariantIsStaticBinary bool `blueprint:"mutated"`
1422 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001423 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001424}
1425
Colin Cross76fada02016-07-27 10:31:13 -07001426func (linker *baseLinker) appendLdflags(flags []string) {
1427 linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
1428}
1429
Dan Willemsend30e6102016-03-30 17:35:50 -07001430func (linker *baseLinker) begin(ctx BaseModuleContext) {
1431 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001432 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001433 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001434 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001435 }
1436}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001437
Colin Crossca860ac2016-01-04 14:34:37 -08001438func (linker *baseLinker) props() []interface{} {
1439 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001440}
1441
Colin Crossca860ac2016-01-04 14:34:37 -08001442func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1443 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1444 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1445 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001446
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001447 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1448 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1449
Dan Willemsena96ff642016-06-07 12:34:45 -07001450 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Stephen Hines10347862016-07-18 15:54:54 -07001451 deps.LateStaticLibs = append(deps.LateStaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001452 }
1453
Colin Crossf6566ed2015-03-24 11:13:38 -07001454 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001455 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001456 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1457 if !Bool(linker.Properties.No_libgcc) {
1458 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001459 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001460
Colin Crossca860ac2016-01-04 14:34:37 -08001461 if !linker.static() {
1462 if linker.Properties.System_shared_libs != nil {
1463 deps.LateSharedLibs = append(deps.LateSharedLibs,
1464 linker.Properties.System_shared_libs...)
1465 } else if !ctx.sdk() {
1466 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1467 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001468 }
Colin Cross577f6e42015-03-27 18:23:34 -07001469
Colin Crossca860ac2016-01-04 14:34:37 -08001470 if ctx.sdk() {
Colin Crossca860ac2016-01-04 14:34:37 -08001471 deps.SharedLibs = append(deps.SharedLibs,
Dan Willemsen97704ed2016-07-07 21:40:39 -07001472 "libc",
1473 "libm",
Colin Cross577f6e42015-03-27 18:23:34 -07001474 )
1475 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001476 }
1477
Colin Crossca860ac2016-01-04 14:34:37 -08001478 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001479}
1480
Colin Crossca860ac2016-01-04 14:34:37 -08001481func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1482 toolchain := ctx.toolchain()
1483
Colin Crossa89d2e12016-01-11 12:48:37 -08001484 flags.Nocrt = Bool(linker.Properties.Nocrt)
1485
Colin Crossca860ac2016-01-04 14:34:37 -08001486 if !ctx.noDefaultCompilerFlags() {
1487 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1488 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1489 }
1490
1491 if flags.Clang {
1492 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1493 } else {
1494 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1495 }
1496
1497 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001498 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1499
Colin Crossca860ac2016-01-04 14:34:37 -08001500 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1501 }
1502 }
1503
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001504 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1505
Dan Willemsen00ced762016-05-10 17:31:21 -07001506 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1507
Dan Willemsend30e6102016-03-30 17:35:50 -07001508 if ctx.Host() && !linker.static() {
1509 rpath_prefix := `\$$ORIGIN/`
1510 if ctx.Darwin() {
1511 rpath_prefix = "@loader_path/"
1512 }
1513
Colin Crossc99deeb2016-04-11 15:06:20 -07001514 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001515 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1516 }
1517 }
1518
Dan Willemsene7174922016-03-30 17:33:52 -07001519 if flags.Clang {
1520 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1521 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001522 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1523 }
1524
1525 return flags
1526}
1527
1528func (linker *baseLinker) static() bool {
1529 return linker.dynamicProperties.VariantIsStatic
1530}
1531
1532func (linker *baseLinker) staticBinary() bool {
1533 return linker.dynamicProperties.VariantIsStaticBinary
1534}
1535
1536func (linker *baseLinker) setStatic(static bool) {
1537 linker.dynamicProperties.VariantIsStatic = static
1538}
1539
Colin Cross16b23492016-01-06 14:41:07 -08001540func (linker *baseLinker) isDependencyRoot() bool {
1541 return false
1542}
1543
Colin Crossca860ac2016-01-04 14:34:37 -08001544type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001545 // Returns true if the build options for the module have selected a static or shared build
1546 buildStatic() bool
1547 buildShared() bool
1548
1549 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001550 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001551
Colin Cross18b6dc52015-04-28 13:20:37 -07001552 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001553 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001554
1555 // Returns whether a module is a static binary
1556 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001557
1558 // Returns true for dependency roots (binaries)
1559 // TODO(ccross): also handle dlopenable libraries
1560 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001561}
1562
Colin Crossca860ac2016-01-04 14:34:37 -08001563type baseInstaller struct {
1564 Properties InstallerProperties
1565
1566 dir string
1567 dir64 string
1568 data bool
1569
Colin Cross635c3b02016-05-18 15:37:25 -07001570 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001571}
1572
1573var _ installer = (*baseInstaller)(nil)
1574
1575func (installer *baseInstaller) props() []interface{} {
1576 return []interface{}{&installer.Properties}
1577}
1578
Colin Cross635c3b02016-05-18 15:37:25 -07001579func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001580 subDir := installer.dir
1581 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1582 subDir = installer.dir64
1583 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001584 if !ctx.Host() && !ctx.Arch().Native {
1585 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1586 }
Colin Cross635c3b02016-05-18 15:37:25 -07001587 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001588 installer.path = ctx.InstallFile(dir, file)
Colin Cross3854a602016-01-11 12:49:11 -08001589 for _, symlink := range installer.Properties.Symlinks {
1590 ctx.InstallSymlink(dir, symlink, installer.path)
1591 }
Colin Crossca860ac2016-01-04 14:34:37 -08001592}
1593
1594func (installer *baseInstaller) inData() bool {
1595 return installer.data
1596}
1597
Colin Cross3f40fa42015-01-30 17:27:36 -08001598//
1599// Combined static+shared libraries
1600//
1601
Colin Cross919281a2016-04-05 16:42:05 -07001602type flagExporter struct {
1603 Properties FlagExporterProperties
1604
1605 flags []string
1606}
1607
1608func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001609 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001610 for _, dir := range includeDirs.Strings() {
Colin Crossf87b2612016-07-13 18:55:43 -07001611 f.flags = append(f.flags, inc+dir)
Dan Willemsene6c7f182016-07-13 10:45:01 -07001612 }
Colin Cross919281a2016-04-05 16:42:05 -07001613}
1614
1615func (f *flagExporter) reexportFlags(flags []string) {
1616 f.flags = append(f.flags, flags...)
1617}
1618
1619func (f *flagExporter) exportedFlags() []string {
1620 return f.flags
1621}
1622
1623type exportedFlagsProducer interface {
1624 exportedFlags() []string
1625}
1626
1627var _ exportedFlagsProducer = (*flagExporter)(nil)
1628
Colin Crossca860ac2016-01-04 14:34:37 -08001629type libraryCompiler struct {
1630 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001631
Colin Crossca860ac2016-01-04 14:34:37 -08001632 linker *libraryLinker
1633 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001634
Colin Crossca860ac2016-01-04 14:34:37 -08001635 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001636 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001637}
1638
Colin Crossca860ac2016-01-04 14:34:37 -08001639var _ compiler = (*libraryCompiler)(nil)
1640
1641func (library *libraryCompiler) props() []interface{} {
1642 props := library.baseCompiler.props()
1643 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001644}
1645
Colin Crossca860ac2016-01-04 14:34:37 -08001646func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1647 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001648
Dan Willemsen490fd492015-11-24 17:53:15 -08001649 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1650 // all code is position independent, and then those warnings get promoted to
1651 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001652 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001653 flags.CFlags = append(flags.CFlags, "-fPIC")
1654 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001655
Colin Crossca860ac2016-01-04 14:34:37 -08001656 if library.linker.static() {
1657 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001658 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001659 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001660 }
1661
Colin Crossca860ac2016-01-04 14:34:37 -08001662 return flags
1663}
1664
Colin Cross635c3b02016-05-18 15:37:25 -07001665func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1666 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001667
Dan Willemsenb40aab62016-04-20 14:21:14 -07001668 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001669 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001670
Dan Albert914449f2016-06-17 16:45:24 -07001671 pathDeps := deps.GeneratedHeaders
1672 pathDeps = append(pathDeps, ndkPathDeps(ctx)...)
1673
Colin Crossca860ac2016-01-04 14:34:37 -08001674 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001675 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001676 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
Dan Albert914449f2016-06-17 16:45:24 -07001677 nil, pathDeps)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001678 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001679 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001680 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
Dan Albert914449f2016-06-17 16:45:24 -07001681 nil, pathDeps)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001682 }
1683
1684 return objFiles
1685}
1686
1687type libraryLinker struct {
1688 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001689 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001690 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001691
1692 Properties LibraryLinkerProperties
1693
1694 dynamicProperties struct {
1695 BuildStatic bool `blueprint:"mutated"`
1696 BuildShared bool `blueprint:"mutated"`
1697 }
1698
Colin Crossca860ac2016-01-04 14:34:37 -08001699 // If we're used as a whole_static_lib, our missing dependencies need
1700 // to be given
1701 wholeStaticMissingDeps []string
1702
1703 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001704 objFiles android.Paths
Dan Albert914449f2016-06-17 16:45:24 -07001705
1706 // Uses the module's name if empty, but can be overridden. Does not include
1707 // shlib suffix.
1708 libName string
Colin Crossca860ac2016-01-04 14:34:37 -08001709}
1710
1711var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001712
Colin Crossc7a38dc2016-07-12 13:13:09 -07001713type libraryInterface interface {
1714 getWholeStaticMissingDeps() []string
1715 static() bool
1716 objs() android.Paths
1717}
1718
Colin Crossca860ac2016-01-04 14:34:37 -08001719func (library *libraryLinker) props() []interface{} {
1720 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001721 return append(props,
1722 &library.Properties,
1723 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001724 &library.flagExporter.Properties,
1725 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001726}
1727
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001728func (library *libraryLinker) getLibName(ctx ModuleContext) string {
Dan Albert914449f2016-06-17 16:45:24 -07001729 name := library.libName
1730 if name == "" {
1731 name = ctx.ModuleName()
1732 }
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001733
Dan Willemsen627d83d2016-07-22 13:40:59 -07001734 if ctx.Host() && Bool(library.Properties.Unique_host_soname) {
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001735 if !strings.HasSuffix(name, "-host") {
1736 name = name + "-host"
1737 }
1738 }
1739
1740 return name + library.Properties.VariantName
1741}
1742
Colin Crossca860ac2016-01-04 14:34:37 -08001743func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1744 flags = library.baseLinker.flags(ctx, flags)
1745
Colin Crossca860ac2016-01-04 14:34:37 -08001746 if !library.static() {
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001747 libName := library.getLibName(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -08001748 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1749 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001750 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001751 sharedFlag = "-shared"
1752 }
Colin Crossf87b2612016-07-13 18:55:43 -07001753 var f []string
Colin Crossf6566ed2015-03-24 11:13:38 -07001754 if ctx.Device() {
Colin Crossf87b2612016-07-13 18:55:43 -07001755 f = append(f,
Dan Willemsen99db8c32016-03-03 18:05:38 -08001756 "-nostdlib",
1757 "-Wl,--gc-sections",
1758 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001759 }
Colin Cross97ba0732015-03-23 17:50:24 -07001760
Colin Cross0af4b842015-04-30 16:36:18 -07001761 if ctx.Darwin() {
Colin Crossf87b2612016-07-13 18:55:43 -07001762 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001763 "-dynamiclib",
1764 "-single_module",
1765 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001766 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001767 )
1768 } else {
Colin Crossf87b2612016-07-13 18:55:43 -07001769 f = append(f,
Colin Cross0af4b842015-04-30 16:36:18 -07001770 sharedFlag,
Colin Crossf87b2612016-07-13 18:55:43 -07001771 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix())
Colin Cross0af4b842015-04-30 16:36:18 -07001772 }
Colin Crossf87b2612016-07-13 18:55:43 -07001773
1774 flags.LdFlags = append(f, flags.LdFlags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001775 }
Colin Cross97ba0732015-03-23 17:50:24 -07001776
1777 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001778}
1779
Colin Crossca860ac2016-01-04 14:34:37 -08001780func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1781 deps = library.baseLinker.deps(ctx, deps)
1782 if library.static() {
1783 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1784 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1785 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1786 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08001787 if ctx.Device() && !Bool(library.baseLinker.Properties.Nocrt) {
Colin Crossca860ac2016-01-04 14:34:37 -08001788 if !ctx.sdk() {
1789 deps.CrtBegin = "crtbegin_so"
1790 deps.CrtEnd = "crtend_so"
1791 } else {
1792 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1793 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1794 }
1795 }
1796 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1797 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1798 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1799 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001800
Colin Crossca860ac2016-01-04 14:34:37 -08001801 return deps
1802}
Colin Cross3f40fa42015-01-30 17:27:36 -08001803
Colin Crossca860ac2016-01-04 14:34:37 -08001804func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001805 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001806
Colin Cross635c3b02016-05-18 15:37:25 -07001807 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001808 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001809
Colin Cross635c3b02016-05-18 15:37:25 -07001810 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001811 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001812
Colin Cross0af4b842015-04-30 16:36:18 -07001813 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001814 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001815 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001816 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001817 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001818
Colin Crossca860ac2016-01-04 14:34:37 -08001819 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001820
1821 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001822
1823 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001824}
1825
Colin Crossca860ac2016-01-04 14:34:37 -08001826func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001827 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001828
Colin Cross635c3b02016-05-18 15:37:25 -07001829 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001830
Colin Cross635c3b02016-05-18 15:37:25 -07001831 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1832 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1833 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1834 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001835 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001836 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001837 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001838 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001839 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001840 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001841 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1842 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001843 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001844 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1845 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001846 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001847 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1848 }
1849 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001850 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001851 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1852 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001853 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001854 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001855 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001856 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001857 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001858 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001859 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001860 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001861 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001862 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001863 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001864 }
Colin Crossaee540a2015-07-06 17:48:31 -07001865 }
1866
Dan Willemsen648c8ae2016-07-21 16:42:14 -07001867 fileName := library.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001868 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001869 ret := outputFile
1870
1871 builderFlags := flagsToBuilderFlags(flags)
1872
1873 if library.stripper.needsStrip(ctx) {
1874 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001875 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001876 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1877 }
1878
Colin Crossca860ac2016-01-04 14:34:37 -08001879 sharedLibs := deps.SharedLibs
1880 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001881
Colin Crossca860ac2016-01-04 14:34:37 -08001882 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1883 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001884 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001885
Colin Cross665dce92016-04-28 14:50:03 -07001886 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001887}
1888
Colin Crossca860ac2016-01-04 14:34:37 -08001889func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001890 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001891
Colin Crossc99deeb2016-04-11 15:06:20 -07001892 objFiles = append(objFiles, deps.ObjFiles...)
1893
Colin Cross635c3b02016-05-18 15:37:25 -07001894 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001895 if library.static() {
1896 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001897 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001898 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001899 }
1900
Colin Cross919281a2016-04-05 16:42:05 -07001901 library.exportIncludes(ctx, "-I")
Dan Willemsen76f08272016-07-09 00:14:08 -07001902 library.reexportFlags(deps.ReexportedFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001903
1904 return out
1905}
1906
1907func (library *libraryLinker) buildStatic() bool {
Colin Cross68861832016-07-08 10:41:41 -07001908 return library.dynamicProperties.BuildStatic &&
1909 (library.Properties.Static.Enabled == nil || *library.Properties.Static.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001910}
1911
1912func (library *libraryLinker) buildShared() bool {
Colin Cross68861832016-07-08 10:41:41 -07001913 return library.dynamicProperties.BuildShared &&
1914 (library.Properties.Shared.Enabled == nil || *library.Properties.Shared.Enabled)
Colin Crossca860ac2016-01-04 14:34:37 -08001915}
1916
1917func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1918 return library.wholeStaticMissingDeps
1919}
1920
Colin Crossc99deeb2016-04-11 15:06:20 -07001921func (library *libraryLinker) installable() bool {
1922 return !library.static()
1923}
1924
Colin Crossc7a38dc2016-07-12 13:13:09 -07001925func (library *libraryLinker) objs() android.Paths {
1926 return library.objFiles
1927}
1928
Colin Crossca860ac2016-01-04 14:34:37 -08001929type libraryInstaller struct {
1930 baseInstaller
1931
Colin Cross30d5f512016-05-03 18:02:42 -07001932 linker *libraryLinker
1933 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001934}
1935
Colin Cross635c3b02016-05-18 15:37:25 -07001936func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001937 if !library.linker.static() {
1938 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001939 }
1940}
1941
Colin Cross30d5f512016-05-03 18:02:42 -07001942func (library *libraryInstaller) inData() bool {
1943 return library.baseInstaller.inData() || library.sanitize.inData()
1944}
1945
Colin Cross635c3b02016-05-18 15:37:25 -07001946func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1947 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001948
Colin Crossca860ac2016-01-04 14:34:37 -08001949 linker := &libraryLinker{}
1950 linker.dynamicProperties.BuildShared = shared
1951 linker.dynamicProperties.BuildStatic = static
1952 module.linker = linker
1953
1954 module.compiler = &libraryCompiler{
1955 linker: linker,
1956 }
1957 module.installer = &libraryInstaller{
1958 baseInstaller: baseInstaller{
1959 dir: "lib",
1960 dir64: "lib64",
1961 },
Colin Cross30d5f512016-05-03 18:02:42 -07001962 linker: linker,
1963 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001964 }
1965
Colin Crossca860ac2016-01-04 14:34:37 -08001966 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001967}
1968
Colin Crossca860ac2016-01-04 14:34:37 -08001969func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001970 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001971 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001972}
1973
Colin Cross3f40fa42015-01-30 17:27:36 -08001974//
1975// Objects (for crt*.o)
1976//
1977
Colin Crossca860ac2016-01-04 14:34:37 -08001978type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001979 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001980}
1981
Colin Crossca860ac2016-01-04 14:34:37 -08001982func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001983 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001984 module.compiler = &baseCompiler{}
1985 module.linker = &objectLinker{}
1986 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001987}
1988
Colin Cross76fada02016-07-27 10:31:13 -07001989func (object *objectLinker) appendLdflags(flags []string) {
1990 panic(fmt.Errorf("appendLdflags on object Linker not supported"))
1991}
1992
Colin Cross81413472016-04-11 14:37:39 -07001993func (object *objectLinker) props() []interface{} {
1994 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001995}
1996
Colin Crossca860ac2016-01-04 14:34:37 -08001997func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001998
Colin Cross81413472016-04-11 14:37:39 -07001999func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2000 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08002001 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002002}
2003
Colin Crossca860ac2016-01-04 14:34:37 -08002004func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07002005 if flags.Clang {
2006 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
2007 } else {
2008 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
2009 }
2010
Colin Crossca860ac2016-01-04 14:34:37 -08002011 return flags
2012}
2013
2014func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002015 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002016
Colin Cross97ba0732015-03-23 17:50:24 -07002017 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08002018
Colin Cross635c3b02016-05-18 15:37:25 -07002019 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08002020 if len(objFiles) == 1 {
2021 outputFile = objFiles[0]
2022 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07002023 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08002024 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002025 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08002026 }
2027
Colin Cross3f40fa42015-01-30 17:27:36 -08002028 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002029 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08002030}
2031
Colin Crossc99deeb2016-04-11 15:06:20 -07002032func (*objectLinker) installable() bool {
2033 return false
2034}
2035
Colin Cross3f40fa42015-01-30 17:27:36 -08002036//
2037// Executables
2038//
2039
Colin Crossca860ac2016-01-04 14:34:37 -08002040type binaryLinker struct {
2041 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07002042 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07002043
Colin Crossca860ac2016-01-04 14:34:37 -08002044 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07002045
Colin Cross635c3b02016-05-18 15:37:25 -07002046 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07002047}
2048
Colin Crossca860ac2016-01-04 14:34:37 -08002049var _ linker = (*binaryLinker)(nil)
2050
2051func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07002052 return append(binary.baseLinker.props(),
2053 &binary.Properties,
2054 &binary.stripper.StripProperties)
2055
Colin Cross3f40fa42015-01-30 17:27:36 -08002056}
2057
Colin Crossca860ac2016-01-04 14:34:37 -08002058func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002059 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07002060}
2061
Colin Crossca860ac2016-01-04 14:34:37 -08002062func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002063 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07002064}
2065
Colin Crossca860ac2016-01-04 14:34:37 -08002066func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07002067 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08002068 if binary.Properties.Stem != "" {
2069 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08002070 }
Colin Cross4ae185c2015-03-26 15:12:10 -07002071
Colin Crossca860ac2016-01-04 14:34:37 -08002072 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08002073}
2074
Colin Crossca860ac2016-01-04 14:34:37 -08002075func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2076 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07002077 if ctx.Device() {
Colin Crossa89d2e12016-01-11 12:48:37 -08002078 if !Bool(binary.baseLinker.Properties.Nocrt) {
2079 if !ctx.sdk() {
2080 if binary.buildStatic() {
2081 deps.CrtBegin = "crtbegin_static"
2082 } else {
2083 deps.CrtBegin = "crtbegin_dynamic"
2084 }
2085 deps.CrtEnd = "crtend_android"
Dan Albertc3144b12015-04-28 18:17:56 -07002086 } else {
Colin Crossa89d2e12016-01-11 12:48:37 -08002087 if binary.buildStatic() {
2088 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
2089 } else {
2090 if Bool(binary.Properties.Static_executable) {
2091 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
2092 } else {
2093 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
2094 }
2095 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
2096 }
Dan Albertc3144b12015-04-28 18:17:56 -07002097 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002098 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002099
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002100 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08002101 if inList("libc++_static", deps.StaticLibs) {
2102 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07002103 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002104 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
2105 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
2106 // move them to the beginning of deps.LateStaticLibs
2107 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08002108 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07002109 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08002110 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002111 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002112 }
Colin Crossca860ac2016-01-04 14:34:37 -08002113
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002114 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08002115 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
2116 "from static libs or set static_executable: true")
2117 }
2118 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002119}
2120
Colin Crossc99deeb2016-04-11 15:06:20 -07002121func (*binaryLinker) installable() bool {
2122 return true
2123}
2124
Colin Cross16b23492016-01-06 14:41:07 -08002125func (binary *binaryLinker) isDependencyRoot() bool {
2126 return true
2127}
2128
Colin Cross635c3b02016-05-18 15:37:25 -07002129func NewBinary(hod android.HostOrDeviceSupported) *Module {
2130 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002131 module.compiler = &baseCompiler{}
2132 module.linker = &binaryLinker{}
2133 module.installer = &baseInstaller{
2134 dir: "bin",
2135 }
2136 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08002137}
2138
Colin Crossca860ac2016-01-04 14:34:37 -08002139func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002140 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002141 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002142}
2143
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002144func (binary *binaryLinker) begin(ctx BaseModuleContext) {
2145 binary.baseLinker.begin(ctx)
2146
2147 static := Bool(binary.Properties.Static_executable)
2148 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07002149 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002150 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
2151 static = true
2152 }
2153 } else {
2154 // Static executables are not supported on Darwin or Windows
2155 static = false
2156 }
Colin Cross0af4b842015-04-30 16:36:18 -07002157 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002158 if static {
2159 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08002160 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07002161 }
2162}
2163
Colin Crossca860ac2016-01-04 14:34:37 -08002164func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2165 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07002166
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002167 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08002168 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07002169 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002170 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
2171 }
2172 }
2173
2174 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
2175 // all code is position independent, and then those warnings get promoted to
2176 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07002177 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08002178 flags.CFlags = append(flags.CFlags, "-fpie")
2179 }
Colin Cross97ba0732015-03-23 17:50:24 -07002180
Colin Crossf6566ed2015-03-24 11:13:38 -07002181 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002182 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002183 // Clang driver needs -static to create static executable.
2184 // However, bionic/linker uses -shared to overwrite.
2185 // Linker for x86 targets does not allow coexistance of -static and -shared,
2186 // so we add -static only if -shared is not used.
2187 if !inList("-shared", flags.LdFlags) {
2188 flags.LdFlags = append(flags.LdFlags, "-static")
2189 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002190
Colin Crossed4cf0b2015-03-26 14:43:45 -07002191 flags.LdFlags = append(flags.LdFlags,
2192 "-nostdlib",
2193 "-Bstatic",
2194 "-Wl,--gc-sections",
2195 )
2196
2197 } else {
Colin Cross16b23492016-01-06 14:41:07 -08002198 if flags.DynamicLinker == "" {
2199 flags.DynamicLinker = "/system/bin/linker"
2200 if flags.Toolchain.Is64Bit() {
2201 flags.DynamicLinker += "64"
2202 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002203 }
2204
2205 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002206 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002207 "-nostdlib",
2208 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002209 "-Wl,--gc-sections",
2210 "-Wl,-z,nocopyreloc",
2211 )
2212 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002213 } else {
2214 if binary.staticBinary() {
2215 flags.LdFlags = append(flags.LdFlags, "-static")
2216 }
2217 if ctx.Darwin() {
2218 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2219 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002220 }
2221
Colin Cross97ba0732015-03-23 17:50:24 -07002222 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002223}
2224
Colin Crossca860ac2016-01-04 14:34:37 -08002225func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002226 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002227
Colin Cross665dce92016-04-28 14:50:03 -07002228 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002229 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002230 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002231 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002232 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002233 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002234
Colin Cross635c3b02016-05-18 15:37:25 -07002235 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002236
Colin Crossca860ac2016-01-04 14:34:37 -08002237 sharedLibs := deps.SharedLibs
2238 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2239
Colin Cross16b23492016-01-06 14:41:07 -08002240 if flags.DynamicLinker != "" {
2241 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2242 }
2243
Colin Cross665dce92016-04-28 14:50:03 -07002244 builderFlags := flagsToBuilderFlags(flags)
2245
2246 if binary.stripper.needsStrip(ctx) {
2247 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002248 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002249 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2250 }
2251
2252 if binary.Properties.Prefix_symbols != "" {
2253 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002254 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002255 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2256 flagsToBuilderFlags(flags), afterPrefixSymbols)
2257 }
2258
Colin Crossca860ac2016-01-04 14:34:37 -08002259 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002260 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002261 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002262
2263 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002264}
Colin Cross3f40fa42015-01-30 17:27:36 -08002265
Colin Cross635c3b02016-05-18 15:37:25 -07002266func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002267 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002268}
2269
Colin Cross665dce92016-04-28 14:50:03 -07002270type stripper struct {
2271 StripProperties StripProperties
2272}
2273
2274func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2275 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2276}
2277
Colin Cross635c3b02016-05-18 15:37:25 -07002278func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002279 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002280 if ctx.Darwin() {
2281 TransformDarwinStrip(ctx, in, out)
2282 } else {
2283 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2284 // TODO(ccross): don't add gnu debuglink for user builds
2285 flags.stripAddGnuDebuglink = true
2286 TransformStrip(ctx, in, out, flags)
2287 }
Colin Cross665dce92016-04-28 14:50:03 -07002288}
2289
Colin Cross635c3b02016-05-18 15:37:25 -07002290func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002291 if m, ok := mctx.Module().(*Module); ok {
Colin Crossc7a38dc2016-07-12 13:13:09 -07002292 if test, ok := m.linker.(*testBinaryLinker); ok {
2293 if Bool(test.testLinker.Properties.Test_per_src) {
Colin Crossca860ac2016-01-04 14:34:37 -08002294 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2295 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2296 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2297 }
2298 tests := mctx.CreateLocalVariations(testNames...)
2299 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2300 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002301 tests[i].(*Module).linker.(*testBinaryLinker).binaryLinker.Properties.Stem = testNames[i]
Colin Crossca860ac2016-01-04 14:34:37 -08002302 }
Colin Cross6002e052015-09-16 16:00:08 -07002303 }
2304 }
2305 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002306}
2307
Colin Crossca860ac2016-01-04 14:34:37 -08002308type testLinker struct {
Colin Crossca860ac2016-01-04 14:34:37 -08002309 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002310}
2311
Colin Crossca860ac2016-01-04 14:34:37 -08002312func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
Colin Crossca860ac2016-01-04 14:34:37 -08002313 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002314 return flags
2315 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002316
Colin Cross97ba0732015-03-23 17:50:24 -07002317 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002318 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002319 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002320
Colin Crossa1ad8d12016-06-01 17:09:44 -07002321 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002322 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002323 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002324 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002325 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2326 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002327 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002328 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2329 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002330 }
2331 } else {
2332 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002333 }
2334
Colin Cross21b9a242015-03-24 14:15:58 -07002335 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002336}
2337
Colin Crossca860ac2016-01-04 14:34:37 -08002338func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2339 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002340 if ctx.sdk() && ctx.Device() {
2341 switch ctx.selectedStl() {
2342 case "ndk_libc++_shared", "ndk_libc++_static":
2343 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2344 case "ndk_libgnustl_static":
2345 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2346 default:
2347 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2348 }
2349 } else {
2350 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2351 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002352 }
Colin Crossc7a38dc2016-07-12 13:13:09 -07002353 return deps
2354}
2355
2356type testBinaryLinker struct {
2357 testLinker
2358 binaryLinker
2359}
2360
2361func (test *testBinaryLinker) begin(ctx BaseModuleContext) {
2362 test.binaryLinker.begin(ctx)
2363 runpath := "../../lib"
2364 if ctx.toolchain().Is64Bit() {
2365 runpath += "64"
2366 }
2367 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
2368}
2369
2370func (test *testBinaryLinker) props() []interface{} {
2371 return append(test.binaryLinker.props(), &test.testLinker.Properties)
2372}
2373
2374func (test *testBinaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2375 flags = test.binaryLinker.flags(ctx, flags)
2376 flags = test.testLinker.flags(ctx, flags)
2377 return flags
2378}
2379
2380func (test *testBinaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2381 deps = test.testLinker.deps(ctx, deps)
Colin Crossca860ac2016-01-04 14:34:37 -08002382 deps = test.binaryLinker.deps(ctx, deps)
2383 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002384}
2385
Colin Crossc7a38dc2016-07-12 13:13:09 -07002386type testLibraryLinker struct {
2387 testLinker
2388 *libraryLinker
2389}
2390
2391func (test *testLibraryLinker) props() []interface{} {
2392 return append(test.libraryLinker.props(), &test.testLinker.Properties)
2393}
2394
2395func (test *testLibraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
2396 flags = test.libraryLinker.flags(ctx, flags)
2397 flags = test.testLinker.flags(ctx, flags)
2398 return flags
2399}
2400
2401func (test *testLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2402 deps = test.testLinker.deps(ctx, deps)
2403 deps = test.libraryLinker.deps(ctx, deps)
2404 return deps
2405}
2406
Colin Crossca860ac2016-01-04 14:34:37 -08002407type testInstaller struct {
2408 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002409}
2410
Colin Cross635c3b02016-05-18 15:37:25 -07002411func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002412 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2413 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2414 installer.baseInstaller.install(ctx, file)
2415}
2416
Colin Cross635c3b02016-05-18 15:37:25 -07002417func NewTest(hod android.HostOrDeviceSupported) *Module {
2418 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002419 module.compiler = &baseCompiler{}
Colin Crossc7a38dc2016-07-12 13:13:09 -07002420 linker := &testBinaryLinker{}
2421 linker.testLinker.Properties.Gtest = true
Colin Crossca860ac2016-01-04 14:34:37 -08002422 module.linker = linker
2423 module.installer = &testInstaller{
2424 baseInstaller: baseInstaller{
2425 dir: "nativetest",
2426 dir64: "nativetest64",
2427 data: true,
2428 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002429 }
Colin Crossca860ac2016-01-04 14:34:37 -08002430 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002431}
2432
Colin Crossca860ac2016-01-04 14:34:37 -08002433func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002434 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002435 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002436}
2437
Colin Crossc7a38dc2016-07-12 13:13:09 -07002438func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
2439 module := NewLibrary(android.HostAndDeviceSupported, false, true)
2440 linker := &testLibraryLinker{
2441 libraryLinker: module.linker.(*libraryLinker),
2442 }
2443 linker.testLinker.Properties.Gtest = true
2444 module.linker = linker
2445 module.installer = &testInstaller{
2446 baseInstaller: baseInstaller{
2447 dir: "nativetest",
2448 dir64: "nativetest64",
2449 data: true,
2450 },
2451 }
2452 return module
2453}
2454
2455func testLibraryFactory() (blueprint.Module, []interface{}) {
2456 module := NewTestLibrary(android.HostAndDeviceSupported)
2457 return module.Init()
2458}
2459
Colin Crossca860ac2016-01-04 14:34:37 -08002460type benchmarkLinker struct {
Colin Crossaa3bf372016-07-14 10:27:10 -07002461 testBinaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002462}
2463
Colin Crossca860ac2016-01-04 14:34:37 -08002464func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Crossaa3bf372016-07-14 10:27:10 -07002465 deps = benchmark.testBinaryLinker.deps(ctx, deps)
Colin Cross26832742016-07-11 14:57:56 -07002466 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
Colin Crossca860ac2016-01-04 14:34:37 -08002467 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002468}
2469
Colin Cross635c3b02016-05-18 15:37:25 -07002470func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2471 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002472 module.compiler = &baseCompiler{}
2473 module.linker = &benchmarkLinker{}
Colin Cross624b8ed2016-07-11 17:20:09 -07002474 module.installer = &testInstaller{
2475 baseInstaller: baseInstaller{
2476 dir: "nativetest",
2477 dir64: "nativetest64",
2478 data: true,
2479 },
Colin Cross2ba19d92015-05-07 15:44:20 -07002480 }
Colin Crossca860ac2016-01-04 14:34:37 -08002481 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002482}
2483
Colin Crossca860ac2016-01-04 14:34:37 -08002484func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002485 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002486 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002487}
2488
Colin Cross3f40fa42015-01-30 17:27:36 -08002489//
2490// Static library
2491//
2492
Colin Crossca860ac2016-01-04 14:34:37 -08002493func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002494 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002495 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002496}
2497
2498//
2499// Shared libraries
2500//
2501
Colin Crossca860ac2016-01-04 14:34:37 -08002502func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002503 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002504 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002505}
2506
2507//
2508// Host static library
2509//
2510
Colin Crossca860ac2016-01-04 14:34:37 -08002511func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002512 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002513 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002514}
2515
2516//
2517// Host Shared libraries
2518//
2519
Colin Crossca860ac2016-01-04 14:34:37 -08002520func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002521 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002522 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002523}
2524
2525//
2526// Host Binaries
2527//
2528
Colin Crossca860ac2016-01-04 14:34:37 -08002529func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002530 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002531 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002532}
2533
2534//
Colin Cross1f8f2342015-03-26 16:09:47 -07002535// Host Tests
2536//
2537
Colin Crossca860ac2016-01-04 14:34:37 -08002538func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002539 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002540 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002541}
2542
2543//
Colin Cross2ba19d92015-05-07 15:44:20 -07002544// Host Benchmarks
2545//
2546
Colin Crossca860ac2016-01-04 14:34:37 -08002547func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002548 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002549 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002550}
2551
2552//
Colin Crosscfad1192015-11-02 16:43:11 -08002553// Defaults
2554//
Colin Crossca860ac2016-01-04 14:34:37 -08002555type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002556 android.ModuleBase
2557 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002558}
2559
Colin Cross635c3b02016-05-18 15:37:25 -07002560func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002561}
2562
Colin Crossca860ac2016-01-04 14:34:37 -08002563func defaultsFactory() (blueprint.Module, []interface{}) {
2564 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002565
2566 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002567 &BaseProperties{},
2568 &BaseCompilerProperties{},
2569 &BaseLinkerProperties{},
2570 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002571 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002572 &LibraryLinkerProperties{},
2573 &BinaryLinkerProperties{},
2574 &TestLinkerProperties{},
2575 &UnusedProperties{},
2576 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002577 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002578 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002579 }
2580
Colin Cross635c3b02016-05-18 15:37:25 -07002581 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2582 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002583
Colin Cross635c3b02016-05-18 15:37:25 -07002584 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002585}
2586
2587//
Colin Cross3f40fa42015-01-30 17:27:36 -08002588// Device libraries shipped with gcc
2589//
2590
Colin Crossca860ac2016-01-04 14:34:37 -08002591type toolchainLibraryLinker struct {
2592 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002593}
2594
Colin Crossca860ac2016-01-04 14:34:37 -08002595var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2596
2597func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002598 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002599 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002600}
2601
Colin Crossca860ac2016-01-04 14:34:37 -08002602func (*toolchainLibraryLinker) buildStatic() bool {
2603 return true
2604}
Colin Cross3f40fa42015-01-30 17:27:36 -08002605
Colin Crossca860ac2016-01-04 14:34:37 -08002606func (*toolchainLibraryLinker) buildShared() bool {
2607 return false
2608}
2609
2610func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002611 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002612 module.compiler = &baseCompiler{}
2613 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002614 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002615 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002616}
2617
Colin Crossca860ac2016-01-04 14:34:37 -08002618func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002619 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002620
2621 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002622 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002623
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002624 if flags.Clang {
2625 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2626 }
2627
Colin Crossca860ac2016-01-04 14:34:37 -08002628 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002629
2630 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002631
Colin Crossca860ac2016-01-04 14:34:37 -08002632 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002633}
2634
Colin Crossc99deeb2016-04-11 15:06:20 -07002635func (*toolchainLibraryLinker) installable() bool {
2636 return false
2637}
2638
Dan Albertbe961682015-03-18 23:38:50 -07002639// NDK prebuilt libraries.
2640//
2641// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2642// either (with the exception of the shared STLs, which are installed to the app's directory rather
2643// than to the system image).
2644
Colin Cross635c3b02016-05-18 15:37:25 -07002645func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002646 suffix := ""
2647 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2648 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002649 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002650 suffix = "64"
2651 }
Colin Cross635c3b02016-05-18 15:37:25 -07002652 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002653 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002654}
2655
Colin Cross635c3b02016-05-18 15:37:25 -07002656func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2657 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002658
2659 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2660 // We want to translate to just NAME.EXT
2661 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2662 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002663 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002664}
2665
Colin Crossca860ac2016-01-04 14:34:37 -08002666type ndkPrebuiltObjectLinker struct {
2667 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002668}
2669
Colin Crossca860ac2016-01-04 14:34:37 -08002670func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002671 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002672 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002673}
2674
Colin Crossca860ac2016-01-04 14:34:37 -08002675func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002676 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002677 module.linker = &ndkPrebuiltObjectLinker{}
Dan Willemsen72d39932016-07-08 23:23:48 -07002678 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002679 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002680}
2681
Colin Crossca860ac2016-01-04 14:34:37 -08002682func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002683 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002684 // A null build step, but it sets up the output path.
2685 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2686 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2687 }
2688
Colin Crossca860ac2016-01-04 14:34:37 -08002689 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002690}
2691
Colin Crossca860ac2016-01-04 14:34:37 -08002692type ndkPrebuiltLibraryLinker struct {
2693 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002694}
2695
Colin Crossca860ac2016-01-04 14:34:37 -08002696var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2697var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002698
Colin Crossca860ac2016-01-04 14:34:37 -08002699func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002700 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002701}
2702
Colin Crossca860ac2016-01-04 14:34:37 -08002703func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002704 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002705 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002706}
2707
Colin Crossca860ac2016-01-04 14:34:37 -08002708func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002709 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002710 linker := &ndkPrebuiltLibraryLinker{}
2711 linker.dynamicProperties.BuildShared = true
2712 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002713 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002714 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002715}
2716
Colin Crossca860ac2016-01-04 14:34:37 -08002717func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002718 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002719 // A null build step, but it sets up the output path.
Colin Cross919281a2016-04-05 16:42:05 -07002720 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002721
Colin Crossca860ac2016-01-04 14:34:37 -08002722 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2723 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002724}
2725
2726// The NDK STLs are slightly different from the prebuilt system libraries:
2727// * Are not specific to each platform version.
2728// * The libraries are not in a predictable location for each STL.
2729
Colin Crossca860ac2016-01-04 14:34:37 -08002730type ndkPrebuiltStlLinker struct {
2731 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002732}
2733
Colin Crossca860ac2016-01-04 14:34:37 -08002734func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002735 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002736 linker := &ndkPrebuiltStlLinker{}
2737 linker.dynamicProperties.BuildShared = true
2738 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002739 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002740 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002741}
2742
Colin Crossca860ac2016-01-04 14:34:37 -08002743func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002744 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002745 linker := &ndkPrebuiltStlLinker{}
2746 linker.dynamicProperties.BuildStatic = true
2747 module.linker = linker
Dan Willemsen72d39932016-07-08 23:23:48 -07002748 module.Properties.HideFromMake = true
Colin Crossca860ac2016-01-04 14:34:37 -08002749 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002750}
2751
Colin Cross635c3b02016-05-18 15:37:25 -07002752func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002753 gccVersion := toolchain.GccVersion()
2754 var libDir string
2755 switch stl {
2756 case "libstlport":
2757 libDir = "cxx-stl/stlport/libs"
2758 case "libc++":
2759 libDir = "cxx-stl/llvm-libc++/libs"
2760 case "libgnustl":
2761 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2762 }
2763
2764 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002765 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002766 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002767 }
2768
2769 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002770 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002771}
2772
Colin Crossca860ac2016-01-04 14:34:37 -08002773func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002774 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002775 // A null build step, but it sets up the output path.
2776 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2777 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2778 }
2779
Colin Cross919281a2016-04-05 16:42:05 -07002780 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002781
2782 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002783 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002784 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002785 libExt = staticLibraryExtension
2786 }
2787
2788 stlName := strings.TrimSuffix(libName, "_shared")
2789 stlName = strings.TrimSuffix(stlName, "_static")
2790 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002791 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002792}
2793
Colin Cross635c3b02016-05-18 15:37:25 -07002794func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002795 if m, ok := mctx.Module().(*Module); ok {
2796 if m.linker != nil {
2797 if linker, ok := m.linker.(baseLinkerInterface); ok {
2798 var modules []blueprint.Module
2799 if linker.buildStatic() && linker.buildShared() {
2800 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002801 static := modules[0].(*Module)
2802 shared := modules[1].(*Module)
2803
2804 static.linker.(baseLinkerInterface).setStatic(true)
2805 shared.linker.(baseLinkerInterface).setStatic(false)
2806
2807 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2808 sharedCompiler := shared.compiler.(*libraryCompiler)
2809 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2810 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2811 // Optimize out compiling common .o files twice for static+shared libraries
2812 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2813 sharedCompiler.baseCompiler.Properties.Srcs = nil
Colin Cross5dab8402016-07-27 10:30:21 -07002814 sharedCompiler.baseCompiler.Properties.Generated_sources = nil
Colin Crossc99deeb2016-04-11 15:06:20 -07002815 }
2816 }
Colin Crossca860ac2016-01-04 14:34:37 -08002817 } else if linker.buildStatic() {
2818 modules = mctx.CreateLocalVariations("static")
2819 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2820 } else if linker.buildShared() {
2821 modules = mctx.CreateLocalVariations("shared")
2822 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2823 } else {
2824 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2825 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002826 }
2827 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002828 }
2829}
Colin Cross74d1ec02015-04-28 13:30:13 -07002830
2831// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2832// modifies the slice contents in place, and returns a subslice of the original slice
2833func lastUniqueElements(list []string) []string {
2834 totalSkip := 0
2835 for i := len(list) - 1; i >= totalSkip; i-- {
2836 skip := 0
2837 for j := i - 1; j >= totalSkip; j-- {
2838 if list[i] == list[j] {
2839 skip++
2840 } else {
2841 list[j+skip] = list[j]
2842 }
2843 }
2844 totalSkip += skip
2845 }
2846 return list[totalSkip:]
2847}
Colin Cross06a931b2015-10-28 17:23:31 -07002848
2849var Bool = proptools.Bool