blob: b1bc069b5f8baf423fca841ef7c76ac6a15b9102 [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"
Colin Cross0af4b842015-04-30 16:36:18 -070024 "runtime"
Colin Cross3f40fa42015-01-30 17:27:36 -080025 "strings"
26
Colin Cross97ba0732015-03-23 17:50:24 -070027 "github.com/google/blueprint"
28 "github.com/google/blueprint/pathtools"
Colin Cross06a931b2015-10-28 17:23:31 -070029 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070030
Colin Cross463a90e2015-06-17 14:20:06 -070031 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080032 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070033 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080034)
35
Colin Cross463a90e2015-06-17 14:20:06 -070036func init() {
37 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
38 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
39 soong.RegisterModuleType("cc_library", CCLibraryFactory)
40 soong.RegisterModuleType("cc_object", CCObjectFactory)
41 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
42 soong.RegisterModuleType("cc_test", CCTestFactory)
43 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080044 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070045
46 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
47 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
48 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
49 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
50 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
51
52 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
53 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
54 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
55 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
56 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
57
58 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
59 // the Go initialization order because this package depends on common, so common's init
60 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070061 common.RegisterBottomUpMutator("link", linkageMutator)
62 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
63 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070064}
65
Colin Cross3f40fa42015-01-30 17:27:36 -080066var (
Colin Cross1332b002015-04-07 17:11:30 -070067 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
68 SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir)
Colin Cross3f40fa42015-01-30 17:27:36 -080069
Dan Willemsen87b17d12015-07-14 00:39:06 -070070 LibcRoot = pctx.StaticVariable("LibcRoot", "bionic/libc")
71 LibmRoot = pctx.StaticVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080072)
73
74// Flags used by lots of devices. Putting them in package static variables will save bytes in
75// build.ninja so they aren't repeated for every file
76var (
77 commonGlobalCflags = []string{
78 "-DANDROID",
79 "-fmessage-length=0",
80 "-W",
81 "-Wall",
82 "-Wno-unused",
83 "-Winit-self",
84 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070085 "-fdiagnostics-color",
86 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080087
88 // COMMON_RELEASE_CFLAGS
89 "-DNDEBUG",
90 "-UDEBUG",
91 }
92
93 deviceGlobalCflags = []string{
94 // 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()
410 factory := toolchainFactories[hod][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800411 if factory == nil {
Colin Crosseeabb892015-11-20 13:07:51 -0800412 ctx.ModuleErrorf("Toolchain not found for %s arch %q", hod.String(), arch.String())
413 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800414 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800415 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800416}
417
Colin Cross6362e272015-10-29 15:25:03 -0700418func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700419}
420
Colin Crosse11befc2015-04-27 17:49:17 -0700421func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700422 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
423 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
424 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700425
Colin Cross21b9a242015-03-24 14:15:58 -0700426 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800427}
428
Colin Cross6362e272015-10-29 15:25:03 -0700429func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700430 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
431 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
432 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
433 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
434
435 staticLibs := c.savedDepNames.WholeStaticLibs
436 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
437 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700438 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800439
Colin Cross74d1ec02015-04-28 13:30:13 -0700440 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700441
Colin Cross6362e272015-10-29 15:25:03 -0700442 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700443 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700444 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700445 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700446 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700447 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700448 }
Colin Cross6362e272015-10-29 15:25:03 -0700449}
Colin Cross21b9a242015-03-24 14:15:58 -0700450
Colin Cross6362e272015-10-29 15:25:03 -0700451func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
452 if c, ok := ctx.Module().(CCModuleType); ok {
453 c.ModifyProperties(ctx)
454 c.depsMutator(ctx)
455 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800456}
457
458// Create a ccFlags struct that collects the compile flags from global values,
459// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700460func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700461 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700462 CFlags: c.Properties.Cflags,
463 CppFlags: c.Properties.Cppflags,
464 ConlyFlags: c.Properties.Conlyflags,
465 LdFlags: c.Properties.Ldflags,
466 AsFlags: c.Properties.Asflags,
467 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700468 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700469 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700470 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800471 }
Colin Cross28344522015-04-22 13:07:53 -0700472
473 // Include dir cflags
Colin Crossf2298272015-05-12 11:36:53 -0700474 common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs")
475 common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs")
476
Colin Crossfa138792015-04-24 17:31:52 -0700477 rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir())
478 localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -0700479 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700480 includeDirsToFlags(localIncludeDirs),
481 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700482
Colin Cross39d97f22015-09-14 12:30:50 -0700483 rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir())
484 localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx))
485
486 flags.GlobalFlags = append(flags.GlobalFlags,
487 includeFilesToFlags(rootIncludeFiles),
488 includeFilesToFlags(localIncludeFiles))
489
Colin Cross06a931b2015-10-28 17:23:31 -0700490 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700491 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700492 flags.GlobalFlags = append(flags.GlobalFlags,
493 "${commonGlobalIncludes}",
494 toolchain.IncludeFlags(),
495 "-I${SrcDir}/libnativehelper/include/nativehelper")
496 }
497
498 flags.GlobalFlags = append(flags.GlobalFlags, []string{
499 "-I" + common.ModuleSrcDir(ctx),
500 "-I" + common.ModuleOutDir(ctx),
501 "-I" + common.ModuleGenDir(ctx),
502 }...)
503 }
504
Colin Cross06a931b2015-10-28 17:23:31 -0700505 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700506 if ctx.Host() {
507 flags.Clang = true
508 }
509
510 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
511 flags.Clang = true
512 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800513 }
514
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800515 instructionSet := c.Properties.Instruction_set
516 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
517 if flags.Clang {
518 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
519 }
520 if err != nil {
521 ctx.ModuleErrorf("%s", err)
522 }
523
524 // TODO: debug
525 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
526
Colin Cross97ba0732015-03-23 17:50:24 -0700527 if flags.Clang {
528 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700529 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
530 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700531 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
532 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
533 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800534
Colin Cross97ba0732015-03-23 17:50:24 -0700535 flags.CFlags = append(flags.CFlags, "${clangExtraCflags}")
536 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Crossf6566ed2015-03-24 11:13:38 -0700537 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -0700538 flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}")
Colin Crossbdd7b1c2015-03-16 16:21:20 -0700539 }
540
Colin Cross3f40fa42015-01-30 17:27:36 -0800541 target := "-target " + toolchain.ClangTriple()
542 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
543
Colin Cross97ba0732015-03-23 17:50:24 -0700544 flags.CFlags = append(flags.CFlags, target, gccPrefix)
545 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
546 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800547 }
548
Colin Cross06a931b2015-10-28 17:23:31 -0700549 if !Bool(c.Properties.No_default_compiler_flags) {
550 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700551 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800552 }
553
Colin Cross56b4d452015-04-21 17:38:44 -0700554 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
555
Colin Cross97ba0732015-03-23 17:50:24 -0700556 if flags.Clang {
557 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700558 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800559 toolchain.ClangCflags(),
560 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700561 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800562 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700563 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700564 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800565 toolchain.Cflags(),
566 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700567 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800568 }
569
Colin Crossf6566ed2015-03-24 11:13:38 -0700570 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700571 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700572 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700574 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800575 }
576 }
577
Colin Cross97ba0732015-03-23 17:50:24 -0700578 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800579
Colin Cross97ba0732015-03-23 17:50:24 -0700580 if flags.Clang {
581 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
582 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700584 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
585 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800586 }
Colin Cross28344522015-04-22 13:07:53 -0700587
588 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700589 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700590 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 }
592
Colin Crossc4bde762015-11-23 16:11:30 -0800593 if flags.Clang {
594 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
595 } else {
596 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
597 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
598 }
599
Colin Cross0676e2d2015-04-24 17:39:18 -0700600 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800601
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700602 if c.Properties.Sdk_version == "" {
603 if ctx.Host() && !flags.Clang {
604 // The host GCC doesn't support C++14 (and is deprecated, so likely
605 // never will). Build these modules with C++11.
606 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
607 } else {
608 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
609 }
610 }
611
612 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
613 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
614 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
615
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 // Optimization to reduce size of build.ninja
617 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700618 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
619 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
620 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
621 flags.CFlags = []string{"$cflags"}
622 flags.CppFlags = []string{"$cppflags"}
623 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800624
625 return flags
626}
627
Colin Cross0676e2d2015-04-24 17:39:18 -0700628func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800629 return flags
630}
631
632// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700633func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700634 subdir string, srcFiles, excludes []string) []string {
Colin Cross581c1892015-04-07 16:50:10 -0700635
636 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800637
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700638 srcFiles = ctx.ExpandSources(srcFiles, excludes)
Colin Cross581c1892015-04-07 16:50:10 -0700639 srcFiles, deps := genSources(ctx, srcFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800640
Colin Cross581c1892015-04-07 16:50:10 -0700641 return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800642}
643
Colin Crossfa138792015-04-24 17:31:52 -0700644// Compile files listed in c.Properties.Srcs into objects
645func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800646
Colin Crossfa138792015-04-24 17:31:52 -0700647 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800648 return nil
649 }
650
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700651 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800652}
653
Colin Cross5049f022015-03-18 13:28:46 -0700654// Compile generated source files from dependencies
Colin Crossfa138792015-04-24 17:31:52 -0700655func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string {
Colin Cross5049f022015-03-18 13:28:46 -0700656 var srcs []string
657
Colin Crossfa138792015-04-24 17:31:52 -0700658 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700659 return nil
660 }
661
662 ctx.VisitDirectDeps(func(module blueprint.Module) {
663 if gen, ok := module.(genrule.SourceFileGenerator); ok {
664 srcs = append(srcs, gen.GeneratedSourceFiles()...)
665 }
666 })
667
668 if len(srcs) == 0 {
669 return nil
670 }
671
Colin Cross581c1892015-04-07 16:50:10 -0700672 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700673}
674
Colin Crossfa138792015-04-24 17:31:52 -0700675func (c *CCBase) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -0800676 return ""
677}
678
Colin Crossfa138792015-04-24 17:31:52 -0700679func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800680 names []string) (modules []common.AndroidModule,
Colin Cross28344522015-04-22 13:07:53 -0700681 outputFiles []string, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800682
683 for _, n := range names {
684 found := false
685 ctx.VisitDirectDeps(func(m blueprint.Module) {
686 otherName := ctx.OtherModuleName(m)
687 if otherName != n {
688 return
689 }
690
Colin Cross97ba0732015-03-23 17:50:24 -0700691 if a, ok := m.(CCModuleType); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -0800692 if a.Disabled() {
693 // If a cc_library host+device module depends on a library that exists as both
694 // cc_library_shared and cc_library_host_shared, it will end up with two
695 // dependencies with the same name, one of which is marked disabled for each
696 // of host and device. Ignore the disabled one.
697 return
698 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700699 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800700 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
701 otherName)
702 return
703 }
704
705 if outputFile := a.outputFile(); outputFile != "" {
706 if found {
707 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
708 return
709 }
710 outputFiles = append(outputFiles, outputFile)
711 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700712 if i, ok := a.(ccExportedFlagsProducer); ok {
713 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800714 }
715 found = true
716 } else {
717 ctx.ModuleErrorf("module %q missing output file", otherName)
718 return
719 }
720 } else {
721 ctx.ModuleErrorf("module %q not an android module", otherName)
722 return
723 }
724 })
725 if !found {
726 ctx.ModuleErrorf("unsatisified dependency on %q", n)
727 }
728 }
729
Colin Cross28344522015-04-22 13:07:53 -0700730 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800731}
732
Colin Cross21b9a242015-03-24 14:15:58 -0700733// Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps
734// containing paths
Colin Crossfa138792015-04-24 17:31:52 -0700735func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -0700736 var depPaths CCDeps
Colin Cross28344522015-04-22 13:07:53 -0700737 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800738
Colin Cross21b9a242015-03-24 14:15:58 -0700739 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800740
Colin Cross28344522015-04-22 13:07:53 -0700741 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700742 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700743 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800744 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800745
Colin Cross21b9a242015-03-24 14:15:58 -0700746 for _, m := range wholeStaticLibModules {
747 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
748 depPaths.WholeStaticLibObjFiles =
749 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
750 } else {
751 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
752 }
753 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800754
Colin Cross28344522015-04-22 13:07:53 -0700755 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
756 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700757
Colin Cross28344522015-04-22 13:07:53 -0700758 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
759 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700760
Colin Cross28344522015-04-22 13:07:53 -0700761 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
762 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700763
764 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700765 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700766 otherName := ctx.OtherModuleName(m)
767 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700768 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700769 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700770 }
771 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700772 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700773 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700774 }
775 } else {
Dan Albertc3144b12015-04-28 18:17:56 -0700776 depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile())
Colin Cross21b9a242015-03-24 14:15:58 -0700777 }
778 }
779 })
780
781 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800782}
783
Colin Cross7d5136f2015-05-11 13:39:40 -0700784type ccLinkedProperties struct {
785 VariantIsShared bool `blueprint:"mutated"`
786 VariantIsStatic bool `blueprint:"mutated"`
787 VariantIsStaticBinary bool `blueprint:"mutated"`
788}
789
Colin Crossfa138792015-04-24 17:31:52 -0700790// CCLinked contains the properties and members used by libraries and executables
791type CCLinked struct {
792 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700793 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800794}
795
Colin Crossfa138792015-04-24 17:31:52 -0700796func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700797 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
798
Colin Crossed4cf0b2015-03-26 14:43:45 -0700799 props = append(props, &dynamic.dynamicProperties)
800
Colin Crossfa138792015-04-24 17:31:52 -0700801 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700802}
803
Colin Crossfa138792015-04-24 17:31:52 -0700804func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700805 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700806 return c.Properties.System_shared_libs
807 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700808 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700809 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700810 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800811 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800812}
813
Colin Crossfa138792015-04-24 17:31:52 -0700814func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
815 if c.Properties.Sdk_version != "" && ctx.Device() {
816 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700817 case "":
818 return "ndk_system"
819 case "c++_shared", "c++_static",
820 "stlport_shared", "stlport_static",
821 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700822 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700823 default:
Colin Crossfa138792015-04-24 17:31:52 -0700824 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700825 return ""
826 }
827 }
828
Colin Crossfa138792015-04-24 17:31:52 -0700829 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700830 case "libc++", "libc++_static",
Colin Crossed4cf0b2015-03-26 14:43:45 -0700831 "libstdc++":
Colin Crossfa138792015-04-24 17:31:52 -0700832 return c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700833 case "none":
834 return ""
835 case "":
Colin Cross18b6dc52015-04-28 13:20:37 -0700836 if c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700837 return "libc++_static"
Colin Cross18b6dc52015-04-28 13:20:37 -0700838 } else {
839 return "libc++" // TODO: mingw needs libstdc++
Colin Crossed4cf0b2015-03-26 14:43:45 -0700840 }
841 default:
Colin Crossfa138792015-04-24 17:31:52 -0700842 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700843 return ""
844 }
845}
846
Colin Cross0af4b842015-04-30 16:36:18 -0700847var hostDynamicGccLibs, hostStaticGccLibs []string
848
849func init() {
850 if runtime.GOOS == "darwin" {
851 hostDynamicGccLibs = []string{"-lc", "-lSystem"}
852 hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"}
853 } else {
854 hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"}
855 hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"}
856 }
857}
Colin Cross712fc022015-04-27 11:13:34 -0700858
Colin Crosse11befc2015-04-27 17:49:17 -0700859func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700860 stl := c.stl(ctx)
861 if ctx.Failed() {
862 return flags
863 }
864
865 switch stl {
866 case "libc++", "libc++_static":
867 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700868 if ctx.Host() {
869 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
870 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700871 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700872 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700873 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700874 } else {
875 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700876 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700877 } else {
878 if ctx.Arch().ArchType == common.Arm {
879 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
880 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700881 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700882 case "libstdc++":
883 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
884 // tree is in good enough shape to not need it.
885 // Host builds will use GNU libstdc++.
886 if ctx.Device() {
Colin Cross28344522015-04-22 13:07:53 -0700887 flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700888 }
889 case "ndk_system":
Colin Cross1332b002015-04-07 17:11:30 -0700890 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/"
Colin Cross28344522015-04-22 13:07:53 -0700891 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700892 case "ndk_libc++_shared", "ndk_libc++_static":
893 // TODO(danalbert): This really shouldn't be here...
894 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
895 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
896 // Nothing
897 case "":
898 // None or error.
899 if ctx.Host() {
900 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
901 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700902 if c.staticBinary() {
Colin Cross712fc022015-04-27 11:13:34 -0700903 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700904 } else {
905 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...)
Colin Cross712fc022015-04-27 11:13:34 -0700906 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700907 }
908 default:
Colin Crossfa138792015-04-24 17:31:52 -0700909 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700910 }
911
912 return flags
913}
914
Colin Crosse11befc2015-04-27 17:49:17 -0700915func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
916 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800917
Colin Crossed4cf0b2015-03-26 14:43:45 -0700918 stl := c.stl(ctx)
919 if ctx.Failed() {
920 return depNames
921 }
922
923 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700924 case "libstdc++":
925 if ctx.Device() {
926 depNames.SharedLibs = append(depNames.SharedLibs, stl)
927 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700928 case "libc++", "libc++_static":
929 if stl == "libc++" {
930 depNames.SharedLibs = append(depNames.SharedLibs, stl)
931 } else {
932 depNames.StaticLibs = append(depNames.StaticLibs, stl)
933 }
934 if ctx.Device() {
935 if ctx.Arch().ArchType == common.Arm {
936 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
937 }
938 if c.staticBinary() {
939 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
940 } else {
941 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
942 }
943 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700944 case "":
945 // None or error.
946 case "ndk_system":
947 // TODO: Make a system STL prebuilt for the NDK.
948 // 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 -0700949 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700950 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700951 case "ndk_libc++_shared", "ndk_libstlport_shared":
952 depNames.SharedLibs = append(depNames.SharedLibs, stl)
953 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
954 depNames.StaticLibs = append(depNames.StaticLibs, stl)
955 default:
Colin Crosse11befc2015-04-27 17:49:17 -0700956 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700957 }
958
Colin Cross74d1ec02015-04-28 13:30:13 -0700959 if ctx.ModuleName() != "libcompiler_rt-extras" {
960 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
961 }
962
Colin Crossf6566ed2015-03-24 11:13:38 -0700963 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -0700964 // libgcc and libatomic have to be last on the command line
Dan Willemsend67be222015-09-16 15:19:33 -0700965 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -0700966 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -0700967 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
968 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700969
Colin Cross18b6dc52015-04-28 13:20:37 -0700970 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700971 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
972 }
Colin Cross577f6e42015-03-27 18:23:34 -0700973
Colin Crossfa138792015-04-24 17:31:52 -0700974 if c.Properties.Sdk_version != "" {
975 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -0700976 depNames.SharedLibs = append(depNames.SharedLibs,
977 "ndk_libc."+version,
978 "ndk_libm."+version,
979 )
980 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800981 }
982
Colin Cross21b9a242015-03-24 14:15:58 -0700983 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800984}
985
Colin Crossed4cf0b2015-03-26 14:43:45 -0700986// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
987type ccLinkedInterface interface {
988 // Returns true if the build options for the module have selected a static or shared build
989 buildStatic() bool
990 buildShared() bool
991
992 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -0700993 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700994
Colin Cross18b6dc52015-04-28 13:20:37 -0700995 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -0700996 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -0700997
998 // Returns whether a module is a static binary
999 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001000}
1001
1002var _ ccLinkedInterface = (*CCLibrary)(nil)
1003var _ ccLinkedInterface = (*CCBinary)(nil)
1004
Colin Crossfa138792015-04-24 17:31:52 -07001005func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001006 return c.dynamicProperties.VariantIsStatic
1007}
1008
Colin Cross18b6dc52015-04-28 13:20:37 -07001009func (c *CCLinked) staticBinary() bool {
1010 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001011}
1012
Colin Cross18b6dc52015-04-28 13:20:37 -07001013func (c *CCLinked) setStatic(static bool) {
1014 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001015}
1016
Colin Cross28344522015-04-22 13:07:53 -07001017type ccExportedFlagsProducer interface {
1018 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001019}
1020
1021//
1022// Combined static+shared libraries
1023//
1024
Colin Cross7d5136f2015-05-11 13:39:40 -07001025type CCLibraryProperties struct {
1026 BuildStatic bool `blueprint:"mutated"`
1027 BuildShared bool `blueprint:"mutated"`
1028 Static struct {
1029 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001030 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001031 Cflags []string `android:"arch_variant"`
1032 Whole_static_libs []string `android:"arch_variant"`
1033 Static_libs []string `android:"arch_variant"`
1034 Shared_libs []string `android:"arch_variant"`
1035 } `android:"arch_variant"`
1036 Shared struct {
1037 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001038 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001039 Cflags []string `android:"arch_variant"`
1040 Whole_static_libs []string `android:"arch_variant"`
1041 Static_libs []string `android:"arch_variant"`
1042 Shared_libs []string `android:"arch_variant"`
1043 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001044
1045 // local file name to pass to the linker as --version_script
1046 Version_script string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001047}
1048
Colin Cross97ba0732015-03-23 17:50:24 -07001049type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001050 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001051
Colin Cross28344522015-04-22 13:07:53 -07001052 reuseFrom ccLibraryInterface
1053 reuseObjFiles []string
1054 objFiles []string
1055 exportFlags []string
1056 out string
Colin Cross3f40fa42015-01-30 17:27:36 -08001057
Colin Cross7d5136f2015-05-11 13:39:40 -07001058 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001059}
1060
Colin Crossed4cf0b2015-03-26 14:43:45 -07001061func (c *CCLibrary) buildStatic() bool {
1062 return c.LibraryProperties.BuildStatic
1063}
1064
1065func (c *CCLibrary) buildShared() bool {
1066 return c.LibraryProperties.BuildShared
1067}
1068
Colin Cross97ba0732015-03-23 17:50:24 -07001069type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001070 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001071 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001072 setReuseFrom(ccLibraryInterface)
1073 getReuseFrom() ccLibraryInterface
1074 getReuseObjFiles() []string
Colin Cross97ba0732015-03-23 17:50:24 -07001075 allObjFiles() []string
Colin Crossc472d572015-03-17 15:06:21 -07001076}
1077
Colin Crossed4cf0b2015-03-26 14:43:45 -07001078var _ ccLibraryInterface = (*CCLibrary)(nil)
1079
Colin Cross97ba0732015-03-23 17:50:24 -07001080func (c *CCLibrary) ccLibrary() *CCLibrary {
1081 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001082}
1083
Colin Cross97ba0732015-03-23 17:50:24 -07001084func NewCCLibrary(library *CCLibrary, module CCModuleType,
1085 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1086
Colin Crossfa138792015-04-24 17:31:52 -07001087 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001088 &library.LibraryProperties)
1089}
1090
1091func CCLibraryFactory() (blueprint.Module, []interface{}) {
1092 module := &CCLibrary{}
1093
1094 module.LibraryProperties.BuildShared = true
1095 module.LibraryProperties.BuildStatic = true
1096
1097 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1098}
1099
Colin Cross0676e2d2015-04-24 17:39:18 -07001100func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001101 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001102 if c.static() {
1103 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1104 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1105 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1106 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001107 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001108 if c.Properties.Sdk_version == "" {
1109 depNames.CrtBegin = "crtbegin_so"
1110 depNames.CrtEnd = "crtend_so"
1111 } else {
1112 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1113 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1114 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001115 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001116 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1117 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1118 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001119 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001120
Colin Cross21b9a242015-03-24 14:15:58 -07001121 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001122}
1123
Colin Cross97ba0732015-03-23 17:50:24 -07001124func (c *CCLibrary) outputFile() string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001125 return c.out
1126}
1127
Colin Crossed4cf0b2015-03-26 14:43:45 -07001128func (c *CCLibrary) getReuseObjFiles() []string {
1129 return c.reuseObjFiles
1130}
1131
1132func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1133 c.reuseFrom = reuseFrom
1134}
1135
1136func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1137 return c.reuseFrom
1138}
1139
Colin Cross97ba0732015-03-23 17:50:24 -07001140func (c *CCLibrary) allObjFiles() []string {
Colin Cross3f40fa42015-01-30 17:27:36 -08001141 return c.objFiles
1142}
1143
Colin Cross28344522015-04-22 13:07:53 -07001144func (c *CCLibrary) exportedFlags() []string {
1145 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001146}
1147
Colin Cross0676e2d2015-04-24 17:39:18 -07001148func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001149 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001150
Colin Cross97ba0732015-03-23 17:50:24 -07001151 flags.CFlags = append(flags.CFlags, "-fPIC")
Colin Cross3f40fa42015-01-30 17:27:36 -08001152
Colin Crossd8e780d2015-04-28 17:39:43 -07001153 if c.static() {
1154 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1155 } else {
1156 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1157 }
1158
Colin Cross18b6dc52015-04-28 13:20:37 -07001159 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001160 libName := ctx.ModuleName()
1161 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1162 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001163 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001164 sharedFlag = "-shared"
1165 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001166 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001167 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001168 }
Colin Cross97ba0732015-03-23 17:50:24 -07001169
Colin Cross0af4b842015-04-30 16:36:18 -07001170 if ctx.Darwin() {
1171 flags.LdFlags = append(flags.LdFlags,
1172 "-dynamiclib",
1173 "-single_module",
1174 //"-read_only_relocs suppress",
1175 "-install_name @rpath/"+libName+sharedLibraryExtension,
1176 )
1177 } else {
1178 flags.LdFlags = append(flags.LdFlags,
1179 "-Wl,--gc-sections",
1180 sharedFlag,
1181 "-Wl,-soname,"+libName+sharedLibraryExtension,
1182 )
1183 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001184 }
Colin Cross97ba0732015-03-23 17:50:24 -07001185
1186 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001187}
1188
Colin Cross97ba0732015-03-23 17:50:24 -07001189func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
1190 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001191
1192 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001193 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001194 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001195
1196 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001197 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001198
1199 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension)
1200
Colin Cross0af4b842015-04-30 16:36:18 -07001201 if ctx.Darwin() {
1202 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1203 } else {
1204 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1205 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001206
1207 c.objFiles = objFiles
1208 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001209
1210 common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs")
Colin Crossfa138792015-04-24 17:31:52 -07001211 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001212 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001213 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001214
1215 ctx.CheckbuildFile(outputFile)
1216}
1217
Colin Cross97ba0732015-03-23 17:50:24 -07001218func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
1219 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001220
1221 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001222 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001223 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001224
1225 objFiles = append(objFiles, objFilesShared...)
1226
1227 outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension)
1228
Colin Crossaee540a2015-07-06 17:48:31 -07001229 var linkerDeps []string
1230
1231 if c.LibraryProperties.Version_script != "" {
1232 versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script)
1233 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript)
1234 linkerDeps = append(linkerDeps, versionScript)
1235 }
1236
Colin Cross97ba0732015-03-23 17:50:24 -07001237 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001238 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001239 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001240
1241 c.out = outputFile
Colin Crossfa138792015-04-24 17:31:52 -07001242 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001243 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001244 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001245}
1246
Colin Cross97ba0732015-03-23 17:50:24 -07001247func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
1248 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001249
1250 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001251 if c.getReuseFrom().ccLibrary() == c {
1252 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001253 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001254 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1255 c.LibraryProperties.Shared.Cflags == nil {
1256 objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...)
1257 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001258 }
1259
Colin Crossed4cf0b2015-03-26 14:43:45 -07001260 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001261 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1262 } else {
1263 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1264 }
1265}
1266
Colin Cross97ba0732015-03-23 17:50:24 -07001267func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001268 // Static libraries do not get installed.
1269}
1270
Colin Cross97ba0732015-03-23 17:50:24 -07001271func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001272 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001273 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001274 installDir = "lib64"
1275 }
1276
Colin Crossfa138792015-04-24 17:31:52 -07001277 ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001278}
1279
Colin Cross97ba0732015-03-23 17:50:24 -07001280func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001281 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001282 c.installStaticLibrary(ctx, flags)
1283 } else {
1284 c.installSharedLibrary(ctx, flags)
1285 }
1286}
1287
Colin Cross3f40fa42015-01-30 17:27:36 -08001288//
1289// Objects (for crt*.o)
1290//
1291
Dan Albertc3144b12015-04-28 18:17:56 -07001292type ccObjectProvider interface {
1293 object() *ccObject
1294}
1295
Colin Cross3f40fa42015-01-30 17:27:36 -08001296type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001297 CCBase
Colin Cross3f40fa42015-01-30 17:27:36 -08001298 out string
1299}
1300
Dan Albertc3144b12015-04-28 18:17:56 -07001301func (c *ccObject) object() *ccObject {
1302 return c
1303}
1304
Colin Cross97ba0732015-03-23 17:50:24 -07001305func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001306 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001307
Colin Crossfa138792015-04-24 17:31:52 -07001308 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001309}
1310
Colin Cross0676e2d2015-04-24 17:39:18 -07001311func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001312 // object files can't have any dynamic dependencies
1313 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001314}
1315
1316func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001317 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001318
Colin Cross97ba0732015-03-23 17:50:24 -07001319 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001320
1321 var outputFile string
1322 if len(objFiles) == 1 {
1323 outputFile = objFiles[0]
1324 } else {
Dan Albertc3144b12015-04-28 18:17:56 -07001325 outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001326 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1327 }
1328
1329 c.out = outputFile
1330
1331 ctx.CheckbuildFile(outputFile)
1332}
1333
Colin Cross97ba0732015-03-23 17:50:24 -07001334func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001335 // Object files do not get installed.
1336}
1337
Colin Cross3f40fa42015-01-30 17:27:36 -08001338func (c *ccObject) outputFile() string {
1339 return c.out
1340}
1341
Dan Albertc3144b12015-04-28 18:17:56 -07001342var _ ccObjectProvider = (*ccObject)(nil)
1343
Colin Cross3f40fa42015-01-30 17:27:36 -08001344//
1345// Executables
1346//
1347
Colin Cross7d5136f2015-05-11 13:39:40 -07001348type CCBinaryProperties struct {
1349 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001350 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001351
1352 // set the name of the output
1353 Stem string `android:"arch_variant"`
1354
1355 // append to the name of the output
1356 Suffix string `android:"arch_variant"`
1357
1358 // if set, add an extra objcopy --prefix-symbols= step
1359 Prefix_symbols string
Colin Cross6002e052015-09-16 16:00:08 -07001360
1361 // Create a separate binary for each source file. Useful when there is
1362 // global state that can not be torn down and reset between each test suite.
Colin Cross06a931b2015-10-28 17:23:31 -07001363 Test_per_src *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001364}
1365
Colin Cross97ba0732015-03-23 17:50:24 -07001366type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001367 CCLinked
Dan Albertc403f7c2015-03-18 14:01:18 -07001368 out string
Colin Crossd350ecd2015-04-28 13:25:36 -07001369 installFile string
Colin Cross7d5136f2015-05-11 13:39:40 -07001370 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001371}
1372
Colin Crossed4cf0b2015-03-26 14:43:45 -07001373func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001374 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001375}
1376
1377func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001378 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001379}
1380
Colin Cross97ba0732015-03-23 17:50:24 -07001381func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001382 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001383 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001384 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001385 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001386
1387 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001388}
1389
Colin Cross0676e2d2015-04-24 17:39:18 -07001390func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001391 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001392 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001393 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001394 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001395 depNames.CrtBegin = "crtbegin_static"
1396 } else {
1397 depNames.CrtBegin = "crtbegin_dynamic"
1398 }
1399 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001400 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001401 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001402 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1403 } else {
1404 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1405 }
1406 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001407 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001408
Colin Cross06a931b2015-10-28 17:23:31 -07001409 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001410 if c.stl(ctx) == "libc++_static" {
1411 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1412 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001413 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1414 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1415 // move them to the beginning of deps.LateStaticLibs
1416 var groupLibs []string
1417 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1418 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1419 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1420 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001421 }
Colin Cross21b9a242015-03-24 14:15:58 -07001422 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001423}
1424
Colin Cross97ba0732015-03-23 17:50:24 -07001425func NewCCBinary(binary *CCBinary, module CCModuleType,
Colin Cross1f8f2342015-03-26 16:09:47 -07001426 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001427
Colin Cross1f8f2342015-03-26 16:09:47 -07001428 props = append(props, &binary.BinaryProperties)
1429
Colin Crossfa138792015-04-24 17:31:52 -07001430 return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001431}
1432
Colin Cross97ba0732015-03-23 17:50:24 -07001433func CCBinaryFactory() (blueprint.Module, []interface{}) {
1434 module := &CCBinary{}
1435
1436 return NewCCBinary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001437}
1438
Colin Cross6362e272015-10-29 15:25:03 -07001439func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001440 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001441 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001442 }
Colin Cross06a931b2015-10-28 17:23:31 -07001443 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001444 c.dynamicProperties.VariantIsStaticBinary = true
1445 }
1446}
1447
Colin Cross0676e2d2015-04-24 17:39:18 -07001448func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001449 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001450
Colin Cross97ba0732015-03-23 17:50:24 -07001451 flags.CFlags = append(flags.CFlags, "-fpie")
1452
Colin Crossf6566ed2015-03-24 11:13:38 -07001453 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001454 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001455 // Clang driver needs -static to create static executable.
1456 // However, bionic/linker uses -shared to overwrite.
1457 // Linker for x86 targets does not allow coexistance of -static and -shared,
1458 // so we add -static only if -shared is not used.
1459 if !inList("-shared", flags.LdFlags) {
1460 flags.LdFlags = append(flags.LdFlags, "-static")
1461 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001462
Colin Crossed4cf0b2015-03-26 14:43:45 -07001463 flags.LdFlags = append(flags.LdFlags,
1464 "-nostdlib",
1465 "-Bstatic",
1466 "-Wl,--gc-sections",
1467 )
1468
1469 } else {
1470 linker := "/system/bin/linker"
1471 if flags.Toolchain.Is64Bit() {
1472 linker = "/system/bin/linker64"
1473 }
1474
1475 flags.LdFlags = append(flags.LdFlags,
1476 "-nostdlib",
1477 "-Bdynamic",
1478 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1479 "-Wl,--gc-sections",
1480 "-Wl,-z,nocopyreloc",
1481 )
1482 }
Colin Cross0af4b842015-04-30 16:36:18 -07001483 } else if ctx.Darwin() {
1484 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001485 }
1486
Colin Cross97ba0732015-03-23 17:50:24 -07001487 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001488}
1489
Colin Cross97ba0732015-03-23 17:50:24 -07001490func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
1491 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001492
Colin Cross06a931b2015-10-28 17:23:31 -07001493 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001494 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1495 "from static libs or set static_executable: true")
1496 }
1497
1498 outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx))
Dan Albertc403f7c2015-03-18 14:01:18 -07001499 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001500 if c.BinaryProperties.Prefix_symbols != "" {
1501 afterPrefixSymbols := outputFile
1502 outputFile = outputFile + ".intermediate"
1503 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1504 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1505 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001506
Colin Crossaee540a2015-07-06 17:48:31 -07001507 var linkerDeps []string
1508
Colin Cross97ba0732015-03-23 17:50:24 -07001509 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001510 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001511 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001512}
Colin Cross3f40fa42015-01-30 17:27:36 -08001513
Colin Cross97ba0732015-03-23 17:50:24 -07001514func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossd350ecd2015-04-28 13:25:36 -07001515 c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out)
1516}
1517
1518func (c *CCBinary) HostToolPath() string {
1519 if c.HostOrDevice().Host() {
1520 return c.installFile
1521 }
1522 return ""
Dan Albertc403f7c2015-03-18 14:01:18 -07001523}
1524
Colin Cross6002e052015-09-16 16:00:08 -07001525func (c *CCBinary) testPerSrc() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001526 return Bool(c.BinaryProperties.Test_per_src)
Colin Cross6002e052015-09-16 16:00:08 -07001527}
1528
1529func (c *CCBinary) binary() *CCBinary {
1530 return c
1531}
1532
1533type testPerSrc interface {
1534 binary() *CCBinary
1535 testPerSrc() bool
1536}
1537
1538var _ testPerSrc = (*CCBinary)(nil)
1539
Colin Cross6362e272015-10-29 15:25:03 -07001540func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001541 if test, ok := mctx.Module().(testPerSrc); ok {
1542 if test.testPerSrc() {
1543 testNames := make([]string, len(test.binary().Properties.Srcs))
1544 for i, src := range test.binary().Properties.Srcs {
1545 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1546 }
1547 tests := mctx.CreateLocalVariations(testNames...)
1548 for i, src := range test.binary().Properties.Srcs {
1549 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
1550 tests[i].(testPerSrc).binary().BinaryProperties.Stem = mctx.ModuleName() + "_" + testNames[i]
1551 }
1552 }
1553 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001554}
1555
Colin Cross9ffb4f52015-04-24 17:48:09 -07001556type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001557 CCBinary
Dan Albertc403f7c2015-03-18 14:01:18 -07001558}
1559
Colin Cross9ffb4f52015-04-24 17:48:09 -07001560func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001561 flags = c.CCBinary.flags(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001562
Colin Cross97ba0732015-03-23 17:50:24 -07001563 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001564 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001565 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Colin Cross28344522015-04-22 13:07:53 -07001566 flags.LdFlags = append(flags.LdFlags, "-lpthread")
Dan Albertc403f7c2015-03-18 14:01:18 -07001567 }
1568
1569 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001570 flags.CFlags = append(flags.CFlags,
1571 "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include"))
Dan Albertc403f7c2015-03-18 14:01:18 -07001572
Colin Cross21b9a242015-03-24 14:15:58 -07001573 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001574}
1575
Colin Cross9ffb4f52015-04-24 17:48:09 -07001576func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsene6540452015-10-20 15:21:33 -07001577 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
Colin Crossa8a93d32015-04-28 13:26:49 -07001578 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001579 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001580}
1581
Colin Cross9ffb4f52015-04-24 17:48:09 -07001582func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossf6566ed2015-03-24 11:13:38 -07001583 if ctx.Device() {
Colin Crossa8a93d32015-04-28 13:26:49 -07001584 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001585 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001586 c.CCBinary.installModule(ctx, flags)
Dan Albertc403f7c2015-03-18 14:01:18 -07001587 }
1588}
1589
Colin Cross9ffb4f52015-04-24 17:48:09 -07001590func NewCCTest(test *CCTest, module CCModuleType,
1591 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1592
Colin Cross9ffb4f52015-04-24 17:48:09 -07001593 return NewCCBinary(&test.CCBinary, module, hod, props...)
1594}
1595
1596func CCTestFactory() (blueprint.Module, []interface{}) {
1597 module := &CCTest{}
1598
1599 return NewCCTest(module, module, common.HostAndDeviceSupported)
1600}
1601
Colin Cross2ba19d92015-05-07 15:44:20 -07001602type CCBenchmark struct {
1603 CCBinary
1604}
1605
1606func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1607 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001608 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001609 return depNames
1610}
1611
1612func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1613 if ctx.Device() {
1614 ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out)
1615 } else {
1616 c.CCBinary.installModule(ctx, flags)
1617 }
1618}
1619
1620func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1621 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1622
1623 return NewCCBinary(&test.CCBinary, module, hod, props...)
1624}
1625
1626func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1627 module := &CCBenchmark{}
1628
1629 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1630}
1631
Colin Cross3f40fa42015-01-30 17:27:36 -08001632//
1633// Static library
1634//
1635
Colin Cross97ba0732015-03-23 17:50:24 -07001636func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1637 module := &CCLibrary{}
1638 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001639
Colin Cross97ba0732015-03-23 17:50:24 -07001640 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001641}
1642
1643//
1644// Shared libraries
1645//
1646
Colin Cross97ba0732015-03-23 17:50:24 -07001647func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1648 module := &CCLibrary{}
1649 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001650
Colin Cross97ba0732015-03-23 17:50:24 -07001651 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001652}
1653
1654//
1655// Host static library
1656//
1657
Colin Cross97ba0732015-03-23 17:50:24 -07001658func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1659 module := &CCLibrary{}
1660 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001661
Colin Cross97ba0732015-03-23 17:50:24 -07001662 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001663}
1664
1665//
1666// Host Shared libraries
1667//
1668
Colin Cross97ba0732015-03-23 17:50:24 -07001669func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1670 module := &CCLibrary{}
1671 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001672
Colin Cross97ba0732015-03-23 17:50:24 -07001673 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001674}
1675
1676//
1677// Host Binaries
1678//
1679
Colin Cross97ba0732015-03-23 17:50:24 -07001680func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1681 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001682
Colin Cross97ba0732015-03-23 17:50:24 -07001683 return NewCCBinary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001684}
1685
1686//
Colin Cross1f8f2342015-03-26 16:09:47 -07001687// Host Tests
1688//
1689
1690func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001691 module := &CCTest{}
Colin Cross6002e052015-09-16 16:00:08 -07001692 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001693}
1694
1695//
Colin Cross2ba19d92015-05-07 15:44:20 -07001696// Host Benchmarks
1697//
1698
1699func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1700 module := &CCBenchmark{}
1701 return NewCCBinary(&module.CCBinary, module, common.HostSupported)
1702}
1703
1704//
Colin Crosscfad1192015-11-02 16:43:11 -08001705// Defaults
1706//
1707type CCDefaults struct {
1708 common.AndroidModuleBase
1709 common.DefaultsModule
1710}
1711
1712func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1713}
1714
1715func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1716 module := &CCDefaults{}
1717
1718 propertyStructs := []interface{}{
1719 &CCBaseProperties{},
1720 &CCLibraryProperties{},
1721 &CCBinaryProperties{},
1722 &CCUnusedProperties{},
1723 }
1724
1725 _, propertyStructs = common.InitAndroidArchModule(module, common.HostOrDeviceSupported(0),
1726 common.Multilib(""), propertyStructs...)
1727
1728 return common.InitDefaultsModule(module, module, propertyStructs...)
1729}
1730
1731//
Colin Cross3f40fa42015-01-30 17:27:36 -08001732// Device libraries shipped with gcc
1733//
1734
1735type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001736 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001737}
1738
Colin Cross0676e2d2015-04-24 17:39:18 -07001739func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001740 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001741 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001742}
1743
Colin Cross97ba0732015-03-23 17:50:24 -07001744func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001745 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001746
Colin Cross97ba0732015-03-23 17:50:24 -07001747 module.LibraryProperties.BuildStatic = true
1748
Colin Crossfa138792015-04-24 17:31:52 -07001749 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001750 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001751}
1752
1753func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Colin Cross97ba0732015-03-23 17:50:24 -07001754 flags CCFlags, deps CCDeps, objFiles []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001755
1756 libName := ctx.ModuleName() + staticLibraryExtension
1757 outputFile := filepath.Join(common.ModuleOutDir(ctx), libName)
1758
1759 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1760
1761 c.out = outputFile
1762
1763 ctx.CheckbuildFile(outputFile)
1764}
1765
Colin Cross97ba0732015-03-23 17:50:24 -07001766func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001767 // Toolchain libraries do not get installed.
1768}
1769
Dan Albertbe961682015-03-18 23:38:50 -07001770// NDK prebuilt libraries.
1771//
1772// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1773// either (with the exception of the shared STLs, which are installed to the app's directory rather
1774// than to the system image).
1775
1776func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string {
1777 return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
Colin Cross1332b002015-04-07 17:11:30 -07001778 ctx.AConfig().SrcDir(), version, toolchain.Name())
Dan Albertbe961682015-03-18 23:38:50 -07001779}
1780
Dan Albertc3144b12015-04-28 18:17:56 -07001781func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
1782 ext string, version string) string {
1783
1784 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1785 // We want to translate to just NAME.EXT
1786 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1787 dir := getNdkLibDir(ctx, toolchain, version)
1788 return filepath.Join(dir, name+ext)
1789}
1790
1791type ndkPrebuiltObject struct {
1792 ccObject
1793}
1794
Dan Albertc3144b12015-04-28 18:17:56 -07001795func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1796 // NDK objects can't have any dependencies
1797 return CCDeps{}
1798}
1799
1800func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1801 module := &ndkPrebuiltObject{}
1802 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1803}
1804
1805func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1806 deps CCDeps, objFiles []string) {
1807 // A null build step, but it sets up the output path.
1808 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1809 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1810 }
1811
1812 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version)
1813}
1814
1815func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1816 // Objects do not get installed.
1817}
1818
1819var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1820
Dan Albertbe961682015-03-18 23:38:50 -07001821type ndkPrebuiltLibrary struct {
1822 CCLibrary
1823}
1824
Colin Cross0676e2d2015-04-24 17:39:18 -07001825func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001826 // NDK libraries can't have any dependencies
1827 return CCDeps{}
1828}
1829
1830func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1831 module := &ndkPrebuiltLibrary{}
1832 module.LibraryProperties.BuildShared = true
1833 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1834}
1835
1836func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1837 deps CCDeps, objFiles []string) {
1838 // A null build step, but it sets up the output path.
1839 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1840 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1841 }
1842
Colin Crossfa138792015-04-24 17:31:52 -07001843 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001844 c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001845
Dan Albertc3144b12015-04-28 18:17:56 -07001846 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension,
1847 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001848}
1849
1850func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001851 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001852}
1853
1854// The NDK STLs are slightly different from the prebuilt system libraries:
1855// * Are not specific to each platform version.
1856// * The libraries are not in a predictable location for each STL.
1857
1858type ndkPrebuiltStl struct {
1859 ndkPrebuiltLibrary
1860}
1861
1862type ndkPrebuiltStaticStl struct {
1863 ndkPrebuiltStl
1864}
1865
1866type ndkPrebuiltSharedStl struct {
1867 ndkPrebuiltStl
1868}
1869
1870func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
1871 module := &ndkPrebuiltSharedStl{}
1872 module.LibraryProperties.BuildShared = true
1873 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1874}
1875
1876func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
1877 module := &ndkPrebuiltStaticStl{}
1878 module.LibraryProperties.BuildStatic = true
1879 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1880}
1881
1882func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string {
1883 gccVersion := toolchain.GccVersion()
1884 var libDir string
1885 switch stl {
1886 case "libstlport":
1887 libDir = "cxx-stl/stlport/libs"
1888 case "libc++":
1889 libDir = "cxx-stl/llvm-libc++/libs"
1890 case "libgnustl":
1891 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
1892 }
1893
1894 if libDir != "" {
Colin Cross1332b002015-04-07 17:11:30 -07001895 ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources"
Dan Albertbe961682015-03-18 23:38:50 -07001896 return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi)
1897 }
1898
1899 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
1900 return ""
1901}
1902
1903func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
1904 deps CCDeps, objFiles []string) {
1905 // A null build step, but it sets up the output path.
1906 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1907 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1908 }
1909
Colin Crossfa138792015-04-24 17:31:52 -07001910 includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx))
Colin Cross28344522015-04-22 13:07:53 -07001911 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07001912
1913 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
1914 libExt := sharedLibraryExtension
1915 if c.LibraryProperties.BuildStatic {
1916 libExt = staticLibraryExtension
1917 }
1918
1919 stlName := strings.TrimSuffix(libName, "_shared")
1920 stlName = strings.TrimSuffix(stlName, "_static")
1921 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
1922 c.out = libDir + "/" + libName + libExt
1923}
1924
Colin Cross6362e272015-10-29 15:25:03 -07001925func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001926 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08001927 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07001928 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001929 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001930 modules[0].(ccLinkedInterface).setStatic(true)
1931 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001932 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001933 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07001934 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001935 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001936 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07001937 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08001938 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07001939 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08001940 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001941
1942 if _, ok := c.(ccLibraryInterface); ok {
1943 reuseFrom := modules[0].(ccLibraryInterface)
1944 for _, m := range modules {
1945 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08001946 }
1947 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001948 }
1949}
Colin Cross74d1ec02015-04-28 13:30:13 -07001950
1951// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
1952// modifies the slice contents in place, and returns a subslice of the original slice
1953func lastUniqueElements(list []string) []string {
1954 totalSkip := 0
1955 for i := len(list) - 1; i >= totalSkip; i-- {
1956 skip := 0
1957 for j := i - 1; j >= totalSkip; j-- {
1958 if list[i] == list[j] {
1959 skip++
1960 } else {
1961 list[j+skip] = list[j]
1962 }
1963 }
1964 totalSkip += skip
1965 }
1966 return list[totalSkip:]
1967}
Colin Cross06a931b2015-10-28 17:23:31 -07001968
1969var Bool = proptools.Bool