blob: 4105811668a88c5327c733effb38c32dcda8dbc4 [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 Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
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() {
35 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
37 soong.RegisterModuleType("cc_library", CCLibraryFactory)
38 soong.RegisterModuleType("cc_object", CCObjectFactory)
39 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
40 soong.RegisterModuleType("cc_test", CCTestFactory)
41 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080042 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
44 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)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
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 Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080083
84 // COMMON_RELEASE_CFLAGS
85 "-DNDEBUG",
86 "-UDEBUG",
87 }
88
89 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080090 "-fdiagnostics-color",
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092 // TARGET_ERROR_FLAGS
93 "-Werror=return-type",
94 "-Werror=non-virtual-dtor",
95 "-Werror=address",
96 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080097 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080098 }
99
100 hostGlobalCflags = []string{}
101
102 commonGlobalCppflags = []string{
103 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700104 }
105
Dan Willemsenbe03f342016-03-03 17:21:04 -0800106 noOverrideGlobalCflags = []string{
107 "-Werror=int-to-pointer-cast",
108 "-Werror=pointer-to-int-cast",
109 }
110
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700111 illegalFlags = []string{
112 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 }
114)
115
116func init() {
117 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
118 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
119 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800120 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800121
122 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
123
124 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800125 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800126 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800127 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 pctx.StaticVariable("hostClangGlobalCflags",
129 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800130 pctx.StaticVariable("noOverrideClangGlobalCflags",
131 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
132
Tim Kilbournf2948142015-03-11 12:03:03 -0700133 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800134 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800135
136 // Everything in this list is a crime against abstraction and dependency tracking.
137 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800138 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700139 []string{
140 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800141 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 "hardware/libhardware/include",
143 "hardware/libhardware_legacy/include",
144 "hardware/ril/include",
145 "libnativehelper/include",
146 "frameworks/native/include",
147 "frameworks/native/opengl/include",
148 "frameworks/av/include",
149 "frameworks/base/include",
150 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800151 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
152 // with this, since there is no associated library.
153 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
154 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800155
Dan Willemsendc5d28a2016-03-16 11:37:17 -0700156 pctx.SourcePathVariable("clangDefaultBase", "prebuilts/clang/host")
157 pctx.VariableFunc("clangBase", func(config interface{}) (string, error) {
158 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_BASE"); override != "" {
159 return override, nil
160 }
161 return "${clangDefaultBase}", nil
162 })
163 pctx.VariableFunc("clangVersion", func(config interface{}) (string, error) {
164 if override := config.(common.Config).Getenv("LLVM_PREBUILTS_VERSION"); override != "" {
165 return override, nil
166 }
167 return "clang-2658975", nil
168 })
169 pctx.StaticVariable("clangPath", "${clangBase}/${HostPrebuiltTag}/${clangVersion}/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800170}
171
Colin Cross6362e272015-10-29 15:25:03 -0700172type CCModuleContext common.AndroidBaseContext
173
Colin Cross3f40fa42015-01-30 17:27:36 -0800174// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700175type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800176 common.AndroidModule
177
Colin Crossfa138792015-04-24 17:31:52 -0700178 // Modify property values after parsing Blueprints file but before starting dependency
179 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700180 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700181
Colin Cross21b9a242015-03-24 14:15:58 -0700182 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700183 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800184
Colin Cross6362e272015-10-29 15:25:03 -0700185 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700186 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800187
Colin Cross6362e272015-10-29 15:25:03 -0700188 // Add dynamic dependencies
189 depsMutator(common.AndroidBottomUpMutatorContext)
190
Colin Cross3f40fa42015-01-30 17:27:36 -0800191 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700192 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800193
Dan Albertc403f7c2015-03-18 14:01:18 -0700194 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700195 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700196
Colin Cross3f40fa42015-01-30 17:27:36 -0800197 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800199}
200
Colin Cross97ba0732015-03-23 17:50:24 -0700201type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700202 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700203
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700204 ObjFiles common.Paths
205
206 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700207
Colin Cross97ba0732015-03-23 17:50:24 -0700208 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700209}
210
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700211type CCPathDeps struct {
212 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
213
214 ObjFiles common.Paths
215 WholeStaticLibObjFiles common.Paths
216
217 Cflags, ReexportedCflags []string
218
219 CrtBegin, CrtEnd common.OptionalPath
220}
221
Colin Cross97ba0732015-03-23 17:50:24 -0700222type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700223 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
224 AsFlags []string // Flags that apply to assembly source files
225 CFlags []string // Flags that apply to C and C++ source files
226 ConlyFlags []string // Flags that apply to C source files
227 CppFlags []string // Flags that apply to C++ source files
228 YaccFlags []string // Flags that apply to Yacc source files
229 LdFlags []string // Flags that apply to linker command lines
230
231 Nocrt bool
232 Toolchain Toolchain
233 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700234}
235
Colin Cross7d5136f2015-05-11 13:39:40 -0700236// Properties used to compile all C or C++ modules
237type CCBaseProperties struct {
238 // 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 -0700239 Srcs []string `android:"arch_variant"`
240
241 // list of source files that should not be used to build the C/C++ module.
242 // This is most useful in the arch/multilib variants to remove non-common files
243 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700244
245 // list of module-specific flags that will be used for C and C++ compiles.
246 Cflags []string `android:"arch_variant"`
247
248 // list of module-specific flags that will be used for C++ compiles
249 Cppflags []string `android:"arch_variant"`
250
251 // list of module-specific flags that will be used for C compiles
252 Conlyflags []string `android:"arch_variant"`
253
254 // list of module-specific flags that will be used for .S compiles
255 Asflags []string `android:"arch_variant"`
256
257 // list of module-specific flags that will be used for .y and .yy compiles
258 Yaccflags []string
259
260 // list of module-specific flags that will be used for all link steps
261 Ldflags []string `android:"arch_variant"`
262
263 // 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
Colin Cross39d97f22015-09-14 12:30:50 -0700274 // list of files relative to the root of the source tree that will be included
275 // using -include.
276 // If possible, don't use this.
277 Include_files []string `android:"arch_variant"`
278
Colin Cross7d5136f2015-05-11 13:39:40 -0700279 // list of directories relative to the Blueprints file that will
280 // be added to the include path using -I
281 Local_include_dirs []string `android:"arch_variant"`
282
Colin Cross39d97f22015-09-14 12:30:50 -0700283 // list of files relative to the Blueprints file that will be included
284 // using -include.
285 // If possible, don't use this.
286 Local_include_files []string `android:"arch_variant"`
287
Colin Cross7d5136f2015-05-11 13:39:40 -0700288 // list of directories relative to the Blueprints file that will
289 // be added to the include path using -I for any module that links against this module
290 Export_include_dirs []string `android:"arch_variant"`
291
292 // list of module-specific flags that will be used for C and C++ compiles when
293 // compiling with clang
294 Clang_cflags []string `android:"arch_variant"`
295
296 // list of module-specific flags that will be used for .S compiles when
297 // compiling with clang
298 Clang_asflags []string `android:"arch_variant"`
299
300 // list of system libraries that will be dynamically linked to
301 // shared library and executable modules. If unset, generally defaults to libc
302 // and libm. Set to [] to prevent linking against libc and libm.
303 System_shared_libs []string
304
305 // list of modules whose object files should be linked into this module
306 // in their entirety. For static library modules, all of the .o files from the intermediate
307 // directory of the dependency will be linked into this modules .a file. For a shared library,
308 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
309 Whole_static_libs []string `android:"arch_variant"`
310
311 // list of modules that should be statically linked into this module.
312 Static_libs []string `android:"arch_variant"`
313
314 // list of modules that should be dynamically linked into this module.
315 Shared_libs []string `android:"arch_variant"`
316
317 // allow the module to contain undefined symbols. By default,
318 // modules cannot contain undefined symbols that are not satisified by their immediate
319 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
320 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700321 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700322
323 // don't link in crt_begin and crt_end. This flag should only be necessary for
324 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700325 Nocrt *bool `android:"arch_variant"`
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 // don't insert default compiler flags into asflags, cflags,
331 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700332 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700333
334 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700335 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700336
337 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700338 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700339
340 // -l arguments to pass to linker for host-provided shared libraries
341 Host_ldlibs []string `android:"arch_variant"`
342
343 // select the STL library to use. Possible values are "libc++", "libc++_static",
344 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
345 // default
346 Stl string
347
348 // Set for combined shared/static libraries to prevent compiling object files a second time
349 SkipCompileObjs bool `blueprint:"mutated"`
350
351 Debug, Release struct {
352 // list of module-specific flags that will be used for C and C++ compiles in debug or
353 // release builds
354 Cflags []string `android:"arch_variant"`
355 } `android:"arch_variant"`
356
357 // Minimum sdk version supported when compiling against the ndk
358 Sdk_version string
359
360 // install to a subdirectory of the default install path for the module
361 Relative_install_path string
362}
363
Colin Crosscfad1192015-11-02 16:43:11 -0800364type CCUnusedProperties struct {
365 Native_coverage *bool
366 Required []string
367 Sanitize []string `android:"arch_variant"`
368 Sanitize_recover []string
369 Strip string
370 Tags []string
371}
372
Colin Crossfa138792015-04-24 17:31:52 -0700373// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700374// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
375// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700376type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700377 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800378 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700379 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700380
Colin Cross7d5136f2015-05-11 13:39:40 -0700381 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700382
Colin Crosscfad1192015-11-02 16:43:11 -0800383 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700384
385 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700386
387 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700388}
389
Colin Crossfa138792015-04-24 17:31:52 -0700390func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700391 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
392
393 base.module = module
394
Colin Crossfa138792015-04-24 17:31:52 -0700395 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700396
Colin Crosscfad1192015-11-02 16:43:11 -0800397 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
398
399 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700400}
401
Colin Crossfa138792015-04-24 17:31:52 -0700402func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800403 toolchain := c.findToolchain(ctx)
404 if ctx.Failed() {
405 return
406 }
407
Colin Cross21b9a242015-03-24 14:15:58 -0700408 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800409 if ctx.Failed() {
410 return
411 }
412
Colin Cross74d1ec02015-04-28 13:30:13 -0700413 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800414 if ctx.Failed() {
415 return
416 }
417
Colin Cross28344522015-04-22 13:07:53 -0700418 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700419
Colin Cross581c1892015-04-07 16:50:10 -0700420 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800421 if ctx.Failed() {
422 return
423 }
424
Colin Cross581c1892015-04-07 16:50:10 -0700425 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700426 if ctx.Failed() {
427 return
428 }
429
430 objFiles = append(objFiles, generatedObjFiles...)
431
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
433 if ctx.Failed() {
434 return
435 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700436
437 c.ccModuleType().installModule(ctx, flags)
438 if ctx.Failed() {
439 return
440 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800441}
442
Colin Crossfa138792015-04-24 17:31:52 -0700443func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800444 return c.module
445}
446
Colin Crossfa138792015-04-24 17:31:52 -0700447func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800448 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700449 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800450 ht := ctx.HostType()
451 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800452 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800453 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800454 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800455 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800456 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800457}
458
Colin Cross6362e272015-10-29 15:25:03 -0700459func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700460}
461
Colin Crosse11befc2015-04-27 17:49:17 -0700462func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700463 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
464 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
465 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700466
Colin Cross21b9a242015-03-24 14:15:58 -0700467 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800468}
469
Colin Cross6362e272015-10-29 15:25:03 -0700470func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700471 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
472 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
473 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
474 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
475
476 staticLibs := c.savedDepNames.WholeStaticLibs
477 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
478 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700479 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800480
Colin Cross74d1ec02015-04-28 13:30:13 -0700481 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700482
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700483 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700484 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700485 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700486 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700487 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700488 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700489 }
Colin Cross6362e272015-10-29 15:25:03 -0700490}
Colin Cross21b9a242015-03-24 14:15:58 -0700491
Colin Cross6362e272015-10-29 15:25:03 -0700492func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
493 if c, ok := ctx.Module().(CCModuleType); ok {
494 c.ModifyProperties(ctx)
495 c.depsMutator(ctx)
496 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800497}
498
499// Create a ccFlags struct that collects the compile flags from global values,
500// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700501func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700502 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700503 CFlags: c.Properties.Cflags,
504 CppFlags: c.Properties.Cppflags,
505 ConlyFlags: c.Properties.Conlyflags,
506 LdFlags: c.Properties.Ldflags,
507 AsFlags: c.Properties.Asflags,
508 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700509 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700510 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700511 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800512 }
Colin Cross28344522015-04-22 13:07:53 -0700513
514 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700515 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
516 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700517 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700518 includeDirsToFlags(localIncludeDirs),
519 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700520
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700521 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
522 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700523
524 flags.GlobalFlags = append(flags.GlobalFlags,
525 includeFilesToFlags(rootIncludeFiles),
526 includeFilesToFlags(localIncludeFiles))
527
Colin Cross06a931b2015-10-28 17:23:31 -0700528 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700529 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700530 flags.GlobalFlags = append(flags.GlobalFlags,
531 "${commonGlobalIncludes}",
532 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800533 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700534 }
535
536 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700537 "-I" + common.PathForModuleSrc(ctx).String(),
538 "-I" + common.PathForModuleOut(ctx).String(),
539 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700540 }...)
541 }
542
Colin Cross06a931b2015-10-28 17:23:31 -0700543 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700544 if ctx.Host() {
545 flags.Clang = true
546 }
547
548 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
549 flags.Clang = true
550 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800551 }
552
Dan Willemsen490fd492015-11-24 17:53:15 -0800553 if !toolchain.ClangSupported() {
554 flags.Clang = false
555 }
556
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800557 instructionSet := c.Properties.Instruction_set
558 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
559 if flags.Clang {
560 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
561 }
562 if err != nil {
563 ctx.ModuleErrorf("%s", err)
564 }
565
566 // TODO: debug
567 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
568
Colin Cross97ba0732015-03-23 17:50:24 -0700569 if flags.Clang {
570 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700571 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
572 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700573 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
574 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
575 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800576
577 target := "-target " + toolchain.ClangTriple()
578 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
579
Colin Cross97ba0732015-03-23 17:50:24 -0700580 flags.CFlags = append(flags.CFlags, target, gccPrefix)
581 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
582 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 }
584
Colin Cross06a931b2015-10-28 17:23:31 -0700585 if !Bool(c.Properties.No_default_compiler_flags) {
586 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700587 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800588 }
589
Colin Cross56b4d452015-04-21 17:38:44 -0700590 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
591
Colin Cross97ba0732015-03-23 17:50:24 -0700592 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800593 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700594 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700595 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 toolchain.ClangCflags(),
597 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700598 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800599
600 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800601 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700602 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700603 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 toolchain.Cflags(),
605 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700606 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800607 }
608
Colin Cross7b66f152015-12-15 16:07:43 -0800609 if Bool(ctx.AConfig().ProductVariables.Brillo) {
610 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
611 }
612
Colin Crossf6566ed2015-03-24 11:13:38 -0700613 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700614 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700615 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700617 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800618 }
619 }
620
Colin Cross97ba0732015-03-23 17:50:24 -0700621 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800622
Colin Cross97ba0732015-03-23 17:50:24 -0700623 if flags.Clang {
624 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
625 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800626 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700627 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
628 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800629 }
Colin Cross28344522015-04-22 13:07:53 -0700630
631 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700632 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700633 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800634 }
635
Colin Crossc4bde762015-11-23 16:11:30 -0800636 if flags.Clang {
637 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
638 } else {
639 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800640 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800641 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800642
Colin Cross0676e2d2015-04-24 17:39:18 -0700643 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800644
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700645 if c.Properties.Sdk_version == "" {
646 if ctx.Host() && !flags.Clang {
647 // The host GCC doesn't support C++14 (and is deprecated, so likely
648 // never will). Build these modules with C++11.
649 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
650 } else {
651 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
652 }
653 }
654
655 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
656 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
657 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
658
Dan Willemsen52b1cd22016-03-01 13:36:34 -0800659 // We can enforce some rules more strictly in the code we own. strict
660 // indicates if this is code that we can be stricter with. If we have
661 // rules that we want to apply to *our* code (but maybe can't for
662 // vendor/device specific things), we could extend this to be a ternary
663 // value.
664 strict := true
665 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
666 strict = false
667 }
668
669 // Can be used to make some annotations stricter for code we can fix
670 // (such as when we mark functions as deprecated).
671 if strict {
672 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
673 }
674
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 // Optimization to reduce size of build.ninja
676 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700677 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
678 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
679 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
680 flags.CFlags = []string{"$cflags"}
681 flags.CppFlags = []string{"$cppflags"}
682 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800683
684 return flags
685}
686
Colin Cross0676e2d2015-04-24 17:39:18 -0700687func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800688 return flags
689}
690
691// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700692func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700693 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700694
695 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800696
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700697 inputFiles := ctx.ExpandSources(srcFiles, excludes)
698 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800699
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700700 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800701}
702
Colin Crossfa138792015-04-24 17:31:52 -0700703// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700704func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800705
Colin Crossfa138792015-04-24 17:31:52 -0700706 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800707 return nil
708 }
709
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700710 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800711}
712
Colin Cross5049f022015-03-18 13:28:46 -0700713// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700714func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
715 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700716
Colin Crossfa138792015-04-24 17:31:52 -0700717 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700718 return nil
719 }
720
721 ctx.VisitDirectDeps(func(module blueprint.Module) {
722 if gen, ok := module.(genrule.SourceFileGenerator); ok {
723 srcs = append(srcs, gen.GeneratedSourceFiles()...)
724 }
725 })
726
727 if len(srcs) == 0 {
728 return nil
729 }
730
Colin Cross581c1892015-04-07 16:50:10 -0700731 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700732}
733
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700734func (c *CCBase) outputFile() common.OptionalPath {
735 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800736}
737
Colin Crossfa138792015-04-24 17:31:52 -0700738func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800739 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700740 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800741
742 for _, n := range names {
743 found := false
744 ctx.VisitDirectDeps(func(m blueprint.Module) {
745 otherName := ctx.OtherModuleName(m)
746 if otherName != n {
747 return
748 }
749
Colin Cross97ba0732015-03-23 17:50:24 -0700750 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800751 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800752 // If a cc_library host+device module depends on a library that exists as both
753 // cc_library_shared and cc_library_host_shared, it will end up with two
754 // dependencies with the same name, one of which is marked disabled for each
755 // of host and device. Ignore the disabled one.
756 return
757 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700758 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800759 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
760 otherName)
761 return
762 }
763
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700764 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800765 if found {
766 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
767 return
768 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700769 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800770 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700771 if i, ok := a.(ccExportedFlagsProducer); ok {
772 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800773 }
774 found = true
775 } else {
776 ctx.ModuleErrorf("module %q missing output file", otherName)
777 return
778 }
779 } else {
780 ctx.ModuleErrorf("module %q not an android module", otherName)
781 return
782 }
783 })
Colin Cross6ff51382015-12-17 16:39:19 -0800784 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800785 ctx.ModuleErrorf("unsatisified dependency on %q", n)
786 }
787 }
788
Colin Cross28344522015-04-22 13:07:53 -0700789 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800790}
791
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700792// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700793// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700794func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
795 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700796 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800797
Colin Cross21b9a242015-03-24 14:15:58 -0700798 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800799
Colin Cross28344522015-04-22 13:07:53 -0700800 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700801 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700802 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800803 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800804
Colin Cross21b9a242015-03-24 14:15:58 -0700805 for _, m := range wholeStaticLibModules {
806 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800807 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
808 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
809 for i := range missingDeps {
810 missingDeps[i] += postfix
811 }
812 ctx.AddMissingDependencies(missingDeps)
813 }
Colin Cross21b9a242015-03-24 14:15:58 -0700814 depPaths.WholeStaticLibObjFiles =
815 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
816 } else {
817 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
818 }
819 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800820
Colin Cross28344522015-04-22 13:07:53 -0700821 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
822 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700823
Colin Cross28344522015-04-22 13:07:53 -0700824 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
825 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700826
Colin Cross28344522015-04-22 13:07:53 -0700827 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
828 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700829
830 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700831 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700832 otherName := ctx.OtherModuleName(m)
833 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700834 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700835 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700836 }
837 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700838 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700839 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700840 }
841 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700842 output := obj.object().outputFile()
843 if output.Valid() {
844 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
845 } else {
846 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
847 }
Colin Cross21b9a242015-03-24 14:15:58 -0700848 }
849 }
850 })
851
852 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800853}
854
Colin Cross7d5136f2015-05-11 13:39:40 -0700855type ccLinkedProperties struct {
856 VariantIsShared bool `blueprint:"mutated"`
857 VariantIsStatic bool `blueprint:"mutated"`
858 VariantIsStaticBinary bool `blueprint:"mutated"`
859}
860
Colin Crossfa138792015-04-24 17:31:52 -0700861// CCLinked contains the properties and members used by libraries and executables
862type CCLinked struct {
863 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700864 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800865}
866
Colin Crossfa138792015-04-24 17:31:52 -0700867func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700868 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
869
Colin Crossed4cf0b2015-03-26 14:43:45 -0700870 props = append(props, &dynamic.dynamicProperties)
871
Colin Crossfa138792015-04-24 17:31:52 -0700872 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700873}
874
Colin Crossfa138792015-04-24 17:31:52 -0700875func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700876 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700877 return c.Properties.System_shared_libs
878 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700879 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700880 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700881 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800882 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800883}
884
Colin Crossfa138792015-04-24 17:31:52 -0700885func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
886 if c.Properties.Sdk_version != "" && ctx.Device() {
887 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700888 case "":
889 return "ndk_system"
890 case "c++_shared", "c++_static",
891 "stlport_shared", "stlport_static",
892 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700893 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700894 default:
Colin Crossfa138792015-04-24 17:31:52 -0700895 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700896 return ""
897 }
898 }
899
Dan Willemsen490fd492015-11-24 17:53:15 -0800900 if ctx.HostType() == common.Windows {
901 switch c.Properties.Stl {
902 case "libc++", "libc++_static", "libstdc++", "":
903 // libc++ is not supported on mingw
904 return "libstdc++"
905 case "none":
906 return ""
907 default:
908 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
909 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700910 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800911 } else {
912 switch c.Properties.Stl {
913 case "libc++", "libc++_static",
914 "libstdc++":
915 return c.Properties.Stl
916 case "none":
917 return ""
918 case "":
919 if c.static() {
920 return "libc++_static"
921 } else {
922 return "libc++"
923 }
924 default:
925 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
926 return ""
927 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700928 }
929}
930
Dan Willemsen490fd492015-11-24 17:53:15 -0800931var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700932
933func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800934 hostDynamicGccLibs = map[common.HostType][]string{
935 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
936 common.Darwin: []string{"-lc", "-lSystem"},
937 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
938 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
939 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
940 "-lmsvcrt"},
941 }
942 hostStaticGccLibs = map[common.HostType][]string{
943 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
944 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
945 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700946 }
947}
Colin Cross712fc022015-04-27 11:13:34 -0700948
Colin Crosse11befc2015-04-27 17:49:17 -0700949func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700950 stl := c.stl(ctx)
951 if ctx.Failed() {
952 return flags
953 }
954
955 switch stl {
956 case "libc++", "libc++_static":
957 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700958 if ctx.Host() {
959 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
960 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Dan Willemsenb9553362016-03-03 19:24:03 -0800961 flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
Colin Cross18b6dc52015-04-28 13:20:37 -0700962 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800963 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700964 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800965 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700966 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700967 } else {
968 if ctx.Arch().ArchType == common.Arm {
969 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
970 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700971 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700972 case "libstdc++":
973 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
974 // tree is in good enough shape to not need it.
975 // Host builds will use GNU libstdc++.
976 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700977 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Dan Willemsen282a4b02016-03-09 10:30:22 -0800978 } else {
979 // Host builds will use the system C++. libc++ on Darwin, GNU libstdc++ everywhere else
980 flags.CppFlags = append(flags.CppFlags, flags.Toolchain.SystemCppCppflags())
981 flags.LdFlags = append(flags.LdFlags, flags.Toolchain.SystemCppLdflags())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700982 }
983 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700984 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
985 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700986 case "ndk_libc++_shared", "ndk_libc++_static":
987 // TODO(danalbert): This really shouldn't be here...
988 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
989 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
990 // Nothing
991 case "":
992 // None or error.
993 if ctx.Host() {
994 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
995 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700996 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800997 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700998 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800999 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -07001000 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001001 }
1002 default:
Colin Crossfa138792015-04-24 17:31:52 -07001003 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001004 }
1005
1006 return flags
1007}
1008
Colin Crosse11befc2015-04-27 17:49:17 -07001009func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1010 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -08001011
Colin Crossed4cf0b2015-03-26 14:43:45 -07001012 stl := c.stl(ctx)
1013 if ctx.Failed() {
1014 return depNames
1015 }
1016
1017 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001018 case "libstdc++":
1019 if ctx.Device() {
1020 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1021 }
Colin Cross74d1ec02015-04-28 13:30:13 -07001022 case "libc++", "libc++_static":
1023 if stl == "libc++" {
1024 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1025 } else {
1026 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1027 }
1028 if ctx.Device() {
1029 if ctx.Arch().ArchType == common.Arm {
1030 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
1031 }
1032 if c.staticBinary() {
1033 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
1034 } else {
1035 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
1036 }
1037 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001038 case "":
1039 // None or error.
1040 case "ndk_system":
1041 // TODO: Make a system STL prebuilt for the NDK.
1042 // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have
Colin Crossfa138792015-04-24 17:31:52 -07001043 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -07001044 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001045 case "ndk_libc++_shared", "ndk_libstlport_shared":
1046 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1047 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
1048 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1049 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001050 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001051 }
1052
Colin Cross74d1ec02015-04-28 13:30:13 -07001053 if ctx.ModuleName() != "libcompiler_rt-extras" {
1054 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1055 }
1056
Colin Crossf6566ed2015-03-24 11:13:38 -07001057 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001058 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001059 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001060 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001061 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1062 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001063
Colin Cross18b6dc52015-04-28 13:20:37 -07001064 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001065 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1066 }
Colin Cross577f6e42015-03-27 18:23:34 -07001067
Colin Crossfa138792015-04-24 17:31:52 -07001068 if c.Properties.Sdk_version != "" {
1069 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001070 depNames.SharedLibs = append(depNames.SharedLibs,
1071 "ndk_libc."+version,
1072 "ndk_libm."+version,
1073 )
1074 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001075 }
1076
Colin Cross21b9a242015-03-24 14:15:58 -07001077 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001078}
1079
Colin Crossed4cf0b2015-03-26 14:43:45 -07001080// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1081type ccLinkedInterface interface {
1082 // Returns true if the build options for the module have selected a static or shared build
1083 buildStatic() bool
1084 buildShared() bool
1085
1086 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001087 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001088
Colin Cross18b6dc52015-04-28 13:20:37 -07001089 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001090 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001091
1092 // Returns whether a module is a static binary
1093 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001094}
1095
1096var _ ccLinkedInterface = (*CCLibrary)(nil)
1097var _ ccLinkedInterface = (*CCBinary)(nil)
1098
Colin Crossfa138792015-04-24 17:31:52 -07001099func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001100 return c.dynamicProperties.VariantIsStatic
1101}
1102
Colin Cross18b6dc52015-04-28 13:20:37 -07001103func (c *CCLinked) staticBinary() bool {
1104 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001105}
1106
Colin Cross18b6dc52015-04-28 13:20:37 -07001107func (c *CCLinked) setStatic(static bool) {
1108 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001109}
1110
Colin Cross28344522015-04-22 13:07:53 -07001111type ccExportedFlagsProducer interface {
1112 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001113}
1114
1115//
1116// Combined static+shared libraries
1117//
1118
Colin Cross7d5136f2015-05-11 13:39:40 -07001119type CCLibraryProperties struct {
1120 BuildStatic bool `blueprint:"mutated"`
1121 BuildShared bool `blueprint:"mutated"`
1122 Static struct {
1123 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001124 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001125 Cflags []string `android:"arch_variant"`
1126 Whole_static_libs []string `android:"arch_variant"`
1127 Static_libs []string `android:"arch_variant"`
1128 Shared_libs []string `android:"arch_variant"`
1129 } `android:"arch_variant"`
1130 Shared struct {
1131 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001132 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001133 Cflags []string `android:"arch_variant"`
1134 Whole_static_libs []string `android:"arch_variant"`
1135 Static_libs []string `android:"arch_variant"`
1136 Shared_libs []string `android:"arch_variant"`
1137 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001138
1139 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001140 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001141 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001142 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001143 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001144 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001145 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001146 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001147}
1148
Colin Cross97ba0732015-03-23 17:50:24 -07001149type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001150 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001151
Colin Cross28344522015-04-22 13:07:53 -07001152 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001153 reuseObjFiles common.Paths
1154 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001155 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001156 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001157 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001158
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001159 // If we're used as a whole_static_lib, our missing dependencies need
1160 // to be given
1161 wholeStaticMissingDeps []string
1162
Colin Cross7d5136f2015-05-11 13:39:40 -07001163 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001164}
1165
Colin Crossed4cf0b2015-03-26 14:43:45 -07001166func (c *CCLibrary) buildStatic() bool {
1167 return c.LibraryProperties.BuildStatic
1168}
1169
1170func (c *CCLibrary) buildShared() bool {
1171 return c.LibraryProperties.BuildShared
1172}
1173
Colin Cross97ba0732015-03-23 17:50:24 -07001174type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001175 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001176 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001177 setReuseFrom(ccLibraryInterface)
1178 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001179 getReuseObjFiles() common.Paths
1180 allObjFiles() common.Paths
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001181 getWholeStaticMissingDeps() []string
Colin Crossc472d572015-03-17 15:06:21 -07001182}
1183
Colin Crossed4cf0b2015-03-26 14:43:45 -07001184var _ ccLibraryInterface = (*CCLibrary)(nil)
1185
Colin Cross97ba0732015-03-23 17:50:24 -07001186func (c *CCLibrary) ccLibrary() *CCLibrary {
1187 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001188}
1189
Colin Cross97ba0732015-03-23 17:50:24 -07001190func NewCCLibrary(library *CCLibrary, module CCModuleType,
1191 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1192
Colin Crossfa138792015-04-24 17:31:52 -07001193 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001194 &library.LibraryProperties)
1195}
1196
1197func CCLibraryFactory() (blueprint.Module, []interface{}) {
1198 module := &CCLibrary{}
1199
1200 module.LibraryProperties.BuildShared = true
1201 module.LibraryProperties.BuildStatic = true
1202
1203 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1204}
1205
Colin Cross0676e2d2015-04-24 17:39:18 -07001206func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001207 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001208 if c.static() {
1209 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1210 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1211 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1212 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001213 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001214 if c.Properties.Sdk_version == "" {
1215 depNames.CrtBegin = "crtbegin_so"
1216 depNames.CrtEnd = "crtend_so"
1217 } else {
1218 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1219 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1220 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001221 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001222 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1223 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1224 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001225 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001226
Dan Willemsen218f6562015-07-08 18:13:11 -07001227 c.systemLibs = c.systemSharedLibs(ctx)
1228
Colin Cross21b9a242015-03-24 14:15:58 -07001229 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001230}
1231
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001232func (c *CCLibrary) outputFile() common.OptionalPath {
1233 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001234}
1235
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001236func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001237 return c.reuseObjFiles
1238}
1239
1240func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1241 c.reuseFrom = reuseFrom
1242}
1243
1244func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1245 return c.reuseFrom
1246}
1247
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001248func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001249 return c.objFiles
1250}
1251
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001252func (c *CCLibrary) getWholeStaticMissingDeps() []string {
1253 return c.wholeStaticMissingDeps
1254}
1255
Colin Cross28344522015-04-22 13:07:53 -07001256func (c *CCLibrary) exportedFlags() []string {
1257 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001258}
1259
Colin Cross0676e2d2015-04-24 17:39:18 -07001260func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001261 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001262
Dan Willemsen490fd492015-11-24 17:53:15 -08001263 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1264 // all code is position independent, and then those warnings get promoted to
1265 // errors.
1266 if ctx.HostType() != common.Windows {
1267 flags.CFlags = append(flags.CFlags, "-fPIC")
1268 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001269
Colin Crossd8e780d2015-04-28 17:39:43 -07001270 if c.static() {
1271 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1272 } else {
1273 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1274 }
1275
Colin Cross18b6dc52015-04-28 13:20:37 -07001276 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001277 libName := ctx.ModuleName()
1278 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1279 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001280 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001281 sharedFlag = "-shared"
1282 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001283 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001284 flags.LdFlags = append(flags.LdFlags,
1285 "-nostdlib",
1286 "-Wl,--gc-sections",
1287 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001288 }
Colin Cross97ba0732015-03-23 17:50:24 -07001289
Colin Cross0af4b842015-04-30 16:36:18 -07001290 if ctx.Darwin() {
1291 flags.LdFlags = append(flags.LdFlags,
1292 "-dynamiclib",
1293 "-single_module",
1294 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001295 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001296 )
1297 } else {
1298 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001299 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001300 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001301 )
1302 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001303 }
Colin Cross97ba0732015-03-23 17:50:24 -07001304
1305 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001306}
1307
Colin Cross97ba0732015-03-23 17:50:24 -07001308func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001309 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001310
1311 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001312 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001313 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001314
1315 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001316 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001317
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001318 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001319
Colin Cross0af4b842015-04-30 16:36:18 -07001320 if ctx.Darwin() {
1321 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1322 } else {
1323 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1324 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001325
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001326 c.wholeStaticMissingDeps = ctx.GetMissingDependencies()
1327
Colin Cross3f40fa42015-01-30 17:27:36 -08001328 c.objFiles = objFiles
1329 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001330
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001332 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001333 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001334
1335 ctx.CheckbuildFile(outputFile)
1336}
1337
Colin Cross97ba0732015-03-23 17:50:24 -07001338func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001339 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001340
1341 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001342 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001343 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001344
1345 objFiles = append(objFiles, objFilesShared...)
1346
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001347 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001348
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001349 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001350
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001351 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1352 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1353 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1354 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001355 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001356 if versionScript.Valid() {
1357 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1358 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001359 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001360 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001361 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1362 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001363 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001364 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1365 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001366 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001367 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1368 }
1369 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001370 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001371 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1372 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001373 if unexportedSymbols.Valid() {
1374 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1375 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001376 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001377 if forceNotWeakSymbols.Valid() {
1378 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1379 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001380 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001381 if forceWeakSymbols.Valid() {
1382 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1383 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001384 }
Colin Crossaee540a2015-07-06 17:48:31 -07001385 }
1386
Colin Cross97ba0732015-03-23 17:50:24 -07001387 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001388 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001389 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001390
1391 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001392 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001393 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001394 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001395}
1396
Colin Cross97ba0732015-03-23 17:50:24 -07001397func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001398 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001399
1400 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001401 if c.getReuseFrom().ccLibrary() == c {
1402 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001403 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001404 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1405 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001406 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001407 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001408 }
1409
Colin Crossed4cf0b2015-03-26 14:43:45 -07001410 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001411 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1412 } else {
1413 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1414 }
1415}
1416
Colin Cross97ba0732015-03-23 17:50:24 -07001417func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001418 // Static libraries do not get installed.
1419}
1420
Colin Cross97ba0732015-03-23 17:50:24 -07001421func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001422 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001423 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001424 installDir = "lib64"
1425 }
1426
Dan Willemsen782a2d12015-12-21 14:55:28 -08001427 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001428}
1429
Colin Cross97ba0732015-03-23 17:50:24 -07001430func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001431 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001432 c.installStaticLibrary(ctx, flags)
1433 } else {
1434 c.installSharedLibrary(ctx, flags)
1435 }
1436}
1437
Colin Cross3f40fa42015-01-30 17:27:36 -08001438//
1439// Objects (for crt*.o)
1440//
1441
Dan Albertc3144b12015-04-28 18:17:56 -07001442type ccObjectProvider interface {
1443 object() *ccObject
1444}
1445
Colin Cross3f40fa42015-01-30 17:27:36 -08001446type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001447 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001448 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001449}
1450
Dan Albertc3144b12015-04-28 18:17:56 -07001451func (c *ccObject) object() *ccObject {
1452 return c
1453}
1454
Colin Cross97ba0732015-03-23 17:50:24 -07001455func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001456 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001457
Colin Crossfa138792015-04-24 17:31:52 -07001458 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001459}
1460
Colin Cross0676e2d2015-04-24 17:39:18 -07001461func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001462 // object files can't have any dynamic dependencies
1463 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001464}
1465
1466func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001467 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001468
Colin Cross97ba0732015-03-23 17:50:24 -07001469 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001470
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001471 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001472 if len(objFiles) == 1 {
1473 outputFile = objFiles[0]
1474 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001475 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1476 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1477 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001478 }
1479
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001480 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001481
1482 ctx.CheckbuildFile(outputFile)
1483}
1484
Colin Cross97ba0732015-03-23 17:50:24 -07001485func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001486 // Object files do not get installed.
1487}
1488
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001489func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001490 return c.out
1491}
1492
Dan Albertc3144b12015-04-28 18:17:56 -07001493var _ ccObjectProvider = (*ccObject)(nil)
1494
Colin Cross3f40fa42015-01-30 17:27:36 -08001495//
1496// Executables
1497//
1498
Colin Cross7d5136f2015-05-11 13:39:40 -07001499type CCBinaryProperties struct {
1500 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001501 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001502
1503 // set the name of the output
1504 Stem string `android:"arch_variant"`
1505
1506 // append to the name of the output
1507 Suffix string `android:"arch_variant"`
1508
1509 // if set, add an extra objcopy --prefix-symbols= step
1510 Prefix_symbols string
1511}
1512
Colin Cross97ba0732015-03-23 17:50:24 -07001513type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001514 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001515 out common.Path
1516 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001517 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001518}
1519
Colin Crossed4cf0b2015-03-26 14:43:45 -07001520func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001521 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001522}
1523
1524func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001525 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001526}
1527
Colin Cross97ba0732015-03-23 17:50:24 -07001528func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001529 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001530 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001531 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001532 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001533
1534 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001535}
1536
Colin Cross0676e2d2015-04-24 17:39:18 -07001537func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001538 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001539 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001540 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001541 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001542 depNames.CrtBegin = "crtbegin_static"
1543 } else {
1544 depNames.CrtBegin = "crtbegin_dynamic"
1545 }
1546 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001547 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001548 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001549 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1550 } else {
1551 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1552 }
1553 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001554 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001555
Colin Cross06a931b2015-10-28 17:23:31 -07001556 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001557 if c.stl(ctx) == "libc++_static" {
1558 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1559 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001560 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1561 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1562 // move them to the beginning of deps.LateStaticLibs
1563 var groupLibs []string
1564 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1565 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1566 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1567 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001568 }
Colin Cross21b9a242015-03-24 14:15:58 -07001569 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001570}
1571
Colin Cross97ba0732015-03-23 17:50:24 -07001572func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001573 hod common.HostOrDeviceSupported, multilib common.Multilib,
1574 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001575
Colin Cross1f8f2342015-03-26 16:09:47 -07001576 props = append(props, &binary.BinaryProperties)
1577
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001578 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001579}
1580
Colin Cross97ba0732015-03-23 17:50:24 -07001581func CCBinaryFactory() (blueprint.Module, []interface{}) {
1582 module := &CCBinary{}
1583
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001584 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001585}
1586
Colin Cross6362e272015-10-29 15:25:03 -07001587func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001588 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001589 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001590 }
Colin Cross06a931b2015-10-28 17:23:31 -07001591 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001592 c.dynamicProperties.VariantIsStaticBinary = true
1593 }
1594}
1595
Colin Cross0676e2d2015-04-24 17:39:18 -07001596func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001597 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001598
Dan Willemsen490fd492015-11-24 17:53:15 -08001599 if ctx.Host() {
1600 flags.LdFlags = append(flags.LdFlags, "-pie")
1601 if ctx.HostType() == common.Windows {
1602 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1603 }
1604 }
1605
1606 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1607 // all code is position independent, and then those warnings get promoted to
1608 // errors.
1609 if ctx.HostType() != common.Windows {
1610 flags.CFlags = append(flags.CFlags, "-fpie")
1611 }
Colin Cross97ba0732015-03-23 17:50:24 -07001612
Colin Crossf6566ed2015-03-24 11:13:38 -07001613 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001614 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001615 // Clang driver needs -static to create static executable.
1616 // However, bionic/linker uses -shared to overwrite.
1617 // Linker for x86 targets does not allow coexistance of -static and -shared,
1618 // so we add -static only if -shared is not used.
1619 if !inList("-shared", flags.LdFlags) {
1620 flags.LdFlags = append(flags.LdFlags, "-static")
1621 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001622
Colin Crossed4cf0b2015-03-26 14:43:45 -07001623 flags.LdFlags = append(flags.LdFlags,
1624 "-nostdlib",
1625 "-Bstatic",
1626 "-Wl,--gc-sections",
1627 )
1628
1629 } else {
1630 linker := "/system/bin/linker"
1631 if flags.Toolchain.Is64Bit() {
1632 linker = "/system/bin/linker64"
1633 }
1634
1635 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001636 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001637 "-nostdlib",
1638 "-Bdynamic",
1639 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1640 "-Wl,--gc-sections",
1641 "-Wl,-z,nocopyreloc",
1642 )
1643 }
Colin Cross0af4b842015-04-30 16:36:18 -07001644 } else if ctx.Darwin() {
1645 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001646 }
1647
Colin Cross97ba0732015-03-23 17:50:24 -07001648 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001649}
1650
Colin Cross97ba0732015-03-23 17:50:24 -07001651func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001652 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001653
Colin Cross06a931b2015-10-28 17:23:31 -07001654 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001655 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1656 "from static libs or set static_executable: true")
1657 }
1658
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001659 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001660 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001661 if c.BinaryProperties.Prefix_symbols != "" {
1662 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001663 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001664 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1665 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1666 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001667
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001668 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001669
Colin Cross97ba0732015-03-23 17:50:24 -07001670 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001671 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001672 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001673}
Colin Cross3f40fa42015-01-30 17:27:36 -08001674
Colin Cross97ba0732015-03-23 17:50:24 -07001675func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001676 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001677}
1678
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001679func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001680 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001681 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001682 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001683 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001684}
1685
Colin Cross6002e052015-09-16 16:00:08 -07001686func (c *CCBinary) binary() *CCBinary {
1687 return c
1688}
1689
1690type testPerSrc interface {
1691 binary() *CCBinary
1692 testPerSrc() bool
1693}
1694
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001695var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001696
Colin Cross6362e272015-10-29 15:25:03 -07001697func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001698 if test, ok := mctx.Module().(testPerSrc); ok {
1699 if test.testPerSrc() {
1700 testNames := make([]string, len(test.binary().Properties.Srcs))
1701 for i, src := range test.binary().Properties.Srcs {
1702 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1703 }
1704 tests := mctx.CreateLocalVariations(testNames...)
1705 for i, src := range test.binary().Properties.Srcs {
1706 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001707 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001708 }
1709 }
1710 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001711}
1712
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001713type CCTestProperties struct {
1714 // if set, build against the gtest library. Defaults to true.
1715 Gtest bool
1716
1717 // Create a separate binary for each source file. Useful when there is
1718 // global state that can not be torn down and reset between each test suite.
1719 Test_per_src *bool
1720}
1721
Colin Cross9ffb4f52015-04-24 17:48:09 -07001722type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001723 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001724
1725 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001726}
1727
Colin Cross9ffb4f52015-04-24 17:48:09 -07001728func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001729 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001730 if !c.TestProperties.Gtest {
1731 return flags
1732 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001733
Colin Cross97ba0732015-03-23 17:50:24 -07001734 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001735 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001736 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001737
1738 if ctx.HostType() == common.Windows {
1739 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1740 } else {
1741 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1742 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1743 }
1744 } else {
1745 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001746 }
1747
1748 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001749 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001750 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001751
Colin Cross21b9a242015-03-24 14:15:58 -07001752 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001753}
1754
Colin Cross9ffb4f52015-04-24 17:48:09 -07001755func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001756 if c.TestProperties.Gtest {
1757 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1758 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001759 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001760 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001761}
1762
Dan Willemsen782a2d12015-12-21 14:55:28 -08001763func (c *CCTest) InstallInData() bool {
1764 return true
1765}
1766
Colin Cross9ffb4f52015-04-24 17:48:09 -07001767func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001768 installDir := "nativetest"
1769 if flags.Toolchain.Is64Bit() {
1770 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001771 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001772 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1773}
1774
1775func (c *CCTest) testPerSrc() bool {
1776 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001777}
1778
Colin Cross9ffb4f52015-04-24 17:48:09 -07001779func NewCCTest(test *CCTest, module CCModuleType,
1780 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1781
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001782 props = append(props, &test.TestProperties)
1783
1784 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001785}
1786
1787func CCTestFactory() (blueprint.Module, []interface{}) {
1788 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001789 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001790
1791 return NewCCTest(module, module, common.HostAndDeviceSupported)
1792}
1793
Colin Cross2ba19d92015-05-07 15:44:20 -07001794type CCBenchmark struct {
1795 CCBinary
1796}
1797
1798func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1799 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001800 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001801 return depNames
1802}
1803
Dan Willemsen782a2d12015-12-21 14:55:28 -08001804func (c *CCBenchmark) InstallInData() bool {
1805 return true
1806}
1807
Colin Cross2ba19d92015-05-07 15:44:20 -07001808func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1809 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001810 installDir := "nativetest"
1811 if flags.Toolchain.Is64Bit() {
1812 installDir = "nativetest64"
1813 }
1814 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001815 } else {
1816 c.CCBinary.installModule(ctx, flags)
1817 }
1818}
1819
1820func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1821 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1822
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001823 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001824}
1825
1826func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1827 module := &CCBenchmark{}
1828
1829 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1830}
1831
Colin Cross3f40fa42015-01-30 17:27:36 -08001832//
1833// Static library
1834//
1835
Colin Cross97ba0732015-03-23 17:50:24 -07001836func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1837 module := &CCLibrary{}
1838 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001839
Colin Cross97ba0732015-03-23 17:50:24 -07001840 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001841}
1842
1843//
1844// Shared libraries
1845//
1846
Colin Cross97ba0732015-03-23 17:50:24 -07001847func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1848 module := &CCLibrary{}
1849 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001850
Colin Cross97ba0732015-03-23 17:50:24 -07001851 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001852}
1853
1854//
1855// Host static library
1856//
1857
Colin Cross97ba0732015-03-23 17:50:24 -07001858func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1859 module := &CCLibrary{}
1860 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001861
Colin Cross97ba0732015-03-23 17:50:24 -07001862 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001863}
1864
1865//
1866// Host Shared libraries
1867//
1868
Colin Cross97ba0732015-03-23 17:50:24 -07001869func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1870 module := &CCLibrary{}
1871 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001872
Colin Cross97ba0732015-03-23 17:50:24 -07001873 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001874}
1875
1876//
1877// Host Binaries
1878//
1879
Colin Cross97ba0732015-03-23 17:50:24 -07001880func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1881 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001882
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001883 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001884}
1885
1886//
Colin Cross1f8f2342015-03-26 16:09:47 -07001887// Host Tests
1888//
1889
1890func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001891 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001892 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001893}
1894
1895//
Colin Cross2ba19d92015-05-07 15:44:20 -07001896// Host Benchmarks
1897//
1898
1899func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1900 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001901 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001902}
1903
1904//
Colin Crosscfad1192015-11-02 16:43:11 -08001905// Defaults
1906//
1907type CCDefaults struct {
1908 common.AndroidModuleBase
1909 common.DefaultsModule
1910}
1911
1912func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1913}
1914
1915func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1916 module := &CCDefaults{}
1917
1918 propertyStructs := []interface{}{
1919 &CCBaseProperties{},
1920 &CCLibraryProperties{},
1921 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001922 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001923 &CCUnusedProperties{},
1924 }
1925
Dan Willemsen218f6562015-07-08 18:13:11 -07001926 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1927 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001928
1929 return common.InitDefaultsModule(module, module, propertyStructs...)
1930}
1931
1932//
Colin Cross3f40fa42015-01-30 17:27:36 -08001933// Device libraries shipped with gcc
1934//
1935
1936type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001937 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001938}
1939
Colin Cross0676e2d2015-04-24 17:39:18 -07001940func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001941 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001942 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001943}
1944
Colin Cross97ba0732015-03-23 17:50:24 -07001945func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001946 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001947
Colin Cross97ba0732015-03-23 17:50:24 -07001948 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001949 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001950
Colin Crossfa138792015-04-24 17:31:52 -07001951 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001952 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001953}
1954
1955func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001956 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001957
1958 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001959 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001960
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001961 if flags.Clang {
1962 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1963 }
1964
Colin Cross3f40fa42015-01-30 17:27:36 -08001965 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1966
1967 c.out = outputFile
1968
1969 ctx.CheckbuildFile(outputFile)
1970}
1971
Colin Cross97ba0732015-03-23 17:50:24 -07001972func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001973 // Toolchain libraries do not get installed.
1974}
1975
Dan Albertbe961682015-03-18 23:38:50 -07001976// NDK prebuilt libraries.
1977//
1978// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1979// either (with the exception of the shared STLs, which are installed to the app's directory rather
1980// than to the system image).
1981
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001982func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1983 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1984 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001985}
1986
Dan Albertc3144b12015-04-28 18:17:56 -07001987func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001988 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001989
1990 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1991 // We want to translate to just NAME.EXT
1992 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1993 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001994 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001995}
1996
1997type ndkPrebuiltObject struct {
1998 ccObject
1999}
2000
Dan Albertc3144b12015-04-28 18:17:56 -07002001func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
2002 // NDK objects can't have any dependencies
2003 return CCDeps{}
2004}
2005
2006func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
2007 module := &ndkPrebuiltObject{}
2008 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
2009}
2010
2011func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002012 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07002013 // A null build step, but it sets up the output path.
2014 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2015 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2016 }
2017
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002018 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07002019}
2020
2021func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
2022 // Objects do not get installed.
2023}
2024
2025var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
2026
Dan Albertbe961682015-03-18 23:38:50 -07002027type ndkPrebuiltLibrary struct {
2028 CCLibrary
2029}
2030
Colin Cross0676e2d2015-04-24 17:39:18 -07002031func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07002032 // NDK libraries can't have any dependencies
2033 return CCDeps{}
2034}
2035
2036func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2037 module := &ndkPrebuiltLibrary{}
2038 module.LibraryProperties.BuildShared = true
2039 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2040}
2041
2042func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002043 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002044 // A null build step, but it sets up the output path.
2045 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2046 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2047 }
2048
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002049 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
2050 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002051
Dan Willemsen490fd492015-11-24 17:53:15 -08002052 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07002053 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07002054}
2055
2056func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07002057 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07002058}
2059
2060// The NDK STLs are slightly different from the prebuilt system libraries:
2061// * Are not specific to each platform version.
2062// * The libraries are not in a predictable location for each STL.
2063
2064type ndkPrebuiltStl struct {
2065 ndkPrebuiltLibrary
2066}
2067
2068type ndkPrebuiltStaticStl struct {
2069 ndkPrebuiltStl
2070}
2071
2072type ndkPrebuiltSharedStl struct {
2073 ndkPrebuiltStl
2074}
2075
2076func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2077 module := &ndkPrebuiltSharedStl{}
2078 module.LibraryProperties.BuildShared = true
2079 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2080}
2081
2082func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2083 module := &ndkPrebuiltStaticStl{}
2084 module.LibraryProperties.BuildStatic = true
2085 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2086}
2087
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002088func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002089 gccVersion := toolchain.GccVersion()
2090 var libDir string
2091 switch stl {
2092 case "libstlport":
2093 libDir = "cxx-stl/stlport/libs"
2094 case "libc++":
2095 libDir = "cxx-stl/llvm-libc++/libs"
2096 case "libgnustl":
2097 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2098 }
2099
2100 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002101 ndkSrcRoot := "prebuilts/ndk/current/sources"
2102 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002103 }
2104
2105 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002106 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002107}
2108
2109func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002110 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002111 // A null build step, but it sets up the output path.
2112 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2113 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2114 }
2115
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002116 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002117 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002118
2119 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002120 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002121 if c.LibraryProperties.BuildStatic {
2122 libExt = staticLibraryExtension
2123 }
2124
2125 stlName := strings.TrimSuffix(libName, "_shared")
2126 stlName = strings.TrimSuffix(stlName, "_static")
2127 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002128 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002129}
2130
Colin Cross6362e272015-10-29 15:25:03 -07002131func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002132 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002133 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002134 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002135 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002136 modules[0].(ccLinkedInterface).setStatic(true)
2137 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002138 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002139 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002140 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002141 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002142 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002143 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002144 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002145 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002146 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002147
2148 if _, ok := c.(ccLibraryInterface); ok {
2149 reuseFrom := modules[0].(ccLibraryInterface)
2150 for _, m := range modules {
2151 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002152 }
2153 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002154 }
2155}
Colin Cross74d1ec02015-04-28 13:30:13 -07002156
2157// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2158// modifies the slice contents in place, and returns a subslice of the original slice
2159func lastUniqueElements(list []string) []string {
2160 totalSkip := 0
2161 for i := len(list) - 1; i >= totalSkip; i-- {
2162 skip := 0
2163 for j := i - 1; j >= totalSkip; j-- {
2164 if list[i] == list[j] {
2165 skip++
2166 } else {
2167 list[j+skip] = list[j]
2168 }
2169 }
2170 totalSkip += skip
2171 }
2172 return list[totalSkip:]
2173}
Colin Cross06a931b2015-10-28 17:23:31 -07002174
2175var Bool = proptools.Bool