blob: cea8cdea780dfe0f21e56543351fa2a6915572a2 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross635c3b02016-05-18 15:37:25 -070030 "android/soong/android"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
Colin Crossca860ac2016-01-04 14:34:37 -080035 soong.RegisterModuleType("cc_library_static", libraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", librarySharedFactory)
37 soong.RegisterModuleType("cc_library", libraryFactory)
38 soong.RegisterModuleType("cc_object", objectFactory)
39 soong.RegisterModuleType("cc_binary", binaryFactory)
40 soong.RegisterModuleType("cc_test", testFactory)
41 soong.RegisterModuleType("cc_benchmark", benchmarkFactory)
42 soong.RegisterModuleType("cc_defaults", defaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
Colin Crossca860ac2016-01-04 14:34:37 -080044 soong.RegisterModuleType("toolchain_library", toolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", ndkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", ndkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", ndkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", ndkPrebuiltSharedStlFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070049
Colin Crossca860ac2016-01-04 14:34:37 -080050 soong.RegisterModuleType("cc_library_host_static", libraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", libraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", binaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", testHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", benchmarkHostFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070055
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross635c3b02016-05-18 15:37:25 -070059 android.RegisterBottomUpMutator("link", linkageMutator)
60 android.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 android.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross16b23492016-01-06 14:41:07 -080062
Colin Cross635c3b02016-05-18 15:37:25 -070063 android.RegisterTopDownMutator("asan_deps", sanitizerDepsMutator(asan))
64 android.RegisterBottomUpMutator("asan", sanitizerMutator(asan))
Colin Cross16b23492016-01-06 14:41:07 -080065
Colin Cross635c3b02016-05-18 15:37:25 -070066 android.RegisterTopDownMutator("tsan_deps", sanitizerDepsMutator(tsan))
67 android.RegisterBottomUpMutator("tsan", sanitizerMutator(tsan))
Colin Cross463a90e2015-06-17 14:20:06 -070068}
69
Colin Cross3f40fa42015-01-30 17:27:36 -080070var (
Colin Cross635c3b02016-05-18 15:37:25 -070071 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080072
Dan Willemsen34cc69e2015-09-23 15:26:20 -070073 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
Colin Cross3f40fa42015-01-30 17:27:36 -080074)
75
76// Flags used by lots of devices. Putting them in package static variables will save bytes in
77// build.ninja so they aren't repeated for every file
78var (
79 commonGlobalCflags = []string{
80 "-DANDROID",
81 "-fmessage-length=0",
82 "-W",
83 "-Wall",
84 "-Wno-unused",
85 "-Winit-self",
86 "-Wpointer-arith",
87
88 // COMMON_RELEASE_CFLAGS
89 "-DNDEBUG",
90 "-UDEBUG",
91 }
92
93 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080094 "-fdiagnostics-color",
95
Colin Cross3f40fa42015-01-30 17:27:36 -080096 // TARGET_ERROR_FLAGS
97 "-Werror=return-type",
98 "-Werror=non-virtual-dtor",
99 "-Werror=address",
100 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -0800101 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -0800102 }
103
104 hostGlobalCflags = []string{}
105
106 commonGlobalCppflags = []string{
107 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700108 }
109
Dan Willemsenbe03f342016-03-03 17:21:04 -0800110 noOverrideGlobalCflags = []string{
111 "-Werror=int-to-pointer-cast",
112 "-Werror=pointer-to-int-cast",
113 }
114
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700115 illegalFlags = []string{
116 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800117 }
118)
119
120func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700121 if android.BuildOs == android.Linux {
Dan Willemsen0c38c5e2016-03-29 17:31:57 -0700122 commonGlobalCflags = append(commonGlobalCflags, "-fdebug-prefix-map=/proc/self/cwd=")
123 }
124
Colin Cross3f40fa42015-01-30 17:27:36 -0800125 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
126 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
127 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800128 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800129
130 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
131
132 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800133 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800134 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800135 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800136 pctx.StaticVariable("hostClangGlobalCflags",
137 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800138 pctx.StaticVariable("noOverrideClangGlobalCflags",
139 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
140
Tim Kilbournf2948142015-03-11 12:03:03 -0700141 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800142 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800143
144 // Everything in this list is a crime against abstraction and dependency tracking.
145 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800146 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700147 []string{
148 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800149 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700150 "hardware/libhardware/include",
151 "hardware/libhardware_legacy/include",
152 "hardware/ril/include",
153 "libnativehelper/include",
154 "frameworks/native/include",
155 "frameworks/native/opengl/include",
156 "frameworks/av/include",
157 "frameworks/base/include",
158 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800159 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
160 // with this, since there is no associated library.
161 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
162 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800163
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700164 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
165 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700166 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700167 return override, nil
168 }
169 return "${clangDefaultBase}", nil
170 })
171 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
Colin Cross635c3b02016-05-18 15:37:25 -0700172 if override := config.(android.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700173 return override, nil
174 }
Stephen Hines369f0132016-04-26 14:34:07 -0700175 return "clang-2812033", nil
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700176 })
Colin Cross16b23492016-01-06 14:41:07 -0800177 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}")
178 pctx.StaticVariable("clangBin", "${clangPath}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800179}
180
Colin Crossca860ac2016-01-04 14:34:37 -0800181type Deps struct {
182 SharedLibs, LateSharedLibs []string
183 StaticLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700184
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700185 ReexportSharedLibHeaders, ReexportStaticLibHeaders []string
186
Colin Cross81413472016-04-11 14:37:39 -0700187 ObjFiles []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700188
Dan Willemsenb40aab62016-04-20 14:21:14 -0700189 GeneratedSources []string
190 GeneratedHeaders []string
191
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700192 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700193
Colin Cross97ba0732015-03-23 17:50:24 -0700194 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700195}
196
Colin Crossca860ac2016-01-04 14:34:37 -0800197type PathDeps struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700198 SharedLibs, LateSharedLibs android.Paths
199 StaticLibs, LateStaticLibs, WholeStaticLibs android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700200
Colin Cross635c3b02016-05-18 15:37:25 -0700201 ObjFiles android.Paths
202 WholeStaticLibObjFiles android.Paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700203
Colin Cross635c3b02016-05-18 15:37:25 -0700204 GeneratedSources android.Paths
205 GeneratedHeaders android.Paths
Dan Willemsenb40aab62016-04-20 14:21:14 -0700206
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700207 Cflags, ReexportedCflags []string
208
Colin Cross635c3b02016-05-18 15:37:25 -0700209 CrtBegin, CrtEnd android.OptionalPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700210}
211
Colin Crossca860ac2016-01-04 14:34:37 -0800212type Flags struct {
Colin Cross28344522015-04-22 13:07:53 -0700213 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
214 AsFlags []string // Flags that apply to assembly source files
215 CFlags []string // Flags that apply to C and C++ source files
216 ConlyFlags []string // Flags that apply to C source files
217 CppFlags []string // Flags that apply to C++ source files
218 YaccFlags []string // Flags that apply to Yacc source files
219 LdFlags []string // Flags that apply to linker command lines
Colin Cross16b23492016-01-06 14:41:07 -0800220 libFlags []string // Flags to add libraries early to the link order
Colin Cross28344522015-04-22 13:07:53 -0700221
222 Nocrt bool
223 Toolchain Toolchain
224 Clang bool
Colin Crossca860ac2016-01-04 14:34:37 -0800225
226 RequiredInstructionSet string
Colin Cross16b23492016-01-06 14:41:07 -0800227 DynamicLinker string
228
Colin Cross635c3b02016-05-18 15:37:25 -0700229 CFlagsDeps android.Paths // Files depended on by compiler flags
Colin Crossc472d572015-03-17 15:06:21 -0700230}
231
Colin Crossca860ac2016-01-04 14:34:37 -0800232type BaseCompilerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700233 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700234 Srcs []string `android:"arch_variant"`
235
236 // list of source files that should not be used to build the C/C++ module.
237 // This is most useful in the arch/multilib variants to remove non-common files
238 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700239
240 // list of module-specific flags that will be used for C and C++ compiles.
241 Cflags []string `android:"arch_variant"`
242
243 // list of module-specific flags that will be used for C++ compiles
244 Cppflags []string `android:"arch_variant"`
245
246 // list of module-specific flags that will be used for C compiles
247 Conlyflags []string `android:"arch_variant"`
248
249 // list of module-specific flags that will be used for .S compiles
250 Asflags []string `android:"arch_variant"`
251
Colin Crossca860ac2016-01-04 14:34:37 -0800252 // list of module-specific flags that will be used for C and C++ compiles when
253 // compiling with clang
254 Clang_cflags []string `android:"arch_variant"`
255
256 // list of module-specific flags that will be used for .S compiles when
257 // compiling with clang
258 Clang_asflags []string `android:"arch_variant"`
259
Colin Cross7d5136f2015-05-11 13:39:40 -0700260 // list of module-specific flags that will be used for .y and .yy compiles
261 Yaccflags []string
262
Colin Cross7d5136f2015-05-11 13:39:40 -0700263 // the instruction set architecture to use to compile the C/C++
264 // module.
265 Instruction_set string `android:"arch_variant"`
266
267 // list of directories relative to the root of the source tree that will
268 // be added to the include path using -I.
269 // If possible, don't use this. If adding paths from the current directory use
270 // local_include_dirs, if adding paths from other modules use export_include_dirs in
271 // that module.
272 Include_dirs []string `android:"arch_variant"`
273
274 // list of directories relative to the Blueprints file that will
275 // be added to the include path using -I
276 Local_include_dirs []string `android:"arch_variant"`
277
Dan Willemsenb40aab62016-04-20 14:21:14 -0700278 // list of generated sources to compile. These are the names of gensrcs or
279 // genrule modules.
280 Generated_sources []string `android:"arch_variant"`
281
282 // list of generated headers to add to the include path. These are the names
283 // of genrule modules.
284 Generated_headers []string `android:"arch_variant"`
285
Colin Crossca860ac2016-01-04 14:34:37 -0800286 // pass -frtti instead of -fno-rtti
287 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700288
Colin Crossca860ac2016-01-04 14:34:37 -0800289 Debug, Release struct {
290 // list of module-specific flags that will be used for C and C++ compiles in debug or
291 // release builds
292 Cflags []string `android:"arch_variant"`
293 } `android:"arch_variant"`
294}
Colin Cross7d5136f2015-05-11 13:39:40 -0700295
Colin Crossca860ac2016-01-04 14:34:37 -0800296type BaseLinkerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700297 // list of modules whose object files should be linked into this module
298 // in their entirety. For static library modules, all of the .o files from the intermediate
299 // directory of the dependency will be linked into this modules .a file. For a shared library,
300 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
Colin Cross6ee75b62016-05-05 15:57:15 -0700301 Whole_static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700302
303 // list of modules that should be statically linked into this module.
Colin Cross6ee75b62016-05-05 15:57:15 -0700304 Static_libs []string `android:"arch_variant,variant_prepend"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700305
306 // list of modules that should be dynamically linked into this module.
307 Shared_libs []string `android:"arch_variant"`
308
Colin Crossca860ac2016-01-04 14:34:37 -0800309 // list of module-specific flags that will be used for all link steps
310 Ldflags []string `android:"arch_variant"`
311
312 // don't insert default compiler flags into asflags, cflags,
313 // cppflags, conlyflags, ldflags, or include_dirs
314 No_default_compiler_flags *bool
315
316 // list of system libraries that will be dynamically linked to
317 // shared library and executable modules. If unset, generally defaults to libc
318 // and libm. Set to [] to prevent linking against libc and libm.
319 System_shared_libs []string
320
Colin Cross7d5136f2015-05-11 13:39:40 -0700321 // allow the module to contain undefined symbols. By default,
322 // modules cannot contain undefined symbols that are not satisified by their immediate
323 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
324 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700325 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700326
Dan Willemsend67be222015-09-16 15:19:33 -0700327 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700328 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700329
Colin Cross7d5136f2015-05-11 13:39:40 -0700330 // -l arguments to pass to linker for host-provided shared libraries
331 Host_ldlibs []string `android:"arch_variant"`
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700332
333 // list of shared libraries to re-export include directories from. Entries must be
334 // present in shared_libs.
335 Export_shared_lib_headers []string `android:"arch_variant"`
336
337 // list of static libraries to re-export include directories from. Entries must be
338 // present in static_libs.
339 Export_static_lib_headers []string `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800340}
Colin Cross7d5136f2015-05-11 13:39:40 -0700341
Colin Crossca860ac2016-01-04 14:34:37 -0800342type LibraryCompilerProperties struct {
343 Static struct {
344 Srcs []string `android:"arch_variant"`
345 Exclude_srcs []string `android:"arch_variant"`
346 Cflags []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700347 } `android:"arch_variant"`
Colin Crossca860ac2016-01-04 14:34:37 -0800348 Shared struct {
349 Srcs []string `android:"arch_variant"`
350 Exclude_srcs []string `android:"arch_variant"`
351 Cflags []string `android:"arch_variant"`
352 } `android:"arch_variant"`
353}
354
Colin Cross919281a2016-04-05 16:42:05 -0700355type FlagExporterProperties struct {
356 // list of directories relative to the Blueprints file that will
357 // be added to the include path using -I for any module that links against this module
358 Export_include_dirs []string `android:"arch_variant"`
359}
360
Colin Crossca860ac2016-01-04 14:34:37 -0800361type LibraryLinkerProperties struct {
362 Static struct {
363 Whole_static_libs []string `android:"arch_variant"`
364 Static_libs []string `android:"arch_variant"`
365 Shared_libs []string `android:"arch_variant"`
366 } `android:"arch_variant"`
367 Shared struct {
368 Whole_static_libs []string `android:"arch_variant"`
369 Static_libs []string `android:"arch_variant"`
370 Shared_libs []string `android:"arch_variant"`
371 } `android:"arch_variant"`
372
373 // local file name to pass to the linker as --version_script
374 Version_script *string `android:"arch_variant"`
375 // local file name to pass to the linker as -unexported_symbols_list
376 Unexported_symbols_list *string `android:"arch_variant"`
377 // local file name to pass to the linker as -force_symbols_not_weak_list
378 Force_symbols_not_weak_list *string `android:"arch_variant"`
379 // local file name to pass to the linker as -force_symbols_weak_list
380 Force_symbols_weak_list *string `android:"arch_variant"`
381
Colin Crossca860ac2016-01-04 14:34:37 -0800382 // don't link in crt_begin and crt_end. This flag should only be necessary for
383 // compiling crt or libc.
384 Nocrt *bool `android:"arch_variant"`
Colin Cross16b23492016-01-06 14:41:07 -0800385
386 VariantName string `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800387}
388
389type BinaryLinkerProperties struct {
390 // compile executable with -static
391 Static_executable *bool
392
393 // set the name of the output
394 Stem string `android:"arch_variant"`
395
396 // append to the name of the output
397 Suffix string `android:"arch_variant"`
398
399 // if set, add an extra objcopy --prefix-symbols= step
400 Prefix_symbols string
401}
402
403type TestLinkerProperties struct {
404 // if set, build against the gtest library. Defaults to true.
405 Gtest bool
406
407 // Create a separate binary for each source file. Useful when there is
408 // global state that can not be torn down and reset between each test suite.
409 Test_per_src *bool
410}
411
Colin Cross81413472016-04-11 14:37:39 -0700412type ObjectLinkerProperties struct {
413 // names of other cc_object modules to link into this module using partial linking
414 Objs []string `android:"arch_variant"`
415}
416
Colin Crossca860ac2016-01-04 14:34:37 -0800417// Properties used to compile all C or C++ modules
418type BaseProperties struct {
419 // compile module with clang instead of gcc
420 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700421
422 // Minimum sdk version supported when compiling against the ndk
423 Sdk_version string
424
Colin Crossca860ac2016-01-04 14:34:37 -0800425 // don't insert default compiler flags into asflags, cflags,
426 // cppflags, conlyflags, ldflags, or include_dirs
427 No_default_compiler_flags *bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700428
429 AndroidMkSharedLibs []string `blueprint:"mutated"`
Colin Crossbc6fb162016-05-24 15:39:04 -0700430 HideFromMake bool `blueprint:"mutated"`
Colin Crossca860ac2016-01-04 14:34:37 -0800431}
432
433type InstallerProperties struct {
Colin Cross7d5136f2015-05-11 13:39:40 -0700434 // install to a subdirectory of the default install path for the module
435 Relative_install_path string
436}
437
Colin Cross665dce92016-04-28 14:50:03 -0700438type StripProperties struct {
439 Strip struct {
440 None bool
441 Keep_symbols bool
442 }
443}
444
Colin Crossca860ac2016-01-04 14:34:37 -0800445type UnusedProperties struct {
Colin Cross21b481b2016-04-15 16:27:17 -0700446 Native_coverage *bool
447 Required []string
Colin Cross21b481b2016-04-15 16:27:17 -0700448 Tags []string
Colin Crosscfad1192015-11-02 16:43:11 -0800449}
450
Colin Crossca860ac2016-01-04 14:34:37 -0800451type ModuleContextIntf interface {
452 module() *Module
453 static() bool
454 staticBinary() bool
455 clang() bool
456 toolchain() Toolchain
457 noDefaultCompilerFlags() bool
458 sdk() bool
459 sdkVersion() string
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700460 selectedStl() string
Colin Crossca860ac2016-01-04 14:34:37 -0800461}
462
463type ModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700464 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800465 ModuleContextIntf
466}
467
468type BaseModuleContext interface {
Colin Cross635c3b02016-05-18 15:37:25 -0700469 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800470 ModuleContextIntf
471}
472
473type Customizer interface {
474 CustomizeProperties(BaseModuleContext)
475 Properties() []interface{}
476}
477
478type feature interface {
479 begin(ctx BaseModuleContext)
480 deps(ctx BaseModuleContext, deps Deps) Deps
481 flags(ctx ModuleContext, flags Flags) Flags
482 props() []interface{}
483}
484
485type compiler interface {
486 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700487 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800488}
489
490type linker interface {
491 feature
Colin Cross635c3b02016-05-18 15:37:25 -0700492 link(ctx ModuleContext, flags Flags, deps PathDeps, objFiles android.Paths) android.Path
Colin Crossc99deeb2016-04-11 15:06:20 -0700493 installable() bool
Colin Crossca860ac2016-01-04 14:34:37 -0800494}
495
496type installer interface {
497 props() []interface{}
Colin Cross635c3b02016-05-18 15:37:25 -0700498 install(ctx ModuleContext, path android.Path)
Colin Crossca860ac2016-01-04 14:34:37 -0800499 inData() bool
500}
501
Colin Crossc99deeb2016-04-11 15:06:20 -0700502type dependencyTag struct {
503 blueprint.BaseDependencyTag
504 name string
505 library bool
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700506
507 reexportFlags bool
Colin Crossc99deeb2016-04-11 15:06:20 -0700508}
509
510var (
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700511 sharedDepTag = dependencyTag{name: "shared", library: true}
512 sharedExportDepTag = dependencyTag{name: "shared", library: true, reexportFlags: true}
513 lateSharedDepTag = dependencyTag{name: "late shared", library: true}
514 staticDepTag = dependencyTag{name: "static", library: true}
515 staticExportDepTag = dependencyTag{name: "static", library: true, reexportFlags: true}
516 lateStaticDepTag = dependencyTag{name: "late static", library: true}
517 wholeStaticDepTag = dependencyTag{name: "whole static", library: true, reexportFlags: true}
518 genSourceDepTag = dependencyTag{name: "gen source"}
519 genHeaderDepTag = dependencyTag{name: "gen header"}
520 objDepTag = dependencyTag{name: "obj"}
521 crtBeginDepTag = dependencyTag{name: "crtbegin"}
522 crtEndDepTag = dependencyTag{name: "crtend"}
523 reuseObjTag = dependencyTag{name: "reuse objects"}
Colin Crossc99deeb2016-04-11 15:06:20 -0700524)
525
Colin Crossca860ac2016-01-04 14:34:37 -0800526// Module contains the properties and members used by all C/C++ module types, and implements
527// the blueprint.Module interface. It delegates to compiler, linker, and installer interfaces
528// to construct the output file. Behavior can be customized with a Customizer interface
529type Module struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700530 android.ModuleBase
531 android.DefaultableModule
Colin Crossc472d572015-03-17 15:06:21 -0700532
Colin Crossca860ac2016-01-04 14:34:37 -0800533 Properties BaseProperties
534 unused UnusedProperties
Colin Crossfa138792015-04-24 17:31:52 -0700535
Colin Crossca860ac2016-01-04 14:34:37 -0800536 // initialize before calling Init
Colin Cross635c3b02016-05-18 15:37:25 -0700537 hod android.HostOrDeviceSupported
538 multilib android.Multilib
Colin Crossc472d572015-03-17 15:06:21 -0700539
Colin Crossca860ac2016-01-04 14:34:37 -0800540 // delegates, initialize before calling Init
541 customizer Customizer
542 features []feature
543 compiler compiler
544 linker linker
545 installer installer
Colin Crossa8e07cc2016-04-04 15:07:06 -0700546 stl *stl
Colin Cross16b23492016-01-06 14:41:07 -0800547 sanitize *sanitize
548
549 androidMkSharedLibDeps []string
Colin Cross74d1ec02015-04-28 13:30:13 -0700550
Colin Cross635c3b02016-05-18 15:37:25 -0700551 outputFile android.OptionalPath
Colin Crossca860ac2016-01-04 14:34:37 -0800552
553 cachedToolchain Toolchain
Colin Crossc472d572015-03-17 15:06:21 -0700554}
555
Colin Crossca860ac2016-01-04 14:34:37 -0800556func (c *Module) Init() (blueprint.Module, []interface{}) {
557 props := []interface{}{&c.Properties, &c.unused}
558 if c.customizer != nil {
559 props = append(props, c.customizer.Properties()...)
560 }
561 if c.compiler != nil {
562 props = append(props, c.compiler.props()...)
563 }
564 if c.linker != nil {
565 props = append(props, c.linker.props()...)
566 }
567 if c.installer != nil {
568 props = append(props, c.installer.props()...)
569 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700570 if c.stl != nil {
571 props = append(props, c.stl.props()...)
572 }
Colin Cross16b23492016-01-06 14:41:07 -0800573 if c.sanitize != nil {
574 props = append(props, c.sanitize.props()...)
575 }
Colin Crossca860ac2016-01-04 14:34:37 -0800576 for _, feature := range c.features {
577 props = append(props, feature.props()...)
578 }
Colin Crossc472d572015-03-17 15:06:21 -0700579
Colin Cross635c3b02016-05-18 15:37:25 -0700580 _, props = android.InitAndroidArchModule(c, c.hod, c.multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700581
Colin Cross635c3b02016-05-18 15:37:25 -0700582 return android.InitDefaultableModule(c, c, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700583}
584
Colin Crossca860ac2016-01-04 14:34:37 -0800585type baseModuleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700586 android.BaseContext
Colin Crossca860ac2016-01-04 14:34:37 -0800587 moduleContextImpl
588}
589
590type moduleContext struct {
Colin Cross635c3b02016-05-18 15:37:25 -0700591 android.ModuleContext
Colin Crossca860ac2016-01-04 14:34:37 -0800592 moduleContextImpl
593}
594
595type moduleContextImpl struct {
596 mod *Module
597 ctx BaseModuleContext
598}
599
600func (ctx *moduleContextImpl) module() *Module {
601 return ctx.mod
602}
603
604func (ctx *moduleContextImpl) clang() bool {
605 return ctx.mod.clang(ctx.ctx)
606}
607
608func (ctx *moduleContextImpl) toolchain() Toolchain {
609 return ctx.mod.toolchain(ctx.ctx)
610}
611
612func (ctx *moduleContextImpl) static() bool {
613 if ctx.mod.linker == nil {
614 panic(fmt.Errorf("static called on module %q with no linker", ctx.ctx.ModuleName()))
615 }
616 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
617 return linker.static()
618 } else {
619 panic(fmt.Errorf("static called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
620 }
621}
622
623func (ctx *moduleContextImpl) staticBinary() bool {
624 if ctx.mod.linker == nil {
625 panic(fmt.Errorf("staticBinary called on module %q with no linker", ctx.ctx.ModuleName()))
626 }
627 if linker, ok := ctx.mod.linker.(baseLinkerInterface); ok {
628 return linker.staticBinary()
629 } else {
630 panic(fmt.Errorf("staticBinary called on module %q that doesn't use base linker", ctx.ctx.ModuleName()))
631 }
632}
633
634func (ctx *moduleContextImpl) noDefaultCompilerFlags() bool {
635 return Bool(ctx.mod.Properties.No_default_compiler_flags)
636}
637
638func (ctx *moduleContextImpl) sdk() bool {
Dan Willemsena96ff642016-06-07 12:34:45 -0700639 if ctx.ctx.Device() {
640 return ctx.mod.Properties.Sdk_version != ""
641 }
642 return false
Colin Crossca860ac2016-01-04 14:34:37 -0800643}
644
645func (ctx *moduleContextImpl) sdkVersion() string {
Dan Willemsena96ff642016-06-07 12:34:45 -0700646 if ctx.ctx.Device() {
647 return ctx.mod.Properties.Sdk_version
648 }
649 return ""
Colin Crossca860ac2016-01-04 14:34:37 -0800650}
651
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700652func (ctx *moduleContextImpl) selectedStl() string {
653 if stl := ctx.mod.stl; stl != nil {
654 return stl.Properties.SelectedStl
655 }
656 return ""
657}
658
Colin Cross635c3b02016-05-18 15:37:25 -0700659func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800660 return &Module{
661 hod: hod,
662 multilib: multilib,
663 }
664}
665
Colin Cross635c3b02016-05-18 15:37:25 -0700666func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800667 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700668 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800669 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800670 return module
671}
672
Colin Cross635c3b02016-05-18 15:37:25 -0700673func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800674 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700675 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800676 moduleContextImpl: moduleContextImpl{
677 mod: c,
678 },
679 }
680 ctx.ctx = ctx
681
682 flags := Flags{
683 Toolchain: c.toolchain(ctx),
684 Clang: c.clang(ctx),
685 }
Colin Crossca860ac2016-01-04 14:34:37 -0800686 if c.compiler != nil {
687 flags = c.compiler.flags(ctx, flags)
688 }
689 if c.linker != nil {
690 flags = c.linker.flags(ctx, flags)
691 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700692 if c.stl != nil {
693 flags = c.stl.flags(ctx, flags)
694 }
Colin Cross16b23492016-01-06 14:41:07 -0800695 if c.sanitize != nil {
696 flags = c.sanitize.flags(ctx, flags)
697 }
Colin Crossca860ac2016-01-04 14:34:37 -0800698 for _, feature := range c.features {
699 flags = feature.flags(ctx, flags)
700 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800701 if ctx.Failed() {
702 return
703 }
704
Colin Crossca860ac2016-01-04 14:34:37 -0800705 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
706 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
707 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800708
Colin Crossca860ac2016-01-04 14:34:37 -0800709 // Optimization to reduce size of build.ninja
710 // Replace the long list of flags for each file with a module-local variable
711 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
712 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
713 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
714 flags.CFlags = []string{"$cflags"}
715 flags.CppFlags = []string{"$cppflags"}
716 flags.AsFlags = []string{"$asflags"}
717
Colin Crossc99deeb2016-04-11 15:06:20 -0700718 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800719 if ctx.Failed() {
720 return
721 }
722
Colin Cross28344522015-04-22 13:07:53 -0700723 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700724
Colin Cross635c3b02016-05-18 15:37:25 -0700725 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800726 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700727 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800728 if ctx.Failed() {
729 return
730 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800731 }
732
Colin Crossca860ac2016-01-04 14:34:37 -0800733 if c.linker != nil {
734 outputFile := c.linker.link(ctx, flags, deps, objFiles)
735 if ctx.Failed() {
736 return
737 }
Colin Cross635c3b02016-05-18 15:37:25 -0700738 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700739
Colin Crossc99deeb2016-04-11 15:06:20 -0700740 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800741 c.installer.install(ctx, outputFile)
742 if ctx.Failed() {
743 return
744 }
745 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700746 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800747}
748
Colin Crossca860ac2016-01-04 14:34:37 -0800749func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
750 if c.cachedToolchain == nil {
751 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700752 os := ctx.Os()
753 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800754 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700755 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800756 return nil
757 }
758 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800759 }
Colin Crossca860ac2016-01-04 14:34:37 -0800760 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800761}
762
Colin Crossca860ac2016-01-04 14:34:37 -0800763func (c *Module) begin(ctx BaseModuleContext) {
764 if c.compiler != nil {
765 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700766 }
Colin Crossca860ac2016-01-04 14:34:37 -0800767 if c.linker != nil {
768 c.linker.begin(ctx)
769 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700770 if c.stl != nil {
771 c.stl.begin(ctx)
772 }
Colin Cross16b23492016-01-06 14:41:07 -0800773 if c.sanitize != nil {
774 c.sanitize.begin(ctx)
775 }
Colin Crossca860ac2016-01-04 14:34:37 -0800776 for _, feature := range c.features {
777 feature.begin(ctx)
778 }
779}
780
Colin Crossc99deeb2016-04-11 15:06:20 -0700781func (c *Module) deps(ctx BaseModuleContext) Deps {
782 deps := Deps{}
783
784 if c.compiler != nil {
785 deps = c.compiler.deps(ctx, deps)
786 }
787 if c.linker != nil {
788 deps = c.linker.deps(ctx, deps)
789 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700790 if c.stl != nil {
791 deps = c.stl.deps(ctx, deps)
792 }
Colin Cross16b23492016-01-06 14:41:07 -0800793 if c.sanitize != nil {
794 deps = c.sanitize.deps(ctx, deps)
795 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700796 for _, feature := range c.features {
797 deps = feature.deps(ctx, deps)
798 }
799
800 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
801 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
802 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
803 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
804 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
805
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700806 for _, lib := range deps.ReexportSharedLibHeaders {
807 if !inList(lib, deps.SharedLibs) {
808 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
809 }
810 }
811
812 for _, lib := range deps.ReexportStaticLibHeaders {
813 if !inList(lib, deps.StaticLibs) {
814 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
815 }
816 }
817
Colin Crossc99deeb2016-04-11 15:06:20 -0700818 return deps
819}
820
Colin Cross635c3b02016-05-18 15:37:25 -0700821func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800822 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700823 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800824 moduleContextImpl: moduleContextImpl{
825 mod: c,
826 },
827 }
828 ctx.ctx = ctx
829
830 if c.customizer != nil {
831 c.customizer.CustomizeProperties(ctx)
832 }
833
834 c.begin(ctx)
835
Colin Crossc99deeb2016-04-11 15:06:20 -0700836 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800837
Colin Crossc99deeb2016-04-11 15:06:20 -0700838 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
839
840 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
841 deps.WholeStaticLibs...)
842
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700843 for _, lib := range deps.StaticLibs {
844 depTag := staticDepTag
845 if inList(lib, deps.ReexportStaticLibHeaders) {
846 depTag = staticExportDepTag
847 }
848 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
849 deps.StaticLibs...)
850 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700851
852 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
853 deps.LateStaticLibs...)
854
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700855 for _, lib := range deps.SharedLibs {
856 depTag := sharedDepTag
857 if inList(lib, deps.ReexportSharedLibHeaders) {
858 depTag = sharedExportDepTag
859 }
860 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
861 deps.SharedLibs...)
862 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700863
864 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
865 deps.LateSharedLibs...)
866
Dan Willemsenb40aab62016-04-20 14:21:14 -0700867 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
868 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
869
Colin Crossc99deeb2016-04-11 15:06:20 -0700870 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
871
872 if deps.CrtBegin != "" {
873 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800874 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700875 if deps.CrtEnd != "" {
876 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700877 }
Colin Cross6362e272015-10-29 15:25:03 -0700878}
Colin Cross21b9a242015-03-24 14:15:58 -0700879
Colin Cross635c3b02016-05-18 15:37:25 -0700880func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800881 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700882 c.depsMutator(ctx)
883 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800884}
885
Colin Crossca860ac2016-01-04 14:34:37 -0800886func (c *Module) clang(ctx BaseModuleContext) bool {
887 clang := Bool(c.Properties.Clang)
888
889 if c.Properties.Clang == nil {
890 if ctx.Host() {
891 clang = true
892 }
893
894 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
895 clang = true
896 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800897 }
Colin Cross28344522015-04-22 13:07:53 -0700898
Colin Crossca860ac2016-01-04 14:34:37 -0800899 if !c.toolchain(ctx).ClangSupported() {
900 clang = false
901 }
902
903 return clang
904}
905
Colin Crossc99deeb2016-04-11 15:06:20 -0700906// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700907func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800908 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800909
Dan Willemsena96ff642016-06-07 12:34:45 -0700910 // Whether a module can link to another module, taking into
911 // account NDK linking.
912 linkTypeOk := func(from, to *Module) bool {
913 if from.Target().Os != android.Android {
914 // Host code is not restricted
915 return true
916 }
917 if from.Properties.Sdk_version == "" {
918 // Platform code can link to anything
919 return true
920 }
921 if _, ok := to.linker.(*toolchainLibraryLinker); ok {
922 // These are always allowed
923 return true
924 }
925 if _, ok := to.linker.(*ndkPrebuiltLibraryLinker); ok {
926 // These are allowed, but don't set sdk_version
927 return true
928 }
929 return from.Properties.Sdk_version != ""
930 }
931
Colin Crossc99deeb2016-04-11 15:06:20 -0700932 ctx.VisitDirectDeps(func(m blueprint.Module) {
933 name := ctx.OtherModuleName(m)
934 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800935
Colin Cross635c3b02016-05-18 15:37:25 -0700936 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700937 if a == nil {
938 ctx.ModuleErrorf("module %q not an android module", name)
939 return
Colin Crossca860ac2016-01-04 14:34:37 -0800940 }
Colin Crossca860ac2016-01-04 14:34:37 -0800941
Dan Willemsena96ff642016-06-07 12:34:45 -0700942 cc, _ := m.(*Module)
943 if cc == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700944 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700945 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700946 case genSourceDepTag:
947 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
948 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
949 genRule.GeneratedSourceFiles()...)
950 } else {
951 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
952 }
953 case genHeaderDepTag:
954 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
955 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
956 genRule.GeneratedSourceFiles()...)
957 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700958 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700959 } else {
960 ctx.ModuleErrorf("module %q is not a genrule", name)
961 }
962 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700963 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800964 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700965 return
966 }
967
968 if !a.Enabled() {
969 ctx.ModuleErrorf("depends on disabled module %q", name)
970 return
971 }
972
Colin Crossa1ad8d12016-06-01 17:09:44 -0700973 if a.Target().Os != ctx.Os() {
974 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
975 return
976 }
977
978 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
979 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700980 return
981 }
982
Dan Willemsena96ff642016-06-07 12:34:45 -0700983 if !cc.outputFile.Valid() {
Colin Crossc99deeb2016-04-11 15:06:20 -0700984 ctx.ModuleErrorf("module %q missing output file", name)
985 return
986 }
987
988 if tag == reuseObjTag {
989 depPaths.ObjFiles = append(depPaths.ObjFiles,
Dan Willemsena96ff642016-06-07 12:34:45 -0700990 cc.compiler.(*libraryCompiler).reuseObjFiles...)
Colin Crossc99deeb2016-04-11 15:06:20 -0700991 return
992 }
993
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700994 if t, ok := tag.(dependencyTag); ok && t.library {
Dan Willemsena96ff642016-06-07 12:34:45 -0700995 if i, ok := cc.linker.(exportedFlagsProducer); ok {
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700996 cflags := i.exportedFlags()
Colin Crossc99deeb2016-04-11 15:06:20 -0700997 depPaths.Cflags = append(depPaths.Cflags, cflags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700998
999 if t.reexportFlags {
1000 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
1001 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001002 }
Dan Willemsena96ff642016-06-07 12:34:45 -07001003
1004 if !linkTypeOk(c, cc) {
1005 ctx.ModuleErrorf("depends on non-NDK-built library %q", name)
1006 }
Colin Crossc99deeb2016-04-11 15:06:20 -07001007 }
1008
Colin Cross635c3b02016-05-18 15:37:25 -07001009 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -07001010
1011 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001012 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001013 depPtr = &depPaths.SharedLibs
1014 case lateSharedDepTag:
1015 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001016 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -07001017 depPtr = &depPaths.StaticLibs
1018 case lateStaticDepTag:
1019 depPtr = &depPaths.LateStaticLibs
1020 case wholeStaticDepTag:
1021 depPtr = &depPaths.WholeStaticLibs
Dan Willemsena96ff642016-06-07 12:34:45 -07001022 staticLib, _ := cc.linker.(*libraryLinker)
Colin Crossc99deeb2016-04-11 15:06:20 -07001023 if staticLib == nil || !staticLib.static() {
Dan Willemsena96ff642016-06-07 12:34:45 -07001024 ctx.ModuleErrorf("module %q not a static library", name)
Colin Crossc99deeb2016-04-11 15:06:20 -07001025 return
1026 }
1027
1028 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
1029 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
1030 for i := range missingDeps {
1031 missingDeps[i] += postfix
1032 }
1033 ctx.AddMissingDependencies(missingDeps)
1034 }
1035 depPaths.WholeStaticLibObjFiles =
1036 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1037 case objDepTag:
1038 depPtr = &depPaths.ObjFiles
1039 case crtBeginDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001040 depPaths.CrtBegin = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001041 case crtEndDepTag:
Dan Willemsena96ff642016-06-07 12:34:45 -07001042 depPaths.CrtEnd = cc.outputFile
Colin Crossc99deeb2016-04-11 15:06:20 -07001043 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001044 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001045 }
1046
1047 if depPtr != nil {
Dan Willemsena96ff642016-06-07 12:34:45 -07001048 *depPtr = append(*depPtr, cc.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001049 }
1050 })
1051
1052 return depPaths
1053}
1054
1055func (c *Module) InstallInData() bool {
1056 if c.installer == nil {
1057 return false
1058 }
1059 return c.installer.inData()
1060}
1061
1062// Compiler
1063
1064type baseCompiler struct {
1065 Properties BaseCompilerProperties
1066}
1067
1068var _ compiler = (*baseCompiler)(nil)
1069
1070func (compiler *baseCompiler) props() []interface{} {
1071 return []interface{}{&compiler.Properties}
1072}
1073
Dan Willemsenb40aab62016-04-20 14:21:14 -07001074func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1075
1076func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1077 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1078 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1079
1080 return deps
1081}
Colin Crossca860ac2016-01-04 14:34:37 -08001082
1083// Create a Flags struct that collects the compile flags from global values,
1084// per-target values, module type values, and per-module Blueprints properties
1085func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1086 toolchain := ctx.toolchain()
1087
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001088 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1089 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1090 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1091 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1092
Colin Crossca860ac2016-01-04 14:34:37 -08001093 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1094 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1095 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1096 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1097 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1098
Colin Cross28344522015-04-22 13:07:53 -07001099 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001100 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1101 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001102 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001103 includeDirsToFlags(localIncludeDirs),
1104 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001105
Colin Crossca860ac2016-01-04 14:34:37 -08001106 if !ctx.noDefaultCompilerFlags() {
1107 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001108 flags.GlobalFlags = append(flags.GlobalFlags,
1109 "${commonGlobalIncludes}",
1110 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001111 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001112 }
1113
1114 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001115 "-I" + android.PathForModuleSrc(ctx).String(),
1116 "-I" + android.PathForModuleOut(ctx).String(),
1117 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001118 }...)
1119 }
1120
Colin Crossca860ac2016-01-04 14:34:37 -08001121 instructionSet := compiler.Properties.Instruction_set
1122 if flags.RequiredInstructionSet != "" {
1123 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001124 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001125 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1126 if flags.Clang {
1127 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1128 }
1129 if err != nil {
1130 ctx.ModuleErrorf("%s", err)
1131 }
1132
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001133 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1134
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001135 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001136 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001137
Colin Cross97ba0732015-03-23 17:50:24 -07001138 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001139 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1140 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1141
Colin Cross97ba0732015-03-23 17:50:24 -07001142 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001143 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1144 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001145 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1146 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1147 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001148
1149 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001150 var gccPrefix string
1151 if !ctx.Darwin() {
1152 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1153 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001154
Colin Cross97ba0732015-03-23 17:50:24 -07001155 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1156 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1157 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001158 }
1159
Colin Crossa1ad8d12016-06-01 17:09:44 -07001160 hod := "host"
1161 if ctx.Os().Class == android.Device {
1162 hod = "device"
1163 }
1164
Colin Crossca860ac2016-01-04 14:34:37 -08001165 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001166 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1167
Colin Cross97ba0732015-03-23 17:50:24 -07001168 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001169 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001170 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001171 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001172 toolchain.ClangCflags(),
1173 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001174 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001175
1176 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001177 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001178 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001179 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001180 toolchain.Cflags(),
1181 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001182 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001183 }
1184
Colin Cross7b66f152015-12-15 16:07:43 -08001185 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1186 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1187 }
1188
Colin Crossf6566ed2015-03-24 11:13:38 -07001189 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001190 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001191 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001192 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001193 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001194 }
1195 }
1196
Colin Cross97ba0732015-03-23 17:50:24 -07001197 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001198
Colin Cross97ba0732015-03-23 17:50:24 -07001199 if flags.Clang {
1200 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001201 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001202 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001203 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001204 }
1205
Colin Crossc4bde762015-11-23 16:11:30 -08001206 if flags.Clang {
1207 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1208 } else {
1209 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001210 }
1211
Colin Crossca860ac2016-01-04 14:34:37 -08001212 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001213 if ctx.Host() && !flags.Clang {
1214 // The host GCC doesn't support C++14 (and is deprecated, so likely
1215 // never will). Build these modules with C++11.
1216 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1217 } else {
1218 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1219 }
1220 }
1221
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001222 // We can enforce some rules more strictly in the code we own. strict
1223 // indicates if this is code that we can be stricter with. If we have
1224 // rules that we want to apply to *our* code (but maybe can't for
1225 // vendor/device specific things), we could extend this to be a ternary
1226 // value.
1227 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001228 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001229 strict = false
1230 }
1231
1232 // Can be used to make some annotations stricter for code we can fix
1233 // (such as when we mark functions as deprecated).
1234 if strict {
1235 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1236 }
1237
Colin Cross3f40fa42015-01-30 17:27:36 -08001238 return flags
1239}
1240
Colin Cross635c3b02016-05-18 15:37:25 -07001241func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001242 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001243 objFiles := compiler.compileObjs(ctx, flags, "",
1244 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1245 deps.GeneratedSources, deps.GeneratedHeaders)
1246
Colin Crossca860ac2016-01-04 14:34:37 -08001247 if ctx.Failed() {
1248 return nil
1249 }
1250
Colin Crossca860ac2016-01-04 14:34:37 -08001251 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001252}
1253
1254// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001255func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1256 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001257
Colin Crossca860ac2016-01-04 14:34:37 -08001258 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001259
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001260 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001261 inputFiles = append(inputFiles, extraSrcs...)
1262 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1263
1264 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001265 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001266
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001267 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001268}
1269
Colin Crossca860ac2016-01-04 14:34:37 -08001270// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1271type baseLinker struct {
1272 Properties BaseLinkerProperties
1273 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001274 VariantIsShared bool `blueprint:"mutated"`
1275 VariantIsStatic bool `blueprint:"mutated"`
1276 VariantIsStaticBinary bool `blueprint:"mutated"`
1277 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001278 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001279}
1280
Dan Willemsend30e6102016-03-30 17:35:50 -07001281func (linker *baseLinker) begin(ctx BaseModuleContext) {
1282 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001283 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001284 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001285 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001286 }
1287}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001288
Colin Crossca860ac2016-01-04 14:34:37 -08001289func (linker *baseLinker) props() []interface{} {
1290 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001291}
1292
Colin Crossca860ac2016-01-04 14:34:37 -08001293func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1294 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1295 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1296 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001297
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001298 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1299 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1300
Dan Willemsena96ff642016-06-07 12:34:45 -07001301 if !ctx.sdk() && ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001302 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001303 }
1304
Colin Crossf6566ed2015-03-24 11:13:38 -07001305 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001306 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001307 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1308 if !Bool(linker.Properties.No_libgcc) {
1309 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001310 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001311
Colin Crossca860ac2016-01-04 14:34:37 -08001312 if !linker.static() {
1313 if linker.Properties.System_shared_libs != nil {
1314 deps.LateSharedLibs = append(deps.LateSharedLibs,
1315 linker.Properties.System_shared_libs...)
1316 } else if !ctx.sdk() {
1317 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1318 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001319 }
Colin Cross577f6e42015-03-27 18:23:34 -07001320
Colin Crossca860ac2016-01-04 14:34:37 -08001321 if ctx.sdk() {
1322 version := ctx.sdkVersion()
1323 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001324 "ndk_libc."+version,
1325 "ndk_libm."+version,
1326 )
1327 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001328 }
1329
Colin Crossca860ac2016-01-04 14:34:37 -08001330 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001331}
1332
Colin Crossca860ac2016-01-04 14:34:37 -08001333func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1334 toolchain := ctx.toolchain()
1335
Colin Crossca860ac2016-01-04 14:34:37 -08001336 if !ctx.noDefaultCompilerFlags() {
1337 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1338 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1339 }
1340
1341 if flags.Clang {
1342 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1343 } else {
1344 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1345 }
1346
1347 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001348 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1349
Colin Crossca860ac2016-01-04 14:34:37 -08001350 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1351 }
1352 }
1353
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001354 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1355
Dan Willemsen00ced762016-05-10 17:31:21 -07001356 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1357
Dan Willemsend30e6102016-03-30 17:35:50 -07001358 if ctx.Host() && !linker.static() {
1359 rpath_prefix := `\$$ORIGIN/`
1360 if ctx.Darwin() {
1361 rpath_prefix = "@loader_path/"
1362 }
1363
Colin Crossc99deeb2016-04-11 15:06:20 -07001364 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001365 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1366 }
1367 }
1368
Dan Willemsene7174922016-03-30 17:33:52 -07001369 if flags.Clang {
1370 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1371 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001372 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1373 }
1374
1375 return flags
1376}
1377
1378func (linker *baseLinker) static() bool {
1379 return linker.dynamicProperties.VariantIsStatic
1380}
1381
1382func (linker *baseLinker) staticBinary() bool {
1383 return linker.dynamicProperties.VariantIsStaticBinary
1384}
1385
1386func (linker *baseLinker) setStatic(static bool) {
1387 linker.dynamicProperties.VariantIsStatic = static
1388}
1389
Colin Cross16b23492016-01-06 14:41:07 -08001390func (linker *baseLinker) isDependencyRoot() bool {
1391 return false
1392}
1393
Colin Crossca860ac2016-01-04 14:34:37 -08001394type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001395 // Returns true if the build options for the module have selected a static or shared build
1396 buildStatic() bool
1397 buildShared() bool
1398
1399 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001400 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001401
Colin Cross18b6dc52015-04-28 13:20:37 -07001402 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001403 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001404
1405 // Returns whether a module is a static binary
1406 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001407
1408 // Returns true for dependency roots (binaries)
1409 // TODO(ccross): also handle dlopenable libraries
1410 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001411}
1412
Colin Crossca860ac2016-01-04 14:34:37 -08001413type baseInstaller struct {
1414 Properties InstallerProperties
1415
1416 dir string
1417 dir64 string
1418 data bool
1419
Colin Cross635c3b02016-05-18 15:37:25 -07001420 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001421}
1422
1423var _ installer = (*baseInstaller)(nil)
1424
1425func (installer *baseInstaller) props() []interface{} {
1426 return []interface{}{&installer.Properties}
1427}
1428
Colin Cross635c3b02016-05-18 15:37:25 -07001429func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001430 subDir := installer.dir
1431 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1432 subDir = installer.dir64
1433 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001434 if !ctx.Host() && !ctx.Arch().Native {
1435 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1436 }
Colin Cross635c3b02016-05-18 15:37:25 -07001437 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001438 installer.path = ctx.InstallFile(dir, file)
1439}
1440
1441func (installer *baseInstaller) inData() bool {
1442 return installer.data
1443}
1444
Colin Cross3f40fa42015-01-30 17:27:36 -08001445//
1446// Combined static+shared libraries
1447//
1448
Colin Cross919281a2016-04-05 16:42:05 -07001449type flagExporter struct {
1450 Properties FlagExporterProperties
1451
1452 flags []string
1453}
1454
1455func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001456 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1457 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001458}
1459
1460func (f *flagExporter) reexportFlags(flags []string) {
1461 f.flags = append(f.flags, flags...)
1462}
1463
1464func (f *flagExporter) exportedFlags() []string {
1465 return f.flags
1466}
1467
1468type exportedFlagsProducer interface {
1469 exportedFlags() []string
1470}
1471
1472var _ exportedFlagsProducer = (*flagExporter)(nil)
1473
Colin Crossca860ac2016-01-04 14:34:37 -08001474type libraryCompiler struct {
1475 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001476
Colin Crossca860ac2016-01-04 14:34:37 -08001477 linker *libraryLinker
1478 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001479
Colin Crossca860ac2016-01-04 14:34:37 -08001480 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001481 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001482}
1483
Colin Crossca860ac2016-01-04 14:34:37 -08001484var _ compiler = (*libraryCompiler)(nil)
1485
1486func (library *libraryCompiler) props() []interface{} {
1487 props := library.baseCompiler.props()
1488 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001489}
1490
Colin Crossca860ac2016-01-04 14:34:37 -08001491func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1492 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001493
Dan Willemsen490fd492015-11-24 17:53:15 -08001494 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1495 // all code is position independent, and then those warnings get promoted to
1496 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001497 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001498 flags.CFlags = append(flags.CFlags, "-fPIC")
1499 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001500
Colin Crossca860ac2016-01-04 14:34:37 -08001501 if library.linker.static() {
1502 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001503 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001504 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001505 }
1506
Colin Crossca860ac2016-01-04 14:34:37 -08001507 return flags
1508}
1509
Colin Cross635c3b02016-05-18 15:37:25 -07001510func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1511 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001512
Dan Willemsenb40aab62016-04-20 14:21:14 -07001513 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001514 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001515
1516 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001517 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001518 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1519 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001520 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001521 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001522 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1523 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001524 }
1525
1526 return objFiles
1527}
1528
1529type libraryLinker struct {
1530 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001531 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001532 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001533
1534 Properties LibraryLinkerProperties
1535
1536 dynamicProperties struct {
1537 BuildStatic bool `blueprint:"mutated"`
1538 BuildShared bool `blueprint:"mutated"`
1539 }
1540
Colin Crossca860ac2016-01-04 14:34:37 -08001541 // If we're used as a whole_static_lib, our missing dependencies need
1542 // to be given
1543 wholeStaticMissingDeps []string
1544
1545 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001546 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001547}
1548
1549var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001550
1551func (library *libraryLinker) props() []interface{} {
1552 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001553 return append(props,
1554 &library.Properties,
1555 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001556 &library.flagExporter.Properties,
1557 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001558}
1559
1560func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1561 flags = library.baseLinker.flags(ctx, flags)
1562
1563 flags.Nocrt = Bool(library.Properties.Nocrt)
1564
1565 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001566 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001567 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1568 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001569 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001570 sharedFlag = "-shared"
1571 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001572 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001573 flags.LdFlags = append(flags.LdFlags,
1574 "-nostdlib",
1575 "-Wl,--gc-sections",
1576 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001577 }
Colin Cross97ba0732015-03-23 17:50:24 -07001578
Colin Cross0af4b842015-04-30 16:36:18 -07001579 if ctx.Darwin() {
1580 flags.LdFlags = append(flags.LdFlags,
1581 "-dynamiclib",
1582 "-single_module",
1583 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001584 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001585 )
1586 } else {
1587 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001588 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001589 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001590 )
1591 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001592 }
Colin Cross97ba0732015-03-23 17:50:24 -07001593
1594 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001595}
1596
Colin Crossca860ac2016-01-04 14:34:37 -08001597func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1598 deps = library.baseLinker.deps(ctx, deps)
1599 if library.static() {
1600 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1601 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1602 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1603 } else {
1604 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1605 if !ctx.sdk() {
1606 deps.CrtBegin = "crtbegin_so"
1607 deps.CrtEnd = "crtend_so"
1608 } else {
1609 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1610 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1611 }
1612 }
1613 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1614 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1615 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1616 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001617
Colin Crossca860ac2016-01-04 14:34:37 -08001618 return deps
1619}
Colin Cross3f40fa42015-01-30 17:27:36 -08001620
Colin Crossca860ac2016-01-04 14:34:37 -08001621func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001622 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001623
Colin Cross635c3b02016-05-18 15:37:25 -07001624 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001625 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001626
Colin Cross635c3b02016-05-18 15:37:25 -07001627 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001628 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001629
Colin Cross0af4b842015-04-30 16:36:18 -07001630 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001631 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001632 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001633 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001634 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001635
Colin Crossca860ac2016-01-04 14:34:37 -08001636 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001637
1638 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001639
1640 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001641}
1642
Colin Crossca860ac2016-01-04 14:34:37 -08001643func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001644 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001645
Colin Cross635c3b02016-05-18 15:37:25 -07001646 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001647
Colin Cross635c3b02016-05-18 15:37:25 -07001648 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1649 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1650 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1651 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001652 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001653 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001654 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001655 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001656 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001657 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001658 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1659 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001660 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001661 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1662 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001663 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001664 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1665 }
1666 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001667 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001668 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1669 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001670 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001671 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001672 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001673 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001674 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001675 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001676 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001677 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001678 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001679 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001680 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001681 }
Colin Crossaee540a2015-07-06 17:48:31 -07001682 }
1683
Colin Cross665dce92016-04-28 14:50:03 -07001684 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001685 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001686 ret := outputFile
1687
1688 builderFlags := flagsToBuilderFlags(flags)
1689
1690 if library.stripper.needsStrip(ctx) {
1691 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001692 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001693 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1694 }
1695
Colin Crossca860ac2016-01-04 14:34:37 -08001696 sharedLibs := deps.SharedLibs
1697 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001698
Colin Crossca860ac2016-01-04 14:34:37 -08001699 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1700 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001701 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001702
Colin Cross665dce92016-04-28 14:50:03 -07001703 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001704}
1705
Colin Crossca860ac2016-01-04 14:34:37 -08001706func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001707 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001708
Colin Crossc99deeb2016-04-11 15:06:20 -07001709 objFiles = append(objFiles, deps.ObjFiles...)
1710
Colin Cross635c3b02016-05-18 15:37:25 -07001711 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001712 if library.static() {
1713 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001714 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001715 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001716 }
1717
Colin Cross919281a2016-04-05 16:42:05 -07001718 library.exportIncludes(ctx, "-I")
1719 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001720
1721 return out
1722}
1723
1724func (library *libraryLinker) buildStatic() bool {
1725 return library.dynamicProperties.BuildStatic
1726}
1727
1728func (library *libraryLinker) buildShared() bool {
1729 return library.dynamicProperties.BuildShared
1730}
1731
1732func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1733 return library.wholeStaticMissingDeps
1734}
1735
Colin Crossc99deeb2016-04-11 15:06:20 -07001736func (library *libraryLinker) installable() bool {
1737 return !library.static()
1738}
1739
Colin Crossca860ac2016-01-04 14:34:37 -08001740type libraryInstaller struct {
1741 baseInstaller
1742
Colin Cross30d5f512016-05-03 18:02:42 -07001743 linker *libraryLinker
1744 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001745}
1746
Colin Cross635c3b02016-05-18 15:37:25 -07001747func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001748 if !library.linker.static() {
1749 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001750 }
1751}
1752
Colin Cross30d5f512016-05-03 18:02:42 -07001753func (library *libraryInstaller) inData() bool {
1754 return library.baseInstaller.inData() || library.sanitize.inData()
1755}
1756
Colin Cross635c3b02016-05-18 15:37:25 -07001757func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1758 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001759
Colin Crossca860ac2016-01-04 14:34:37 -08001760 linker := &libraryLinker{}
1761 linker.dynamicProperties.BuildShared = shared
1762 linker.dynamicProperties.BuildStatic = static
1763 module.linker = linker
1764
1765 module.compiler = &libraryCompiler{
1766 linker: linker,
1767 }
1768 module.installer = &libraryInstaller{
1769 baseInstaller: baseInstaller{
1770 dir: "lib",
1771 dir64: "lib64",
1772 },
Colin Cross30d5f512016-05-03 18:02:42 -07001773 linker: linker,
1774 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001775 }
1776
Colin Crossca860ac2016-01-04 14:34:37 -08001777 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001778}
1779
Colin Crossca860ac2016-01-04 14:34:37 -08001780func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001781 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001782 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001783}
1784
Colin Cross3f40fa42015-01-30 17:27:36 -08001785//
1786// Objects (for crt*.o)
1787//
1788
Colin Crossca860ac2016-01-04 14:34:37 -08001789type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001790 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001791}
1792
Colin Crossca860ac2016-01-04 14:34:37 -08001793func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001794 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001795 module.compiler = &baseCompiler{}
1796 module.linker = &objectLinker{}
1797 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001798}
1799
Colin Cross81413472016-04-11 14:37:39 -07001800func (object *objectLinker) props() []interface{} {
1801 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001802}
1803
Colin Crossca860ac2016-01-04 14:34:37 -08001804func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001805
Colin Cross81413472016-04-11 14:37:39 -07001806func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1807 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001808 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001809}
1810
Colin Crossca860ac2016-01-04 14:34:37 -08001811func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001812 if flags.Clang {
1813 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1814 } else {
1815 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1816 }
1817
Colin Crossca860ac2016-01-04 14:34:37 -08001818 return flags
1819}
1820
1821func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001822 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001823
Colin Cross97ba0732015-03-23 17:50:24 -07001824 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001825
Colin Cross635c3b02016-05-18 15:37:25 -07001826 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001827 if len(objFiles) == 1 {
1828 outputFile = objFiles[0]
1829 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001830 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001831 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001832 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001833 }
1834
Colin Cross3f40fa42015-01-30 17:27:36 -08001835 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001836 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001837}
1838
Colin Crossc99deeb2016-04-11 15:06:20 -07001839func (*objectLinker) installable() bool {
1840 return false
1841}
1842
Colin Cross3f40fa42015-01-30 17:27:36 -08001843//
1844// Executables
1845//
1846
Colin Crossca860ac2016-01-04 14:34:37 -08001847type binaryLinker struct {
1848 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001849 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001850
Colin Crossca860ac2016-01-04 14:34:37 -08001851 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001852
Colin Cross635c3b02016-05-18 15:37:25 -07001853 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001854}
1855
Colin Crossca860ac2016-01-04 14:34:37 -08001856var _ linker = (*binaryLinker)(nil)
1857
1858func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001859 return append(binary.baseLinker.props(),
1860 &binary.Properties,
1861 &binary.stripper.StripProperties)
1862
Colin Cross3f40fa42015-01-30 17:27:36 -08001863}
1864
Colin Crossca860ac2016-01-04 14:34:37 -08001865func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001866 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001867}
1868
Colin Crossca860ac2016-01-04 14:34:37 -08001869func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001870 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001871}
1872
Colin Crossca860ac2016-01-04 14:34:37 -08001873func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001874 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001875 if binary.Properties.Stem != "" {
1876 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001877 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001878
Colin Crossca860ac2016-01-04 14:34:37 -08001879 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001880}
1881
Colin Crossca860ac2016-01-04 14:34:37 -08001882func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1883 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001884 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001885 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001886 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001887 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001888 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001889 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001890 }
Colin Crossca860ac2016-01-04 14:34:37 -08001891 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001892 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001893 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001894 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001895 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001896 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001897 }
Colin Crossca860ac2016-01-04 14:34:37 -08001898 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001899 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001900
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001901 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001902 if inList("libc++_static", deps.StaticLibs) {
1903 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001904 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001905 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1906 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1907 // move them to the beginning of deps.LateStaticLibs
1908 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001909 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001910 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001911 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001912 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001913 }
Colin Crossca860ac2016-01-04 14:34:37 -08001914
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001915 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001916 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1917 "from static libs or set static_executable: true")
1918 }
1919 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001920}
1921
Colin Crossc99deeb2016-04-11 15:06:20 -07001922func (*binaryLinker) installable() bool {
1923 return true
1924}
1925
Colin Cross16b23492016-01-06 14:41:07 -08001926func (binary *binaryLinker) isDependencyRoot() bool {
1927 return true
1928}
1929
Colin Cross635c3b02016-05-18 15:37:25 -07001930func NewBinary(hod android.HostOrDeviceSupported) *Module {
1931 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001932 module.compiler = &baseCompiler{}
1933 module.linker = &binaryLinker{}
1934 module.installer = &baseInstaller{
1935 dir: "bin",
1936 }
1937 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001938}
1939
Colin Crossca860ac2016-01-04 14:34:37 -08001940func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001941 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001942 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001943}
1944
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001945func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1946 binary.baseLinker.begin(ctx)
1947
1948 static := Bool(binary.Properties.Static_executable)
1949 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001950 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001951 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1952 static = true
1953 }
1954 } else {
1955 // Static executables are not supported on Darwin or Windows
1956 static = false
1957 }
Colin Cross0af4b842015-04-30 16:36:18 -07001958 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001959 if static {
1960 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001961 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001962 }
1963}
1964
Colin Crossca860ac2016-01-04 14:34:37 -08001965func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1966 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001967
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001968 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001969 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07001970 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001971 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1972 }
1973 }
1974
1975 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1976 // all code is position independent, and then those warnings get promoted to
1977 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001978 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001979 flags.CFlags = append(flags.CFlags, "-fpie")
1980 }
Colin Cross97ba0732015-03-23 17:50:24 -07001981
Colin Crossf6566ed2015-03-24 11:13:38 -07001982 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001983 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001984 // Clang driver needs -static to create static executable.
1985 // However, bionic/linker uses -shared to overwrite.
1986 // Linker for x86 targets does not allow coexistance of -static and -shared,
1987 // so we add -static only if -shared is not used.
1988 if !inList("-shared", flags.LdFlags) {
1989 flags.LdFlags = append(flags.LdFlags, "-static")
1990 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001991
Colin Crossed4cf0b2015-03-26 14:43:45 -07001992 flags.LdFlags = append(flags.LdFlags,
1993 "-nostdlib",
1994 "-Bstatic",
1995 "-Wl,--gc-sections",
1996 )
1997
1998 } else {
Colin Cross16b23492016-01-06 14:41:07 -08001999 if flags.DynamicLinker == "" {
2000 flags.DynamicLinker = "/system/bin/linker"
2001 if flags.Toolchain.Is64Bit() {
2002 flags.DynamicLinker += "64"
2003 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002004 }
2005
2006 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08002007 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002008 "-nostdlib",
2009 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07002010 "-Wl,--gc-sections",
2011 "-Wl,-z,nocopyreloc",
2012 )
2013 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07002014 } else {
2015 if binary.staticBinary() {
2016 flags.LdFlags = append(flags.LdFlags, "-static")
2017 }
2018 if ctx.Darwin() {
2019 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
2020 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002021 }
2022
Colin Cross97ba0732015-03-23 17:50:24 -07002023 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08002024}
2025
Colin Crossca860ac2016-01-04 14:34:37 -08002026func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002027 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002028
Colin Cross665dce92016-04-28 14:50:03 -07002029 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07002030 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002031 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002032 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002033 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002034 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002035
Colin Cross635c3b02016-05-18 15:37:25 -07002036 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002037
Colin Crossca860ac2016-01-04 14:34:37 -08002038 sharedLibs := deps.SharedLibs
2039 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2040
Colin Cross16b23492016-01-06 14:41:07 -08002041 if flags.DynamicLinker != "" {
2042 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2043 }
2044
Colin Cross665dce92016-04-28 14:50:03 -07002045 builderFlags := flagsToBuilderFlags(flags)
2046
2047 if binary.stripper.needsStrip(ctx) {
2048 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002049 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002050 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2051 }
2052
2053 if binary.Properties.Prefix_symbols != "" {
2054 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002055 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002056 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2057 flagsToBuilderFlags(flags), afterPrefixSymbols)
2058 }
2059
Colin Crossca860ac2016-01-04 14:34:37 -08002060 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002061 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002062 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002063
2064 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002065}
Colin Cross3f40fa42015-01-30 17:27:36 -08002066
Colin Cross635c3b02016-05-18 15:37:25 -07002067func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002068 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002069}
2070
Colin Cross665dce92016-04-28 14:50:03 -07002071type stripper struct {
2072 StripProperties StripProperties
2073}
2074
2075func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2076 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2077}
2078
Colin Cross635c3b02016-05-18 15:37:25 -07002079func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002080 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002081 if ctx.Darwin() {
2082 TransformDarwinStrip(ctx, in, out)
2083 } else {
2084 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2085 // TODO(ccross): don't add gnu debuglink for user builds
2086 flags.stripAddGnuDebuglink = true
2087 TransformStrip(ctx, in, out, flags)
2088 }
Colin Cross665dce92016-04-28 14:50:03 -07002089}
2090
Colin Cross635c3b02016-05-18 15:37:25 -07002091func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002092 if m, ok := mctx.Module().(*Module); ok {
2093 if test, ok := m.linker.(*testLinker); ok {
2094 if Bool(test.Properties.Test_per_src) {
2095 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2096 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2097 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2098 }
2099 tests := mctx.CreateLocalVariations(testNames...)
2100 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2101 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2102 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2103 }
Colin Cross6002e052015-09-16 16:00:08 -07002104 }
2105 }
2106 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002107}
2108
Colin Crossca860ac2016-01-04 14:34:37 -08002109type testLinker struct {
2110 binaryLinker
2111 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002112}
2113
Dan Willemsend30e6102016-03-30 17:35:50 -07002114func (test *testLinker) begin(ctx BaseModuleContext) {
2115 test.binaryLinker.begin(ctx)
2116
2117 runpath := "../../lib"
2118 if ctx.toolchain().Is64Bit() {
2119 runpath += "64"
2120 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002121 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002122}
2123
Colin Crossca860ac2016-01-04 14:34:37 -08002124func (test *testLinker) props() []interface{} {
2125 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002126}
2127
Colin Crossca860ac2016-01-04 14:34:37 -08002128func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2129 flags = test.binaryLinker.flags(ctx, flags)
2130
2131 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002132 return flags
2133 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002134
Colin Cross97ba0732015-03-23 17:50:24 -07002135 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002136 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002137 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002138
Colin Crossa1ad8d12016-06-01 17:09:44 -07002139 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002140 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002141 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002142 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002143 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2144 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002145 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002146 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2147 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002148 }
2149 } else {
2150 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002151 }
2152
Colin Cross21b9a242015-03-24 14:15:58 -07002153 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002154}
2155
Colin Crossca860ac2016-01-04 14:34:37 -08002156func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2157 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002158 if ctx.sdk() && ctx.Device() {
2159 switch ctx.selectedStl() {
2160 case "ndk_libc++_shared", "ndk_libc++_static":
2161 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2162 case "ndk_libgnustl_static":
2163 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2164 default:
2165 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2166 }
2167 } else {
2168 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2169 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002170 }
Colin Crossca860ac2016-01-04 14:34:37 -08002171 deps = test.binaryLinker.deps(ctx, deps)
2172 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002173}
2174
Colin Crossca860ac2016-01-04 14:34:37 -08002175type testInstaller struct {
2176 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002177}
2178
Colin Cross635c3b02016-05-18 15:37:25 -07002179func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002180 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2181 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2182 installer.baseInstaller.install(ctx, file)
2183}
2184
Colin Cross635c3b02016-05-18 15:37:25 -07002185func NewTest(hod android.HostOrDeviceSupported) *Module {
2186 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002187 module.compiler = &baseCompiler{}
2188 linker := &testLinker{}
2189 linker.Properties.Gtest = true
2190 module.linker = linker
2191 module.installer = &testInstaller{
2192 baseInstaller: baseInstaller{
2193 dir: "nativetest",
2194 dir64: "nativetest64",
2195 data: true,
2196 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002197 }
Colin Crossca860ac2016-01-04 14:34:37 -08002198 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002199}
2200
Colin Crossca860ac2016-01-04 14:34:37 -08002201func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002202 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002203 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002204}
2205
Colin Crossca860ac2016-01-04 14:34:37 -08002206type benchmarkLinker struct {
2207 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002208}
2209
Colin Crossca860ac2016-01-04 14:34:37 -08002210func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2211 deps = benchmark.binaryLinker.deps(ctx, deps)
2212 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2213 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002214}
2215
Colin Cross635c3b02016-05-18 15:37:25 -07002216func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2217 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002218 module.compiler = &baseCompiler{}
2219 module.linker = &benchmarkLinker{}
2220 module.installer = &baseInstaller{
2221 dir: "nativetest",
2222 dir64: "nativetest64",
2223 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002224 }
Colin Crossca860ac2016-01-04 14:34:37 -08002225 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002226}
2227
Colin Crossca860ac2016-01-04 14:34:37 -08002228func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002229 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002230 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002231}
2232
Colin Cross3f40fa42015-01-30 17:27:36 -08002233//
2234// Static library
2235//
2236
Colin Crossca860ac2016-01-04 14:34:37 -08002237func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002238 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002239 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002240}
2241
2242//
2243// Shared libraries
2244//
2245
Colin Crossca860ac2016-01-04 14:34:37 -08002246func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002247 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002248 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002249}
2250
2251//
2252// Host static library
2253//
2254
Colin Crossca860ac2016-01-04 14:34:37 -08002255func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002256 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002257 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002258}
2259
2260//
2261// Host Shared libraries
2262//
2263
Colin Crossca860ac2016-01-04 14:34:37 -08002264func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002265 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002266 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002267}
2268
2269//
2270// Host Binaries
2271//
2272
Colin Crossca860ac2016-01-04 14:34:37 -08002273func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002274 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002275 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002276}
2277
2278//
Colin Cross1f8f2342015-03-26 16:09:47 -07002279// Host Tests
2280//
2281
Colin Crossca860ac2016-01-04 14:34:37 -08002282func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002283 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002284 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002285}
2286
2287//
Colin Cross2ba19d92015-05-07 15:44:20 -07002288// Host Benchmarks
2289//
2290
Colin Crossca860ac2016-01-04 14:34:37 -08002291func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002292 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002293 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002294}
2295
2296//
Colin Crosscfad1192015-11-02 16:43:11 -08002297// Defaults
2298//
Colin Crossca860ac2016-01-04 14:34:37 -08002299type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002300 android.ModuleBase
2301 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002302}
2303
Colin Cross635c3b02016-05-18 15:37:25 -07002304func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002305}
2306
Colin Crossca860ac2016-01-04 14:34:37 -08002307func defaultsFactory() (blueprint.Module, []interface{}) {
2308 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002309
2310 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002311 &BaseProperties{},
2312 &BaseCompilerProperties{},
2313 &BaseLinkerProperties{},
2314 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002315 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002316 &LibraryLinkerProperties{},
2317 &BinaryLinkerProperties{},
2318 &TestLinkerProperties{},
2319 &UnusedProperties{},
2320 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002321 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002322 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002323 }
2324
Colin Cross635c3b02016-05-18 15:37:25 -07002325 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2326 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002327
Colin Cross635c3b02016-05-18 15:37:25 -07002328 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002329}
2330
2331//
Colin Cross3f40fa42015-01-30 17:27:36 -08002332// Device libraries shipped with gcc
2333//
2334
Colin Crossca860ac2016-01-04 14:34:37 -08002335type toolchainLibraryLinker struct {
2336 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002337}
2338
Colin Crossca860ac2016-01-04 14:34:37 -08002339var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2340
2341func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002342 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002343 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002344}
2345
Colin Crossca860ac2016-01-04 14:34:37 -08002346func (*toolchainLibraryLinker) buildStatic() bool {
2347 return true
2348}
Colin Cross3f40fa42015-01-30 17:27:36 -08002349
Colin Crossca860ac2016-01-04 14:34:37 -08002350func (*toolchainLibraryLinker) buildShared() bool {
2351 return false
2352}
2353
2354func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002355 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002356 module.compiler = &baseCompiler{}
2357 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002358 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002359 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002360}
2361
Colin Crossca860ac2016-01-04 14:34:37 -08002362func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002363 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002364
2365 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002366 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002367
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002368 if flags.Clang {
2369 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2370 }
2371
Colin Crossca860ac2016-01-04 14:34:37 -08002372 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002373
2374 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002375
Colin Crossca860ac2016-01-04 14:34:37 -08002376 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002377}
2378
Colin Crossc99deeb2016-04-11 15:06:20 -07002379func (*toolchainLibraryLinker) installable() bool {
2380 return false
2381}
2382
Dan Albertbe961682015-03-18 23:38:50 -07002383// NDK prebuilt libraries.
2384//
2385// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2386// either (with the exception of the shared STLs, which are installed to the app's directory rather
2387// than to the system image).
2388
Colin Cross635c3b02016-05-18 15:37:25 -07002389func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002390 suffix := ""
2391 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2392 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002393 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002394 suffix = "64"
2395 }
Colin Cross635c3b02016-05-18 15:37:25 -07002396 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002397 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002398}
2399
Colin Cross635c3b02016-05-18 15:37:25 -07002400func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2401 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002402
2403 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2404 // We want to translate to just NAME.EXT
2405 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2406 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002407 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002408}
2409
Colin Crossca860ac2016-01-04 14:34:37 -08002410type ndkPrebuiltObjectLinker struct {
2411 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002412}
2413
Colin Crossca860ac2016-01-04 14:34:37 -08002414func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002415 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002416 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002417}
2418
Colin Crossca860ac2016-01-04 14:34:37 -08002419func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002420 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002421 module.linker = &ndkPrebuiltObjectLinker{}
2422 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002423}
2424
Colin Crossca860ac2016-01-04 14:34:37 -08002425func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002426 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002427 // A null build step, but it sets up the output path.
2428 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2429 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2430 }
2431
Colin Crossca860ac2016-01-04 14:34:37 -08002432 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002433}
2434
Colin Crossca860ac2016-01-04 14:34:37 -08002435type ndkPrebuiltLibraryLinker struct {
2436 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002437}
2438
Colin Crossca860ac2016-01-04 14:34:37 -08002439var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2440var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002441
Colin Crossca860ac2016-01-04 14:34:37 -08002442func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002443 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002444}
2445
Colin Crossca860ac2016-01-04 14:34:37 -08002446func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002447 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002448 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002449}
2450
Colin Crossca860ac2016-01-04 14:34:37 -08002451func ndkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002452 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002453 linker := &ndkPrebuiltLibraryLinker{}
2454 linker.dynamicProperties.BuildShared = true
2455 module.linker = linker
2456 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002457}
2458
Colin Crossca860ac2016-01-04 14:34:37 -08002459func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002460 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002461 // A null build step, but it sets up the output path.
2462 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2463 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2464 }
2465
Colin Cross919281a2016-04-05 16:42:05 -07002466 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002467
Colin Crossca860ac2016-01-04 14:34:37 -08002468 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2469 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002470}
2471
2472// The NDK STLs are slightly different from the prebuilt system libraries:
2473// * Are not specific to each platform version.
2474// * The libraries are not in a predictable location for each STL.
2475
Colin Crossca860ac2016-01-04 14:34:37 -08002476type ndkPrebuiltStlLinker struct {
2477 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002478}
2479
Colin Crossca860ac2016-01-04 14:34:37 -08002480func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002481 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002482 linker := &ndkPrebuiltStlLinker{}
2483 linker.dynamicProperties.BuildShared = true
2484 module.linker = linker
2485 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002486}
2487
Colin Crossca860ac2016-01-04 14:34:37 -08002488func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002489 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002490 linker := &ndkPrebuiltStlLinker{}
2491 linker.dynamicProperties.BuildStatic = true
2492 module.linker = linker
2493 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002494}
2495
Colin Cross635c3b02016-05-18 15:37:25 -07002496func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002497 gccVersion := toolchain.GccVersion()
2498 var libDir string
2499 switch stl {
2500 case "libstlport":
2501 libDir = "cxx-stl/stlport/libs"
2502 case "libc++":
2503 libDir = "cxx-stl/llvm-libc++/libs"
2504 case "libgnustl":
2505 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2506 }
2507
2508 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002509 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002510 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002511 }
2512
2513 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002514 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002515}
2516
Colin Crossca860ac2016-01-04 14:34:37 -08002517func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002518 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002519 // A null build step, but it sets up the output path.
2520 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2521 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2522 }
2523
Colin Cross919281a2016-04-05 16:42:05 -07002524 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002525
2526 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002527 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002528 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002529 libExt = staticLibraryExtension
2530 }
2531
2532 stlName := strings.TrimSuffix(libName, "_shared")
2533 stlName = strings.TrimSuffix(stlName, "_static")
2534 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002535 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002536}
2537
Colin Cross635c3b02016-05-18 15:37:25 -07002538func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002539 if m, ok := mctx.Module().(*Module); ok {
2540 if m.linker != nil {
2541 if linker, ok := m.linker.(baseLinkerInterface); ok {
2542 var modules []blueprint.Module
2543 if linker.buildStatic() && linker.buildShared() {
2544 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002545 static := modules[0].(*Module)
2546 shared := modules[1].(*Module)
2547
2548 static.linker.(baseLinkerInterface).setStatic(true)
2549 shared.linker.(baseLinkerInterface).setStatic(false)
2550
2551 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2552 sharedCompiler := shared.compiler.(*libraryCompiler)
2553 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2554 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2555 // Optimize out compiling common .o files twice for static+shared libraries
2556 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2557 sharedCompiler.baseCompiler.Properties.Srcs = nil
2558 }
2559 }
Colin Crossca860ac2016-01-04 14:34:37 -08002560 } else if linker.buildStatic() {
2561 modules = mctx.CreateLocalVariations("static")
2562 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2563 } else if linker.buildShared() {
2564 modules = mctx.CreateLocalVariations("shared")
2565 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2566 } else {
2567 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2568 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002569 }
2570 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002571 }
2572}
Colin Cross74d1ec02015-04-28 13:30:13 -07002573
2574// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2575// modifies the slice contents in place, and returns a subslice of the original slice
2576func lastUniqueElements(list []string) []string {
2577 totalSkip := 0
2578 for i := len(list) - 1; i >= totalSkip; i-- {
2579 skip := 0
2580 for j := i - 1; j >= totalSkip; j-- {
2581 if list[i] == list[j] {
2582 skip++
2583 } else {
2584 list[j+skip] = list[j]
2585 }
2586 }
2587 totalSkip += skip
2588 }
2589 return list[totalSkip:]
2590}
Colin Cross06a931b2015-10-28 17:23:31 -07002591
2592var Bool = proptools.Bool