blob: 65ccf3d327d65c1e1c8dff93f1cf0f3715c2dbd1 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
35 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
37 soong.RegisterModuleType("cc_library", CCLibraryFactory)
38 soong.RegisterModuleType("cc_object", CCObjectFactory)
39 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
40 soong.RegisterModuleType("cc_test", CCTestFactory)
41 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080042 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
44 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080083
84 // COMMON_RELEASE_CFLAGS
85 "-DNDEBUG",
86 "-UDEBUG",
87 }
88
89 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080090 "-fdiagnostics-color",
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092 // TARGET_ERROR_FLAGS
93 "-Werror=return-type",
94 "-Werror=non-virtual-dtor",
95 "-Werror=address",
96 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080097 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080098 }
99
100 hostGlobalCflags = []string{}
101
102 commonGlobalCppflags = []string{
103 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700104 }
105
106 illegalFlags = []string{
107 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800108 }
109)
110
111func init() {
112 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
113 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
114 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
115
116 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
117
118 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800119 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800121 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800122 pctx.StaticVariable("hostClangGlobalCflags",
123 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700124 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800125 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
127 // Everything in this list is a crime against abstraction and dependency tracking.
128 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800129 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700130 []string{
131 "system/core/include",
132 "hardware/libhardware/include",
133 "hardware/libhardware_legacy/include",
134 "hardware/ril/include",
135 "libnativehelper/include",
136 "frameworks/native/include",
137 "frameworks/native/opengl/include",
138 "frameworks/av/include",
139 "frameworks/base/include",
140 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800141 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
142 // with this, since there is no associated library.
143 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
144 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800145
Dan Willemsen767078e2016-03-01 13:42:19 -0800146 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2629532/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800147}
148
Colin Cross6362e272015-10-29 15:25:03 -0700149type CCModuleContext common.AndroidBaseContext
150
Colin Cross3f40fa42015-01-30 17:27:36 -0800151// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700152type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800153 common.AndroidModule
154
Colin Crossfa138792015-04-24 17:31:52 -0700155 // Modify property values after parsing Blueprints file but before starting dependency
156 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700157 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700158
Colin Cross21b9a242015-03-24 14:15:58 -0700159 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700160 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800161
Colin Cross6362e272015-10-29 15:25:03 -0700162 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700163 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800164
Colin Cross6362e272015-10-29 15:25:03 -0700165 // Add dynamic dependencies
166 depsMutator(common.AndroidBottomUpMutatorContext)
167
Colin Cross3f40fa42015-01-30 17:27:36 -0800168 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700169 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800170
Dan Albertc403f7c2015-03-18 14:01:18 -0700171 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700172 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700173
Colin Cross3f40fa42015-01-30 17:27:36 -0800174 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700175 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800176}
177
Colin Cross97ba0732015-03-23 17:50:24 -0700178type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700180
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700181 ObjFiles common.Paths
182
183 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700184
Colin Cross97ba0732015-03-23 17:50:24 -0700185 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700186}
187
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700188type CCPathDeps struct {
189 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
190
191 ObjFiles common.Paths
192 WholeStaticLibObjFiles common.Paths
193
194 Cflags, ReexportedCflags []string
195
196 CrtBegin, CrtEnd common.OptionalPath
197}
198
Colin Cross97ba0732015-03-23 17:50:24 -0700199type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700200 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
201 AsFlags []string // Flags that apply to assembly source files
202 CFlags []string // Flags that apply to C and C++ source files
203 ConlyFlags []string // Flags that apply to C source files
204 CppFlags []string // Flags that apply to C++ source files
205 YaccFlags []string // Flags that apply to Yacc source files
206 LdFlags []string // Flags that apply to linker command lines
207
208 Nocrt bool
209 Toolchain Toolchain
210 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700211}
212
Colin Cross7d5136f2015-05-11 13:39:40 -0700213// Properties used to compile all C or C++ modules
214type CCBaseProperties struct {
215 // 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 -0700216 Srcs []string `android:"arch_variant"`
217
218 // list of source files that should not be used to build the C/C++ module.
219 // This is most useful in the arch/multilib variants to remove non-common files
220 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700221
222 // list of module-specific flags that will be used for C and C++ compiles.
223 Cflags []string `android:"arch_variant"`
224
225 // list of module-specific flags that will be used for C++ compiles
226 Cppflags []string `android:"arch_variant"`
227
228 // list of module-specific flags that will be used for C compiles
229 Conlyflags []string `android:"arch_variant"`
230
231 // list of module-specific flags that will be used for .S compiles
232 Asflags []string `android:"arch_variant"`
233
234 // list of module-specific flags that will be used for .y and .yy compiles
235 Yaccflags []string
236
237 // list of module-specific flags that will be used for all link steps
238 Ldflags []string `android:"arch_variant"`
239
240 // the instruction set architecture to use to compile the C/C++
241 // module.
242 Instruction_set string `android:"arch_variant"`
243
244 // list of directories relative to the root of the source tree that will
245 // be added to the include path using -I.
246 // If possible, don't use this. If adding paths from the current directory use
247 // local_include_dirs, if adding paths from other modules use export_include_dirs in
248 // that module.
249 Include_dirs []string `android:"arch_variant"`
250
Colin Cross39d97f22015-09-14 12:30:50 -0700251 // list of files relative to the root of the source tree that will be included
252 // using -include.
253 // If possible, don't use this.
254 Include_files []string `android:"arch_variant"`
255
Colin Cross7d5136f2015-05-11 13:39:40 -0700256 // list of directories relative to the Blueprints file that will
257 // be added to the include path using -I
258 Local_include_dirs []string `android:"arch_variant"`
259
Colin Cross39d97f22015-09-14 12:30:50 -0700260 // list of files relative to the Blueprints file that will be included
261 // using -include.
262 // If possible, don't use this.
263 Local_include_files []string `android:"arch_variant"`
264
Colin Cross7d5136f2015-05-11 13:39:40 -0700265 // list of directories relative to the Blueprints file that will
266 // be added to the include path using -I for any module that links against this module
267 Export_include_dirs []string `android:"arch_variant"`
268
269 // list of module-specific flags that will be used for C and C++ compiles when
270 // compiling with clang
271 Clang_cflags []string `android:"arch_variant"`
272
273 // list of module-specific flags that will be used for .S compiles when
274 // compiling with clang
275 Clang_asflags []string `android:"arch_variant"`
276
277 // list of system libraries that will be dynamically linked to
278 // shared library and executable modules. If unset, generally defaults to libc
279 // and libm. Set to [] to prevent linking against libc and libm.
280 System_shared_libs []string
281
282 // list of modules whose object files should be linked into this module
283 // in their entirety. For static library modules, all of the .o files from the intermediate
284 // directory of the dependency will be linked into this modules .a file. For a shared library,
285 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
286 Whole_static_libs []string `android:"arch_variant"`
287
288 // list of modules that should be statically linked into this module.
289 Static_libs []string `android:"arch_variant"`
290
291 // list of modules that should be dynamically linked into this module.
292 Shared_libs []string `android:"arch_variant"`
293
294 // allow the module to contain undefined symbols. By default,
295 // modules cannot contain undefined symbols that are not satisified by their immediate
296 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
297 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700298 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700299
300 // don't link in crt_begin and crt_end. This flag should only be necessary for
301 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700302 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700303
Dan Willemsend67be222015-09-16 15:19:33 -0700304 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700305 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700306
Colin Cross7d5136f2015-05-11 13:39:40 -0700307 // don't insert default compiler flags into asflags, cflags,
308 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700309 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700310
311 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700312 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700313
314 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700315 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700316
317 // -l arguments to pass to linker for host-provided shared libraries
318 Host_ldlibs []string `android:"arch_variant"`
319
320 // select the STL library to use. Possible values are "libc++", "libc++_static",
321 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
322 // default
323 Stl string
324
325 // Set for combined shared/static libraries to prevent compiling object files a second time
326 SkipCompileObjs bool `blueprint:"mutated"`
327
328 Debug, Release struct {
329 // list of module-specific flags that will be used for C and C++ compiles in debug or
330 // release builds
331 Cflags []string `android:"arch_variant"`
332 } `android:"arch_variant"`
333
334 // Minimum sdk version supported when compiling against the ndk
335 Sdk_version string
336
337 // install to a subdirectory of the default install path for the module
338 Relative_install_path string
339}
340
Colin Crosscfad1192015-11-02 16:43:11 -0800341type CCUnusedProperties struct {
342 Native_coverage *bool
343 Required []string
344 Sanitize []string `android:"arch_variant"`
345 Sanitize_recover []string
346 Strip string
347 Tags []string
348}
349
Colin Crossfa138792015-04-24 17:31:52 -0700350// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700351// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
352// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700353type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700354 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800355 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700356 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700357
Colin Cross7d5136f2015-05-11 13:39:40 -0700358 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700359
Colin Crosscfad1192015-11-02 16:43:11 -0800360 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700361
362 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700363
364 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700365}
366
Colin Crossfa138792015-04-24 17:31:52 -0700367func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700368 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
369
370 base.module = module
371
Colin Crossfa138792015-04-24 17:31:52 -0700372 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700373
Colin Crosscfad1192015-11-02 16:43:11 -0800374 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
375
376 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700377}
378
Colin Crossfa138792015-04-24 17:31:52 -0700379func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800380 toolchain := c.findToolchain(ctx)
381 if ctx.Failed() {
382 return
383 }
384
Colin Cross21b9a242015-03-24 14:15:58 -0700385 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800386 if ctx.Failed() {
387 return
388 }
389
Colin Cross74d1ec02015-04-28 13:30:13 -0700390 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800391 if ctx.Failed() {
392 return
393 }
394
Colin Cross28344522015-04-22 13:07:53 -0700395 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700396
Colin Cross581c1892015-04-07 16:50:10 -0700397 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800398 if ctx.Failed() {
399 return
400 }
401
Colin Cross581c1892015-04-07 16:50:10 -0700402 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700403 if ctx.Failed() {
404 return
405 }
406
407 objFiles = append(objFiles, generatedObjFiles...)
408
Colin Cross3f40fa42015-01-30 17:27:36 -0800409 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
410 if ctx.Failed() {
411 return
412 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700413
414 c.ccModuleType().installModule(ctx, flags)
415 if ctx.Failed() {
416 return
417 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800418}
419
Colin Crossfa138792015-04-24 17:31:52 -0700420func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800421 return c.module
422}
423
Colin Crossfa138792015-04-24 17:31:52 -0700424func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800425 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700426 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800427 ht := ctx.HostType()
428 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800429 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800430 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800431 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800432 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800433 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800434}
435
Colin Cross6362e272015-10-29 15:25:03 -0700436func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700437}
438
Colin Crosse11befc2015-04-27 17:49:17 -0700439func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700440 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
441 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
442 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700443
Colin Cross21b9a242015-03-24 14:15:58 -0700444 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800445}
446
Colin Cross6362e272015-10-29 15:25:03 -0700447func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700448 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
449 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
450 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
451 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
452
453 staticLibs := c.savedDepNames.WholeStaticLibs
454 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
455 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700456 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800457
Colin Cross74d1ec02015-04-28 13:30:13 -0700458 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700459
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700460 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700461 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700462 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700463 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700464 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700465 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700466 }
Colin Cross6362e272015-10-29 15:25:03 -0700467}
Colin Cross21b9a242015-03-24 14:15:58 -0700468
Colin Cross6362e272015-10-29 15:25:03 -0700469func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
470 if c, ok := ctx.Module().(CCModuleType); ok {
471 c.ModifyProperties(ctx)
472 c.depsMutator(ctx)
473 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800474}
475
476// Create a ccFlags struct that collects the compile flags from global values,
477// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700478func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700479 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700480 CFlags: c.Properties.Cflags,
481 CppFlags: c.Properties.Cppflags,
482 ConlyFlags: c.Properties.Conlyflags,
483 LdFlags: c.Properties.Ldflags,
484 AsFlags: c.Properties.Asflags,
485 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700486 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700487 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700488 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800489 }
Colin Cross28344522015-04-22 13:07:53 -0700490
491 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700492 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
493 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700494 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700495 includeDirsToFlags(localIncludeDirs),
496 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700497
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700498 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
499 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700500
501 flags.GlobalFlags = append(flags.GlobalFlags,
502 includeFilesToFlags(rootIncludeFiles),
503 includeFilesToFlags(localIncludeFiles))
504
Colin Cross06a931b2015-10-28 17:23:31 -0700505 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700506 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700507 flags.GlobalFlags = append(flags.GlobalFlags,
508 "${commonGlobalIncludes}",
509 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800510 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700511 }
512
513 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700514 "-I" + common.PathForModuleSrc(ctx).String(),
515 "-I" + common.PathForModuleOut(ctx).String(),
516 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700517 }...)
518 }
519
Colin Cross06a931b2015-10-28 17:23:31 -0700520 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700521 if ctx.Host() {
522 flags.Clang = true
523 }
524
525 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
526 flags.Clang = true
527 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800528 }
529
Dan Willemsen490fd492015-11-24 17:53:15 -0800530 if !toolchain.ClangSupported() {
531 flags.Clang = false
532 }
533
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800534 instructionSet := c.Properties.Instruction_set
535 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
536 if flags.Clang {
537 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
538 }
539 if err != nil {
540 ctx.ModuleErrorf("%s", err)
541 }
542
543 // TODO: debug
544 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
545
Colin Cross97ba0732015-03-23 17:50:24 -0700546 if flags.Clang {
547 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700548 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
549 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700550 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
551 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
552 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800553
554 target := "-target " + toolchain.ClangTriple()
555 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
556
Colin Cross97ba0732015-03-23 17:50:24 -0700557 flags.CFlags = append(flags.CFlags, target, gccPrefix)
558 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
559 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800560 }
561
Colin Cross06a931b2015-10-28 17:23:31 -0700562 if !Bool(c.Properties.No_default_compiler_flags) {
563 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700564 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800565 }
566
Colin Cross56b4d452015-04-21 17:38:44 -0700567 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
568
Colin Cross97ba0732015-03-23 17:50:24 -0700569 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800570 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700571 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700572 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800573 toolchain.ClangCflags(),
574 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700575 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800576
577 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800578 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700579 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700580 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800581 toolchain.Cflags(),
582 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700583 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800584 }
585
Colin Cross7b66f152015-12-15 16:07:43 -0800586 if Bool(ctx.AConfig().ProductVariables.Brillo) {
587 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
588 }
589
Colin Crossf6566ed2015-03-24 11:13:38 -0700590 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700591 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700592 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800593 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700594 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800595 }
596 }
597
Colin Cross97ba0732015-03-23 17:50:24 -0700598 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800599
Colin Cross97ba0732015-03-23 17:50:24 -0700600 if flags.Clang {
601 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
602 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800603 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700604 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
605 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800606 }
Colin Cross28344522015-04-22 13:07:53 -0700607
608 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700609 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700610 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800611 }
612
Colin Crossc4bde762015-11-23 16:11:30 -0800613 if flags.Clang {
614 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
615 } else {
616 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800617 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800618 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800619
Colin Cross0676e2d2015-04-24 17:39:18 -0700620 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800621
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700622 if c.Properties.Sdk_version == "" {
623 if ctx.Host() && !flags.Clang {
624 // The host GCC doesn't support C++14 (and is deprecated, so likely
625 // never will). Build these modules with C++11.
626 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
627 } else {
628 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
629 }
630 }
631
632 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
633 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
634 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
635
Colin Cross3f40fa42015-01-30 17:27:36 -0800636 // Optimization to reduce size of build.ninja
637 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700638 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
639 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
640 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
641 flags.CFlags = []string{"$cflags"}
642 flags.CppFlags = []string{"$cppflags"}
643 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800644
645 return flags
646}
647
Colin Cross0676e2d2015-04-24 17:39:18 -0700648func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800649 return flags
650}
651
652// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700653func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700654 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700655
656 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800657
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700658 inputFiles := ctx.ExpandSources(srcFiles, excludes)
659 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800660
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800662}
663
Colin Crossfa138792015-04-24 17:31:52 -0700664// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700665func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800666
Colin Crossfa138792015-04-24 17:31:52 -0700667 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800668 return nil
669 }
670
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700671 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800672}
673
Colin Cross5049f022015-03-18 13:28:46 -0700674// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700675func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
676 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700677
Colin Crossfa138792015-04-24 17:31:52 -0700678 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700679 return nil
680 }
681
682 ctx.VisitDirectDeps(func(module blueprint.Module) {
683 if gen, ok := module.(genrule.SourceFileGenerator); ok {
684 srcs = append(srcs, gen.GeneratedSourceFiles()...)
685 }
686 })
687
688 if len(srcs) == 0 {
689 return nil
690 }
691
Colin Cross581c1892015-04-07 16:50:10 -0700692 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700693}
694
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700695func (c *CCBase) outputFile() common.OptionalPath {
696 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800697}
698
Colin Crossfa138792015-04-24 17:31:52 -0700699func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800700 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700701 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800702
703 for _, n := range names {
704 found := false
705 ctx.VisitDirectDeps(func(m blueprint.Module) {
706 otherName := ctx.OtherModuleName(m)
707 if otherName != n {
708 return
709 }
710
Colin Cross97ba0732015-03-23 17:50:24 -0700711 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800712 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800713 // If a cc_library host+device module depends on a library that exists as both
714 // cc_library_shared and cc_library_host_shared, it will end up with two
715 // dependencies with the same name, one of which is marked disabled for each
716 // of host and device. Ignore the disabled one.
717 return
718 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700719 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800720 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
721 otherName)
722 return
723 }
724
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700725 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800726 if found {
727 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
728 return
729 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700730 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800731 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700732 if i, ok := a.(ccExportedFlagsProducer); ok {
733 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800734 }
735 found = true
736 } else {
737 ctx.ModuleErrorf("module %q missing output file", otherName)
738 return
739 }
740 } else {
741 ctx.ModuleErrorf("module %q not an android module", otherName)
742 return
743 }
744 })
Colin Cross6ff51382015-12-17 16:39:19 -0800745 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 ctx.ModuleErrorf("unsatisified dependency on %q", n)
747 }
748 }
749
Colin Cross28344522015-04-22 13:07:53 -0700750 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800751}
752
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700753// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700754// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700755func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
756 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700757 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800758
Colin Cross21b9a242015-03-24 14:15:58 -0700759 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800760
Colin Cross28344522015-04-22 13:07:53 -0700761 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700762 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700763 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800764 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800765
Colin Cross21b9a242015-03-24 14:15:58 -0700766 for _, m := range wholeStaticLibModules {
767 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
768 depPaths.WholeStaticLibObjFiles =
769 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
770 } else {
771 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
772 }
773 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800774
Colin Cross28344522015-04-22 13:07:53 -0700775 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
776 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700777
Colin Cross28344522015-04-22 13:07:53 -0700778 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
779 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700780
Colin Cross28344522015-04-22 13:07:53 -0700781 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
782 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700783
784 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700785 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700786 otherName := ctx.OtherModuleName(m)
787 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700788 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700789 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700790 }
791 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700792 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700793 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700794 }
795 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700796 output := obj.object().outputFile()
797 if output.Valid() {
798 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
799 } else {
800 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
801 }
Colin Cross21b9a242015-03-24 14:15:58 -0700802 }
803 }
804 })
805
806 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800807}
808
Colin Cross7d5136f2015-05-11 13:39:40 -0700809type ccLinkedProperties struct {
810 VariantIsShared bool `blueprint:"mutated"`
811 VariantIsStatic bool `blueprint:"mutated"`
812 VariantIsStaticBinary bool `blueprint:"mutated"`
813}
814
Colin Crossfa138792015-04-24 17:31:52 -0700815// CCLinked contains the properties and members used by libraries and executables
816type CCLinked struct {
817 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700818 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800819}
820
Colin Crossfa138792015-04-24 17:31:52 -0700821func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700822 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
823
Colin Crossed4cf0b2015-03-26 14:43:45 -0700824 props = append(props, &dynamic.dynamicProperties)
825
Colin Crossfa138792015-04-24 17:31:52 -0700826 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700827}
828
Colin Crossfa138792015-04-24 17:31:52 -0700829func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700830 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700831 return c.Properties.System_shared_libs
832 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700833 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700834 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700835 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800836 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800837}
838
Colin Crossfa138792015-04-24 17:31:52 -0700839func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
840 if c.Properties.Sdk_version != "" && ctx.Device() {
841 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700842 case "":
843 return "ndk_system"
844 case "c++_shared", "c++_static",
845 "stlport_shared", "stlport_static",
846 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700847 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700848 default:
Colin Crossfa138792015-04-24 17:31:52 -0700849 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700850 return ""
851 }
852 }
853
Dan Willemsen490fd492015-11-24 17:53:15 -0800854 if ctx.HostType() == common.Windows {
855 switch c.Properties.Stl {
856 case "libc++", "libc++_static", "libstdc++", "":
857 // libc++ is not supported on mingw
858 return "libstdc++"
859 case "none":
860 return ""
861 default:
862 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
863 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700864 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800865 } else {
866 switch c.Properties.Stl {
867 case "libc++", "libc++_static",
868 "libstdc++":
869 return c.Properties.Stl
870 case "none":
871 return ""
872 case "":
873 if c.static() {
874 return "libc++_static"
875 } else {
876 return "libc++"
877 }
878 default:
879 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
880 return ""
881 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700882 }
883}
884
Dan Willemsen490fd492015-11-24 17:53:15 -0800885var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700886
887func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800888 hostDynamicGccLibs = map[common.HostType][]string{
889 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
890 common.Darwin: []string{"-lc", "-lSystem"},
891 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
892 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
893 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
894 "-lmsvcrt"},
895 }
896 hostStaticGccLibs = map[common.HostType][]string{
897 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
898 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
899 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700900 }
901}
Colin Cross712fc022015-04-27 11:13:34 -0700902
Colin Crosse11befc2015-04-27 17:49:17 -0700903func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700904 stl := c.stl(ctx)
905 if ctx.Failed() {
906 return flags
907 }
908
909 switch stl {
910 case "libc++", "libc++_static":
911 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700912 if ctx.Host() {
913 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
914 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700915 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700916 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800917 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700918 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800919 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700920 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700921 } else {
922 if ctx.Arch().ArchType == common.Arm {
923 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
924 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700925 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700926 case "libstdc++":
927 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
928 // tree is in good enough shape to not need it.
929 // Host builds will use GNU libstdc++.
930 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700931 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700932 }
933 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700934 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
935 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700936 case "ndk_libc++_shared", "ndk_libc++_static":
937 // TODO(danalbert): This really shouldn't be here...
938 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
939 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
940 // Nothing
941 case "":
942 // None or error.
943 if ctx.Host() {
944 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
945 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700946 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800947 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700948 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800949 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700950 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700951 }
952 default:
Colin Crossfa138792015-04-24 17:31:52 -0700953 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700954 }
955
956 return flags
957}
958
Colin Crosse11befc2015-04-27 17:49:17 -0700959func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
960 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800961
Colin Crossed4cf0b2015-03-26 14:43:45 -0700962 stl := c.stl(ctx)
963 if ctx.Failed() {
964 return depNames
965 }
966
967 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700968 case "libstdc++":
969 if ctx.Device() {
970 depNames.SharedLibs = append(depNames.SharedLibs, stl)
971 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700972 case "libc++", "libc++_static":
973 if stl == "libc++" {
974 depNames.SharedLibs = append(depNames.SharedLibs, stl)
975 } else {
976 depNames.StaticLibs = append(depNames.StaticLibs, stl)
977 }
978 if ctx.Device() {
979 if ctx.Arch().ArchType == common.Arm {
980 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
981 }
982 if c.staticBinary() {
983 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
984 } else {
985 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
986 }
987 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700988 case "":
989 // None or error.
990 case "ndk_system":
991 // TODO: Make a system STL prebuilt for the NDK.
992 // 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 -0700993 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -0700994 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700995 case "ndk_libc++_shared", "ndk_libstlport_shared":
996 depNames.SharedLibs = append(depNames.SharedLibs, stl)
997 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
998 depNames.StaticLibs = append(depNames.StaticLibs, stl)
999 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001000 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001001 }
1002
Colin Cross74d1ec02015-04-28 13:30:13 -07001003 if ctx.ModuleName() != "libcompiler_rt-extras" {
1004 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1005 }
1006
Colin Crossf6566ed2015-03-24 11:13:38 -07001007 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001008 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001009 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001010 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001011 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1012 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001013
Colin Cross18b6dc52015-04-28 13:20:37 -07001014 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001015 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1016 }
Colin Cross577f6e42015-03-27 18:23:34 -07001017
Colin Crossfa138792015-04-24 17:31:52 -07001018 if c.Properties.Sdk_version != "" {
1019 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001020 depNames.SharedLibs = append(depNames.SharedLibs,
1021 "ndk_libc."+version,
1022 "ndk_libm."+version,
1023 )
1024 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001025 }
1026
Colin Cross21b9a242015-03-24 14:15:58 -07001027 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001028}
1029
Colin Crossed4cf0b2015-03-26 14:43:45 -07001030// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1031type ccLinkedInterface interface {
1032 // Returns true if the build options for the module have selected a static or shared build
1033 buildStatic() bool
1034 buildShared() bool
1035
1036 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001037 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001038
Colin Cross18b6dc52015-04-28 13:20:37 -07001039 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001040 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001041
1042 // Returns whether a module is a static binary
1043 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001044}
1045
1046var _ ccLinkedInterface = (*CCLibrary)(nil)
1047var _ ccLinkedInterface = (*CCBinary)(nil)
1048
Colin Crossfa138792015-04-24 17:31:52 -07001049func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001050 return c.dynamicProperties.VariantIsStatic
1051}
1052
Colin Cross18b6dc52015-04-28 13:20:37 -07001053func (c *CCLinked) staticBinary() bool {
1054 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001055}
1056
Colin Cross18b6dc52015-04-28 13:20:37 -07001057func (c *CCLinked) setStatic(static bool) {
1058 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001059}
1060
Colin Cross28344522015-04-22 13:07:53 -07001061type ccExportedFlagsProducer interface {
1062 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001063}
1064
1065//
1066// Combined static+shared libraries
1067//
1068
Colin Cross7d5136f2015-05-11 13:39:40 -07001069type CCLibraryProperties struct {
1070 BuildStatic bool `blueprint:"mutated"`
1071 BuildShared bool `blueprint:"mutated"`
1072 Static struct {
1073 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001074 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001075 Cflags []string `android:"arch_variant"`
1076 Whole_static_libs []string `android:"arch_variant"`
1077 Static_libs []string `android:"arch_variant"`
1078 Shared_libs []string `android:"arch_variant"`
1079 } `android:"arch_variant"`
1080 Shared struct {
1081 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001082 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001083 Cflags []string `android:"arch_variant"`
1084 Whole_static_libs []string `android:"arch_variant"`
1085 Static_libs []string `android:"arch_variant"`
1086 Shared_libs []string `android:"arch_variant"`
1087 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001088
1089 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001090 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001091 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001092 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001093 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001094 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001095 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001096 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001097}
1098
Colin Cross97ba0732015-03-23 17:50:24 -07001099type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001100 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001101
Colin Cross28344522015-04-22 13:07:53 -07001102 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001103 reuseObjFiles common.Paths
1104 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001105 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001106 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001107 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001108
Colin Cross7d5136f2015-05-11 13:39:40 -07001109 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001110}
1111
Colin Crossed4cf0b2015-03-26 14:43:45 -07001112func (c *CCLibrary) buildStatic() bool {
1113 return c.LibraryProperties.BuildStatic
1114}
1115
1116func (c *CCLibrary) buildShared() bool {
1117 return c.LibraryProperties.BuildShared
1118}
1119
Colin Cross97ba0732015-03-23 17:50:24 -07001120type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001121 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001122 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001123 setReuseFrom(ccLibraryInterface)
1124 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001125 getReuseObjFiles() common.Paths
1126 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001127}
1128
Colin Crossed4cf0b2015-03-26 14:43:45 -07001129var _ ccLibraryInterface = (*CCLibrary)(nil)
1130
Colin Cross97ba0732015-03-23 17:50:24 -07001131func (c *CCLibrary) ccLibrary() *CCLibrary {
1132 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001133}
1134
Colin Cross97ba0732015-03-23 17:50:24 -07001135func NewCCLibrary(library *CCLibrary, module CCModuleType,
1136 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1137
Colin Crossfa138792015-04-24 17:31:52 -07001138 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001139 &library.LibraryProperties)
1140}
1141
1142func CCLibraryFactory() (blueprint.Module, []interface{}) {
1143 module := &CCLibrary{}
1144
1145 module.LibraryProperties.BuildShared = true
1146 module.LibraryProperties.BuildStatic = true
1147
1148 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1149}
1150
Colin Cross0676e2d2015-04-24 17:39:18 -07001151func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001152 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001153 if c.static() {
1154 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1155 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1156 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1157 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001158 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001159 if c.Properties.Sdk_version == "" {
1160 depNames.CrtBegin = "crtbegin_so"
1161 depNames.CrtEnd = "crtend_so"
1162 } else {
1163 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1164 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1165 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001166 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001167 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1168 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1169 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001170 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001171
Dan Willemsen218f6562015-07-08 18:13:11 -07001172 c.systemLibs = c.systemSharedLibs(ctx)
1173
Colin Cross21b9a242015-03-24 14:15:58 -07001174 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001175}
1176
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001177func (c *CCLibrary) outputFile() common.OptionalPath {
1178 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001179}
1180
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001181func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001182 return c.reuseObjFiles
1183}
1184
1185func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1186 c.reuseFrom = reuseFrom
1187}
1188
1189func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1190 return c.reuseFrom
1191}
1192
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001193func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001194 return c.objFiles
1195}
1196
Colin Cross28344522015-04-22 13:07:53 -07001197func (c *CCLibrary) exportedFlags() []string {
1198 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001199}
1200
Colin Cross0676e2d2015-04-24 17:39:18 -07001201func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001202 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001203
Dan Willemsen490fd492015-11-24 17:53:15 -08001204 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1205 // all code is position independent, and then those warnings get promoted to
1206 // errors.
1207 if ctx.HostType() != common.Windows {
1208 flags.CFlags = append(flags.CFlags, "-fPIC")
1209 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001210
Colin Crossd8e780d2015-04-28 17:39:43 -07001211 if c.static() {
1212 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1213 } else {
1214 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1215 }
1216
Colin Cross18b6dc52015-04-28 13:20:37 -07001217 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001218 libName := ctx.ModuleName()
1219 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1220 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001221 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001222 sharedFlag = "-shared"
1223 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001224 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001225 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001226 }
Colin Cross97ba0732015-03-23 17:50:24 -07001227
Colin Cross0af4b842015-04-30 16:36:18 -07001228 if ctx.Darwin() {
1229 flags.LdFlags = append(flags.LdFlags,
1230 "-dynamiclib",
1231 "-single_module",
1232 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001233 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001234 )
1235 } else {
1236 flags.LdFlags = append(flags.LdFlags,
1237 "-Wl,--gc-sections",
1238 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001239 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001240 )
1241 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001242 }
Colin Cross97ba0732015-03-23 17:50:24 -07001243
1244 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001245}
1246
Colin Cross97ba0732015-03-23 17:50:24 -07001247func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001248 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001249
1250 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001251 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001252 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001253
1254 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001255 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001256
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001257 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001258
Colin Cross0af4b842015-04-30 16:36:18 -07001259 if ctx.Darwin() {
1260 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1261 } else {
1262 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1263 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001264
1265 c.objFiles = objFiles
1266 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001267
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001268 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001269 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001270 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001271
1272 ctx.CheckbuildFile(outputFile)
1273}
1274
Colin Cross97ba0732015-03-23 17:50:24 -07001275func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001276 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001277
1278 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001279 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001280 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001281
1282 objFiles = append(objFiles, objFilesShared...)
1283
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001284 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001285
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001286 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001287
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001288 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1289 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1290 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1291 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001292 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001293 if versionScript.Valid() {
1294 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1295 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001296 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001297 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001298 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1299 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001300 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001301 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1302 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001304 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1305 }
1306 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001307 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001308 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1309 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001310 if unexportedSymbols.Valid() {
1311 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1312 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001313 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001314 if forceNotWeakSymbols.Valid() {
1315 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1316 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001317 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001318 if forceWeakSymbols.Valid() {
1319 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1320 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001321 }
Colin Crossaee540a2015-07-06 17:48:31 -07001322 }
1323
Colin Cross97ba0732015-03-23 17:50:24 -07001324 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001325 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001326 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001327
1328 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001329 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001330 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001331 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001332}
1333
Colin Cross97ba0732015-03-23 17:50:24 -07001334func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001335 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001336
1337 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001338 if c.getReuseFrom().ccLibrary() == c {
1339 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001340 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001341 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1342 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001343 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001344 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001345 }
1346
Colin Crossed4cf0b2015-03-26 14:43:45 -07001347 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001348 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1349 } else {
1350 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1351 }
1352}
1353
Colin Cross97ba0732015-03-23 17:50:24 -07001354func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001355 // Static libraries do not get installed.
1356}
1357
Colin Cross97ba0732015-03-23 17:50:24 -07001358func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001359 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001360 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001361 installDir = "lib64"
1362 }
1363
Dan Willemsen782a2d12015-12-21 14:55:28 -08001364 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001365}
1366
Colin Cross97ba0732015-03-23 17:50:24 -07001367func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001368 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001369 c.installStaticLibrary(ctx, flags)
1370 } else {
1371 c.installSharedLibrary(ctx, flags)
1372 }
1373}
1374
Colin Cross3f40fa42015-01-30 17:27:36 -08001375//
1376// Objects (for crt*.o)
1377//
1378
Dan Albertc3144b12015-04-28 18:17:56 -07001379type ccObjectProvider interface {
1380 object() *ccObject
1381}
1382
Colin Cross3f40fa42015-01-30 17:27:36 -08001383type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001384 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001385 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001386}
1387
Dan Albertc3144b12015-04-28 18:17:56 -07001388func (c *ccObject) object() *ccObject {
1389 return c
1390}
1391
Colin Cross97ba0732015-03-23 17:50:24 -07001392func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001393 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001394
Colin Crossfa138792015-04-24 17:31:52 -07001395 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001396}
1397
Colin Cross0676e2d2015-04-24 17:39:18 -07001398func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001399 // object files can't have any dynamic dependencies
1400 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001401}
1402
1403func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001404 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001405
Colin Cross97ba0732015-03-23 17:50:24 -07001406 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001407
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001408 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001409 if len(objFiles) == 1 {
1410 outputFile = objFiles[0]
1411 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001412 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1413 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1414 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001415 }
1416
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001417 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001418
1419 ctx.CheckbuildFile(outputFile)
1420}
1421
Colin Cross97ba0732015-03-23 17:50:24 -07001422func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001423 // Object files do not get installed.
1424}
1425
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001426func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001427 return c.out
1428}
1429
Dan Albertc3144b12015-04-28 18:17:56 -07001430var _ ccObjectProvider = (*ccObject)(nil)
1431
Colin Cross3f40fa42015-01-30 17:27:36 -08001432//
1433// Executables
1434//
1435
Colin Cross7d5136f2015-05-11 13:39:40 -07001436type CCBinaryProperties struct {
1437 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001438 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001439
1440 // set the name of the output
1441 Stem string `android:"arch_variant"`
1442
1443 // append to the name of the output
1444 Suffix string `android:"arch_variant"`
1445
1446 // if set, add an extra objcopy --prefix-symbols= step
1447 Prefix_symbols string
1448}
1449
Colin Cross97ba0732015-03-23 17:50:24 -07001450type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001451 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001452 out common.Path
1453 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001454 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001455}
1456
Colin Crossed4cf0b2015-03-26 14:43:45 -07001457func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001458 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001459}
1460
1461func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001462 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001463}
1464
Colin Cross97ba0732015-03-23 17:50:24 -07001465func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001466 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001467 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001468 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001469 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001470
1471 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001472}
1473
Colin Cross0676e2d2015-04-24 17:39:18 -07001474func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001475 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001476 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001477 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001478 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001479 depNames.CrtBegin = "crtbegin_static"
1480 } else {
1481 depNames.CrtBegin = "crtbegin_dynamic"
1482 }
1483 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001484 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001485 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001486 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1487 } else {
1488 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1489 }
1490 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001491 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001492
Colin Cross06a931b2015-10-28 17:23:31 -07001493 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001494 if c.stl(ctx) == "libc++_static" {
1495 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1496 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001497 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1498 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1499 // move them to the beginning of deps.LateStaticLibs
1500 var groupLibs []string
1501 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1502 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1503 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1504 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001505 }
Colin Cross21b9a242015-03-24 14:15:58 -07001506 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001507}
1508
Colin Cross97ba0732015-03-23 17:50:24 -07001509func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001510 hod common.HostOrDeviceSupported, multilib common.Multilib,
1511 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001512
Colin Cross1f8f2342015-03-26 16:09:47 -07001513 props = append(props, &binary.BinaryProperties)
1514
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001515 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001516}
1517
Colin Cross97ba0732015-03-23 17:50:24 -07001518func CCBinaryFactory() (blueprint.Module, []interface{}) {
1519 module := &CCBinary{}
1520
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001521 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001522}
1523
Colin Cross6362e272015-10-29 15:25:03 -07001524func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001525 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001526 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001527 }
Colin Cross06a931b2015-10-28 17:23:31 -07001528 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001529 c.dynamicProperties.VariantIsStaticBinary = true
1530 }
1531}
1532
Colin Cross0676e2d2015-04-24 17:39:18 -07001533func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001534 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001535
Dan Willemsen490fd492015-11-24 17:53:15 -08001536 if ctx.Host() {
1537 flags.LdFlags = append(flags.LdFlags, "-pie")
1538 if ctx.HostType() == common.Windows {
1539 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1540 }
1541 }
1542
1543 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1544 // all code is position independent, and then those warnings get promoted to
1545 // errors.
1546 if ctx.HostType() != common.Windows {
1547 flags.CFlags = append(flags.CFlags, "-fpie")
1548 }
Colin Cross97ba0732015-03-23 17:50:24 -07001549
Colin Crossf6566ed2015-03-24 11:13:38 -07001550 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001551 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001552 // Clang driver needs -static to create static executable.
1553 // However, bionic/linker uses -shared to overwrite.
1554 // Linker for x86 targets does not allow coexistance of -static and -shared,
1555 // so we add -static only if -shared is not used.
1556 if !inList("-shared", flags.LdFlags) {
1557 flags.LdFlags = append(flags.LdFlags, "-static")
1558 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001559
Colin Crossed4cf0b2015-03-26 14:43:45 -07001560 flags.LdFlags = append(flags.LdFlags,
1561 "-nostdlib",
1562 "-Bstatic",
1563 "-Wl,--gc-sections",
1564 )
1565
1566 } else {
1567 linker := "/system/bin/linker"
1568 if flags.Toolchain.Is64Bit() {
1569 linker = "/system/bin/linker64"
1570 }
1571
1572 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001573 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001574 "-nostdlib",
1575 "-Bdynamic",
1576 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1577 "-Wl,--gc-sections",
1578 "-Wl,-z,nocopyreloc",
1579 )
1580 }
Colin Cross0af4b842015-04-30 16:36:18 -07001581 } else if ctx.Darwin() {
1582 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001583 }
1584
Colin Cross97ba0732015-03-23 17:50:24 -07001585 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001586}
1587
Colin Cross97ba0732015-03-23 17:50:24 -07001588func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001589 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001590
Colin Cross06a931b2015-10-28 17:23:31 -07001591 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001592 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1593 "from static libs or set static_executable: true")
1594 }
1595
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001596 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001597 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001598 if c.BinaryProperties.Prefix_symbols != "" {
1599 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001600 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001601 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1602 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1603 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001604
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001605 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001606
Colin Cross97ba0732015-03-23 17:50:24 -07001607 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001608 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001609 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001610}
Colin Cross3f40fa42015-01-30 17:27:36 -08001611
Colin Cross97ba0732015-03-23 17:50:24 -07001612func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001613 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001614}
1615
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001616func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001617 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001618 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001619 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001620 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001621}
1622
Colin Cross6002e052015-09-16 16:00:08 -07001623func (c *CCBinary) binary() *CCBinary {
1624 return c
1625}
1626
1627type testPerSrc interface {
1628 binary() *CCBinary
1629 testPerSrc() bool
1630}
1631
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001632var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001633
Colin Cross6362e272015-10-29 15:25:03 -07001634func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001635 if test, ok := mctx.Module().(testPerSrc); ok {
1636 if test.testPerSrc() {
1637 testNames := make([]string, len(test.binary().Properties.Srcs))
1638 for i, src := range test.binary().Properties.Srcs {
1639 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1640 }
1641 tests := mctx.CreateLocalVariations(testNames...)
1642 for i, src := range test.binary().Properties.Srcs {
1643 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001644 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001645 }
1646 }
1647 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001648}
1649
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001650type CCTestProperties struct {
1651 // if set, build against the gtest library. Defaults to true.
1652 Gtest bool
1653
1654 // Create a separate binary for each source file. Useful when there is
1655 // global state that can not be torn down and reset between each test suite.
1656 Test_per_src *bool
1657}
1658
Colin Cross9ffb4f52015-04-24 17:48:09 -07001659type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001660 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001661
1662 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001663}
1664
Colin Cross9ffb4f52015-04-24 17:48:09 -07001665func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001666 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001667 if !c.TestProperties.Gtest {
1668 return flags
1669 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001670
Colin Cross97ba0732015-03-23 17:50:24 -07001671 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001672 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001673 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001674
1675 if ctx.HostType() == common.Windows {
1676 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1677 } else {
1678 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1679 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1680 }
1681 } else {
1682 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001683 }
1684
1685 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001686 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001687 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001688
Colin Cross21b9a242015-03-24 14:15:58 -07001689 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001690}
1691
Colin Cross9ffb4f52015-04-24 17:48:09 -07001692func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001693 if c.TestProperties.Gtest {
1694 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1695 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001696 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001697 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001698}
1699
Dan Willemsen782a2d12015-12-21 14:55:28 -08001700func (c *CCTest) InstallInData() bool {
1701 return true
1702}
1703
Colin Cross9ffb4f52015-04-24 17:48:09 -07001704func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001705 installDir := "nativetest"
1706 if flags.Toolchain.Is64Bit() {
1707 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001708 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001709 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1710}
1711
1712func (c *CCTest) testPerSrc() bool {
1713 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001714}
1715
Colin Cross9ffb4f52015-04-24 17:48:09 -07001716func NewCCTest(test *CCTest, module CCModuleType,
1717 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1718
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001719 props = append(props, &test.TestProperties)
1720
1721 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001722}
1723
1724func CCTestFactory() (blueprint.Module, []interface{}) {
1725 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001726 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001727
1728 return NewCCTest(module, module, common.HostAndDeviceSupported)
1729}
1730
Colin Cross2ba19d92015-05-07 15:44:20 -07001731type CCBenchmark struct {
1732 CCBinary
1733}
1734
1735func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1736 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001737 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001738 return depNames
1739}
1740
Dan Willemsen782a2d12015-12-21 14:55:28 -08001741func (c *CCBenchmark) InstallInData() bool {
1742 return true
1743}
1744
Colin Cross2ba19d92015-05-07 15:44:20 -07001745func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1746 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001747 installDir := "nativetest"
1748 if flags.Toolchain.Is64Bit() {
1749 installDir = "nativetest64"
1750 }
1751 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001752 } else {
1753 c.CCBinary.installModule(ctx, flags)
1754 }
1755}
1756
1757func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1758 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1759
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001760 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001761}
1762
1763func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1764 module := &CCBenchmark{}
1765
1766 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1767}
1768
Colin Cross3f40fa42015-01-30 17:27:36 -08001769//
1770// Static library
1771//
1772
Colin Cross97ba0732015-03-23 17:50:24 -07001773func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1774 module := &CCLibrary{}
1775 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001776
Colin Cross97ba0732015-03-23 17:50:24 -07001777 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001778}
1779
1780//
1781// Shared libraries
1782//
1783
Colin Cross97ba0732015-03-23 17:50:24 -07001784func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1785 module := &CCLibrary{}
1786 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001787
Colin Cross97ba0732015-03-23 17:50:24 -07001788 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001789}
1790
1791//
1792// Host static library
1793//
1794
Colin Cross97ba0732015-03-23 17:50:24 -07001795func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1796 module := &CCLibrary{}
1797 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001798
Colin Cross97ba0732015-03-23 17:50:24 -07001799 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001800}
1801
1802//
1803// Host Shared libraries
1804//
1805
Colin Cross97ba0732015-03-23 17:50:24 -07001806func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1807 module := &CCLibrary{}
1808 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001809
Colin Cross97ba0732015-03-23 17:50:24 -07001810 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001811}
1812
1813//
1814// Host Binaries
1815//
1816
Colin Cross97ba0732015-03-23 17:50:24 -07001817func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1818 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001819
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001820 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001821}
1822
1823//
Colin Cross1f8f2342015-03-26 16:09:47 -07001824// Host Tests
1825//
1826
1827func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001828 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001829 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001830}
1831
1832//
Colin Cross2ba19d92015-05-07 15:44:20 -07001833// Host Benchmarks
1834//
1835
1836func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1837 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001838 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001839}
1840
1841//
Colin Crosscfad1192015-11-02 16:43:11 -08001842// Defaults
1843//
1844type CCDefaults struct {
1845 common.AndroidModuleBase
1846 common.DefaultsModule
1847}
1848
1849func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1850}
1851
1852func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1853 module := &CCDefaults{}
1854
1855 propertyStructs := []interface{}{
1856 &CCBaseProperties{},
1857 &CCLibraryProperties{},
1858 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001859 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001860 &CCUnusedProperties{},
1861 }
1862
Dan Willemsen218f6562015-07-08 18:13:11 -07001863 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1864 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001865
1866 return common.InitDefaultsModule(module, module, propertyStructs...)
1867}
1868
1869//
Colin Cross3f40fa42015-01-30 17:27:36 -08001870// Device libraries shipped with gcc
1871//
1872
1873type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001874 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001875}
1876
Colin Cross0676e2d2015-04-24 17:39:18 -07001877func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001878 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001879 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001880}
1881
Colin Cross97ba0732015-03-23 17:50:24 -07001882func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001883 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001884
Colin Cross97ba0732015-03-23 17:50:24 -07001885 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001886 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001887
Colin Crossfa138792015-04-24 17:31:52 -07001888 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001889 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001890}
1891
1892func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001893 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001894
1895 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001896 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001897
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001898 if flags.Clang {
1899 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1900 }
1901
Colin Cross3f40fa42015-01-30 17:27:36 -08001902 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1903
1904 c.out = outputFile
1905
1906 ctx.CheckbuildFile(outputFile)
1907}
1908
Colin Cross97ba0732015-03-23 17:50:24 -07001909func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001910 // Toolchain libraries do not get installed.
1911}
1912
Dan Albertbe961682015-03-18 23:38:50 -07001913// NDK prebuilt libraries.
1914//
1915// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1916// either (with the exception of the shared STLs, which are installed to the app's directory rather
1917// than to the system image).
1918
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001919func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1920 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1921 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001922}
1923
Dan Albertc3144b12015-04-28 18:17:56 -07001924func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001925 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001926
1927 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1928 // We want to translate to just NAME.EXT
1929 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1930 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001931 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001932}
1933
1934type ndkPrebuiltObject struct {
1935 ccObject
1936}
1937
Dan Albertc3144b12015-04-28 18:17:56 -07001938func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1939 // NDK objects can't have any dependencies
1940 return CCDeps{}
1941}
1942
1943func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1944 module := &ndkPrebuiltObject{}
1945 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1946}
1947
1948func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001949 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001950 // A null build step, but it sets up the output path.
1951 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1952 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1953 }
1954
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001955 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001956}
1957
1958func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1959 // Objects do not get installed.
1960}
1961
1962var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1963
Dan Albertbe961682015-03-18 23:38:50 -07001964type ndkPrebuiltLibrary struct {
1965 CCLibrary
1966}
1967
Colin Cross0676e2d2015-04-24 17:39:18 -07001968func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001969 // NDK libraries can't have any dependencies
1970 return CCDeps{}
1971}
1972
1973func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1974 module := &ndkPrebuiltLibrary{}
1975 module.LibraryProperties.BuildShared = true
1976 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1977}
1978
1979func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001980 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001981 // A null build step, but it sets up the output path.
1982 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
1983 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
1984 }
1985
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001986 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
1987 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07001988
Dan Willemsen490fd492015-11-24 17:53:15 -08001989 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07001990 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07001991}
1992
1993func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07001994 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07001995}
1996
1997// The NDK STLs are slightly different from the prebuilt system libraries:
1998// * Are not specific to each platform version.
1999// * The libraries are not in a predictable location for each STL.
2000
2001type ndkPrebuiltStl struct {
2002 ndkPrebuiltLibrary
2003}
2004
2005type ndkPrebuiltStaticStl struct {
2006 ndkPrebuiltStl
2007}
2008
2009type ndkPrebuiltSharedStl struct {
2010 ndkPrebuiltStl
2011}
2012
2013func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2014 module := &ndkPrebuiltSharedStl{}
2015 module.LibraryProperties.BuildShared = true
2016 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2017}
2018
2019func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2020 module := &ndkPrebuiltStaticStl{}
2021 module.LibraryProperties.BuildStatic = true
2022 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2023}
2024
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002025func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002026 gccVersion := toolchain.GccVersion()
2027 var libDir string
2028 switch stl {
2029 case "libstlport":
2030 libDir = "cxx-stl/stlport/libs"
2031 case "libc++":
2032 libDir = "cxx-stl/llvm-libc++/libs"
2033 case "libgnustl":
2034 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2035 }
2036
2037 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002038 ndkSrcRoot := "prebuilts/ndk/current/sources"
2039 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002040 }
2041
2042 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002043 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002044}
2045
2046func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002047 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002048 // A null build step, but it sets up the output path.
2049 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2050 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2051 }
2052
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002053 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002054 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002055
2056 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002057 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002058 if c.LibraryProperties.BuildStatic {
2059 libExt = staticLibraryExtension
2060 }
2061
2062 stlName := strings.TrimSuffix(libName, "_shared")
2063 stlName = strings.TrimSuffix(stlName, "_static")
2064 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002065 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002066}
2067
Colin Cross6362e272015-10-29 15:25:03 -07002068func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002069 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002070 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002071 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002072 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002073 modules[0].(ccLinkedInterface).setStatic(true)
2074 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002075 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002076 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002077 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002078 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002079 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002080 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002081 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002082 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002083 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002084
2085 if _, ok := c.(ccLibraryInterface); ok {
2086 reuseFrom := modules[0].(ccLibraryInterface)
2087 for _, m := range modules {
2088 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002089 }
2090 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002091 }
2092}
Colin Cross74d1ec02015-04-28 13:30:13 -07002093
2094// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2095// modifies the slice contents in place, and returns a subslice of the original slice
2096func lastUniqueElements(list []string) []string {
2097 totalSkip := 0
2098 for i := len(list) - 1; i >= totalSkip; i-- {
2099 skip := 0
2100 for j := i - 1; j >= totalSkip; j-- {
2101 if list[i] == list[j] {
2102 skip++
2103 } else {
2104 list[j+skip] = list[j]
2105 }
2106 }
2107 totalSkip += skip
2108 }
2109 return list[totalSkip:]
2110}
Colin Cross06a931b2015-10-28 17:23:31 -07002111
2112var Bool = proptools.Bool