blob: e091be3f26e3dd5a1f657d7c1e60e5483b9d374d [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 Willemseneb371e52016-03-11 12:44:29 -0800156 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2658975/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() {
Dan Willemsen6553f5e2016-03-10 18:14:25 -0800794 if missingDeps := staticLib.getWholeStaticMissingDeps(); missingDeps != nil {
795 postfix := " (required by " + ctx.OtherModuleName(m) + ")"
796 for i := range missingDeps {
797 missingDeps[i] += postfix
798 }
799 ctx.AddMissingDependencies(missingDeps)
800 }
Colin Cross21b9a242015-03-24 14:15:58 -0700801 depPaths.WholeStaticLibObjFiles =
802 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
803 } else {
804 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
805 }
806 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800807
Colin Cross28344522015-04-22 13:07:53 -0700808 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
809 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700810
Colin Cross28344522015-04-22 13:07:53 -0700811 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
812 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700813
Colin Cross28344522015-04-22 13:07:53 -0700814 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
815 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700816
817 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700818 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700819 otherName := ctx.OtherModuleName(m)
820 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700821 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700822 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700823 }
824 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700825 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700826 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700827 }
828 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700829 output := obj.object().outputFile()
830 if output.Valid() {
831 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
832 } else {
833 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
834 }
Colin Cross21b9a242015-03-24 14:15:58 -0700835 }
836 }
837 })
838
839 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800840}
841
Colin Cross7d5136f2015-05-11 13:39:40 -0700842type ccLinkedProperties struct {
843 VariantIsShared bool `blueprint:"mutated"`
844 VariantIsStatic bool `blueprint:"mutated"`
845 VariantIsStaticBinary bool `blueprint:"mutated"`
846}
847
Colin Crossfa138792015-04-24 17:31:52 -0700848// CCLinked contains the properties and members used by libraries and executables
849type CCLinked struct {
850 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700851 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800852}
853
Colin Crossfa138792015-04-24 17:31:52 -0700854func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700855 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
856
Colin Crossed4cf0b2015-03-26 14:43:45 -0700857 props = append(props, &dynamic.dynamicProperties)
858
Colin Crossfa138792015-04-24 17:31:52 -0700859 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700860}
861
Colin Crossfa138792015-04-24 17:31:52 -0700862func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700863 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700864 return c.Properties.System_shared_libs
865 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700866 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700867 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700868 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800869 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800870}
871
Colin Crossfa138792015-04-24 17:31:52 -0700872func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
873 if c.Properties.Sdk_version != "" && ctx.Device() {
874 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700875 case "":
876 return "ndk_system"
877 case "c++_shared", "c++_static",
878 "stlport_shared", "stlport_static",
879 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700880 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700881 default:
Colin Crossfa138792015-04-24 17:31:52 -0700882 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700883 return ""
884 }
885 }
886
Dan Willemsen490fd492015-11-24 17:53:15 -0800887 if ctx.HostType() == common.Windows {
888 switch c.Properties.Stl {
889 case "libc++", "libc++_static", "libstdc++", "":
890 // libc++ is not supported on mingw
891 return "libstdc++"
892 case "none":
893 return ""
894 default:
895 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
896 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700897 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800898 } else {
899 switch c.Properties.Stl {
900 case "libc++", "libc++_static",
901 "libstdc++":
902 return c.Properties.Stl
903 case "none":
904 return ""
905 case "":
906 if c.static() {
907 return "libc++_static"
908 } else {
909 return "libc++"
910 }
911 default:
912 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
913 return ""
914 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700915 }
916}
917
Dan Willemsen490fd492015-11-24 17:53:15 -0800918var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700919
920func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800921 hostDynamicGccLibs = map[common.HostType][]string{
922 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
923 common.Darwin: []string{"-lc", "-lSystem"},
924 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
925 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
926 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
927 "-lmsvcrt"},
928 }
929 hostStaticGccLibs = map[common.HostType][]string{
930 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
931 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
932 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700933 }
934}
Colin Cross712fc022015-04-27 11:13:34 -0700935
Colin Crosse11befc2015-04-27 17:49:17 -0700936func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700937 stl := c.stl(ctx)
938 if ctx.Failed() {
939 return flags
940 }
941
942 switch stl {
943 case "libc++", "libc++_static":
944 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700945 if ctx.Host() {
946 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
947 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Dan Willemsenb9553362016-03-03 19:24:03 -0800948 flags.LdFlags = append(flags.LdFlags, "-lpthread", "-lm")
Colin Cross18b6dc52015-04-28 13:20:37 -0700949 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800950 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700951 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800952 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700953 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700954 } else {
955 if ctx.Arch().ArchType == common.Arm {
956 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
957 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700958 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700959 case "libstdc++":
960 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
961 // tree is in good enough shape to not need it.
962 // Host builds will use GNU libstdc++.
963 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700964 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Dan Willemsen282a4b02016-03-09 10:30:22 -0800965 } else {
966 // Host builds will use the system C++. libc++ on Darwin, GNU libstdc++ everywhere else
967 flags.CppFlags = append(flags.CppFlags, flags.Toolchain.SystemCppCppflags())
968 flags.LdFlags = append(flags.LdFlags, flags.Toolchain.SystemCppLdflags())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700969 }
970 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700971 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
972 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700973 case "ndk_libc++_shared", "ndk_libc++_static":
974 // TODO(danalbert): This really shouldn't be here...
975 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
976 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
977 // Nothing
978 case "":
979 // None or error.
980 if ctx.Host() {
981 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
982 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700983 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800984 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700985 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800986 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700987 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700988 }
989 default:
Colin Crossfa138792015-04-24 17:31:52 -0700990 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700991 }
992
993 return flags
994}
995
Colin Crosse11befc2015-04-27 17:49:17 -0700996func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
997 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800998
Colin Crossed4cf0b2015-03-26 14:43:45 -0700999 stl := c.stl(ctx)
1000 if ctx.Failed() {
1001 return depNames
1002 }
1003
1004 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001005 case "libstdc++":
1006 if ctx.Device() {
1007 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1008 }
Colin Cross74d1ec02015-04-28 13:30:13 -07001009 case "libc++", "libc++_static":
1010 if stl == "libc++" {
1011 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1012 } else {
1013 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1014 }
1015 if ctx.Device() {
1016 if ctx.Arch().ArchType == common.Arm {
1017 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
1018 }
1019 if c.staticBinary() {
1020 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
1021 } else {
1022 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
1023 }
1024 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001025 case "":
1026 // None or error.
1027 case "ndk_system":
1028 // TODO: Make a system STL prebuilt for the NDK.
1029 // 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 -07001030 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -07001031 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001032 case "ndk_libc++_shared", "ndk_libstlport_shared":
1033 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1034 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
1035 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1036 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001037 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001038 }
1039
Colin Cross74d1ec02015-04-28 13:30:13 -07001040 if ctx.ModuleName() != "libcompiler_rt-extras" {
1041 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1042 }
1043
Colin Crossf6566ed2015-03-24 11:13:38 -07001044 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001045 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001046 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001047 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001048 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1049 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001050
Colin Cross18b6dc52015-04-28 13:20:37 -07001051 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001052 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1053 }
Colin Cross577f6e42015-03-27 18:23:34 -07001054
Colin Crossfa138792015-04-24 17:31:52 -07001055 if c.Properties.Sdk_version != "" {
1056 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001057 depNames.SharedLibs = append(depNames.SharedLibs,
1058 "ndk_libc."+version,
1059 "ndk_libm."+version,
1060 )
1061 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001062 }
1063
Colin Cross21b9a242015-03-24 14:15:58 -07001064 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001065}
1066
Colin Crossed4cf0b2015-03-26 14:43:45 -07001067// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1068type ccLinkedInterface interface {
1069 // Returns true if the build options for the module have selected a static or shared build
1070 buildStatic() bool
1071 buildShared() bool
1072
1073 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001074 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001075
Colin Cross18b6dc52015-04-28 13:20:37 -07001076 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001077 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001078
1079 // Returns whether a module is a static binary
1080 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001081}
1082
1083var _ ccLinkedInterface = (*CCLibrary)(nil)
1084var _ ccLinkedInterface = (*CCBinary)(nil)
1085
Colin Crossfa138792015-04-24 17:31:52 -07001086func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001087 return c.dynamicProperties.VariantIsStatic
1088}
1089
Colin Cross18b6dc52015-04-28 13:20:37 -07001090func (c *CCLinked) staticBinary() bool {
1091 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001092}
1093
Colin Cross18b6dc52015-04-28 13:20:37 -07001094func (c *CCLinked) setStatic(static bool) {
1095 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001096}
1097
Colin Cross28344522015-04-22 13:07:53 -07001098type ccExportedFlagsProducer interface {
1099 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001100}
1101
1102//
1103// Combined static+shared libraries
1104//
1105
Colin Cross7d5136f2015-05-11 13:39:40 -07001106type CCLibraryProperties struct {
1107 BuildStatic bool `blueprint:"mutated"`
1108 BuildShared bool `blueprint:"mutated"`
1109 Static struct {
1110 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001111 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001112 Cflags []string `android:"arch_variant"`
1113 Whole_static_libs []string `android:"arch_variant"`
1114 Static_libs []string `android:"arch_variant"`
1115 Shared_libs []string `android:"arch_variant"`
1116 } `android:"arch_variant"`
1117 Shared struct {
1118 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001119 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001120 Cflags []string `android:"arch_variant"`
1121 Whole_static_libs []string `android:"arch_variant"`
1122 Static_libs []string `android:"arch_variant"`
1123 Shared_libs []string `android:"arch_variant"`
1124 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001125
1126 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001127 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001128 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001129 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001130 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001131 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001132 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001133 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001134}
1135
Colin Cross97ba0732015-03-23 17:50:24 -07001136type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001137 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001138
Colin Cross28344522015-04-22 13:07:53 -07001139 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001140 reuseObjFiles common.Paths
1141 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001142 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001143 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001144 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001145
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001146 // If we're used as a whole_static_lib, our missing dependencies need
1147 // to be given
1148 wholeStaticMissingDeps []string
1149
Colin Cross7d5136f2015-05-11 13:39:40 -07001150 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001151}
1152
Colin Crossed4cf0b2015-03-26 14:43:45 -07001153func (c *CCLibrary) buildStatic() bool {
1154 return c.LibraryProperties.BuildStatic
1155}
1156
1157func (c *CCLibrary) buildShared() bool {
1158 return c.LibraryProperties.BuildShared
1159}
1160
Colin Cross97ba0732015-03-23 17:50:24 -07001161type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001162 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001163 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001164 setReuseFrom(ccLibraryInterface)
1165 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001166 getReuseObjFiles() common.Paths
1167 allObjFiles() common.Paths
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001168 getWholeStaticMissingDeps() []string
Colin Crossc472d572015-03-17 15:06:21 -07001169}
1170
Colin Crossed4cf0b2015-03-26 14:43:45 -07001171var _ ccLibraryInterface = (*CCLibrary)(nil)
1172
Colin Cross97ba0732015-03-23 17:50:24 -07001173func (c *CCLibrary) ccLibrary() *CCLibrary {
1174 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001175}
1176
Colin Cross97ba0732015-03-23 17:50:24 -07001177func NewCCLibrary(library *CCLibrary, module CCModuleType,
1178 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1179
Colin Crossfa138792015-04-24 17:31:52 -07001180 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001181 &library.LibraryProperties)
1182}
1183
1184func CCLibraryFactory() (blueprint.Module, []interface{}) {
1185 module := &CCLibrary{}
1186
1187 module.LibraryProperties.BuildShared = true
1188 module.LibraryProperties.BuildStatic = true
1189
1190 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1191}
1192
Colin Cross0676e2d2015-04-24 17:39:18 -07001193func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001194 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001195 if c.static() {
1196 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1197 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1198 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1199 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001200 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001201 if c.Properties.Sdk_version == "" {
1202 depNames.CrtBegin = "crtbegin_so"
1203 depNames.CrtEnd = "crtend_so"
1204 } else {
1205 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1206 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1207 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001208 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001209 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1210 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1211 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001212 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001213
Dan Willemsen218f6562015-07-08 18:13:11 -07001214 c.systemLibs = c.systemSharedLibs(ctx)
1215
Colin Cross21b9a242015-03-24 14:15:58 -07001216 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001217}
1218
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001219func (c *CCLibrary) outputFile() common.OptionalPath {
1220 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001221}
1222
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001223func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001224 return c.reuseObjFiles
1225}
1226
1227func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1228 c.reuseFrom = reuseFrom
1229}
1230
1231func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1232 return c.reuseFrom
1233}
1234
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001235func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001236 return c.objFiles
1237}
1238
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001239func (c *CCLibrary) getWholeStaticMissingDeps() []string {
1240 return c.wholeStaticMissingDeps
1241}
1242
Colin Cross28344522015-04-22 13:07:53 -07001243func (c *CCLibrary) exportedFlags() []string {
1244 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001245}
1246
Colin Cross0676e2d2015-04-24 17:39:18 -07001247func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001248 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001249
Dan Willemsen490fd492015-11-24 17:53:15 -08001250 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1251 // all code is position independent, and then those warnings get promoted to
1252 // errors.
1253 if ctx.HostType() != common.Windows {
1254 flags.CFlags = append(flags.CFlags, "-fPIC")
1255 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001256
Colin Crossd8e780d2015-04-28 17:39:43 -07001257 if c.static() {
1258 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1259 } else {
1260 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1261 }
1262
Colin Cross18b6dc52015-04-28 13:20:37 -07001263 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001264 libName := ctx.ModuleName()
1265 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1266 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001267 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001268 sharedFlag = "-shared"
1269 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001270 if ctx.Device() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001271 flags.LdFlags = append(flags.LdFlags,
1272 "-nostdlib",
1273 "-Wl,--gc-sections",
1274 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001275 }
Colin Cross97ba0732015-03-23 17:50:24 -07001276
Colin Cross0af4b842015-04-30 16:36:18 -07001277 if ctx.Darwin() {
1278 flags.LdFlags = append(flags.LdFlags,
1279 "-dynamiclib",
1280 "-single_module",
1281 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001282 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001283 )
1284 } else {
1285 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001286 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001287 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001288 )
1289 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001290 }
Colin Cross97ba0732015-03-23 17:50:24 -07001291
1292 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001293}
1294
Colin Cross97ba0732015-03-23 17:50:24 -07001295func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001296 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001297
1298 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001299 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001300 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001301
1302 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001303 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001304
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001305 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001306
Colin Cross0af4b842015-04-30 16:36:18 -07001307 if ctx.Darwin() {
1308 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1309 } else {
1310 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1311 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001312
Dan Willemsen6553f5e2016-03-10 18:14:25 -08001313 c.wholeStaticMissingDeps = ctx.GetMissingDependencies()
1314
Colin Cross3f40fa42015-01-30 17:27:36 -08001315 c.objFiles = objFiles
1316 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001317
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001318 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001319 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001320 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001321
1322 ctx.CheckbuildFile(outputFile)
1323}
1324
Colin Cross97ba0732015-03-23 17:50:24 -07001325func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001326 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001327
1328 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001329 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001330 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001331
1332 objFiles = append(objFiles, objFilesShared...)
1333
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001334 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001335
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001336 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001337
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001338 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1339 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1340 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1341 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001342 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001343 if versionScript.Valid() {
1344 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1345 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001346 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001347 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001348 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1349 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001350 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001351 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1352 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001353 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001354 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1355 }
1356 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001357 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001358 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1359 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001360 if unexportedSymbols.Valid() {
1361 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1362 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001363 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001364 if forceNotWeakSymbols.Valid() {
1365 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1366 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001367 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001368 if forceWeakSymbols.Valid() {
1369 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1370 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001371 }
Colin Crossaee540a2015-07-06 17:48:31 -07001372 }
1373
Colin Cross97ba0732015-03-23 17:50:24 -07001374 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001375 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001376 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001377
1378 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001379 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001380 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001381 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001382}
1383
Colin Cross97ba0732015-03-23 17:50:24 -07001384func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001385 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001386
1387 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001388 if c.getReuseFrom().ccLibrary() == c {
1389 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001390 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001391 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1392 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001393 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001394 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001395 }
1396
Colin Crossed4cf0b2015-03-26 14:43:45 -07001397 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001398 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1399 } else {
1400 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1401 }
1402}
1403
Colin Cross97ba0732015-03-23 17:50:24 -07001404func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001405 // Static libraries do not get installed.
1406}
1407
Colin Cross97ba0732015-03-23 17:50:24 -07001408func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001409 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001410 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001411 installDir = "lib64"
1412 }
1413
Dan Willemsen782a2d12015-12-21 14:55:28 -08001414 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001415}
1416
Colin Cross97ba0732015-03-23 17:50:24 -07001417func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001418 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001419 c.installStaticLibrary(ctx, flags)
1420 } else {
1421 c.installSharedLibrary(ctx, flags)
1422 }
1423}
1424
Colin Cross3f40fa42015-01-30 17:27:36 -08001425//
1426// Objects (for crt*.o)
1427//
1428
Dan Albertc3144b12015-04-28 18:17:56 -07001429type ccObjectProvider interface {
1430 object() *ccObject
1431}
1432
Colin Cross3f40fa42015-01-30 17:27:36 -08001433type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001434 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001435 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001436}
1437
Dan Albertc3144b12015-04-28 18:17:56 -07001438func (c *ccObject) object() *ccObject {
1439 return c
1440}
1441
Colin Cross97ba0732015-03-23 17:50:24 -07001442func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001443 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001444
Colin Crossfa138792015-04-24 17:31:52 -07001445 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001446}
1447
Colin Cross0676e2d2015-04-24 17:39:18 -07001448func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001449 // object files can't have any dynamic dependencies
1450 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001451}
1452
1453func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001454 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001455
Colin Cross97ba0732015-03-23 17:50:24 -07001456 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001457
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001458 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001459 if len(objFiles) == 1 {
1460 outputFile = objFiles[0]
1461 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001462 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1463 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1464 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001465 }
1466
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001467 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001468
1469 ctx.CheckbuildFile(outputFile)
1470}
1471
Colin Cross97ba0732015-03-23 17:50:24 -07001472func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001473 // Object files do not get installed.
1474}
1475
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001476func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001477 return c.out
1478}
1479
Dan Albertc3144b12015-04-28 18:17:56 -07001480var _ ccObjectProvider = (*ccObject)(nil)
1481
Colin Cross3f40fa42015-01-30 17:27:36 -08001482//
1483// Executables
1484//
1485
Colin Cross7d5136f2015-05-11 13:39:40 -07001486type CCBinaryProperties struct {
1487 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001488 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001489
1490 // set the name of the output
1491 Stem string `android:"arch_variant"`
1492
1493 // append to the name of the output
1494 Suffix string `android:"arch_variant"`
1495
1496 // if set, add an extra objcopy --prefix-symbols= step
1497 Prefix_symbols string
1498}
1499
Colin Cross97ba0732015-03-23 17:50:24 -07001500type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001501 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001502 out common.Path
1503 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001504 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001505}
1506
Colin Crossed4cf0b2015-03-26 14:43:45 -07001507func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001508 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001509}
1510
1511func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001512 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001513}
1514
Colin Cross97ba0732015-03-23 17:50:24 -07001515func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001516 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001517 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001518 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001519 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001520
1521 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001522}
1523
Colin Cross0676e2d2015-04-24 17:39:18 -07001524func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001525 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001526 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001527 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001528 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001529 depNames.CrtBegin = "crtbegin_static"
1530 } else {
1531 depNames.CrtBegin = "crtbegin_dynamic"
1532 }
1533 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001534 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001535 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001536 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1537 } else {
1538 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1539 }
1540 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001541 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001542
Colin Cross06a931b2015-10-28 17:23:31 -07001543 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001544 if c.stl(ctx) == "libc++_static" {
1545 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1546 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001547 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1548 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1549 // move them to the beginning of deps.LateStaticLibs
1550 var groupLibs []string
1551 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1552 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1553 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1554 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001555 }
Colin Cross21b9a242015-03-24 14:15:58 -07001556 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001557}
1558
Colin Cross97ba0732015-03-23 17:50:24 -07001559func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001560 hod common.HostOrDeviceSupported, multilib common.Multilib,
1561 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001562
Colin Cross1f8f2342015-03-26 16:09:47 -07001563 props = append(props, &binary.BinaryProperties)
1564
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001565 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001566}
1567
Colin Cross97ba0732015-03-23 17:50:24 -07001568func CCBinaryFactory() (blueprint.Module, []interface{}) {
1569 module := &CCBinary{}
1570
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001571 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001572}
1573
Colin Cross6362e272015-10-29 15:25:03 -07001574func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001575 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001576 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001577 }
Colin Cross06a931b2015-10-28 17:23:31 -07001578 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001579 c.dynamicProperties.VariantIsStaticBinary = true
1580 }
1581}
1582
Colin Cross0676e2d2015-04-24 17:39:18 -07001583func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001584 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001585
Dan Willemsen490fd492015-11-24 17:53:15 -08001586 if ctx.Host() {
1587 flags.LdFlags = append(flags.LdFlags, "-pie")
1588 if ctx.HostType() == common.Windows {
1589 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1590 }
1591 }
1592
1593 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1594 // all code is position independent, and then those warnings get promoted to
1595 // errors.
1596 if ctx.HostType() != common.Windows {
1597 flags.CFlags = append(flags.CFlags, "-fpie")
1598 }
Colin Cross97ba0732015-03-23 17:50:24 -07001599
Colin Crossf6566ed2015-03-24 11:13:38 -07001600 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001601 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001602 // Clang driver needs -static to create static executable.
1603 // However, bionic/linker uses -shared to overwrite.
1604 // Linker for x86 targets does not allow coexistance of -static and -shared,
1605 // so we add -static only if -shared is not used.
1606 if !inList("-shared", flags.LdFlags) {
1607 flags.LdFlags = append(flags.LdFlags, "-static")
1608 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001609
Colin Crossed4cf0b2015-03-26 14:43:45 -07001610 flags.LdFlags = append(flags.LdFlags,
1611 "-nostdlib",
1612 "-Bstatic",
1613 "-Wl,--gc-sections",
1614 )
1615
1616 } else {
1617 linker := "/system/bin/linker"
1618 if flags.Toolchain.Is64Bit() {
1619 linker = "/system/bin/linker64"
1620 }
1621
1622 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001623 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001624 "-nostdlib",
1625 "-Bdynamic",
1626 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1627 "-Wl,--gc-sections",
1628 "-Wl,-z,nocopyreloc",
1629 )
1630 }
Colin Cross0af4b842015-04-30 16:36:18 -07001631 } else if ctx.Darwin() {
1632 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001633 }
1634
Colin Cross97ba0732015-03-23 17:50:24 -07001635 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001636}
1637
Colin Cross97ba0732015-03-23 17:50:24 -07001638func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001639 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001640
Colin Cross06a931b2015-10-28 17:23:31 -07001641 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001642 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1643 "from static libs or set static_executable: true")
1644 }
1645
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001646 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001647 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001648 if c.BinaryProperties.Prefix_symbols != "" {
1649 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001650 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001651 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1652 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1653 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001654
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001655 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001656
Colin Cross97ba0732015-03-23 17:50:24 -07001657 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001658 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001659 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001660}
Colin Cross3f40fa42015-01-30 17:27:36 -08001661
Colin Cross97ba0732015-03-23 17:50:24 -07001662func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001663 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001664}
1665
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001666func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001667 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001668 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001669 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001670 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001671}
1672
Colin Cross6002e052015-09-16 16:00:08 -07001673func (c *CCBinary) binary() *CCBinary {
1674 return c
1675}
1676
1677type testPerSrc interface {
1678 binary() *CCBinary
1679 testPerSrc() bool
1680}
1681
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001682var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001683
Colin Cross6362e272015-10-29 15:25:03 -07001684func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001685 if test, ok := mctx.Module().(testPerSrc); ok {
1686 if test.testPerSrc() {
1687 testNames := make([]string, len(test.binary().Properties.Srcs))
1688 for i, src := range test.binary().Properties.Srcs {
1689 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1690 }
1691 tests := mctx.CreateLocalVariations(testNames...)
1692 for i, src := range test.binary().Properties.Srcs {
1693 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001694 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001695 }
1696 }
1697 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001698}
1699
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001700type CCTestProperties struct {
1701 // if set, build against the gtest library. Defaults to true.
1702 Gtest bool
1703
1704 // Create a separate binary for each source file. Useful when there is
1705 // global state that can not be torn down and reset between each test suite.
1706 Test_per_src *bool
1707}
1708
Colin Cross9ffb4f52015-04-24 17:48:09 -07001709type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001710 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001711
1712 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001713}
1714
Colin Cross9ffb4f52015-04-24 17:48:09 -07001715func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001716 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001717 if !c.TestProperties.Gtest {
1718 return flags
1719 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001720
Colin Cross97ba0732015-03-23 17:50:24 -07001721 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001722 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001723 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001724
1725 if ctx.HostType() == common.Windows {
1726 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1727 } else {
1728 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1729 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1730 }
1731 } else {
1732 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001733 }
1734
1735 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001736 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001737 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001738
Colin Cross21b9a242015-03-24 14:15:58 -07001739 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001740}
1741
Colin Cross9ffb4f52015-04-24 17:48:09 -07001742func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001743 if c.TestProperties.Gtest {
1744 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1745 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001746 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001747 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001748}
1749
Dan Willemsen782a2d12015-12-21 14:55:28 -08001750func (c *CCTest) InstallInData() bool {
1751 return true
1752}
1753
Colin Cross9ffb4f52015-04-24 17:48:09 -07001754func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001755 installDir := "nativetest"
1756 if flags.Toolchain.Is64Bit() {
1757 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001758 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001759 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1760}
1761
1762func (c *CCTest) testPerSrc() bool {
1763 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001764}
1765
Colin Cross9ffb4f52015-04-24 17:48:09 -07001766func NewCCTest(test *CCTest, module CCModuleType,
1767 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1768
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001769 props = append(props, &test.TestProperties)
1770
1771 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001772}
1773
1774func CCTestFactory() (blueprint.Module, []interface{}) {
1775 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001776 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001777
1778 return NewCCTest(module, module, common.HostAndDeviceSupported)
1779}
1780
Colin Cross2ba19d92015-05-07 15:44:20 -07001781type CCBenchmark struct {
1782 CCBinary
1783}
1784
1785func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1786 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001787 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001788 return depNames
1789}
1790
Dan Willemsen782a2d12015-12-21 14:55:28 -08001791func (c *CCBenchmark) InstallInData() bool {
1792 return true
1793}
1794
Colin Cross2ba19d92015-05-07 15:44:20 -07001795func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1796 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001797 installDir := "nativetest"
1798 if flags.Toolchain.Is64Bit() {
1799 installDir = "nativetest64"
1800 }
1801 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001802 } else {
1803 c.CCBinary.installModule(ctx, flags)
1804 }
1805}
1806
1807func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1808 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1809
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001810 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001811}
1812
1813func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1814 module := &CCBenchmark{}
1815
1816 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1817}
1818
Colin Cross3f40fa42015-01-30 17:27:36 -08001819//
1820// Static library
1821//
1822
Colin Cross97ba0732015-03-23 17:50:24 -07001823func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1824 module := &CCLibrary{}
1825 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001826
Colin Cross97ba0732015-03-23 17:50:24 -07001827 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001828}
1829
1830//
1831// Shared libraries
1832//
1833
Colin Cross97ba0732015-03-23 17:50:24 -07001834func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1835 module := &CCLibrary{}
1836 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001837
Colin Cross97ba0732015-03-23 17:50:24 -07001838 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001839}
1840
1841//
1842// Host static library
1843//
1844
Colin Cross97ba0732015-03-23 17:50:24 -07001845func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1846 module := &CCLibrary{}
1847 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001848
Colin Cross97ba0732015-03-23 17:50:24 -07001849 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001850}
1851
1852//
1853// Host Shared libraries
1854//
1855
Colin Cross97ba0732015-03-23 17:50:24 -07001856func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1857 module := &CCLibrary{}
1858 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001859
Colin Cross97ba0732015-03-23 17:50:24 -07001860 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001861}
1862
1863//
1864// Host Binaries
1865//
1866
Colin Cross97ba0732015-03-23 17:50:24 -07001867func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1868 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001869
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001870 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001871}
1872
1873//
Colin Cross1f8f2342015-03-26 16:09:47 -07001874// Host Tests
1875//
1876
1877func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001878 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001879 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001880}
1881
1882//
Colin Cross2ba19d92015-05-07 15:44:20 -07001883// Host Benchmarks
1884//
1885
1886func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1887 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001888 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001889}
1890
1891//
Colin Crosscfad1192015-11-02 16:43:11 -08001892// Defaults
1893//
1894type CCDefaults struct {
1895 common.AndroidModuleBase
1896 common.DefaultsModule
1897}
1898
1899func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1900}
1901
1902func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1903 module := &CCDefaults{}
1904
1905 propertyStructs := []interface{}{
1906 &CCBaseProperties{},
1907 &CCLibraryProperties{},
1908 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001909 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001910 &CCUnusedProperties{},
1911 }
1912
Dan Willemsen218f6562015-07-08 18:13:11 -07001913 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1914 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001915
1916 return common.InitDefaultsModule(module, module, propertyStructs...)
1917}
1918
1919//
Colin Cross3f40fa42015-01-30 17:27:36 -08001920// Device libraries shipped with gcc
1921//
1922
1923type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001924 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001925}
1926
Colin Cross0676e2d2015-04-24 17:39:18 -07001927func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001928 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001929 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001930}
1931
Colin Cross97ba0732015-03-23 17:50:24 -07001932func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001933 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001934
Colin Cross97ba0732015-03-23 17:50:24 -07001935 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001936 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001937
Colin Crossfa138792015-04-24 17:31:52 -07001938 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001939 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001940}
1941
1942func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001943 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001944
1945 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001946 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001947
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001948 if flags.Clang {
1949 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1950 }
1951
Colin Cross3f40fa42015-01-30 17:27:36 -08001952 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1953
1954 c.out = outputFile
1955
1956 ctx.CheckbuildFile(outputFile)
1957}
1958
Colin Cross97ba0732015-03-23 17:50:24 -07001959func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001960 // Toolchain libraries do not get installed.
1961}
1962
Dan Albertbe961682015-03-18 23:38:50 -07001963// NDK prebuilt libraries.
1964//
1965// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1966// either (with the exception of the shared STLs, which are installed to the app's directory rather
1967// than to the system image).
1968
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001969func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1970 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1971 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001972}
1973
Dan Albertc3144b12015-04-28 18:17:56 -07001974func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001975 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001976
1977 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1978 // We want to translate to just NAME.EXT
1979 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1980 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001981 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001982}
1983
1984type ndkPrebuiltObject struct {
1985 ccObject
1986}
1987
Dan Albertc3144b12015-04-28 18:17:56 -07001988func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1989 // NDK objects can't have any dependencies
1990 return CCDeps{}
1991}
1992
1993func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1994 module := &ndkPrebuiltObject{}
1995 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1996}
1997
1998func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001999 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07002000 // A null build step, but it sets up the output path.
2001 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
2002 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
2003 }
2004
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002005 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07002006}
2007
2008func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
2009 // Objects do not get installed.
2010}
2011
2012var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
2013
Dan Albertbe961682015-03-18 23:38:50 -07002014type ndkPrebuiltLibrary struct {
2015 CCLibrary
2016}
2017
Colin Cross0676e2d2015-04-24 17:39:18 -07002018func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07002019 // NDK libraries can't have any dependencies
2020 return CCDeps{}
2021}
2022
2023func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2024 module := &ndkPrebuiltLibrary{}
2025 module.LibraryProperties.BuildShared = true
2026 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2027}
2028
2029func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002030 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002031 // A null build step, but it sets up the output path.
2032 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2033 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2034 }
2035
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002036 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
2037 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002038
Dan Willemsen490fd492015-11-24 17:53:15 -08002039 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07002040 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07002041}
2042
2043func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07002044 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07002045}
2046
2047// The NDK STLs are slightly different from the prebuilt system libraries:
2048// * Are not specific to each platform version.
2049// * The libraries are not in a predictable location for each STL.
2050
2051type ndkPrebuiltStl struct {
2052 ndkPrebuiltLibrary
2053}
2054
2055type ndkPrebuiltStaticStl struct {
2056 ndkPrebuiltStl
2057}
2058
2059type ndkPrebuiltSharedStl struct {
2060 ndkPrebuiltStl
2061}
2062
2063func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2064 module := &ndkPrebuiltSharedStl{}
2065 module.LibraryProperties.BuildShared = true
2066 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2067}
2068
2069func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2070 module := &ndkPrebuiltStaticStl{}
2071 module.LibraryProperties.BuildStatic = true
2072 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2073}
2074
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002075func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002076 gccVersion := toolchain.GccVersion()
2077 var libDir string
2078 switch stl {
2079 case "libstlport":
2080 libDir = "cxx-stl/stlport/libs"
2081 case "libc++":
2082 libDir = "cxx-stl/llvm-libc++/libs"
2083 case "libgnustl":
2084 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2085 }
2086
2087 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002088 ndkSrcRoot := "prebuilts/ndk/current/sources"
2089 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002090 }
2091
2092 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002093 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002094}
2095
2096func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002097 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002098 // A null build step, but it sets up the output path.
2099 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2100 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2101 }
2102
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002103 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002104 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002105
2106 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002107 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002108 if c.LibraryProperties.BuildStatic {
2109 libExt = staticLibraryExtension
2110 }
2111
2112 stlName := strings.TrimSuffix(libName, "_shared")
2113 stlName = strings.TrimSuffix(stlName, "_static")
2114 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002115 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002116}
2117
Colin Cross6362e272015-10-29 15:25:03 -07002118func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002119 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002120 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002121 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002122 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002123 modules[0].(ccLinkedInterface).setStatic(true)
2124 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002125 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002126 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002127 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002128 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002129 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002130 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002131 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002132 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002133 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002134
2135 if _, ok := c.(ccLibraryInterface); ok {
2136 reuseFrom := modules[0].(ccLibraryInterface)
2137 for _, m := range modules {
2138 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002139 }
2140 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002141 }
2142}
Colin Cross74d1ec02015-04-28 13:30:13 -07002143
2144// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2145// modifies the slice contents in place, and returns a subslice of the original slice
2146func lastUniqueElements(list []string) []string {
2147 totalSkip := 0
2148 for i := len(list) - 1; i >= totalSkip; i-- {
2149 skip := 0
2150 for j := i - 1; j >= totalSkip; j-- {
2151 if list[i] == list[j] {
2152 skip++
2153 } else {
2154 list[j+skip] = list[j]
2155 }
2156 }
2157 totalSkip += skip
2158 }
2159 return list[totalSkip:]
2160}
Colin Cross06a931b2015-10-28 17:23:31 -07002161
2162var Bool = proptools.Bool