blob: 4a874337b3fc368f881ac34d19179e53c48fc970 [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
Dan Willemsenbe03f342016-03-03 17:21:04 -0800106 noOverrideGlobalCflags = []string{
107 "-Werror=int-to-pointer-cast",
108 "-Werror=pointer-to-int-cast",
109 }
110
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700111 illegalFlags = []string{
112 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 }
114)
115
116func init() {
117 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
118 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
119 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800120 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800121
122 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
123
124 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800125 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800126 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800127 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 pctx.StaticVariable("hostClangGlobalCflags",
129 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800130 pctx.StaticVariable("noOverrideClangGlobalCflags",
131 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
132
Tim Kilbournf2948142015-03-11 12:03:03 -0700133 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800134 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800135
136 // Everything in this list is a crime against abstraction and dependency tracking.
137 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800138 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700139 []string{
140 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800141 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 "hardware/libhardware/include",
143 "hardware/libhardware_legacy/include",
144 "hardware/ril/include",
145 "libnativehelper/include",
146 "frameworks/native/include",
147 "frameworks/native/opengl/include",
148 "frameworks/av/include",
149 "frameworks/base/include",
150 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800151 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
152 // with this, since there is no associated library.
153 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
154 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800155
Dan Willemsen767078e2016-03-01 13:42:19 -0800156 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2629532/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800157}
158
Colin Cross6362e272015-10-29 15:25:03 -0700159type CCModuleContext common.AndroidBaseContext
160
Colin Cross3f40fa42015-01-30 17:27:36 -0800161// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700162type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 common.AndroidModule
164
Colin Crossfa138792015-04-24 17:31:52 -0700165 // Modify property values after parsing Blueprints file but before starting dependency
166 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700167 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700168
Colin Cross21b9a242015-03-24 14:15:58 -0700169 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700170 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800171
Colin Cross6362e272015-10-29 15:25:03 -0700172 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700173 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800174
Colin Cross6362e272015-10-29 15:25:03 -0700175 // Add dynamic dependencies
176 depsMutator(common.AndroidBottomUpMutatorContext)
177
Colin Cross3f40fa42015-01-30 17:27:36 -0800178 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800180
Dan Albertc403f7c2015-03-18 14:01:18 -0700181 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700182 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700183
Colin Cross3f40fa42015-01-30 17:27:36 -0800184 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700185 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800186}
187
Colin Cross97ba0732015-03-23 17:50:24 -0700188type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700190
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700191 ObjFiles common.Paths
192
193 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700194
Colin Cross97ba0732015-03-23 17:50:24 -0700195 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700196}
197
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198type CCPathDeps struct {
199 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
200
201 ObjFiles common.Paths
202 WholeStaticLibObjFiles common.Paths
203
204 Cflags, ReexportedCflags []string
205
206 CrtBegin, CrtEnd common.OptionalPath
207}
208
Colin Cross97ba0732015-03-23 17:50:24 -0700209type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700210 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
211 AsFlags []string // Flags that apply to assembly source files
212 CFlags []string // Flags that apply to C and C++ source files
213 ConlyFlags []string // Flags that apply to C source files
214 CppFlags []string // Flags that apply to C++ source files
215 YaccFlags []string // Flags that apply to Yacc source files
216 LdFlags []string // Flags that apply to linker command lines
217
218 Nocrt bool
219 Toolchain Toolchain
220 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700221}
222
Colin Cross7d5136f2015-05-11 13:39:40 -0700223// Properties used to compile all C or C++ modules
224type CCBaseProperties struct {
225 // 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 -0700226 Srcs []string `android:"arch_variant"`
227
228 // list of source files that should not be used to build the C/C++ module.
229 // This is most useful in the arch/multilib variants to remove non-common files
230 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700231
232 // list of module-specific flags that will be used for C and C++ compiles.
233 Cflags []string `android:"arch_variant"`
234
235 // list of module-specific flags that will be used for C++ compiles
236 Cppflags []string `android:"arch_variant"`
237
238 // list of module-specific flags that will be used for C compiles
239 Conlyflags []string `android:"arch_variant"`
240
241 // list of module-specific flags that will be used for .S compiles
242 Asflags []string `android:"arch_variant"`
243
244 // list of module-specific flags that will be used for .y and .yy compiles
245 Yaccflags []string
246
247 // list of module-specific flags that will be used for all link steps
248 Ldflags []string `android:"arch_variant"`
249
250 // the instruction set architecture to use to compile the C/C++
251 // module.
252 Instruction_set string `android:"arch_variant"`
253
254 // list of directories relative to the root of the source tree that will
255 // be added to the include path using -I.
256 // If possible, don't use this. If adding paths from the current directory use
257 // local_include_dirs, if adding paths from other modules use export_include_dirs in
258 // that module.
259 Include_dirs []string `android:"arch_variant"`
260
Colin Cross39d97f22015-09-14 12:30:50 -0700261 // list of files relative to the root of the source tree that will be included
262 // using -include.
263 // If possible, don't use this.
264 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
268 Local_include_dirs []string `android:"arch_variant"`
269
Colin Cross39d97f22015-09-14 12:30:50 -0700270 // list of files relative to the Blueprints file that will be included
271 // using -include.
272 // If possible, don't use this.
273 Local_include_files []string `android:"arch_variant"`
274
Colin Cross7d5136f2015-05-11 13:39:40 -0700275 // list of directories relative to the Blueprints file that will
276 // be added to the include path using -I for any module that links against this module
277 Export_include_dirs []string `android:"arch_variant"`
278
279 // list of module-specific flags that will be used for C and C++ compiles when
280 // compiling with clang
281 Clang_cflags []string `android:"arch_variant"`
282
283 // list of module-specific flags that will be used for .S compiles when
284 // compiling with clang
285 Clang_asflags []string `android:"arch_variant"`
286
287 // list of system libraries that will be dynamically linked to
288 // shared library and executable modules. If unset, generally defaults to libc
289 // and libm. Set to [] to prevent linking against libc and libm.
290 System_shared_libs []string
291
292 // list of modules whose object files should be linked into this module
293 // in their entirety. For static library modules, all of the .o files from the intermediate
294 // directory of the dependency will be linked into this modules .a file. For a shared library,
295 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
296 Whole_static_libs []string `android:"arch_variant"`
297
298 // list of modules that should be statically linked into this module.
299 Static_libs []string `android:"arch_variant"`
300
301 // list of modules that should be dynamically linked into this module.
302 Shared_libs []string `android:"arch_variant"`
303
304 // allow the module to contain undefined symbols. By default,
305 // modules cannot contain undefined symbols that are not satisified by their immediate
306 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
307 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700308 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700309
310 // don't link in crt_begin and crt_end. This flag should only be necessary for
311 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700312 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700313
Dan Willemsend67be222015-09-16 15:19:33 -0700314 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700315 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700316
Colin Cross7d5136f2015-05-11 13:39:40 -0700317 // don't insert default compiler flags into asflags, cflags,
318 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700319 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700320
321 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700322 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700323
324 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700325 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700326
327 // -l arguments to pass to linker for host-provided shared libraries
328 Host_ldlibs []string `android:"arch_variant"`
329
330 // select the STL library to use. Possible values are "libc++", "libc++_static",
331 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
332 // default
333 Stl string
334
335 // Set for combined shared/static libraries to prevent compiling object files a second time
336 SkipCompileObjs bool `blueprint:"mutated"`
337
338 Debug, Release struct {
339 // list of module-specific flags that will be used for C and C++ compiles in debug or
340 // release builds
341 Cflags []string `android:"arch_variant"`
342 } `android:"arch_variant"`
343
344 // Minimum sdk version supported when compiling against the ndk
345 Sdk_version string
346
347 // install to a subdirectory of the default install path for the module
348 Relative_install_path string
349}
350
Colin Crosscfad1192015-11-02 16:43:11 -0800351type CCUnusedProperties struct {
352 Native_coverage *bool
353 Required []string
354 Sanitize []string `android:"arch_variant"`
355 Sanitize_recover []string
356 Strip string
357 Tags []string
358}
359
Colin Crossfa138792015-04-24 17:31:52 -0700360// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700361// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
362// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700363type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700364 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800365 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700366 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700367
Colin Cross7d5136f2015-05-11 13:39:40 -0700368 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700369
Colin Crosscfad1192015-11-02 16:43:11 -0800370 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700371
372 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700373
374 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700375}
376
Colin Crossfa138792015-04-24 17:31:52 -0700377func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700378 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
379
380 base.module = module
381
Colin Crossfa138792015-04-24 17:31:52 -0700382 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700383
Colin Crosscfad1192015-11-02 16:43:11 -0800384 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
385
386 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700387}
388
Colin Crossfa138792015-04-24 17:31:52 -0700389func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 toolchain := c.findToolchain(ctx)
391 if ctx.Failed() {
392 return
393 }
394
Colin Cross21b9a242015-03-24 14:15:58 -0700395 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800396 if ctx.Failed() {
397 return
398 }
399
Colin Cross74d1ec02015-04-28 13:30:13 -0700400 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800401 if ctx.Failed() {
402 return
403 }
404
Colin Cross28344522015-04-22 13:07:53 -0700405 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700406
Colin Cross581c1892015-04-07 16:50:10 -0700407 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 if ctx.Failed() {
409 return
410 }
411
Colin Cross581c1892015-04-07 16:50:10 -0700412 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700413 if ctx.Failed() {
414 return
415 }
416
417 objFiles = append(objFiles, generatedObjFiles...)
418
Colin Cross3f40fa42015-01-30 17:27:36 -0800419 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
420 if ctx.Failed() {
421 return
422 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700423
424 c.ccModuleType().installModule(ctx, flags)
425 if ctx.Failed() {
426 return
427 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800428}
429
Colin Crossfa138792015-04-24 17:31:52 -0700430func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800431 return c.module
432}
433
Colin Crossfa138792015-04-24 17:31:52 -0700434func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800435 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700436 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800437 ht := ctx.HostType()
438 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800439 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800440 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800441 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800443 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800444}
445
Colin Cross6362e272015-10-29 15:25:03 -0700446func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700447}
448
Colin Crosse11befc2015-04-27 17:49:17 -0700449func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700450 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
451 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
452 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700453
Colin Cross21b9a242015-03-24 14:15:58 -0700454 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800455}
456
Colin Cross6362e272015-10-29 15:25:03 -0700457func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700458 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
459 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
460 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
461 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
462
463 staticLibs := c.savedDepNames.WholeStaticLibs
464 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
465 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700466 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800467
Colin Cross74d1ec02015-04-28 13:30:13 -0700468 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700469
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700470 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700471 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700472 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700473 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700474 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700475 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700476 }
Colin Cross6362e272015-10-29 15:25:03 -0700477}
Colin Cross21b9a242015-03-24 14:15:58 -0700478
Colin Cross6362e272015-10-29 15:25:03 -0700479func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
480 if c, ok := ctx.Module().(CCModuleType); ok {
481 c.ModifyProperties(ctx)
482 c.depsMutator(ctx)
483 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800484}
485
486// Create a ccFlags struct that collects the compile flags from global values,
487// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700488func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700489 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700490 CFlags: c.Properties.Cflags,
491 CppFlags: c.Properties.Cppflags,
492 ConlyFlags: c.Properties.Conlyflags,
493 LdFlags: c.Properties.Ldflags,
494 AsFlags: c.Properties.Asflags,
495 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700496 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700497 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700498 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 }
Colin Cross28344522015-04-22 13:07:53 -0700500
501 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700502 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
503 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700504 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700505 includeDirsToFlags(localIncludeDirs),
506 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700507
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700508 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
509 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700510
511 flags.GlobalFlags = append(flags.GlobalFlags,
512 includeFilesToFlags(rootIncludeFiles),
513 includeFilesToFlags(localIncludeFiles))
514
Colin Cross06a931b2015-10-28 17:23:31 -0700515 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700516 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700517 flags.GlobalFlags = append(flags.GlobalFlags,
518 "${commonGlobalIncludes}",
519 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800520 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700521 }
522
523 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700524 "-I" + common.PathForModuleSrc(ctx).String(),
525 "-I" + common.PathForModuleOut(ctx).String(),
526 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700527 }...)
528 }
529
Colin Cross06a931b2015-10-28 17:23:31 -0700530 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700531 if ctx.Host() {
532 flags.Clang = true
533 }
534
535 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
536 flags.Clang = true
537 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800538 }
539
Dan Willemsen490fd492015-11-24 17:53:15 -0800540 if !toolchain.ClangSupported() {
541 flags.Clang = false
542 }
543
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800544 instructionSet := c.Properties.Instruction_set
545 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
546 if flags.Clang {
547 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
548 }
549 if err != nil {
550 ctx.ModuleErrorf("%s", err)
551 }
552
553 // TODO: debug
554 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
555
Colin Cross97ba0732015-03-23 17:50:24 -0700556 if flags.Clang {
557 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700558 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
559 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700560 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
561 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
562 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800563
564 target := "-target " + toolchain.ClangTriple()
565 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
566
Colin Cross97ba0732015-03-23 17:50:24 -0700567 flags.CFlags = append(flags.CFlags, target, gccPrefix)
568 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
569 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 }
571
Colin Cross06a931b2015-10-28 17:23:31 -0700572 if !Bool(c.Properties.No_default_compiler_flags) {
573 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700574 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800575 }
576
Colin Cross56b4d452015-04-21 17:38:44 -0700577 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
578
Colin Cross97ba0732015-03-23 17:50:24 -0700579 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800580 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700581 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700582 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 toolchain.ClangCflags(),
584 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700585 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800586
587 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800588 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700589 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700590 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 toolchain.Cflags(),
592 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700593 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800594 }
595
Colin Cross7b66f152015-12-15 16:07:43 -0800596 if Bool(ctx.AConfig().ProductVariables.Brillo) {
597 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
598 }
599
Colin Crossf6566ed2015-03-24 11:13:38 -0700600 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700601 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700602 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800603 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700604 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 }
606 }
607
Colin Cross97ba0732015-03-23 17:50:24 -0700608 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800609
Colin Cross97ba0732015-03-23 17:50:24 -0700610 if flags.Clang {
611 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
612 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700614 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
615 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 }
Colin Cross28344522015-04-22 13:07:53 -0700617
618 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700619 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700620 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800621 }
622
Colin Crossc4bde762015-11-23 16:11:30 -0800623 if flags.Clang {
624 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
625 } else {
626 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800627 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800628 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800629
Colin Cross0676e2d2015-04-24 17:39:18 -0700630 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800631
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700632 if c.Properties.Sdk_version == "" {
633 if ctx.Host() && !flags.Clang {
634 // The host GCC doesn't support C++14 (and is deprecated, so likely
635 // never will). Build these modules with C++11.
636 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
637 } else {
638 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
639 }
640 }
641
642 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
643 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
644 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
645
Dan Willemsen52b1cd22016-03-01 13:36:34 -0800646 // We can enforce some rules more strictly in the code we own. strict
647 // indicates if this is code that we can be stricter with. If we have
648 // rules that we want to apply to *our* code (but maybe can't for
649 // vendor/device specific things), we could extend this to be a ternary
650 // value.
651 strict := true
652 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
653 strict = false
654 }
655
656 // Can be used to make some annotations stricter for code we can fix
657 // (such as when we mark functions as deprecated).
658 if strict {
659 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
660 }
661
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 // Optimization to reduce size of build.ninja
663 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700664 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
665 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
666 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
667 flags.CFlags = []string{"$cflags"}
668 flags.CppFlags = []string{"$cppflags"}
669 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800670
671 return flags
672}
673
Colin Cross0676e2d2015-04-24 17:39:18 -0700674func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 return flags
676}
677
678// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700679func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700680 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700681
682 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800683
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700684 inputFiles := ctx.ExpandSources(srcFiles, excludes)
685 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800686
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700687 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800688}
689
Colin Crossfa138792015-04-24 17:31:52 -0700690// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700691func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800692
Colin Crossfa138792015-04-24 17:31:52 -0700693 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800694 return nil
695 }
696
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700697 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800698}
699
Colin Cross5049f022015-03-18 13:28:46 -0700700// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700701func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
702 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700703
Colin Crossfa138792015-04-24 17:31:52 -0700704 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700705 return nil
706 }
707
708 ctx.VisitDirectDeps(func(module blueprint.Module) {
709 if gen, ok := module.(genrule.SourceFileGenerator); ok {
710 srcs = append(srcs, gen.GeneratedSourceFiles()...)
711 }
712 })
713
714 if len(srcs) == 0 {
715 return nil
716 }
717
Colin Cross581c1892015-04-07 16:50:10 -0700718 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700719}
720
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700721func (c *CCBase) outputFile() common.OptionalPath {
722 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800723}
724
Colin Crossfa138792015-04-24 17:31:52 -0700725func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800726 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700727 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800728
729 for _, n := range names {
730 found := false
731 ctx.VisitDirectDeps(func(m blueprint.Module) {
732 otherName := ctx.OtherModuleName(m)
733 if otherName != n {
734 return
735 }
736
Colin Cross97ba0732015-03-23 17:50:24 -0700737 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800738 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800739 // If a cc_library host+device module depends on a library that exists as both
740 // cc_library_shared and cc_library_host_shared, it will end up with two
741 // dependencies with the same name, one of which is marked disabled for each
742 // of host and device. Ignore the disabled one.
743 return
744 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700745 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
747 otherName)
748 return
749 }
750
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700751 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800752 if found {
753 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
754 return
755 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700756 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800757 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700758 if i, ok := a.(ccExportedFlagsProducer); ok {
759 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800760 }
761 found = true
762 } else {
763 ctx.ModuleErrorf("module %q missing output file", otherName)
764 return
765 }
766 } else {
767 ctx.ModuleErrorf("module %q not an android module", otherName)
768 return
769 }
770 })
Colin Cross6ff51382015-12-17 16:39:19 -0800771 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800772 ctx.ModuleErrorf("unsatisified dependency on %q", n)
773 }
774 }
775
Colin Cross28344522015-04-22 13:07:53 -0700776 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800777}
778
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700779// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700780// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700781func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
782 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700783 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800784
Colin Cross21b9a242015-03-24 14:15:58 -0700785 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800786
Colin Cross28344522015-04-22 13:07:53 -0700787 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700788 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700789 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800790 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800791
Colin Cross21b9a242015-03-24 14:15:58 -0700792 for _, m := range wholeStaticLibModules {
793 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
794 depPaths.WholeStaticLibObjFiles =
795 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
796 } else {
797 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
798 }
799 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800800
Colin Cross28344522015-04-22 13:07:53 -0700801 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
802 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700803
Colin Cross28344522015-04-22 13:07:53 -0700804 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
805 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700806
Colin Cross28344522015-04-22 13:07:53 -0700807 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
808 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700809
810 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700811 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700812 otherName := ctx.OtherModuleName(m)
813 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700814 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700815 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700816 }
817 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700818 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700819 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700820 }
821 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700822 output := obj.object().outputFile()
823 if output.Valid() {
824 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
825 } else {
826 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
827 }
Colin Cross21b9a242015-03-24 14:15:58 -0700828 }
829 }
830 })
831
832 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800833}
834
Colin Cross7d5136f2015-05-11 13:39:40 -0700835type ccLinkedProperties struct {
836 VariantIsShared bool `blueprint:"mutated"`
837 VariantIsStatic bool `blueprint:"mutated"`
838 VariantIsStaticBinary bool `blueprint:"mutated"`
839}
840
Colin Crossfa138792015-04-24 17:31:52 -0700841// CCLinked contains the properties and members used by libraries and executables
842type CCLinked struct {
843 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700844 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800845}
846
Colin Crossfa138792015-04-24 17:31:52 -0700847func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700848 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
849
Colin Crossed4cf0b2015-03-26 14:43:45 -0700850 props = append(props, &dynamic.dynamicProperties)
851
Colin Crossfa138792015-04-24 17:31:52 -0700852 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700853}
854
Colin Crossfa138792015-04-24 17:31:52 -0700855func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700856 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700857 return c.Properties.System_shared_libs
858 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700859 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700860 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700861 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800862 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800863}
864
Colin Crossfa138792015-04-24 17:31:52 -0700865func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
866 if c.Properties.Sdk_version != "" && ctx.Device() {
867 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700868 case "":
869 return "ndk_system"
870 case "c++_shared", "c++_static",
871 "stlport_shared", "stlport_static",
872 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700873 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700874 default:
Colin Crossfa138792015-04-24 17:31:52 -0700875 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700876 return ""
877 }
878 }
879
Dan Willemsen490fd492015-11-24 17:53:15 -0800880 if ctx.HostType() == common.Windows {
881 switch c.Properties.Stl {
882 case "libc++", "libc++_static", "libstdc++", "":
883 // libc++ is not supported on mingw
884 return "libstdc++"
885 case "none":
886 return ""
887 default:
888 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
889 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700890 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800891 } else {
892 switch c.Properties.Stl {
893 case "libc++", "libc++_static",
894 "libstdc++":
895 return c.Properties.Stl
896 case "none":
897 return ""
898 case "":
899 if c.static() {
900 return "libc++_static"
901 } else {
902 return "libc++"
903 }
904 default:
905 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
906 return ""
907 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700908 }
909}
910
Dan Willemsen490fd492015-11-24 17:53:15 -0800911var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700912
913func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800914 hostDynamicGccLibs = map[common.HostType][]string{
915 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
916 common.Darwin: []string{"-lc", "-lSystem"},
917 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
918 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
919 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
920 "-lmsvcrt"},
921 }
922 hostStaticGccLibs = map[common.HostType][]string{
923 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
924 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
925 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700926 }
927}
Colin Cross712fc022015-04-27 11:13:34 -0700928
Colin Crosse11befc2015-04-27 17:49:17 -0700929func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700930 stl := c.stl(ctx)
931 if ctx.Failed() {
932 return flags
933 }
934
935 switch stl {
936 case "libc++", "libc++_static":
937 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700938 if ctx.Host() {
939 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
940 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Dan Willemsenb9553362016-03-03 19:24:03 -0800941 flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
Colin Cross18b6dc52015-04-28 13:20:37 -0700942 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800943 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700944 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800945 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700946 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700947 } else {
948 if ctx.Arch().ArchType == common.Arm {
949 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
950 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700951 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700952 case "libstdc++":
953 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
954 // tree is in good enough shape to not need it.
955 // Host builds will use GNU libstdc++.
956 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700957 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Dan Willemsen282a4b02016-03-09 10:30:22 -0800958 } else {
959 // Host builds will use the system C++. libc++ on Darwin, GNU libstdc++ everywhere else
960 flags.CppFlags = append(flags.CppFlags, flags.Toolchain.SystemCppCppflags())
961 flags.LdFlags = append(flags.LdFlags, flags.Toolchain.SystemCppLdflags())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700962 }
963 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700964 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
965 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700966 case "ndk_libc++_shared", "ndk_libc++_static":
967 // TODO(danalbert): This really shouldn't be here...
968 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
969 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
970 // Nothing
971 case "":
972 // None or error.
973 if ctx.Host() {
974 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
975 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700976 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800977 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700978 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800979 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700980 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700981 }
982 default:
Colin Crossfa138792015-04-24 17:31:52 -0700983 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700984 }
985
986 return flags
987}
988
Colin Crosse11befc2015-04-27 17:49:17 -0700989func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
990 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800991
Colin Crossed4cf0b2015-03-26 14:43:45 -0700992 stl := c.stl(ctx)
993 if ctx.Failed() {
994 return depNames
995 }
996
997 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700998 case "libstdc++":
999 if ctx.Device() {
1000 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1001 }
Colin Cross74d1ec02015-04-28 13:30:13 -07001002 case "libc++", "libc++_static":
1003 if stl == "libc++" {
1004 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1005 } else {
1006 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1007 }
1008 if ctx.Device() {
1009 if ctx.Arch().ArchType == common.Arm {
1010 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
1011 }
1012 if c.staticBinary() {
1013 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
1014 } else {
1015 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
1016 }
1017 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001018 case "":
1019 // None or error.
1020 case "ndk_system":
1021 // TODO: Make a system STL prebuilt for the NDK.
1022 // 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 -07001023 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -07001024 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001025 case "ndk_libc++_shared", "ndk_libstlport_shared":
1026 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1027 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
1028 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1029 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001030 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001031 }
1032
Colin Cross74d1ec02015-04-28 13:30:13 -07001033 if ctx.ModuleName() != "libcompiler_rt-extras" {
1034 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1035 }
1036
Colin Crossf6566ed2015-03-24 11:13:38 -07001037 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001038 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001039 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001040 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001041 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1042 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001043
Colin Cross18b6dc52015-04-28 13:20:37 -07001044 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001045 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1046 }
Colin Cross577f6e42015-03-27 18:23:34 -07001047
Colin Crossfa138792015-04-24 17:31:52 -07001048 if c.Properties.Sdk_version != "" {
1049 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001050 depNames.SharedLibs = append(depNames.SharedLibs,
1051 "ndk_libc."+version,
1052 "ndk_libm."+version,
1053 )
1054 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001055 }
1056
Colin Cross21b9a242015-03-24 14:15:58 -07001057 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001058}
1059
Colin Crossed4cf0b2015-03-26 14:43:45 -07001060// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1061type ccLinkedInterface interface {
1062 // Returns true if the build options for the module have selected a static or shared build
1063 buildStatic() bool
1064 buildShared() bool
1065
1066 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001067 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001068
Colin Cross18b6dc52015-04-28 13:20:37 -07001069 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001070 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001071
1072 // Returns whether a module is a static binary
1073 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001074}
1075
1076var _ ccLinkedInterface = (*CCLibrary)(nil)
1077var _ ccLinkedInterface = (*CCBinary)(nil)
1078
Colin Crossfa138792015-04-24 17:31:52 -07001079func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001080 return c.dynamicProperties.VariantIsStatic
1081}
1082
Colin Cross18b6dc52015-04-28 13:20:37 -07001083func (c *CCLinked) staticBinary() bool {
1084 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001085}
1086
Colin Cross18b6dc52015-04-28 13:20:37 -07001087func (c *CCLinked) setStatic(static bool) {
1088 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001089}
1090
Colin Cross28344522015-04-22 13:07:53 -07001091type ccExportedFlagsProducer interface {
1092 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001093}
1094
1095//
1096// Combined static+shared libraries
1097//
1098
Colin Cross7d5136f2015-05-11 13:39:40 -07001099type CCLibraryProperties struct {
1100 BuildStatic bool `blueprint:"mutated"`
1101 BuildShared bool `blueprint:"mutated"`
1102 Static struct {
1103 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001104 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001105 Cflags []string `android:"arch_variant"`
1106 Whole_static_libs []string `android:"arch_variant"`
1107 Static_libs []string `android:"arch_variant"`
1108 Shared_libs []string `android:"arch_variant"`
1109 } `android:"arch_variant"`
1110 Shared struct {
1111 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001112 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001113 Cflags []string `android:"arch_variant"`
1114 Whole_static_libs []string `android:"arch_variant"`
1115 Static_libs []string `android:"arch_variant"`
1116 Shared_libs []string `android:"arch_variant"`
1117 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001118
1119 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001120 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001121 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001122 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001123 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001124 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001125 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001126 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001127}
1128
Colin Cross97ba0732015-03-23 17:50:24 -07001129type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001130 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001131
Colin Cross28344522015-04-22 13:07:53 -07001132 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001133 reuseObjFiles common.Paths
1134 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001135 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001136 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001137 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001138
Colin Cross7d5136f2015-05-11 13:39:40 -07001139 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001140}
1141
Colin Crossed4cf0b2015-03-26 14:43:45 -07001142func (c *CCLibrary) buildStatic() bool {
1143 return c.LibraryProperties.BuildStatic
1144}
1145
1146func (c *CCLibrary) buildShared() bool {
1147 return c.LibraryProperties.BuildShared
1148}
1149
Colin Cross97ba0732015-03-23 17:50:24 -07001150type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001151 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001152 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001153 setReuseFrom(ccLibraryInterface)
1154 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001155 getReuseObjFiles() common.Paths
1156 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001157}
1158
Colin Crossed4cf0b2015-03-26 14:43:45 -07001159var _ ccLibraryInterface = (*CCLibrary)(nil)
1160
Colin Cross97ba0732015-03-23 17:50:24 -07001161func (c *CCLibrary) ccLibrary() *CCLibrary {
1162 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001163}
1164
Colin Cross97ba0732015-03-23 17:50:24 -07001165func NewCCLibrary(library *CCLibrary, module CCModuleType,
1166 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1167
Colin Crossfa138792015-04-24 17:31:52 -07001168 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001169 &library.LibraryProperties)
1170}
1171
1172func CCLibraryFactory() (blueprint.Module, []interface{}) {
1173 module := &CCLibrary{}
1174
1175 module.LibraryProperties.BuildShared = true
1176 module.LibraryProperties.BuildStatic = true
1177
1178 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1179}
1180
Colin Cross0676e2d2015-04-24 17:39:18 -07001181func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001182 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001183 if c.static() {
1184 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1185 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1186 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1187 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001188 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001189 if c.Properties.Sdk_version == "" {
1190 depNames.CrtBegin = "crtbegin_so"
1191 depNames.CrtEnd = "crtend_so"
1192 } else {
1193 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1194 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1195 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001196 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001197 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1198 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1199 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001200 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001201
Dan Willemsen218f6562015-07-08 18:13:11 -07001202 c.systemLibs = c.systemSharedLibs(ctx)
1203
Colin Cross21b9a242015-03-24 14:15:58 -07001204 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001205}
1206
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001207func (c *CCLibrary) outputFile() common.OptionalPath {
1208 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001209}
1210
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001211func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001212 return c.reuseObjFiles
1213}
1214
1215func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1216 c.reuseFrom = reuseFrom
1217}
1218
1219func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1220 return c.reuseFrom
1221}
1222
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001223func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001224 return c.objFiles
1225}
1226
Colin Cross28344522015-04-22 13:07:53 -07001227func (c *CCLibrary) exportedFlags() []string {
1228 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001229}
1230
Colin Cross0676e2d2015-04-24 17:39:18 -07001231func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001232 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001233
Dan Willemsen490fd492015-11-24 17:53:15 -08001234 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1235 // all code is position independent, and then those warnings get promoted to
1236 // errors.
1237 if ctx.HostType() != common.Windows {
1238 flags.CFlags = append(flags.CFlags, "-fPIC")
1239 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001240
Colin Crossd8e780d2015-04-28 17:39:43 -07001241 if c.static() {
1242 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1243 } else {
1244 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1245 }
1246
Colin Cross18b6dc52015-04-28 13:20:37 -07001247 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001248 libName := ctx.ModuleName()
1249 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1250 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001251 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001252 sharedFlag = "-shared"
1253 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001254 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001255 flags.LdFlags = append(flags.LdFlags,
1256 "-nostdlib",
1257 "-Wl,--gc-sections",
1258 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001259 }
Colin Cross97ba0732015-03-23 17:50:24 -07001260
Colin Cross0af4b842015-04-30 16:36:18 -07001261 if ctx.Darwin() {
1262 flags.LdFlags = append(flags.LdFlags,
1263 "-dynamiclib",
1264 "-single_module",
1265 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001266 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001267 )
1268 } else {
1269 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001270 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001271 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001272 )
1273 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001274 }
Colin Cross97ba0732015-03-23 17:50:24 -07001275
1276 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001277}
1278
Colin Cross97ba0732015-03-23 17:50:24 -07001279func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001280 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001281
1282 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001283 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001284 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001285
1286 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001287 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001288
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001289 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001290
Colin Cross0af4b842015-04-30 16:36:18 -07001291 if ctx.Darwin() {
1292 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1293 } else {
1294 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1295 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001296
1297 c.objFiles = objFiles
1298 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001299
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001300 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001301 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001302 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001303
1304 ctx.CheckbuildFile(outputFile)
1305}
1306
Colin Cross97ba0732015-03-23 17:50:24 -07001307func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001308 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001309
1310 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001311 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001312 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001313
1314 objFiles = append(objFiles, objFilesShared...)
1315
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001316 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001317
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001318 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001319
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001320 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1321 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1322 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1323 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001324 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001325 if versionScript.Valid() {
1326 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1327 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001328 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001329 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001330 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1331 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001332 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001333 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1334 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001335 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001336 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1337 }
1338 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001339 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001340 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1341 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001342 if unexportedSymbols.Valid() {
1343 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1344 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001345 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001346 if forceNotWeakSymbols.Valid() {
1347 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1348 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001349 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001350 if forceWeakSymbols.Valid() {
1351 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1352 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001353 }
Colin Crossaee540a2015-07-06 17:48:31 -07001354 }
1355
Colin Cross97ba0732015-03-23 17:50:24 -07001356 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001357 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001358 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001359
1360 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001361 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001362 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001363 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001364}
1365
Colin Cross97ba0732015-03-23 17:50:24 -07001366func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001367 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001368
1369 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001370 if c.getReuseFrom().ccLibrary() == c {
1371 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001372 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001373 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1374 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001375 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001376 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001377 }
1378
Colin Crossed4cf0b2015-03-26 14:43:45 -07001379 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001380 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1381 } else {
1382 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1383 }
1384}
1385
Colin Cross97ba0732015-03-23 17:50:24 -07001386func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001387 // Static libraries do not get installed.
1388}
1389
Colin Cross97ba0732015-03-23 17:50:24 -07001390func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001391 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001392 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001393 installDir = "lib64"
1394 }
1395
Dan Willemsen782a2d12015-12-21 14:55:28 -08001396 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001397}
1398
Colin Cross97ba0732015-03-23 17:50:24 -07001399func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001400 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001401 c.installStaticLibrary(ctx, flags)
1402 } else {
1403 c.installSharedLibrary(ctx, flags)
1404 }
1405}
1406
Colin Cross3f40fa42015-01-30 17:27:36 -08001407//
1408// Objects (for crt*.o)
1409//
1410
Dan Albertc3144b12015-04-28 18:17:56 -07001411type ccObjectProvider interface {
1412 object() *ccObject
1413}
1414
Colin Cross3f40fa42015-01-30 17:27:36 -08001415type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001416 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001417 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001418}
1419
Dan Albertc3144b12015-04-28 18:17:56 -07001420func (c *ccObject) object() *ccObject {
1421 return c
1422}
1423
Colin Cross97ba0732015-03-23 17:50:24 -07001424func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001425 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001426
Colin Crossfa138792015-04-24 17:31:52 -07001427 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001428}
1429
Colin Cross0676e2d2015-04-24 17:39:18 -07001430func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001431 // object files can't have any dynamic dependencies
1432 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001433}
1434
1435func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001437
Colin Cross97ba0732015-03-23 17:50:24 -07001438 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001439
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001440 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001441 if len(objFiles) == 1 {
1442 outputFile = objFiles[0]
1443 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001444 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1445 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1446 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001447 }
1448
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001449 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001450
1451 ctx.CheckbuildFile(outputFile)
1452}
1453
Colin Cross97ba0732015-03-23 17:50:24 -07001454func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001455 // Object files do not get installed.
1456}
1457
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001458func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001459 return c.out
1460}
1461
Dan Albertc3144b12015-04-28 18:17:56 -07001462var _ ccObjectProvider = (*ccObject)(nil)
1463
Colin Cross3f40fa42015-01-30 17:27:36 -08001464//
1465// Executables
1466//
1467
Colin Cross7d5136f2015-05-11 13:39:40 -07001468type CCBinaryProperties struct {
1469 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001470 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001471
1472 // set the name of the output
1473 Stem string `android:"arch_variant"`
1474
1475 // append to the name of the output
1476 Suffix string `android:"arch_variant"`
1477
1478 // if set, add an extra objcopy --prefix-symbols= step
1479 Prefix_symbols string
1480}
1481
Colin Cross97ba0732015-03-23 17:50:24 -07001482type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001483 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001484 out common.Path
1485 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001486 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001487}
1488
Colin Crossed4cf0b2015-03-26 14:43:45 -07001489func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001490 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001491}
1492
1493func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001494 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001495}
1496
Colin Cross97ba0732015-03-23 17:50:24 -07001497func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001498 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001499 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001500 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001501 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001502
1503 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001504}
1505
Colin Cross0676e2d2015-04-24 17:39:18 -07001506func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001507 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001508 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001509 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001510 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001511 depNames.CrtBegin = "crtbegin_static"
1512 } else {
1513 depNames.CrtBegin = "crtbegin_dynamic"
1514 }
1515 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001516 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001517 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001518 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1519 } else {
1520 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1521 }
1522 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001523 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001524
Colin Cross06a931b2015-10-28 17:23:31 -07001525 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001526 if c.stl(ctx) == "libc++_static" {
1527 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1528 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001529 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1530 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1531 // move them to the beginning of deps.LateStaticLibs
1532 var groupLibs []string
1533 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1534 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1535 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1536 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001537 }
Colin Cross21b9a242015-03-24 14:15:58 -07001538 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001539}
1540
Colin Cross97ba0732015-03-23 17:50:24 -07001541func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001542 hod common.HostOrDeviceSupported, multilib common.Multilib,
1543 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001544
Colin Cross1f8f2342015-03-26 16:09:47 -07001545 props = append(props, &binary.BinaryProperties)
1546
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001547 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001548}
1549
Colin Cross97ba0732015-03-23 17:50:24 -07001550func CCBinaryFactory() (blueprint.Module, []interface{}) {
1551 module := &CCBinary{}
1552
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001553 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001554}
1555
Colin Cross6362e272015-10-29 15:25:03 -07001556func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001557 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001558 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001559 }
Colin Cross06a931b2015-10-28 17:23:31 -07001560 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001561 c.dynamicProperties.VariantIsStaticBinary = true
1562 }
1563}
1564
Colin Cross0676e2d2015-04-24 17:39:18 -07001565func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001566 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001567
Dan Willemsen490fd492015-11-24 17:53:15 -08001568 if ctx.Host() {
1569 flags.LdFlags = append(flags.LdFlags, "-pie")
1570 if ctx.HostType() == common.Windows {
1571 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1572 }
1573 }
1574
1575 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1576 // all code is position independent, and then those warnings get promoted to
1577 // errors.
1578 if ctx.HostType() != common.Windows {
1579 flags.CFlags = append(flags.CFlags, "-fpie")
1580 }
Colin Cross97ba0732015-03-23 17:50:24 -07001581
Colin Crossf6566ed2015-03-24 11:13:38 -07001582 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001583 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001584 // Clang driver needs -static to create static executable.
1585 // However, bionic/linker uses -shared to overwrite.
1586 // Linker for x86 targets does not allow coexistance of -static and -shared,
1587 // so we add -static only if -shared is not used.
1588 if !inList("-shared", flags.LdFlags) {
1589 flags.LdFlags = append(flags.LdFlags, "-static")
1590 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001591
Colin Crossed4cf0b2015-03-26 14:43:45 -07001592 flags.LdFlags = append(flags.LdFlags,
1593 "-nostdlib",
1594 "-Bstatic",
1595 "-Wl,--gc-sections",
1596 )
1597
1598 } else {
1599 linker := "/system/bin/linker"
1600 if flags.Toolchain.Is64Bit() {
1601 linker = "/system/bin/linker64"
1602 }
1603
1604 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001605 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001606 "-nostdlib",
1607 "-Bdynamic",
1608 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1609 "-Wl,--gc-sections",
1610 "-Wl,-z,nocopyreloc",
1611 )
1612 }
Colin Cross0af4b842015-04-30 16:36:18 -07001613 } else if ctx.Darwin() {
1614 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001615 }
1616
Colin Cross97ba0732015-03-23 17:50:24 -07001617 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001618}
1619
Colin Cross97ba0732015-03-23 17:50:24 -07001620func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001621 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001622
Colin Cross06a931b2015-10-28 17:23:31 -07001623 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001624 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1625 "from static libs or set static_executable: true")
1626 }
1627
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001628 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001629 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001630 if c.BinaryProperties.Prefix_symbols != "" {
1631 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001632 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001633 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1634 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1635 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001636
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001637 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001638
Colin Cross97ba0732015-03-23 17:50:24 -07001639 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001640 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001641 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001642}
Colin Cross3f40fa42015-01-30 17:27:36 -08001643
Colin Cross97ba0732015-03-23 17:50:24 -07001644func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001645 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001646}
1647
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001648func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001649 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001650 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001651 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001652 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001653}
1654
Colin Cross6002e052015-09-16 16:00:08 -07001655func (c *CCBinary) binary() *CCBinary {
1656 return c
1657}
1658
1659type testPerSrc interface {
1660 binary() *CCBinary
1661 testPerSrc() bool
1662}
1663
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001664var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001665
Colin Cross6362e272015-10-29 15:25:03 -07001666func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001667 if test, ok := mctx.Module().(testPerSrc); ok {
1668 if test.testPerSrc() {
1669 testNames := make([]string, len(test.binary().Properties.Srcs))
1670 for i, src := range test.binary().Properties.Srcs {
1671 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1672 }
1673 tests := mctx.CreateLocalVariations(testNames...)
1674 for i, src := range test.binary().Properties.Srcs {
1675 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001676 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001677 }
1678 }
1679 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001680}
1681
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001682type CCTestProperties struct {
1683 // if set, build against the gtest library. Defaults to true.
1684 Gtest bool
1685
1686 // Create a separate binary for each source file. Useful when there is
1687 // global state that can not be torn down and reset between each test suite.
1688 Test_per_src *bool
1689}
1690
Colin Cross9ffb4f52015-04-24 17:48:09 -07001691type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001692 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001693
1694 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001695}
1696
Colin Cross9ffb4f52015-04-24 17:48:09 -07001697func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001698 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001699 if !c.TestProperties.Gtest {
1700 return flags
1701 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001702
Colin Cross97ba0732015-03-23 17:50:24 -07001703 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001704 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001705 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001706
1707 if ctx.HostType() == common.Windows {
1708 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1709 } else {
1710 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1711 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1712 }
1713 } else {
1714 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001715 }
1716
1717 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001718 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001719 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001720
Colin Cross21b9a242015-03-24 14:15:58 -07001721 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001722}
1723
Colin Cross9ffb4f52015-04-24 17:48:09 -07001724func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001725 if c.TestProperties.Gtest {
1726 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1727 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001728 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001729 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001730}
1731
Dan Willemsen782a2d12015-12-21 14:55:28 -08001732func (c *CCTest) InstallInData() bool {
1733 return true
1734}
1735
Colin Cross9ffb4f52015-04-24 17:48:09 -07001736func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001737 installDir := "nativetest"
1738 if flags.Toolchain.Is64Bit() {
1739 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001740 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001741 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1742}
1743
1744func (c *CCTest) testPerSrc() bool {
1745 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001746}
1747
Colin Cross9ffb4f52015-04-24 17:48:09 -07001748func NewCCTest(test *CCTest, module CCModuleType,
1749 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1750
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001751 props = append(props, &test.TestProperties)
1752
1753 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001754}
1755
1756func CCTestFactory() (blueprint.Module, []interface{}) {
1757 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001758 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001759
1760 return NewCCTest(module, module, common.HostAndDeviceSupported)
1761}
1762
Colin Cross2ba19d92015-05-07 15:44:20 -07001763type CCBenchmark struct {
1764 CCBinary
1765}
1766
1767func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1768 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001769 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001770 return depNames
1771}
1772
Dan Willemsen782a2d12015-12-21 14:55:28 -08001773func (c *CCBenchmark) InstallInData() bool {
1774 return true
1775}
1776
Colin Cross2ba19d92015-05-07 15:44:20 -07001777func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1778 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001779 installDir := "nativetest"
1780 if flags.Toolchain.Is64Bit() {
1781 installDir = "nativetest64"
1782 }
1783 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001784 } else {
1785 c.CCBinary.installModule(ctx, flags)
1786 }
1787}
1788
1789func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1790 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1791
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001792 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001793}
1794
1795func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1796 module := &CCBenchmark{}
1797
1798 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1799}
1800
Colin Cross3f40fa42015-01-30 17:27:36 -08001801//
1802// Static library
1803//
1804
Colin Cross97ba0732015-03-23 17:50:24 -07001805func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1806 module := &CCLibrary{}
1807 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001808
Colin Cross97ba0732015-03-23 17:50:24 -07001809 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001810}
1811
1812//
1813// Shared libraries
1814//
1815
Colin Cross97ba0732015-03-23 17:50:24 -07001816func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1817 module := &CCLibrary{}
1818 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001819
Colin Cross97ba0732015-03-23 17:50:24 -07001820 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001821}
1822
1823//
1824// Host static library
1825//
1826
Colin Cross97ba0732015-03-23 17:50:24 -07001827func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1828 module := &CCLibrary{}
1829 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001830
Colin Cross97ba0732015-03-23 17:50:24 -07001831 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001832}
1833
1834//
1835// Host Shared libraries
1836//
1837
Colin Cross97ba0732015-03-23 17:50:24 -07001838func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1839 module := &CCLibrary{}
1840 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001841
Colin Cross97ba0732015-03-23 17:50:24 -07001842 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001843}
1844
1845//
1846// Host Binaries
1847//
1848
Colin Cross97ba0732015-03-23 17:50:24 -07001849func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1850 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001851
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001852 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001853}
1854
1855//
Colin Cross1f8f2342015-03-26 16:09:47 -07001856// Host Tests
1857//
1858
1859func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001860 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001861 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001862}
1863
1864//
Colin Cross2ba19d92015-05-07 15:44:20 -07001865// Host Benchmarks
1866//
1867
1868func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1869 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001870 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001871}
1872
1873//
Colin Crosscfad1192015-11-02 16:43:11 -08001874// Defaults
1875//
1876type CCDefaults struct {
1877 common.AndroidModuleBase
1878 common.DefaultsModule
1879}
1880
1881func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1882}
1883
1884func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1885 module := &CCDefaults{}
1886
1887 propertyStructs := []interface{}{
1888 &CCBaseProperties{},
1889 &CCLibraryProperties{},
1890 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001891 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001892 &CCUnusedProperties{},
1893 }
1894
Dan Willemsen218f6562015-07-08 18:13:11 -07001895 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1896 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001897
1898 return common.InitDefaultsModule(module, module, propertyStructs...)
1899}
1900
1901//
Colin Cross3f40fa42015-01-30 17:27:36 -08001902// Device libraries shipped with gcc
1903//
1904
1905type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001906 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001907}
1908
Colin Cross0676e2d2015-04-24 17:39:18 -07001909func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001910 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001911 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001912}
1913
Colin Cross97ba0732015-03-23 17:50:24 -07001914func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001915 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001916
Colin Cross97ba0732015-03-23 17:50:24 -07001917 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001918 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001919
Colin Crossfa138792015-04-24 17:31:52 -07001920 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001921 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001922}
1923
1924func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001925 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001926
1927 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001928 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001929
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001930 if flags.Clang {
1931 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1932 }
1933
Colin Cross3f40fa42015-01-30 17:27:36 -08001934 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1935
1936 c.out = outputFile
1937
1938 ctx.CheckbuildFile(outputFile)
1939}
1940
Colin Cross97ba0732015-03-23 17:50:24 -07001941func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001942 // Toolchain libraries do not get installed.
1943}
1944
Dan Albertbe961682015-03-18 23:38:50 -07001945// NDK prebuilt libraries.
1946//
1947// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1948// either (with the exception of the shared STLs, which are installed to the app's directory rather
1949// than to the system image).
1950
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001951func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1952 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1953 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001954}
1955
Dan Albertc3144b12015-04-28 18:17:56 -07001956func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001957 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001958
1959 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1960 // We want to translate to just NAME.EXT
1961 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1962 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001963 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001964}
1965
1966type ndkPrebuiltObject struct {
1967 ccObject
1968}
1969
Dan Albertc3144b12015-04-28 18:17:56 -07001970func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1971 // NDK objects can't have any dependencies
1972 return CCDeps{}
1973}
1974
1975func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1976 module := &ndkPrebuiltObject{}
1977 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1978}
1979
1980func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001981 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001982 // A null build step, but it sets up the output path.
1983 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1984 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1985 }
1986
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001987 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001988}
1989
1990func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1991 // Objects do not get installed.
1992}
1993
1994var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1995
Dan Albertbe961682015-03-18 23:38:50 -07001996type ndkPrebuiltLibrary struct {
1997 CCLibrary
1998}
1999
Colin Cross0676e2d2015-04-24 17:39:18 -07002000func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07002001 // NDK libraries can't have any dependencies
2002 return CCDeps{}
2003}
2004
2005func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2006 module := &ndkPrebuiltLibrary{}
2007 module.LibraryProperties.BuildShared = true
2008 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2009}
2010
2011func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002012 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002013 // A null build step, but it sets up the output path.
2014 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2015 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2016 }
2017
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002018 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
2019 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002020
Dan Willemsen490fd492015-11-24 17:53:15 -08002021 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07002022 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07002023}
2024
2025func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07002026 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07002027}
2028
2029// The NDK STLs are slightly different from the prebuilt system libraries:
2030// * Are not specific to each platform version.
2031// * The libraries are not in a predictable location for each STL.
2032
2033type ndkPrebuiltStl struct {
2034 ndkPrebuiltLibrary
2035}
2036
2037type ndkPrebuiltStaticStl struct {
2038 ndkPrebuiltStl
2039}
2040
2041type ndkPrebuiltSharedStl struct {
2042 ndkPrebuiltStl
2043}
2044
2045func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2046 module := &ndkPrebuiltSharedStl{}
2047 module.LibraryProperties.BuildShared = true
2048 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2049}
2050
2051func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2052 module := &ndkPrebuiltStaticStl{}
2053 module.LibraryProperties.BuildStatic = true
2054 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2055}
2056
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002057func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002058 gccVersion := toolchain.GccVersion()
2059 var libDir string
2060 switch stl {
2061 case "libstlport":
2062 libDir = "cxx-stl/stlport/libs"
2063 case "libc++":
2064 libDir = "cxx-stl/llvm-libc++/libs"
2065 case "libgnustl":
2066 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2067 }
2068
2069 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002070 ndkSrcRoot := "prebuilts/ndk/current/sources"
2071 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002072 }
2073
2074 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002075 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002076}
2077
2078func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002079 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002080 // A null build step, but it sets up the output path.
2081 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2082 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2083 }
2084
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002085 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002086 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002087
2088 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002089 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002090 if c.LibraryProperties.BuildStatic {
2091 libExt = staticLibraryExtension
2092 }
2093
2094 stlName := strings.TrimSuffix(libName, "_shared")
2095 stlName = strings.TrimSuffix(stlName, "_static")
2096 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002097 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002098}
2099
Colin Cross6362e272015-10-29 15:25:03 -07002100func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002101 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002102 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002103 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002104 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002105 modules[0].(ccLinkedInterface).setStatic(true)
2106 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002107 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002108 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002109 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002110 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002111 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002112 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002113 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002114 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002115 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002116
2117 if _, ok := c.(ccLibraryInterface); ok {
2118 reuseFrom := modules[0].(ccLibraryInterface)
2119 for _, m := range modules {
2120 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002121 }
2122 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002123 }
2124}
Colin Cross74d1ec02015-04-28 13:30:13 -07002125
2126// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2127// modifies the slice contents in place, and returns a subslice of the original slice
2128func lastUniqueElements(list []string) []string {
2129 totalSkip := 0
2130 for i := len(list) - 1; i >= totalSkip; i-- {
2131 skip := 0
2132 for j := i - 1; j >= totalSkip; j-- {
2133 if list[i] == list[j] {
2134 skip++
2135 } else {
2136 list[j+skip] = list[j]
2137 }
2138 }
2139 totalSkip += skip
2140 }
2141 return list[totalSkip:]
2142}
Colin Cross06a931b2015-10-28 17:23:31 -07002143
2144var Bool = proptools.Bool