blob: a5c14e1b9d3580bd3cdfff17b5f280e7249a8ab0 [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
106 illegalFlags = []string{
107 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800108 }
109)
110
111func init() {
112 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
113 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
114 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
115
116 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
117
118 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800119 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800121 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800122 pctx.StaticVariable("hostClangGlobalCflags",
123 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700124 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800125 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
127 // Everything in this list is a crime against abstraction and dependency tracking.
128 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800129 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700130 []string{
131 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800132 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700133 "hardware/libhardware/include",
134 "hardware/libhardware_legacy/include",
135 "hardware/ril/include",
136 "libnativehelper/include",
137 "frameworks/native/include",
138 "frameworks/native/opengl/include",
139 "frameworks/av/include",
140 "frameworks/base/include",
141 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800142 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
143 // with this, since there is no associated library.
144 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
145 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800146
Dan Willemsen767078e2016-03-01 13:42:19 -0800147 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2629532/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800148}
149
Colin Cross6362e272015-10-29 15:25:03 -0700150type CCModuleContext common.AndroidBaseContext
151
Colin Cross3f40fa42015-01-30 17:27:36 -0800152// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700153type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800154 common.AndroidModule
155
Colin Crossfa138792015-04-24 17:31:52 -0700156 // Modify property values after parsing Blueprints file but before starting dependency
157 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700158 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700159
Colin Cross21b9a242015-03-24 14:15:58 -0700160 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700161 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800162
Colin Cross6362e272015-10-29 15:25:03 -0700163 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700164 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800165
Colin Cross6362e272015-10-29 15:25:03 -0700166 // Add dynamic dependencies
167 depsMutator(common.AndroidBottomUpMutatorContext)
168
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800171
Dan Albertc403f7c2015-03-18 14:01:18 -0700172 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700173 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700174
Colin Cross3f40fa42015-01-30 17:27:36 -0800175 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800177}
178
Colin Cross97ba0732015-03-23 17:50:24 -0700179type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700180 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700181
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700182 ObjFiles common.Paths
183
184 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700185
Colin Cross97ba0732015-03-23 17:50:24 -0700186 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700187}
188
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189type CCPathDeps struct {
190 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
191
192 ObjFiles common.Paths
193 WholeStaticLibObjFiles common.Paths
194
195 Cflags, ReexportedCflags []string
196
197 CrtBegin, CrtEnd common.OptionalPath
198}
199
Colin Cross97ba0732015-03-23 17:50:24 -0700200type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700201 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
202 AsFlags []string // Flags that apply to assembly source files
203 CFlags []string // Flags that apply to C and C++ source files
204 ConlyFlags []string // Flags that apply to C source files
205 CppFlags []string // Flags that apply to C++ source files
206 YaccFlags []string // Flags that apply to Yacc source files
207 LdFlags []string // Flags that apply to linker command lines
208
209 Nocrt bool
210 Toolchain Toolchain
211 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700212}
213
Colin Cross7d5136f2015-05-11 13:39:40 -0700214// Properties used to compile all C or C++ modules
215type CCBaseProperties struct {
216 // 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 -0700217 Srcs []string `android:"arch_variant"`
218
219 // list of source files that should not be used to build the C/C++ module.
220 // This is most useful in the arch/multilib variants to remove non-common files
221 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700222
223 // list of module-specific flags that will be used for C and C++ compiles.
224 Cflags []string `android:"arch_variant"`
225
226 // list of module-specific flags that will be used for C++ compiles
227 Cppflags []string `android:"arch_variant"`
228
229 // list of module-specific flags that will be used for C compiles
230 Conlyflags []string `android:"arch_variant"`
231
232 // list of module-specific flags that will be used for .S compiles
233 Asflags []string `android:"arch_variant"`
234
235 // list of module-specific flags that will be used for .y and .yy compiles
236 Yaccflags []string
237
238 // list of module-specific flags that will be used for all link steps
239 Ldflags []string `android:"arch_variant"`
240
241 // the instruction set architecture to use to compile the C/C++
242 // module.
243 Instruction_set string `android:"arch_variant"`
244
245 // list of directories relative to the root of the source tree that will
246 // be added to the include path using -I.
247 // If possible, don't use this. If adding paths from the current directory use
248 // local_include_dirs, if adding paths from other modules use export_include_dirs in
249 // that module.
250 Include_dirs []string `android:"arch_variant"`
251
Colin Cross39d97f22015-09-14 12:30:50 -0700252 // list of files relative to the root of the source tree that will be included
253 // using -include.
254 // If possible, don't use this.
255 Include_files []string `android:"arch_variant"`
256
Colin Cross7d5136f2015-05-11 13:39:40 -0700257 // list of directories relative to the Blueprints file that will
258 // be added to the include path using -I
259 Local_include_dirs []string `android:"arch_variant"`
260
Colin Cross39d97f22015-09-14 12:30:50 -0700261 // list of files relative to the Blueprints file that will be included
262 // using -include.
263 // If possible, don't use this.
264 Local_include_files []string `android:"arch_variant"`
265
Colin Cross7d5136f2015-05-11 13:39:40 -0700266 // list of directories relative to the Blueprints file that will
267 // be added to the include path using -I for any module that links against this module
268 Export_include_dirs []string `android:"arch_variant"`
269
270 // list of module-specific flags that will be used for C and C++ compiles when
271 // compiling with clang
272 Clang_cflags []string `android:"arch_variant"`
273
274 // list of module-specific flags that will be used for .S compiles when
275 // compiling with clang
276 Clang_asflags []string `android:"arch_variant"`
277
278 // list of system libraries that will be dynamically linked to
279 // shared library and executable modules. If unset, generally defaults to libc
280 // and libm. Set to [] to prevent linking against libc and libm.
281 System_shared_libs []string
282
283 // list of modules whose object files should be linked into this module
284 // in their entirety. For static library modules, all of the .o files from the intermediate
285 // directory of the dependency will be linked into this modules .a file. For a shared library,
286 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
287 Whole_static_libs []string `android:"arch_variant"`
288
289 // list of modules that should be statically linked into this module.
290 Static_libs []string `android:"arch_variant"`
291
292 // list of modules that should be dynamically linked into this module.
293 Shared_libs []string `android:"arch_variant"`
294
295 // allow the module to contain undefined symbols. By default,
296 // modules cannot contain undefined symbols that are not satisified by their immediate
297 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
298 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700299 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700300
301 // don't link in crt_begin and crt_end. This flag should only be necessary for
302 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700303 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700304
Dan Willemsend67be222015-09-16 15:19:33 -0700305 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700306 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700307
Colin Cross7d5136f2015-05-11 13:39:40 -0700308 // don't insert default compiler flags into asflags, cflags,
309 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700310 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700311
312 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700313 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700314
315 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700316 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700317
318 // -l arguments to pass to linker for host-provided shared libraries
319 Host_ldlibs []string `android:"arch_variant"`
320
321 // select the STL library to use. Possible values are "libc++", "libc++_static",
322 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
323 // default
324 Stl string
325
326 // Set for combined shared/static libraries to prevent compiling object files a second time
327 SkipCompileObjs bool `blueprint:"mutated"`
328
329 Debug, Release struct {
330 // list of module-specific flags that will be used for C and C++ compiles in debug or
331 // release builds
332 Cflags []string `android:"arch_variant"`
333 } `android:"arch_variant"`
334
335 // Minimum sdk version supported when compiling against the ndk
336 Sdk_version string
337
338 // install to a subdirectory of the default install path for the module
339 Relative_install_path string
340}
341
Colin Crosscfad1192015-11-02 16:43:11 -0800342type CCUnusedProperties struct {
343 Native_coverage *bool
344 Required []string
345 Sanitize []string `android:"arch_variant"`
346 Sanitize_recover []string
347 Strip string
348 Tags []string
349}
350
Colin Crossfa138792015-04-24 17:31:52 -0700351// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700352// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
353// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700354type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700355 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800356 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700357 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700358
Colin Cross7d5136f2015-05-11 13:39:40 -0700359 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700360
Colin Crosscfad1192015-11-02 16:43:11 -0800361 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700362
363 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700364
365 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700366}
367
Colin Crossfa138792015-04-24 17:31:52 -0700368func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700369 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
370
371 base.module = module
372
Colin Crossfa138792015-04-24 17:31:52 -0700373 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700374
Colin Crosscfad1192015-11-02 16:43:11 -0800375 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
376
377 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700378}
379
Colin Crossfa138792015-04-24 17:31:52 -0700380func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 toolchain := c.findToolchain(ctx)
382 if ctx.Failed() {
383 return
384 }
385
Colin Cross21b9a242015-03-24 14:15:58 -0700386 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 if ctx.Failed() {
388 return
389 }
390
Colin Cross74d1ec02015-04-28 13:30:13 -0700391 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 if ctx.Failed() {
393 return
394 }
395
Colin Cross28344522015-04-22 13:07:53 -0700396 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700397
Colin Cross581c1892015-04-07 16:50:10 -0700398 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800399 if ctx.Failed() {
400 return
401 }
402
Colin Cross581c1892015-04-07 16:50:10 -0700403 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700404 if ctx.Failed() {
405 return
406 }
407
408 objFiles = append(objFiles, generatedObjFiles...)
409
Colin Cross3f40fa42015-01-30 17:27:36 -0800410 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
411 if ctx.Failed() {
412 return
413 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700414
415 c.ccModuleType().installModule(ctx, flags)
416 if ctx.Failed() {
417 return
418 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800419}
420
Colin Crossfa138792015-04-24 17:31:52 -0700421func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 return c.module
423}
424
Colin Crossfa138792015-04-24 17:31:52 -0700425func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700427 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800428 ht := ctx.HostType()
429 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800430 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800431 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800432 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800434 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800435}
436
Colin Cross6362e272015-10-29 15:25:03 -0700437func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700438}
439
Colin Crosse11befc2015-04-27 17:49:17 -0700440func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700441 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
442 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
443 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700444
Colin Cross21b9a242015-03-24 14:15:58 -0700445 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800446}
447
Colin Cross6362e272015-10-29 15:25:03 -0700448func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700449 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
450 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
451 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
452 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
453
454 staticLibs := c.savedDepNames.WholeStaticLibs
455 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
456 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700457 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800458
Colin Cross74d1ec02015-04-28 13:30:13 -0700459 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700460
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700461 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700462 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700463 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700464 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700465 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700466 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700467 }
Colin Cross6362e272015-10-29 15:25:03 -0700468}
Colin Cross21b9a242015-03-24 14:15:58 -0700469
Colin Cross6362e272015-10-29 15:25:03 -0700470func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
471 if c, ok := ctx.Module().(CCModuleType); ok {
472 c.ModifyProperties(ctx)
473 c.depsMutator(ctx)
474 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800475}
476
477// Create a ccFlags struct that collects the compile flags from global values,
478// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700479func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700480 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700481 CFlags: c.Properties.Cflags,
482 CppFlags: c.Properties.Cppflags,
483 ConlyFlags: c.Properties.Conlyflags,
484 LdFlags: c.Properties.Ldflags,
485 AsFlags: c.Properties.Asflags,
486 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700487 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700488 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700489 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800490 }
Colin Cross28344522015-04-22 13:07:53 -0700491
492 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
494 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700495 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700496 includeDirsToFlags(localIncludeDirs),
497 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700498
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700499 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
500 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700501
502 flags.GlobalFlags = append(flags.GlobalFlags,
503 includeFilesToFlags(rootIncludeFiles),
504 includeFilesToFlags(localIncludeFiles))
505
Colin Cross06a931b2015-10-28 17:23:31 -0700506 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700507 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700508 flags.GlobalFlags = append(flags.GlobalFlags,
509 "${commonGlobalIncludes}",
510 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800511 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700512 }
513
514 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700515 "-I" + common.PathForModuleSrc(ctx).String(),
516 "-I" + common.PathForModuleOut(ctx).String(),
517 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700518 }...)
519 }
520
Colin Cross06a931b2015-10-28 17:23:31 -0700521 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700522 if ctx.Host() {
523 flags.Clang = true
524 }
525
526 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
527 flags.Clang = true
528 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800529 }
530
Dan Willemsen490fd492015-11-24 17:53:15 -0800531 if !toolchain.ClangSupported() {
532 flags.Clang = false
533 }
534
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800535 instructionSet := c.Properties.Instruction_set
536 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
537 if flags.Clang {
538 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
539 }
540 if err != nil {
541 ctx.ModuleErrorf("%s", err)
542 }
543
544 // TODO: debug
545 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
546
Colin Cross97ba0732015-03-23 17:50:24 -0700547 if flags.Clang {
548 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700549 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
550 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700551 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
552 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
553 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800554
555 target := "-target " + toolchain.ClangTriple()
556 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
557
Colin Cross97ba0732015-03-23 17:50:24 -0700558 flags.CFlags = append(flags.CFlags, target, gccPrefix)
559 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
560 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 }
562
Colin Cross06a931b2015-10-28 17:23:31 -0700563 if !Bool(c.Properties.No_default_compiler_flags) {
564 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700565 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 }
567
Colin Cross56b4d452015-04-21 17:38:44 -0700568 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
569
Colin Cross97ba0732015-03-23 17:50:24 -0700570 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800571 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700572 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700573 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800574 toolchain.ClangCflags(),
575 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700576 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800577
578 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800579 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700580 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700581 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800582 toolchain.Cflags(),
583 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700584 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800585 }
586
Colin Cross7b66f152015-12-15 16:07:43 -0800587 if Bool(ctx.AConfig().ProductVariables.Brillo) {
588 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
589 }
590
Colin Crossf6566ed2015-03-24 11:13:38 -0700591 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700592 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700593 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800594 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700595 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 }
597 }
598
Colin Cross97ba0732015-03-23 17:50:24 -0700599 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800600
Colin Cross97ba0732015-03-23 17:50:24 -0700601 if flags.Clang {
602 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
603 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700605 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
606 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800607 }
Colin Cross28344522015-04-22 13:07:53 -0700608
609 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700610 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700611 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800612 }
613
Colin Crossc4bde762015-11-23 16:11:30 -0800614 if flags.Clang {
615 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
616 } else {
617 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800618 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800619 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800620
Colin Cross0676e2d2015-04-24 17:39:18 -0700621 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800622
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700623 if c.Properties.Sdk_version == "" {
624 if ctx.Host() && !flags.Clang {
625 // The host GCC doesn't support C++14 (and is deprecated, so likely
626 // never will). Build these modules with C++11.
627 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
628 } else {
629 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
630 }
631 }
632
633 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
634 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
635 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
636
Colin Cross3f40fa42015-01-30 17:27:36 -0800637 // Optimization to reduce size of build.ninja
638 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700639 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
640 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
641 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
642 flags.CFlags = []string{"$cflags"}
643 flags.CppFlags = []string{"$cppflags"}
644 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800645
646 return flags
647}
648
Colin Cross0676e2d2015-04-24 17:39:18 -0700649func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800650 return flags
651}
652
653// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700654func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700655 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700656
657 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800658
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700659 inputFiles := ctx.ExpandSources(srcFiles, excludes)
660 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800661
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700662 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800663}
664
Colin Crossfa138792015-04-24 17:31:52 -0700665// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700666func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800667
Colin Crossfa138792015-04-24 17:31:52 -0700668 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800669 return nil
670 }
671
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700672 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800673}
674
Colin Cross5049f022015-03-18 13:28:46 -0700675// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700676func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
677 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700678
Colin Crossfa138792015-04-24 17:31:52 -0700679 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700680 return nil
681 }
682
683 ctx.VisitDirectDeps(func(module blueprint.Module) {
684 if gen, ok := module.(genrule.SourceFileGenerator); ok {
685 srcs = append(srcs, gen.GeneratedSourceFiles()...)
686 }
687 })
688
689 if len(srcs) == 0 {
690 return nil
691 }
692
Colin Cross581c1892015-04-07 16:50:10 -0700693 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700694}
695
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700696func (c *CCBase) outputFile() common.OptionalPath {
697 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800698}
699
Colin Crossfa138792015-04-24 17:31:52 -0700700func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800701 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700702 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800703
704 for _, n := range names {
705 found := false
706 ctx.VisitDirectDeps(func(m blueprint.Module) {
707 otherName := ctx.OtherModuleName(m)
708 if otherName != n {
709 return
710 }
711
Colin Cross97ba0732015-03-23 17:50:24 -0700712 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800713 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800714 // If a cc_library host+device module depends on a library that exists as both
715 // cc_library_shared and cc_library_host_shared, it will end up with two
716 // dependencies with the same name, one of which is marked disabled for each
717 // of host and device. Ignore the disabled one.
718 return
719 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700720 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800721 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
722 otherName)
723 return
724 }
725
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700726 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800727 if found {
728 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
729 return
730 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700731 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800732 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700733 if i, ok := a.(ccExportedFlagsProducer); ok {
734 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800735 }
736 found = true
737 } else {
738 ctx.ModuleErrorf("module %q missing output file", otherName)
739 return
740 }
741 } else {
742 ctx.ModuleErrorf("module %q not an android module", otherName)
743 return
744 }
745 })
Colin Cross6ff51382015-12-17 16:39:19 -0800746 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800747 ctx.ModuleErrorf("unsatisified dependency on %q", n)
748 }
749 }
750
Colin Cross28344522015-04-22 13:07:53 -0700751 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800752}
753
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700754// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700755// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700756func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
757 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700758 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800759
Colin Cross21b9a242015-03-24 14:15:58 -0700760 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800761
Colin Cross28344522015-04-22 13:07:53 -0700762 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700763 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700764 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800765 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800766
Colin Cross21b9a242015-03-24 14:15:58 -0700767 for _, m := range wholeStaticLibModules {
768 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
769 depPaths.WholeStaticLibObjFiles =
770 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
771 } else {
772 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
773 }
774 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800775
Colin Cross28344522015-04-22 13:07:53 -0700776 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
777 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700778
Colin Cross28344522015-04-22 13:07:53 -0700779 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
780 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700781
Colin Cross28344522015-04-22 13:07:53 -0700782 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
783 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700784
785 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700786 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700787 otherName := ctx.OtherModuleName(m)
788 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700789 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700790 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700791 }
792 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700793 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700794 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700795 }
796 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700797 output := obj.object().outputFile()
798 if output.Valid() {
799 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
800 } else {
801 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
802 }
Colin Cross21b9a242015-03-24 14:15:58 -0700803 }
804 }
805 })
806
807 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800808}
809
Colin Cross7d5136f2015-05-11 13:39:40 -0700810type ccLinkedProperties struct {
811 VariantIsShared bool `blueprint:"mutated"`
812 VariantIsStatic bool `blueprint:"mutated"`
813 VariantIsStaticBinary bool `blueprint:"mutated"`
814}
815
Colin Crossfa138792015-04-24 17:31:52 -0700816// CCLinked contains the properties and members used by libraries and executables
817type CCLinked struct {
818 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700819 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800820}
821
Colin Crossfa138792015-04-24 17:31:52 -0700822func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700823 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
824
Colin Crossed4cf0b2015-03-26 14:43:45 -0700825 props = append(props, &dynamic.dynamicProperties)
826
Colin Crossfa138792015-04-24 17:31:52 -0700827 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700828}
829
Colin Crossfa138792015-04-24 17:31:52 -0700830func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700831 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700832 return c.Properties.System_shared_libs
833 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700834 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700835 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700836 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800837 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800838}
839
Colin Crossfa138792015-04-24 17:31:52 -0700840func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
841 if c.Properties.Sdk_version != "" && ctx.Device() {
842 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700843 case "":
844 return "ndk_system"
845 case "c++_shared", "c++_static",
846 "stlport_shared", "stlport_static",
847 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700848 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700849 default:
Colin Crossfa138792015-04-24 17:31:52 -0700850 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700851 return ""
852 }
853 }
854
Dan Willemsen490fd492015-11-24 17:53:15 -0800855 if ctx.HostType() == common.Windows {
856 switch c.Properties.Stl {
857 case "libc++", "libc++_static", "libstdc++", "":
858 // libc++ is not supported on mingw
859 return "libstdc++"
860 case "none":
861 return ""
862 default:
863 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
864 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700865 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800866 } else {
867 switch c.Properties.Stl {
868 case "libc++", "libc++_static",
869 "libstdc++":
870 return c.Properties.Stl
871 case "none":
872 return ""
873 case "":
874 if c.static() {
875 return "libc++_static"
876 } else {
877 return "libc++"
878 }
879 default:
880 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
881 return ""
882 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700883 }
884}
885
Dan Willemsen490fd492015-11-24 17:53:15 -0800886var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700887
888func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800889 hostDynamicGccLibs = map[common.HostType][]string{
890 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
891 common.Darwin: []string{"-lc", "-lSystem"},
892 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
893 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
894 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
895 "-lmsvcrt"},
896 }
897 hostStaticGccLibs = map[common.HostType][]string{
898 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
899 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
900 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700901 }
902}
Colin Cross712fc022015-04-27 11:13:34 -0700903
Colin Crosse11befc2015-04-27 17:49:17 -0700904func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700905 stl := c.stl(ctx)
906 if ctx.Failed() {
907 return flags
908 }
909
910 switch stl {
911 case "libc++", "libc++_static":
912 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700913 if ctx.Host() {
914 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
915 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700916 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700917 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800918 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700919 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800920 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700921 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700922 } else {
923 if ctx.Arch().ArchType == common.Arm {
924 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
925 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700926 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700927 case "libstdc++":
928 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
929 // tree is in good enough shape to not need it.
930 // Host builds will use GNU libstdc++.
931 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700932 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700933 }
934 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700935 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
936 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700937 case "ndk_libc++_shared", "ndk_libc++_static":
938 // TODO(danalbert): This really shouldn't be here...
939 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
940 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
941 // Nothing
942 case "":
943 // None or error.
944 if ctx.Host() {
945 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
946 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700947 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800948 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700949 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800950 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700951 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700952 }
953 default:
Colin Crossfa138792015-04-24 17:31:52 -0700954 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700955 }
956
957 return flags
958}
959
Colin Crosse11befc2015-04-27 17:49:17 -0700960func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
961 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800962
Colin Crossed4cf0b2015-03-26 14:43:45 -0700963 stl := c.stl(ctx)
964 if ctx.Failed() {
965 return depNames
966 }
967
968 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700969 case "libstdc++":
970 if ctx.Device() {
971 depNames.SharedLibs = append(depNames.SharedLibs, stl)
972 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700973 case "libc++", "libc++_static":
974 if stl == "libc++" {
975 depNames.SharedLibs = append(depNames.SharedLibs, stl)
976 } else {
977 depNames.StaticLibs = append(depNames.StaticLibs, stl)
978 }
979 if ctx.Device() {
980 if ctx.Arch().ArchType == common.Arm {
981 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
982 }
983 if c.staticBinary() {
984 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
985 } else {
986 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
987 }
988 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700989 case "":
990 // None or error.
991 case "ndk_system":
992 // TODO: Make a system STL prebuilt for the NDK.
993 // 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 -0700994 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700995 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700996 case "ndk_libc++_shared", "ndk_libstlport_shared":
997 depNames.SharedLibs = append(depNames.SharedLibs, stl)
998 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
999 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1000 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001001 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001002 }
1003
Colin Cross74d1ec02015-04-28 13:30:13 -07001004 if ctx.ModuleName() != "libcompiler_rt-extras" {
1005 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1006 }
1007
Colin Crossf6566ed2015-03-24 11:13:38 -07001008 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001009 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001010 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001011 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001012 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1013 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001014
Colin Cross18b6dc52015-04-28 13:20:37 -07001015 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001016 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1017 }
Colin Cross577f6e42015-03-27 18:23:34 -07001018
Colin Crossfa138792015-04-24 17:31:52 -07001019 if c.Properties.Sdk_version != "" {
1020 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001021 depNames.SharedLibs = append(depNames.SharedLibs,
1022 "ndk_libc."+version,
1023 "ndk_libm."+version,
1024 )
1025 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001026 }
1027
Colin Cross21b9a242015-03-24 14:15:58 -07001028 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001029}
1030
Colin Crossed4cf0b2015-03-26 14:43:45 -07001031// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1032type ccLinkedInterface interface {
1033 // Returns true if the build options for the module have selected a static or shared build
1034 buildStatic() bool
1035 buildShared() bool
1036
1037 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001038 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001039
Colin Cross18b6dc52015-04-28 13:20:37 -07001040 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001041 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001042
1043 // Returns whether a module is a static binary
1044 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001045}
1046
1047var _ ccLinkedInterface = (*CCLibrary)(nil)
1048var _ ccLinkedInterface = (*CCBinary)(nil)
1049
Colin Crossfa138792015-04-24 17:31:52 -07001050func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001051 return c.dynamicProperties.VariantIsStatic
1052}
1053
Colin Cross18b6dc52015-04-28 13:20:37 -07001054func (c *CCLinked) staticBinary() bool {
1055 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001056}
1057
Colin Cross18b6dc52015-04-28 13:20:37 -07001058func (c *CCLinked) setStatic(static bool) {
1059 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001060}
1061
Colin Cross28344522015-04-22 13:07:53 -07001062type ccExportedFlagsProducer interface {
1063 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001064}
1065
1066//
1067// Combined static+shared libraries
1068//
1069
Colin Cross7d5136f2015-05-11 13:39:40 -07001070type CCLibraryProperties struct {
1071 BuildStatic bool `blueprint:"mutated"`
1072 BuildShared bool `blueprint:"mutated"`
1073 Static struct {
1074 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001075 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001076 Cflags []string `android:"arch_variant"`
1077 Whole_static_libs []string `android:"arch_variant"`
1078 Static_libs []string `android:"arch_variant"`
1079 Shared_libs []string `android:"arch_variant"`
1080 } `android:"arch_variant"`
1081 Shared struct {
1082 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001083 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001084 Cflags []string `android:"arch_variant"`
1085 Whole_static_libs []string `android:"arch_variant"`
1086 Static_libs []string `android:"arch_variant"`
1087 Shared_libs []string `android:"arch_variant"`
1088 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001089
1090 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001091 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001092 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001093 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001094 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001095 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001096 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001097 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001098}
1099
Colin Cross97ba0732015-03-23 17:50:24 -07001100type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001101 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001102
Colin Cross28344522015-04-22 13:07:53 -07001103 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001104 reuseObjFiles common.Paths
1105 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001106 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001107 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001108 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001109
Colin Cross7d5136f2015-05-11 13:39:40 -07001110 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001111}
1112
Colin Crossed4cf0b2015-03-26 14:43:45 -07001113func (c *CCLibrary) buildStatic() bool {
1114 return c.LibraryProperties.BuildStatic
1115}
1116
1117func (c *CCLibrary) buildShared() bool {
1118 return c.LibraryProperties.BuildShared
1119}
1120
Colin Cross97ba0732015-03-23 17:50:24 -07001121type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001122 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001123 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001124 setReuseFrom(ccLibraryInterface)
1125 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001126 getReuseObjFiles() common.Paths
1127 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001128}
1129
Colin Crossed4cf0b2015-03-26 14:43:45 -07001130var _ ccLibraryInterface = (*CCLibrary)(nil)
1131
Colin Cross97ba0732015-03-23 17:50:24 -07001132func (c *CCLibrary) ccLibrary() *CCLibrary {
1133 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001134}
1135
Colin Cross97ba0732015-03-23 17:50:24 -07001136func NewCCLibrary(library *CCLibrary, module CCModuleType,
1137 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1138
Colin Crossfa138792015-04-24 17:31:52 -07001139 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001140 &library.LibraryProperties)
1141}
1142
1143func CCLibraryFactory() (blueprint.Module, []interface{}) {
1144 module := &CCLibrary{}
1145
1146 module.LibraryProperties.BuildShared = true
1147 module.LibraryProperties.BuildStatic = true
1148
1149 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1150}
1151
Colin Cross0676e2d2015-04-24 17:39:18 -07001152func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001153 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001154 if c.static() {
1155 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1156 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1157 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1158 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001159 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001160 if c.Properties.Sdk_version == "" {
1161 depNames.CrtBegin = "crtbegin_so"
1162 depNames.CrtEnd = "crtend_so"
1163 } else {
1164 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1165 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1166 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001167 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001168 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1169 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1170 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001171 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001172
Dan Willemsen218f6562015-07-08 18:13:11 -07001173 c.systemLibs = c.systemSharedLibs(ctx)
1174
Colin Cross21b9a242015-03-24 14:15:58 -07001175 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001176}
1177
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001178func (c *CCLibrary) outputFile() common.OptionalPath {
1179 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001180}
1181
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001182func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001183 return c.reuseObjFiles
1184}
1185
1186func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1187 c.reuseFrom = reuseFrom
1188}
1189
1190func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1191 return c.reuseFrom
1192}
1193
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001194func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001195 return c.objFiles
1196}
1197
Colin Cross28344522015-04-22 13:07:53 -07001198func (c *CCLibrary) exportedFlags() []string {
1199 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001200}
1201
Colin Cross0676e2d2015-04-24 17:39:18 -07001202func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001203 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001204
Dan Willemsen490fd492015-11-24 17:53:15 -08001205 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1206 // all code is position independent, and then those warnings get promoted to
1207 // errors.
1208 if ctx.HostType() != common.Windows {
1209 flags.CFlags = append(flags.CFlags, "-fPIC")
1210 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001211
Colin Crossd8e780d2015-04-28 17:39:43 -07001212 if c.static() {
1213 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1214 } else {
1215 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1216 }
1217
Colin Cross18b6dc52015-04-28 13:20:37 -07001218 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001219 libName := ctx.ModuleName()
1220 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1221 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001222 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001223 sharedFlag = "-shared"
1224 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001225 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001226 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001227 }
Colin Cross97ba0732015-03-23 17:50:24 -07001228
Colin Cross0af4b842015-04-30 16:36:18 -07001229 if ctx.Darwin() {
1230 flags.LdFlags = append(flags.LdFlags,
1231 "-dynamiclib",
1232 "-single_module",
1233 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001234 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001235 )
1236 } else {
1237 flags.LdFlags = append(flags.LdFlags,
1238 "-Wl,--gc-sections",
1239 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001240 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001241 )
1242 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001243 }
Colin Cross97ba0732015-03-23 17:50:24 -07001244
1245 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001246}
1247
Colin Cross97ba0732015-03-23 17:50:24 -07001248func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001249 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001250
1251 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001252 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001253 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001254
1255 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001256 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001257
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001258 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001259
Colin Cross0af4b842015-04-30 16:36:18 -07001260 if ctx.Darwin() {
1261 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1262 } else {
1263 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1264 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001265
1266 c.objFiles = objFiles
1267 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001268
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001269 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001270 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001271 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001272
1273 ctx.CheckbuildFile(outputFile)
1274}
1275
Colin Cross97ba0732015-03-23 17:50:24 -07001276func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001277 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001278
1279 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001280 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001281 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001282
1283 objFiles = append(objFiles, objFilesShared...)
1284
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001285 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001286
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001287 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001288
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001289 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1290 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1291 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1292 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001293 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001294 if versionScript.Valid() {
1295 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1296 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001297 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001298 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001299 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1300 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001301 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001302 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1303 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001304 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001305 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1306 }
1307 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001308 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001309 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1310 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001311 if unexportedSymbols.Valid() {
1312 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1313 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001314 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001315 if forceNotWeakSymbols.Valid() {
1316 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1317 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001318 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001319 if forceWeakSymbols.Valid() {
1320 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1321 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001322 }
Colin Crossaee540a2015-07-06 17:48:31 -07001323 }
1324
Colin Cross97ba0732015-03-23 17:50:24 -07001325 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001326 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001327 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001328
1329 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001330 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001331 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001332 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001333}
1334
Colin Cross97ba0732015-03-23 17:50:24 -07001335func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001336 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001337
1338 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001339 if c.getReuseFrom().ccLibrary() == c {
1340 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001341 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001342 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1343 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001344 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001345 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001346 }
1347
Colin Crossed4cf0b2015-03-26 14:43:45 -07001348 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001349 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1350 } else {
1351 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1352 }
1353}
1354
Colin Cross97ba0732015-03-23 17:50:24 -07001355func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001356 // Static libraries do not get installed.
1357}
1358
Colin Cross97ba0732015-03-23 17:50:24 -07001359func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001360 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001361 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001362 installDir = "lib64"
1363 }
1364
Dan Willemsen782a2d12015-12-21 14:55:28 -08001365 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001366}
1367
Colin Cross97ba0732015-03-23 17:50:24 -07001368func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001369 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001370 c.installStaticLibrary(ctx, flags)
1371 } else {
1372 c.installSharedLibrary(ctx, flags)
1373 }
1374}
1375
Colin Cross3f40fa42015-01-30 17:27:36 -08001376//
1377// Objects (for crt*.o)
1378//
1379
Dan Albertc3144b12015-04-28 18:17:56 -07001380type ccObjectProvider interface {
1381 object() *ccObject
1382}
1383
Colin Cross3f40fa42015-01-30 17:27:36 -08001384type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001385 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001386 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001387}
1388
Dan Albertc3144b12015-04-28 18:17:56 -07001389func (c *ccObject) object() *ccObject {
1390 return c
1391}
1392
Colin Cross97ba0732015-03-23 17:50:24 -07001393func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001394 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001395
Colin Crossfa138792015-04-24 17:31:52 -07001396 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001397}
1398
Colin Cross0676e2d2015-04-24 17:39:18 -07001399func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001400 // object files can't have any dynamic dependencies
1401 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001402}
1403
1404func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001405 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001406
Colin Cross97ba0732015-03-23 17:50:24 -07001407 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001408
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001409 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001410 if len(objFiles) == 1 {
1411 outputFile = objFiles[0]
1412 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001413 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1414 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1415 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001416 }
1417
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001418 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001419
1420 ctx.CheckbuildFile(outputFile)
1421}
1422
Colin Cross97ba0732015-03-23 17:50:24 -07001423func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001424 // Object files do not get installed.
1425}
1426
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001427func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001428 return c.out
1429}
1430
Dan Albertc3144b12015-04-28 18:17:56 -07001431var _ ccObjectProvider = (*ccObject)(nil)
1432
Colin Cross3f40fa42015-01-30 17:27:36 -08001433//
1434// Executables
1435//
1436
Colin Cross7d5136f2015-05-11 13:39:40 -07001437type CCBinaryProperties struct {
1438 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001439 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001440
1441 // set the name of the output
1442 Stem string `android:"arch_variant"`
1443
1444 // append to the name of the output
1445 Suffix string `android:"arch_variant"`
1446
1447 // if set, add an extra objcopy --prefix-symbols= step
1448 Prefix_symbols string
1449}
1450
Colin Cross97ba0732015-03-23 17:50:24 -07001451type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001452 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001453 out common.Path
1454 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001455 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001456}
1457
Colin Crossed4cf0b2015-03-26 14:43:45 -07001458func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001459 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001460}
1461
1462func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001463 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001464}
1465
Colin Cross97ba0732015-03-23 17:50:24 -07001466func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001467 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001468 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001469 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001470 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001471
1472 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001473}
1474
Colin Cross0676e2d2015-04-24 17:39:18 -07001475func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001476 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001477 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001478 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001479 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001480 depNames.CrtBegin = "crtbegin_static"
1481 } else {
1482 depNames.CrtBegin = "crtbegin_dynamic"
1483 }
1484 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001485 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001486 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001487 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1488 } else {
1489 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1490 }
1491 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001492 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001493
Colin Cross06a931b2015-10-28 17:23:31 -07001494 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001495 if c.stl(ctx) == "libc++_static" {
1496 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1497 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001498 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1499 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1500 // move them to the beginning of deps.LateStaticLibs
1501 var groupLibs []string
1502 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1503 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1504 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1505 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001506 }
Colin Cross21b9a242015-03-24 14:15:58 -07001507 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001508}
1509
Colin Cross97ba0732015-03-23 17:50:24 -07001510func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001511 hod common.HostOrDeviceSupported, multilib common.Multilib,
1512 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001513
Colin Cross1f8f2342015-03-26 16:09:47 -07001514 props = append(props, &binary.BinaryProperties)
1515
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001516 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001517}
1518
Colin Cross97ba0732015-03-23 17:50:24 -07001519func CCBinaryFactory() (blueprint.Module, []interface{}) {
1520 module := &CCBinary{}
1521
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001522 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001523}
1524
Colin Cross6362e272015-10-29 15:25:03 -07001525func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001526 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001527 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001528 }
Colin Cross06a931b2015-10-28 17:23:31 -07001529 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001530 c.dynamicProperties.VariantIsStaticBinary = true
1531 }
1532}
1533
Colin Cross0676e2d2015-04-24 17:39:18 -07001534func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001535 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001536
Dan Willemsen490fd492015-11-24 17:53:15 -08001537 if ctx.Host() {
1538 flags.LdFlags = append(flags.LdFlags, "-pie")
1539 if ctx.HostType() == common.Windows {
1540 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1541 }
1542 }
1543
1544 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1545 // all code is position independent, and then those warnings get promoted to
1546 // errors.
1547 if ctx.HostType() != common.Windows {
1548 flags.CFlags = append(flags.CFlags, "-fpie")
1549 }
Colin Cross97ba0732015-03-23 17:50:24 -07001550
Colin Crossf6566ed2015-03-24 11:13:38 -07001551 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001552 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001553 // Clang driver needs -static to create static executable.
1554 // However, bionic/linker uses -shared to overwrite.
1555 // Linker for x86 targets does not allow coexistance of -static and -shared,
1556 // so we add -static only if -shared is not used.
1557 if !inList("-shared", flags.LdFlags) {
1558 flags.LdFlags = append(flags.LdFlags, "-static")
1559 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001560
Colin Crossed4cf0b2015-03-26 14:43:45 -07001561 flags.LdFlags = append(flags.LdFlags,
1562 "-nostdlib",
1563 "-Bstatic",
1564 "-Wl,--gc-sections",
1565 )
1566
1567 } else {
1568 linker := "/system/bin/linker"
1569 if flags.Toolchain.Is64Bit() {
1570 linker = "/system/bin/linker64"
1571 }
1572
1573 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001574 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001575 "-nostdlib",
1576 "-Bdynamic",
1577 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1578 "-Wl,--gc-sections",
1579 "-Wl,-z,nocopyreloc",
1580 )
1581 }
Colin Cross0af4b842015-04-30 16:36:18 -07001582 } else if ctx.Darwin() {
1583 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001584 }
1585
Colin Cross97ba0732015-03-23 17:50:24 -07001586 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001587}
1588
Colin Cross97ba0732015-03-23 17:50:24 -07001589func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001590 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001591
Colin Cross06a931b2015-10-28 17:23:31 -07001592 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001593 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1594 "from static libs or set static_executable: true")
1595 }
1596
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001597 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001598 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001599 if c.BinaryProperties.Prefix_symbols != "" {
1600 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001601 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001602 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1603 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1604 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001605
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001606 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001607
Colin Cross97ba0732015-03-23 17:50:24 -07001608 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001609 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001610 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001611}
Colin Cross3f40fa42015-01-30 17:27:36 -08001612
Colin Cross97ba0732015-03-23 17:50:24 -07001613func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001614 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001615}
1616
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001618 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001619 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001620 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001621 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001622}
1623
Colin Cross6002e052015-09-16 16:00:08 -07001624func (c *CCBinary) binary() *CCBinary {
1625 return c
1626}
1627
1628type testPerSrc interface {
1629 binary() *CCBinary
1630 testPerSrc() bool
1631}
1632
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001633var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001634
Colin Cross6362e272015-10-29 15:25:03 -07001635func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001636 if test, ok := mctx.Module().(testPerSrc); ok {
1637 if test.testPerSrc() {
1638 testNames := make([]string, len(test.binary().Properties.Srcs))
1639 for i, src := range test.binary().Properties.Srcs {
1640 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1641 }
1642 tests := mctx.CreateLocalVariations(testNames...)
1643 for i, src := range test.binary().Properties.Srcs {
1644 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001645 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001646 }
1647 }
1648 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001649}
1650
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001651type CCTestProperties struct {
1652 // if set, build against the gtest library. Defaults to true.
1653 Gtest bool
1654
1655 // Create a separate binary for each source file. Useful when there is
1656 // global state that can not be torn down and reset between each test suite.
1657 Test_per_src *bool
1658}
1659
Colin Cross9ffb4f52015-04-24 17:48:09 -07001660type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001661 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001662
1663 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001664}
1665
Colin Cross9ffb4f52015-04-24 17:48:09 -07001666func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001667 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001668 if !c.TestProperties.Gtest {
1669 return flags
1670 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001671
Colin Cross97ba0732015-03-23 17:50:24 -07001672 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001673 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001674 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001675
1676 if ctx.HostType() == common.Windows {
1677 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1678 } else {
1679 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1680 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1681 }
1682 } else {
1683 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001684 }
1685
1686 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001687 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001688 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001689
Colin Cross21b9a242015-03-24 14:15:58 -07001690 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001691}
1692
Colin Cross9ffb4f52015-04-24 17:48:09 -07001693func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001694 if c.TestProperties.Gtest {
1695 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1696 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001697 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001698 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001699}
1700
Dan Willemsen782a2d12015-12-21 14:55:28 -08001701func (c *CCTest) InstallInData() bool {
1702 return true
1703}
1704
Colin Cross9ffb4f52015-04-24 17:48:09 -07001705func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001706 installDir := "nativetest"
1707 if flags.Toolchain.Is64Bit() {
1708 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001709 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001710 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1711}
1712
1713func (c *CCTest) testPerSrc() bool {
1714 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001715}
1716
Colin Cross9ffb4f52015-04-24 17:48:09 -07001717func NewCCTest(test *CCTest, module CCModuleType,
1718 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1719
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001720 props = append(props, &test.TestProperties)
1721
1722 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001723}
1724
1725func CCTestFactory() (blueprint.Module, []interface{}) {
1726 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001727 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001728
1729 return NewCCTest(module, module, common.HostAndDeviceSupported)
1730}
1731
Colin Cross2ba19d92015-05-07 15:44:20 -07001732type CCBenchmark struct {
1733 CCBinary
1734}
1735
1736func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1737 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001738 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001739 return depNames
1740}
1741
Dan Willemsen782a2d12015-12-21 14:55:28 -08001742func (c *CCBenchmark) InstallInData() bool {
1743 return true
1744}
1745
Colin Cross2ba19d92015-05-07 15:44:20 -07001746func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1747 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001748 installDir := "nativetest"
1749 if flags.Toolchain.Is64Bit() {
1750 installDir = "nativetest64"
1751 }
1752 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001753 } else {
1754 c.CCBinary.installModule(ctx, flags)
1755 }
1756}
1757
1758func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1759 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1760
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001761 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001762}
1763
1764func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1765 module := &CCBenchmark{}
1766
1767 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1768}
1769
Colin Cross3f40fa42015-01-30 17:27:36 -08001770//
1771// Static library
1772//
1773
Colin Cross97ba0732015-03-23 17:50:24 -07001774func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1775 module := &CCLibrary{}
1776 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001777
Colin Cross97ba0732015-03-23 17:50:24 -07001778 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001779}
1780
1781//
1782// Shared libraries
1783//
1784
Colin Cross97ba0732015-03-23 17:50:24 -07001785func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1786 module := &CCLibrary{}
1787 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001788
Colin Cross97ba0732015-03-23 17:50:24 -07001789 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001790}
1791
1792//
1793// Host static library
1794//
1795
Colin Cross97ba0732015-03-23 17:50:24 -07001796func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1797 module := &CCLibrary{}
1798 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001799
Colin Cross97ba0732015-03-23 17:50:24 -07001800 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001801}
1802
1803//
1804// Host Shared libraries
1805//
1806
Colin Cross97ba0732015-03-23 17:50:24 -07001807func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1808 module := &CCLibrary{}
1809 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001810
Colin Cross97ba0732015-03-23 17:50:24 -07001811 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001812}
1813
1814//
1815// Host Binaries
1816//
1817
Colin Cross97ba0732015-03-23 17:50:24 -07001818func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1819 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001820
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001821 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001822}
1823
1824//
Colin Cross1f8f2342015-03-26 16:09:47 -07001825// Host Tests
1826//
1827
1828func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001829 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001830 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001831}
1832
1833//
Colin Cross2ba19d92015-05-07 15:44:20 -07001834// Host Benchmarks
1835//
1836
1837func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1838 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001839 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001840}
1841
1842//
Colin Crosscfad1192015-11-02 16:43:11 -08001843// Defaults
1844//
1845type CCDefaults struct {
1846 common.AndroidModuleBase
1847 common.DefaultsModule
1848}
1849
1850func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1851}
1852
1853func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1854 module := &CCDefaults{}
1855
1856 propertyStructs := []interface{}{
1857 &CCBaseProperties{},
1858 &CCLibraryProperties{},
1859 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001860 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001861 &CCUnusedProperties{},
1862 }
1863
Dan Willemsen218f6562015-07-08 18:13:11 -07001864 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1865 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001866
1867 return common.InitDefaultsModule(module, module, propertyStructs...)
1868}
1869
1870//
Colin Cross3f40fa42015-01-30 17:27:36 -08001871// Device libraries shipped with gcc
1872//
1873
1874type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001875 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001876}
1877
Colin Cross0676e2d2015-04-24 17:39:18 -07001878func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001879 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001880 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001881}
1882
Colin Cross97ba0732015-03-23 17:50:24 -07001883func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001884 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001885
Colin Cross97ba0732015-03-23 17:50:24 -07001886 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001887 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001888
Colin Crossfa138792015-04-24 17:31:52 -07001889 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001890 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001891}
1892
1893func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001894 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001895
1896 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001897 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001898
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001899 if flags.Clang {
1900 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1901 }
1902
Colin Cross3f40fa42015-01-30 17:27:36 -08001903 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1904
1905 c.out = outputFile
1906
1907 ctx.CheckbuildFile(outputFile)
1908}
1909
Colin Cross97ba0732015-03-23 17:50:24 -07001910func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001911 // Toolchain libraries do not get installed.
1912}
1913
Dan Albertbe961682015-03-18 23:38:50 -07001914// NDK prebuilt libraries.
1915//
1916// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1917// either (with the exception of the shared STLs, which are installed to the app's directory rather
1918// than to the system image).
1919
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001920func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1921 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1922 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001923}
1924
Dan Albertc3144b12015-04-28 18:17:56 -07001925func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001926 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001927
1928 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1929 // We want to translate to just NAME.EXT
1930 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1931 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001932 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001933}
1934
1935type ndkPrebuiltObject struct {
1936 ccObject
1937}
1938
Dan Albertc3144b12015-04-28 18:17:56 -07001939func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1940 // NDK objects can't have any dependencies
1941 return CCDeps{}
1942}
1943
1944func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1945 module := &ndkPrebuiltObject{}
1946 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1947}
1948
1949func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001950 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001951 // A null build step, but it sets up the output path.
1952 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1953 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1954 }
1955
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001956 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001957}
1958
1959func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1960 // Objects do not get installed.
1961}
1962
1963var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1964
Dan Albertbe961682015-03-18 23:38:50 -07001965type ndkPrebuiltLibrary struct {
1966 CCLibrary
1967}
1968
Colin Cross0676e2d2015-04-24 17:39:18 -07001969func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001970 // NDK libraries can't have any dependencies
1971 return CCDeps{}
1972}
1973
1974func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1975 module := &ndkPrebuiltLibrary{}
1976 module.LibraryProperties.BuildShared = true
1977 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1978}
1979
1980func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001981 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001982 // A null build step, but it sets up the output path.
1983 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1984 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1985 }
1986
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001987 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
1988 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001989
Dan Willemsen490fd492015-11-24 17:53:15 -08001990 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001991 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001992}
1993
1994func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001995 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001996}
1997
1998// The NDK STLs are slightly different from the prebuilt system libraries:
1999// * Are not specific to each platform version.
2000// * The libraries are not in a predictable location for each STL.
2001
2002type ndkPrebuiltStl struct {
2003 ndkPrebuiltLibrary
2004}
2005
2006type ndkPrebuiltStaticStl struct {
2007 ndkPrebuiltStl
2008}
2009
2010type ndkPrebuiltSharedStl struct {
2011 ndkPrebuiltStl
2012}
2013
2014func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2015 module := &ndkPrebuiltSharedStl{}
2016 module.LibraryProperties.BuildShared = true
2017 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2018}
2019
2020func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2021 module := &ndkPrebuiltStaticStl{}
2022 module.LibraryProperties.BuildStatic = true
2023 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2024}
2025
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002026func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002027 gccVersion := toolchain.GccVersion()
2028 var libDir string
2029 switch stl {
2030 case "libstlport":
2031 libDir = "cxx-stl/stlport/libs"
2032 case "libc++":
2033 libDir = "cxx-stl/llvm-libc++/libs"
2034 case "libgnustl":
2035 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2036 }
2037
2038 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002039 ndkSrcRoot := "prebuilts/ndk/current/sources"
2040 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002041 }
2042
2043 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002044 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002045}
2046
2047func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002048 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002049 // A null build step, but it sets up the output path.
2050 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2051 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2052 }
2053
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002054 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002055 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002056
2057 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002058 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002059 if c.LibraryProperties.BuildStatic {
2060 libExt = staticLibraryExtension
2061 }
2062
2063 stlName := strings.TrimSuffix(libName, "_shared")
2064 stlName = strings.TrimSuffix(stlName, "_static")
2065 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002066 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002067}
2068
Colin Cross6362e272015-10-29 15:25:03 -07002069func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002070 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002071 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002072 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002073 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002074 modules[0].(ccLinkedInterface).setStatic(true)
2075 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002076 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002077 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002078 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002079 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002080 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002081 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002082 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002083 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002084 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002085
2086 if _, ok := c.(ccLibraryInterface); ok {
2087 reuseFrom := modules[0].(ccLibraryInterface)
2088 for _, m := range modules {
2089 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002090 }
2091 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002092 }
2093}
Colin Cross74d1ec02015-04-28 13:30:13 -07002094
2095// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2096// modifies the slice contents in place, and returns a subslice of the original slice
2097func lastUniqueElements(list []string) []string {
2098 totalSkip := 0
2099 for i := len(list) - 1; i >= totalSkip; i-- {
2100 skip := 0
2101 for j := i - 1; j >= totalSkip; j-- {
2102 if list[i] == list[j] {
2103 skip++
2104 } else {
2105 list[j+skip] = list[j]
2106 }
2107 }
2108 totalSkip += skip
2109 }
2110 return list[totalSkip:]
2111}
Colin Cross06a931b2015-10-28 17:23:31 -07002112
2113var Bool = proptools.Bool