blob: 717e598b24bd7d859e4c31b759ca9290f264b4d6 [file] [log] [blame]
Colin Cross5049f022015-03-18 13:28:46 -07001// Copyright 2015 Google Inc. All rights reserved.
Colin Cross3f40fa42015-01-30 17:27:36 -08002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17// This file contains the module types for compiling C/C++ for Android, and converts the properties
18// into the flags and filenames necessary to pass to the compiler. The final creation of the rules
19// is handled in builder.go
20
21import (
Colin Cross3f40fa42015-01-30 17:27:36 -080022 "fmt"
23 "path/filepath"
24 "strings"
25
Colin Cross97ba0732015-03-23 17:50:24 -070026 "github.com/google/blueprint"
Colin Cross06a931b2015-10-28 17:23:31 -070027 "github.com/google/blueprint/proptools"
Colin Cross97ba0732015-03-23 17:50:24 -070028
Colin Cross463a90e2015-06-17 14:20:06 -070029 "android/soong"
Colin Cross3f40fa42015-01-30 17:27:36 -080030 "android/soong/common"
Colin Cross5049f022015-03-18 13:28:46 -070031 "android/soong/genrule"
Colin Cross3f40fa42015-01-30 17:27:36 -080032)
33
Colin Cross463a90e2015-06-17 14:20:06 -070034func init() {
35 soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory)
36 soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory)
37 soong.RegisterModuleType("cc_library", CCLibraryFactory)
38 soong.RegisterModuleType("cc_object", CCObjectFactory)
39 soong.RegisterModuleType("cc_binary", CCBinaryFactory)
40 soong.RegisterModuleType("cc_test", CCTestFactory)
41 soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory)
Colin Crosscfad1192015-11-02 16:43:11 -080042 soong.RegisterModuleType("cc_defaults", CCDefaultsFactory)
Colin Cross463a90e2015-06-17 14:20:06 -070043
44 soong.RegisterModuleType("toolchain_library", ToolchainLibraryFactory)
45 soong.RegisterModuleType("ndk_prebuilt_library", NdkPrebuiltLibraryFactory)
46 soong.RegisterModuleType("ndk_prebuilt_object", NdkPrebuiltObjectFactory)
47 soong.RegisterModuleType("ndk_prebuilt_static_stl", NdkPrebuiltStaticStlFactory)
48 soong.RegisterModuleType("ndk_prebuilt_shared_stl", NdkPrebuiltSharedStlFactory)
49
50 soong.RegisterModuleType("cc_library_host_static", CCLibraryHostStaticFactory)
51 soong.RegisterModuleType("cc_library_host_shared", CCLibraryHostSharedFactory)
52 soong.RegisterModuleType("cc_binary_host", CCBinaryHostFactory)
53 soong.RegisterModuleType("cc_test_host", CCTestHostFactory)
54 soong.RegisterModuleType("cc_benchmark_host", CCBenchmarkHostFactory)
55
56 // LinkageMutator must be registered after common.ArchMutator, but that is guaranteed by
57 // the Go initialization order because this package depends on common, so common's init
58 // functions will run first.
Colin Cross6362e272015-10-29 15:25:03 -070059 common.RegisterBottomUpMutator("link", linkageMutator)
60 common.RegisterBottomUpMutator("test_per_src", testPerSrcMutator)
61 common.RegisterBottomUpMutator("deps", depsMutator)
Colin Cross463a90e2015-06-17 14:20:06 -070062}
63
Colin Cross3f40fa42015-01-30 17:27:36 -080064var (
Colin Cross1332b002015-04-07 17:11:30 -070065 HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS)
Colin Cross3f40fa42015-01-30 17:27:36 -080066
Dan Willemsen34cc69e2015-09-23 15:26:20 -070067 LibcRoot = pctx.SourcePathVariable("LibcRoot", "bionic/libc")
68 LibmRoot = pctx.SourcePathVariable("LibmRoot", "bionic/libm")
Colin Cross3f40fa42015-01-30 17:27:36 -080069)
70
71// Flags used by lots of devices. Putting them in package static variables will save bytes in
72// build.ninja so they aren't repeated for every file
73var (
74 commonGlobalCflags = []string{
75 "-DANDROID",
76 "-fmessage-length=0",
77 "-W",
78 "-Wall",
79 "-Wno-unused",
80 "-Winit-self",
81 "-Wpointer-arith",
Dan Willemsene6540452015-10-20 15:21:33 -070082 "-fdebug-prefix-map=/proc/self/cwd=",
Colin Cross3f40fa42015-01-30 17:27:36 -080083
84 // COMMON_RELEASE_CFLAGS
85 "-DNDEBUG",
86 "-UDEBUG",
87 }
88
89 deviceGlobalCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080090 "-fdiagnostics-color",
91
Colin Cross3f40fa42015-01-30 17:27:36 -080092 // TARGET_ERROR_FLAGS
93 "-Werror=return-type",
94 "-Werror=non-virtual-dtor",
95 "-Werror=address",
96 "-Werror=sequence-point",
Dan Willemsena6084a32016-03-01 15:16:50 -080097 "-Werror=date-time",
Colin Cross3f40fa42015-01-30 17:27:36 -080098 }
99
100 hostGlobalCflags = []string{}
101
102 commonGlobalCppflags = []string{
103 "-Wsign-promo",
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700104 }
105
Dan Willemsenbe03f342016-03-03 17:21:04 -0800106 noOverrideGlobalCflags = []string{
107 "-Werror=int-to-pointer-cast",
108 "-Werror=pointer-to-int-cast",
109 }
110
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700111 illegalFlags = []string{
112 "-w",
Colin Cross3f40fa42015-01-30 17:27:36 -0800113 }
114)
115
116func init() {
117 pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " "))
118 pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " "))
119 pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800120 pctx.StaticVariable("noOverrideGlobalCflags", strings.Join(noOverrideGlobalCflags, " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800121
122 pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " "))
123
124 pctx.StaticVariable("commonClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800125 strings.Join(append(clangFilterUnknownCflags(commonGlobalCflags), "${clangExtraCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800126 pctx.StaticVariable("deviceClangGlobalCflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800127 strings.Join(append(clangFilterUnknownCflags(deviceGlobalCflags), "${clangExtraTargetCflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800128 pctx.StaticVariable("hostClangGlobalCflags",
129 strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " "))
Dan Willemsenbe03f342016-03-03 17:21:04 -0800130 pctx.StaticVariable("noOverrideClangGlobalCflags",
131 strings.Join(append(clangFilterUnknownCflags(noOverrideGlobalCflags), "${clangExtraNoOverrideCflags}"), " "))
132
Tim Kilbournf2948142015-03-11 12:03:03 -0700133 pctx.StaticVariable("commonClangGlobalCppflags",
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800134 strings.Join(append(clangFilterUnknownCflags(commonGlobalCppflags), "${clangExtraCppflags}"), " "))
Colin Cross3f40fa42015-01-30 17:27:36 -0800135
136 // Everything in this list is a crime against abstraction and dependency tracking.
137 // Do not add anything to this list.
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800138 pctx.PrefixedPathsForOptionalSourceVariable("commonGlobalIncludes", "-isystem ",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700139 []string{
140 "system/core/include",
Dan Willemsen98f93c72016-03-01 15:27:03 -0800141 "system/media/audio/include",
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 "hardware/libhardware/include",
143 "hardware/libhardware_legacy/include",
144 "hardware/ril/include",
145 "libnativehelper/include",
146 "frameworks/native/include",
147 "frameworks/native/opengl/include",
148 "frameworks/av/include",
149 "frameworks/base/include",
150 })
Dan Willemsene0378dd2016-01-07 17:42:34 -0800151 // This is used by non-NDK modules to get jni.h. export_include_dirs doesn't help
152 // with this, since there is no associated library.
153 pctx.PrefixedPathsForOptionalSourceVariable("commonNativehelperInclude", "-I",
154 []string{"libnativehelper/include/nativehelper"})
Colin Cross3f40fa42015-01-30 17:27:36 -0800155
Dan Willemsen767078e2016-03-01 13:42:19 -0800156 pctx.SourcePathVariable("clangPath", "prebuilts/clang/host/${HostPrebuiltTag}/clang-2629532/bin")
Colin Cross3f40fa42015-01-30 17:27:36 -0800157}
158
Colin Cross6362e272015-10-29 15:25:03 -0700159type CCModuleContext common.AndroidBaseContext
160
Colin Cross3f40fa42015-01-30 17:27:36 -0800161// Building C/C++ code is handled by objects that satisfy this interface via composition
Colin Cross97ba0732015-03-23 17:50:24 -0700162type CCModuleType interface {
Colin Cross3f40fa42015-01-30 17:27:36 -0800163 common.AndroidModule
164
Colin Crossfa138792015-04-24 17:31:52 -0700165 // Modify property values after parsing Blueprints file but before starting dependency
166 // resolution or build rule generation
Colin Cross6362e272015-10-29 15:25:03 -0700167 ModifyProperties(CCModuleContext)
Colin Crossfa138792015-04-24 17:31:52 -0700168
Colin Cross21b9a242015-03-24 14:15:58 -0700169 // Modify the ccFlags
Colin Cross0676e2d2015-04-24 17:39:18 -0700170 flags(common.AndroidModuleContext, CCFlags) CCFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800171
Colin Cross6362e272015-10-29 15:25:03 -0700172 // Return list of dependency names for use in depsMutator
Colin Cross0676e2d2015-04-24 17:39:18 -0700173 depNames(common.AndroidBaseContext, CCDeps) CCDeps
Colin Cross3f40fa42015-01-30 17:27:36 -0800174
Colin Cross6362e272015-10-29 15:25:03 -0700175 // Add dynamic dependencies
176 depsMutator(common.AndroidBottomUpMutatorContext)
177
Colin Cross3f40fa42015-01-30 17:27:36 -0800178 // Compile objects into final module
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179 compileModule(common.AndroidModuleContext, CCFlags, CCPathDeps, common.Paths)
Colin Cross3f40fa42015-01-30 17:27:36 -0800180
Dan Albertc403f7c2015-03-18 14:01:18 -0700181 // Install the built module.
Colin Cross97ba0732015-03-23 17:50:24 -0700182 installModule(common.AndroidModuleContext, CCFlags)
Dan Albertc403f7c2015-03-18 14:01:18 -0700183
Colin Cross3f40fa42015-01-30 17:27:36 -0800184 // Return the output file (.o, .a or .so) for use by other modules
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700185 outputFile() common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -0800186}
187
Colin Cross97ba0732015-03-23 17:50:24 -0700188type CCDeps struct {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs []string
Colin Crossc472d572015-03-17 15:06:21 -0700190
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700191 ObjFiles common.Paths
192
193 Cflags, ReexportedCflags []string
Colin Cross21b9a242015-03-24 14:15:58 -0700194
Colin Cross97ba0732015-03-23 17:50:24 -0700195 CrtBegin, CrtEnd string
Colin Crossc472d572015-03-17 15:06:21 -0700196}
197
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700198type CCPathDeps struct {
199 StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs common.Paths
200
201 ObjFiles common.Paths
202 WholeStaticLibObjFiles common.Paths
203
204 Cflags, ReexportedCflags []string
205
206 CrtBegin, CrtEnd common.OptionalPath
207}
208
Colin Cross97ba0732015-03-23 17:50:24 -0700209type CCFlags struct {
Colin Cross28344522015-04-22 13:07:53 -0700210 GlobalFlags []string // Flags that apply to C, C++, and assembly source files
211 AsFlags []string // Flags that apply to assembly source files
212 CFlags []string // Flags that apply to C and C++ source files
213 ConlyFlags []string // Flags that apply to C source files
214 CppFlags []string // Flags that apply to C++ source files
215 YaccFlags []string // Flags that apply to Yacc source files
216 LdFlags []string // Flags that apply to linker command lines
217
218 Nocrt bool
219 Toolchain Toolchain
220 Clang bool
Colin Crossc472d572015-03-17 15:06:21 -0700221}
222
Colin Cross7d5136f2015-05-11 13:39:40 -0700223// Properties used to compile all C or C++ modules
224type CCBaseProperties struct {
225 // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files.
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700226 Srcs []string `android:"arch_variant"`
227
228 // list of source files that should not be used to build the C/C++ module.
229 // This is most useful in the arch/multilib variants to remove non-common files
230 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700231
232 // list of module-specific flags that will be used for C and C++ compiles.
233 Cflags []string `android:"arch_variant"`
234
235 // list of module-specific flags that will be used for C++ compiles
236 Cppflags []string `android:"arch_variant"`
237
238 // list of module-specific flags that will be used for C compiles
239 Conlyflags []string `android:"arch_variant"`
240
241 // list of module-specific flags that will be used for .S compiles
242 Asflags []string `android:"arch_variant"`
243
244 // list of module-specific flags that will be used for .y and .yy compiles
245 Yaccflags []string
246
247 // list of module-specific flags that will be used for all link steps
248 Ldflags []string `android:"arch_variant"`
249
250 // the instruction set architecture to use to compile the C/C++
251 // module.
252 Instruction_set string `android:"arch_variant"`
253
254 // list of directories relative to the root of the source tree that will
255 // be added to the include path using -I.
256 // If possible, don't use this. If adding paths from the current directory use
257 // local_include_dirs, if adding paths from other modules use export_include_dirs in
258 // that module.
259 Include_dirs []string `android:"arch_variant"`
260
Colin Cross39d97f22015-09-14 12:30:50 -0700261 // list of files relative to the root of the source tree that will be included
262 // using -include.
263 // If possible, don't use this.
264 Include_files []string `android:"arch_variant"`
265
Colin Cross7d5136f2015-05-11 13:39:40 -0700266 // list of directories relative to the Blueprints file that will
267 // be added to the include path using -I
268 Local_include_dirs []string `android:"arch_variant"`
269
Colin Cross39d97f22015-09-14 12:30:50 -0700270 // list of files relative to the Blueprints file that will be included
271 // using -include.
272 // If possible, don't use this.
273 Local_include_files []string `android:"arch_variant"`
274
Colin Cross7d5136f2015-05-11 13:39:40 -0700275 // list of directories relative to the Blueprints file that will
276 // be added to the include path using -I for any module that links against this module
277 Export_include_dirs []string `android:"arch_variant"`
278
279 // list of module-specific flags that will be used for C and C++ compiles when
280 // compiling with clang
281 Clang_cflags []string `android:"arch_variant"`
282
283 // list of module-specific flags that will be used for .S compiles when
284 // compiling with clang
285 Clang_asflags []string `android:"arch_variant"`
286
287 // list of system libraries that will be dynamically linked to
288 // shared library and executable modules. If unset, generally defaults to libc
289 // and libm. Set to [] to prevent linking against libc and libm.
290 System_shared_libs []string
291
292 // list of modules whose object files should be linked into this module
293 // in their entirety. For static library modules, all of the .o files from the intermediate
294 // directory of the dependency will be linked into this modules .a file. For a shared library,
295 // the dependency's .a file will be linked into this module using -Wl,--whole-archive.
296 Whole_static_libs []string `android:"arch_variant"`
297
298 // list of modules that should be statically linked into this module.
299 Static_libs []string `android:"arch_variant"`
300
301 // list of modules that should be dynamically linked into this module.
302 Shared_libs []string `android:"arch_variant"`
303
304 // allow the module to contain undefined symbols. By default,
305 // modules cannot contain undefined symbols that are not satisified by their immediate
306 // dependencies. Set this flag to true to remove --no-undefined from the linker flags.
307 // This flag should only be necessary for compiling low-level libraries like libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700308 Allow_undefined_symbols *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700309
310 // don't link in crt_begin and crt_end. This flag should only be necessary for
311 // compiling crt or libc.
Colin Cross06a931b2015-10-28 17:23:31 -0700312 Nocrt *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700313
Dan Willemsend67be222015-09-16 15:19:33 -0700314 // don't link in libgcc.a
Colin Cross06a931b2015-10-28 17:23:31 -0700315 No_libgcc *bool
Dan Willemsend67be222015-09-16 15:19:33 -0700316
Colin Cross7d5136f2015-05-11 13:39:40 -0700317 // don't insert default compiler flags into asflags, cflags,
318 // cppflags, conlyflags, ldflags, or include_dirs
Colin Cross06a931b2015-10-28 17:23:31 -0700319 No_default_compiler_flags *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700320
321 // compile module with clang instead of gcc
Colin Cross06a931b2015-10-28 17:23:31 -0700322 Clang *bool `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -0700323
324 // pass -frtti instead of -fno-rtti
Colin Cross06a931b2015-10-28 17:23:31 -0700325 Rtti *bool
Colin Cross7d5136f2015-05-11 13:39:40 -0700326
327 // -l arguments to pass to linker for host-provided shared libraries
328 Host_ldlibs []string `android:"arch_variant"`
329
330 // select the STL library to use. Possible values are "libc++", "libc++_static",
331 // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the
332 // default
333 Stl string
334
335 // Set for combined shared/static libraries to prevent compiling object files a second time
336 SkipCompileObjs bool `blueprint:"mutated"`
337
338 Debug, Release struct {
339 // list of module-specific flags that will be used for C and C++ compiles in debug or
340 // release builds
341 Cflags []string `android:"arch_variant"`
342 } `android:"arch_variant"`
343
344 // Minimum sdk version supported when compiling against the ndk
345 Sdk_version string
346
347 // install to a subdirectory of the default install path for the module
348 Relative_install_path string
349}
350
Colin Crosscfad1192015-11-02 16:43:11 -0800351type CCUnusedProperties struct {
352 Native_coverage *bool
353 Required []string
354 Sanitize []string `android:"arch_variant"`
355 Sanitize_recover []string
356 Strip string
357 Tags []string
358}
359
Colin Crossfa138792015-04-24 17:31:52 -0700360// CCBase contains the properties and members used by all C/C++ module types, and implements
Colin Crossc472d572015-03-17 15:06:21 -0700361// the blueprint.Module interface. It expects to be embedded into an outer specialization struct,
362// and uses a ccModuleType interface to that struct to create the build steps.
Colin Crossfa138792015-04-24 17:31:52 -0700363type CCBase struct {
Colin Crossc472d572015-03-17 15:06:21 -0700364 common.AndroidModuleBase
Colin Crosscfad1192015-11-02 16:43:11 -0800365 common.DefaultableModule
Colin Cross97ba0732015-03-23 17:50:24 -0700366 module CCModuleType
Colin Crossc472d572015-03-17 15:06:21 -0700367
Colin Cross7d5136f2015-05-11 13:39:40 -0700368 Properties CCBaseProperties
Colin Crossfa138792015-04-24 17:31:52 -0700369
Colin Crosscfad1192015-11-02 16:43:11 -0800370 unused CCUnusedProperties
Colin Crossc472d572015-03-17 15:06:21 -0700371
372 installPath string
Colin Cross74d1ec02015-04-28 13:30:13 -0700373
374 savedDepNames CCDeps
Colin Crossc472d572015-03-17 15:06:21 -0700375}
376
Colin Crossfa138792015-04-24 17:31:52 -0700377func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700378 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
379
380 base.module = module
381
Colin Crossfa138792015-04-24 17:31:52 -0700382 props = append(props, &base.Properties, &base.unused)
Colin Crossc472d572015-03-17 15:06:21 -0700383
Colin Crosscfad1192015-11-02 16:43:11 -0800384 _, props = common.InitAndroidArchModule(module, hod, multilib, props...)
385
386 return common.InitDefaultableModule(module, base, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700387}
388
Colin Crossfa138792015-04-24 17:31:52 -0700389func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800390 toolchain := c.findToolchain(ctx)
391 if ctx.Failed() {
392 return
393 }
394
Colin Cross21b9a242015-03-24 14:15:58 -0700395 flags := c.collectFlags(ctx, toolchain)
Colin Cross3f40fa42015-01-30 17:27:36 -0800396 if ctx.Failed() {
397 return
398 }
399
Colin Cross74d1ec02015-04-28 13:30:13 -0700400 deps := c.depsToPaths(ctx, c.savedDepNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800401 if ctx.Failed() {
402 return
403 }
404
Colin Cross28344522015-04-22 13:07:53 -0700405 flags.CFlags = append(flags.CFlags, deps.Cflags...)
Colin Crossed9f8682015-03-18 17:17:35 -0700406
Colin Cross581c1892015-04-07 16:50:10 -0700407 objFiles := c.compileObjs(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800408 if ctx.Failed() {
409 return
410 }
411
Colin Cross581c1892015-04-07 16:50:10 -0700412 generatedObjFiles := c.compileGeneratedObjs(ctx, flags)
Colin Cross5049f022015-03-18 13:28:46 -0700413 if ctx.Failed() {
414 return
415 }
416
417 objFiles = append(objFiles, generatedObjFiles...)
418
Colin Cross3f40fa42015-01-30 17:27:36 -0800419 c.ccModuleType().compileModule(ctx, flags, deps, objFiles)
420 if ctx.Failed() {
421 return
422 }
Dan Albertc403f7c2015-03-18 14:01:18 -0700423
424 c.ccModuleType().installModule(ctx, flags)
425 if ctx.Failed() {
426 return
427 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800428}
429
Colin Crossfa138792015-04-24 17:31:52 -0700430func (c *CCBase) ccModuleType() CCModuleType {
Colin Cross3f40fa42015-01-30 17:27:36 -0800431 return c.module
432}
433
Colin Crossfa138792015-04-24 17:31:52 -0700434func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain {
Colin Cross3f40fa42015-01-30 17:27:36 -0800435 arch := ctx.Arch()
Colin Crossd3ba0392015-05-07 14:11:29 -0700436 hod := ctx.HostOrDevice()
Dan Willemsen490fd492015-11-24 17:53:15 -0800437 ht := ctx.HostType()
438 factory := toolchainFactories[hod][ht][arch.ArchType]
Colin Cross3f40fa42015-01-30 17:27:36 -0800439 if factory == nil {
Dan Willemsen490fd492015-11-24 17:53:15 -0800440 ctx.ModuleErrorf("Toolchain not found for %s %s arch %q", hod.String(), ht.String(), arch.String())
Colin Crosseeabb892015-11-20 13:07:51 -0800441 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800442 }
Colin Crossc5c24ad2015-11-20 15:35:00 -0800443 return factory(arch)
Colin Cross3f40fa42015-01-30 17:27:36 -0800444}
445
Colin Cross6362e272015-10-29 15:25:03 -0700446func (c *CCBase) ModifyProperties(ctx CCModuleContext) {
Colin Crossfa138792015-04-24 17:31:52 -0700447}
448
Colin Crosse11befc2015-04-27 17:49:17 -0700449func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crossfa138792015-04-24 17:31:52 -0700450 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...)
451 depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...)
452 depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700453
Colin Cross21b9a242015-03-24 14:15:58 -0700454 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -0800455}
456
Colin Cross6362e272015-10-29 15:25:03 -0700457func (c *CCBase) depsMutator(ctx common.AndroidBottomUpMutatorContext) {
Colin Cross74d1ec02015-04-28 13:30:13 -0700458 c.savedDepNames = c.module.depNames(ctx, CCDeps{})
459 c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs)
460 c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs)
461 c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs)
462
463 staticLibs := c.savedDepNames.WholeStaticLibs
464 staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...)
465 staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700466 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800467
Colin Cross74d1ec02015-04-28 13:30:13 -0700468 ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...)
Colin Cross21b9a242015-03-24 14:15:58 -0700469
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700470 ctx.AddDependency(ctx.Module(), c.savedDepNames.ObjFiles.Strings()...)
Colin Cross74d1ec02015-04-28 13:30:13 -0700471 if c.savedDepNames.CrtBegin != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700472 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtBegin)
Colin Cross21b9a242015-03-24 14:15:58 -0700473 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700474 if c.savedDepNames.CrtEnd != "" {
Colin Cross6362e272015-10-29 15:25:03 -0700475 ctx.AddDependency(ctx.Module(), c.savedDepNames.CrtEnd)
Colin Cross21b9a242015-03-24 14:15:58 -0700476 }
Colin Cross6362e272015-10-29 15:25:03 -0700477}
Colin Cross21b9a242015-03-24 14:15:58 -0700478
Colin Cross6362e272015-10-29 15:25:03 -0700479func depsMutator(ctx common.AndroidBottomUpMutatorContext) {
480 if c, ok := ctx.Module().(CCModuleType); ok {
481 c.ModifyProperties(ctx)
482 c.depsMutator(ctx)
483 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800484}
485
486// Create a ccFlags struct that collects the compile flags from global values,
487// per-target values, module type values, and per-module Blueprints properties
Colin Crossfa138792015-04-24 17:31:52 -0700488func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags {
Colin Cross97ba0732015-03-23 17:50:24 -0700489 flags := CCFlags{
Colin Crossfa138792015-04-24 17:31:52 -0700490 CFlags: c.Properties.Cflags,
491 CppFlags: c.Properties.Cppflags,
492 ConlyFlags: c.Properties.Conlyflags,
493 LdFlags: c.Properties.Ldflags,
494 AsFlags: c.Properties.Asflags,
495 YaccFlags: c.Properties.Yaccflags,
Colin Cross06a931b2015-10-28 17:23:31 -0700496 Nocrt: Bool(c.Properties.Nocrt),
Colin Cross97ba0732015-03-23 17:50:24 -0700497 Toolchain: toolchain,
Colin Cross06a931b2015-10-28 17:23:31 -0700498 Clang: Bool(c.Properties.Clang),
Colin Cross3f40fa42015-01-30 17:27:36 -0800499 }
Colin Cross28344522015-04-22 13:07:53 -0700500
501 // Include dir cflags
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700502 rootIncludeDirs := common.PathsForSource(ctx, c.Properties.Include_dirs)
503 localIncludeDirs := common.PathsForModuleSrc(ctx, c.Properties.Local_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -0700504 flags.GlobalFlags = append(flags.GlobalFlags,
Dan Willemsen1e898b92015-09-23 15:26:32 -0700505 includeDirsToFlags(localIncludeDirs),
506 includeDirsToFlags(rootIncludeDirs))
Colin Cross28344522015-04-22 13:07:53 -0700507
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700508 rootIncludeFiles := common.PathsForSource(ctx, c.Properties.Include_files)
509 localIncludeFiles := common.PathsForModuleSrc(ctx, c.Properties.Local_include_files)
Colin Cross39d97f22015-09-14 12:30:50 -0700510
511 flags.GlobalFlags = append(flags.GlobalFlags,
512 includeFilesToFlags(rootIncludeFiles),
513 includeFilesToFlags(localIncludeFiles))
514
Colin Cross06a931b2015-10-28 17:23:31 -0700515 if !Bool(c.Properties.No_default_compiler_flags) {
Colin Crossfa138792015-04-24 17:31:52 -0700516 if c.Properties.Sdk_version == "" || ctx.Host() {
Colin Cross28344522015-04-22 13:07:53 -0700517 flags.GlobalFlags = append(flags.GlobalFlags,
518 "${commonGlobalIncludes}",
519 toolchain.IncludeFlags(),
Dan Willemsene0378dd2016-01-07 17:42:34 -0800520 "${commonNativehelperInclude}")
Colin Cross28344522015-04-22 13:07:53 -0700521 }
522
523 flags.GlobalFlags = append(flags.GlobalFlags, []string{
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700524 "-I" + common.PathForModuleSrc(ctx).String(),
525 "-I" + common.PathForModuleOut(ctx).String(),
526 "-I" + common.PathForModuleGen(ctx).String(),
Colin Cross28344522015-04-22 13:07:53 -0700527 }...)
528 }
529
Colin Cross06a931b2015-10-28 17:23:31 -0700530 if c.Properties.Clang == nil {
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700531 if ctx.Host() {
532 flags.Clang = true
533 }
534
535 if ctx.Device() && ctx.AConfig().DeviceUsesClang() {
536 flags.Clang = true
537 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800538 }
539
Dan Willemsen490fd492015-11-24 17:53:15 -0800540 if !toolchain.ClangSupported() {
541 flags.Clang = false
542 }
543
Dan Willemsen6d11dd82015-11-03 14:27:00 -0800544 instructionSet := c.Properties.Instruction_set
545 instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet)
546 if flags.Clang {
547 instructionSetFlags, err = toolchain.ClangInstructionSetFlags(instructionSet)
548 }
549 if err != nil {
550 ctx.ModuleErrorf("%s", err)
551 }
552
553 // TODO: debug
554 flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...)
555
Colin Cross97ba0732015-03-23 17:50:24 -0700556 if flags.Clang {
557 flags.CFlags = clangFilterUnknownCflags(flags.CFlags)
Colin Crossfa138792015-04-24 17:31:52 -0700558 flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...)
559 flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...)
Colin Cross97ba0732015-03-23 17:50:24 -0700560 flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags)
561 flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags)
562 flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800563
564 target := "-target " + toolchain.ClangTriple()
565 gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin")
566
Colin Cross97ba0732015-03-23 17:50:24 -0700567 flags.CFlags = append(flags.CFlags, target, gccPrefix)
568 flags.AsFlags = append(flags.AsFlags, target, gccPrefix)
569 flags.LdFlags = append(flags.LdFlags, target, gccPrefix)
Colin Cross3f40fa42015-01-30 17:27:36 -0800570 }
571
Colin Cross06a931b2015-10-28 17:23:31 -0700572 if !Bool(c.Properties.No_default_compiler_flags) {
573 if ctx.Device() && !Bool(c.Properties.Allow_undefined_symbols) {
Colin Cross97ba0732015-03-23 17:50:24 -0700574 flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined")
Colin Cross3f40fa42015-01-30 17:27:36 -0800575 }
576
Colin Cross56b4d452015-04-21 17:38:44 -0700577 flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags)
578
Colin Cross97ba0732015-03-23 17:50:24 -0700579 if flags.Clang {
Dan Willemsen32968a22016-01-12 22:25:34 -0800580 flags.AsFlags = append(flags.AsFlags, toolchain.ClangAsflags())
Colin Cross97ba0732015-03-23 17:50:24 -0700581 flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700582 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800583 toolchain.ClangCflags(),
584 "${commonClangGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700585 fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice()))
Dan Willemsenac5e1cb2016-01-12 16:22:40 -0800586
587 flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}")
Colin Cross3f40fa42015-01-30 17:27:36 -0800588 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700589 flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}")
Colin Cross56b4d452015-04-21 17:38:44 -0700590 flags.GlobalFlags = append(flags.GlobalFlags,
Colin Cross3f40fa42015-01-30 17:27:36 -0800591 toolchain.Cflags(),
592 "${commonGlobalCflags}",
Colin Crossd3ba0392015-05-07 14:11:29 -0700593 fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice()))
Colin Cross3f40fa42015-01-30 17:27:36 -0800594 }
595
Colin Cross7b66f152015-12-15 16:07:43 -0800596 if Bool(ctx.AConfig().ProductVariables.Brillo) {
597 flags.GlobalFlags = append(flags.GlobalFlags, "-D__BRILLO__")
598 }
599
Colin Crossf6566ed2015-03-24 11:13:38 -0700600 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -0700601 if Bool(c.Properties.Rtti) {
Colin Cross97ba0732015-03-23 17:50:24 -0700602 flags.CppFlags = append(flags.CppFlags, "-frtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800603 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700604 flags.CppFlags = append(flags.CppFlags, "-fno-rtti")
Colin Cross3f40fa42015-01-30 17:27:36 -0800605 }
606 }
607
Colin Cross97ba0732015-03-23 17:50:24 -0700608 flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__")
Colin Cross3f40fa42015-01-30 17:27:36 -0800609
Colin Cross97ba0732015-03-23 17:50:24 -0700610 if flags.Clang {
611 flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags())
612 flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800613 } else {
Colin Cross97ba0732015-03-23 17:50:24 -0700614 flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags())
615 flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags())
Colin Cross3f40fa42015-01-30 17:27:36 -0800616 }
Colin Cross28344522015-04-22 13:07:53 -0700617
618 if ctx.Host() {
Colin Crossfa138792015-04-24 17:31:52 -0700619 flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...)
Colin Cross28344522015-04-22 13:07:53 -0700620 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800621 }
622
Colin Crossc4bde762015-11-23 16:11:30 -0800623 if flags.Clang {
624 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainClangCflags())
625 } else {
626 flags.GlobalFlags = append(flags.GlobalFlags, toolchain.ToolchainCflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800627 }
Dan Willemsen6dd06602016-01-12 21:51:11 -0800628 flags.LdFlags = append(flags.LdFlags, toolchain.ToolchainLdflags())
Colin Crossc4bde762015-11-23 16:11:30 -0800629
Colin Cross0676e2d2015-04-24 17:39:18 -0700630 flags = c.ccModuleType().flags(ctx, flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800631
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700632 if c.Properties.Sdk_version == "" {
633 if ctx.Host() && !flags.Clang {
634 // The host GCC doesn't support C++14 (and is deprecated, so likely
635 // never will). Build these modules with C++11.
636 flags.CppFlags = append(flags.CppFlags, "-std=gnu++11")
637 } else {
638 flags.CppFlags = append(flags.CppFlags, "-std=gnu++14")
639 }
640 }
641
642 flags.CFlags, _ = filterList(flags.CFlags, illegalFlags)
643 flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags)
644 flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags)
645
Dan Willemsen52b1cd22016-03-01 13:36:34 -0800646 // We can enforce some rules more strictly in the code we own. strict
647 // indicates if this is code that we can be stricter with. If we have
648 // rules that we want to apply to *our* code (but maybe can't for
649 // vendor/device specific things), we could extend this to be a ternary
650 // value.
651 strict := true
652 if strings.HasPrefix(common.PathForModuleSrc(ctx).String(), "external/") {
653 strict = false
654 }
655
656 // Can be used to make some annotations stricter for code we can fix
657 // (such as when we mark functions as deprecated).
658 if strict {
659 flags.CFlags = append(flags.CFlags, "-DANDROID_STRICT")
660 }
661
Colin Cross3f40fa42015-01-30 17:27:36 -0800662 // Optimization to reduce size of build.ninja
663 // Replace the long list of flags for each file with a module-local variable
Colin Cross97ba0732015-03-23 17:50:24 -0700664 ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " "))
665 ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " "))
666 ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " "))
667 flags.CFlags = []string{"$cflags"}
668 flags.CppFlags = []string{"$cppflags"}
669 flags.AsFlags = []string{"$asflags"}
Colin Cross3f40fa42015-01-30 17:27:36 -0800670
671 return flags
672}
673
Colin Cross0676e2d2015-04-24 17:39:18 -0700674func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross3f40fa42015-01-30 17:27:36 -0800675 return flags
676}
677
678// Compile a list of source files into objects a specified subdirectory
Colin Crossfa138792015-04-24 17:31:52 -0700679func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700680 subdir string, srcFiles, excludes []string) common.Paths {
Colin Cross581c1892015-04-07 16:50:10 -0700681
682 buildFlags := ccFlagsToBuilderFlags(flags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800683
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700684 inputFiles := ctx.ExpandSources(srcFiles, excludes)
685 srcPaths, deps := genSources(ctx, inputFiles, buildFlags)
Colin Cross3f40fa42015-01-30 17:27:36 -0800686
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700687 return TransformSourceToObj(ctx, subdir, srcPaths, buildFlags, deps)
Colin Cross3f40fa42015-01-30 17:27:36 -0800688}
689
Colin Crossfa138792015-04-24 17:31:52 -0700690// Compile files listed in c.Properties.Srcs into objects
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700691func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -0800692
Colin Crossfa138792015-04-24 17:31:52 -0700693 if c.Properties.SkipCompileObjs {
Colin Cross3f40fa42015-01-30 17:27:36 -0800694 return nil
695 }
696
Dan Willemsen2ef08f42015-06-30 18:15:24 -0700697 return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -0800698}
699
Colin Cross5049f022015-03-18 13:28:46 -0700700// Compile generated source files from dependencies
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700701func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) common.Paths {
702 var srcs common.Paths
Colin Cross5049f022015-03-18 13:28:46 -0700703
Colin Crossfa138792015-04-24 17:31:52 -0700704 if c.Properties.SkipCompileObjs {
Colin Cross5049f022015-03-18 13:28:46 -0700705 return nil
706 }
707
708 ctx.VisitDirectDeps(func(module blueprint.Module) {
709 if gen, ok := module.(genrule.SourceFileGenerator); ok {
710 srcs = append(srcs, gen.GeneratedSourceFiles()...)
711 }
712 })
713
714 if len(srcs) == 0 {
715 return nil
716 }
717
Colin Cross581c1892015-04-07 16:50:10 -0700718 return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil)
Colin Cross5049f022015-03-18 13:28:46 -0700719}
720
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700721func (c *CCBase) outputFile() common.OptionalPath {
722 return common.OptionalPath{}
Colin Cross3f40fa42015-01-30 17:27:36 -0800723}
724
Colin Crossfa138792015-04-24 17:31:52 -0700725func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext,
Colin Cross3f40fa42015-01-30 17:27:36 -0800726 names []string) (modules []common.AndroidModule,
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700727 outputFiles common.Paths, exportedFlags []string) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800728
729 for _, n := range names {
730 found := false
731 ctx.VisitDirectDeps(func(m blueprint.Module) {
732 otherName := ctx.OtherModuleName(m)
733 if otherName != n {
734 return
735 }
736
Colin Cross97ba0732015-03-23 17:50:24 -0700737 if a, ok := m.(CCModuleType); ok {
Dan Willemsen0effe062015-11-30 16:06:01 -0800738 if !a.Enabled() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800739 // If a cc_library host+device module depends on a library that exists as both
740 // cc_library_shared and cc_library_host_shared, it will end up with two
741 // dependencies with the same name, one of which is marked disabled for each
742 // of host and device. Ignore the disabled one.
743 return
744 }
Colin Crossd3ba0392015-05-07 14:11:29 -0700745 if a.HostOrDevice() != ctx.HostOrDevice() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800746 ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(),
747 otherName)
748 return
749 }
750
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700751 if outputFile := a.outputFile(); outputFile.Valid() {
Colin Cross3f40fa42015-01-30 17:27:36 -0800752 if found {
753 ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName)
754 return
755 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700756 outputFiles = append(outputFiles, outputFile.Path())
Colin Cross3f40fa42015-01-30 17:27:36 -0800757 modules = append(modules, a)
Colin Cross28344522015-04-22 13:07:53 -0700758 if i, ok := a.(ccExportedFlagsProducer); ok {
759 exportedFlags = append(exportedFlags, i.exportedFlags()...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800760 }
761 found = true
762 } else {
763 ctx.ModuleErrorf("module %q missing output file", otherName)
764 return
765 }
766 } else {
767 ctx.ModuleErrorf("module %q not an android module", otherName)
768 return
769 }
770 })
Colin Cross6ff51382015-12-17 16:39:19 -0800771 if !found && !inList(n, ctx.GetMissingDependencies()) {
Colin Cross3f40fa42015-01-30 17:27:36 -0800772 ctx.ModuleErrorf("unsatisified dependency on %q", n)
773 }
774 }
775
Colin Cross28344522015-04-22 13:07:53 -0700776 return modules, outputFiles, exportedFlags
Colin Cross3f40fa42015-01-30 17:27:36 -0800777}
778
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700779// Convert dependency names to paths. Takes a CCDeps containing names and returns a CCPathDeps
Colin Cross21b9a242015-03-24 14:15:58 -0700780// containing paths
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700781func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCPathDeps {
782 var depPaths CCPathDeps
Colin Cross28344522015-04-22 13:07:53 -0700783 var newCflags []string
Colin Cross3f40fa42015-01-30 17:27:36 -0800784
Colin Cross21b9a242015-03-24 14:15:58 -0700785 var wholeStaticLibModules []common.AndroidModule
Colin Cross3f40fa42015-01-30 17:27:36 -0800786
Colin Cross28344522015-04-22 13:07:53 -0700787 wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags =
Colin Cross21b9a242015-03-24 14:15:58 -0700788 c.depsToPathsFromList(ctx, depNames.WholeStaticLibs)
Colin Cross28344522015-04-22 13:07:53 -0700789 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Crossa48f71f2015-11-16 18:00:41 -0800790 depPaths.ReexportedCflags = append(depPaths.ReexportedCflags, newCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -0800791
Colin Cross21b9a242015-03-24 14:15:58 -0700792 for _, m := range wholeStaticLibModules {
793 if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() {
794 depPaths.WholeStaticLibObjFiles =
795 append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...)
796 } else {
797 ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m))
798 }
799 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800800
Colin Cross28344522015-04-22 13:07:53 -0700801 _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs)
802 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700803
Colin Cross28344522015-04-22 13:07:53 -0700804 _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs)
805 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700806
Colin Cross28344522015-04-22 13:07:53 -0700807 _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs)
808 depPaths.Cflags = append(depPaths.Cflags, newCflags...)
Colin Cross21b9a242015-03-24 14:15:58 -0700809
810 ctx.VisitDirectDeps(func(m blueprint.Module) {
Dan Albertc3144b12015-04-28 18:17:56 -0700811 if obj, ok := m.(ccObjectProvider); ok {
Colin Cross21b9a242015-03-24 14:15:58 -0700812 otherName := ctx.OtherModuleName(m)
813 if otherName == depNames.CrtBegin {
Colin Cross06a931b2015-10-28 17:23:31 -0700814 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700815 depPaths.CrtBegin = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700816 }
817 } else if otherName == depNames.CrtEnd {
Colin Cross06a931b2015-10-28 17:23:31 -0700818 if !Bool(c.Properties.Nocrt) {
Dan Albertc3144b12015-04-28 18:17:56 -0700819 depPaths.CrtEnd = obj.object().outputFile()
Colin Cross21b9a242015-03-24 14:15:58 -0700820 }
821 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700822 output := obj.object().outputFile()
823 if output.Valid() {
824 depPaths.ObjFiles = append(depPaths.ObjFiles, output.Path())
825 } else {
826 ctx.ModuleErrorf("module %s did not provide an output file", otherName)
827 }
Colin Cross21b9a242015-03-24 14:15:58 -0700828 }
829 }
830 })
831
832 return depPaths
Colin Cross3f40fa42015-01-30 17:27:36 -0800833}
834
Colin Cross7d5136f2015-05-11 13:39:40 -0700835type ccLinkedProperties struct {
836 VariantIsShared bool `blueprint:"mutated"`
837 VariantIsStatic bool `blueprint:"mutated"`
838 VariantIsStaticBinary bool `blueprint:"mutated"`
839}
840
Colin Crossfa138792015-04-24 17:31:52 -0700841// CCLinked contains the properties and members used by libraries and executables
842type CCLinked struct {
843 CCBase
Colin Cross7d5136f2015-05-11 13:39:40 -0700844 dynamicProperties ccLinkedProperties
Colin Cross3f40fa42015-01-30 17:27:36 -0800845}
846
Colin Crossfa138792015-04-24 17:31:52 -0700847func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported,
Colin Crossc472d572015-03-17 15:06:21 -0700848 multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) {
849
Colin Crossed4cf0b2015-03-26 14:43:45 -0700850 props = append(props, &dynamic.dynamicProperties)
851
Colin Crossfa138792015-04-24 17:31:52 -0700852 return newCCBase(&dynamic.CCBase, module, hod, multilib, props...)
Colin Crossc472d572015-03-17 15:06:21 -0700853}
854
Colin Crossfa138792015-04-24 17:31:52 -0700855func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string {
Colin Cross06a931b2015-10-28 17:23:31 -0700856 if c.Properties.System_shared_libs != nil {
Colin Crossfa138792015-04-24 17:31:52 -0700857 return c.Properties.System_shared_libs
858 } else if ctx.Device() && c.Properties.Sdk_version == "" {
Colin Cross577f6e42015-03-27 18:23:34 -0700859 return []string{"libc", "libm"}
Colin Cross28d76592015-03-26 16:14:04 -0700860 } else {
Colin Cross577f6e42015-03-27 18:23:34 -0700861 return nil
Colin Cross3f40fa42015-01-30 17:27:36 -0800862 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800863}
864
Colin Crossfa138792015-04-24 17:31:52 -0700865func (c *CCLinked) stl(ctx common.AndroidBaseContext) string {
866 if c.Properties.Sdk_version != "" && ctx.Device() {
867 switch c.Properties.Stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700868 case "":
869 return "ndk_system"
870 case "c++_shared", "c++_static",
871 "stlport_shared", "stlport_static",
872 "gnustl_static":
Colin Crossfa138792015-04-24 17:31:52 -0700873 return "ndk_lib" + c.Properties.Stl
Colin Crossed4cf0b2015-03-26 14:43:45 -0700874 default:
Colin Crossfa138792015-04-24 17:31:52 -0700875 ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl)
Colin Crossed4cf0b2015-03-26 14:43:45 -0700876 return ""
877 }
878 }
879
Dan Willemsen490fd492015-11-24 17:53:15 -0800880 if ctx.HostType() == common.Windows {
881 switch c.Properties.Stl {
882 case "libc++", "libc++_static", "libstdc++", "":
883 // libc++ is not supported on mingw
884 return "libstdc++"
885 case "none":
886 return ""
887 default:
888 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
889 return ""
Colin Crossed4cf0b2015-03-26 14:43:45 -0700890 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800891 } else {
892 switch c.Properties.Stl {
893 case "libc++", "libc++_static",
894 "libstdc++":
895 return c.Properties.Stl
896 case "none":
897 return ""
898 case "":
899 if c.static() {
900 return "libc++_static"
901 } else {
902 return "libc++"
903 }
904 default:
905 ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl)
906 return ""
907 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700908 }
909}
910
Dan Willemsen490fd492015-11-24 17:53:15 -0800911var hostDynamicGccLibs, hostStaticGccLibs map[common.HostType][]string
Colin Cross0af4b842015-04-30 16:36:18 -0700912
913func init() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800914 hostDynamicGccLibs = map[common.HostType][]string{
915 common.Linux: []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"},
916 common.Darwin: []string{"-lc", "-lSystem"},
917 common.Windows: []string{"-lmsvcr110", "-lmingw32", "-lgcc", "-lmoldname",
918 "-lmingwex", "-lmsvcrt", "-ladvapi32", "-lshell32", "-luser32",
919 "-lkernel32", "-lmingw32", "-lgcc", "-lmoldname", "-lmingwex",
920 "-lmsvcrt"},
921 }
922 hostStaticGccLibs = map[common.HostType][]string{
923 common.Linux: []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"},
924 common.Darwin: []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"},
925 common.Windows: []string{"NO_STATIC_HOST_BINARIES_ON_WINDOWS"},
Colin Cross0af4b842015-04-30 16:36:18 -0700926 }
927}
Colin Cross712fc022015-04-27 11:13:34 -0700928
Colin Crosse11befc2015-04-27 17:49:17 -0700929func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700930 stl := c.stl(ctx)
931 if ctx.Failed() {
932 return flags
933 }
934
935 switch stl {
936 case "libc++", "libc++_static":
937 flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX")
Colin Crossed4cf0b2015-03-26 14:43:45 -0700938 if ctx.Host() {
939 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
940 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross712fc022015-04-27 11:13:34 -0700941 flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread")
Colin Cross18b6dc52015-04-28 13:20:37 -0700942 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800943 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700944 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800945 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700946 }
Dan Willemsen3bf6b472015-09-11 17:41:10 -0700947 } else {
948 if ctx.Arch().ArchType == common.Arm {
949 flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a")
950 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700951 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700952 case "libstdc++":
953 // Using bionic's basic libstdc++. Not actually an STL. Only around until the
954 // tree is in good enough shape to not need it.
955 // Host builds will use GNU libstdc++.
956 if ctx.Device() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700957 flags.CFlags = append(flags.CFlags, "-I"+common.PathForSource(ctx, "bionic/libstdc++/include").String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700958 }
959 case "ndk_system":
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700960 ndkSrcRoot := common.PathForSource(ctx, "prebuilts/ndk/current/sources/cxx-stl/system/include")
961 flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot.String())
Colin Crossed4cf0b2015-03-26 14:43:45 -0700962 case "ndk_libc++_shared", "ndk_libc++_static":
963 // TODO(danalbert): This really shouldn't be here...
964 flags.CppFlags = append(flags.CppFlags, "-std=c++11")
965 case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static":
966 // Nothing
967 case "":
968 // None or error.
969 if ctx.Host() {
970 flags.CppFlags = append(flags.CppFlags, "-nostdinc++")
971 flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs")
Colin Cross18b6dc52015-04-28 13:20:37 -0700972 if c.staticBinary() {
Dan Willemsen490fd492015-11-24 17:53:15 -0800973 flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs[ctx.HostType()]...)
Colin Cross18b6dc52015-04-28 13:20:37 -0700974 } else {
Dan Willemsen490fd492015-11-24 17:53:15 -0800975 flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs[ctx.HostType()]...)
Colin Cross712fc022015-04-27 11:13:34 -0700976 }
Colin Crossed4cf0b2015-03-26 14:43:45 -0700977 }
978 default:
Colin Crossfa138792015-04-24 17:31:52 -0700979 panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -0700980 }
981
982 return flags
983}
984
Colin Crosse11befc2015-04-27 17:49:17 -0700985func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
986 depNames = c.CCBase.depNames(ctx, depNames)
Colin Cross3f40fa42015-01-30 17:27:36 -0800987
Colin Crossed4cf0b2015-03-26 14:43:45 -0700988 stl := c.stl(ctx)
989 if ctx.Failed() {
990 return depNames
991 }
992
993 switch stl {
Colin Crossed4cf0b2015-03-26 14:43:45 -0700994 case "libstdc++":
995 if ctx.Device() {
996 depNames.SharedLibs = append(depNames.SharedLibs, stl)
997 }
Colin Cross74d1ec02015-04-28 13:30:13 -0700998 case "libc++", "libc++_static":
999 if stl == "libc++" {
1000 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1001 } else {
1002 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1003 }
1004 if ctx.Device() {
1005 if ctx.Arch().ArchType == common.Arm {
1006 depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm")
1007 }
1008 if c.staticBinary() {
1009 depNames.StaticLibs = append(depNames.StaticLibs, "libdl")
1010 } else {
1011 depNames.SharedLibs = append(depNames.SharedLibs, "libdl")
1012 }
1013 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001014 case "":
1015 // None or error.
1016 case "ndk_system":
1017 // TODO: Make a system STL prebuilt for the NDK.
1018 // 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 -07001019 // its own includes. The includes are handled in CCBase.Flags().
Colin Cross577f6e42015-03-27 18:23:34 -07001020 depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001021 case "ndk_libc++_shared", "ndk_libstlport_shared":
1022 depNames.SharedLibs = append(depNames.SharedLibs, stl)
1023 case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static":
1024 depNames.StaticLibs = append(depNames.StaticLibs, stl)
1025 default:
Colin Crosse11befc2015-04-27 17:49:17 -07001026 panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl))
Colin Crossed4cf0b2015-03-26 14:43:45 -07001027 }
1028
Colin Cross74d1ec02015-04-28 13:30:13 -07001029 if ctx.ModuleName() != "libcompiler_rt-extras" {
1030 depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras")
1031 }
1032
Colin Crossf6566ed2015-03-24 11:13:38 -07001033 if ctx.Device() {
Colin Cross77b00fa2015-03-16 16:15:49 -07001034 // libgcc and libatomic have to be last on the command line
Dan Willemsen415cb0f2016-01-12 21:40:17 -08001035 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libatomic")
Colin Cross06a931b2015-10-28 17:23:31 -07001036 if !Bool(c.Properties.No_libgcc) {
Dan Willemsend67be222015-09-16 15:19:33 -07001037 depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcc")
1038 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001039
Colin Cross18b6dc52015-04-28 13:20:37 -07001040 if !c.static() {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001041 depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...)
1042 }
Colin Cross577f6e42015-03-27 18:23:34 -07001043
Colin Crossfa138792015-04-24 17:31:52 -07001044 if c.Properties.Sdk_version != "" {
1045 version := c.Properties.Sdk_version
Colin Cross577f6e42015-03-27 18:23:34 -07001046 depNames.SharedLibs = append(depNames.SharedLibs,
1047 "ndk_libc."+version,
1048 "ndk_libm."+version,
1049 )
1050 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001051 }
1052
Colin Cross21b9a242015-03-24 14:15:58 -07001053 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001054}
1055
Colin Crossed4cf0b2015-03-26 14:43:45 -07001056// ccLinkedInterface interface is used on ccLinked to deal with static or shared variants
1057type ccLinkedInterface interface {
1058 // Returns true if the build options for the module have selected a static or shared build
1059 buildStatic() bool
1060 buildShared() bool
1061
1062 // Sets whether a specific variant is static or shared
Colin Cross18b6dc52015-04-28 13:20:37 -07001063 setStatic(bool)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001064
Colin Cross18b6dc52015-04-28 13:20:37 -07001065 // Returns whether a specific variant is a static library or binary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001066 static() bool
Colin Cross18b6dc52015-04-28 13:20:37 -07001067
1068 // Returns whether a module is a static binary
1069 staticBinary() bool
Colin Crossed4cf0b2015-03-26 14:43:45 -07001070}
1071
1072var _ ccLinkedInterface = (*CCLibrary)(nil)
1073var _ ccLinkedInterface = (*CCBinary)(nil)
1074
Colin Crossfa138792015-04-24 17:31:52 -07001075func (c *CCLinked) static() bool {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001076 return c.dynamicProperties.VariantIsStatic
1077}
1078
Colin Cross18b6dc52015-04-28 13:20:37 -07001079func (c *CCLinked) staticBinary() bool {
1080 return c.dynamicProperties.VariantIsStaticBinary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001081}
1082
Colin Cross18b6dc52015-04-28 13:20:37 -07001083func (c *CCLinked) setStatic(static bool) {
1084 c.dynamicProperties.VariantIsStatic = static
Colin Crossed4cf0b2015-03-26 14:43:45 -07001085}
1086
Colin Cross28344522015-04-22 13:07:53 -07001087type ccExportedFlagsProducer interface {
1088 exportedFlags() []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001089}
1090
1091//
1092// Combined static+shared libraries
1093//
1094
Colin Cross7d5136f2015-05-11 13:39:40 -07001095type CCLibraryProperties struct {
1096 BuildStatic bool `blueprint:"mutated"`
1097 BuildShared bool `blueprint:"mutated"`
1098 Static struct {
1099 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001100 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001101 Cflags []string `android:"arch_variant"`
1102 Whole_static_libs []string `android:"arch_variant"`
1103 Static_libs []string `android:"arch_variant"`
1104 Shared_libs []string `android:"arch_variant"`
1105 } `android:"arch_variant"`
1106 Shared struct {
1107 Srcs []string `android:"arch_variant"`
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001108 Exclude_srcs []string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001109 Cflags []string `android:"arch_variant"`
1110 Whole_static_libs []string `android:"arch_variant"`
1111 Static_libs []string `android:"arch_variant"`
1112 Shared_libs []string `android:"arch_variant"`
1113 } `android:"arch_variant"`
Colin Crossaee540a2015-07-06 17:48:31 -07001114
1115 // local file name to pass to the linker as --version_script
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001116 Version_script *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001117 // local file name to pass to the linker as -unexported_symbols_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001118 Unexported_symbols_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001119 // local file name to pass to the linker as -force_symbols_not_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001120 Force_symbols_not_weak_list *string `android:"arch_variant"`
Dan Willemsen93c28312015-12-04 14:59:08 -08001121 // local file name to pass to the linker as -force_symbols_weak_list
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001122 Force_symbols_weak_list *string `android:"arch_variant"`
Colin Cross7d5136f2015-05-11 13:39:40 -07001123}
1124
Colin Cross97ba0732015-03-23 17:50:24 -07001125type CCLibrary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001126 CCLinked
Colin Cross3f40fa42015-01-30 17:27:36 -08001127
Colin Cross28344522015-04-22 13:07:53 -07001128 reuseFrom ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001129 reuseObjFiles common.Paths
1130 objFiles common.Paths
Colin Cross28344522015-04-22 13:07:53 -07001131 exportFlags []string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001132 out common.Path
Dan Willemsen218f6562015-07-08 18:13:11 -07001133 systemLibs []string
Colin Cross3f40fa42015-01-30 17:27:36 -08001134
Colin Cross7d5136f2015-05-11 13:39:40 -07001135 LibraryProperties CCLibraryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001136}
1137
Colin Crossed4cf0b2015-03-26 14:43:45 -07001138func (c *CCLibrary) buildStatic() bool {
1139 return c.LibraryProperties.BuildStatic
1140}
1141
1142func (c *CCLibrary) buildShared() bool {
1143 return c.LibraryProperties.BuildShared
1144}
1145
Colin Cross97ba0732015-03-23 17:50:24 -07001146type ccLibraryInterface interface {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001147 ccLinkedInterface
Colin Cross97ba0732015-03-23 17:50:24 -07001148 ccLibrary() *CCLibrary
Colin Crossed4cf0b2015-03-26 14:43:45 -07001149 setReuseFrom(ccLibraryInterface)
1150 getReuseFrom() ccLibraryInterface
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001151 getReuseObjFiles() common.Paths
1152 allObjFiles() common.Paths
Colin Crossc472d572015-03-17 15:06:21 -07001153}
1154
Colin Crossed4cf0b2015-03-26 14:43:45 -07001155var _ ccLibraryInterface = (*CCLibrary)(nil)
1156
Colin Cross97ba0732015-03-23 17:50:24 -07001157func (c *CCLibrary) ccLibrary() *CCLibrary {
1158 return c
Colin Cross3f40fa42015-01-30 17:27:36 -08001159}
1160
Colin Cross97ba0732015-03-23 17:50:24 -07001161func NewCCLibrary(library *CCLibrary, module CCModuleType,
1162 hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) {
1163
Colin Crossfa138792015-04-24 17:31:52 -07001164 return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth,
Colin Cross97ba0732015-03-23 17:50:24 -07001165 &library.LibraryProperties)
1166}
1167
1168func CCLibraryFactory() (blueprint.Module, []interface{}) {
1169 module := &CCLibrary{}
1170
1171 module.LibraryProperties.BuildShared = true
1172 module.LibraryProperties.BuildStatic = true
1173
1174 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
1175}
1176
Colin Cross0676e2d2015-04-24 17:39:18 -07001177func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001178 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Cross2732e9a2015-04-28 13:23:52 -07001179 if c.static() {
1180 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...)
1181 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...)
1182 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...)
1183 } else {
Colin Crossf6566ed2015-03-24 11:13:38 -07001184 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001185 if c.Properties.Sdk_version == "" {
1186 depNames.CrtBegin = "crtbegin_so"
1187 depNames.CrtEnd = "crtend_so"
1188 } else {
1189 depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version
1190 depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version
1191 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001192 }
Colin Cross2732e9a2015-04-28 13:23:52 -07001193 depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...)
1194 depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...)
1195 depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001196 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001197
Dan Willemsen218f6562015-07-08 18:13:11 -07001198 c.systemLibs = c.systemSharedLibs(ctx)
1199
Colin Cross21b9a242015-03-24 14:15:58 -07001200 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001201}
1202
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001203func (c *CCLibrary) outputFile() common.OptionalPath {
1204 return common.OptionalPathForPath(c.out)
Colin Cross3f40fa42015-01-30 17:27:36 -08001205}
1206
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001207func (c *CCLibrary) getReuseObjFiles() common.Paths {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001208 return c.reuseObjFiles
1209}
1210
1211func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) {
1212 c.reuseFrom = reuseFrom
1213}
1214
1215func (c *CCLibrary) getReuseFrom() ccLibraryInterface {
1216 return c.reuseFrom
1217}
1218
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001219func (c *CCLibrary) allObjFiles() common.Paths {
Colin Cross3f40fa42015-01-30 17:27:36 -08001220 return c.objFiles
1221}
1222
Colin Cross28344522015-04-22 13:07:53 -07001223func (c *CCLibrary) exportedFlags() []string {
1224 return c.exportFlags
Colin Cross3f40fa42015-01-30 17:27:36 -08001225}
1226
Colin Cross0676e2d2015-04-24 17:39:18 -07001227func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001228 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001229
Dan Willemsen490fd492015-11-24 17:53:15 -08001230 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1231 // all code is position independent, and then those warnings get promoted to
1232 // errors.
1233 if ctx.HostType() != common.Windows {
1234 flags.CFlags = append(flags.CFlags, "-fPIC")
1235 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001236
Colin Crossd8e780d2015-04-28 17:39:43 -07001237 if c.static() {
1238 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...)
1239 } else {
1240 flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...)
1241 }
1242
Colin Cross18b6dc52015-04-28 13:20:37 -07001243 if !c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001244 libName := ctx.ModuleName()
1245 // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead
1246 sharedFlag := "-Wl,-shared"
Dan Willemsendd0e2c32015-10-20 14:29:35 -07001247 if flags.Clang || ctx.Host() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001248 sharedFlag = "-shared"
1249 }
Colin Crossf6566ed2015-03-24 11:13:38 -07001250 if ctx.Device() {
Colin Cross97ba0732015-03-23 17:50:24 -07001251 flags.LdFlags = append(flags.LdFlags, "-nostdlib")
Colin Cross3f40fa42015-01-30 17:27:36 -08001252 }
Colin Cross97ba0732015-03-23 17:50:24 -07001253
Colin Cross0af4b842015-04-30 16:36:18 -07001254 if ctx.Darwin() {
1255 flags.LdFlags = append(flags.LdFlags,
1256 "-dynamiclib",
1257 "-single_module",
1258 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001259 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001260 )
1261 } else {
1262 flags.LdFlags = append(flags.LdFlags,
1263 "-Wl,--gc-sections",
1264 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001265 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001266 )
1267 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001268 }
Colin Cross97ba0732015-03-23 17:50:24 -07001269
1270 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001271}
1272
Colin Cross97ba0732015-03-23 17:50:24 -07001273func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001274 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001275
1276 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001277 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001278 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001279
1280 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001281 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001282
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001283 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001284
Colin Cross0af4b842015-04-30 16:36:18 -07001285 if ctx.Darwin() {
1286 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1287 } else {
1288 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1289 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001290
1291 c.objFiles = objFiles
1292 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001293
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001294 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001295 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001296 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001297
1298 ctx.CheckbuildFile(outputFile)
1299}
1300
Colin Cross97ba0732015-03-23 17:50:24 -07001301func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001303
1304 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001305 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001306 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001307
1308 objFiles = append(objFiles, objFilesShared...)
1309
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001310 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001311
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001312 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001313
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001314 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1315 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1316 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1317 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001318 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001319 if versionScript.Valid() {
1320 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1321 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001322 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001323 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001324 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1325 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001326 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001327 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1328 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001329 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001330 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1331 }
1332 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001333 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001334 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1335 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001336 if unexportedSymbols.Valid() {
1337 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1338 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001339 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001340 if forceNotWeakSymbols.Valid() {
1341 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1342 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001343 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001344 if forceWeakSymbols.Valid() {
1345 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1346 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001347 }
Colin Crossaee540a2015-07-06 17:48:31 -07001348 }
1349
Colin Cross97ba0732015-03-23 17:50:24 -07001350 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001351 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001352 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001353
1354 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001355 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001356 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001357 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001358}
1359
Colin Cross97ba0732015-03-23 17:50:24 -07001360func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001361 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001362
1363 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001364 if c.getReuseFrom().ccLibrary() == c {
1365 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001366 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001367 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1368 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001369 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001370 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001371 }
1372
Colin Crossed4cf0b2015-03-26 14:43:45 -07001373 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001374 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1375 } else {
1376 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1377 }
1378}
1379
Colin Cross97ba0732015-03-23 17:50:24 -07001380func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001381 // Static libraries do not get installed.
1382}
1383
Colin Cross97ba0732015-03-23 17:50:24 -07001384func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001385 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001386 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001387 installDir = "lib64"
1388 }
1389
Dan Willemsen782a2d12015-12-21 14:55:28 -08001390 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001391}
1392
Colin Cross97ba0732015-03-23 17:50:24 -07001393func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001394 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001395 c.installStaticLibrary(ctx, flags)
1396 } else {
1397 c.installSharedLibrary(ctx, flags)
1398 }
1399}
1400
Colin Cross3f40fa42015-01-30 17:27:36 -08001401//
1402// Objects (for crt*.o)
1403//
1404
Dan Albertc3144b12015-04-28 18:17:56 -07001405type ccObjectProvider interface {
1406 object() *ccObject
1407}
1408
Colin Cross3f40fa42015-01-30 17:27:36 -08001409type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001410 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001411 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001412}
1413
Dan Albertc3144b12015-04-28 18:17:56 -07001414func (c *ccObject) object() *ccObject {
1415 return c
1416}
1417
Colin Cross97ba0732015-03-23 17:50:24 -07001418func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001419 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001420
Colin Crossfa138792015-04-24 17:31:52 -07001421 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001422}
1423
Colin Cross0676e2d2015-04-24 17:39:18 -07001424func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001425 // object files can't have any dynamic dependencies
1426 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001427}
1428
1429func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001430 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001431
Colin Cross97ba0732015-03-23 17:50:24 -07001432 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001433
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001434 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001435 if len(objFiles) == 1 {
1436 outputFile = objFiles[0]
1437 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001438 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1439 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1440 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001441 }
1442
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001443 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001444
1445 ctx.CheckbuildFile(outputFile)
1446}
1447
Colin Cross97ba0732015-03-23 17:50:24 -07001448func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001449 // Object files do not get installed.
1450}
1451
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001452func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001453 return c.out
1454}
1455
Dan Albertc3144b12015-04-28 18:17:56 -07001456var _ ccObjectProvider = (*ccObject)(nil)
1457
Colin Cross3f40fa42015-01-30 17:27:36 -08001458//
1459// Executables
1460//
1461
Colin Cross7d5136f2015-05-11 13:39:40 -07001462type CCBinaryProperties struct {
1463 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001464 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001465
1466 // set the name of the output
1467 Stem string `android:"arch_variant"`
1468
1469 // append to the name of the output
1470 Suffix string `android:"arch_variant"`
1471
1472 // if set, add an extra objcopy --prefix-symbols= step
1473 Prefix_symbols string
1474}
1475
Colin Cross97ba0732015-03-23 17:50:24 -07001476type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001477 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001478 out common.Path
1479 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001480 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001481}
1482
Colin Crossed4cf0b2015-03-26 14:43:45 -07001483func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001484 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001485}
1486
1487func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001488 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001489}
1490
Colin Cross97ba0732015-03-23 17:50:24 -07001491func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001492 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001493 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001494 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001495 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001496
1497 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001498}
1499
Colin Cross0676e2d2015-04-24 17:39:18 -07001500func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001501 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001502 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001503 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001504 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001505 depNames.CrtBegin = "crtbegin_static"
1506 } else {
1507 depNames.CrtBegin = "crtbegin_dynamic"
1508 }
1509 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001510 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001511 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001512 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1513 } else {
1514 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1515 }
1516 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001517 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001518
Colin Cross06a931b2015-10-28 17:23:31 -07001519 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001520 if c.stl(ctx) == "libc++_static" {
1521 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1522 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001523 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1524 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1525 // move them to the beginning of deps.LateStaticLibs
1526 var groupLibs []string
1527 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1528 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1529 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1530 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001531 }
Colin Cross21b9a242015-03-24 14:15:58 -07001532 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001533}
1534
Colin Cross97ba0732015-03-23 17:50:24 -07001535func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001536 hod common.HostOrDeviceSupported, multilib common.Multilib,
1537 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001538
Colin Cross1f8f2342015-03-26 16:09:47 -07001539 props = append(props, &binary.BinaryProperties)
1540
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001541 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001542}
1543
Colin Cross97ba0732015-03-23 17:50:24 -07001544func CCBinaryFactory() (blueprint.Module, []interface{}) {
1545 module := &CCBinary{}
1546
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001547 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001548}
1549
Colin Cross6362e272015-10-29 15:25:03 -07001550func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001551 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001552 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001553 }
Colin Cross06a931b2015-10-28 17:23:31 -07001554 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001555 c.dynamicProperties.VariantIsStaticBinary = true
1556 }
1557}
1558
Colin Cross0676e2d2015-04-24 17:39:18 -07001559func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001560 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001561
Dan Willemsen490fd492015-11-24 17:53:15 -08001562 if ctx.Host() {
1563 flags.LdFlags = append(flags.LdFlags, "-pie")
1564 if ctx.HostType() == common.Windows {
1565 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1566 }
1567 }
1568
1569 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1570 // all code is position independent, and then those warnings get promoted to
1571 // errors.
1572 if ctx.HostType() != common.Windows {
1573 flags.CFlags = append(flags.CFlags, "-fpie")
1574 }
Colin Cross97ba0732015-03-23 17:50:24 -07001575
Colin Crossf6566ed2015-03-24 11:13:38 -07001576 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001577 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001578 // Clang driver needs -static to create static executable.
1579 // However, bionic/linker uses -shared to overwrite.
1580 // Linker for x86 targets does not allow coexistance of -static and -shared,
1581 // so we add -static only if -shared is not used.
1582 if !inList("-shared", flags.LdFlags) {
1583 flags.LdFlags = append(flags.LdFlags, "-static")
1584 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001585
Colin Crossed4cf0b2015-03-26 14:43:45 -07001586 flags.LdFlags = append(flags.LdFlags,
1587 "-nostdlib",
1588 "-Bstatic",
1589 "-Wl,--gc-sections",
1590 )
1591
1592 } else {
1593 linker := "/system/bin/linker"
1594 if flags.Toolchain.Is64Bit() {
1595 linker = "/system/bin/linker64"
1596 }
1597
1598 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001599 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001600 "-nostdlib",
1601 "-Bdynamic",
1602 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1603 "-Wl,--gc-sections",
1604 "-Wl,-z,nocopyreloc",
1605 )
1606 }
Colin Cross0af4b842015-04-30 16:36:18 -07001607 } else if ctx.Darwin() {
1608 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001609 }
1610
Colin Cross97ba0732015-03-23 17:50:24 -07001611 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001612}
1613
Colin Cross97ba0732015-03-23 17:50:24 -07001614func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001615 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001616
Colin Cross06a931b2015-10-28 17:23:31 -07001617 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001618 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1619 "from static libs or set static_executable: true")
1620 }
1621
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001622 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001623 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001624 if c.BinaryProperties.Prefix_symbols != "" {
1625 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001626 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001627 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1628 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1629 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001630
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001631 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001632
Colin Cross97ba0732015-03-23 17:50:24 -07001633 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001634 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001635 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001636}
Colin Cross3f40fa42015-01-30 17:27:36 -08001637
Colin Cross97ba0732015-03-23 17:50:24 -07001638func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001639 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001640}
1641
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001642func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001643 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001644 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001645 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001646 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001647}
1648
Colin Cross6002e052015-09-16 16:00:08 -07001649func (c *CCBinary) binary() *CCBinary {
1650 return c
1651}
1652
1653type testPerSrc interface {
1654 binary() *CCBinary
1655 testPerSrc() bool
1656}
1657
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001658var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001659
Colin Cross6362e272015-10-29 15:25:03 -07001660func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001661 if test, ok := mctx.Module().(testPerSrc); ok {
1662 if test.testPerSrc() {
1663 testNames := make([]string, len(test.binary().Properties.Srcs))
1664 for i, src := range test.binary().Properties.Srcs {
1665 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1666 }
1667 tests := mctx.CreateLocalVariations(testNames...)
1668 for i, src := range test.binary().Properties.Srcs {
1669 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001670 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001671 }
1672 }
1673 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001674}
1675
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001676type CCTestProperties struct {
1677 // if set, build against the gtest library. Defaults to true.
1678 Gtest bool
1679
1680 // Create a separate binary for each source file. Useful when there is
1681 // global state that can not be torn down and reset between each test suite.
1682 Test_per_src *bool
1683}
1684
Colin Cross9ffb4f52015-04-24 17:48:09 -07001685type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001686 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001687
1688 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001689}
1690
Colin Cross9ffb4f52015-04-24 17:48:09 -07001691func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001692 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001693 if !c.TestProperties.Gtest {
1694 return flags
1695 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001696
Colin Cross97ba0732015-03-23 17:50:24 -07001697 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001698 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001699 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001700
1701 if ctx.HostType() == common.Windows {
1702 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1703 } else {
1704 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1705 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1706 }
1707 } else {
1708 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001709 }
1710
1711 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001712 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001714
Colin Cross21b9a242015-03-24 14:15:58 -07001715 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001716}
1717
Colin Cross9ffb4f52015-04-24 17:48:09 -07001718func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001719 if c.TestProperties.Gtest {
1720 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1721 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001722 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001723 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001724}
1725
Dan Willemsen782a2d12015-12-21 14:55:28 -08001726func (c *CCTest) InstallInData() bool {
1727 return true
1728}
1729
Colin Cross9ffb4f52015-04-24 17:48:09 -07001730func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001731 installDir := "nativetest"
1732 if flags.Toolchain.Is64Bit() {
1733 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001734 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001735 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1736}
1737
1738func (c *CCTest) testPerSrc() bool {
1739 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001740}
1741
Colin Cross9ffb4f52015-04-24 17:48:09 -07001742func NewCCTest(test *CCTest, module CCModuleType,
1743 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1744
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001745 props = append(props, &test.TestProperties)
1746
1747 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001748}
1749
1750func CCTestFactory() (blueprint.Module, []interface{}) {
1751 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001752 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001753
1754 return NewCCTest(module, module, common.HostAndDeviceSupported)
1755}
1756
Colin Cross2ba19d92015-05-07 15:44:20 -07001757type CCBenchmark struct {
1758 CCBinary
1759}
1760
1761func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1762 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001763 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001764 return depNames
1765}
1766
Dan Willemsen782a2d12015-12-21 14:55:28 -08001767func (c *CCBenchmark) InstallInData() bool {
1768 return true
1769}
1770
Colin Cross2ba19d92015-05-07 15:44:20 -07001771func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1772 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001773 installDir := "nativetest"
1774 if flags.Toolchain.Is64Bit() {
1775 installDir = "nativetest64"
1776 }
1777 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001778 } else {
1779 c.CCBinary.installModule(ctx, flags)
1780 }
1781}
1782
1783func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1784 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1785
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001786 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001787}
1788
1789func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1790 module := &CCBenchmark{}
1791
1792 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1793}
1794
Colin Cross3f40fa42015-01-30 17:27:36 -08001795//
1796// Static library
1797//
1798
Colin Cross97ba0732015-03-23 17:50:24 -07001799func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1800 module := &CCLibrary{}
1801 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001802
Colin Cross97ba0732015-03-23 17:50:24 -07001803 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001804}
1805
1806//
1807// Shared libraries
1808//
1809
Colin Cross97ba0732015-03-23 17:50:24 -07001810func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1811 module := &CCLibrary{}
1812 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001813
Colin Cross97ba0732015-03-23 17:50:24 -07001814 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001815}
1816
1817//
1818// Host static library
1819//
1820
Colin Cross97ba0732015-03-23 17:50:24 -07001821func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1822 module := &CCLibrary{}
1823 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001824
Colin Cross97ba0732015-03-23 17:50:24 -07001825 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001826}
1827
1828//
1829// Host Shared libraries
1830//
1831
Colin Cross97ba0732015-03-23 17:50:24 -07001832func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1833 module := &CCLibrary{}
1834 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001835
Colin Cross97ba0732015-03-23 17:50:24 -07001836 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001837}
1838
1839//
1840// Host Binaries
1841//
1842
Colin Cross97ba0732015-03-23 17:50:24 -07001843func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1844 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001845
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001846 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001847}
1848
1849//
Colin Cross1f8f2342015-03-26 16:09:47 -07001850// Host Tests
1851//
1852
1853func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001854 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001855 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001856}
1857
1858//
Colin Cross2ba19d92015-05-07 15:44:20 -07001859// Host Benchmarks
1860//
1861
1862func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1863 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001864 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001865}
1866
1867//
Colin Crosscfad1192015-11-02 16:43:11 -08001868// Defaults
1869//
1870type CCDefaults struct {
1871 common.AndroidModuleBase
1872 common.DefaultsModule
1873}
1874
1875func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1876}
1877
1878func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1879 module := &CCDefaults{}
1880
1881 propertyStructs := []interface{}{
1882 &CCBaseProperties{},
1883 &CCLibraryProperties{},
1884 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001885 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001886 &CCUnusedProperties{},
1887 }
1888
Dan Willemsen218f6562015-07-08 18:13:11 -07001889 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1890 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001891
1892 return common.InitDefaultsModule(module, module, propertyStructs...)
1893}
1894
1895//
Colin Cross3f40fa42015-01-30 17:27:36 -08001896// Device libraries shipped with gcc
1897//
1898
1899type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001900 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001901}
1902
Colin Cross0676e2d2015-04-24 17:39:18 -07001903func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001904 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001905 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001906}
1907
Colin Cross97ba0732015-03-23 17:50:24 -07001908func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001909 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001910
Colin Cross97ba0732015-03-23 17:50:24 -07001911 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001912 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001913
Colin Crossfa138792015-04-24 17:31:52 -07001914 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001915 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001916}
1917
1918func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001919 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001920
1921 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001922 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001923
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001924 if flags.Clang {
1925 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1926 }
1927
Colin Cross3f40fa42015-01-30 17:27:36 -08001928 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1929
1930 c.out = outputFile
1931
1932 ctx.CheckbuildFile(outputFile)
1933}
1934
Colin Cross97ba0732015-03-23 17:50:24 -07001935func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001936 // Toolchain libraries do not get installed.
1937}
1938
Dan Albertbe961682015-03-18 23:38:50 -07001939// NDK prebuilt libraries.
1940//
1941// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1942// either (with the exception of the shared STLs, which are installed to the app's directory rather
1943// than to the system image).
1944
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001945func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1946 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1947 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001948}
1949
Dan Albertc3144b12015-04-28 18:17:56 -07001950func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001951 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001952
1953 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1954 // We want to translate to just NAME.EXT
1955 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1956 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001957 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001958}
1959
1960type ndkPrebuiltObject struct {
1961 ccObject
1962}
1963
Dan Albertc3144b12015-04-28 18:17:56 -07001964func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1965 // NDK objects can't have any dependencies
1966 return CCDeps{}
1967}
1968
1969func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1970 module := &ndkPrebuiltObject{}
1971 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1972}
1973
1974func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001975 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001976 // A null build step, but it sets up the output path.
1977 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1978 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1979 }
1980
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001981 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001982}
1983
1984func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1985 // Objects do not get installed.
1986}
1987
1988var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1989
Dan Albertbe961682015-03-18 23:38:50 -07001990type ndkPrebuiltLibrary struct {
1991 CCLibrary
1992}
1993
Colin Cross0676e2d2015-04-24 17:39:18 -07001994func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001995 // NDK libraries can't have any dependencies
1996 return CCDeps{}
1997}
1998
1999func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2000 module := &ndkPrebuiltLibrary{}
2001 module.LibraryProperties.BuildShared = true
2002 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2003}
2004
2005func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002006 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002007 // A null build step, but it sets up the output path.
2008 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2009 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2010 }
2011
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002012 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
2013 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002014
Dan Willemsen490fd492015-11-24 17:53:15 -08002015 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07002016 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07002017}
2018
2019func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07002020 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07002021}
2022
2023// The NDK STLs are slightly different from the prebuilt system libraries:
2024// * Are not specific to each platform version.
2025// * The libraries are not in a predictable location for each STL.
2026
2027type ndkPrebuiltStl struct {
2028 ndkPrebuiltLibrary
2029}
2030
2031type ndkPrebuiltStaticStl struct {
2032 ndkPrebuiltStl
2033}
2034
2035type ndkPrebuiltSharedStl struct {
2036 ndkPrebuiltStl
2037}
2038
2039func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2040 module := &ndkPrebuiltSharedStl{}
2041 module.LibraryProperties.BuildShared = true
2042 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2043}
2044
2045func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2046 module := &ndkPrebuiltStaticStl{}
2047 module.LibraryProperties.BuildStatic = true
2048 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2049}
2050
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002051func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002052 gccVersion := toolchain.GccVersion()
2053 var libDir string
2054 switch stl {
2055 case "libstlport":
2056 libDir = "cxx-stl/stlport/libs"
2057 case "libc++":
2058 libDir = "cxx-stl/llvm-libc++/libs"
2059 case "libgnustl":
2060 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2061 }
2062
2063 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002064 ndkSrcRoot := "prebuilts/ndk/current/sources"
2065 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002066 }
2067
2068 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002069 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002070}
2071
2072func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002073 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002074 // A null build step, but it sets up the output path.
2075 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2076 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2077 }
2078
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002079 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002080 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002081
2082 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002083 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002084 if c.LibraryProperties.BuildStatic {
2085 libExt = staticLibraryExtension
2086 }
2087
2088 stlName := strings.TrimSuffix(libName, "_shared")
2089 stlName = strings.TrimSuffix(stlName, "_static")
2090 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002091 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002092}
2093
Colin Cross6362e272015-10-29 15:25:03 -07002094func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002095 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002096 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002097 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002098 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002099 modules[0].(ccLinkedInterface).setStatic(true)
2100 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002101 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002102 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002103 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002104 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002105 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002106 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002107 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002108 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002109 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002110
2111 if _, ok := c.(ccLibraryInterface); ok {
2112 reuseFrom := modules[0].(ccLibraryInterface)
2113 for _, m := range modules {
2114 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002115 }
2116 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002117 }
2118}
Colin Cross74d1ec02015-04-28 13:30:13 -07002119
2120// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2121// modifies the slice contents in place, and returns a subslice of the original slice
2122func lastUniqueElements(list []string) []string {
2123 totalSkip := 0
2124 for i := len(list) - 1; i >= totalSkip; i-- {
2125 skip := 0
2126 for j := i - 1; j >= totalSkip; j-- {
2127 if list[i] == list[j] {
2128 skip++
2129 } else {
2130 list[j+skip] = list[j]
2131 }
2132 }
2133 totalSkip += skip
2134 }
2135 return list[totalSkip:]
2136}
Colin Cross06a931b2015-10-28 17:23:31 -07002137
2138var Bool = proptools.Bool