blob: 9acc48e8f7306257003bfc399fdbc4c4550fe77c [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 {
639 return ctx.mod.Properties.Sdk_version != ""
640}
641
642func (ctx *moduleContextImpl) sdkVersion() string {
643 return ctx.mod.Properties.Sdk_version
644}
645
Dan Willemsen8146b2f2016-03-30 21:00:30 -0700646func (ctx *moduleContextImpl) selectedStl() string {
647 if stl := ctx.mod.stl; stl != nil {
648 return stl.Properties.SelectedStl
649 }
650 return ""
651}
652
Colin Cross635c3b02016-05-18 15:37:25 -0700653func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800654 return &Module{
655 hod: hod,
656 multilib: multilib,
657 }
658}
659
Colin Cross635c3b02016-05-18 15:37:25 -0700660func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
Colin Crossca860ac2016-01-04 14:34:37 -0800661 module := newBaseModule(hod, multilib)
Colin Crossa8e07cc2016-04-04 15:07:06 -0700662 module.stl = &stl{}
Colin Cross16b23492016-01-06 14:41:07 -0800663 module.sanitize = &sanitize{}
Colin Crossca860ac2016-01-04 14:34:37 -0800664 return module
665}
666
Colin Cross635c3b02016-05-18 15:37:25 -0700667func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800668 ctx := &moduleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700669 ModuleContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800670 moduleContextImpl: moduleContextImpl{
671 mod: c,
672 },
673 }
674 ctx.ctx = ctx
675
676 flags := Flags{
677 Toolchain: c.toolchain(ctx),
678 Clang: c.clang(ctx),
679 }
Colin Crossca860ac2016-01-04 14:34:37 -0800680 if c.compiler != nil {
681 flags = c.compiler.flags(ctx, flags)
682 }
683 if c.linker != nil {
684 flags = c.linker.flags(ctx, flags)
685 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700686 if c.stl != nil {
687 flags = c.stl.flags(ctx, flags)
688 }
Colin Cross16b23492016-01-06 14:41:07 -0800689 if c.sanitize != nil {
690 flags = c.sanitize.flags(ctx, flags)
691 }
Colin Crossca860ac2016-01-04 14:34:37 -0800692 for _, feature := range c.features {
693 flags = feature.flags(ctx, flags)
694 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800695 if ctx.Failed() {
696 return
697 }
698
Colin Crossca860ac2016-01-04 14:34:37 -0800699 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
700 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
701 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800702
Colin Crossca860ac2016-01-04 14:34:37 -0800703 // Optimization to reduce size of build.ninja
704 // Replace the long list of flags for each file with a module-local variable
705 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
706 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
707 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
708 flags.CFlags = []string{"$cflags"}
709 flags.CppFlags = []string{"$cppflags"}
710 flags.AsFlags = []string{"$asflags"}
711
Colin Crossc99deeb2016-04-11 15:06:20 -0700712 deps := c.depsToPaths(ctx)
Colin Cross3f40fa42015-01-30 17:27:36 -0800713 if ctx.Failed() {
714 return
715 }
716
Colin Cross28344522015-04-22 13:07:53 -0700717 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700718
Colin Cross635c3b02016-05-18 15:37:25 -0700719 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -0800720 if c.compiler != nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700721 objFiles = c.compiler.compile(ctx, flags, deps)
Colin Crossca860ac2016-01-04 14:34:37 -0800722 if ctx.Failed() {
723 return
724 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800725 }
726
Colin Crossca860ac2016-01-04 14:34:37 -0800727 if c.linker != nil {
728 outputFile := c.linker.link(ctx, flags, deps, objFiles)
729 if ctx.Failed() {
730 return
731 }
Colin Cross635c3b02016-05-18 15:37:25 -0700732 c.outputFile = android.OptionalPathForPath(outputFile)
Colin Cross5049f022015-03-18 13:28:46 -0700733
Colin Crossc99deeb2016-04-11 15:06:20 -0700734 if c.installer != nil && c.linker.installable() {
Colin Crossca860ac2016-01-04 14:34:37 -0800735 c.installer.install(ctx, outputFile)
736 if ctx.Failed() {
737 return
738 }
739 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700740 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800741}
742
Colin Crossca860ac2016-01-04 14:34:37 -0800743func (c *Module) toolchain(ctx BaseModuleContext) Toolchain {
744 if c.cachedToolchain == nil {
745 arch := ctx.Arch()
Colin Crossa1ad8d12016-06-01 17:09:44 -0700746 os := ctx.Os()
747 factory := toolchainFactories[os][arch.ArchType]
Colin Crossca860ac2016-01-04 14:34:37 -0800748 if factory == nil {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700749 ctx.ModuleErrorf("Toolchain not found for %s arch %q", os.String(), arch.String())
Colin Crossca860ac2016-01-04 14:34:37 -0800750 return nil
751 }
752 c.cachedToolchain = factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800753 }
Colin Crossca860ac2016-01-04 14:34:37 -0800754 return c.cachedToolchain
Colin Cross3f40fa42015-01-30 17:27:36 -0800755}
756
Colin Crossca860ac2016-01-04 14:34:37 -0800757func (c *Module) begin(ctx BaseModuleContext) {
758 if c.compiler != nil {
759 c.compiler.begin(ctx)
Colin Cross21b9a242015-03-24 14:15:58 -0700760 }
Colin Crossca860ac2016-01-04 14:34:37 -0800761 if c.linker != nil {
762 c.linker.begin(ctx)
763 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700764 if c.stl != nil {
765 c.stl.begin(ctx)
766 }
Colin Cross16b23492016-01-06 14:41:07 -0800767 if c.sanitize != nil {
768 c.sanitize.begin(ctx)
769 }
Colin Crossca860ac2016-01-04 14:34:37 -0800770 for _, feature := range c.features {
771 feature.begin(ctx)
772 }
773}
774
Colin Crossc99deeb2016-04-11 15:06:20 -0700775func (c *Module) deps(ctx BaseModuleContext) Deps {
776 deps := Deps{}
777
778 if c.compiler != nil {
779 deps = c.compiler.deps(ctx, deps)
780 }
781 if c.linker != nil {
782 deps = c.linker.deps(ctx, deps)
783 }
Colin Crossa8e07cc2016-04-04 15:07:06 -0700784 if c.stl != nil {
785 deps = c.stl.deps(ctx, deps)
786 }
Colin Cross16b23492016-01-06 14:41:07 -0800787 if c.sanitize != nil {
788 deps = c.sanitize.deps(ctx, deps)
789 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700790 for _, feature := range c.features {
791 deps = feature.deps(ctx, deps)
792 }
793
794 deps.WholeStaticLibs = lastUniqueElements(deps.WholeStaticLibs)
795 deps.StaticLibs = lastUniqueElements(deps.StaticLibs)
796 deps.LateStaticLibs = lastUniqueElements(deps.LateStaticLibs)
797 deps.SharedLibs = lastUniqueElements(deps.SharedLibs)
798 deps.LateSharedLibs = lastUniqueElements(deps.LateSharedLibs)
799
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700800 for _, lib := range deps.ReexportSharedLibHeaders {
801 if !inList(lib, deps.SharedLibs) {
802 ctx.PropertyErrorf("export_shared_lib_headers", "Shared library not in shared_libs: '%s'", lib)
803 }
804 }
805
806 for _, lib := range deps.ReexportStaticLibHeaders {
807 if !inList(lib, deps.StaticLibs) {
808 ctx.PropertyErrorf("export_static_lib_headers", "Static library not in static_libs: '%s'", lib)
809 }
810 }
811
Colin Crossc99deeb2016-04-11 15:06:20 -0700812 return deps
813}
814
Colin Cross635c3b02016-05-18 15:37:25 -0700815func (c *Module) depsMutator(actx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800816 ctx := &baseModuleContext{
Colin Cross635c3b02016-05-18 15:37:25 -0700817 BaseContext: actx,
Colin Crossca860ac2016-01-04 14:34:37 -0800818 moduleContextImpl: moduleContextImpl{
819 mod: c,
820 },
821 }
822 ctx.ctx = ctx
823
824 if c.customizer != nil {
825 c.customizer.CustomizeProperties(ctx)
826 }
827
828 c.begin(ctx)
829
Colin Crossc99deeb2016-04-11 15:06:20 -0700830 deps := c.deps(ctx)
Colin Crossca860ac2016-01-04 14:34:37 -0800831
Colin Crossc99deeb2016-04-11 15:06:20 -0700832 c.Properties.AndroidMkSharedLibs = deps.SharedLibs
833
834 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, wholeStaticDepTag,
835 deps.WholeStaticLibs...)
836
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700837 for _, lib := range deps.StaticLibs {
838 depTag := staticDepTag
839 if inList(lib, deps.ReexportStaticLibHeaders) {
840 depTag = staticExportDepTag
841 }
842 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, depTag,
843 deps.StaticLibs...)
844 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700845
846 actx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, lateStaticDepTag,
847 deps.LateStaticLibs...)
848
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700849 for _, lib := range deps.SharedLibs {
850 depTag := sharedDepTag
851 if inList(lib, deps.ReexportSharedLibHeaders) {
852 depTag = sharedExportDepTag
853 }
854 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, depTag,
855 deps.SharedLibs...)
856 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700857
858 actx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, lateSharedDepTag,
859 deps.LateSharedLibs...)
860
Dan Willemsenb40aab62016-04-20 14:21:14 -0700861 actx.AddDependency(ctx.module(), genSourceDepTag, deps.GeneratedSources...)
862 actx.AddDependency(ctx.module(), genHeaderDepTag, deps.GeneratedHeaders...)
863
Colin Crossc99deeb2016-04-11 15:06:20 -0700864 actx.AddDependency(ctx.module(), objDepTag, deps.ObjFiles...)
865
866 if deps.CrtBegin != "" {
867 actx.AddDependency(ctx.module(), crtBeginDepTag, deps.CrtBegin)
Colin Crossca860ac2016-01-04 14:34:37 -0800868 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700869 if deps.CrtEnd != "" {
870 actx.AddDependency(ctx.module(), crtEndDepTag, deps.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700871 }
Colin Cross6362e272015-10-29 15:25:03 -0700872}
Colin Cross21b9a242015-03-24 14:15:58 -0700873
Colin Cross635c3b02016-05-18 15:37:25 -0700874func depsMutator(ctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -0800875 if c, ok := ctx.Module().(*Module); ok {
Colin Cross6362e272015-10-29 15:25:03 -0700876 c.depsMutator(ctx)
877 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800878}
879
Colin Crossca860ac2016-01-04 14:34:37 -0800880func (c *Module) clang(ctx BaseModuleContext) bool {
881 clang := Bool(c.Properties.Clang)
882
883 if c.Properties.Clang == nil {
884 if ctx.Host() {
885 clang = true
886 }
887
888 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
889 clang = true
890 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800891 }
Colin Cross28344522015-04-22 13:07:53 -0700892
Colin Crossca860ac2016-01-04 14:34:37 -0800893 if !c.toolchain(ctx).ClangSupported() {
894 clang = false
895 }
896
897 return clang
898}
899
Colin Crossc99deeb2016-04-11 15:06:20 -0700900// Convert dependencies to paths. Returns a PathDeps containing paths
Colin Cross635c3b02016-05-18 15:37:25 -0700901func (c *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
Colin Crossca860ac2016-01-04 14:34:37 -0800902 var depPaths PathDeps
Colin Crossca860ac2016-01-04 14:34:37 -0800903
Colin Crossc99deeb2016-04-11 15:06:20 -0700904 ctx.VisitDirectDeps(func(m blueprint.Module) {
905 name := ctx.OtherModuleName(m)
906 tag := ctx.OtherModuleDependencyTag(m)
Colin Crossca860ac2016-01-04 14:34:37 -0800907
Colin Cross635c3b02016-05-18 15:37:25 -0700908 a, _ := m.(android.Module)
Colin Crossc99deeb2016-04-11 15:06:20 -0700909 if a == nil {
910 ctx.ModuleErrorf("module %q not an android module", name)
911 return
Colin Crossca860ac2016-01-04 14:34:37 -0800912 }
Colin Crossca860ac2016-01-04 14:34:37 -0800913
Colin Crossc99deeb2016-04-11 15:06:20 -0700914 c, _ := m.(*Module)
915 if c == nil {
Dan Willemsenb40aab62016-04-20 14:21:14 -0700916 switch tag {
Colin Cross635c3b02016-05-18 15:37:25 -0700917 case android.DefaultsDepTag:
Dan Willemsenb40aab62016-04-20 14:21:14 -0700918 case genSourceDepTag:
919 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
920 depPaths.GeneratedSources = append(depPaths.GeneratedSources,
921 genRule.GeneratedSourceFiles()...)
922 } else {
923 ctx.ModuleErrorf("module %q is not a gensrcs or genrule", name)
924 }
925 case genHeaderDepTag:
926 if genRule, ok := m.(genrule.SourceFileGenerator); ok {
927 depPaths.GeneratedHeaders = append(depPaths.GeneratedHeaders,
928 genRule.GeneratedSourceFiles()...)
929 depPaths.Cflags = append(depPaths.Cflags,
Colin Cross635c3b02016-05-18 15:37:25 -0700930 includeDirsToFlags(android.Paths{genRule.GeneratedHeaderDir()}))
Dan Willemsenb40aab62016-04-20 14:21:14 -0700931 } else {
932 ctx.ModuleErrorf("module %q is not a genrule", name)
933 }
934 default:
Colin Crossc99deeb2016-04-11 15:06:20 -0700935 ctx.ModuleErrorf("depends on non-cc module %q", name)
Colin Crossca860ac2016-01-04 14:34:37 -0800936 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700937 return
938 }
939
940 if !a.Enabled() {
941 ctx.ModuleErrorf("depends on disabled module %q", name)
942 return
943 }
944
Colin Crossa1ad8d12016-06-01 17:09:44 -0700945 if a.Target().Os != ctx.Os() {
946 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), name)
947 return
948 }
949
950 if a.Target().Arch.ArchType != ctx.Arch().ArchType {
951 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), name)
Colin Crossc99deeb2016-04-11 15:06:20 -0700952 return
953 }
954
955 if !c.outputFile.Valid() {
956 ctx.ModuleErrorf("module %q missing output file", name)
957 return
958 }
959
960 if tag == reuseObjTag {
961 depPaths.ObjFiles = append(depPaths.ObjFiles,
962 c.compiler.(*libraryCompiler).reuseObjFiles...)
963 return
964 }
965
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700966 if t, ok := tag.(dependencyTag); ok && t.library {
Colin Crossc99deeb2016-04-11 15:06:20 -0700967 if i, ok := c.linker.(exportedFlagsProducer); ok {
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700968 cflags := i.exportedFlags()
Colin Crossc99deeb2016-04-11 15:06:20 -0700969 depPaths.Cflags = append(depPaths.Cflags, cflags...)
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700970
971 if t.reexportFlags {
972 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, cflags...)
973 }
Colin Crossc99deeb2016-04-11 15:06:20 -0700974 }
975 }
976
Colin Cross635c3b02016-05-18 15:37:25 -0700977 var depPtr *android.Paths
Colin Crossc99deeb2016-04-11 15:06:20 -0700978
979 switch tag {
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700980 case sharedDepTag, sharedExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700981 depPtr = &depPaths.SharedLibs
982 case lateSharedDepTag:
983 depPtr = &depPaths.LateSharedLibs
Dan Willemsen490a8dc2016-06-06 18:22:19 -0700984 case staticDepTag, staticExportDepTag:
Colin Crossc99deeb2016-04-11 15:06:20 -0700985 depPtr = &depPaths.StaticLibs
986 case lateStaticDepTag:
987 depPtr = &depPaths.LateStaticLibs
988 case wholeStaticDepTag:
989 depPtr = &depPaths.WholeStaticLibs
Colin Crossc99deeb2016-04-11 15:06:20 -0700990 staticLib, _ := c.linker.(*libraryLinker)
991 if staticLib == nil || !staticLib.static() {
992 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
993 return
994 }
995
996 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
997 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
998 for i := range missingDeps {
999 missingDeps[i] += postfix
1000 }
1001 ctx.AddMissingDependencies(missingDeps)
1002 }
1003 depPaths.WholeStaticLibObjFiles =
1004 append(depPaths.WholeStaticLibObjFiles, staticLib.objFiles...)
1005 case objDepTag:
1006 depPtr = &depPaths.ObjFiles
1007 case crtBeginDepTag:
1008 depPaths.CrtBegin = c.outputFile
1009 case crtEndDepTag:
1010 depPaths.CrtEnd = c.outputFile
1011 default:
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001012 panic(fmt.Errorf("unknown dependency tag: %s", tag))
Colin Crossc99deeb2016-04-11 15:06:20 -07001013 }
1014
1015 if depPtr != nil {
1016 *depPtr = append(*depPtr, c.outputFile.Path())
Colin Crossca860ac2016-01-04 14:34:37 -08001017 }
1018 })
1019
1020 return depPaths
1021}
1022
1023func (c *Module) InstallInData() bool {
1024 if c.installer == nil {
1025 return false
1026 }
1027 return c.installer.inData()
1028}
1029
1030// Compiler
1031
1032type baseCompiler struct {
1033 Properties BaseCompilerProperties
1034}
1035
1036var _ compiler = (*baseCompiler)(nil)
1037
1038func (compiler *baseCompiler) props() []interface{} {
1039 return []interface{}{&compiler.Properties}
1040}
1041
Dan Willemsenb40aab62016-04-20 14:21:14 -07001042func (compiler *baseCompiler) begin(ctx BaseModuleContext) {}
1043
1044func (compiler *baseCompiler) deps(ctx BaseModuleContext, deps Deps) Deps {
1045 deps.GeneratedSources = append(deps.GeneratedSources, compiler.Properties.Generated_sources...)
1046 deps.GeneratedHeaders = append(deps.GeneratedHeaders, compiler.Properties.Generated_headers...)
1047
1048 return deps
1049}
Colin Crossca860ac2016-01-04 14:34:37 -08001050
1051// Create a Flags struct that collects the compile flags from global values,
1052// per-target values, module type values, and per-module Blueprints properties
1053func (compiler *baseCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1054 toolchain := ctx.toolchain()
1055
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001056 CheckBadCompilerFlags(ctx, "cflags", compiler.Properties.Cflags)
1057 CheckBadCompilerFlags(ctx, "cppflags", compiler.Properties.Cppflags)
1058 CheckBadCompilerFlags(ctx, "conlyflags", compiler.Properties.Conlyflags)
1059 CheckBadCompilerFlags(ctx, "asflags", compiler.Properties.Asflags)
1060
Colin Crossca860ac2016-01-04 14:34:37 -08001061 flags.CFlags = append(flags.CFlags, compiler.Properties.Cflags...)
1062 flags.CppFlags = append(flags.CppFlags, compiler.Properties.Cppflags...)
1063 flags.ConlyFlags = append(flags.ConlyFlags, compiler.Properties.Conlyflags...)
1064 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Asflags...)
1065 flags.YaccFlags = append(flags.YaccFlags, compiler.Properties.Yaccflags...)
1066
Colin Cross28344522015-04-22 13:07:53 -07001067 // Include dir cflags
Colin Cross635c3b02016-05-18 15:37:25 -07001068 rootIncludeDirs := android.PathsForSource(ctx, compiler.Properties.Include_dirs)
1069 localIncludeDirs := android.PathsForModuleSrc(ctx, compiler.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001070 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -07001071 includeDirsToFlags(localIncludeDirs),
1072 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -07001073
Colin Crossca860ac2016-01-04 14:34:37 -08001074 if !ctx.noDefaultCompilerFlags() {
1075 if !ctx.sdk() || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -07001076 flags.GlobalFlags = append(flags.GlobalFlags,
1077 "${commonGlobalIncludes}",
1078 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -08001079 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -07001080 }
1081
1082 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Colin Cross635c3b02016-05-18 15:37:25 -07001083 "-I" + android.PathForModuleSrc(ctx).String(),
1084 "-I" + android.PathForModuleOut(ctx).String(),
1085 "-I" + android.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -07001086 }...)
1087 }
1088
Colin Crossca860ac2016-01-04 14:34:37 -08001089 instructionSet := compiler.Properties.Instruction_set
1090 if flags.RequiredInstructionSet != "" {
1091 instructionSet = flags.RequiredInstructionSet
Colin Cross3f40fa42015-01-30 17:27:36 -08001092 }
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001093 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
1094 if flags.Clang {
1095 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
1096 }
1097 if err != nil {
1098 ctx.ModuleErrorf("%s", err)
1099 }
1100
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001101 CheckBadCompilerFlags(ctx, "release.cflags", compiler.Properties.Release.Cflags)
1102
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001103 // TODO: debug
Colin Crossca860ac2016-01-04 14:34:37 -08001104 flags.CFlags = append(flags.CFlags, compiler.Properties.Release.Cflags...)
Dan Willemsen6d11dd82015-11-03 14:27:00 -08001105
Colin Cross97ba0732015-03-23 17:50:24 -07001106 if flags.Clang {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001107 CheckBadCompilerFlags(ctx, "clang_cflags", compiler.Properties.Clang_cflags)
1108 CheckBadCompilerFlags(ctx, "clang_asflags", compiler.Properties.Clang_asflags)
1109
Colin Cross97ba0732015-03-23 17:50:24 -07001110 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossca860ac2016-01-04 14:34:37 -08001111 flags.CFlags = append(flags.CFlags, compiler.Properties.Clang_cflags...)
1112 flags.AsFlags = append(flags.AsFlags, compiler.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -07001113 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
1114 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
1115 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001116
1117 target := "-target " + toolchain.ClangTriple()
Dan Willemsen3772da12016-05-16 18:01:46 -07001118 var gccPrefix string
1119 if !ctx.Darwin() {
1120 gccPrefix = "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
1121 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001122
Colin Cross97ba0732015-03-23 17:50:24 -07001123 flags.CFlags = append(flags.CFlags, target, gccPrefix)
1124 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
1125 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -08001126 }
1127
Colin Crossa1ad8d12016-06-01 17:09:44 -07001128 hod := "host"
1129 if ctx.Os().Class == android.Device {
1130 hod = "device"
1131 }
1132
Colin Crossca860ac2016-01-04 14:34:37 -08001133 if !ctx.noDefaultCompilerFlags() {
Colin Cross56b4d452015-04-21 17:38:44 -07001134 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
1135
Colin Cross97ba0732015-03-23 17:50:24 -07001136 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -08001137 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -07001138 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001139 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001140 toolchain.ClangCflags(),
1141 "${commonClangGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001142 fmt.Sprintf("${%sClangGlobalCflags}", hod))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -08001143
1144 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -08001145 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001146 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -07001147 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -08001148 toolchain.Cflags(),
1149 "${commonGlobalCflags}",
Colin Crossa1ad8d12016-06-01 17:09:44 -07001150 fmt.Sprintf("${%sGlobalCflags}", hod))
Colin Cross3f40fa42015-01-30 17:27:36 -08001151 }
1152
Colin Cross7b66f152015-12-15 16:07:43 -08001153 if Bool(ctx.AConfig().ProductVariables.Brillo) {
1154 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
1155 }
1156
Colin Crossf6566ed2015-03-24 11:13:38 -07001157 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001158 if Bool(compiler.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -07001159 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001161 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -08001162 }
1163 }
1164
Colin Cross97ba0732015-03-23 17:50:24 -07001165 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -08001166
Colin Cross97ba0732015-03-23 17:50:24 -07001167 if flags.Clang {
1168 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
Colin Cross3f40fa42015-01-30 17:27:36 -08001169 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001170 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
Colin Cross28344522015-04-22 13:07:53 -07001171 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001172 }
1173
Colin Crossc4bde762015-11-23 16:11:30 -08001174 if flags.Clang {
1175 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
1176 } else {
1177 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -08001178 }
1179
Colin Crossca860ac2016-01-04 14:34:37 -08001180 if !ctx.sdk() {
Dan Willemsen3bf6b472015-09-11 17:41:10 -07001181 if ctx.Host() && !flags.Clang {
1182 // The host GCC doesn't support C++14 (and is deprecated, so likely
1183 // never will). Build these modules with C++11.
1184 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
1185 } else {
1186 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
1187 }
1188 }
1189
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001190 // We can enforce some rules more strictly in the code we own. strict
1191 // indicates if this is code that we can be stricter with. If we have
1192 // rules that we want to apply to *our* code (but maybe can't for
1193 // vendor/device specific things), we could extend this to be a ternary
1194 // value.
1195 strict := true
Colin Cross635c3b02016-05-18 15:37:25 -07001196 if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
Dan Willemsen52b1cd22016-03-01 13:36:34 -08001197 strict = false
1198 }
1199
1200 // Can be used to make some annotations stricter for code we can fix
1201 // (such as when we mark functions as deprecated).
1202 if strict {
1203 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
1204 }
1205
Colin Cross3f40fa42015-01-30 17:27:36 -08001206 return flags
1207}
1208
Colin Cross635c3b02016-05-18 15:37:25 -07001209func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
Colin Crossca860ac2016-01-04 14:34:37 -08001210 // Compile files listed in c.Properties.Srcs into objects
Dan Willemsenb40aab62016-04-20 14:21:14 -07001211 objFiles := compiler.compileObjs(ctx, flags, "",
1212 compiler.Properties.Srcs, compiler.Properties.Exclude_srcs,
1213 deps.GeneratedSources, deps.GeneratedHeaders)
1214
Colin Crossca860ac2016-01-04 14:34:37 -08001215 if ctx.Failed() {
1216 return nil
1217 }
1218
Colin Crossca860ac2016-01-04 14:34:37 -08001219 return objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001220}
1221
1222// Compile a list of source files into objects a specified subdirectory
Colin Cross635c3b02016-05-18 15:37:25 -07001223func (compiler *baseCompiler) compileObjs(ctx android.ModuleContext, flags Flags,
1224 subdir string, srcFiles, excludes []string, extraSrcs, deps android.Paths) android.Paths {
Colin Cross581c1892015-04-07 16:50:10 -07001225
Colin Crossca860ac2016-01-04 14:34:37 -08001226 buildFlags := flagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -08001227
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001228 inputFiles := ctx.ExpandSources(srcFiles, excludes)
Dan Willemsenb40aab62016-04-20 14:21:14 -07001229 inputFiles = append(inputFiles, extraSrcs...)
1230 srcPaths, gendeps := genSources(ctx, inputFiles, buildFlags)
1231
1232 deps = append(deps, gendeps...)
Colin Cross16b23492016-01-06 14:41:07 -08001233 deps = append(deps, flags.CFlagsDeps...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001234
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001235 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -08001236}
1237
Colin Crossca860ac2016-01-04 14:34:37 -08001238// baseLinker provides support for shared_libs, static_libs, and whole_static_libs properties
1239type baseLinker struct {
1240 Properties BaseLinkerProperties
1241 dynamicProperties struct {
Colin Crossc99deeb2016-04-11 15:06:20 -07001242 VariantIsShared bool `blueprint:"mutated"`
1243 VariantIsStatic bool `blueprint:"mutated"`
1244 VariantIsStaticBinary bool `blueprint:"mutated"`
1245 RunPaths []string `blueprint:"mutated"`
Colin Cross3f40fa42015-01-30 17:27:36 -08001246 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001247}
1248
Dan Willemsend30e6102016-03-30 17:35:50 -07001249func (linker *baseLinker) begin(ctx BaseModuleContext) {
1250 if ctx.toolchain().Is64Bit() {
Colin Crossc99deeb2016-04-11 15:06:20 -07001251 linker.dynamicProperties.RunPaths = []string{"../lib64", "lib64"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001252 } else {
Colin Crossc99deeb2016-04-11 15:06:20 -07001253 linker.dynamicProperties.RunPaths = []string{"../lib", "lib"}
Dan Willemsend30e6102016-03-30 17:35:50 -07001254 }
1255}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001256
Colin Crossca860ac2016-01-04 14:34:37 -08001257func (linker *baseLinker) props() []interface{} {
1258 return []interface{}{&linker.Properties, &linker.dynamicProperties}
Colin Crossed4cf0b2015-03-26 14:43:45 -07001259}
1260
Colin Crossca860ac2016-01-04 14:34:37 -08001261func (linker *baseLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1262 deps.WholeStaticLibs = append(deps.WholeStaticLibs, linker.Properties.Whole_static_libs...)
1263 deps.StaticLibs = append(deps.StaticLibs, linker.Properties.Static_libs...)
1264 deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Shared_libs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001265
Dan Willemsen490a8dc2016-06-06 18:22:19 -07001266 deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, linker.Properties.Export_static_lib_headers...)
1267 deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, linker.Properties.Export_shared_lib_headers...)
1268
Colin Cross74d1ec02015-04-28 13:30:13 -07001269 if ctx.ModuleName() != "libcompiler_rt-extras" {
Colin Crossca860ac2016-01-04 14:34:37 -08001270 deps.StaticLibs = append(deps.StaticLibs, "libcompiler_rt-extras")
Colin Cross74d1ec02015-04-28 13:30:13 -07001271 }
1272
Colin Crossf6566ed2015-03-24 11:13:38 -07001273 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001274 // libgcc and libatomic have to be last on the command line
Colin Crossca860ac2016-01-04 14:34:37 -08001275 deps.LateStaticLibs = append(deps.LateStaticLibs, "libatomic")
1276 if !Bool(linker.Properties.No_libgcc) {
1277 deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
Dan Willemsend67be222015-09-16 15:19:33 -07001278 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001279
Colin Crossca860ac2016-01-04 14:34:37 -08001280 if !linker.static() {
1281 if linker.Properties.System_shared_libs != nil {
1282 deps.LateSharedLibs = append(deps.LateSharedLibs,
1283 linker.Properties.System_shared_libs...)
1284 } else if !ctx.sdk() {
1285 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc", "libm")
1286 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001287 }
Colin Cross577f6e42015-03-27 18:23:34 -07001288
Colin Crossca860ac2016-01-04 14:34:37 -08001289 if ctx.sdk() {
1290 version := ctx.sdkVersion()
1291 deps.SharedLibs = append(deps.SharedLibs,
Colin Cross577f6e42015-03-27 18:23:34 -07001292 "ndk_libc."+version,
1293 "ndk_libm."+version,
1294 )
1295 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001296 }
1297
Colin Crossca860ac2016-01-04 14:34:37 -08001298 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001299}
1300
Colin Crossca860ac2016-01-04 14:34:37 -08001301func (linker *baseLinker) flags(ctx ModuleContext, flags Flags) Flags {
1302 toolchain := ctx.toolchain()
1303
Colin Crossca860ac2016-01-04 14:34:37 -08001304 if !ctx.noDefaultCompilerFlags() {
1305 if ctx.Device() && !Bool(linker.Properties.Allow_undefined_symbols) {
1306 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
1307 }
1308
1309 if flags.Clang {
1310 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
1311 } else {
1312 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
1313 }
1314
1315 if ctx.Host() {
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001316 CheckBadHostLdlibs(ctx, "host_ldlibs", linker.Properties.Host_ldlibs)
1317
Colin Crossca860ac2016-01-04 14:34:37 -08001318 flags.LdFlags = append(flags.LdFlags, linker.Properties.Host_ldlibs...)
1319 }
1320 }
1321
Dan Willemsen20acc5c2016-05-25 14:47:21 -07001322 CheckBadLinkerFlags(ctx, "ldflags", linker.Properties.Ldflags)
1323
Dan Willemsen00ced762016-05-10 17:31:21 -07001324 flags.LdFlags = append(flags.LdFlags, linker.Properties.Ldflags...)
1325
Dan Willemsend30e6102016-03-30 17:35:50 -07001326 if ctx.Host() && !linker.static() {
1327 rpath_prefix := `\$$ORIGIN/`
1328 if ctx.Darwin() {
1329 rpath_prefix = "@loader_path/"
1330 }
1331
Colin Crossc99deeb2016-04-11 15:06:20 -07001332 for _, rpath := range linker.dynamicProperties.RunPaths {
Dan Willemsend30e6102016-03-30 17:35:50 -07001333 flags.LdFlags = append(flags.LdFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
1334 }
1335 }
1336
Dan Willemsene7174922016-03-30 17:33:52 -07001337 if flags.Clang {
1338 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainClangLdflags())
1339 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001340 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
1341 }
1342
1343 return flags
1344}
1345
1346func (linker *baseLinker) static() bool {
1347 return linker.dynamicProperties.VariantIsStatic
1348}
1349
1350func (linker *baseLinker) staticBinary() bool {
1351 return linker.dynamicProperties.VariantIsStaticBinary
1352}
1353
1354func (linker *baseLinker) setStatic(static bool) {
1355 linker.dynamicProperties.VariantIsStatic = static
1356}
1357
Colin Cross16b23492016-01-06 14:41:07 -08001358func (linker *baseLinker) isDependencyRoot() bool {
1359 return false
1360}
1361
Colin Crossca860ac2016-01-04 14:34:37 -08001362type baseLinkerInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001363 // Returns true if the build options for the module have selected a static or shared build
1364 buildStatic() bool
1365 buildShared() bool
1366
1367 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001368 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001369
Colin Cross18b6dc52015-04-28 13:20:37 -07001370 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001371 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001372
1373 // Returns whether a module is a static binary
1374 staticBinary() bool
Colin Cross16b23492016-01-06 14:41:07 -08001375
1376 // Returns true for dependency roots (binaries)
1377 // TODO(ccross): also handle dlopenable libraries
1378 isDependencyRoot() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001379}
1380
Colin Crossca860ac2016-01-04 14:34:37 -08001381type baseInstaller struct {
1382 Properties InstallerProperties
1383
1384 dir string
1385 dir64 string
1386 data bool
1387
Colin Cross635c3b02016-05-18 15:37:25 -07001388 path android.OutputPath
Colin Crossca860ac2016-01-04 14:34:37 -08001389}
1390
1391var _ installer = (*baseInstaller)(nil)
1392
1393func (installer *baseInstaller) props() []interface{} {
1394 return []interface{}{&installer.Properties}
1395}
1396
Colin Cross635c3b02016-05-18 15:37:25 -07001397func (installer *baseInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001398 subDir := installer.dir
1399 if ctx.toolchain().Is64Bit() && installer.dir64 != "" {
1400 subDir = installer.dir64
1401 }
Dan Willemsen17f05262016-05-31 16:27:00 -07001402 if !ctx.Host() && !ctx.Arch().Native {
1403 subDir = filepath.Join(subDir, ctx.Arch().ArchType.String())
1404 }
Colin Cross635c3b02016-05-18 15:37:25 -07001405 dir := android.PathForModuleInstall(ctx, subDir, installer.Properties.Relative_install_path)
Colin Crossca860ac2016-01-04 14:34:37 -08001406 installer.path = ctx.InstallFile(dir, file)
1407}
1408
1409func (installer *baseInstaller) inData() bool {
1410 return installer.data
1411}
1412
Colin Cross3f40fa42015-01-30 17:27:36 -08001413//
1414// Combined static+shared libraries
1415//
1416
Colin Cross919281a2016-04-05 16:42:05 -07001417type flagExporter struct {
1418 Properties FlagExporterProperties
1419
1420 flags []string
1421}
1422
1423func (f *flagExporter) exportIncludes(ctx ModuleContext, inc string) {
Colin Cross635c3b02016-05-18 15:37:25 -07001424 includeDirs := android.PathsForModuleSrc(ctx, f.Properties.Export_include_dirs)
1425 f.flags = append(f.flags, android.JoinWithPrefix(includeDirs.Strings(), inc))
Colin Cross919281a2016-04-05 16:42:05 -07001426}
1427
1428func (f *flagExporter) reexportFlags(flags []string) {
1429 f.flags = append(f.flags, flags...)
1430}
1431
1432func (f *flagExporter) exportedFlags() []string {
1433 return f.flags
1434}
1435
1436type exportedFlagsProducer interface {
1437 exportedFlags() []string
1438}
1439
1440var _ exportedFlagsProducer = (*flagExporter)(nil)
1441
Colin Crossca860ac2016-01-04 14:34:37 -08001442type libraryCompiler struct {
1443 baseCompiler
Colin Crossaee540a2015-07-06 17:48:31 -07001444
Colin Crossca860ac2016-01-04 14:34:37 -08001445 linker *libraryLinker
1446 Properties LibraryCompilerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001447
Colin Crossca860ac2016-01-04 14:34:37 -08001448 // For reusing static library objects for shared library
Colin Cross635c3b02016-05-18 15:37:25 -07001449 reuseObjFiles android.Paths
Colin Cross3f40fa42015-01-30 17:27:36 -08001450}
1451
Colin Crossca860ac2016-01-04 14:34:37 -08001452var _ compiler = (*libraryCompiler)(nil)
1453
1454func (library *libraryCompiler) props() []interface{} {
1455 props := library.baseCompiler.props()
1456 return append(props, &library.Properties)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001457}
1458
Colin Crossca860ac2016-01-04 14:34:37 -08001459func (library *libraryCompiler) flags(ctx ModuleContext, flags Flags) Flags {
1460 flags = library.baseCompiler.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001461
Dan Willemsen490fd492015-11-24 17:53:15 -08001462 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1463 // all code is position independent, and then those warnings get promoted to
1464 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001465 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001466 flags.CFlags = append(flags.CFlags, "-fPIC")
1467 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001468
Colin Crossca860ac2016-01-04 14:34:37 -08001469 if library.linker.static() {
1470 flags.CFlags = append(flags.CFlags, library.Properties.Static.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001471 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001472 flags.CFlags = append(flags.CFlags, library.Properties.Shared.Cflags...)
Colin Crossd8e780d2015-04-28 17:39:43 -07001473 }
1474
Colin Crossca860ac2016-01-04 14:34:37 -08001475 return flags
1476}
1477
Colin Cross635c3b02016-05-18 15:37:25 -07001478func (library *libraryCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Paths {
1479 var objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001480
Dan Willemsenb40aab62016-04-20 14:21:14 -07001481 objFiles = library.baseCompiler.compile(ctx, flags, deps)
Colin Crossc99deeb2016-04-11 15:06:20 -07001482 library.reuseObjFiles = objFiles
Colin Crossca860ac2016-01-04 14:34:37 -08001483
1484 if library.linker.static() {
Colin Cross635c3b02016-05-18 15:37:25 -07001485 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceStaticLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001486 library.Properties.Static.Srcs, library.Properties.Static.Exclude_srcs,
1487 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001488 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001489 objFiles = append(objFiles, library.compileObjs(ctx, flags, android.DeviceSharedLibrary,
Dan Willemsenb40aab62016-04-20 14:21:14 -07001490 library.Properties.Shared.Srcs, library.Properties.Shared.Exclude_srcs,
1491 nil, deps.GeneratedHeaders)...)
Colin Crossca860ac2016-01-04 14:34:37 -08001492 }
1493
1494 return objFiles
1495}
1496
1497type libraryLinker struct {
1498 baseLinker
Colin Cross919281a2016-04-05 16:42:05 -07001499 flagExporter
Colin Cross665dce92016-04-28 14:50:03 -07001500 stripper
Colin Crossca860ac2016-01-04 14:34:37 -08001501
1502 Properties LibraryLinkerProperties
1503
1504 dynamicProperties struct {
1505 BuildStatic bool `blueprint:"mutated"`
1506 BuildShared bool `blueprint:"mutated"`
1507 }
1508
Colin Crossca860ac2016-01-04 14:34:37 -08001509 // If we're used as a whole_static_lib, our missing dependencies need
1510 // to be given
1511 wholeStaticMissingDeps []string
1512
1513 // For whole_static_libs
Colin Cross635c3b02016-05-18 15:37:25 -07001514 objFiles android.Paths
Colin Crossca860ac2016-01-04 14:34:37 -08001515}
1516
1517var _ linker = (*libraryLinker)(nil)
Colin Crossca860ac2016-01-04 14:34:37 -08001518
1519func (library *libraryLinker) props() []interface{} {
1520 props := library.baseLinker.props()
Colin Cross919281a2016-04-05 16:42:05 -07001521 return append(props,
1522 &library.Properties,
1523 &library.dynamicProperties,
Colin Cross665dce92016-04-28 14:50:03 -07001524 &library.flagExporter.Properties,
1525 &library.stripper.StripProperties)
Colin Crossca860ac2016-01-04 14:34:37 -08001526}
1527
1528func (library *libraryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1529 flags = library.baseLinker.flags(ctx, flags)
1530
1531 flags.Nocrt = Bool(library.Properties.Nocrt)
1532
1533 if !library.static() {
Colin Cross30d5f512016-05-03 18:02:42 -07001534 libName := ctx.ModuleName() + library.Properties.VariantName
Colin Cross3f40fa42015-01-30 17:27:36 -08001535 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1536 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001537 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001538 sharedFlag = "-shared"
1539 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001540 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001541 flags.LdFlags = append(flags.LdFlags,
1542 "-nostdlib",
1543 "-Wl,--gc-sections",
1544 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001545 }
Colin Cross97ba0732015-03-23 17:50:24 -07001546
Colin Cross0af4b842015-04-30 16:36:18 -07001547 if ctx.Darwin() {
1548 flags.LdFlags = append(flags.LdFlags,
1549 "-dynamiclib",
1550 "-single_module",
1551 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001552 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001553 )
1554 } else {
1555 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001556 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001557 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001558 )
1559 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001560 }
Colin Cross97ba0732015-03-23 17:50:24 -07001561
1562 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001563}
1564
Colin Crossca860ac2016-01-04 14:34:37 -08001565func (library *libraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1566 deps = library.baseLinker.deps(ctx, deps)
1567 if library.static() {
1568 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Static.Whole_static_libs...)
1569 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Static.Static_libs...)
1570 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Static.Shared_libs...)
1571 } else {
1572 if ctx.Device() && !Bool(library.Properties.Nocrt) {
1573 if !ctx.sdk() {
1574 deps.CrtBegin = "crtbegin_so"
1575 deps.CrtEnd = "crtend_so"
1576 } else {
1577 deps.CrtBegin = "ndk_crtbegin_so." + ctx.sdkVersion()
1578 deps.CrtEnd = "ndk_crtend_so." + ctx.sdkVersion()
1579 }
1580 }
1581 deps.WholeStaticLibs = append(deps.WholeStaticLibs, library.Properties.Shared.Whole_static_libs...)
1582 deps.StaticLibs = append(deps.StaticLibs, library.Properties.Shared.Static_libs...)
1583 deps.SharedLibs = append(deps.SharedLibs, library.Properties.Shared.Shared_libs...)
1584 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001585
Colin Crossca860ac2016-01-04 14:34:37 -08001586 return deps
1587}
Colin Cross3f40fa42015-01-30 17:27:36 -08001588
Colin Crossca860ac2016-01-04 14:34:37 -08001589func (library *libraryLinker) linkStatic(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001590 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Crossca860ac2016-01-04 14:34:37 -08001591
Colin Cross635c3b02016-05-18 15:37:25 -07001592 library.objFiles = append(android.Paths{}, deps.WholeStaticLibObjFiles...)
Dan Willemsen025b4802016-05-11 17:25:48 -07001593 library.objFiles = append(library.objFiles, objFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001594
Colin Cross635c3b02016-05-18 15:37:25 -07001595 outputFile := android.PathForModuleOut(ctx,
Colin Cross16b23492016-01-06 14:41:07 -08001596 ctx.ModuleName()+library.Properties.VariantName+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001597
Colin Cross0af4b842015-04-30 16:36:18 -07001598 if ctx.Darwin() {
Dan Willemsen025b4802016-05-11 17:25:48 -07001599 TransformDarwinObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001600 } else {
Dan Willemsen025b4802016-05-11 17:25:48 -07001601 TransformObjToStaticLib(ctx, library.objFiles, flagsToBuilderFlags(flags), outputFile)
Colin Cross0af4b842015-04-30 16:36:18 -07001602 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001603
Colin Crossca860ac2016-01-04 14:34:37 -08001604 library.wholeStaticMissingDeps = ctx.GetMissingDependencies()
Colin Cross3f40fa42015-01-30 17:27:36 -08001605
1606 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001607
1608 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001609}
1610
Colin Crossca860ac2016-01-04 14:34:37 -08001611func (library *libraryLinker) linkShared(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001612 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001613
Colin Cross635c3b02016-05-18 15:37:25 -07001614 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001615
Colin Cross635c3b02016-05-18 15:37:25 -07001616 versionScript := android.OptionalPathForModuleSrc(ctx, library.Properties.Version_script)
1617 unexportedSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Unexported_symbols_list)
1618 forceNotWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_not_weak_list)
1619 forceWeakSymbols := android.OptionalPathForModuleSrc(ctx, library.Properties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001620 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001621 if versionScript.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001622 flags.LdFlags = append(flags.LdFlags, "-Wl,--version-script,"+versionScript.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001623 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001624 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001625 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001626 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1627 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001628 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001629 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1630 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001631 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001632 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1633 }
1634 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001635 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001636 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1637 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001638 if unexportedSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001639 flags.LdFlags = append(flags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001640 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001641 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001642 if forceNotWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001643 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001644 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001645 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001646 if forceWeakSymbols.Valid() {
Colin Crossca860ac2016-01-04 14:34:37 -08001647 flags.LdFlags = append(flags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001648 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001649 }
Colin Crossaee540a2015-07-06 17:48:31 -07001650 }
1651
Colin Cross665dce92016-04-28 14:50:03 -07001652 fileName := ctx.ModuleName() + library.Properties.VariantName + flags.Toolchain.ShlibSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001653 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001654 ret := outputFile
1655
1656 builderFlags := flagsToBuilderFlags(flags)
1657
1658 if library.stripper.needsStrip(ctx) {
1659 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07001660 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001661 library.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
1662 }
1663
Colin Crossca860ac2016-01-04 14:34:37 -08001664 sharedLibs := deps.SharedLibs
1665 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001666
Colin Crossca860ac2016-01-04 14:34:37 -08001667 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs,
1668 deps.StaticLibs, deps.LateStaticLibs, deps.WholeStaticLibs,
Colin Cross665dce92016-04-28 14:50:03 -07001669 linkerDeps, deps.CrtBegin, deps.CrtEnd, false, builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001670
Colin Cross665dce92016-04-28 14:50:03 -07001671 return ret
Colin Cross3f40fa42015-01-30 17:27:36 -08001672}
1673
Colin Crossca860ac2016-01-04 14:34:37 -08001674func (library *libraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001675 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001676
Colin Crossc99deeb2016-04-11 15:06:20 -07001677 objFiles = append(objFiles, deps.ObjFiles...)
1678
Colin Cross635c3b02016-05-18 15:37:25 -07001679 var out android.Path
Colin Crossca860ac2016-01-04 14:34:37 -08001680 if library.static() {
1681 out = library.linkStatic(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001682 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001683 out = library.linkShared(ctx, flags, deps, objFiles)
Colin Cross3f40fa42015-01-30 17:27:36 -08001684 }
1685
Colin Cross919281a2016-04-05 16:42:05 -07001686 library.exportIncludes(ctx, "-I")
1687 library.reexportFlags(deps.ReexportedCflags)
Colin Crossca860ac2016-01-04 14:34:37 -08001688
1689 return out
1690}
1691
1692func (library *libraryLinker) buildStatic() bool {
1693 return library.dynamicProperties.BuildStatic
1694}
1695
1696func (library *libraryLinker) buildShared() bool {
1697 return library.dynamicProperties.BuildShared
1698}
1699
1700func (library *libraryLinker) getWholeStaticMissingDeps() []string {
1701 return library.wholeStaticMissingDeps
1702}
1703
Colin Crossc99deeb2016-04-11 15:06:20 -07001704func (library *libraryLinker) installable() bool {
1705 return !library.static()
1706}
1707
Colin Crossca860ac2016-01-04 14:34:37 -08001708type libraryInstaller struct {
1709 baseInstaller
1710
Colin Cross30d5f512016-05-03 18:02:42 -07001711 linker *libraryLinker
1712 sanitize *sanitize
Colin Crossca860ac2016-01-04 14:34:37 -08001713}
1714
Colin Cross635c3b02016-05-18 15:37:25 -07001715func (library *libraryInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08001716 if !library.linker.static() {
1717 library.baseInstaller.install(ctx, file)
Colin Cross3f40fa42015-01-30 17:27:36 -08001718 }
1719}
1720
Colin Cross30d5f512016-05-03 18:02:42 -07001721func (library *libraryInstaller) inData() bool {
1722 return library.baseInstaller.inData() || library.sanitize.inData()
1723}
1724
Colin Cross635c3b02016-05-18 15:37:25 -07001725func NewLibrary(hod android.HostOrDeviceSupported, shared, static bool) *Module {
1726 module := newModule(hod, android.MultilibBoth)
Dan Albertc403f7c2015-03-18 14:01:18 -07001727
Colin Crossca860ac2016-01-04 14:34:37 -08001728 linker := &libraryLinker{}
1729 linker.dynamicProperties.BuildShared = shared
1730 linker.dynamicProperties.BuildStatic = static
1731 module.linker = linker
1732
1733 module.compiler = &libraryCompiler{
1734 linker: linker,
1735 }
1736 module.installer = &libraryInstaller{
1737 baseInstaller: baseInstaller{
1738 dir: "lib",
1739 dir64: "lib64",
1740 },
Colin Cross30d5f512016-05-03 18:02:42 -07001741 linker: linker,
1742 sanitize: module.sanitize,
Dan Albertc403f7c2015-03-18 14:01:18 -07001743 }
1744
Colin Crossca860ac2016-01-04 14:34:37 -08001745 return module
Dan Albertc403f7c2015-03-18 14:01:18 -07001746}
1747
Colin Crossca860ac2016-01-04 14:34:37 -08001748func libraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001749 module := NewLibrary(android.HostAndDeviceSupported, true, true)
Colin Crossca860ac2016-01-04 14:34:37 -08001750 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07001751}
1752
Colin Cross3f40fa42015-01-30 17:27:36 -08001753//
1754// Objects (for crt*.o)
1755//
1756
Colin Crossca860ac2016-01-04 14:34:37 -08001757type objectLinker struct {
Colin Cross81413472016-04-11 14:37:39 -07001758 Properties ObjectLinkerProperties
Dan Albertc3144b12015-04-28 18:17:56 -07001759}
1760
Colin Crossca860ac2016-01-04 14:34:37 -08001761func objectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001762 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08001763 module.compiler = &baseCompiler{}
1764 module.linker = &objectLinker{}
1765 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001766}
1767
Colin Cross81413472016-04-11 14:37:39 -07001768func (object *objectLinker) props() []interface{} {
1769 return []interface{}{&object.Properties}
Dan Albertc3144b12015-04-28 18:17:56 -07001770}
1771
Colin Crossca860ac2016-01-04 14:34:37 -08001772func (*objectLinker) begin(ctx BaseModuleContext) {}
Colin Cross3f40fa42015-01-30 17:27:36 -08001773
Colin Cross81413472016-04-11 14:37:39 -07001774func (object *objectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1775 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Crossca860ac2016-01-04 14:34:37 -08001776 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001777}
1778
Colin Crossca860ac2016-01-04 14:34:37 -08001779func (*objectLinker) flags(ctx ModuleContext, flags Flags) Flags {
Dan Willemsene7174922016-03-30 17:33:52 -07001780 if flags.Clang {
1781 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainClangLdflags())
1782 } else {
1783 flags.LdFlags = append(flags.LdFlags, ctx.toolchain().ToolchainLdflags())
1784 }
1785
Colin Crossca860ac2016-01-04 14:34:37 -08001786 return flags
1787}
1788
1789func (object *objectLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001790 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001791
Colin Cross97ba0732015-03-23 17:50:24 -07001792 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001793
Colin Cross635c3b02016-05-18 15:37:25 -07001794 var outputFile android.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001795 if len(objFiles) == 1 {
1796 outputFile = objFiles[0]
1797 } else {
Colin Cross635c3b02016-05-18 15:37:25 -07001798 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Crossca860ac2016-01-04 14:34:37 -08001799 TransformObjsToObj(ctx, objFiles, flagsToBuilderFlags(flags), output)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001800 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001801 }
1802
Colin Cross3f40fa42015-01-30 17:27:36 -08001803 ctx.CheckbuildFile(outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08001804 return outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001805}
1806
Colin Crossc99deeb2016-04-11 15:06:20 -07001807func (*objectLinker) installable() bool {
1808 return false
1809}
1810
Colin Cross3f40fa42015-01-30 17:27:36 -08001811//
1812// Executables
1813//
1814
Colin Crossca860ac2016-01-04 14:34:37 -08001815type binaryLinker struct {
1816 baseLinker
Colin Cross665dce92016-04-28 14:50:03 -07001817 stripper
Colin Cross7d5136f2015-05-11 13:39:40 -07001818
Colin Crossca860ac2016-01-04 14:34:37 -08001819 Properties BinaryLinkerProperties
Colin Cross7d5136f2015-05-11 13:39:40 -07001820
Colin Cross635c3b02016-05-18 15:37:25 -07001821 hostToolPath android.OptionalPath
Colin Cross7d5136f2015-05-11 13:39:40 -07001822}
1823
Colin Crossca860ac2016-01-04 14:34:37 -08001824var _ linker = (*binaryLinker)(nil)
1825
1826func (binary *binaryLinker) props() []interface{} {
Colin Cross665dce92016-04-28 14:50:03 -07001827 return append(binary.baseLinker.props(),
1828 &binary.Properties,
1829 &binary.stripper.StripProperties)
1830
Colin Cross3f40fa42015-01-30 17:27:36 -08001831}
1832
Colin Crossca860ac2016-01-04 14:34:37 -08001833func (binary *binaryLinker) buildStatic() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001834 return binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001835}
1836
Colin Crossca860ac2016-01-04 14:34:37 -08001837func (binary *binaryLinker) buildShared() bool {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001838 return !binary.baseLinker.staticBinary()
Colin Crossed4cf0b2015-03-26 14:43:45 -07001839}
1840
Colin Crossca860ac2016-01-04 14:34:37 -08001841func (binary *binaryLinker) getStem(ctx BaseModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001842 stem := ctx.ModuleName()
Colin Crossca860ac2016-01-04 14:34:37 -08001843 if binary.Properties.Stem != "" {
1844 stem = binary.Properties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001845 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001846
Colin Crossca860ac2016-01-04 14:34:37 -08001847 return stem + binary.Properties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001848}
1849
Colin Crossca860ac2016-01-04 14:34:37 -08001850func (binary *binaryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
1851 deps = binary.baseLinker.deps(ctx, deps)
Colin Crossf6566ed2015-03-24 11:13:38 -07001852 if ctx.Device() {
Colin Crossca860ac2016-01-04 14:34:37 -08001853 if !ctx.sdk() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001854 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001855 deps.CrtBegin = "crtbegin_static"
Dan Albertc3144b12015-04-28 18:17:56 -07001856 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001857 deps.CrtBegin = "crtbegin_dynamic"
Dan Albertc3144b12015-04-28 18:17:56 -07001858 }
Colin Crossca860ac2016-01-04 14:34:37 -08001859 deps.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001860 } else {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001861 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001862 deps.CrtBegin = "ndk_crtbegin_static." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001863 } else {
Colin Crossca860ac2016-01-04 14:34:37 -08001864 deps.CrtBegin = "ndk_crtbegin_dynamic." + ctx.sdkVersion()
Dan Albertc3144b12015-04-28 18:17:56 -07001865 }
Colin Crossca860ac2016-01-04 14:34:37 -08001866 deps.CrtEnd = "ndk_crtend_android." + ctx.sdkVersion()
Colin Cross3f40fa42015-01-30 17:27:36 -08001867 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001868
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001869 if binary.buildStatic() {
Colin Crossca860ac2016-01-04 14:34:37 -08001870 if inList("libc++_static", deps.StaticLibs) {
1871 deps.StaticLibs = append(deps.StaticLibs, "libm", "libc", "libdl")
Colin Cross74d1ec02015-04-28 13:30:13 -07001872 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001873 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1874 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1875 // move them to the beginning of deps.LateStaticLibs
1876 var groupLibs []string
Colin Crossca860ac2016-01-04 14:34:37 -08001877 deps.StaticLibs, groupLibs = filterList(deps.StaticLibs,
Colin Crossed4cf0b2015-03-26 14:43:45 -07001878 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
Colin Crossca860ac2016-01-04 14:34:37 -08001879 deps.LateStaticLibs = append(groupLibs, deps.LateStaticLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001880 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001881 }
Colin Crossca860ac2016-01-04 14:34:37 -08001882
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001883 if binary.buildShared() && inList("libc", deps.StaticLibs) {
Colin Crossca860ac2016-01-04 14:34:37 -08001884 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1885 "from static libs or set static_executable: true")
1886 }
1887 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08001888}
1889
Colin Crossc99deeb2016-04-11 15:06:20 -07001890func (*binaryLinker) installable() bool {
1891 return true
1892}
1893
Colin Cross16b23492016-01-06 14:41:07 -08001894func (binary *binaryLinker) isDependencyRoot() bool {
1895 return true
1896}
1897
Colin Cross635c3b02016-05-18 15:37:25 -07001898func NewBinary(hod android.HostOrDeviceSupported) *Module {
1899 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08001900 module.compiler = &baseCompiler{}
1901 module.linker = &binaryLinker{}
1902 module.installer = &baseInstaller{
1903 dir: "bin",
1904 }
1905 return module
Colin Cross3f40fa42015-01-30 17:27:36 -08001906}
1907
Colin Crossca860ac2016-01-04 14:34:37 -08001908func binaryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07001909 module := NewBinary(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08001910 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08001911}
1912
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001913func (binary *binaryLinker) begin(ctx BaseModuleContext) {
1914 binary.baseLinker.begin(ctx)
1915
1916 static := Bool(binary.Properties.Static_executable)
1917 if ctx.Host() {
Colin Crossa1ad8d12016-06-01 17:09:44 -07001918 if ctx.Os() == android.Linux {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001919 if binary.Properties.Static_executable == nil && Bool(ctx.AConfig().ProductVariables.HostStaticBinaries) {
1920 static = true
1921 }
1922 } else {
1923 // Static executables are not supported on Darwin or Windows
1924 static = false
1925 }
Colin Cross0af4b842015-04-30 16:36:18 -07001926 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001927 if static {
1928 binary.dynamicProperties.VariantIsStatic = true
Colin Crossca860ac2016-01-04 14:34:37 -08001929 binary.dynamicProperties.VariantIsStaticBinary = true
Colin Cross18b6dc52015-04-28 13:20:37 -07001930 }
1931}
1932
Colin Crossca860ac2016-01-04 14:34:37 -08001933func (binary *binaryLinker) flags(ctx ModuleContext, flags Flags) Flags {
1934 flags = binary.baseLinker.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001935
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001936 if ctx.Host() && !binary.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -08001937 flags.LdFlags = append(flags.LdFlags, "-pie")
Colin Crossa1ad8d12016-06-01 17:09:44 -07001938 if ctx.Os() == android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001939 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1940 }
1941 }
1942
1943 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1944 // all code is position independent, and then those warnings get promoted to
1945 // errors.
Colin Crossa1ad8d12016-06-01 17:09:44 -07001946 if ctx.Os() != android.Windows {
Dan Willemsen490fd492015-11-24 17:53:15 -08001947 flags.CFlags = append(flags.CFlags, "-fpie")
1948 }
Colin Cross97ba0732015-03-23 17:50:24 -07001949
Colin Crossf6566ed2015-03-24 11:13:38 -07001950 if ctx.Device() {
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001951 if binary.buildStatic() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001952 // Clang driver needs -static to create static executable.
1953 // However, bionic/linker uses -shared to overwrite.
1954 // Linker for x86 targets does not allow coexistance of -static and -shared,
1955 // so we add -static only if -shared is not used.
1956 if !inList("-shared", flags.LdFlags) {
1957 flags.LdFlags = append(flags.LdFlags, "-static")
1958 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001959
Colin Crossed4cf0b2015-03-26 14:43:45 -07001960 flags.LdFlags = append(flags.LdFlags,
1961 "-nostdlib",
1962 "-Bstatic",
1963 "-Wl,--gc-sections",
1964 )
1965
1966 } else {
Colin Cross16b23492016-01-06 14:41:07 -08001967 if flags.DynamicLinker == "" {
1968 flags.DynamicLinker = "/system/bin/linker"
1969 if flags.Toolchain.Is64Bit() {
1970 flags.DynamicLinker += "64"
1971 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001972 }
1973
1974 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001975 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001976 "-nostdlib",
1977 "-Bdynamic",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001978 "-Wl,--gc-sections",
1979 "-Wl,-z,nocopyreloc",
1980 )
1981 }
Dan Willemsen36cff8b2016-05-17 16:35:02 -07001982 } else {
1983 if binary.staticBinary() {
1984 flags.LdFlags = append(flags.LdFlags, "-static")
1985 }
1986 if ctx.Darwin() {
1987 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
1988 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001989 }
1990
Colin Cross97ba0732015-03-23 17:50:24 -07001991 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001992}
1993
Colin Crossca860ac2016-01-04 14:34:37 -08001994func (binary *binaryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07001995 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08001996
Colin Cross665dce92016-04-28 14:50:03 -07001997 fileName := binary.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
Colin Cross635c3b02016-05-18 15:37:25 -07001998 outputFile := android.PathForModuleOut(ctx, fileName)
Colin Cross665dce92016-04-28 14:50:03 -07001999 ret := outputFile
Colin Crossa1ad8d12016-06-01 17:09:44 -07002000 if ctx.Os().Class == android.Host {
Colin Cross635c3b02016-05-18 15:37:25 -07002001 binary.hostToolPath = android.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002002 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002003
Colin Cross635c3b02016-05-18 15:37:25 -07002004 var linkerDeps android.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07002005
Colin Crossca860ac2016-01-04 14:34:37 -08002006 sharedLibs := deps.SharedLibs
2007 sharedLibs = append(sharedLibs, deps.LateSharedLibs...)
2008
Colin Cross16b23492016-01-06 14:41:07 -08002009 if flags.DynamicLinker != "" {
2010 flags.LdFlags = append(flags.LdFlags, " -Wl,-dynamic-linker,"+flags.DynamicLinker)
2011 }
2012
Colin Cross665dce92016-04-28 14:50:03 -07002013 builderFlags := flagsToBuilderFlags(flags)
2014
2015 if binary.stripper.needsStrip(ctx) {
2016 strippedOutputFile := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002017 outputFile = android.PathForModuleOut(ctx, "unstripped", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002018 binary.stripper.strip(ctx, outputFile, strippedOutputFile, builderFlags)
2019 }
2020
2021 if binary.Properties.Prefix_symbols != "" {
2022 afterPrefixSymbols := outputFile
Colin Cross635c3b02016-05-18 15:37:25 -07002023 outputFile = android.PathForModuleOut(ctx, "unprefixed", fileName)
Colin Cross665dce92016-04-28 14:50:03 -07002024 TransformBinaryPrefixSymbols(ctx, binary.Properties.Prefix_symbols, outputFile,
2025 flagsToBuilderFlags(flags), afterPrefixSymbols)
2026 }
2027
Colin Crossca860ac2016-01-04 14:34:37 -08002028 TransformObjToDynamicBinary(ctx, objFiles, sharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07002029 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross665dce92016-04-28 14:50:03 -07002030 builderFlags, outputFile)
Colin Crossca860ac2016-01-04 14:34:37 -08002031
2032 return ret
Dan Albertc403f7c2015-03-18 14:01:18 -07002033}
Colin Cross3f40fa42015-01-30 17:27:36 -08002034
Colin Cross635c3b02016-05-18 15:37:25 -07002035func (binary *binaryLinker) HostToolPath() android.OptionalPath {
Colin Crossca860ac2016-01-04 14:34:37 -08002036 return binary.hostToolPath
Colin Crossd350ecd2015-04-28 13:25:36 -07002037}
2038
Colin Cross665dce92016-04-28 14:50:03 -07002039type stripper struct {
2040 StripProperties StripProperties
2041}
2042
2043func (stripper *stripper) needsStrip(ctx ModuleContext) bool {
2044 return !ctx.AConfig().EmbeddedInMake() && !stripper.StripProperties.Strip.None
2045}
2046
Colin Cross635c3b02016-05-18 15:37:25 -07002047func (stripper *stripper) strip(ctx ModuleContext, in, out android.ModuleOutPath,
Colin Cross665dce92016-04-28 14:50:03 -07002048 flags builderFlags) {
Colin Crossb8ecdfe2016-05-03 15:10:29 -07002049 if ctx.Darwin() {
2050 TransformDarwinStrip(ctx, in, out)
2051 } else {
2052 flags.stripKeepSymbols = stripper.StripProperties.Strip.Keep_symbols
2053 // TODO(ccross): don't add gnu debuglink for user builds
2054 flags.stripAddGnuDebuglink = true
2055 TransformStrip(ctx, in, out, flags)
2056 }
Colin Cross665dce92016-04-28 14:50:03 -07002057}
2058
Colin Cross635c3b02016-05-18 15:37:25 -07002059func testPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002060 if m, ok := mctx.Module().(*Module); ok {
2061 if test, ok := m.linker.(*testLinker); ok {
2062 if Bool(test.Properties.Test_per_src) {
2063 testNames := make([]string, len(m.compiler.(*baseCompiler).Properties.Srcs))
2064 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2065 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
2066 }
2067 tests := mctx.CreateLocalVariations(testNames...)
2068 for i, src := range m.compiler.(*baseCompiler).Properties.Srcs {
2069 tests[i].(*Module).compiler.(*baseCompiler).Properties.Srcs = []string{src}
2070 tests[i].(*Module).linker.(*testLinker).binaryLinker.Properties.Stem = testNames[i]
2071 }
Colin Cross6002e052015-09-16 16:00:08 -07002072 }
2073 }
2074 }
Colin Cross7d5136f2015-05-11 13:39:40 -07002075}
2076
Colin Crossca860ac2016-01-04 14:34:37 -08002077type testLinker struct {
2078 binaryLinker
2079 Properties TestLinkerProperties
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002080}
2081
Dan Willemsend30e6102016-03-30 17:35:50 -07002082func (test *testLinker) begin(ctx BaseModuleContext) {
2083 test.binaryLinker.begin(ctx)
2084
2085 runpath := "../../lib"
2086 if ctx.toolchain().Is64Bit() {
2087 runpath += "64"
2088 }
Colin Crossc99deeb2016-04-11 15:06:20 -07002089 test.dynamicProperties.RunPaths = append([]string{runpath}, test.dynamicProperties.RunPaths...)
Dan Willemsend30e6102016-03-30 17:35:50 -07002090}
2091
Colin Crossca860ac2016-01-04 14:34:37 -08002092func (test *testLinker) props() []interface{} {
2093 return append(test.binaryLinker.props(), &test.Properties)
Dan Albertc403f7c2015-03-18 14:01:18 -07002094}
2095
Colin Crossca860ac2016-01-04 14:34:37 -08002096func (test *testLinker) flags(ctx ModuleContext, flags Flags) Flags {
2097 flags = test.binaryLinker.flags(ctx, flags)
2098
2099 if !test.Properties.Gtest {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002100 return flags
2101 }
Dan Albertc403f7c2015-03-18 14:01:18 -07002102
Colin Cross97ba0732015-03-23 17:50:24 -07002103 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07002104 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07002105 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002106
Colin Crossa1ad8d12016-06-01 17:09:44 -07002107 switch ctx.Os() {
Colin Cross635c3b02016-05-18 15:37:25 -07002108 case android.Windows:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002109 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross635c3b02016-05-18 15:37:25 -07002110 case android.Linux:
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002111 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
2112 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Colin Cross635c3b02016-05-18 15:37:25 -07002113 case android.Darwin:
Dan Willemsen4a946832016-05-13 14:13:01 -07002114 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_MAC")
2115 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002116 }
2117 } else {
2118 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07002119 }
2120
Colin Cross21b9a242015-03-24 14:15:58 -07002121 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07002122}
2123
Colin Crossca860ac2016-01-04 14:34:37 -08002124func (test *testLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2125 if test.Properties.Gtest {
Dan Willemsen8146b2f2016-03-30 21:00:30 -07002126 if ctx.sdk() && ctx.Device() {
2127 switch ctx.selectedStl() {
2128 case "ndk_libc++_shared", "ndk_libc++_static":
2129 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_libcxx", "libgtest_ndk_libcxx")
2130 case "ndk_libgnustl_static":
2131 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_gnustl", "libgtest_ndk_gnustl")
2132 default:
2133 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk", "libgtest_ndk")
2134 }
2135 } else {
2136 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
2137 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002138 }
Colin Crossca860ac2016-01-04 14:34:37 -08002139 deps = test.binaryLinker.deps(ctx, deps)
2140 return deps
Dan Albertc403f7c2015-03-18 14:01:18 -07002141}
2142
Colin Crossca860ac2016-01-04 14:34:37 -08002143type testInstaller struct {
2144 baseInstaller
Dan Willemsen782a2d12015-12-21 14:55:28 -08002145}
2146
Colin Cross635c3b02016-05-18 15:37:25 -07002147func (installer *testInstaller) install(ctx ModuleContext, file android.Path) {
Colin Crossca860ac2016-01-04 14:34:37 -08002148 installer.dir = filepath.Join(installer.dir, ctx.ModuleName())
2149 installer.dir64 = filepath.Join(installer.dir64, ctx.ModuleName())
2150 installer.baseInstaller.install(ctx, file)
2151}
2152
Colin Cross635c3b02016-05-18 15:37:25 -07002153func NewTest(hod android.HostOrDeviceSupported) *Module {
2154 module := newModule(hod, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002155 module.compiler = &baseCompiler{}
2156 linker := &testLinker{}
2157 linker.Properties.Gtest = true
2158 module.linker = linker
2159 module.installer = &testInstaller{
2160 baseInstaller: baseInstaller{
2161 dir: "nativetest",
2162 dir64: "nativetest64",
2163 data: true,
2164 },
Dan Albertc403f7c2015-03-18 14:01:18 -07002165 }
Colin Crossca860ac2016-01-04 14:34:37 -08002166 return module
Dan Willemsen10d52fd2015-12-21 15:25:58 -08002167}
2168
Colin Crossca860ac2016-01-04 14:34:37 -08002169func testFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002170 module := NewTest(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002171 return module.Init()
Dan Albertc403f7c2015-03-18 14:01:18 -07002172}
2173
Colin Crossca860ac2016-01-04 14:34:37 -08002174type benchmarkLinker struct {
2175 binaryLinker
Colin Cross9ffb4f52015-04-24 17:48:09 -07002176}
2177
Colin Crossca860ac2016-01-04 14:34:37 -08002178func (benchmark *benchmarkLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
2179 deps = benchmark.binaryLinker.deps(ctx, deps)
2180 deps.StaticLibs = append(deps.StaticLibs, "libbenchmark", "libbase")
2181 return deps
Colin Cross9ffb4f52015-04-24 17:48:09 -07002182}
2183
Colin Cross635c3b02016-05-18 15:37:25 -07002184func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
2185 module := newModule(hod, android.MultilibFirst)
Colin Crossca860ac2016-01-04 14:34:37 -08002186 module.compiler = &baseCompiler{}
2187 module.linker = &benchmarkLinker{}
2188 module.installer = &baseInstaller{
2189 dir: "nativetest",
2190 dir64: "nativetest64",
2191 data: true,
Colin Cross2ba19d92015-05-07 15:44:20 -07002192 }
Colin Crossca860ac2016-01-04 14:34:37 -08002193 return module
Colin Cross2ba19d92015-05-07 15:44:20 -07002194}
2195
Colin Crossca860ac2016-01-04 14:34:37 -08002196func benchmarkFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002197 module := NewBenchmark(android.HostAndDeviceSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002198 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002199}
2200
Colin Cross3f40fa42015-01-30 17:27:36 -08002201//
2202// Static library
2203//
2204
Colin Crossca860ac2016-01-04 14:34:37 -08002205func libraryStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002206 module := NewLibrary(android.HostAndDeviceSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002207 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002208}
2209
2210//
2211// Shared libraries
2212//
2213
Colin Crossca860ac2016-01-04 14:34:37 -08002214func librarySharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002215 module := NewLibrary(android.HostAndDeviceSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002216 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002217}
2218
2219//
2220// Host static library
2221//
2222
Colin Crossca860ac2016-01-04 14:34:37 -08002223func libraryHostStaticFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002224 module := NewLibrary(android.HostSupported, false, true)
Colin Crossca860ac2016-01-04 14:34:37 -08002225 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002226}
2227
2228//
2229// Host Shared libraries
2230//
2231
Colin Crossca860ac2016-01-04 14:34:37 -08002232func libraryHostSharedFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002233 module := NewLibrary(android.HostSupported, true, false)
Colin Crossca860ac2016-01-04 14:34:37 -08002234 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002235}
2236
2237//
2238// Host Binaries
2239//
2240
Colin Crossca860ac2016-01-04 14:34:37 -08002241func binaryHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002242 module := NewBinary(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002243 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002244}
2245
2246//
Colin Cross1f8f2342015-03-26 16:09:47 -07002247// Host Tests
2248//
2249
Colin Crossca860ac2016-01-04 14:34:37 -08002250func testHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002251 module := NewTest(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002252 return module.Init()
Colin Cross1f8f2342015-03-26 16:09:47 -07002253}
2254
2255//
Colin Cross2ba19d92015-05-07 15:44:20 -07002256// Host Benchmarks
2257//
2258
Colin Crossca860ac2016-01-04 14:34:37 -08002259func benchmarkHostFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002260 module := NewBenchmark(android.HostSupported)
Colin Crossca860ac2016-01-04 14:34:37 -08002261 return module.Init()
Colin Cross2ba19d92015-05-07 15:44:20 -07002262}
2263
2264//
Colin Crosscfad1192015-11-02 16:43:11 -08002265// Defaults
2266//
Colin Crossca860ac2016-01-04 14:34:37 -08002267type Defaults struct {
Colin Cross635c3b02016-05-18 15:37:25 -07002268 android.ModuleBase
2269 android.DefaultsModule
Colin Crosscfad1192015-11-02 16:43:11 -08002270}
2271
Colin Cross635c3b02016-05-18 15:37:25 -07002272func (*Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crosscfad1192015-11-02 16:43:11 -08002273}
2274
Colin Crossca860ac2016-01-04 14:34:37 -08002275func defaultsFactory() (blueprint.Module, []interface{}) {
2276 module := &Defaults{}
Colin Crosscfad1192015-11-02 16:43:11 -08002277
2278 propertyStructs := []interface{}{
Colin Crossca860ac2016-01-04 14:34:37 -08002279 &BaseProperties{},
2280 &BaseCompilerProperties{},
2281 &BaseLinkerProperties{},
2282 &LibraryCompilerProperties{},
Colin Cross919281a2016-04-05 16:42:05 -07002283 &FlagExporterProperties{},
Colin Crossca860ac2016-01-04 14:34:37 -08002284 &LibraryLinkerProperties{},
2285 &BinaryLinkerProperties{},
2286 &TestLinkerProperties{},
2287 &UnusedProperties{},
2288 &StlProperties{},
Colin Cross16b23492016-01-06 14:41:07 -08002289 &SanitizeProperties{},
Colin Cross665dce92016-04-28 14:50:03 -07002290 &StripProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08002291 }
2292
Colin Cross635c3b02016-05-18 15:37:25 -07002293 _, propertyStructs = android.InitAndroidArchModule(module, android.HostAndDeviceDefault,
2294 android.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002295
Colin Cross635c3b02016-05-18 15:37:25 -07002296 return android.InitDefaultsModule(module, module, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08002297}
2298
2299//
Colin Cross3f40fa42015-01-30 17:27:36 -08002300// Device libraries shipped with gcc
2301//
2302
Colin Crossca860ac2016-01-04 14:34:37 -08002303type toolchainLibraryLinker struct {
2304 baseLinker
Colin Cross3f40fa42015-01-30 17:27:36 -08002305}
2306
Colin Crossca860ac2016-01-04 14:34:37 -08002307var _ baseLinkerInterface = (*toolchainLibraryLinker)(nil)
2308
2309func (*toolchainLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross3f40fa42015-01-30 17:27:36 -08002310 // toolchain libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002311 return deps
Colin Cross3f40fa42015-01-30 17:27:36 -08002312}
2313
Colin Crossca860ac2016-01-04 14:34:37 -08002314func (*toolchainLibraryLinker) buildStatic() bool {
2315 return true
2316}
Colin Cross3f40fa42015-01-30 17:27:36 -08002317
Colin Crossca860ac2016-01-04 14:34:37 -08002318func (*toolchainLibraryLinker) buildShared() bool {
2319 return false
2320}
2321
2322func toolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002323 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002324 module.compiler = &baseCompiler{}
2325 module.linker = &toolchainLibraryLinker{}
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002326 module.Properties.Clang = proptools.BoolPtr(false)
Colin Crossca860ac2016-01-04 14:34:37 -08002327 return module.Init()
Colin Cross3f40fa42015-01-30 17:27:36 -08002328}
2329
Colin Crossca860ac2016-01-04 14:34:37 -08002330func (library *toolchainLibraryLinker) link(ctx ModuleContext,
Colin Cross635c3b02016-05-18 15:37:25 -07002331 flags Flags, deps PathDeps, objFiles android.Paths) android.Path {
Colin Cross3f40fa42015-01-30 17:27:36 -08002332
2333 libName := ctx.ModuleName() + staticLibraryExtension
Colin Cross635c3b02016-05-18 15:37:25 -07002334 outputFile := android.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08002335
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08002336 if flags.Clang {
2337 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
2338 }
2339
Colin Crossca860ac2016-01-04 14:34:37 -08002340 CopyGccLib(ctx, libName, flagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002341
2342 ctx.CheckbuildFile(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08002343
Colin Crossca860ac2016-01-04 14:34:37 -08002344 return outputFile
Dan Albertc403f7c2015-03-18 14:01:18 -07002345}
2346
Colin Crossc99deeb2016-04-11 15:06:20 -07002347func (*toolchainLibraryLinker) installable() bool {
2348 return false
2349}
2350
Dan Albertbe961682015-03-18 23:38:50 -07002351// NDK prebuilt libraries.
2352//
2353// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
2354// either (with the exception of the shared STLs, which are installed to the app's directory rather
2355// than to the system image).
2356
Colin Cross635c3b02016-05-18 15:37:25 -07002357func getNdkLibDir(ctx android.ModuleContext, toolchain Toolchain, version string) android.SourcePath {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002358 suffix := ""
2359 // Most 64-bit NDK prebuilts store libraries in "lib64", except for arm64 which is not a
2360 // multilib toolchain and stores the libraries in "lib".
Colin Cross635c3b02016-05-18 15:37:25 -07002361 if toolchain.Is64Bit() && ctx.Arch().ArchType != android.Arm64 {
Colin Crossc7fd91a2016-05-17 13:15:15 -07002362 suffix = "64"
2363 }
Colin Cross635c3b02016-05-18 15:37:25 -07002364 return android.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib%s",
Colin Crossc7fd91a2016-05-17 13:15:15 -07002365 version, toolchain.Name(), suffix))
Dan Albertbe961682015-03-18 23:38:50 -07002366}
2367
Colin Cross635c3b02016-05-18 15:37:25 -07002368func ndkPrebuiltModuleToPath(ctx android.ModuleContext, toolchain Toolchain,
2369 ext string, version string) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002370
2371 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
2372 // We want to translate to just NAME.EXT
2373 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
2374 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002375 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07002376}
2377
Colin Crossca860ac2016-01-04 14:34:37 -08002378type ndkPrebuiltObjectLinker struct {
2379 objectLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002380}
2381
Colin Crossca860ac2016-01-04 14:34:37 -08002382func (*ndkPrebuiltObjectLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertc3144b12015-04-28 18:17:56 -07002383 // NDK objects can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002384 return deps
Dan Albertc3144b12015-04-28 18:17:56 -07002385}
2386
Colin Crossca860ac2016-01-04 14:34:37 -08002387func ndkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002388 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002389 module.linker = &ndkPrebuiltObjectLinker{}
2390 return module.Init()
Dan Albertc3144b12015-04-28 18:17:56 -07002391}
2392
Colin Crossca860ac2016-01-04 14:34:37 -08002393func (c *ndkPrebuiltObjectLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002394 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07002395 // A null build step, but it sets up the output path.
2396 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2397 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2398 }
2399
Colin Crossca860ac2016-01-04 14:34:37 -08002400 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, ctx.sdkVersion())
Dan Albertc3144b12015-04-28 18:17:56 -07002401}
2402
Colin Crossca860ac2016-01-04 14:34:37 -08002403type ndkPrebuiltLibraryLinker struct {
2404 libraryLinker
Dan Albertc3144b12015-04-28 18:17:56 -07002405}
2406
Colin Crossca860ac2016-01-04 14:34:37 -08002407var _ baseLinkerInterface = (*ndkPrebuiltLibraryLinker)(nil)
2408var _ exportedFlagsProducer = (*libraryLinker)(nil)
Dan Albertc3144b12015-04-28 18:17:56 -07002409
Colin Crossca860ac2016-01-04 14:34:37 -08002410func (ndk *ndkPrebuiltLibraryLinker) props() []interface{} {
Colin Cross919281a2016-04-05 16:42:05 -07002411 return append(ndk.libraryLinker.props(), &ndk.Properties, &ndk.flagExporter.Properties)
Dan Albertbe961682015-03-18 23:38:50 -07002412}
2413
Colin Crossca860ac2016-01-04 14:34:37 -08002414func (*ndkPrebuiltLibraryLinker) deps(ctx BaseModuleContext, deps Deps) Deps {
Dan Albertbe961682015-03-18 23:38:50 -07002415 // NDK libraries can't have any dependencies
Colin Crossca860ac2016-01-04 14:34:37 -08002416 return deps
Dan Albertbe961682015-03-18 23:38:50 -07002417}
2418
Colin Crossca860ac2016-01-04 14:34:37 -08002419func ndkPrebuiltLibraryFactory() (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 linker := &ndkPrebuiltLibraryLinker{}
2422 linker.dynamicProperties.BuildShared = true
2423 module.linker = linker
2424 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002425}
2426
Colin Crossca860ac2016-01-04 14:34:37 -08002427func (ndk *ndkPrebuiltLibraryLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002428 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002429 // A null build step, but it sets up the output path.
2430 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2431 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2432 }
2433
Colin Cross919281a2016-04-05 16:42:05 -07002434 ndk.exportIncludes(ctx, "-isystem")
Dan Albertbe961682015-03-18 23:38:50 -07002435
Colin Crossca860ac2016-01-04 14:34:37 -08002436 return ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
2437 ctx.sdkVersion())
Dan Albertbe961682015-03-18 23:38:50 -07002438}
2439
2440// The NDK STLs are slightly different from the prebuilt system libraries:
2441// * Are not specific to each platform version.
2442// * The libraries are not in a predictable location for each STL.
2443
Colin Crossca860ac2016-01-04 14:34:37 -08002444type ndkPrebuiltStlLinker struct {
2445 ndkPrebuiltLibraryLinker
Dan Albertbe961682015-03-18 23:38:50 -07002446}
2447
Colin Crossca860ac2016-01-04 14:34:37 -08002448func ndkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002449 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002450 linker := &ndkPrebuiltStlLinker{}
2451 linker.dynamicProperties.BuildShared = true
2452 module.linker = linker
2453 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002454}
2455
Colin Crossca860ac2016-01-04 14:34:37 -08002456func ndkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
Colin Cross635c3b02016-05-18 15:37:25 -07002457 module := newBaseModule(android.DeviceSupported, android.MultilibBoth)
Colin Crossca860ac2016-01-04 14:34:37 -08002458 linker := &ndkPrebuiltStlLinker{}
2459 linker.dynamicProperties.BuildStatic = true
2460 module.linker = linker
2461 return module.Init()
Dan Albertbe961682015-03-18 23:38:50 -07002462}
2463
Colin Cross635c3b02016-05-18 15:37:25 -07002464func getNdkStlLibDir(ctx android.ModuleContext, toolchain Toolchain, stl string) android.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002465 gccVersion := toolchain.GccVersion()
2466 var libDir string
2467 switch stl {
2468 case "libstlport":
2469 libDir = "cxx-stl/stlport/libs"
2470 case "libc++":
2471 libDir = "cxx-stl/llvm-libc++/libs"
2472 case "libgnustl":
2473 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2474 }
2475
2476 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002477 ndkSrcRoot := "prebuilts/ndk/current/sources"
Colin Cross635c3b02016-05-18 15:37:25 -07002478 return android.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002479 }
2480
2481 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Colin Cross635c3b02016-05-18 15:37:25 -07002482 return android.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002483}
2484
Colin Crossca860ac2016-01-04 14:34:37 -08002485func (ndk *ndkPrebuiltStlLinker) link(ctx ModuleContext, flags Flags,
Colin Cross635c3b02016-05-18 15:37:25 -07002486 deps PathDeps, objFiles android.Paths) android.Path {
Dan Albertbe961682015-03-18 23:38:50 -07002487 // A null build step, but it sets up the output path.
2488 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2489 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2490 }
2491
Colin Cross919281a2016-04-05 16:42:05 -07002492 ndk.exportIncludes(ctx, "-I")
Dan Albertbe961682015-03-18 23:38:50 -07002493
2494 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002495 libExt := flags.Toolchain.ShlibSuffix()
Colin Crossca860ac2016-01-04 14:34:37 -08002496 if ndk.dynamicProperties.BuildStatic {
Dan Albertbe961682015-03-18 23:38:50 -07002497 libExt = staticLibraryExtension
2498 }
2499
2500 stlName := strings.TrimSuffix(libName, "_shared")
2501 stlName = strings.TrimSuffix(stlName, "_static")
2502 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Colin Crossca860ac2016-01-04 14:34:37 -08002503 return libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002504}
2505
Colin Cross635c3b02016-05-18 15:37:25 -07002506func linkageMutator(mctx android.BottomUpMutatorContext) {
Colin Crossca860ac2016-01-04 14:34:37 -08002507 if m, ok := mctx.Module().(*Module); ok {
2508 if m.linker != nil {
2509 if linker, ok := m.linker.(baseLinkerInterface); ok {
2510 var modules []blueprint.Module
2511 if linker.buildStatic() && linker.buildShared() {
2512 modules = mctx.CreateLocalVariations("static", "shared")
Colin Crossc99deeb2016-04-11 15:06:20 -07002513 static := modules[0].(*Module)
2514 shared := modules[1].(*Module)
2515
2516 static.linker.(baseLinkerInterface).setStatic(true)
2517 shared.linker.(baseLinkerInterface).setStatic(false)
2518
2519 if staticCompiler, ok := static.compiler.(*libraryCompiler); ok {
2520 sharedCompiler := shared.compiler.(*libraryCompiler)
2521 if len(staticCompiler.Properties.Static.Cflags) == 0 &&
2522 len(sharedCompiler.Properties.Shared.Cflags) == 0 {
2523 // Optimize out compiling common .o files twice for static+shared libraries
2524 mctx.AddInterVariantDependency(reuseObjTag, shared, static)
2525 sharedCompiler.baseCompiler.Properties.Srcs = nil
2526 }
2527 }
Colin Crossca860ac2016-01-04 14:34:37 -08002528 } else if linker.buildStatic() {
2529 modules = mctx.CreateLocalVariations("static")
2530 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(true)
2531 } else if linker.buildShared() {
2532 modules = mctx.CreateLocalVariations("shared")
2533 modules[0].(*Module).linker.(baseLinkerInterface).setStatic(false)
2534 } else {
2535 panic(fmt.Errorf("library %q not static or shared", mctx.ModuleName()))
2536 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002537 }
2538 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002539 }
2540}
Colin Cross74d1ec02015-04-28 13:30:13 -07002541
2542// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2543// modifies the slice contents in place, and returns a subslice of the original slice
2544func lastUniqueElements(list []string) []string {
2545 totalSkip := 0
2546 for i := len(list) - 1; i >= totalSkip; i-- {
2547 skip := 0
2548 for j := i - 1; j >= totalSkip; j-- {
2549 if list[i] == list[j] {
2550 skip++
2551 } else {
2552 list[j+skip] = list[j]
2553 }
2554 }
2555 totalSkip += skip
2556 }
2557 return list[totalSkip:]
2558}
Colin Cross06a931b2015-10-28 17:23:31 -07002559
2560var Bool = proptools.Bool