blob: cf4bae3eb5118a60f462baf5238b6c2d02a26ac8 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
35 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
37 soong.RegisterModuleType("cc_library", CCLibraryFactory)
38 soong.RegisterModuleType("cc_object", CCObjectFactory)
39 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
40 soong.RegisterModuleType("cc_test", CCTestFactory)
41 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080042 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
44 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080083
84 // COMMON_RELEASE_CFLAGS
85 "-DNDEBUG",
86 "-UDEBUG",
87 }
88
89 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080090 "-fdiagnostics-color",
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092 // TARGET_ERROR_FLAGS
93 "-Werror=return-type",
94 "-Werror=non-virtual-dtor",
95 "-Werror=address",
96 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080097 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080098 }
99
100 hostGlobalCflags = []string{}
101
102 commonGlobalCppflags = []string{
103 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700104 }
105
106 illegalFlags = []string{
107 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800108 }
109)
110
111func init() {
112 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
113 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
114 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
115
116 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
117
118 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800119 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800120 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800121 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800122 pctx.StaticVariable("hostClangGlobalCflags",
123 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Tim Kilbournf2948142015-03-11 12:03:03 -0700124 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800125 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800126
127 // Everything in this list is a crime against abstraction and dependency tracking.
128 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800129 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700130 []string{
131 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800132 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700133 "hardware/libhardware/include",
134 "hardware/libhardware_legacy/include",
135 "hardware/ril/include",
136 "libnativehelper/include",
137 "frameworks/native/include",
138 "frameworks/native/opengl/include",
139 "frameworks/av/include",
140 "frameworks/base/include",
141 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800142 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
143 // with this, since there is no associated library.
144 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
145 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800146
Dan Willemsen767078e2016-03-01 13:42:19 -0800147 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2629532/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800148}
149
Colin Cross6362e272015-10-29 15:25:03 -0700150type CCModuleContext common.AndroidBaseContext
151
Colin Cross3f40fa42015-01-30 17:27:36 -0800152// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700153type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800154 common.AndroidModule
155
Colin Crossfa138792015-04-24 17:31:52 -0700156 // Modify property values after parsing Blueprints file but before starting dependency
157 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700158 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700159
Colin Cross21b9a242015-03-24 14:15:58 -0700160 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700161 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800162
Colin Cross6362e272015-10-29 15:25:03 -0700163 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700164 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800165
Colin Cross6362e272015-10-29 15:25:03 -0700166 // Add dynamic dependencies
167 depsMutator(common.AndroidBottomUpMutatorContext)
168
Colin Cross3f40fa42015-01-30 17:27:36 -0800169 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800171
Dan Albertc403f7c2015-03-18 14:01:18 -0700172 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700173 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700174
Colin Cross3f40fa42015-01-30 17:27:36 -0800175 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700176 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800177}
178
Colin Cross97ba0732015-03-23 17:50:24 -0700179type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700180 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700181
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700182 ObjFiles common.Paths
183
184 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700185
Colin Cross97ba0732015-03-23 17:50:24 -0700186 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700187}
188
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189type CCPathDeps struct {
190 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
191
192 ObjFiles common.Paths
193 WholeStaticLibObjFiles common.Paths
194
195 Cflags, ReexportedCflags []string
196
197 CrtBegin, CrtEnd common.OptionalPath
198}
199
Colin Cross97ba0732015-03-23 17:50:24 -0700200type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700201 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
202 AsFlags []string // Flags that apply to assembly source files
203 CFlags []string // Flags that apply to C and C++ source files
204 ConlyFlags []string // Flags that apply to C source files
205 CppFlags []string // Flags that apply to C++ source files
206 YaccFlags []string // Flags that apply to Yacc source files
207 LdFlags []string // Flags that apply to linker command lines
208
209 Nocrt bool
210 Toolchain Toolchain
211 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700212}
213
Colin Cross7d5136f2015-05-11 13:39:40 -0700214// Properties used to compile all C or C++ modules
215type CCBaseProperties struct {
216 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700217 Srcs []string `android:"arch_variant"`
218
219 // list of source files that should not be used to build the C/C++ module.
220 // This is most useful in the arch/multilib variants to remove non-common files
221 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700222
223 // list of module-specific flags that will be used for C and C++ compiles.
224 Cflags []string `android:"arch_variant"`
225
226 // list of module-specific flags that will be used for C++ compiles
227 Cppflags []string `android:"arch_variant"`
228
229 // list of module-specific flags that will be used for C compiles
230 Conlyflags []string `android:"arch_variant"`
231
232 // list of module-specific flags that will be used for .S compiles
233 Asflags []string `android:"arch_variant"`
234
235 // list of module-specific flags that will be used for .y and .yy compiles
236 Yaccflags []string
237
238 // list of module-specific flags that will be used for all link steps
239 Ldflags []string `android:"arch_variant"`
240
241 // the instruction set architecture to use to compile the C/C++
242 // module.
243 Instruction_set string `android:"arch_variant"`
244
245 // list of directories relative to the root of the source tree that will
246 // be added to the include path using -I.
247 // If possible, don't use this. If adding paths from the current directory use
248 // local_include_dirs, if adding paths from other modules use export_include_dirs in
249 // that module.
250 Include_dirs []string `android:"arch_variant"`
251
Colin Cross39d97f22015-09-14 12:30:50 -0700252 // list of files relative to the root of the source tree that will be included
253 // using -include.
254 // If possible, don't use this.
255 Include_files []string `android:"arch_variant"`
256
Colin Cross7d5136f2015-05-11 13:39:40 -0700257 // list of directories relative to the Blueprints file that will
258 // be added to the include path using -I
259 Local_include_dirs []string `android:"arch_variant"`
260
Colin Cross39d97f22015-09-14 12:30:50 -0700261 // list of files relative to the Blueprints file that will be included
262 // using -include.
263 // If possible, don't use this.
264 Local_include_files []string `android:"arch_variant"`
265
Colin Cross7d5136f2015-05-11 13:39:40 -0700266 // list of directories relative to the Blueprints file that will
267 // be added to the include path using -I for any module that links against this module
268 Export_include_dirs []string `android:"arch_variant"`
269
270 // list of module-specific flags that will be used for C and C++ compiles when
271 // compiling with clang
272 Clang_cflags []string `android:"arch_variant"`
273
274 // list of module-specific flags that will be used for .S compiles when
275 // compiling with clang
276 Clang_asflags []string `android:"arch_variant"`
277
278 // list of system libraries that will be dynamically linked to
279 // shared library and executable modules. If unset, generally defaults to libc
280 // and libm. Set to [] to prevent linking against libc and libm.
281 System_shared_libs []string
282
283 // list of modules whose object files should be linked into this module
284 // in their entirety. For static library modules, all of the .o files from the intermediate
285 // directory of the dependency will be linked into this modules .a file. For a shared library,
286 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
287 Whole_static_libs []string `android:"arch_variant"`
288
289 // list of modules that should be statically linked into this module.
290 Static_libs []string `android:"arch_variant"`
291
292 // list of modules that should be dynamically linked into this module.
293 Shared_libs []string `android:"arch_variant"`
294
295 // allow the module to contain undefined symbols. By default,
296 // modules cannot contain undefined symbols that are not satisified by their immediate
297 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
298 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700299 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700300
301 // don't link in crt_begin and crt_end. This flag should only be necessary for
302 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700303 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700304
Dan Willemsend67be222015-09-16 15:19:33 -0700305 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700306 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700307
Colin Cross7d5136f2015-05-11 13:39:40 -0700308 // don't insert default compiler flags into asflags, cflags,
309 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700310 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700311
312 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700313 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700314
315 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700316 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700317
318 // -l arguments to pass to linker for host-provided shared libraries
319 Host_ldlibs []string `android:"arch_variant"`
320
321 // select the STL library to use. Possible values are "libc++", "libc++_static",
322 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
323 // default
324 Stl string
325
326 // Set for combined shared/static libraries to prevent compiling object files a second time
327 SkipCompileObjs bool `blueprint:"mutated"`
328
329 Debug, Release struct {
330 // list of module-specific flags that will be used for C and C++ compiles in debug or
331 // release builds
332 Cflags []string `android:"arch_variant"`
333 } `android:"arch_variant"`
334
335 // Minimum sdk version supported when compiling against the ndk
336 Sdk_version string
337
338 // install to a subdirectory of the default install path for the module
339 Relative_install_path string
340}
341
Colin Crosscfad1192015-11-02 16:43:11 -0800342type CCUnusedProperties struct {
343 Native_coverage *bool
344 Required []string
345 Sanitize []string `android:"arch_variant"`
346 Sanitize_recover []string
347 Strip string
348 Tags []string
349}
350
Colin Crossfa138792015-04-24 17:31:52 -0700351// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700352// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
353// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700354type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700355 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800356 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700357 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700358
Colin Cross7d5136f2015-05-11 13:39:40 -0700359 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700360
Colin Crosscfad1192015-11-02 16:43:11 -0800361 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700362
363 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700364
365 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700366}
367
Colin Crossfa138792015-04-24 17:31:52 -0700368func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700369 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
370
371 base.module = module
372
Colin Crossfa138792015-04-24 17:31:52 -0700373 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700374
Colin Crosscfad1192015-11-02 16:43:11 -0800375 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
376
377 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700378}
379
Colin Crossfa138792015-04-24 17:31:52 -0700380func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800381 toolchain := c.findToolchain(ctx)
382 if ctx.Failed() {
383 return
384 }
385
Colin Cross21b9a242015-03-24 14:15:58 -0700386 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800387 if ctx.Failed() {
388 return
389 }
390
Colin Cross74d1ec02015-04-28 13:30:13 -0700391 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800392 if ctx.Failed() {
393 return
394 }
395
Colin Cross28344522015-04-22 13:07:53 -0700396 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700397
Colin Cross581c1892015-04-07 16:50:10 -0700398 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800399 if ctx.Failed() {
400 return
401 }
402
Colin Cross581c1892015-04-07 16:50:10 -0700403 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700404 if ctx.Failed() {
405 return
406 }
407
408 objFiles = append(objFiles, generatedObjFiles...)
409
Colin Cross3f40fa42015-01-30 17:27:36 -0800410 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
411 if ctx.Failed() {
412 return
413 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700414
415 c.ccModuleType().installModule(ctx, flags)
416 if ctx.Failed() {
417 return
418 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800419}
420
Colin Crossfa138792015-04-24 17:31:52 -0700421func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800422 return c.module
423}
424
Colin Crossfa138792015-04-24 17:31:52 -0700425func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800426 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700427 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800428 ht := ctx.HostType()
429 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800430 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800431 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800432 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800433 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800434 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800435}
436
Colin Cross6362e272015-10-29 15:25:03 -0700437func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700438}
439
Colin Crosse11befc2015-04-27 17:49:17 -0700440func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700441 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
442 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
443 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700444
Colin Cross21b9a242015-03-24 14:15:58 -0700445 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800446}
447
Colin Cross6362e272015-10-29 15:25:03 -0700448func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700449 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
450 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
451 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
452 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
453
454 staticLibs := c.savedDepNames.WholeStaticLibs
455 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
456 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700457 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800458
Colin Cross74d1ec02015-04-28 13:30:13 -0700459 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700460
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700461 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700462 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700463 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700464 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700465 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700466 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700467 }
Colin Cross6362e272015-10-29 15:25:03 -0700468}
Colin Cross21b9a242015-03-24 14:15:58 -0700469
Colin Cross6362e272015-10-29 15:25:03 -0700470func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
471 if c, ok := ctx.Module().(CCModuleType); ok {
472 c.ModifyProperties(ctx)
473 c.depsMutator(ctx)
474 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800475}
476
477// Create a ccFlags struct that collects the compile flags from global values,
478// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700479func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700480 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700481 CFlags: c.Properties.Cflags,
482 CppFlags: c.Properties.Cppflags,
483 ConlyFlags: c.Properties.Conlyflags,
484 LdFlags: c.Properties.Ldflags,
485 AsFlags: c.Properties.Asflags,
486 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700487 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700488 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700489 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800490 }
Colin Cross28344522015-04-22 13:07:53 -0700491
492 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700493 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
494 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700495 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700496 includeDirsToFlags(localIncludeDirs),
497 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700498
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700499 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
500 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700501
502 flags.GlobalFlags = append(flags.GlobalFlags,
503 includeFilesToFlags(rootIncludeFiles),
504 includeFilesToFlags(localIncludeFiles))
505
Colin Cross06a931b2015-10-28 17:23:31 -0700506 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700507 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700508 flags.GlobalFlags = append(flags.GlobalFlags,
509 "${commonGlobalIncludes}",
510 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800511 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700512 }
513
514 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700515 "-I" + common.PathForModuleSrc(ctx).String(),
516 "-I" + common.PathForModuleOut(ctx).String(),
517 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700518 }...)
519 }
520
Colin Cross06a931b2015-10-28 17:23:31 -0700521 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700522 if ctx.Host() {
523 flags.Clang = true
524 }
525
526 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
527 flags.Clang = true
528 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800529 }
530
Dan Willemsen490fd492015-11-24 17:53:15 -0800531 if !toolchain.ClangSupported() {
532 flags.Clang = false
533 }
534
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800535 instructionSet := c.Properties.Instruction_set
536 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
537 if flags.Clang {
538 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
539 }
540 if err != nil {
541 ctx.ModuleErrorf("%s", err)
542 }
543
544 // TODO: debug
545 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
546
Colin Cross97ba0732015-03-23 17:50:24 -0700547 if flags.Clang {
548 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700549 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
550 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700551 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
552 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
553 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800554
555 target := "-target " + toolchain.ClangTriple()
556 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
557
Colin Cross97ba0732015-03-23 17:50:24 -0700558 flags.CFlags = append(flags.CFlags, target, gccPrefix)
559 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
560 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800561 }
562
Colin Cross06a931b2015-10-28 17:23:31 -0700563 if !Bool(c.Properties.No_default_compiler_flags) {
564 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700565 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800566 }
567
Colin Cross56b4d452015-04-21 17:38:44 -0700568 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
569
Colin Cross97ba0732015-03-23 17:50:24 -0700570 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800571 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700572 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700573 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800574 toolchain.ClangCflags(),
575 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700576 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800577
578 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800579 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700580 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700581 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800582 toolchain.Cflags(),
583 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700584 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800585 }
586
Colin Cross7b66f152015-12-15 16:07:43 -0800587 if Bool(ctx.AConfig().ProductVariables.Brillo) {
588 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
589 }
590
Colin Crossf6566ed2015-03-24 11:13:38 -0700591 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700592 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700593 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800594 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700595 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800596 }
597 }
598
Colin Cross97ba0732015-03-23 17:50:24 -0700599 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800600
Colin Cross97ba0732015-03-23 17:50:24 -0700601 if flags.Clang {
602 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
603 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800604 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700605 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
606 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800607 }
Colin Cross28344522015-04-22 13:07:53 -0700608
609 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700610 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700611 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800612 }
613
Colin Crossc4bde762015-11-23 16:11:30 -0800614 if flags.Clang {
615 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
616 } else {
617 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800618 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800619 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800620
Colin Cross0676e2d2015-04-24 17:39:18 -0700621 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800622
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700623 if c.Properties.Sdk_version == "" {
624 if ctx.Host() && !flags.Clang {
625 // The host GCC doesn't support C++14 (and is deprecated, so likely
626 // never will). Build these modules with C++11.
627 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
628 } else {
629 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
630 }
631 }
632
633 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
634 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
635 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
636
Dan Willemsen52b1cd22016-03-01 13:36:34 -0800637 // We can enforce some rules more strictly in the code we own. strict
638 // indicates if this is code that we can be stricter with. If we have
639 // rules that we want to apply to *our* code (but maybe can't for
640 // vendor/device specific things), we could extend this to be a ternary
641 // value.
642 strict := true
643 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
644 strict = false
645 }
646
647 // Can be used to make some annotations stricter for code we can fix
648 // (such as when we mark functions as deprecated).
649 if strict {
650 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
651 }
652
Colin Cross3f40fa42015-01-30 17:27:36 -0800653 // Optimization to reduce size of build.ninja
654 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700655 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
656 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
657 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
658 flags.CFlags = []string{"$cflags"}
659 flags.CppFlags = []string{"$cppflags"}
660 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800661
662 return flags
663}
664
Colin Cross0676e2d2015-04-24 17:39:18 -0700665func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800666 return flags
667}
668
669// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700670func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700671 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700672
673 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800674
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700675 inputFiles := ctx.ExpandSources(srcFiles, excludes)
676 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800677
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700678 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800679}
680
Colin Crossfa138792015-04-24 17:31:52 -0700681// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700682func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800683
Colin Crossfa138792015-04-24 17:31:52 -0700684 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800685 return nil
686 }
687
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700688 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800689}
690
Colin Cross5049f022015-03-18 13:28:46 -0700691// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700692func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
693 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700694
Colin Crossfa138792015-04-24 17:31:52 -0700695 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700696 return nil
697 }
698
699 ctx.VisitDirectDeps(func(module blueprint.Module) {
700 if gen, ok := module.(genrule.SourceFileGenerator); ok {
701 srcs = append(srcs, gen.GeneratedSourceFiles()...)
702 }
703 })
704
705 if len(srcs) == 0 {
706 return nil
707 }
708
Colin Cross581c1892015-04-07 16:50:10 -0700709 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700710}
711
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700712func (c *CCBase) outputFile() common.OptionalPath {
713 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800714}
715
Colin Crossfa138792015-04-24 17:31:52 -0700716func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800717 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700718 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800719
720 for _, n := range names {
721 found := false
722 ctx.VisitDirectDeps(func(m blueprint.Module) {
723 otherName := ctx.OtherModuleName(m)
724 if otherName != n {
725 return
726 }
727
Colin Cross97ba0732015-03-23 17:50:24 -0700728 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800729 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800730 // If a cc_library host+device module depends on a library that exists as both
731 // cc_library_shared and cc_library_host_shared, it will end up with two
732 // dependencies with the same name, one of which is marked disabled for each
733 // of host and device. Ignore the disabled one.
734 return
735 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700736 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800737 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
738 otherName)
739 return
740 }
741
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700742 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800743 if found {
744 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
745 return
746 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700747 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800748 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700749 if i, ok := a.(ccExportedFlagsProducer); ok {
750 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800751 }
752 found = true
753 } else {
754 ctx.ModuleErrorf("module %q missing output file", otherName)
755 return
756 }
757 } else {
758 ctx.ModuleErrorf("module %q not an android module", otherName)
759 return
760 }
761 })
Colin Cross6ff51382015-12-17 16:39:19 -0800762 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800763 ctx.ModuleErrorf("unsatisified dependency on %q", n)
764 }
765 }
766
Colin Cross28344522015-04-22 13:07:53 -0700767 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800768}
769
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700770// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700771// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700772func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
773 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700774 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800775
Colin Cross21b9a242015-03-24 14:15:58 -0700776 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800777
Colin Cross28344522015-04-22 13:07:53 -0700778 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700779 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700780 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800781 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800782
Colin Cross21b9a242015-03-24 14:15:58 -0700783 for _, m := range wholeStaticLibModules {
784 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
785 depPaths.WholeStaticLibObjFiles =
786 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
787 } else {
788 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
789 }
790 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800791
Colin Cross28344522015-04-22 13:07:53 -0700792 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
793 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700794
Colin Cross28344522015-04-22 13:07:53 -0700795 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
796 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700797
Colin Cross28344522015-04-22 13:07:53 -0700798 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
799 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700800
801 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700802 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700803 otherName := ctx.OtherModuleName(m)
804 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700805 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700806 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700807 }
808 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700809 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700810 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700811 }
812 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700813 output := obj.object().outputFile()
814 if output.Valid() {
815 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
816 } else {
817 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
818 }
Colin Cross21b9a242015-03-24 14:15:58 -0700819 }
820 }
821 })
822
823 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800824}
825
Colin Cross7d5136f2015-05-11 13:39:40 -0700826type ccLinkedProperties struct {
827 VariantIsShared bool `blueprint:"mutated"`
828 VariantIsStatic bool `blueprint:"mutated"`
829 VariantIsStaticBinary bool `blueprint:"mutated"`
830}
831
Colin Crossfa138792015-04-24 17:31:52 -0700832// CCLinked contains the properties and members used by libraries and executables
833type CCLinked struct {
834 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700835 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800836}
837
Colin Crossfa138792015-04-24 17:31:52 -0700838func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700839 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
840
Colin Crossed4cf0b2015-03-26 14:43:45 -0700841 props = append(props, &dynamic.dynamicProperties)
842
Colin Crossfa138792015-04-24 17:31:52 -0700843 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700844}
845
Colin Crossfa138792015-04-24 17:31:52 -0700846func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700847 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700848 return c.Properties.System_shared_libs
849 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700850 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700851 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700852 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800853 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800854}
855
Colin Crossfa138792015-04-24 17:31:52 -0700856func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
857 if c.Properties.Sdk_version != "" && ctx.Device() {
858 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700859 case "":
860 return "ndk_system"
861 case "c++_shared", "c++_static",
862 "stlport_shared", "stlport_static",
863 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700864 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700865 default:
Colin Crossfa138792015-04-24 17:31:52 -0700866 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700867 return ""
868 }
869 }
870
Dan Willemsen490fd492015-11-24 17:53:15 -0800871 if ctx.HostType() == common.Windows {
872 switch c.Properties.Stl {
873 case "libc++", "libc++_static", "libstdc++", "":
874 // libc++ is not supported on mingw
875 return "libstdc++"
876 case "none":
877 return ""
878 default:
879 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
880 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700881 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800882 } else {
883 switch c.Properties.Stl {
884 case "libc++", "libc++_static",
885 "libstdc++":
886 return c.Properties.Stl
887 case "none":
888 return ""
889 case "":
890 if c.static() {
891 return "libc++_static"
892 } else {
893 return "libc++"
894 }
895 default:
896 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
897 return ""
898 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700899 }
900}
901
Dan Willemsen490fd492015-11-24 17:53:15 -0800902var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700903
904func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800905 hostDynamicGccLibs = map[common.HostType][]string{
906 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
907 common.Darwin: []string{"-lc", "-lSystem"},
908 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
909 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
910 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
911 "-lmsvcrt"},
912 }
913 hostStaticGccLibs = map[common.HostType][]string{
914 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
915 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
916 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700917 }
918}
Colin Cross712fc022015-04-27 11:13:34 -0700919
Colin Crosse11befc2015-04-27 17:49:17 -0700920func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700921 stl := c.stl(ctx)
922 if ctx.Failed() {
923 return flags
924 }
925
926 switch stl {
927 case "libc++", "libc++_static":
928 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700929 if ctx.Host() {
930 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
931 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700932 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700933 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800934 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700935 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800936 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700937 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700938 } else {
939 if ctx.Arch().ArchType == common.Arm {
940 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
941 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700942 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700943 case "libstdc++":
944 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
945 // tree is in good enough shape to not need it.
946 // Host builds will use GNU libstdc++.
947 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700948 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700949 }
950 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700951 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
952 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700953 case "ndk_libc++_shared", "ndk_libc++_static":
954 // TODO(danalbert): This really shouldn't be here...
955 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
956 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
957 // Nothing
958 case "":
959 // None or error.
960 if ctx.Host() {
961 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
962 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700963 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800964 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700965 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800966 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700967 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700968 }
969 default:
Colin Crossfa138792015-04-24 17:31:52 -0700970 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700971 }
972
973 return flags
974}
975
Colin Crosse11befc2015-04-27 17:49:17 -0700976func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
977 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800978
Colin Crossed4cf0b2015-03-26 14:43:45 -0700979 stl := c.stl(ctx)
980 if ctx.Failed() {
981 return depNames
982 }
983
984 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700985 case "libstdc++":
986 if ctx.Device() {
987 depNames.SharedLibs = append(depNames.SharedLibs, stl)
988 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700989 case "libc++", "libc++_static":
990 if stl == "libc++" {
991 depNames.SharedLibs = append(depNames.SharedLibs, stl)
992 } else {
993 depNames.StaticLibs = append(depNames.StaticLibs, stl)
994 }
995 if ctx.Device() {
996 if ctx.Arch().ArchType == common.Arm {
997 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
998 }
999 if c.staticBinary() {
1000 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
1001 } else {
1002 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
1003 }
1004 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001005 case "":
1006 // None or error.
1007 case "ndk_system":
1008 // TODO: Make a system STL prebuilt for the NDK.
1009 // 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 -07001010 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -07001011 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001012 case "ndk_libc++_shared", "ndk_libstlport_shared":
1013 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1014 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
1015 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1016 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001017 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001018 }
1019
Colin Cross74d1ec02015-04-28 13:30:13 -07001020 if ctx.ModuleName() != "libcompiler_rt-extras" {
1021 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1022 }
1023
Colin Crossf6566ed2015-03-24 11:13:38 -07001024 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001025 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001026 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001027 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001028 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1029 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001030
Colin Cross18b6dc52015-04-28 13:20:37 -07001031 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001032 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1033 }
Colin Cross577f6e42015-03-27 18:23:34 -07001034
Colin Crossfa138792015-04-24 17:31:52 -07001035 if c.Properties.Sdk_version != "" {
1036 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001037 depNames.SharedLibs = append(depNames.SharedLibs,
1038 "ndk_libc."+version,
1039 "ndk_libm."+version,
1040 )
1041 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001042 }
1043
Colin Cross21b9a242015-03-24 14:15:58 -07001044 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001045}
1046
Colin Crossed4cf0b2015-03-26 14:43:45 -07001047// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1048type ccLinkedInterface interface {
1049 // Returns true if the build options for the module have selected a static or shared build
1050 buildStatic() bool
1051 buildShared() bool
1052
1053 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001054 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001055
Colin Cross18b6dc52015-04-28 13:20:37 -07001056 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001057 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001058
1059 // Returns whether a module is a static binary
1060 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001061}
1062
1063var _ ccLinkedInterface = (*CCLibrary)(nil)
1064var _ ccLinkedInterface = (*CCBinary)(nil)
1065
Colin Crossfa138792015-04-24 17:31:52 -07001066func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001067 return c.dynamicProperties.VariantIsStatic
1068}
1069
Colin Cross18b6dc52015-04-28 13:20:37 -07001070func (c *CCLinked) staticBinary() bool {
1071 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001072}
1073
Colin Cross18b6dc52015-04-28 13:20:37 -07001074func (c *CCLinked) setStatic(static bool) {
1075 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001076}
1077
Colin Cross28344522015-04-22 13:07:53 -07001078type ccExportedFlagsProducer interface {
1079 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001080}
1081
1082//
1083// Combined static+shared libraries
1084//
1085
Colin Cross7d5136f2015-05-11 13:39:40 -07001086type CCLibraryProperties struct {
1087 BuildStatic bool `blueprint:"mutated"`
1088 BuildShared bool `blueprint:"mutated"`
1089 Static struct {
1090 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001091 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001092 Cflags []string `android:"arch_variant"`
1093 Whole_static_libs []string `android:"arch_variant"`
1094 Static_libs []string `android:"arch_variant"`
1095 Shared_libs []string `android:"arch_variant"`
1096 } `android:"arch_variant"`
1097 Shared struct {
1098 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001099 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001100 Cflags []string `android:"arch_variant"`
1101 Whole_static_libs []string `android:"arch_variant"`
1102 Static_libs []string `android:"arch_variant"`
1103 Shared_libs []string `android:"arch_variant"`
1104 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001105
1106 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001107 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001108 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001109 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001110 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001111 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001112 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001113 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001114}
1115
Colin Cross97ba0732015-03-23 17:50:24 -07001116type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001117 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001118
Colin Cross28344522015-04-22 13:07:53 -07001119 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001120 reuseObjFiles common.Paths
1121 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001122 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001123 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001124 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001125
Colin Cross7d5136f2015-05-11 13:39:40 -07001126 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001127}
1128
Colin Crossed4cf0b2015-03-26 14:43:45 -07001129func (c *CCLibrary) buildStatic() bool {
1130 return c.LibraryProperties.BuildStatic
1131}
1132
1133func (c *CCLibrary) buildShared() bool {
1134 return c.LibraryProperties.BuildShared
1135}
1136
Colin Cross97ba0732015-03-23 17:50:24 -07001137type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001138 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001139 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001140 setReuseFrom(ccLibraryInterface)
1141 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001142 getReuseObjFiles() common.Paths
1143 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001144}
1145
Colin Crossed4cf0b2015-03-26 14:43:45 -07001146var _ ccLibraryInterface = (*CCLibrary)(nil)
1147
Colin Cross97ba0732015-03-23 17:50:24 -07001148func (c *CCLibrary) ccLibrary() *CCLibrary {
1149 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001150}
1151
Colin Cross97ba0732015-03-23 17:50:24 -07001152func NewCCLibrary(library *CCLibrary, module CCModuleType,
1153 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1154
Colin Crossfa138792015-04-24 17:31:52 -07001155 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001156 &library.LibraryProperties)
1157}
1158
1159func CCLibraryFactory() (blueprint.Module, []interface{}) {
1160 module := &CCLibrary{}
1161
1162 module.LibraryProperties.BuildShared = true
1163 module.LibraryProperties.BuildStatic = true
1164
1165 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1166}
1167
Colin Cross0676e2d2015-04-24 17:39:18 -07001168func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001169 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001170 if c.static() {
1171 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1172 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1173 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1174 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001175 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001176 if c.Properties.Sdk_version == "" {
1177 depNames.CrtBegin = "crtbegin_so"
1178 depNames.CrtEnd = "crtend_so"
1179 } else {
1180 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1181 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1182 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001183 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001184 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1185 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1186 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001187 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001188
Dan Willemsen218f6562015-07-08 18:13:11 -07001189 c.systemLibs = c.systemSharedLibs(ctx)
1190
Colin Cross21b9a242015-03-24 14:15:58 -07001191 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001192}
1193
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001194func (c *CCLibrary) outputFile() common.OptionalPath {
1195 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001196}
1197
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001198func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001199 return c.reuseObjFiles
1200}
1201
1202func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1203 c.reuseFrom = reuseFrom
1204}
1205
1206func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1207 return c.reuseFrom
1208}
1209
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001210func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001211 return c.objFiles
1212}
1213
Colin Cross28344522015-04-22 13:07:53 -07001214func (c *CCLibrary) exportedFlags() []string {
1215 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001216}
1217
Colin Cross0676e2d2015-04-24 17:39:18 -07001218func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001219 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001220
Dan Willemsen490fd492015-11-24 17:53:15 -08001221 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1222 // all code is position independent, and then those warnings get promoted to
1223 // errors.
1224 if ctx.HostType() != common.Windows {
1225 flags.CFlags = append(flags.CFlags, "-fPIC")
1226 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001227
Colin Crossd8e780d2015-04-28 17:39:43 -07001228 if c.static() {
1229 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1230 } else {
1231 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1232 }
1233
Colin Cross18b6dc52015-04-28 13:20:37 -07001234 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001235 libName := ctx.ModuleName()
1236 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1237 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001238 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001239 sharedFlag = "-shared"
1240 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001241 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001242 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001243 }
Colin Cross97ba0732015-03-23 17:50:24 -07001244
Colin Cross0af4b842015-04-30 16:36:18 -07001245 if ctx.Darwin() {
1246 flags.LdFlags = append(flags.LdFlags,
1247 "-dynamiclib",
1248 "-single_module",
1249 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001250 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001251 )
1252 } else {
1253 flags.LdFlags = append(flags.LdFlags,
1254 "-Wl,--gc-sections",
1255 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001256 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001257 )
1258 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001259 }
Colin Cross97ba0732015-03-23 17:50:24 -07001260
1261 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001262}
1263
Colin Cross97ba0732015-03-23 17:50:24 -07001264func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001265 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001266
1267 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001268 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001269 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001270
1271 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001272 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001273
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001274 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001275
Colin Cross0af4b842015-04-30 16:36:18 -07001276 if ctx.Darwin() {
1277 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1278 } else {
1279 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1280 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001281
1282 c.objFiles = objFiles
1283 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001284
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001285 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001286 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001287 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001288
1289 ctx.CheckbuildFile(outputFile)
1290}
1291
Colin Cross97ba0732015-03-23 17:50:24 -07001292func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001293 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001294
1295 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001296 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001297 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001298
1299 objFiles = append(objFiles, objFilesShared...)
1300
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001301 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001302
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001303 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001304
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001305 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1306 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1307 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1308 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001309 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001310 if versionScript.Valid() {
1311 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1312 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001313 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001314 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001315 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1316 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001317 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001318 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1319 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001320 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001321 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1322 }
1323 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001324 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001325 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1326 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001327 if unexportedSymbols.Valid() {
1328 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1329 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001330 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331 if forceNotWeakSymbols.Valid() {
1332 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1333 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001334 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001335 if forceWeakSymbols.Valid() {
1336 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1337 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001338 }
Colin Crossaee540a2015-07-06 17:48:31 -07001339 }
1340
Colin Cross97ba0732015-03-23 17:50:24 -07001341 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001342 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001343 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001344
1345 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001346 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001347 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001348 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001349}
1350
Colin Cross97ba0732015-03-23 17:50:24 -07001351func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001352 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001353
1354 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001355 if c.getReuseFrom().ccLibrary() == c {
1356 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001357 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001358 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1359 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001360 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001361 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001362 }
1363
Colin Crossed4cf0b2015-03-26 14:43:45 -07001364 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001365 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1366 } else {
1367 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1368 }
1369}
1370
Colin Cross97ba0732015-03-23 17:50:24 -07001371func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001372 // Static libraries do not get installed.
1373}
1374
Colin Cross97ba0732015-03-23 17:50:24 -07001375func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001376 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001377 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001378 installDir = "lib64"
1379 }
1380
Dan Willemsen782a2d12015-12-21 14:55:28 -08001381 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001382}
1383
Colin Cross97ba0732015-03-23 17:50:24 -07001384func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001385 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001386 c.installStaticLibrary(ctx, flags)
1387 } else {
1388 c.installSharedLibrary(ctx, flags)
1389 }
1390}
1391
Colin Cross3f40fa42015-01-30 17:27:36 -08001392//
1393// Objects (for crt*.o)
1394//
1395
Dan Albertc3144b12015-04-28 18:17:56 -07001396type ccObjectProvider interface {
1397 object() *ccObject
1398}
1399
Colin Cross3f40fa42015-01-30 17:27:36 -08001400type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001401 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001402 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001403}
1404
Dan Albertc3144b12015-04-28 18:17:56 -07001405func (c *ccObject) object() *ccObject {
1406 return c
1407}
1408
Colin Cross97ba0732015-03-23 17:50:24 -07001409func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001410 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001411
Colin Crossfa138792015-04-24 17:31:52 -07001412 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001413}
1414
Colin Cross0676e2d2015-04-24 17:39:18 -07001415func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001416 // object files can't have any dynamic dependencies
1417 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001418}
1419
1420func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001421 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001422
Colin Cross97ba0732015-03-23 17:50:24 -07001423 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001424
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001425 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001426 if len(objFiles) == 1 {
1427 outputFile = objFiles[0]
1428 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001429 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1430 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1431 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001432 }
1433
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001434 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001435
1436 ctx.CheckbuildFile(outputFile)
1437}
1438
Colin Cross97ba0732015-03-23 17:50:24 -07001439func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001440 // Object files do not get installed.
1441}
1442
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001443func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001444 return c.out
1445}
1446
Dan Albertc3144b12015-04-28 18:17:56 -07001447var _ ccObjectProvider = (*ccObject)(nil)
1448
Colin Cross3f40fa42015-01-30 17:27:36 -08001449//
1450// Executables
1451//
1452
Colin Cross7d5136f2015-05-11 13:39:40 -07001453type CCBinaryProperties struct {
1454 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001455 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001456
1457 // set the name of the output
1458 Stem string `android:"arch_variant"`
1459
1460 // append to the name of the output
1461 Suffix string `android:"arch_variant"`
1462
1463 // if set, add an extra objcopy --prefix-symbols= step
1464 Prefix_symbols string
1465}
1466
Colin Cross97ba0732015-03-23 17:50:24 -07001467type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001468 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001469 out common.Path
1470 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001471 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001472}
1473
Colin Crossed4cf0b2015-03-26 14:43:45 -07001474func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001475 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001476}
1477
1478func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001479 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001480}
1481
Colin Cross97ba0732015-03-23 17:50:24 -07001482func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001483 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001484 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001485 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001486 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001487
1488 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001489}
1490
Colin Cross0676e2d2015-04-24 17:39:18 -07001491func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001492 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001493 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001494 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001495 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001496 depNames.CrtBegin = "crtbegin_static"
1497 } else {
1498 depNames.CrtBegin = "crtbegin_dynamic"
1499 }
1500 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001501 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001502 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001503 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1504 } else {
1505 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1506 }
1507 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001508 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001509
Colin Cross06a931b2015-10-28 17:23:31 -07001510 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001511 if c.stl(ctx) == "libc++_static" {
1512 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1513 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001514 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1515 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1516 // move them to the beginning of deps.LateStaticLibs
1517 var groupLibs []string
1518 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1519 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1520 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1521 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001522 }
Colin Cross21b9a242015-03-24 14:15:58 -07001523 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001524}
1525
Colin Cross97ba0732015-03-23 17:50:24 -07001526func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001527 hod common.HostOrDeviceSupported, multilib common.Multilib,
1528 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001529
Colin Cross1f8f2342015-03-26 16:09:47 -07001530 props = append(props, &binary.BinaryProperties)
1531
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001532 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001533}
1534
Colin Cross97ba0732015-03-23 17:50:24 -07001535func CCBinaryFactory() (blueprint.Module, []interface{}) {
1536 module := &CCBinary{}
1537
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001538 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001539}
1540
Colin Cross6362e272015-10-29 15:25:03 -07001541func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001542 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001543 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001544 }
Colin Cross06a931b2015-10-28 17:23:31 -07001545 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001546 c.dynamicProperties.VariantIsStaticBinary = true
1547 }
1548}
1549
Colin Cross0676e2d2015-04-24 17:39:18 -07001550func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001551 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001552
Dan Willemsen490fd492015-11-24 17:53:15 -08001553 if ctx.Host() {
1554 flags.LdFlags = append(flags.LdFlags, "-pie")
1555 if ctx.HostType() == common.Windows {
1556 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1557 }
1558 }
1559
1560 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1561 // all code is position independent, and then those warnings get promoted to
1562 // errors.
1563 if ctx.HostType() != common.Windows {
1564 flags.CFlags = append(flags.CFlags, "-fpie")
1565 }
Colin Cross97ba0732015-03-23 17:50:24 -07001566
Colin Crossf6566ed2015-03-24 11:13:38 -07001567 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001568 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001569 // Clang driver needs -static to create static executable.
1570 // However, bionic/linker uses -shared to overwrite.
1571 // Linker for x86 targets does not allow coexistance of -static and -shared,
1572 // so we add -static only if -shared is not used.
1573 if !inList("-shared", flags.LdFlags) {
1574 flags.LdFlags = append(flags.LdFlags, "-static")
1575 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001576
Colin Crossed4cf0b2015-03-26 14:43:45 -07001577 flags.LdFlags = append(flags.LdFlags,
1578 "-nostdlib",
1579 "-Bstatic",
1580 "-Wl,--gc-sections",
1581 )
1582
1583 } else {
1584 linker := "/system/bin/linker"
1585 if flags.Toolchain.Is64Bit() {
1586 linker = "/system/bin/linker64"
1587 }
1588
1589 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001590 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001591 "-nostdlib",
1592 "-Bdynamic",
1593 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1594 "-Wl,--gc-sections",
1595 "-Wl,-z,nocopyreloc",
1596 )
1597 }
Colin Cross0af4b842015-04-30 16:36:18 -07001598 } else if ctx.Darwin() {
1599 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001600 }
1601
Colin Cross97ba0732015-03-23 17:50:24 -07001602 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001603}
1604
Colin Cross97ba0732015-03-23 17:50:24 -07001605func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001606 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001607
Colin Cross06a931b2015-10-28 17:23:31 -07001608 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001609 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1610 "from static libs or set static_executable: true")
1611 }
1612
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001613 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001614 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001615 if c.BinaryProperties.Prefix_symbols != "" {
1616 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001618 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1619 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1620 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001621
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001622 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001623
Colin Cross97ba0732015-03-23 17:50:24 -07001624 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001625 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001626 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001627}
Colin Cross3f40fa42015-01-30 17:27:36 -08001628
Colin Cross97ba0732015-03-23 17:50:24 -07001629func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001630 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001631}
1632
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001633func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001634 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001635 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001636 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001637 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001638}
1639
Colin Cross6002e052015-09-16 16:00:08 -07001640func (c *CCBinary) binary() *CCBinary {
1641 return c
1642}
1643
1644type testPerSrc interface {
1645 binary() *CCBinary
1646 testPerSrc() bool
1647}
1648
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001649var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001650
Colin Cross6362e272015-10-29 15:25:03 -07001651func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001652 if test, ok := mctx.Module().(testPerSrc); ok {
1653 if test.testPerSrc() {
1654 testNames := make([]string, len(test.binary().Properties.Srcs))
1655 for i, src := range test.binary().Properties.Srcs {
1656 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1657 }
1658 tests := mctx.CreateLocalVariations(testNames...)
1659 for i, src := range test.binary().Properties.Srcs {
1660 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001661 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001662 }
1663 }
1664 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001665}
1666
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001667type CCTestProperties struct {
1668 // if set, build against the gtest library. Defaults to true.
1669 Gtest bool
1670
1671 // Create a separate binary for each source file. Useful when there is
1672 // global state that can not be torn down and reset between each test suite.
1673 Test_per_src *bool
1674}
1675
Colin Cross9ffb4f52015-04-24 17:48:09 -07001676type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001677 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001678
1679 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001680}
1681
Colin Cross9ffb4f52015-04-24 17:48:09 -07001682func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001683 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001684 if !c.TestProperties.Gtest {
1685 return flags
1686 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001687
Colin Cross97ba0732015-03-23 17:50:24 -07001688 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001689 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001690 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001691
1692 if ctx.HostType() == common.Windows {
1693 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1694 } else {
1695 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1696 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1697 }
1698 } else {
1699 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001700 }
1701
1702 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001703 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001704 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001705
Colin Cross21b9a242015-03-24 14:15:58 -07001706 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001707}
1708
Colin Cross9ffb4f52015-04-24 17:48:09 -07001709func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001710 if c.TestProperties.Gtest {
1711 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1712 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001713 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001714 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001715}
1716
Dan Willemsen782a2d12015-12-21 14:55:28 -08001717func (c *CCTest) InstallInData() bool {
1718 return true
1719}
1720
Colin Cross9ffb4f52015-04-24 17:48:09 -07001721func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001722 installDir := "nativetest"
1723 if flags.Toolchain.Is64Bit() {
1724 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001725 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001726 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1727}
1728
1729func (c *CCTest) testPerSrc() bool {
1730 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001731}
1732
Colin Cross9ffb4f52015-04-24 17:48:09 -07001733func NewCCTest(test *CCTest, module CCModuleType,
1734 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1735
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001736 props = append(props, &test.TestProperties)
1737
1738 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001739}
1740
1741func CCTestFactory() (blueprint.Module, []interface{}) {
1742 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001743 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001744
1745 return NewCCTest(module, module, common.HostAndDeviceSupported)
1746}
1747
Colin Cross2ba19d92015-05-07 15:44:20 -07001748type CCBenchmark struct {
1749 CCBinary
1750}
1751
1752func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1753 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001754 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001755 return depNames
1756}
1757
Dan Willemsen782a2d12015-12-21 14:55:28 -08001758func (c *CCBenchmark) InstallInData() bool {
1759 return true
1760}
1761
Colin Cross2ba19d92015-05-07 15:44:20 -07001762func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1763 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001764 installDir := "nativetest"
1765 if flags.Toolchain.Is64Bit() {
1766 installDir = "nativetest64"
1767 }
1768 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001769 } else {
1770 c.CCBinary.installModule(ctx, flags)
1771 }
1772}
1773
1774func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1775 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1776
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001777 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001778}
1779
1780func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1781 module := &CCBenchmark{}
1782
1783 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1784}
1785
Colin Cross3f40fa42015-01-30 17:27:36 -08001786//
1787// Static library
1788//
1789
Colin Cross97ba0732015-03-23 17:50:24 -07001790func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1791 module := &CCLibrary{}
1792 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001793
Colin Cross97ba0732015-03-23 17:50:24 -07001794 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001795}
1796
1797//
1798// Shared libraries
1799//
1800
Colin Cross97ba0732015-03-23 17:50:24 -07001801func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1802 module := &CCLibrary{}
1803 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001804
Colin Cross97ba0732015-03-23 17:50:24 -07001805 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001806}
1807
1808//
1809// Host static library
1810//
1811
Colin Cross97ba0732015-03-23 17:50:24 -07001812func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1813 module := &CCLibrary{}
1814 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001815
Colin Cross97ba0732015-03-23 17:50:24 -07001816 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001817}
1818
1819//
1820// Host Shared libraries
1821//
1822
Colin Cross97ba0732015-03-23 17:50:24 -07001823func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1824 module := &CCLibrary{}
1825 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001826
Colin Cross97ba0732015-03-23 17:50:24 -07001827 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001828}
1829
1830//
1831// Host Binaries
1832//
1833
Colin Cross97ba0732015-03-23 17:50:24 -07001834func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1835 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001836
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001837 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001838}
1839
1840//
Colin Cross1f8f2342015-03-26 16:09:47 -07001841// Host Tests
1842//
1843
1844func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001845 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001846 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001847}
1848
1849//
Colin Cross2ba19d92015-05-07 15:44:20 -07001850// Host Benchmarks
1851//
1852
1853func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1854 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001855 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001856}
1857
1858//
Colin Crosscfad1192015-11-02 16:43:11 -08001859// Defaults
1860//
1861type CCDefaults struct {
1862 common.AndroidModuleBase
1863 common.DefaultsModule
1864}
1865
1866func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1867}
1868
1869func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1870 module := &CCDefaults{}
1871
1872 propertyStructs := []interface{}{
1873 &CCBaseProperties{},
1874 &CCLibraryProperties{},
1875 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001876 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001877 &CCUnusedProperties{},
1878 }
1879
Dan Willemsen218f6562015-07-08 18:13:11 -07001880 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1881 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001882
1883 return common.InitDefaultsModule(module, module, propertyStructs...)
1884}
1885
1886//
Colin Cross3f40fa42015-01-30 17:27:36 -08001887// Device libraries shipped with gcc
1888//
1889
1890type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001891 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001892}
1893
Colin Cross0676e2d2015-04-24 17:39:18 -07001894func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001895 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001896 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001897}
1898
Colin Cross97ba0732015-03-23 17:50:24 -07001899func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001900 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001901
Colin Cross97ba0732015-03-23 17:50:24 -07001902 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001903 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001904
Colin Crossfa138792015-04-24 17:31:52 -07001905 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001906 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001907}
1908
1909func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001910 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001911
1912 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001913 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001914
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001915 if flags.Clang {
1916 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1917 }
1918
Colin Cross3f40fa42015-01-30 17:27:36 -08001919 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1920
1921 c.out = outputFile
1922
1923 ctx.CheckbuildFile(outputFile)
1924}
1925
Colin Cross97ba0732015-03-23 17:50:24 -07001926func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001927 // Toolchain libraries do not get installed.
1928}
1929
Dan Albertbe961682015-03-18 23:38:50 -07001930// NDK prebuilt libraries.
1931//
1932// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1933// either (with the exception of the shared STLs, which are installed to the app's directory rather
1934// than to the system image).
1935
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001936func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1937 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1938 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001939}
1940
Dan Albertc3144b12015-04-28 18:17:56 -07001941func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001942 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001943
1944 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1945 // We want to translate to just NAME.EXT
1946 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1947 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001948 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001949}
1950
1951type ndkPrebuiltObject struct {
1952 ccObject
1953}
1954
Dan Albertc3144b12015-04-28 18:17:56 -07001955func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1956 // NDK objects can't have any dependencies
1957 return CCDeps{}
1958}
1959
1960func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1961 module := &ndkPrebuiltObject{}
1962 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1963}
1964
1965func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001966 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001967 // A null build step, but it sets up the output path.
1968 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1969 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1970 }
1971
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001972 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001973}
1974
1975func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1976 // Objects do not get installed.
1977}
1978
1979var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1980
Dan Albertbe961682015-03-18 23:38:50 -07001981type ndkPrebuiltLibrary struct {
1982 CCLibrary
1983}
1984
Colin Cross0676e2d2015-04-24 17:39:18 -07001985func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001986 // NDK libraries can't have any dependencies
1987 return CCDeps{}
1988}
1989
1990func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
1991 module := &ndkPrebuiltLibrary{}
1992 module.LibraryProperties.BuildShared = true
1993 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
1994}
1995
1996func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001997 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07001998 // A null build step, but it sets up the output path.
1999 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2000 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2001 }
2002
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002003 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
2004 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002005
Dan Willemsen490fd492015-11-24 17:53:15 -08002006 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07002007 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07002008}
2009
2010func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07002011 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07002012}
2013
2014// The NDK STLs are slightly different from the prebuilt system libraries:
2015// * Are not specific to each platform version.
2016// * The libraries are not in a predictable location for each STL.
2017
2018type ndkPrebuiltStl struct {
2019 ndkPrebuiltLibrary
2020}
2021
2022type ndkPrebuiltStaticStl struct {
2023 ndkPrebuiltStl
2024}
2025
2026type ndkPrebuiltSharedStl struct {
2027 ndkPrebuiltStl
2028}
2029
2030func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2031 module := &ndkPrebuiltSharedStl{}
2032 module.LibraryProperties.BuildShared = true
2033 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2034}
2035
2036func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2037 module := &ndkPrebuiltStaticStl{}
2038 module.LibraryProperties.BuildStatic = true
2039 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2040}
2041
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002042func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002043 gccVersion := toolchain.GccVersion()
2044 var libDir string
2045 switch stl {
2046 case "libstlport":
2047 libDir = "cxx-stl/stlport/libs"
2048 case "libc++":
2049 libDir = "cxx-stl/llvm-libc++/libs"
2050 case "libgnustl":
2051 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2052 }
2053
2054 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002055 ndkSrcRoot := "prebuilts/ndk/current/sources"
2056 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002057 }
2058
2059 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002060 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002061}
2062
2063func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002064 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002065 // A null build step, but it sets up the output path.
2066 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2067 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2068 }
2069
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002070 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002071 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002072
2073 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002074 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002075 if c.LibraryProperties.BuildStatic {
2076 libExt = staticLibraryExtension
2077 }
2078
2079 stlName := strings.TrimSuffix(libName, "_shared")
2080 stlName = strings.TrimSuffix(stlName, "_static")
2081 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002082 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002083}
2084
Colin Cross6362e272015-10-29 15:25:03 -07002085func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002086 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002087 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002088 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002089 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002090 modules[0].(ccLinkedInterface).setStatic(true)
2091 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002092 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002093 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002094 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002095 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002096 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002097 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002098 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002099 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002100 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002101
2102 if _, ok := c.(ccLibraryInterface); ok {
2103 reuseFrom := modules[0].(ccLibraryInterface)
2104 for _, m := range modules {
2105 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002106 }
2107 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002108 }
2109}
Colin Cross74d1ec02015-04-28 13:30:13 -07002110
2111// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2112// modifies the slice contents in place, and returns a subslice of the original slice
2113func lastUniqueElements(list []string) []string {
2114 totalSkip := 0
2115 for i := len(list) - 1; i >= totalSkip; i-- {
2116 skip := 0
2117 for j := i - 1; j >= totalSkip; j-- {
2118 if list[i] == list[j] {
2119 skip++
2120 } else {
2121 list[j+skip] = list[j]
2122 }
2123 }
2124 totalSkip += skip
2125 }
2126 return list[totalSkip:]
2127}
Colin Cross06a931b2015-10-28 17:23:31 -07002128
2129var Bool = proptools.Bool