blob: 3ffe26f4c6d46cc36b76ee3c15ec19aadf1e22bb [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 (
22 "blueprint"
23 "blueprint/pathtools"
24 "fmt"
25 "path/filepath"
26 "strings"
27
28 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070029 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080030)
31
32type Config interface {
33 SrcDir() string
34 PrebuiltOS() string
35}
36
37var (
38 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", Config.PrebuiltOS)
39 SrcDir = pctx.VariableConfigMethod("SrcDir", Config.SrcDir)
40
41 LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc")
42 LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm")
43)
44
45// Flags used by lots of devices. Putting them in package static variables will save bytes in
46// build.ninja so they aren't repeated for every file
47var (
48 commonGlobalCflags = []string{
49 "-DANDROID",
50 "-fmessage-length=0",
51 "-W",
52 "-Wall",
53 "-Wno-unused",
54 "-Winit-self",
55 "-Wpointer-arith",
56
57 // COMMON_RELEASE_CFLAGS
58 "-DNDEBUG",
59 "-UDEBUG",
60 }
61
62 deviceGlobalCflags = []string{
63 // TARGET_ERROR_FLAGS
64 "-Werror=return-type",
65 "-Werror=non-virtual-dtor",
66 "-Werror=address",
67 "-Werror=sequence-point",
68 }
69
70 hostGlobalCflags = []string{}
71
72 commonGlobalCppflags = []string{
73 "-Wsign-promo",
74 "-std=gnu++11",
75 }
76)
77
78func init() {
79 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
80 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
81 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
82
83 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
84
85 pctx.StaticVariable("commonClangGlobalCflags",
86 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
87 pctx.StaticVariable("deviceClangGlobalCflags",
88 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
89 pctx.StaticVariable("hostClangGlobalCflags",
90 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -070091 pctx.StaticVariable("commonClangGlobalCppflags",
92 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -080093
94 // Everything in this list is a crime against abstraction and dependency tracking.
95 // Do not add anything to this list.
96 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
97 "-isystem ${SrcDir}/system/core/include",
98 "-isystem ${SrcDir}/hardware/libhardware/include",
99 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
100 "-isystem ${SrcDir}/hardware/ril/include",
101 "-isystem ${SrcDir}/libnativehelper/include",
102 "-isystem ${SrcDir}/frameworks/native/include",
103 "-isystem ${SrcDir}/frameworks/native/opengl/include",
104 "-isystem ${SrcDir}/frameworks/av/include",
105 "-isystem ${SrcDir}/frameworks/base/include",
106 }, " "))
107
108 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/")
109}
110
111// CcProperties describes properties used to compile all C or C++ modules
112type ccProperties struct {
113 // srcs: list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
114 Srcs []string `android:"arch_variant,arch_subtract"`
115
116 // cflags: list of module-specific flags that will be used for C and C++ compiles.
117 Cflags []string `android:"arch_variant"`
118
119 // cppflags: list of module-specific flags that will be used for C++ compiles
120 Cppflags []string `android:"arch_variant"`
121
122 // conlyflags: list of module-specific flags that will be used for C compiles
123 Conlyflags []string `android:"arch_variant"`
124
125 // asflags: list of module-specific flags that will be used for .S compiles
126 Asflags []string `android:"arch_variant"`
127
128 // ldflags: list of module-specific flags that will be used for all link steps
129 Ldflags []string `android:"arch_variant"`
130
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700131 // instruction_set: the instruction set architecture to use to compile the C/C++
132 // module.
133 Instruction_set string `android:"arch_variant"`
134
Colin Cross3f40fa42015-01-30 17:27:36 -0800135 // include_dirs: list of directories relative to the root of the source tree that will
136 // be added to the include path using -I.
137 // If possible, don't use this. If adding paths from the current directory use
138 // local_include_dirs, if adding paths from other modules use export_include_dirs in
139 // that module.
140 Include_dirs []string `android:"arch_variant"`
141
142 // local_include_dirs: list of directories relative to the Blueprints file that will
143 // be added to the include path using -I
144 Local_include_dirs []string `android:"arch_variant"`
145
146 // export_include_dirs: list of directories relative to the Blueprints file that will
147 // be added to the include path using -I for any module that links against this module
148 Export_include_dirs []string
149
150 // clang_cflags: list of module-specific flags that will be used for C and C++ compiles when
151 // compiling with clang
152 Clang_cflags []string `android:"arch_variant"`
153
154 // clang_asflags: list of module-specific flags that will be used for .S compiles when
155 // compiling with clang
156 Clang_asflags []string `android:"arch_variant"`
157
158 // system_shared_libs: list of system libraries that will be dynamically linked to
159 // shared library and executable modules. If unset, generally defaults to libc
160 // and libm. Set to [] to prevent linking against libc and libm.
161 System_shared_libs []string
162
163 // whole_static_libs: list of modules whose object files should be linked into this module
164 // in their entirety. For static library modules, all of the .o files from the intermediate
165 // directory of the dependency will be linked into this modules .a file. For a shared library,
166 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
167 Whole_static_libs []string `android:"arch_variant"`
168
169 // static_libs: list of modules that should be statically linked into this module.
170 Static_libs []string `android:"arch_variant"`
171
172 // shared_libs: list of modules that should be dynamically linked into this module.
173 Shared_libs []string `android:"arch_variant"`
174
175 // allow_undefined_symbols: allow the module to contain undefined symbols. By default,
176 // modules cannot contain undefined symbols that are not satisified by their immediate
177 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
178 // This flag should only be necessary for compiling low-level libraries like libc.
179 Allow_undefined_symbols bool
180
181 // nocrt: don't link in crt_begin and crt_end. This flag should only be necessary for
182 // compiling crt or libc.
183 Nocrt bool `android:"arch_variant"`
184
185 // no_default_compiler_flags: don't insert default compiler flags into asflags, cflags,
186 // cppflags, conlyflags, ldflags, or include_dirs
187 No_default_compiler_flags bool
188
189 // clang: compile module with clang instead of gcc
190 Clang bool `android:"arch_variant"`
191
192 // rtti: pass -frtti instead of -fno-rtti
193 Rtti bool
194
195 // host_ldlibs: -l arguments to pass to linker for host-provided shared libraries
196 Host_ldlibs []string `android:"arch_variant"`
197
198 // stl: select the STL library to use. Possible values are "libc++", "libc++_static",
199 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
200 // default
201 Stl string
202
203 // Set for combined shared/static libraries to prevent compiling object files a second time
204 SkipCompileObjs bool `blueprint:"mutated"`
Colin Crossaf19a292015-03-18 12:07:10 -0700205
206 Debug struct {
207 Cflags []string `android:"arch_variant"`
208 } `android:"arch_variant"`
209 Release struct {
210 Cflags []string `android:"arch_variant"`
211 } `android:"arch_variant"`
Colin Cross3f40fa42015-01-30 17:27:36 -0800212}
213
214type unusedProperties struct {
215 Asan bool
216 Native_coverage bool
217 Strip string
218 Tags []string
219 Required []string
220}
221
222// Building C/C++ code is handled by objects that satisfy this interface via composition
223type ccModuleType interface {
224 common.AndroidModule
225
226 // Return the cflags that are specific to this _type_ of module
227 moduleTypeCflags(common.AndroidModuleContext, toolchain) []string
228
229 // Return the ldflags that are specific to this _type_ of module
230 moduleTypeLdflags(common.AndroidModuleContext, toolchain) []string
231
232 // Create a ccDeps struct that collects the module dependency info. Can also
233 // modify ccFlags in order to add dependency include directories, etc.
234 collectDeps(common.AndroidModuleContext, ccFlags) (ccDeps, ccFlags)
235
236 // Compile objects into final module
237 compileModule(common.AndroidModuleContext, ccFlags, ccDeps, []string)
238
Dan Albertc403f7c2015-03-18 14:01:18 -0700239 // Install the built module.
240 installModule(common.AndroidModuleContext, ccFlags)
241
Colin Cross3f40fa42015-01-30 17:27:36 -0800242 // Return the output file (.o, .a or .so) for use by other modules
243 outputFile() string
244}
245
Colin Crossc472d572015-03-17 15:06:21 -0700246type ccDeps struct {
247 staticLibs, sharedLibs, lateStaticLibs, wholeStaticLibs, objFiles, includeDirs []string
248
249 crtBegin, crtEnd string
250}
251
252type ccFlags struct {
253 globalFlags []string
254 asFlags []string
255 cFlags []string
256 conlyFlags []string
257 cppFlags []string
258 ldFlags []string
259 ldLibs []string
260 includeDirs []string
261 nocrt bool
262 toolchain toolchain
263 clang bool
264
265 extraStaticLibs []string
266 extraSharedLibs []string
267}
268
269// ccBase contains the properties and members used by all C/C++ module types, and implements
270// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
271// and uses a ccModuleType interface to that struct to create the build steps.
272type ccBase struct {
273 common.AndroidModuleBase
274 module ccModuleType
275
276 properties ccProperties
277 unused unusedProperties
278
279 installPath string
280}
281
282func newCCBase(base *ccBase, module ccModuleType, hod common.HostOrDeviceSupported,
283 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
284
285 base.module = module
286
287 props = append(props, &base.properties, &base.unused)
288
Colin Cross5049f022015-03-18 13:28:46 -0700289 return common.InitAndroidArchModule(module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700290}
291
Colin Cross3f40fa42015-01-30 17:27:36 -0800292func (c *ccBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
293 toolchain := c.findToolchain(ctx)
294 if ctx.Failed() {
295 return
296 }
297
298 flags := c.flags(ctx, toolchain)
299 if ctx.Failed() {
300 return
301 }
302
303 flags = c.addStlFlags(ctx, flags)
304 if ctx.Failed() {
305 return
306 }
307
308 deps, flags := c.ccModuleType().collectDeps(ctx, flags)
309 if ctx.Failed() {
310 return
311 }
312
313 objFiles := c.compileObjs(ctx, flags, deps)
314 if ctx.Failed() {
315 return
316 }
317
Colin Cross5049f022015-03-18 13:28:46 -0700318 generatedObjFiles := c.compileGeneratedObjs(ctx, flags, deps)
319 if ctx.Failed() {
320 return
321 }
322
323 objFiles = append(objFiles, generatedObjFiles...)
324
Colin Cross3f40fa42015-01-30 17:27:36 -0800325 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
326 if ctx.Failed() {
327 return
328 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700329
330 c.ccModuleType().installModule(ctx, flags)
331 if ctx.Failed() {
332 return
333 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800334}
335
336func (c *ccBase) ccModuleType() ccModuleType {
337 return c.module
338}
339
340var _ common.AndroidDynamicDepender = (*ccBase)(nil)
341
342func (c *ccBase) findToolchain(ctx common.AndroidModuleContext) toolchain {
343 arch := ctx.Arch()
344 factory := toolchainFactories[arch.HostOrDevice][arch.ArchType]
345 if factory == nil {
346 panic(fmt.Sprintf("Toolchain not found for %s arch %q",
347 arch.HostOrDevice.String(), arch.String()))
348 }
349 return factory(arch.ArchVariant, arch.CpuVariant)
350}
351
Colin Cross3f40fa42015-01-30 17:27:36 -0800352func (c *ccBase) moduleTypeCflags(ctx common.AndroidModuleContext, toolchain toolchain) []string {
353 return nil
354}
355
356func (c *ccBase) moduleTypeLdflags(ctx common.AndroidModuleContext, toolchain toolchain) []string {
357 return nil
358}
359
360func (c *ccBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
361 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, c.properties.Whole_static_libs...)
362 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, c.properties.Static_libs...)
363 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.properties.Shared_libs...)
364
365 return nil
366}
367
368// Create a ccFlags struct that collects the compile flags from global values,
369// per-target values, module type values, and per-module Blueprints properties
370func (c *ccBase) flags(ctx common.AndroidModuleContext, toolchain toolchain) ccFlags {
371
372 arch := ctx.Arch()
373
374 flags := ccFlags{
375 cFlags: c.properties.Cflags,
376 cppFlags: c.properties.Cppflags,
377 conlyFlags: c.properties.Conlyflags,
378 ldFlags: c.properties.Ldflags,
379 asFlags: c.properties.Asflags,
380 nocrt: c.properties.Nocrt,
381 toolchain: toolchain,
382 clang: c.properties.Clang,
383 }
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700384 instructionSet := c.properties.Instruction_set
385 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
386 if err != nil {
387 ctx.ModuleErrorf("%s", err)
388 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800389
Colin Crossaf19a292015-03-18 12:07:10 -0700390 // TODO: debug
391 flags.cFlags = append(flags.cFlags, c.properties.Release.Cflags...)
392
Colin Cross3f40fa42015-01-30 17:27:36 -0800393 if arch.HostOrDevice.Host() {
394 // TODO: allow per-module clang disable for host
395 flags.clang = true
396 }
397
398 if flags.clang {
399 flags.cFlags = clangFilterUnknownCflags(flags.cFlags)
400 flags.cFlags = append(flags.cFlags, c.properties.Clang_cflags...)
401 flags.asFlags = append(flags.asFlags, c.properties.Clang_asflags...)
402 flags.cppFlags = clangFilterUnknownCflags(flags.cppFlags)
403 flags.conlyFlags = clangFilterUnknownCflags(flags.conlyFlags)
404 flags.ldFlags = clangFilterUnknownCflags(flags.ldFlags)
405
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700406 flags.cFlags = append(flags.cFlags, "${clangExtraCflags}")
407 flags.conlyFlags = append(flags.conlyFlags, "${clangExtraConlyflags}")
408 if arch.HostOrDevice.Device() {
409 flags.cFlags = append(flags.cFlags, "${clangExtraTargetCflags}")
410 }
411
Colin Cross3f40fa42015-01-30 17:27:36 -0800412 target := "-target " + toolchain.ClangTriple()
413 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
414
415 flags.cFlags = append(flags.cFlags, target, gccPrefix)
416 flags.asFlags = append(flags.asFlags, target, gccPrefix)
417 flags.ldFlags = append(flags.ldFlags, target, gccPrefix)
418
419 if arch.HostOrDevice.Host() {
420 gccToolchain := "--gcc-toolchain=" + toolchain.GccRoot()
421 sysroot := "--sysroot=" + filepath.Join(toolchain.GccRoot(), "sysroot")
422
423 // TODO: also need more -B, -L flags to make host builds hermetic
424 flags.cFlags = append(flags.cFlags, gccToolchain, sysroot)
425 flags.asFlags = append(flags.asFlags, gccToolchain, sysroot)
426 flags.ldFlags = append(flags.ldFlags, gccToolchain, sysroot)
427 }
428 }
429
430 flags.includeDirs = pathtools.PrefixPaths(c.properties.Include_dirs, ctx.Config().(Config).SrcDir())
431 localIncludeDirs := pathtools.PrefixPaths(c.properties.Local_include_dirs, common.ModuleSrcDir(ctx))
432 flags.includeDirs = append(flags.includeDirs, localIncludeDirs...)
433
434 if !c.properties.No_default_compiler_flags {
435 flags.includeDirs = append(flags.includeDirs, []string{
436 common.ModuleSrcDir(ctx),
437 common.ModuleOutDir(ctx),
438 common.ModuleGenDir(ctx),
439 }...)
440
441 if arch.HostOrDevice.Device() && !c.properties.Allow_undefined_symbols {
442 flags.ldFlags = append(flags.ldFlags, "-Wl,--no-undefined")
443 }
444
445 if flags.clang {
Tim Kilbournf2948142015-03-11 12:03:03 -0700446 flags.cppFlags = append(flags.cppFlags, "${commonClangGlobalCppflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800447 flags.globalFlags = []string{
448 "${commonGlobalIncludes}",
449 toolchain.IncludeFlags(),
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700450 instructionSetFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800451 toolchain.ClangCflags(),
452 "${commonClangGlobalCflags}",
453 fmt.Sprintf("${%sClangGlobalCflags}", arch.HostOrDevice),
454 }
455 } else {
Tim Kilbournf2948142015-03-11 12:03:03 -0700456 flags.cppFlags = append(flags.cppFlags, "${commonGlobalCppflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800457 flags.globalFlags = []string{
458 "${commonGlobalIncludes}",
459 toolchain.IncludeFlags(),
Tim Kilbourn1a9bf262015-03-18 12:28:32 -0700460 instructionSetFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800461 toolchain.Cflags(),
462 "${commonGlobalCflags}",
463 fmt.Sprintf("${%sGlobalCflags}", arch.HostOrDevice),
464 }
465 }
466
467 if arch.HostOrDevice.Host() {
468 flags.ldFlags = append(flags.ldFlags, c.properties.Host_ldlibs...)
469 }
470
471 if arch.HostOrDevice.Device() {
472 if c.properties.Rtti {
473 flags.cppFlags = append(flags.cppFlags, "-frtti")
474 } else {
475 flags.cppFlags = append(flags.cppFlags, "-fno-rtti")
476 }
477 }
478
479 flags.asFlags = append(flags.asFlags, "-D__ASSEMBLY__")
480
481 if flags.clang {
482 flags.cppFlags = append(flags.cppFlags, toolchain.ClangCppflags())
483 flags.ldFlags = append(flags.ldFlags, toolchain.ClangLdflags())
484 } else {
485 flags.cppFlags = append(flags.cppFlags, toolchain.Cppflags())
486 flags.ldFlags = append(flags.ldFlags, toolchain.Ldflags())
487 }
488 }
489
490 flags.cFlags = append(flags.cFlags, c.ccModuleType().moduleTypeCflags(ctx, toolchain)...)
491 flags.ldFlags = append(flags.ldFlags, c.ccModuleType().moduleTypeLdflags(ctx, toolchain)...)
492
493 // Optimization to reduce size of build.ninja
494 // Replace the long list of flags for each file with a module-local variable
495 ctx.Variable(pctx, "cflags", strings.Join(flags.cFlags, " "))
496 ctx.Variable(pctx, "cppflags", strings.Join(flags.cppFlags, " "))
497 ctx.Variable(pctx, "asflags", strings.Join(flags.asFlags, " "))
498 flags.cFlags = []string{"$cflags"}
499 flags.cppFlags = []string{"$cppflags"}
500 flags.asFlags = []string{"$asflags"}
501
502 return flags
503}
504
505// Modify ccFlags structs with STL library info
506func (c *ccBase) addStlFlags(ctx common.AndroidModuleContext, flags ccFlags) ccFlags {
507 if !c.properties.No_default_compiler_flags {
508 arch := ctx.Arch()
509 stl := "libc++" // TODO: mingw needs libstdc++
510 if c.properties.Stl != "" {
511 stl = c.properties.Stl
512 }
513
514 stlStatic := false
515 if strings.HasSuffix(stl, "_static") {
516 stlStatic = true
517 }
518
519 switch stl {
520 case "libc++", "libc++_static":
521 flags.cFlags = append(flags.cFlags, "-D_USING_LIBCXX")
522 flags.includeDirs = append(flags.includeDirs, "${SrcDir}/external/libcxx/include")
523 if arch.HostOrDevice.Host() {
524 flags.cppFlags = append(flags.cppFlags, "-nostdinc++")
525 flags.ldFlags = append(flags.ldFlags, "-nodefaultlibs")
526 flags.ldLibs = append(flags.ldLibs, "-lc", "-lm", "-lpthread")
527 }
528 if stlStatic {
529 flags.extraStaticLibs = append(flags.extraStaticLibs, "libc++_static")
530 } else {
531 flags.extraSharedLibs = append(flags.extraSharedLibs, "libc++")
532 }
533 case "stlport", "stlport_static":
534 if arch.HostOrDevice.Device() {
535 flags.includeDirs = append(flags.includeDirs,
536 "${SrcDir}/external/stlport/stlport",
537 "${SrcDir}/bionic/libstdc++/include",
538 "${SrcDir}/bionic")
539 if stlStatic {
540 flags.extraStaticLibs = append(flags.extraStaticLibs, "libstdc++", "libstlport_static")
541 } else {
542 flags.extraSharedLibs = append(flags.extraSharedLibs, "libstdc++", "libstlport")
543 }
544 }
545 case "ndk":
546 panic("TODO")
547 case "libstdc++":
548 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
549 // tree is in good enough shape to not need it.
550 // Host builds will use GNU libstdc++.
551 if arch.HostOrDevice.Device() {
552 flags.includeDirs = append(flags.includeDirs, "${SrcDir}/bionic/libstdc++/include")
553 flags.extraSharedLibs = append(flags.extraSharedLibs, "libstdc++")
554 }
555 case "none":
556 if arch.HostOrDevice.Host() {
557 flags.cppFlags = append(flags.cppFlags, "-nostdinc++")
558 flags.ldFlags = append(flags.ldFlags, "-nodefaultlibs")
559 flags.ldLibs = append(flags.ldLibs, "-lc", "-lm")
560 }
561 default:
562 ctx.ModuleErrorf("stl: %q is not a supported STL", stl)
563 }
564
565 }
566 return flags
567}
568
569// Compile a list of source files into objects a specified subdirectory
570func (c *ccBase) customCompileObjs(ctx common.AndroidModuleContext, flags ccFlags,
571 deps ccDeps, subdir string, srcFiles []string) []string {
572
573 srcFiles = pathtools.PrefixPaths(srcFiles, common.ModuleSrcDir(ctx))
574 srcFiles = common.ExpandGlobs(ctx, srcFiles)
575
576 return TransformSourceToObj(ctx, subdir, srcFiles, ccFlagsToBuilderFlags(flags))
577}
578
579// Compile files listed in c.properties.Srcs into objects
580func (c *ccBase) compileObjs(ctx common.AndroidModuleContext, flags ccFlags,
581 deps ccDeps) []string {
582
583 if c.properties.SkipCompileObjs {
584 return nil
585 }
586
587 return c.customCompileObjs(ctx, flags, deps, "", c.properties.Srcs)
588}
589
Colin Cross5049f022015-03-18 13:28:46 -0700590// Compile generated source files from dependencies
591func (c *ccBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags ccFlags,
592 deps ccDeps) []string {
593 var srcs []string
594
595 if c.properties.SkipCompileObjs {
596 return nil
597 }
598
599 ctx.VisitDirectDeps(func(module blueprint.Module) {
600 if gen, ok := module.(genrule.SourceFileGenerator); ok {
601 srcs = append(srcs, gen.GeneratedSourceFiles()...)
602 }
603 })
604
605 if len(srcs) == 0 {
606 return nil
607 }
608
609 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags))
610}
611
Colin Cross3f40fa42015-01-30 17:27:36 -0800612func (c *ccBase) outputFile() string {
613 return ""
614}
615
616func (c *ccBase) collectDepsFromList(ctx common.AndroidModuleContext,
617 names []string) (modules []common.AndroidModule,
618 outputFiles []string, exportedIncludeDirs []string) {
619
620 for _, n := range names {
621 found := false
622 ctx.VisitDirectDeps(func(m blueprint.Module) {
623 otherName := ctx.OtherModuleName(m)
624 if otherName != n {
625 return
626 }
627
628 if a, ok := m.(ccModuleType); ok {
629 if a.Disabled() {
630 // If a cc_library host+device module depends on a library that exists as both
631 // cc_library_shared and cc_library_host_shared, it will end up with two
632 // dependencies with the same name, one of which is marked disabled for each
633 // of host and device. Ignore the disabled one.
634 return
635 }
636 if a.HostOrDevice() != ctx.Arch().HostOrDevice {
637 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
638 otherName)
639 return
640 }
641
642 if outputFile := a.outputFile(); outputFile != "" {
643 if found {
644 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
645 return
646 }
647 outputFiles = append(outputFiles, outputFile)
648 modules = append(modules, a)
649 if i, ok := a.(ccExportedIncludeDirsProducer); ok {
650 exportedIncludeDirs = append(exportedIncludeDirs, i.exportedIncludeDirs()...)
651 }
652 found = true
653 } else {
654 ctx.ModuleErrorf("module %q missing output file", otherName)
655 return
656 }
657 } else {
658 ctx.ModuleErrorf("module %q not an android module", otherName)
659 return
660 }
661 })
662 if !found {
663 ctx.ModuleErrorf("unsatisified dependency on %q", n)
664 }
665 }
666
667 return modules, outputFiles, exportedIncludeDirs
668}
669
670func (c *ccBase) collectDeps(ctx common.AndroidModuleContext, flags ccFlags) (ccDeps, ccFlags) {
671 var deps ccDeps
672 var newIncludeDirs []string
673
674 wholeStaticLibNames := c.properties.Whole_static_libs
675 _, deps.wholeStaticLibs, newIncludeDirs = c.collectDepsFromList(ctx, wholeStaticLibNames)
676
677 deps.includeDirs = append(deps.includeDirs, newIncludeDirs...)
678
679 staticLibNames := c.properties.Static_libs
680 staticLibNames = append(staticLibNames, flags.extraStaticLibs...)
681 _, deps.staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
682 deps.includeDirs = append(deps.includeDirs, newIncludeDirs...)
683
684 return deps, flags
685}
686
687// ccDynamic contains the properties and members used by shared libraries and dynamic executables
688type ccDynamic struct {
689 ccBase
690}
691
Colin Crossc472d572015-03-17 15:06:21 -0700692func newCCDynamic(dynamic *ccDynamic, module ccModuleType, hod common.HostOrDeviceSupported,
693 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
694
695 dynamic.properties.System_shared_libs = []string{defaultSystemSharedLibraries}
696
697 return newCCBase(&dynamic.ccBase, module, hod, multilib, props...)
698}
699
Colin Cross3f40fa42015-01-30 17:27:36 -0800700const defaultSystemSharedLibraries = "__default__"
701
702func (c *ccDynamic) systemSharedLibs() []string {
703
704 if len(c.properties.System_shared_libs) == 1 &&
705 c.properties.System_shared_libs[0] == defaultSystemSharedLibraries {
706
707 if c.HostOrDevice().Host() {
708 return []string{}
709 } else {
710 return []string{"libc", "libm"}
711 }
712 }
713 return c.properties.System_shared_libs
714}
715
716var (
717 stlSharedLibs = []string{"libc++", "libstlport", "libstdc++"}
718 stlSharedHostLibs = []string{"libc++"}
719 stlStaticLibs = []string{"libc++_static", "libstlport_static", "libstdc++"}
720 stlStaticHostLibs = []string{"libc++_static"}
721)
722
723func (c *ccDynamic) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
724 deps := c.ccBase.AndroidDynamicDependencies(ctx)
725
726 if c.HostOrDevice().Device() {
727 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.systemSharedLibs()...)
728 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}},
729 "libcompiler_rt-extras",
Colin Cross77b00fa2015-03-16 16:15:49 -0700730 "libgcov",
Colin Cross3f40fa42015-01-30 17:27:36 -0800731 "libatomic",
732 "libgcc")
733
734 if c.properties.Stl != "none" {
735 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, stlSharedLibs...)
736 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, stlStaticLibs...)
737 }
738 } else {
739 if c.properties.Stl != "none" {
740 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, stlSharedHostLibs...)
741 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, stlStaticHostLibs...)
742 }
743 }
744
745 return deps
746}
747
Colin Cross3f40fa42015-01-30 17:27:36 -0800748func (c *ccDynamic) collectDeps(ctx common.AndroidModuleContext, flags ccFlags) (ccDeps, ccFlags) {
749 var newIncludeDirs []string
750
751 deps, flags := c.ccBase.collectDeps(ctx, flags)
752
753 systemSharedLibs := c.systemSharedLibs()
754 sharedLibNames := make([]string, 0, len(c.properties.Shared_libs)+len(systemSharedLibs)+
755 len(flags.extraSharedLibs))
756 sharedLibNames = append(sharedLibNames, c.properties.Shared_libs...)
757 sharedLibNames = append(sharedLibNames, systemSharedLibs...)
758 sharedLibNames = append(sharedLibNames, flags.extraSharedLibs...)
759 _, deps.sharedLibs, newIncludeDirs = c.collectDepsFromList(ctx, sharedLibNames)
760 deps.includeDirs = append(deps.includeDirs, newIncludeDirs...)
761
762 if ctx.Arch().HostOrDevice.Device() {
763 var staticLibs []string
Colin Cross77b00fa2015-03-16 16:15:49 -0700764 staticLibNames := []string{"libcompiler_rt-extras"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800765 _, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
766 deps.staticLibs = append(deps.staticLibs, staticLibs...)
767 deps.includeDirs = append(deps.includeDirs, newIncludeDirs...)
Colin Cross77b00fa2015-03-16 16:15:49 -0700768
769 // libgcc and libatomic have to be last on the command line
770 staticLibNames = []string{"libgcov", "libatomic", "libgcc"}
771 _, staticLibs, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
772 deps.lateStaticLibs = append(deps.lateStaticLibs, staticLibs...)
773 deps.includeDirs = append(deps.includeDirs, newIncludeDirs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800774 }
775
776 ctx.VisitDirectDeps(func(m blueprint.Module) {
777 if obj, ok := m.(*ccObject); ok {
778 otherName := ctx.OtherModuleName(m)
779 if strings.HasPrefix(otherName, "crtbegin") {
780 if !c.properties.Nocrt {
781 deps.crtBegin = obj.outputFile()
782 }
783 } else if strings.HasPrefix(otherName, "crtend") {
784 if !c.properties.Nocrt {
785 deps.crtEnd = obj.outputFile()
786 }
787 } else {
788 ctx.ModuleErrorf("object module type only support for crtbegin and crtend, found %q",
789 ctx.OtherModuleName(m))
790 }
791 }
792 })
793
794 flags.includeDirs = append(flags.includeDirs, deps.includeDirs...)
795
796 return deps, flags
797}
798
799type ccExportedIncludeDirsProducer interface {
800 exportedIncludeDirs() []string
801}
802
803//
804// Combined static+shared libraries
805//
806
807type ccLibrary struct {
808 ccDynamic
809
810 primary *ccLibrary
811 primaryObjFiles []string
812 objFiles []string
813 exportIncludeDirs []string
814 out string
815
816 libraryProperties struct {
817 BuildStatic bool `blueprint:"mutated"`
818 BuildShared bool `blueprint:"mutated"`
819 IsShared bool `blueprint:"mutated"`
820 IsStatic bool `blueprint:"mutated"`
821
822 Static struct {
823 Srcs []string `android:"arch_variant"`
824 Cflags []string `android:"arch_variant"`
825 } `android:"arch_variant"`
826 Shared struct {
827 Srcs []string `android:"arch_variant"`
828 Cflags []string `android:"arch_variant"`
829 } `android:"arch_variant"`
830 }
831}
832
Colin Crossc472d572015-03-17 15:06:21 -0700833func newCCLibrary(library *ccLibrary, hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
834 return newCCDynamic(&library.ccDynamic, library, hod, common.MultilibBoth,
835 &library.libraryProperties)
836}
837
Colin Cross3f40fa42015-01-30 17:27:36 -0800838func NewCCLibrary() (blueprint.Module, []interface{}) {
839 module := &ccLibrary{}
Colin Crossc472d572015-03-17 15:06:21 -0700840
Colin Cross3f40fa42015-01-30 17:27:36 -0800841 module.libraryProperties.BuildShared = true
842 module.libraryProperties.BuildStatic = true
843
Colin Crossc472d572015-03-17 15:06:21 -0700844 return newCCLibrary(module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -0800845}
846
847func (c *ccLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
848 if c.libraryProperties.IsShared {
849 deps := c.ccDynamic.AndroidDynamicDependencies(ctx)
850 if c.HostOrDevice().Device() {
851 deps = append(deps, "crtbegin_so", "crtend_so")
852 }
853 return deps
854 } else {
855 return c.ccBase.AndroidDynamicDependencies(ctx)
856 }
857}
858
859func (c *ccLibrary) collectDeps(ctx common.AndroidModuleContext, flags ccFlags) (ccDeps, ccFlags) {
860 if c.libraryProperties.IsStatic {
861 deps, flags := c.ccBase.collectDeps(ctx, flags)
862 wholeStaticLibNames := c.properties.Whole_static_libs
863 wholeStaticLibs, _, _ := c.collectDepsFromList(ctx, wholeStaticLibNames)
864
865 for _, m := range wholeStaticLibs {
866 if staticLib, ok := m.(*ccLibrary); ok && staticLib.libraryProperties.IsStatic {
867 deps.objFiles = append(deps.objFiles, staticLib.allObjFiles()...)
868 } else {
869 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
870 }
871 }
872
873 return deps, flags
874 } else if c.libraryProperties.IsShared {
875 return c.ccDynamic.collectDeps(ctx, flags)
876 } else {
877 panic("Not shared or static")
878 }
879}
880
881func (c *ccLibrary) outputFile() string {
882 return c.out
883}
884
885func (c *ccLibrary) allObjFiles() []string {
886 return c.objFiles
887}
888
889func (c *ccLibrary) exportedIncludeDirs() []string {
890 return c.exportIncludeDirs
891}
892
893func (c *ccLibrary) moduleTypeCflags(ctx common.AndroidModuleContext, toolchain toolchain) []string {
894 return []string{"-fPIC"}
895}
896
897func (c *ccLibrary) moduleTypeLdflags(ctx common.AndroidModuleContext, toolchain toolchain) []string {
898 if c.libraryProperties.IsShared {
899 libName := ctx.ModuleName()
900 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
901 sharedFlag := "-Wl,-shared"
902 if c.properties.Clang || ctx.Arch().HostOrDevice.Host() {
903 sharedFlag = "-shared"
904 }
905 if ctx.Arch().HostOrDevice.Device() {
906 return []string{
907 "-nostdlib",
908 "-Wl,--gc-sections",
909 sharedFlag,
Colin Cross6072ad42015-03-16 16:22:04 -0700910 "-Wl,-soname," + libName + sharedLibraryExtension,
Colin Cross3f40fa42015-01-30 17:27:36 -0800911 }
912 } else {
913 return []string{
914 "-Wl,--gc-sections",
915 sharedFlag,
Colin Cross6072ad42015-03-16 16:22:04 -0700916 "-Wl,-soname," + libName + sharedLibraryExtension,
Colin Cross3f40fa42015-01-30 17:27:36 -0800917 }
918 }
919 } else {
920 return nil
921 }
922}
923
924func (c *ccLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
925 flags ccFlags, deps ccDeps, objFiles []string) {
926
927 staticFlags := flags
928 staticFlags.cFlags = append(staticFlags.cFlags, c.libraryProperties.Static.Cflags...)
929 objFilesStatic := c.customCompileObjs(ctx, staticFlags, deps, common.DeviceStaticLibrary,
930 c.libraryProperties.Static.Srcs)
931
932 objFiles = append(objFiles, objFilesStatic...)
933
934 var includeDirs []string
935
936 wholeStaticLibNames := c.properties.Whole_static_libs
937 wholeStaticLibs, _, newIncludeDirs := c.collectDepsFromList(ctx, wholeStaticLibNames)
938 includeDirs = append(includeDirs, newIncludeDirs...)
939
940 for _, m := range wholeStaticLibs {
941 if staticLib, ok := m.(*ccLibrary); ok && staticLib.libraryProperties.IsStatic {
942 objFiles = append(objFiles, staticLib.allObjFiles()...)
943 } else {
944 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
945 }
946 }
947
948 staticLibNames := c.properties.Static_libs
949 _, _, newIncludeDirs = c.collectDepsFromList(ctx, staticLibNames)
950 includeDirs = append(includeDirs, newIncludeDirs...)
951
952 ctx.VisitDirectDeps(func(m blueprint.Module) {
953 if obj, ok := m.(*ccObject); ok {
954 otherName := ctx.OtherModuleName(m)
955 if !strings.HasPrefix(otherName, "crtbegin") && !strings.HasPrefix(otherName, "crtend") {
956 objFiles = append(objFiles, obj.outputFile())
957 }
958 }
959 })
960
961 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
962
963 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
964
965 c.objFiles = objFiles
966 c.out = outputFile
967 c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs,
968 common.ModuleSrcDir(ctx))
969
970 ctx.CheckbuildFile(outputFile)
971}
972
973func (c *ccLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
974 flags ccFlags, deps ccDeps, objFiles []string) {
975
976 sharedFlags := flags
977 sharedFlags.cFlags = append(sharedFlags.cFlags, c.libraryProperties.Shared.Cflags...)
978 objFilesShared := c.customCompileObjs(ctx, sharedFlags, deps, common.DeviceSharedLibrary,
979 c.libraryProperties.Shared.Srcs)
980
981 objFiles = append(objFiles, objFilesShared...)
982
983 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
984
Colin Cross77b00fa2015-03-16 16:15:49 -0700985 TransformObjToDynamicBinary(ctx, objFiles, deps.sharedLibs, deps.staticLibs,
986 deps.lateStaticLibs, deps.wholeStaticLibs, deps.crtBegin, deps.crtEnd,
987 ccFlagsToBuilderFlags(flags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -0800988
989 c.out = outputFile
990 c.exportIncludeDirs = pathtools.PrefixPaths(c.properties.Export_include_dirs,
991 common.ModuleSrcDir(ctx))
Colin Cross3f40fa42015-01-30 17:27:36 -0800992}
993
994func (c *ccLibrary) compileModule(ctx common.AndroidModuleContext,
995 flags ccFlags, deps ccDeps, objFiles []string) {
996
997 // Reuse the object files from the matching static library if it exists
998 if c.primary == c {
999 c.primaryObjFiles = objFiles
1000 } else {
1001 objFiles = append([]string(nil), c.primary.primaryObjFiles...)
1002 }
1003
1004 if c.libraryProperties.IsStatic {
1005 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1006 } else {
1007 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1008 }
1009}
1010
Dan Albertc403f7c2015-03-18 14:01:18 -07001011func (c *ccLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags ccFlags) {
1012 // Static libraries do not get installed.
1013}
1014
1015func (c *ccLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags ccFlags) {
1016 installDir := "lib"
1017 if flags.toolchain.Is64Bit() {
1018 installDir = "lib64"
1019 }
1020
1021 ctx.InstallFile(installDir, c.out)
1022}
1023
1024func (c *ccLibrary) installModule(ctx common.AndroidModuleContext, flags ccFlags) {
1025 if c.libraryProperties.IsStatic {
1026 c.installStaticLibrary(ctx, flags)
1027 } else {
1028 c.installSharedLibrary(ctx, flags)
1029 }
1030}
1031
Colin Cross3f40fa42015-01-30 17:27:36 -08001032//
1033// Objects (for crt*.o)
1034//
1035
1036type ccObject struct {
1037 ccBase
1038 out string
1039}
1040
1041func NewCCObject() (blueprint.Module, []interface{}) {
1042 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001043
Colin Crossc472d572015-03-17 15:06:21 -07001044 return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001045}
1046
1047func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1048 // object files can't have any dynamic dependencies
1049 return nil
1050}
1051
1052func (c *ccObject) collectDeps(ctx common.AndroidModuleContext, flags ccFlags) (ccDeps, ccFlags) {
1053 deps, flags := c.ccBase.collectDeps(ctx, flags)
1054 ctx.VisitDirectDeps(func(m blueprint.Module) {
1055 if obj, ok := m.(*ccObject); ok {
1056 deps.objFiles = append(deps.objFiles, obj.outputFile())
1057 } else {
1058 ctx.ModuleErrorf("Unknown module type for dependency %q", ctx.OtherModuleName(m))
1059 }
1060 })
1061
1062 return deps, flags
1063}
1064
1065func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
1066 flags ccFlags, deps ccDeps, objFiles []string) {
1067
1068 objFiles = append(objFiles, deps.objFiles...)
1069
1070 var outputFile string
1071 if len(objFiles) == 1 {
1072 outputFile = objFiles[0]
1073 } else {
1074 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+".o")
1075 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1076 }
1077
1078 c.out = outputFile
1079
1080 ctx.CheckbuildFile(outputFile)
1081}
1082
Dan Albertc403f7c2015-03-18 14:01:18 -07001083func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags ccFlags) {
1084 // Object files do not get installed.
1085}
1086
Colin Cross3f40fa42015-01-30 17:27:36 -08001087func (c *ccObject) outputFile() string {
1088 return c.out
1089}
1090
1091//
1092// Executables
1093//
1094
1095type ccBinary struct {
1096 ccDynamic
Dan Albertc403f7c2015-03-18 14:01:18 -07001097 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001098 binaryProperties binaryProperties
1099}
1100
1101type binaryProperties struct {
1102 // static_executable: compile executable with -static
1103 Static_executable bool
1104
1105 // stem: set the name of the output
1106 Stem string `android:"arch_variant"`
1107
1108 // prefix_symbols: if set, add an extra objcopy --prefix-symbols= step
1109 Prefix_symbols string
1110}
1111
1112func (c *ccBinary) getStem(ctx common.AndroidModuleContext) string {
1113 if c.binaryProperties.Stem != "" {
1114 return c.binaryProperties.Stem
1115 }
1116 return ctx.ModuleName()
1117}
1118
1119func (c *ccBinary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1120 deps := c.ccDynamic.AndroidDynamicDependencies(ctx)
1121 if c.HostOrDevice().Device() {
1122 if c.binaryProperties.Static_executable {
1123 deps = append(deps, "crtbegin_static", "crtend_android")
1124 } else {
1125 deps = append(deps, "crtbegin_dynamic", "crtend_android")
1126 }
1127 }
1128 return deps
1129}
1130
1131func NewCCBinary() (blueprint.Module, []interface{}) {
1132 module := &ccBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001133
Colin Crossc472d572015-03-17 15:06:21 -07001134 return newCCDynamic(&module.ccDynamic, module, common.HostAndDeviceSupported, common.MultilibFirst,
1135 &module.binaryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001136}
1137
1138func (c *ccBinary) moduleTypeCflags(ctx common.AndroidModuleContext, toolchain toolchain) []string {
1139 return []string{"-fpie"}
1140}
1141
1142func (c *ccBinary) moduleTypeLdflags(ctx common.AndroidModuleContext, toolchain toolchain) []string {
1143 if ctx.Arch().HostOrDevice.Device() {
1144 linker := "/system/bin/linker"
1145 if toolchain.Is64Bit() {
1146 linker = "/system/bin/linker64"
1147 }
1148
1149 return []string{
1150 "-nostdlib",
1151 "-Bdynamic",
1152 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1153 "-Wl,--gc-sections",
1154 "-Wl,-z,nocopyreloc",
1155 }
1156 }
1157
1158 return nil
1159}
1160
1161func (c *ccBinary) compileModule(ctx common.AndroidModuleContext,
1162 flags ccFlags, deps ccDeps, objFiles []string) {
1163
1164 if !c.binaryProperties.Static_executable && inList("libc", c.properties.Static_libs) {
1165 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1166 "from static libs or set static_executable: true")
1167 }
1168
1169 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001170 c.out = outputFile
Colin Cross3f40fa42015-01-30 17:27:36 -08001171
Colin Cross77b00fa2015-03-16 16:15:49 -07001172 TransformObjToDynamicBinary(ctx, objFiles, deps.sharedLibs, deps.staticLibs,
1173 deps.lateStaticLibs, deps.wholeStaticLibs, deps.crtBegin, deps.crtEnd,
1174 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001175}
Colin Cross3f40fa42015-01-30 17:27:36 -08001176
Dan Albertc403f7c2015-03-18 14:01:18 -07001177func (c *ccBinary) installModule(ctx common.AndroidModuleContext, flags ccFlags) {
1178 ctx.InstallFile("bin", c.out)
1179}
1180
1181type ccTest struct {
1182 ccBinary
1183}
1184
1185var (
1186 gtestLibs = []string{"libgtest", "libgtest_main"}
1187)
1188
1189func (c *ccTest) collectDeps(ctx common.AndroidModuleContext, flags ccFlags) (ccDeps, ccFlags) {
1190 deps, flags := c.ccBinary.collectDeps(ctx, flags)
1191
1192 flags.cFlags = append(flags.cFlags, "-DGTEST_HAS_STD_STRING")
1193 if c.HostOrDevice().Host() {
1194 flags.cFlags = append(flags.cFlags, "-O0", "-g")
1195 flags.ldLibs = append(flags.ldLibs, "-lpthread")
1196 }
1197
1198 // TODO(danalbert): Make gtest export its dependencies.
Tim Kilbourn5ccc7302015-03-19 10:02:21 -07001199 flags.includeDirs = append(flags.includeDirs,
1200 filepath.Join(ctx.Config().(Config).SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001201
1202 _, staticLibs, _ := c.collectDepsFromList(ctx, gtestLibs)
1203 deps.staticLibs = append(deps.staticLibs, staticLibs...)
1204
1205 return deps, flags
1206}
1207
1208func (c *ccTest) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1209 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, gtestLibs...)
1210 deps := c.ccBinary.AndroidDynamicDependencies(ctx)
1211 return append(deps, gtestLibs...)
1212}
1213
1214func (c *ccTest) installModule(ctx common.AndroidModuleContext, flags ccFlags) {
1215 if c.HostOrDevice().Device() {
Tim Kilbourn5ccc7302015-03-19 10:02:21 -07001216 ctx.InstallFile("../data/nativetest/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001217 } else {
1218 c.ccBinary.installModule(ctx, flags)
1219 }
1220}
1221
1222func NewCCTest() (blueprint.Module, []interface{}) {
1223 module := &ccTest{}
1224 return newCCDynamic(&module.ccDynamic, module, common.HostAndDeviceSupported,
1225 common.MultilibFirst, &module.binaryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001226}
1227
1228//
1229// Static library
1230//
1231
1232func NewCCLibraryStatic() (blueprint.Module, []interface{}) {
1233 module := &ccLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001234 module.libraryProperties.BuildStatic = true
1235
Colin Crossc472d572015-03-17 15:06:21 -07001236 return newCCLibrary(module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001237}
1238
1239//
1240// Shared libraries
1241//
1242
1243func NewCCLibraryShared() (blueprint.Module, []interface{}) {
1244 module := &ccLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001245 module.libraryProperties.BuildShared = true
1246
Colin Crossc472d572015-03-17 15:06:21 -07001247 return newCCLibrary(module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001248}
1249
1250//
1251// Host static library
1252//
1253
1254func NewCCLibraryHostStatic() (blueprint.Module, []interface{}) {
1255 module := &ccLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001256 module.libraryProperties.BuildStatic = true
1257
Colin Crossc472d572015-03-17 15:06:21 -07001258 return newCCLibrary(module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001259}
1260
1261//
1262// Host Shared libraries
1263//
1264
1265func NewCCLibraryHostShared() (blueprint.Module, []interface{}) {
1266 module := &ccLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001267 module.libraryProperties.BuildShared = true
1268
Colin Crossc472d572015-03-17 15:06:21 -07001269 return newCCLibrary(module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001270}
1271
1272//
1273// Host Binaries
1274//
1275
1276func NewCCBinaryHost() (blueprint.Module, []interface{}) {
1277 module := &ccBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001278
Colin Crossc472d572015-03-17 15:06:21 -07001279 return newCCDynamic(&module.ccDynamic, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001280}
1281
1282//
1283// Device libraries shipped with gcc
1284//
1285
1286type toolchainLibrary struct {
1287 ccLibrary
1288}
1289
1290func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string {
1291 // toolchain libraries can't have any dependencies
1292 return nil
1293}
1294
1295func (*toolchainLibrary) collectDeps(ctx common.AndroidModuleContext, flags ccFlags) (ccDeps, ccFlags) {
1296 // toolchain libraries can't have any dependencies
1297 return ccDeps{}, flags
1298}
1299
1300func NewToolchainLibrary() (blueprint.Module, []interface{}) {
1301 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001302
Colin Crossc472d572015-03-17 15:06:21 -07001303 return newCCBase(&module.ccBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001304}
1305
1306func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
1307 flags ccFlags, deps ccDeps, objFiles []string) {
1308
1309 libName := ctx.ModuleName() + staticLibraryExtension
1310 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1311
1312 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1313
1314 c.out = outputFile
1315
1316 ctx.CheckbuildFile(outputFile)
1317}
1318
Dan Albertc403f7c2015-03-18 14:01:18 -07001319func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags ccFlags) {
1320 // Toolchain libraries do not get installed.
1321}
1322
Colin Cross3f40fa42015-01-30 17:27:36 -08001323func LinkageMutator(mctx blueprint.EarlyMutatorContext) {
1324 if c, ok := mctx.Module().(*ccLibrary); ok {
1325 var modules []blueprint.Module
1326 if c.libraryProperties.BuildStatic && c.libraryProperties.BuildShared {
1327 modules = mctx.CreateLocalVariations("static", "shared")
1328 modules[0].(*ccLibrary).libraryProperties.IsStatic = true
1329 modules[1].(*ccLibrary).libraryProperties.IsShared = true
1330 } else if c.libraryProperties.BuildStatic {
1331 modules = mctx.CreateLocalVariations("static")
1332 modules[0].(*ccLibrary).libraryProperties.IsStatic = true
1333 } else if c.libraryProperties.BuildShared {
1334 modules = mctx.CreateLocalVariations("shared")
1335 modules[0].(*ccLibrary).libraryProperties.IsShared = true
1336 } else {
1337 panic("ccLibrary not static or shared")
1338 }
1339 primary := modules[0].(*ccLibrary)
1340 for _, m := range modules {
1341 m.(*ccLibrary).primary = primary
1342 if m != primary {
1343 m.(*ccLibrary).properties.SkipCompileObjs = true
1344 }
1345 }
1346 } else if _, ok := mctx.Module().(*toolchainLibrary); ok {
1347 mctx.CreateLocalVariations("static")
1348 }
1349}