blob: 3367a239f14c49dcddd4c728ccfb68ba17f0e0fa [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"
27 "github.com/google/blueprint/pathtools"
Colin Cross06a931b2015-10-28 17:23:31 -070028 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070029
Colin Cross463a90e2015-06-17 14:20:06 -070030 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080031 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070032 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Cross463a90e2015-06-17 14:20:06 -070035func init() {
36 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
37 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
38 soong.RegisterModuleType("cc_library", CCLibraryFactory)
39 soong.RegisterModuleType("cc_object", CCObjectFactory)
40 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
41 soong.RegisterModuleType("cc_test", CCTestFactory)
42 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080043 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070044
45 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
47 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
48 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
49 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
50
51 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
52 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
53 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
54 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
55 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
56
57 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
58 // the Go initialization order because this package depends on common, so common's init
59 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070060 common.RegisterBottomUpMutator("link", linkageMutator)
61 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
62 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070063}
64
Colin Cross3f40fa42015-01-30 17:27:36 -080065var (
Colin Cross1332b002015-04-07 17:11:30 -070066 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
67 SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
Colin Cross3f40fa42015-01-30 17:27:36 -080068
Dan Willemsen87b17d12015-07-14 00:39:06 -070069 LibcRoot = pctx.StaticVariable("LibcRoot", "bionic/libc")
70 LibmRoot = pctx.StaticVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080071)
72
73// Flags used by lots of devices. Putting them in package static variables will save bytes in
74// build.ninja so they aren't repeated for every file
75var (
76 commonGlobalCflags = []string{
77 "-DANDROID",
78 "-fmessage-length=0",
79 "-W",
80 "-Wall",
81 "-Wno-unused",
82 "-Winit-self",
83 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070084 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080085
86 // COMMON_RELEASE_CFLAGS
87 "-DNDEBUG",
88 "-UDEBUG",
89 }
90
91 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080092 "-fdiagnostics-color",
93
Colin Cross3f40fa42015-01-30 17:27:36 -080094 // TARGET_ERROR_FLAGS
95 "-Werror=return-type",
96 "-Werror=non-virtual-dtor",
97 "-Werror=address",
98 "-Werror=sequence-point",
99 }
100
101 hostGlobalCflags = []string{}
102
103 commonGlobalCppflags = []string{
104 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700105 }
106
107 illegalFlags = []string{
108 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800109 }
110)
111
112func init() {
113 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
114 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
115 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
116
117 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
118
119 pctx.StaticVariable("commonClangGlobalCflags",
120 strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " "))
121 pctx.StaticVariable("deviceClangGlobalCflags",
122 strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " "))
123 pctx.StaticVariable("hostClangGlobalCflags",
124 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700125 pctx.StaticVariable("commonClangGlobalCppflags",
126 strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800127
128 // Everything in this list is a crime against abstraction and dependency tracking.
129 // Do not add anything to this list.
130 pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{
131 "-isystem ${SrcDir}/system/core/include",
132 "-isystem ${SrcDir}/hardware/libhardware/include",
133 "-isystem ${SrcDir}/hardware/libhardware_legacy/include",
134 "-isystem ${SrcDir}/hardware/ril/include",
135 "-isystem ${SrcDir}/libnativehelper/include",
136 "-isystem ${SrcDir}/frameworks/native/include",
137 "-isystem ${SrcDir}/frameworks/native/opengl/include",
138 "-isystem ${SrcDir}/frameworks/av/include",
139 "-isystem ${SrcDir}/frameworks/base/include",
140 }, " "))
141
Dan Willemsenb30a8112015-11-24 13:02:49 -0800142 pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/host/${HostPrebuiltTag}/3.8/bin/")
Colin Cross3f40fa42015-01-30 17:27:36 -0800143}
144
Colin Cross6362e272015-10-29 15:25:03 -0700145type CCModuleContext common.AndroidBaseContext
146
Colin Cross3f40fa42015-01-30 17:27:36 -0800147// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700148type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800149 common.AndroidModule
150
Colin Crossfa138792015-04-24 17:31:52 -0700151 // Modify property values after parsing Blueprints file but before starting dependency
152 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700153 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700154
Colin Cross21b9a242015-03-24 14:15:58 -0700155 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700156 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800157
Colin Cross6362e272015-10-29 15:25:03 -0700158 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700159 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800160
Colin Cross6362e272015-10-29 15:25:03 -0700161 // Add dynamic dependencies
162 depsMutator(common.AndroidBottomUpMutatorContext)
163
Colin Cross3f40fa42015-01-30 17:27:36 -0800164 // Compile objects into final module
Colin Cross97ba0732015-03-23 17:50:24 -0700165 compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string)
Colin Cross3f40fa42015-01-30 17:27:36 -0800166
Dan Albertc403f7c2015-03-18 14:01:18 -0700167 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700168 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700169
Colin Cross3f40fa42015-01-30 17:27:36 -0800170 // Return the output file (.o, .a or .so) for use by other modules
171 outputFile() string
172}
173
Colin Cross97ba0732015-03-23 17:50:24 -0700174type CCDeps struct {
Colin Crossa48f71f2015-11-16 18:00:41 -0800175 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags, ReexportedCflags []string
Colin Crossc472d572015-03-17 15:06:21 -0700176
Colin Cross21b9a242015-03-24 14:15:58 -0700177 WholeStaticLibObjFiles []string
178
Colin Cross97ba0732015-03-23 17:50:24 -0700179 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700180}
181
Colin Cross97ba0732015-03-23 17:50:24 -0700182type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700183 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
184 AsFlags []string // Flags that apply to assembly source files
185 CFlags []string // Flags that apply to C and C++ source files
186 ConlyFlags []string // Flags that apply to C source files
187 CppFlags []string // Flags that apply to C++ source files
188 YaccFlags []string // Flags that apply to Yacc source files
189 LdFlags []string // Flags that apply to linker command lines
190
191 Nocrt bool
192 Toolchain Toolchain
193 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700194}
195
Colin Cross7d5136f2015-05-11 13:39:40 -0700196// Properties used to compile all C or C++ modules
197type CCBaseProperties struct {
198 // 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 -0700199 Srcs []string `android:"arch_variant"`
200
201 // list of source files that should not be used to build the C/C++ module.
202 // This is most useful in the arch/multilib variants to remove non-common files
203 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700204
205 // list of module-specific flags that will be used for C and C++ compiles.
206 Cflags []string `android:"arch_variant"`
207
208 // list of module-specific flags that will be used for C++ compiles
209 Cppflags []string `android:"arch_variant"`
210
211 // list of module-specific flags that will be used for C compiles
212 Conlyflags []string `android:"arch_variant"`
213
214 // list of module-specific flags that will be used for .S compiles
215 Asflags []string `android:"arch_variant"`
216
217 // list of module-specific flags that will be used for .y and .yy compiles
218 Yaccflags []string
219
220 // list of module-specific flags that will be used for all link steps
221 Ldflags []string `android:"arch_variant"`
222
223 // the instruction set architecture to use to compile the C/C++
224 // module.
225 Instruction_set string `android:"arch_variant"`
226
227 // list of directories relative to the root of the source tree that will
228 // be added to the include path using -I.
229 // If possible, don't use this. If adding paths from the current directory use
230 // local_include_dirs, if adding paths from other modules use export_include_dirs in
231 // that module.
232 Include_dirs []string `android:"arch_variant"`
233
Colin Cross39d97f22015-09-14 12:30:50 -0700234 // list of files relative to the root of the source tree that will be included
235 // using -include.
236 // If possible, don't use this.
237 Include_files []string `android:"arch_variant"`
238
Colin Cross7d5136f2015-05-11 13:39:40 -0700239 // list of directories relative to the Blueprints file that will
240 // be added to the include path using -I
241 Local_include_dirs []string `android:"arch_variant"`
242
Colin Cross39d97f22015-09-14 12:30:50 -0700243 // list of files relative to the Blueprints file that will be included
244 // using -include.
245 // If possible, don't use this.
246 Local_include_files []string `android:"arch_variant"`
247
Colin Cross7d5136f2015-05-11 13:39:40 -0700248 // list of directories relative to the Blueprints file that will
249 // be added to the include path using -I for any module that links against this module
250 Export_include_dirs []string `android:"arch_variant"`
251
252 // list of module-specific flags that will be used for C and C++ compiles when
253 // compiling with clang
254 Clang_cflags []string `android:"arch_variant"`
255
256 // list of module-specific flags that will be used for .S compiles when
257 // compiling with clang
258 Clang_asflags []string `android:"arch_variant"`
259
260 // list of system libraries that will be dynamically linked to
261 // shared library and executable modules. If unset, generally defaults to libc
262 // and libm. Set to [] to prevent linking against libc and libm.
263 System_shared_libs []string
264
265 // list of modules whose object files should be linked into this module
266 // in their entirety. For static library modules, all of the .o files from the intermediate
267 // directory of the dependency will be linked into this modules .a file. For a shared library,
268 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
269 Whole_static_libs []string `android:"arch_variant"`
270
271 // list of modules that should be statically linked into this module.
272 Static_libs []string `android:"arch_variant"`
273
274 // list of modules that should be dynamically linked into this module.
275 Shared_libs []string `android:"arch_variant"`
276
277 // allow the module to contain undefined symbols. By default,
278 // modules cannot contain undefined symbols that are not satisified by their immediate
279 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
280 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700281 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700282
283 // don't link in crt_begin and crt_end. This flag should only be necessary for
284 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700285 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700286
Dan Willemsend67be222015-09-16 15:19:33 -0700287 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700288 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700289
Colin Cross7d5136f2015-05-11 13:39:40 -0700290 // don't insert default compiler flags into asflags, cflags,
291 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700292 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700293
294 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700295 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700296
297 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700298 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700299
300 // -l arguments to pass to linker for host-provided shared libraries
301 Host_ldlibs []string `android:"arch_variant"`
302
303 // select the STL library to use. Possible values are "libc++", "libc++_static",
304 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
305 // default
306 Stl string
307
308 // Set for combined shared/static libraries to prevent compiling object files a second time
309 SkipCompileObjs bool `blueprint:"mutated"`
310
311 Debug, Release struct {
312 // list of module-specific flags that will be used for C and C++ compiles in debug or
313 // release builds
314 Cflags []string `android:"arch_variant"`
315 } `android:"arch_variant"`
316
317 // Minimum sdk version supported when compiling against the ndk
318 Sdk_version string
319
320 // install to a subdirectory of the default install path for the module
321 Relative_install_path string
322}
323
Colin Crosscfad1192015-11-02 16:43:11 -0800324type CCUnusedProperties struct {
325 Native_coverage *bool
326 Required []string
327 Sanitize []string `android:"arch_variant"`
328 Sanitize_recover []string
329 Strip string
330 Tags []string
331}
332
Colin Crossfa138792015-04-24 17:31:52 -0700333// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700334// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
335// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700336type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700337 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800338 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700339 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700340
Colin Cross7d5136f2015-05-11 13:39:40 -0700341 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700342
Colin Crosscfad1192015-11-02 16:43:11 -0800343 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700344
345 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700346
347 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700348}
349
Colin Crossfa138792015-04-24 17:31:52 -0700350func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700351 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
352
353 base.module = module
354
Colin Crossfa138792015-04-24 17:31:52 -0700355 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700356
Colin Crosscfad1192015-11-02 16:43:11 -0800357 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
358
359 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700360}
361
Colin Crossfa138792015-04-24 17:31:52 -0700362func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800363 toolchain := c.findToolchain(ctx)
364 if ctx.Failed() {
365 return
366 }
367
Colin Cross21b9a242015-03-24 14:15:58 -0700368 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800369 if ctx.Failed() {
370 return
371 }
372
Colin Cross74d1ec02015-04-28 13:30:13 -0700373 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800374 if ctx.Failed() {
375 return
376 }
377
Colin Cross28344522015-04-22 13:07:53 -0700378 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700379
Colin Cross581c1892015-04-07 16:50:10 -0700380 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 if ctx.Failed() {
382 return
383 }
384
Colin Cross581c1892015-04-07 16:50:10 -0700385 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700386 if ctx.Failed() {
387 return
388 }
389
390 objFiles = append(objFiles, generatedObjFiles...)
391
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
393 if ctx.Failed() {
394 return
395 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700396
397 c.ccModuleType().installModule(ctx, flags)
398 if ctx.Failed() {
399 return
400 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800401}
402
Colin Crossfa138792015-04-24 17:31:52 -0700403func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800404 return c.module
405}
406
Colin Crossfa138792015-04-24 17:31:52 -0700407func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700409 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800410 ht := ctx.HostType()
411 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800412 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800413 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800414 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800415 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800416 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800417}
418
Colin Cross6362e272015-10-29 15:25:03 -0700419func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700420}
421
Colin Crosse11befc2015-04-27 17:49:17 -0700422func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700423 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
424 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
425 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700426
Colin Cross21b9a242015-03-24 14:15:58 -0700427 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800428}
429
Colin Cross6362e272015-10-29 15:25:03 -0700430func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700431 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
432 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
433 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
434 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
435
436 staticLibs := c.savedDepNames.WholeStaticLibs
437 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
438 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700439 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800440
Colin Cross74d1ec02015-04-28 13:30:13 -0700441 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700442
Colin Cross6362e272015-10-29 15:25:03 -0700443 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700444 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700445 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700446 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700447 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700448 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700449 }
Colin Cross6362e272015-10-29 15:25:03 -0700450}
Colin Cross21b9a242015-03-24 14:15:58 -0700451
Colin Cross6362e272015-10-29 15:25:03 -0700452func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
453 if c, ok := ctx.Module().(CCModuleType); ok {
454 c.ModifyProperties(ctx)
455 c.depsMutator(ctx)
456 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800457}
458
459// Create a ccFlags struct that collects the compile flags from global values,
460// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700461func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700462 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700463 CFlags: c.Properties.Cflags,
464 CppFlags: c.Properties.Cppflags,
465 ConlyFlags: c.Properties.Conlyflags,
466 LdFlags: c.Properties.Ldflags,
467 AsFlags: c.Properties.Asflags,
468 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700469 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700470 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700471 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800472 }
Colin Cross28344522015-04-22 13:07:53 -0700473
474 // Include dir cflags
Colin Crossf2298272015-05-12 11:36:53 -0700475 common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
476 common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
477
Colin Crossfa138792015-04-24 17:31:52 -0700478 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
479 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700480 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700481 includeDirsToFlags(localIncludeDirs),
482 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700483
Colin Cross39d97f22015-09-14 12:30:50 -0700484 rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
485 localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
486
487 flags.GlobalFlags = append(flags.GlobalFlags,
488 includeFilesToFlags(rootIncludeFiles),
489 includeFilesToFlags(localIncludeFiles))
490
Colin Cross06a931b2015-10-28 17:23:31 -0700491 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700492 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700493 flags.GlobalFlags = append(flags.GlobalFlags,
494 "${commonGlobalIncludes}",
495 toolchain.IncludeFlags(),
496 "-I${SrcDir}/libnativehelper/include/nativehelper")
497 }
498
499 flags.GlobalFlags = append(flags.GlobalFlags, []string{
500 "-I" + common.ModuleSrcDir(ctx),
501 "-I" + common.ModuleOutDir(ctx),
502 "-I" + common.ModuleGenDir(ctx),
503 }...)
504 }
505
Colin Cross06a931b2015-10-28 17:23:31 -0700506 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700507 if ctx.Host() {
508 flags.Clang = true
509 }
510
511 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
512 flags.Clang = true
513 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800514 }
515
Dan Willemsen490fd492015-11-24 17:53:15 -0800516 if !toolchain.ClangSupported() {
517 flags.Clang = false
518 }
519
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800520 instructionSet := c.Properties.Instruction_set
521 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
522 if flags.Clang {
523 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
524 }
525 if err != nil {
526 ctx.ModuleErrorf("%s", err)
527 }
528
529 // TODO: debug
530 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
531
Colin Cross97ba0732015-03-23 17:50:24 -0700532 if flags.Clang {
533 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700534 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
535 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700536 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
537 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
538 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800539
Colin Cross97ba0732015-03-23 17:50:24 -0700540 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
541 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700542 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700543 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700544 }
545
Colin Cross3f40fa42015-01-30 17:27:36 -0800546 target := "-target " + toolchain.ClangTriple()
547 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
548
Colin Cross97ba0732015-03-23 17:50:24 -0700549 flags.CFlags = append(flags.CFlags, target, gccPrefix)
550 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
551 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800552 }
553
Colin Cross06a931b2015-10-28 17:23:31 -0700554 if !Bool(c.Properties.No_default_compiler_flags) {
555 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700556 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800557 }
558
Colin Cross56b4d452015-04-21 17:38:44 -0700559 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
560
Colin Cross97ba0732015-03-23 17:50:24 -0700561 if flags.Clang {
562 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700563 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800564 toolchain.ClangCflags(),
565 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700566 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800567 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700568 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700569 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 toolchain.Cflags(),
571 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700572 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 }
574
Colin Crossf6566ed2015-03-24 11:13:38 -0700575 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700576 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700577 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800578 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700579 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800580 }
581 }
582
Colin Cross97ba0732015-03-23 17:50:24 -0700583 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800584
Colin Cross97ba0732015-03-23 17:50:24 -0700585 if flags.Clang {
586 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
587 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800588 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700589 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
590 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 }
Colin Cross28344522015-04-22 13:07:53 -0700592
593 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700594 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700595 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 }
597
Colin Crossc4bde762015-11-23 16:11:30 -0800598 if flags.Clang {
599 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
600 } else {
601 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
602 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
603 }
604
Colin Cross0676e2d2015-04-24 17:39:18 -0700605 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800606
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700607 if c.Properties.Sdk_version == "" {
608 if ctx.Host() && !flags.Clang {
609 // The host GCC doesn't support C++14 (and is deprecated, so likely
610 // never will). Build these modules with C++11.
611 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
612 } else {
613 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
614 }
615 }
616
617 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
618 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
619 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
620
Colin Cross3f40fa42015-01-30 17:27:36 -0800621 // Optimization to reduce size of build.ninja
622 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700623 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
624 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
625 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
626 flags.CFlags = []string{"$cflags"}
627 flags.CppFlags = []string{"$cppflags"}
628 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800629
630 return flags
631}
632
Colin Cross0676e2d2015-04-24 17:39:18 -0700633func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800634 return flags
635}
636
637// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700638func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700639 subdir string, srcFiles, excludes []string) []string {
Colin Cross581c1892015-04-07 16:50:10 -0700640
641 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800642
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700643 srcFiles = ctx.ExpandSources(srcFiles, excludes)
Colin Cross581c1892015-04-07 16:50:10 -0700644 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800645
Colin Cross581c1892015-04-07 16:50:10 -0700646 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800647}
648
Colin Crossfa138792015-04-24 17:31:52 -0700649// Compile files listed in c.Properties.Srcs into objects
650func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800651
Colin Crossfa138792015-04-24 17:31:52 -0700652 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800653 return nil
654 }
655
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700656 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800657}
658
Colin Cross5049f022015-03-18 13:28:46 -0700659// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700660func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700661 var srcs []string
662
Colin Crossfa138792015-04-24 17:31:52 -0700663 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700664 return nil
665 }
666
667 ctx.VisitDirectDeps(func(module blueprint.Module) {
668 if gen, ok := module.(genrule.SourceFileGenerator); ok {
669 srcs = append(srcs, gen.GeneratedSourceFiles()...)
670 }
671 })
672
673 if len(srcs) == 0 {
674 return nil
675 }
676
Colin Cross581c1892015-04-07 16:50:10 -0700677 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700678}
679
Colin Crossfa138792015-04-24 17:31:52 -0700680func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800681 return ""
682}
683
Colin Crossfa138792015-04-24 17:31:52 -0700684func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800685 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700686 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800687
688 for _, n := range names {
689 found := false
690 ctx.VisitDirectDeps(func(m blueprint.Module) {
691 otherName := ctx.OtherModuleName(m)
692 if otherName != n {
693 return
694 }
695
Colin Cross97ba0732015-03-23 17:50:24 -0700696 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800697 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800698 // If a cc_library host+device module depends on a library that exists as both
699 // cc_library_shared and cc_library_host_shared, it will end up with two
700 // dependencies with the same name, one of which is marked disabled for each
701 // of host and device. Ignore the disabled one.
702 return
703 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700704 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800705 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
706 otherName)
707 return
708 }
709
710 if outputFile := a.outputFile(); outputFile != "" {
711 if found {
712 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
713 return
714 }
715 outputFiles = append(outputFiles, outputFile)
716 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700717 if i, ok := a.(ccExportedFlagsProducer); ok {
718 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800719 }
720 found = true
721 } else {
722 ctx.ModuleErrorf("module %q missing output file", otherName)
723 return
724 }
725 } else {
726 ctx.ModuleErrorf("module %q not an android module", otherName)
727 return
728 }
729 })
730 if !found {
731 ctx.ModuleErrorf("unsatisified dependency on %q", n)
732 }
733 }
734
Colin Cross28344522015-04-22 13:07:53 -0700735 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800736}
737
Colin Cross21b9a242015-03-24 14:15:58 -0700738// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
739// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700740func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700741 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700742 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800743
Colin Cross21b9a242015-03-24 14:15:58 -0700744 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800745
Colin Cross28344522015-04-22 13:07:53 -0700746 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700747 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700748 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800749 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800750
Colin Cross21b9a242015-03-24 14:15:58 -0700751 for _, m := range wholeStaticLibModules {
752 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
753 depPaths.WholeStaticLibObjFiles =
754 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
755 } else {
756 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
757 }
758 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800759
Colin Cross28344522015-04-22 13:07:53 -0700760 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
761 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700762
Colin Cross28344522015-04-22 13:07:53 -0700763 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
764 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700765
Colin Cross28344522015-04-22 13:07:53 -0700766 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
767 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700768
769 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700770 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700771 otherName := ctx.OtherModuleName(m)
772 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700773 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700774 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700775 }
776 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700777 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700778 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700779 }
780 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700781 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700782 }
783 }
784 })
785
786 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800787}
788
Colin Cross7d5136f2015-05-11 13:39:40 -0700789type ccLinkedProperties struct {
790 VariantIsShared bool `blueprint:"mutated"`
791 VariantIsStatic bool `blueprint:"mutated"`
792 VariantIsStaticBinary bool `blueprint:"mutated"`
793}
794
Colin Crossfa138792015-04-24 17:31:52 -0700795// CCLinked contains the properties and members used by libraries and executables
796type CCLinked struct {
797 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700798 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800799}
800
Colin Crossfa138792015-04-24 17:31:52 -0700801func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700802 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
803
Colin Crossed4cf0b2015-03-26 14:43:45 -0700804 props = append(props, &dynamic.dynamicProperties)
805
Colin Crossfa138792015-04-24 17:31:52 -0700806 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700807}
808
Colin Crossfa138792015-04-24 17:31:52 -0700809func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700810 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700811 return c.Properties.System_shared_libs
812 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700813 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700814 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700815 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800816 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800817}
818
Colin Crossfa138792015-04-24 17:31:52 -0700819func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
820 if c.Properties.Sdk_version != "" && ctx.Device() {
821 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700822 case "":
823 return "ndk_system"
824 case "c++_shared", "c++_static",
825 "stlport_shared", "stlport_static",
826 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700827 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700828 default:
Colin Crossfa138792015-04-24 17:31:52 -0700829 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700830 return ""
831 }
832 }
833
Dan Willemsen490fd492015-11-24 17:53:15 -0800834 if ctx.HostType() == common.Windows {
835 switch c.Properties.Stl {
836 case "libc++", "libc++_static", "libstdc++", "":
837 // libc++ is not supported on mingw
838 return "libstdc++"
839 case "none":
840 return ""
841 default:
842 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
843 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700844 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800845 } else {
846 switch c.Properties.Stl {
847 case "libc++", "libc++_static",
848 "libstdc++":
849 return c.Properties.Stl
850 case "none":
851 return ""
852 case "":
853 if c.static() {
854 return "libc++_static"
855 } else {
856 return "libc++"
857 }
858 default:
859 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
860 return ""
861 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700862 }
863}
864
Dan Willemsen490fd492015-11-24 17:53:15 -0800865var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700866
867func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800868 hostDynamicGccLibs = map[common.HostType][]string{
869 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
870 common.Darwin: []string{"-lc", "-lSystem"},
871 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
872 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
873 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
874 "-lmsvcrt"},
875 }
876 hostStaticGccLibs = map[common.HostType][]string{
877 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
878 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
879 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700880 }
881}
Colin Cross712fc022015-04-27 11:13:34 -0700882
Colin Crosse11befc2015-04-27 17:49:17 -0700883func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700884 stl := c.stl(ctx)
885 if ctx.Failed() {
886 return flags
887 }
888
889 switch stl {
890 case "libc++", "libc++_static":
891 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700892 if ctx.Host() {
893 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
894 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700895 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700896 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800897 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700898 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800899 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700900 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700901 } else {
902 if ctx.Arch().ArchType == common.Arm {
903 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
904 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700905 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700906 case "libstdc++":
907 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
908 // tree is in good enough shape to not need it.
909 // Host builds will use GNU libstdc++.
910 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700911 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700912 }
913 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700914 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700915 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700916 case "ndk_libc++_shared", "ndk_libc++_static":
917 // TODO(danalbert): This really shouldn't be here...
918 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
919 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
920 // Nothing
921 case "":
922 // None or error.
923 if ctx.Host() {
924 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
925 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700926 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800927 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700928 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800929 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700930 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700931 }
932 default:
Colin Crossfa138792015-04-24 17:31:52 -0700933 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700934 }
935
936 return flags
937}
938
Colin Crosse11befc2015-04-27 17:49:17 -0700939func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
940 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800941
Colin Crossed4cf0b2015-03-26 14:43:45 -0700942 stl := c.stl(ctx)
943 if ctx.Failed() {
944 return depNames
945 }
946
947 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700948 case "libstdc++":
949 if ctx.Device() {
950 depNames.SharedLibs = append(depNames.SharedLibs, stl)
951 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700952 case "libc++", "libc++_static":
953 if stl == "libc++" {
954 depNames.SharedLibs = append(depNames.SharedLibs, stl)
955 } else {
956 depNames.StaticLibs = append(depNames.StaticLibs, stl)
957 }
958 if ctx.Device() {
959 if ctx.Arch().ArchType == common.Arm {
960 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
961 }
962 if c.staticBinary() {
963 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
964 } else {
965 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
966 }
967 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700968 case "":
969 // None or error.
970 case "ndk_system":
971 // TODO: Make a system STL prebuilt for the NDK.
972 // 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 -0700973 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700974 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700975 case "ndk_libc++_shared", "ndk_libstlport_shared":
976 depNames.SharedLibs = append(depNames.SharedLibs, stl)
977 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
978 depNames.StaticLibs = append(depNames.StaticLibs, stl)
979 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700980 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700981 }
982
Colin Cross74d1ec02015-04-28 13:30:13 -0700983 if ctx.ModuleName() != "libcompiler_rt-extras" {
984 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
985 }
986
Colin Crossf6566ed2015-03-24 11:13:38 -0700987 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700988 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -0700989 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -0700990 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -0700991 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
992 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700993
Colin Cross18b6dc52015-04-28 13:20:37 -0700994 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700995 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
996 }
Colin Cross577f6e42015-03-27 18:23:34 -0700997
Colin Crossfa138792015-04-24 17:31:52 -0700998 if c.Properties.Sdk_version != "" {
999 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001000 depNames.SharedLibs = append(depNames.SharedLibs,
1001 "ndk_libc."+version,
1002 "ndk_libm."+version,
1003 )
1004 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001005 }
1006
Colin Cross21b9a242015-03-24 14:15:58 -07001007 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001008}
1009
Colin Crossed4cf0b2015-03-26 14:43:45 -07001010// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1011type ccLinkedInterface interface {
1012 // Returns true if the build options for the module have selected a static or shared build
1013 buildStatic() bool
1014 buildShared() bool
1015
1016 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001017 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001018
Colin Cross18b6dc52015-04-28 13:20:37 -07001019 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001020 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001021
1022 // Returns whether a module is a static binary
1023 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001024}
1025
1026var _ ccLinkedInterface = (*CCLibrary)(nil)
1027var _ ccLinkedInterface = (*CCBinary)(nil)
1028
Colin Crossfa138792015-04-24 17:31:52 -07001029func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001030 return c.dynamicProperties.VariantIsStatic
1031}
1032
Colin Cross18b6dc52015-04-28 13:20:37 -07001033func (c *CCLinked) staticBinary() bool {
1034 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001035}
1036
Colin Cross18b6dc52015-04-28 13:20:37 -07001037func (c *CCLinked) setStatic(static bool) {
1038 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001039}
1040
Colin Cross28344522015-04-22 13:07:53 -07001041type ccExportedFlagsProducer interface {
1042 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001043}
1044
1045//
1046// Combined static+shared libraries
1047//
1048
Colin Cross7d5136f2015-05-11 13:39:40 -07001049type CCLibraryProperties struct {
1050 BuildStatic bool `blueprint:"mutated"`
1051 BuildShared bool `blueprint:"mutated"`
1052 Static struct {
1053 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001054 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001055 Cflags []string `android:"arch_variant"`
1056 Whole_static_libs []string `android:"arch_variant"`
1057 Static_libs []string `android:"arch_variant"`
1058 Shared_libs []string `android:"arch_variant"`
1059 } `android:"arch_variant"`
1060 Shared struct {
1061 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001062 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001063 Cflags []string `android:"arch_variant"`
1064 Whole_static_libs []string `android:"arch_variant"`
1065 Static_libs []string `android:"arch_variant"`
1066 Shared_libs []string `android:"arch_variant"`
1067 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001068
1069 // local file name to pass to the linker as --version_script
1070 Version_script string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001071}
1072
Colin Cross97ba0732015-03-23 17:50:24 -07001073type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001074 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001075
Colin Cross28344522015-04-22 13:07:53 -07001076 reuseFrom ccLibraryInterface
1077 reuseObjFiles []string
1078 objFiles []string
1079 exportFlags []string
1080 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001081
Colin Cross7d5136f2015-05-11 13:39:40 -07001082 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001083}
1084
Colin Crossed4cf0b2015-03-26 14:43:45 -07001085func (c *CCLibrary) buildStatic() bool {
1086 return c.LibraryProperties.BuildStatic
1087}
1088
1089func (c *CCLibrary) buildShared() bool {
1090 return c.LibraryProperties.BuildShared
1091}
1092
Colin Cross97ba0732015-03-23 17:50:24 -07001093type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001094 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001095 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001096 setReuseFrom(ccLibraryInterface)
1097 getReuseFrom() ccLibraryInterface
1098 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -07001099 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -07001100}
1101
Colin Crossed4cf0b2015-03-26 14:43:45 -07001102var _ ccLibraryInterface = (*CCLibrary)(nil)
1103
Colin Cross97ba0732015-03-23 17:50:24 -07001104func (c *CCLibrary) ccLibrary() *CCLibrary {
1105 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001106}
1107
Colin Cross97ba0732015-03-23 17:50:24 -07001108func NewCCLibrary(library *CCLibrary, module CCModuleType,
1109 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1110
Colin Crossfa138792015-04-24 17:31:52 -07001111 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001112 &library.LibraryProperties)
1113}
1114
1115func CCLibraryFactory() (blueprint.Module, []interface{}) {
1116 module := &CCLibrary{}
1117
1118 module.LibraryProperties.BuildShared = true
1119 module.LibraryProperties.BuildStatic = true
1120
1121 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1122}
1123
Colin Cross0676e2d2015-04-24 17:39:18 -07001124func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001125 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001126 if c.static() {
1127 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1128 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1129 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1130 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001131 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001132 if c.Properties.Sdk_version == "" {
1133 depNames.CrtBegin = "crtbegin_so"
1134 depNames.CrtEnd = "crtend_so"
1135 } else {
1136 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1137 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1138 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001139 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001140 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1141 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1142 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001143 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001144
Colin Cross21b9a242015-03-24 14:15:58 -07001145 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001146}
1147
Colin Cross97ba0732015-03-23 17:50:24 -07001148func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001149 return c.out
1150}
1151
Colin Crossed4cf0b2015-03-26 14:43:45 -07001152func (c *CCLibrary) getReuseObjFiles() []string {
1153 return c.reuseObjFiles
1154}
1155
1156func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1157 c.reuseFrom = reuseFrom
1158}
1159
1160func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1161 return c.reuseFrom
1162}
1163
Colin Cross97ba0732015-03-23 17:50:24 -07001164func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001165 return c.objFiles
1166}
1167
Colin Cross28344522015-04-22 13:07:53 -07001168func (c *CCLibrary) exportedFlags() []string {
1169 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001170}
1171
Colin Cross0676e2d2015-04-24 17:39:18 -07001172func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001173 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001174
Dan Willemsen490fd492015-11-24 17:53:15 -08001175 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1176 // all code is position independent, and then those warnings get promoted to
1177 // errors.
1178 if ctx.HostType() != common.Windows {
1179 flags.CFlags = append(flags.CFlags, "-fPIC")
1180 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001181
Colin Crossd8e780d2015-04-28 17:39:43 -07001182 if c.static() {
1183 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1184 } else {
1185 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1186 }
1187
Colin Cross18b6dc52015-04-28 13:20:37 -07001188 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001189 libName := ctx.ModuleName()
1190 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1191 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001192 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001193 sharedFlag = "-shared"
1194 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001195 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001196 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001197 }
Colin Cross97ba0732015-03-23 17:50:24 -07001198
Colin Cross0af4b842015-04-30 16:36:18 -07001199 if ctx.Darwin() {
1200 flags.LdFlags = append(flags.LdFlags,
1201 "-dynamiclib",
1202 "-single_module",
1203 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001204 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001205 )
1206 } else {
1207 flags.LdFlags = append(flags.LdFlags,
1208 "-Wl,--gc-sections",
1209 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001210 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001211 )
1212 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001213 }
Colin Cross97ba0732015-03-23 17:50:24 -07001214
1215 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001216}
1217
Colin Cross97ba0732015-03-23 17:50:24 -07001218func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1219 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001220
1221 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001222 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001223 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001224
1225 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001226 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001227
1228 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1229
Colin Cross0af4b842015-04-30 16:36:18 -07001230 if ctx.Darwin() {
1231 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1232 } else {
1233 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1234 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001235
1236 c.objFiles = objFiles
1237 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001238
1239 common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
Colin Crossfa138792015-04-24 17:31:52 -07001240 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001241 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001242 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001243
1244 ctx.CheckbuildFile(outputFile)
1245}
1246
Colin Cross97ba0732015-03-23 17:50:24 -07001247func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1248 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001249
1250 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001251 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001252 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001253
1254 objFiles = append(objFiles, objFilesShared...)
1255
Dan Willemsen490fd492015-11-24 17:53:15 -08001256 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001257
Colin Crossaee540a2015-07-06 17:48:31 -07001258 var linkerDeps []string
1259
1260 if c.LibraryProperties.Version_script != "" {
1261 versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
1262 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
1263 linkerDeps = append(linkerDeps, versionScript)
1264 }
1265
Colin Cross97ba0732015-03-23 17:50:24 -07001266 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001267 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001268 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001269
1270 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001271 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001272 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001273 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001274}
1275
Colin Cross97ba0732015-03-23 17:50:24 -07001276func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1277 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001278
1279 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001280 if c.getReuseFrom().ccLibrary() == c {
1281 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001282 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001283 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1284 c.LibraryProperties.Shared.Cflags == nil {
1285 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1286 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001287 }
1288
Colin Crossed4cf0b2015-03-26 14:43:45 -07001289 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001290 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1291 } else {
1292 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1293 }
1294}
1295
Colin Cross97ba0732015-03-23 17:50:24 -07001296func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001297 // Static libraries do not get installed.
1298}
1299
Colin Cross97ba0732015-03-23 17:50:24 -07001300func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001301 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001302 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001303 installDir = "lib64"
1304 }
1305
Colin Crossfa138792015-04-24 17:31:52 -07001306 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001307}
1308
Colin Cross97ba0732015-03-23 17:50:24 -07001309func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001310 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001311 c.installStaticLibrary(ctx, flags)
1312 } else {
1313 c.installSharedLibrary(ctx, flags)
1314 }
1315}
1316
Colin Cross3f40fa42015-01-30 17:27:36 -08001317//
1318// Objects (for crt*.o)
1319//
1320
Dan Albertc3144b12015-04-28 18:17:56 -07001321type ccObjectProvider interface {
1322 object() *ccObject
1323}
1324
Colin Cross3f40fa42015-01-30 17:27:36 -08001325type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001326 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001327 out string
1328}
1329
Dan Albertc3144b12015-04-28 18:17:56 -07001330func (c *ccObject) object() *ccObject {
1331 return c
1332}
1333
Colin Cross97ba0732015-03-23 17:50:24 -07001334func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001335 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001336
Colin Crossfa138792015-04-24 17:31:52 -07001337 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001338}
1339
Colin Cross0676e2d2015-04-24 17:39:18 -07001340func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001341 // object files can't have any dynamic dependencies
1342 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001343}
1344
1345func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001346 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001347
Colin Cross97ba0732015-03-23 17:50:24 -07001348 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001349
1350 var outputFile string
1351 if len(objFiles) == 1 {
1352 outputFile = objFiles[0]
1353 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001354 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001355 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1356 }
1357
1358 c.out = outputFile
1359
1360 ctx.CheckbuildFile(outputFile)
1361}
1362
Colin Cross97ba0732015-03-23 17:50:24 -07001363func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001364 // Object files do not get installed.
1365}
1366
Colin Cross3f40fa42015-01-30 17:27:36 -08001367func (c *ccObject) outputFile() string {
1368 return c.out
1369}
1370
Dan Albertc3144b12015-04-28 18:17:56 -07001371var _ ccObjectProvider = (*ccObject)(nil)
1372
Colin Cross3f40fa42015-01-30 17:27:36 -08001373//
1374// Executables
1375//
1376
Colin Cross7d5136f2015-05-11 13:39:40 -07001377type CCBinaryProperties struct {
1378 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001379 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001380
1381 // set the name of the output
1382 Stem string `android:"arch_variant"`
1383
1384 // append to the name of the output
1385 Suffix string `android:"arch_variant"`
1386
1387 // if set, add an extra objcopy --prefix-symbols= step
1388 Prefix_symbols string
Colin Cross6002e052015-09-16 16:00:08 -07001389
1390 // Create a separate binary for each source file. Useful when there is
1391 // global state that can not be torn down and reset between each test suite.
Colin Cross06a931b2015-10-28 17:23:31 -07001392 Test_per_src *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001393}
1394
Colin Cross97ba0732015-03-23 17:50:24 -07001395type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001396 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001397 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001398 installFile string
Colin Cross7d5136f2015-05-11 13:39:40 -07001399 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001400}
1401
Colin Crossed4cf0b2015-03-26 14:43:45 -07001402func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001403 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001404}
1405
1406func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001407 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001408}
1409
Colin Cross97ba0732015-03-23 17:50:24 -07001410func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001411 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001412 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001413 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001414 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001415
1416 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001417}
1418
Colin Cross0676e2d2015-04-24 17:39:18 -07001419func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001420 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001421 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001422 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001423 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001424 depNames.CrtBegin = "crtbegin_static"
1425 } else {
1426 depNames.CrtBegin = "crtbegin_dynamic"
1427 }
1428 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001429 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001430 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001431 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1432 } else {
1433 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1434 }
1435 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001436 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001437
Colin Cross06a931b2015-10-28 17:23:31 -07001438 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001439 if c.stl(ctx) == "libc++_static" {
1440 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1441 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001442 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1443 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1444 // move them to the beginning of deps.LateStaticLibs
1445 var groupLibs []string
1446 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1447 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1448 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1449 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001450 }
Colin Cross21b9a242015-03-24 14:15:58 -07001451 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001452}
1453
Colin Cross97ba0732015-03-23 17:50:24 -07001454func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001455 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001456
Colin Cross1f8f2342015-03-26 16:09:47 -07001457 props = append(props, &binary.BinaryProperties)
1458
Colin Crossfa138792015-04-24 17:31:52 -07001459 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001460}
1461
Colin Cross97ba0732015-03-23 17:50:24 -07001462func CCBinaryFactory() (blueprint.Module, []interface{}) {
1463 module := &CCBinary{}
1464
1465 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001466}
1467
Colin Cross6362e272015-10-29 15:25:03 -07001468func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001469 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001470 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001471 }
Colin Cross06a931b2015-10-28 17:23:31 -07001472 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001473 c.dynamicProperties.VariantIsStaticBinary = true
1474 }
1475}
1476
Colin Cross0676e2d2015-04-24 17:39:18 -07001477func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001478 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001479
Dan Willemsen490fd492015-11-24 17:53:15 -08001480 if ctx.Host() {
1481 flags.LdFlags = append(flags.LdFlags, "-pie")
1482 if ctx.HostType() == common.Windows {
1483 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1484 }
1485 }
1486
1487 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1488 // all code is position independent, and then those warnings get promoted to
1489 // errors.
1490 if ctx.HostType() != common.Windows {
1491 flags.CFlags = append(flags.CFlags, "-fpie")
1492 }
Colin Cross97ba0732015-03-23 17:50:24 -07001493
Colin Crossf6566ed2015-03-24 11:13:38 -07001494 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001495 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001496 // Clang driver needs -static to create static executable.
1497 // However, bionic/linker uses -shared to overwrite.
1498 // Linker for x86 targets does not allow coexistance of -static and -shared,
1499 // so we add -static only if -shared is not used.
1500 if !inList("-shared", flags.LdFlags) {
1501 flags.LdFlags = append(flags.LdFlags, "-static")
1502 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001503
Colin Crossed4cf0b2015-03-26 14:43:45 -07001504 flags.LdFlags = append(flags.LdFlags,
1505 "-nostdlib",
1506 "-Bstatic",
1507 "-Wl,--gc-sections",
1508 )
1509
1510 } else {
1511 linker := "/system/bin/linker"
1512 if flags.Toolchain.Is64Bit() {
1513 linker = "/system/bin/linker64"
1514 }
1515
1516 flags.LdFlags = append(flags.LdFlags,
1517 "-nostdlib",
1518 "-Bdynamic",
1519 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1520 "-Wl,--gc-sections",
1521 "-Wl,-z,nocopyreloc",
1522 )
1523 }
Colin Cross0af4b842015-04-30 16:36:18 -07001524 } else if ctx.Darwin() {
1525 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001526 }
1527
Colin Cross97ba0732015-03-23 17:50:24 -07001528 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001529}
1530
Colin Cross97ba0732015-03-23 17:50:24 -07001531func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1532 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001533
Colin Cross06a931b2015-10-28 17:23:31 -07001534 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001535 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1536 "from static libs or set static_executable: true")
1537 }
1538
Dan Willemsen490fd492015-11-24 17:53:15 -08001539 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001540 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001541 if c.BinaryProperties.Prefix_symbols != "" {
1542 afterPrefixSymbols := outputFile
1543 outputFile = outputFile + ".intermediate"
1544 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1545 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1546 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001547
Colin Crossaee540a2015-07-06 17:48:31 -07001548 var linkerDeps []string
1549
Colin Cross97ba0732015-03-23 17:50:24 -07001550 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001551 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001552 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001553}
Colin Cross3f40fa42015-01-30 17:27:36 -08001554
Colin Cross97ba0732015-03-23 17:50:24 -07001555func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001556 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1557}
1558
1559func (c *CCBinary) HostToolPath() string {
1560 if c.HostOrDevice().Host() {
1561 return c.installFile
1562 }
1563 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001564}
1565
Colin Cross6002e052015-09-16 16:00:08 -07001566func (c *CCBinary) testPerSrc() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001567 return Bool(c.BinaryProperties.Test_per_src)
Colin Cross6002e052015-09-16 16:00:08 -07001568}
1569
1570func (c *CCBinary) binary() *CCBinary {
1571 return c
1572}
1573
1574type testPerSrc interface {
1575 binary() *CCBinary
1576 testPerSrc() bool
1577}
1578
1579var _ testPerSrc = (*CCBinary)(nil)
1580
Colin Cross6362e272015-10-29 15:25:03 -07001581func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001582 if test, ok := mctx.Module().(testPerSrc); ok {
1583 if test.testPerSrc() {
1584 testNames := make([]string, len(test.binary().Properties.Srcs))
1585 for i, src := range test.binary().Properties.Srcs {
1586 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1587 }
1588 tests := mctx.CreateLocalVariations(testNames...)
1589 for i, src := range test.binary().Properties.Srcs {
1590 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
1591 tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
1592 }
1593 }
1594 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001595}
1596
Colin Cross9ffb4f52015-04-24 17:48:09 -07001597type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001598 CCBinary
Dan Albertc403f7c2015-03-18 14:01:18 -07001599}
1600
Colin Cross9ffb4f52015-04-24 17:48:09 -07001601func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001602 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001603
Colin Cross97ba0732015-03-23 17:50:24 -07001604 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001605 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001606 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001607 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001608 }
1609
1610 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001611 flags.CFlags = append(flags.CFlags,
1612 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001613
Colin Cross21b9a242015-03-24 14:15:58 -07001614 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001615}
1616
Colin Cross9ffb4f52015-04-24 17:48:09 -07001617func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsene6540452015-10-20 15:21:33 -07001618 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
Colin Crossa8a93d32015-04-28 13:26:49 -07001619 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001620 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001621}
1622
Colin Cross9ffb4f52015-04-24 17:48:09 -07001623func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001624 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001625 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001626 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001627 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001628 }
1629}
1630
Colin Cross9ffb4f52015-04-24 17:48:09 -07001631func NewCCTest(test *CCTest, module CCModuleType,
1632 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1633
Colin Cross9ffb4f52015-04-24 17:48:09 -07001634 return NewCCBinary(&test.CCBinary, module, hod, props...)
1635}
1636
1637func CCTestFactory() (blueprint.Module, []interface{}) {
1638 module := &CCTest{}
1639
1640 return NewCCTest(module, module, common.HostAndDeviceSupported)
1641}
1642
Colin Cross2ba19d92015-05-07 15:44:20 -07001643type CCBenchmark struct {
1644 CCBinary
1645}
1646
1647func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1648 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001649 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001650 return depNames
1651}
1652
1653func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1654 if ctx.Device() {
1655 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1656 } else {
1657 c.CCBinary.installModule(ctx, flags)
1658 }
1659}
1660
1661func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1662 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1663
1664 return NewCCBinary(&test.CCBinary, module, hod, props...)
1665}
1666
1667func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1668 module := &CCBenchmark{}
1669
1670 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1671}
1672
Colin Cross3f40fa42015-01-30 17:27:36 -08001673//
1674// Static library
1675//
1676
Colin Cross97ba0732015-03-23 17:50:24 -07001677func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1678 module := &CCLibrary{}
1679 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001680
Colin Cross97ba0732015-03-23 17:50:24 -07001681 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001682}
1683
1684//
1685// Shared libraries
1686//
1687
Colin Cross97ba0732015-03-23 17:50:24 -07001688func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1689 module := &CCLibrary{}
1690 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001691
Colin Cross97ba0732015-03-23 17:50:24 -07001692 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001693}
1694
1695//
1696// Host static library
1697//
1698
Colin Cross97ba0732015-03-23 17:50:24 -07001699func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1700 module := &CCLibrary{}
1701 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001702
Colin Cross97ba0732015-03-23 17:50:24 -07001703 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001704}
1705
1706//
1707// Host Shared libraries
1708//
1709
Colin Cross97ba0732015-03-23 17:50:24 -07001710func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1711 module := &CCLibrary{}
1712 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001713
Colin Cross97ba0732015-03-23 17:50:24 -07001714 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001715}
1716
1717//
1718// Host Binaries
1719//
1720
Colin Cross97ba0732015-03-23 17:50:24 -07001721func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1722 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001723
Colin Cross97ba0732015-03-23 17:50:24 -07001724 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001725}
1726
1727//
Colin Cross1f8f2342015-03-26 16:09:47 -07001728// Host Tests
1729//
1730
1731func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001732 module := &CCTest{}
Colin Cross6002e052015-09-16 16:00:08 -07001733 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001734}
1735
1736//
Colin Cross2ba19d92015-05-07 15:44:20 -07001737// Host Benchmarks
1738//
1739
1740func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1741 module := &CCBenchmark{}
1742 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1743}
1744
1745//
Colin Crosscfad1192015-11-02 16:43:11 -08001746// Defaults
1747//
1748type CCDefaults struct {
1749 common.AndroidModuleBase
1750 common.DefaultsModule
1751}
1752
1753func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1754}
1755
1756func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1757 module := &CCDefaults{}
1758
1759 propertyStructs := []interface{}{
1760 &CCBaseProperties{},
1761 &CCLibraryProperties{},
1762 &CCBinaryProperties{},
1763 &CCUnusedProperties{},
1764 }
1765
1766 _, propertyStructs = common.InitAndroidArchModule(module, common.HostOrDeviceSupported(0),
1767 common.Multilib(""), propertyStructs...)
1768
1769 return common.InitDefaultsModule(module, module, propertyStructs...)
1770}
1771
1772//
Colin Cross3f40fa42015-01-30 17:27:36 -08001773// Device libraries shipped with gcc
1774//
1775
1776type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001777 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001778}
1779
Colin Cross0676e2d2015-04-24 17:39:18 -07001780func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001781 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001782 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001783}
1784
Colin Cross97ba0732015-03-23 17:50:24 -07001785func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001786 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001787
Colin Cross97ba0732015-03-23 17:50:24 -07001788 module.LibraryProperties.BuildStatic = true
1789
Colin Crossfa138792015-04-24 17:31:52 -07001790 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001791 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001792}
1793
1794func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001795 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001796
1797 libName := ctx.ModuleName() + staticLibraryExtension
1798 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1799
1800 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1801
1802 c.out = outputFile
1803
1804 ctx.CheckbuildFile(outputFile)
1805}
1806
Colin Cross97ba0732015-03-23 17:50:24 -07001807func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001808 // Toolchain libraries do not get installed.
1809}
1810
Dan Albertbe961682015-03-18 23:38:50 -07001811// NDK prebuilt libraries.
1812//
1813// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1814// either (with the exception of the shared STLs, which are installed to the app's directory rather
1815// than to the system image).
1816
1817func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1818 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001819 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001820}
1821
Dan Albertc3144b12015-04-28 18:17:56 -07001822func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1823 ext string, version string) string {
1824
1825 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1826 // We want to translate to just NAME.EXT
1827 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1828 dir := getNdkLibDir(ctx, toolchain, version)
1829 return filepath.Join(dir, name+ext)
1830}
1831
1832type ndkPrebuiltObject struct {
1833 ccObject
1834}
1835
Dan Albertc3144b12015-04-28 18:17:56 -07001836func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1837 // NDK objects can't have any dependencies
1838 return CCDeps{}
1839}
1840
1841func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1842 module := &ndkPrebuiltObject{}
1843 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1844}
1845
1846func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1847 deps CCDeps, objFiles []string) {
1848 // A null build step, but it sets up the output path.
1849 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1850 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1851 }
1852
1853 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1854}
1855
1856func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1857 // Objects do not get installed.
1858}
1859
1860var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1861
Dan Albertbe961682015-03-18 23:38:50 -07001862type ndkPrebuiltLibrary struct {
1863 CCLibrary
1864}
1865
Colin Cross0676e2d2015-04-24 17:39:18 -07001866func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001867 // NDK libraries can't have any dependencies
1868 return CCDeps{}
1869}
1870
1871func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1872 module := &ndkPrebuiltLibrary{}
1873 module.LibraryProperties.BuildShared = true
1874 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1875}
1876
1877func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1878 deps CCDeps, objFiles []string) {
1879 // A null build step, but it sets up the output path.
1880 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1881 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1882 }
1883
Colin Crossfa138792015-04-24 17:31:52 -07001884 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001885 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001886
Dan Willemsen490fd492015-11-24 17:53:15 -08001887 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001888 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001889}
1890
1891func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001892 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001893}
1894
1895// The NDK STLs are slightly different from the prebuilt system libraries:
1896// * Are not specific to each platform version.
1897// * The libraries are not in a predictable location for each STL.
1898
1899type ndkPrebuiltStl struct {
1900 ndkPrebuiltLibrary
1901}
1902
1903type ndkPrebuiltStaticStl struct {
1904 ndkPrebuiltStl
1905}
1906
1907type ndkPrebuiltSharedStl struct {
1908 ndkPrebuiltStl
1909}
1910
1911func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1912 module := &ndkPrebuiltSharedStl{}
1913 module.LibraryProperties.BuildShared = true
1914 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1915}
1916
1917func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1918 module := &ndkPrebuiltStaticStl{}
1919 module.LibraryProperties.BuildStatic = true
1920 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1921}
1922
1923func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1924 gccVersion := toolchain.GccVersion()
1925 var libDir string
1926 switch stl {
1927 case "libstlport":
1928 libDir = "cxx-stl/stlport/libs"
1929 case "libc++":
1930 libDir = "cxx-stl/llvm-libc++/libs"
1931 case "libgnustl":
1932 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1933 }
1934
1935 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001936 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001937 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1938 }
1939
1940 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1941 return ""
1942}
1943
1944func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1945 deps CCDeps, objFiles []string) {
1946 // A null build step, but it sets up the output path.
1947 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1948 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1949 }
1950
Colin Crossfa138792015-04-24 17:31:52 -07001951 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001952 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001953
1954 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08001955 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07001956 if c.LibraryProperties.BuildStatic {
1957 libExt = staticLibraryExtension
1958 }
1959
1960 stlName := strings.TrimSuffix(libName, "_shared")
1961 stlName = strings.TrimSuffix(stlName, "_static")
1962 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1963 c.out = libDir + "/" + libName + libExt
1964}
1965
Colin Cross6362e272015-10-29 15:25:03 -07001966func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001967 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001968 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001969 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001970 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001971 modules[0].(ccLinkedInterface).setStatic(true)
1972 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001973 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001974 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001975 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001976 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001977 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001978 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001979 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001980 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001981 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001982
1983 if _, ok := c.(ccLibraryInterface); ok {
1984 reuseFrom := modules[0].(ccLibraryInterface)
1985 for _, m := range modules {
1986 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001987 }
1988 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001989 }
1990}
Colin Cross74d1ec02015-04-28 13:30:13 -07001991
1992// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1993// modifies the slice contents in place, and returns a subslice of the original slice
1994func lastUniqueElements(list []string) []string {
1995 totalSkip := 0
1996 for i := len(list) - 1; i >= totalSkip; i-- {
1997 skip := 0
1998 for j := i - 1; j >= totalSkip; j-- {
1999 if list[i] == list[j] {
2000 skip++
2001 } else {
2002 list[j+skip] = list[j]
2003 }
2004 }
2005 totalSkip += skip
2006 }
2007 return list[totalSkip:]
2008}
Colin Cross06a931b2015-10-28 17:23:31 -07002009
2010var Bool = proptools.Bool