blob: eb9c58fdc6cf9a6adae057191b4b5d744a3b3d82 [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() {
Dan Willemsen99db8c32016-03-03 18:05:38 -08001251 flags.LdFlags = append(flags.LdFlags,
1252 "-nostdlib",
1253 "-Wl,--gc-sections",
1254 )
Colin Cross3f40fa42015-01-30 17:27:36 -08001255 }
Colin Cross97ba0732015-03-23 17:50:24 -07001256
Colin Cross0af4b842015-04-30 16:36:18 -07001257 if ctx.Darwin() {
1258 flags.LdFlags = append(flags.LdFlags,
1259 "-dynamiclib",
1260 "-single_module",
1261 //"-read_only_relocs suppress",
Dan Willemsen490fd492015-11-24 17:53:15 -08001262 "-install_name @rpath/"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001263 )
1264 } else {
1265 flags.LdFlags = append(flags.LdFlags,
Colin Cross0af4b842015-04-30 16:36:18 -07001266 sharedFlag,
Dan Willemsen490fd492015-11-24 17:53:15 -08001267 "-Wl,-soname,"+libName+flags.Toolchain.ShlibSuffix(),
Colin Cross0af4b842015-04-30 16:36:18 -07001268 )
1269 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001270 }
Colin Cross97ba0732015-03-23 17:50:24 -07001271
1272 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001273}
1274
Colin Cross97ba0732015-03-23 17:50:24 -07001275func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001276 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001277
1278 staticFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001279 objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001280 c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001281
1282 objFiles = append(objFiles, objFilesStatic...)
Colin Cross21b9a242015-03-24 14:15:58 -07001283 objFiles = append(objFiles, deps.WholeStaticLibObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001284
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001285 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension)
Colin Cross3f40fa42015-01-30 17:27:36 -08001286
Colin Cross0af4b842015-04-30 16:36:18 -07001287 if ctx.Darwin() {
1288 TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1289 } else {
1290 TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile)
1291 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001292
1293 c.objFiles = objFiles
1294 c.out = outputFile
Colin Crossf2298272015-05-12 11:36:53 -07001295
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001296 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001297 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001298 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001299
1300 ctx.CheckbuildFile(outputFile)
1301}
1302
Colin Cross97ba0732015-03-23 17:50:24 -07001303func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001304 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001305
1306 sharedFlags := flags
Colin Cross581c1892015-04-07 16:50:10 -07001307 objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary,
Dan Willemsen2ef08f42015-06-30 18:15:24 -07001308 c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs)
Colin Cross3f40fa42015-01-30 17:27:36 -08001309
1310 objFiles = append(objFiles, objFilesShared...)
1311
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001312 outputFile := common.PathForModuleOut(ctx, ctx.ModuleName()+flags.Toolchain.ShlibSuffix())
Colin Cross3f40fa42015-01-30 17:27:36 -08001313
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001314 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001315
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001316 versionScript := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Version_script)
1317 unexportedSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Unexported_symbols_list)
1318 forceNotWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_not_weak_list)
1319 forceWeakSymbols := common.OptionalPathForModuleSrc(ctx, c.LibraryProperties.Force_symbols_weak_list)
Dan Willemsen93c28312015-12-04 14:59:08 -08001320 if !ctx.Darwin() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001321 if versionScript.Valid() {
1322 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript.String())
1323 linkerDeps = append(linkerDeps, versionScript.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001324 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001325 if unexportedSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001326 ctx.PropertyErrorf("unexported_symbols_list", "Only supported on Darwin")
1327 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001328 if forceNotWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001329 ctx.PropertyErrorf("force_symbols_not_weak_list", "Only supported on Darwin")
1330 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331 if forceWeakSymbols.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001332 ctx.PropertyErrorf("force_symbols_weak_list", "Only supported on Darwin")
1333 }
1334 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001335 if versionScript.Valid() {
Dan Willemsen93c28312015-12-04 14:59:08 -08001336 ctx.PropertyErrorf("version_script", "Not supported on Darwin")
1337 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001338 if unexportedSymbols.Valid() {
1339 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-unexported_symbols_list,"+unexportedSymbols.String())
1340 linkerDeps = append(linkerDeps, unexportedSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001341 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001342 if forceNotWeakSymbols.Valid() {
1343 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_not_weak_list,"+forceNotWeakSymbols.String())
1344 linkerDeps = append(linkerDeps, forceNotWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001345 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001346 if forceWeakSymbols.Valid() {
1347 sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,-force_symbols_weak_list,"+forceWeakSymbols.String())
1348 linkerDeps = append(linkerDeps, forceWeakSymbols.Path())
Dan Willemsen93c28312015-12-04 14:59:08 -08001349 }
Colin Crossaee540a2015-07-06 17:48:31 -07001350 }
1351
Colin Cross97ba0732015-03-23 17:50:24 -07001352 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001353 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false,
Dan Willemsen6203ac02015-11-24 12:58:57 -08001354 ccFlagsToBuilderFlags(sharedFlags), outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001355
1356 c.out = outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001357 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07001358 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Colin Crossa48f71f2015-11-16 18:00:41 -08001359 c.exportFlags = append(c.exportFlags, deps.ReexportedCflags...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001360}
1361
Colin Cross97ba0732015-03-23 17:50:24 -07001362func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001363 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001364
1365 // Reuse the object files from the matching static library if it exists
Colin Crossed4cf0b2015-03-26 14:43:45 -07001366 if c.getReuseFrom().ccLibrary() == c {
1367 c.reuseObjFiles = objFiles
Colin Cross3f40fa42015-01-30 17:27:36 -08001368 } else {
Colin Cross2732e9a2015-04-28 13:23:52 -07001369 if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil &&
1370 c.LibraryProperties.Shared.Cflags == nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001371 objFiles = append(common.Paths(nil), c.getReuseFrom().getReuseObjFiles()...)
Colin Cross2732e9a2015-04-28 13:23:52 -07001372 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001373 }
1374
Colin Crossed4cf0b2015-03-26 14:43:45 -07001375 if c.static() {
Colin Cross3f40fa42015-01-30 17:27:36 -08001376 c.compileStaticLibrary(ctx, flags, deps, objFiles)
1377 } else {
1378 c.compileSharedLibrary(ctx, flags, deps, objFiles)
1379 }
1380}
1381
Colin Cross97ba0732015-03-23 17:50:24 -07001382func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001383 // Static libraries do not get installed.
1384}
1385
Colin Cross97ba0732015-03-23 17:50:24 -07001386func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001387 installDir := "lib"
Colin Cross97ba0732015-03-23 17:50:24 -07001388 if flags.Toolchain.Is64Bit() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001389 installDir = "lib64"
1390 }
1391
Dan Willemsen782a2d12015-12-21 14:55:28 -08001392 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, c.Properties.Relative_install_path), c.out)
Dan Albertc403f7c2015-03-18 14:01:18 -07001393}
1394
Colin Cross97ba0732015-03-23 17:50:24 -07001395func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001396 if c.static() {
Dan Albertc403f7c2015-03-18 14:01:18 -07001397 c.installStaticLibrary(ctx, flags)
1398 } else {
1399 c.installSharedLibrary(ctx, flags)
1400 }
1401}
1402
Colin Cross3f40fa42015-01-30 17:27:36 -08001403//
1404// Objects (for crt*.o)
1405//
1406
Dan Albertc3144b12015-04-28 18:17:56 -07001407type ccObjectProvider interface {
1408 object() *ccObject
1409}
1410
Colin Cross3f40fa42015-01-30 17:27:36 -08001411type ccObject struct {
Colin Crossfa138792015-04-24 17:31:52 -07001412 CCBase
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001413 out common.OptionalPath
Colin Cross3f40fa42015-01-30 17:27:36 -08001414}
1415
Dan Albertc3144b12015-04-28 18:17:56 -07001416func (c *ccObject) object() *ccObject {
1417 return c
1418}
1419
Colin Cross97ba0732015-03-23 17:50:24 -07001420func CCObjectFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001421 module := &ccObject{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001422
Colin Crossfa138792015-04-24 17:31:52 -07001423 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
Colin Cross3f40fa42015-01-30 17:27:36 -08001424}
1425
Colin Cross0676e2d2015-04-24 17:39:18 -07001426func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross21b9a242015-03-24 14:15:58 -07001427 // object files can't have any dynamic dependencies
1428 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001429}
1430
1431func (c *ccObject) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001432 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001433
Colin Cross97ba0732015-03-23 17:50:24 -07001434 objFiles = append(objFiles, deps.ObjFiles...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001435
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001436 var outputFile common.Path
Colin Cross3f40fa42015-01-30 17:27:36 -08001437 if len(objFiles) == 1 {
1438 outputFile = objFiles[0]
1439 } else {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001440 output := common.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
1441 TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), output)
1442 outputFile = output
Colin Cross3f40fa42015-01-30 17:27:36 -08001443 }
1444
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001445 c.out = common.OptionalPathForPath(outputFile)
Colin Cross3f40fa42015-01-30 17:27:36 -08001446
1447 ctx.CheckbuildFile(outputFile)
1448}
1449
Colin Cross97ba0732015-03-23 17:50:24 -07001450func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001451 // Object files do not get installed.
1452}
1453
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001454func (c *ccObject) outputFile() common.OptionalPath {
Colin Cross3f40fa42015-01-30 17:27:36 -08001455 return c.out
1456}
1457
Dan Albertc3144b12015-04-28 18:17:56 -07001458var _ ccObjectProvider = (*ccObject)(nil)
1459
Colin Cross3f40fa42015-01-30 17:27:36 -08001460//
1461// Executables
1462//
1463
Colin Cross7d5136f2015-05-11 13:39:40 -07001464type CCBinaryProperties struct {
1465 // compile executable with -static
Colin Cross06a931b2015-10-28 17:23:31 -07001466 Static_executable *bool
Colin Cross7d5136f2015-05-11 13:39:40 -07001467
1468 // set the name of the output
1469 Stem string `android:"arch_variant"`
1470
1471 // append to the name of the output
1472 Suffix string `android:"arch_variant"`
1473
1474 // if set, add an extra objcopy --prefix-symbols= step
1475 Prefix_symbols string
1476}
1477
Colin Cross97ba0732015-03-23 17:50:24 -07001478type CCBinary struct {
Colin Crossfa138792015-04-24 17:31:52 -07001479 CCLinked
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001480 out common.Path
1481 installFile common.Path
Colin Cross7d5136f2015-05-11 13:39:40 -07001482 BinaryProperties CCBinaryProperties
Colin Cross3f40fa42015-01-30 17:27:36 -08001483}
1484
Colin Crossed4cf0b2015-03-26 14:43:45 -07001485func (c *CCBinary) buildStatic() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001486 return Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001487}
1488
1489func (c *CCBinary) buildShared() bool {
Colin Cross06a931b2015-10-28 17:23:31 -07001490 return !Bool(c.BinaryProperties.Static_executable)
Colin Crossed4cf0b2015-03-26 14:43:45 -07001491}
1492
Colin Cross97ba0732015-03-23 17:50:24 -07001493func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string {
Colin Cross4ae185c2015-03-26 15:12:10 -07001494 stem := ctx.ModuleName()
Colin Cross97ba0732015-03-23 17:50:24 -07001495 if c.BinaryProperties.Stem != "" {
Colin Cross4ae185c2015-03-26 15:12:10 -07001496 stem = c.BinaryProperties.Stem
Colin Cross3f40fa42015-01-30 17:27:36 -08001497 }
Colin Cross4ae185c2015-03-26 15:12:10 -07001498
1499 return stem + c.BinaryProperties.Suffix
Colin Cross3f40fa42015-01-30 17:27:36 -08001500}
1501
Colin Cross0676e2d2015-04-24 17:39:18 -07001502func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Crosse11befc2015-04-27 17:49:17 -07001503 depNames = c.CCLinked.depNames(ctx, depNames)
Colin Crossf6566ed2015-03-24 11:13:38 -07001504 if ctx.Device() {
Dan Albertc3144b12015-04-28 18:17:56 -07001505 if c.Properties.Sdk_version == "" {
Colin Cross06a931b2015-10-28 17:23:31 -07001506 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001507 depNames.CrtBegin = "crtbegin_static"
1508 } else {
1509 depNames.CrtBegin = "crtbegin_dynamic"
1510 }
1511 depNames.CrtEnd = "crtend_android"
Colin Cross3f40fa42015-01-30 17:27:36 -08001512 } else {
Colin Cross06a931b2015-10-28 17:23:31 -07001513 if Bool(c.BinaryProperties.Static_executable) {
Dan Albertc3144b12015-04-28 18:17:56 -07001514 depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version
1515 } else {
1516 depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version
1517 }
1518 depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version
Colin Cross3f40fa42015-01-30 17:27:36 -08001519 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001520
Colin Cross06a931b2015-10-28 17:23:31 -07001521 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross74d1ec02015-04-28 13:30:13 -07001522 if c.stl(ctx) == "libc++_static" {
1523 depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl")
1524 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07001525 // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with
1526 // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs,
1527 // move them to the beginning of deps.LateStaticLibs
1528 var groupLibs []string
1529 depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs,
1530 []string{"libc", "libc_nomalloc", "libcompiler_rt"})
1531 depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...)
1532 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001533 }
Colin Cross21b9a242015-03-24 14:15:58 -07001534 return depNames
Colin Cross3f40fa42015-01-30 17:27:36 -08001535}
1536
Colin Cross97ba0732015-03-23 17:50:24 -07001537func NewCCBinary(binary *CCBinary, module CCModuleType,
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001538 hod common.HostOrDeviceSupported, multilib common.Multilib,
1539 props ...interface{}) (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001540
Colin Cross1f8f2342015-03-26 16:09:47 -07001541 props = append(props, &binary.BinaryProperties)
1542
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001543 return newCCDynamic(&binary.CCLinked, module, hod, multilib, props...)
Colin Cross3f40fa42015-01-30 17:27:36 -08001544}
1545
Colin Cross97ba0732015-03-23 17:50:24 -07001546func CCBinaryFactory() (blueprint.Module, []interface{}) {
1547 module := &CCBinary{}
1548
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001549 return NewCCBinary(module, module, common.HostAndDeviceSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001550}
1551
Colin Cross6362e272015-10-29 15:25:03 -07001552func (c *CCBinary) ModifyProperties(ctx CCModuleContext) {
Colin Cross0af4b842015-04-30 16:36:18 -07001553 if ctx.Darwin() {
Colin Cross06a931b2015-10-28 17:23:31 -07001554 c.BinaryProperties.Static_executable = proptools.BoolPtr(false)
Colin Cross0af4b842015-04-30 16:36:18 -07001555 }
Colin Cross06a931b2015-10-28 17:23:31 -07001556 if Bool(c.BinaryProperties.Static_executable) {
Colin Cross18b6dc52015-04-28 13:20:37 -07001557 c.dynamicProperties.VariantIsStaticBinary = true
1558 }
1559}
1560
Colin Cross0676e2d2015-04-24 17:39:18 -07001561func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Crosse11befc2015-04-27 17:49:17 -07001562 flags = c.CCLinked.flags(ctx, flags)
Colin Cross21b9a242015-03-24 14:15:58 -07001563
Dan Willemsen490fd492015-11-24 17:53:15 -08001564 if ctx.Host() {
1565 flags.LdFlags = append(flags.LdFlags, "-pie")
1566 if ctx.HostType() == common.Windows {
1567 flags.LdFlags = append(flags.LdFlags, "-Wl,-e_mainCRTStartup")
1568 }
1569 }
1570
1571 // MinGW spits out warnings about -fPIC even for -fpie?!) being ignored because
1572 // all code is position independent, and then those warnings get promoted to
1573 // errors.
1574 if ctx.HostType() != common.Windows {
1575 flags.CFlags = append(flags.CFlags, "-fpie")
1576 }
Colin Cross97ba0732015-03-23 17:50:24 -07001577
Colin Crossf6566ed2015-03-24 11:13:38 -07001578 if ctx.Device() {
Colin Cross06a931b2015-10-28 17:23:31 -07001579 if Bool(c.BinaryProperties.Static_executable) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07001580 // Clang driver needs -static to create static executable.
1581 // However, bionic/linker uses -shared to overwrite.
1582 // Linker for x86 targets does not allow coexistance of -static and -shared,
1583 // so we add -static only if -shared is not used.
1584 if !inList("-shared", flags.LdFlags) {
1585 flags.LdFlags = append(flags.LdFlags, "-static")
1586 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001587
Colin Crossed4cf0b2015-03-26 14:43:45 -07001588 flags.LdFlags = append(flags.LdFlags,
1589 "-nostdlib",
1590 "-Bstatic",
1591 "-Wl,--gc-sections",
1592 )
1593
1594 } else {
1595 linker := "/system/bin/linker"
1596 if flags.Toolchain.Is64Bit() {
1597 linker = "/system/bin/linker64"
1598 }
1599
1600 flags.LdFlags = append(flags.LdFlags,
Colin Cross979422c2015-12-01 14:09:48 -08001601 "-pie",
Colin Crossed4cf0b2015-03-26 14:43:45 -07001602 "-nostdlib",
1603 "-Bdynamic",
1604 fmt.Sprintf("-Wl,-dynamic-linker,%s", linker),
1605 "-Wl,--gc-sections",
1606 "-Wl,-z,nocopyreloc",
1607 )
1608 }
Colin Cross0af4b842015-04-30 16:36:18 -07001609 } else if ctx.Darwin() {
1610 flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names")
Colin Cross3f40fa42015-01-30 17:27:36 -08001611 }
1612
Colin Cross97ba0732015-03-23 17:50:24 -07001613 return flags
Colin Cross3f40fa42015-01-30 17:27:36 -08001614}
1615
Colin Cross97ba0732015-03-23 17:50:24 -07001616func (c *CCBinary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001618
Colin Cross06a931b2015-10-28 17:23:31 -07001619 if !Bool(c.BinaryProperties.Static_executable) && inList("libc", c.Properties.Static_libs) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001620 ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" +
1621 "from static libs or set static_executable: true")
1622 }
1623
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001624 outputFile := common.PathForModuleOut(ctx, c.getStem(ctx)+flags.Toolchain.ExecutableSuffix())
Dan Albertc403f7c2015-03-18 14:01:18 -07001625 c.out = outputFile
Colin Crossbfae8852015-03-26 14:44:11 -07001626 if c.BinaryProperties.Prefix_symbols != "" {
1627 afterPrefixSymbols := outputFile
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001628 outputFile = common.PathForModuleOut(ctx, c.getStem(ctx)+".intermediate")
Colin Crossbfae8852015-03-26 14:44:11 -07001629 TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile,
1630 ccFlagsToBuilderFlags(flags), afterPrefixSymbols)
1631 }
Colin Cross3f40fa42015-01-30 17:27:36 -08001632
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001633 var linkerDeps common.Paths
Colin Crossaee540a2015-07-06 17:48:31 -07001634
Colin Cross97ba0732015-03-23 17:50:24 -07001635 TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs,
Colin Crossaee540a2015-07-06 17:48:31 -07001636 deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true,
Colin Cross77b00fa2015-03-16 16:15:49 -07001637 ccFlagsToBuilderFlags(flags), outputFile)
Dan Albertc403f7c2015-03-18 14:01:18 -07001638}
Colin Cross3f40fa42015-01-30 17:27:36 -08001639
Colin Cross97ba0732015-03-23 17:50:24 -07001640func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001641 c.installFile = ctx.InstallFile(common.PathForModuleInstall(ctx, "bin", c.Properties.Relative_install_path), c.out)
Colin Crossd350ecd2015-04-28 13:25:36 -07001642}
1643
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001644func (c *CCBinary) HostToolPath() common.OptionalPath {
Colin Crossd350ecd2015-04-28 13:25:36 -07001645 if c.HostOrDevice().Host() {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001646 return common.OptionalPathForPath(c.installFile)
Colin Crossd350ecd2015-04-28 13:25:36 -07001647 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001648 return common.OptionalPath{}
Dan Albertc403f7c2015-03-18 14:01:18 -07001649}
1650
Colin Cross6002e052015-09-16 16:00:08 -07001651func (c *CCBinary) binary() *CCBinary {
1652 return c
1653}
1654
1655type testPerSrc interface {
1656 binary() *CCBinary
1657 testPerSrc() bool
1658}
1659
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001660var _ testPerSrc = (*CCTest)(nil)
Colin Cross6002e052015-09-16 16:00:08 -07001661
Colin Cross6362e272015-10-29 15:25:03 -07001662func testPerSrcMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Cross6002e052015-09-16 16:00:08 -07001663 if test, ok := mctx.Module().(testPerSrc); ok {
1664 if test.testPerSrc() {
1665 testNames := make([]string, len(test.binary().Properties.Srcs))
1666 for i, src := range test.binary().Properties.Srcs {
1667 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
1668 }
1669 tests := mctx.CreateLocalVariations(testNames...)
1670 for i, src := range test.binary().Properties.Srcs {
1671 tests[i].(testPerSrc).binary().Properties.Srcs = []string{src}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001672 tests[i].(testPerSrc).binary().BinaryProperties.Stem = testNames[i]
Colin Cross6002e052015-09-16 16:00:08 -07001673 }
1674 }
1675 }
Colin Cross7d5136f2015-05-11 13:39:40 -07001676}
1677
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001678type CCTestProperties struct {
1679 // if set, build against the gtest library. Defaults to true.
1680 Gtest bool
1681
1682 // Create a separate binary for each source file. Useful when there is
1683 // global state that can not be torn down and reset between each test suite.
1684 Test_per_src *bool
1685}
1686
Colin Cross9ffb4f52015-04-24 17:48:09 -07001687type CCTest struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001688 CCBinary
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001689
1690 TestProperties CCTestProperties
Dan Albertc403f7c2015-03-18 14:01:18 -07001691}
1692
Colin Cross9ffb4f52015-04-24 17:48:09 -07001693func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags {
Colin Cross0676e2d2015-04-24 17:39:18 -07001694 flags = c.CCBinary.flags(ctx, flags)
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001695 if !c.TestProperties.Gtest {
1696 return flags
1697 }
Dan Albertc403f7c2015-03-18 14:01:18 -07001698
Colin Cross97ba0732015-03-23 17:50:24 -07001699 flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Crossf6566ed2015-03-24 11:13:38 -07001700 if ctx.Host() {
Colin Cross97ba0732015-03-23 17:50:24 -07001701 flags.CFlags = append(flags.CFlags, "-O0", "-g")
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001702
1703 if ctx.HostType() == common.Windows {
1704 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_WINDOWS")
1705 } else {
1706 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX")
1707 flags.LdFlags = append(flags.LdFlags, "-lpthread")
1708 }
1709 } else {
1710 flags.CFlags = append(flags.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Dan Albertc403f7c2015-03-18 14:01:18 -07001711 }
1712
1713 // TODO(danalbert): Make gtest export its dependencies.
Colin Cross28344522015-04-22 13:07:53 -07001714 flags.CFlags = append(flags.CFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001715 "-I"+common.PathForSource(ctx, "external/gtest/include").String())
Dan Albertc403f7c2015-03-18 14:01:18 -07001716
Colin Cross21b9a242015-03-24 14:15:58 -07001717 return flags
Dan Albertc403f7c2015-03-18 14:01:18 -07001718}
1719
Colin Cross9ffb4f52015-04-24 17:48:09 -07001720func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001721 if c.TestProperties.Gtest {
1722 depNames.StaticLibs = append(depNames.StaticLibs, "libgtest_main", "libgtest")
1723 }
Colin Crossa8a93d32015-04-28 13:26:49 -07001724 depNames = c.CCBinary.depNames(ctx, depNames)
Colin Cross21b9a242015-03-24 14:15:58 -07001725 return depNames
Dan Albertc403f7c2015-03-18 14:01:18 -07001726}
1727
Dan Willemsen782a2d12015-12-21 14:55:28 -08001728func (c *CCTest) InstallInData() bool {
1729 return true
1730}
1731
Colin Cross9ffb4f52015-04-24 17:48:09 -07001732func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001733 installDir := "nativetest"
1734 if flags.Toolchain.Is64Bit() {
1735 installDir = "nativetest64"
Dan Albertc403f7c2015-03-18 14:01:18 -07001736 }
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001737 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
1738}
1739
1740func (c *CCTest) testPerSrc() bool {
1741 return Bool(c.TestProperties.Test_per_src)
Dan Albertc403f7c2015-03-18 14:01:18 -07001742}
1743
Colin Cross9ffb4f52015-04-24 17:48:09 -07001744func NewCCTest(test *CCTest, module CCModuleType,
1745 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1746
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001747 props = append(props, &test.TestProperties)
1748
1749 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibBoth, props...)
Colin Cross9ffb4f52015-04-24 17:48:09 -07001750}
1751
1752func CCTestFactory() (blueprint.Module, []interface{}) {
1753 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001754 module.TestProperties.Gtest = true
Colin Cross9ffb4f52015-04-24 17:48:09 -07001755
1756 return NewCCTest(module, module, common.HostAndDeviceSupported)
1757}
1758
Colin Cross2ba19d92015-05-07 15:44:20 -07001759type CCBenchmark struct {
1760 CCBinary
1761}
1762
1763func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1764 depNames = c.CCBinary.depNames(ctx, depNames)
Dan Willemsenf8e98b02015-09-11 17:41:44 -07001765 depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase")
Colin Cross2ba19d92015-05-07 15:44:20 -07001766 return depNames
1767}
1768
Dan Willemsen782a2d12015-12-21 14:55:28 -08001769func (c *CCBenchmark) InstallInData() bool {
1770 return true
1771}
1772
Colin Cross2ba19d92015-05-07 15:44:20 -07001773func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1774 if ctx.Device() {
Dan Willemsen782a2d12015-12-21 14:55:28 -08001775 installDir := "nativetest"
1776 if flags.Toolchain.Is64Bit() {
1777 installDir = "nativetest64"
1778 }
1779 ctx.InstallFile(common.PathForModuleInstall(ctx, installDir, ctx.ModuleName()), c.out)
Colin Cross2ba19d92015-05-07 15:44:20 -07001780 } else {
1781 c.CCBinary.installModule(ctx, flags)
1782 }
1783}
1784
1785func NewCCBenchmark(test *CCBenchmark, module CCModuleType,
1786 hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) {
1787
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001788 return NewCCBinary(&test.CCBinary, module, hod, common.MultilibFirst, props...)
Colin Cross2ba19d92015-05-07 15:44:20 -07001789}
1790
1791func CCBenchmarkFactory() (blueprint.Module, []interface{}) {
1792 module := &CCBenchmark{}
1793
1794 return NewCCBenchmark(module, module, common.HostAndDeviceSupported)
1795}
1796
Colin Cross3f40fa42015-01-30 17:27:36 -08001797//
1798// Static library
1799//
1800
Colin Cross97ba0732015-03-23 17:50:24 -07001801func CCLibraryStaticFactory() (blueprint.Module, []interface{}) {
1802 module := &CCLibrary{}
1803 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001804
Colin Cross97ba0732015-03-23 17:50:24 -07001805 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001806}
1807
1808//
1809// Shared libraries
1810//
1811
Colin Cross97ba0732015-03-23 17:50:24 -07001812func CCLibrarySharedFactory() (blueprint.Module, []interface{}) {
1813 module := &CCLibrary{}
1814 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001815
Colin Cross97ba0732015-03-23 17:50:24 -07001816 return NewCCLibrary(module, module, common.HostAndDeviceSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001817}
1818
1819//
1820// Host static library
1821//
1822
Colin Cross97ba0732015-03-23 17:50:24 -07001823func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) {
1824 module := &CCLibrary{}
1825 module.LibraryProperties.BuildStatic = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001826
Colin Cross97ba0732015-03-23 17:50:24 -07001827 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001828}
1829
1830//
1831// Host Shared libraries
1832//
1833
Colin Cross97ba0732015-03-23 17:50:24 -07001834func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) {
1835 module := &CCLibrary{}
1836 module.LibraryProperties.BuildShared = true
Colin Cross3f40fa42015-01-30 17:27:36 -08001837
Colin Cross97ba0732015-03-23 17:50:24 -07001838 return NewCCLibrary(module, module, common.HostSupported)
Colin Cross3f40fa42015-01-30 17:27:36 -08001839}
1840
1841//
1842// Host Binaries
1843//
1844
Colin Cross97ba0732015-03-23 17:50:24 -07001845func CCBinaryHostFactory() (blueprint.Module, []interface{}) {
1846 module := &CCBinary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001847
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001848 return NewCCBinary(module, module, common.HostSupported, common.MultilibFirst)
Colin Cross3f40fa42015-01-30 17:27:36 -08001849}
1850
1851//
Colin Cross1f8f2342015-03-26 16:09:47 -07001852// Host Tests
1853//
1854
1855func CCTestHostFactory() (blueprint.Module, []interface{}) {
Colin Cross9ffb4f52015-04-24 17:48:09 -07001856 module := &CCTest{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001857 return NewCCTest(module, module, common.HostSupported)
Colin Cross1f8f2342015-03-26 16:09:47 -07001858}
1859
1860//
Colin Cross2ba19d92015-05-07 15:44:20 -07001861// Host Benchmarks
1862//
1863
1864func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) {
1865 module := &CCBenchmark{}
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001866 return NewCCBinary(&module.CCBinary, module, common.HostSupported, common.MultilibFirst)
Colin Cross2ba19d92015-05-07 15:44:20 -07001867}
1868
1869//
Colin Crosscfad1192015-11-02 16:43:11 -08001870// Defaults
1871//
1872type CCDefaults struct {
1873 common.AndroidModuleBase
1874 common.DefaultsModule
1875}
1876
1877func (*CCDefaults) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) {
1878}
1879
1880func CCDefaultsFactory() (blueprint.Module, []interface{}) {
1881 module := &CCDefaults{}
1882
1883 propertyStructs := []interface{}{
1884 &CCBaseProperties{},
1885 &CCLibraryProperties{},
1886 &CCBinaryProperties{},
Dan Willemsen10d52fd2015-12-21 15:25:58 -08001887 &CCTestProperties{},
Colin Crosscfad1192015-11-02 16:43:11 -08001888 &CCUnusedProperties{},
1889 }
1890
Dan Willemsen218f6562015-07-08 18:13:11 -07001891 _, propertyStructs = common.InitAndroidArchModule(module, common.HostAndDeviceDefault,
1892 common.MultilibDefault, propertyStructs...)
Colin Crosscfad1192015-11-02 16:43:11 -08001893
1894 return common.InitDefaultsModule(module, module, propertyStructs...)
1895}
1896
1897//
Colin Cross3f40fa42015-01-30 17:27:36 -08001898// Device libraries shipped with gcc
1899//
1900
1901type toolchainLibrary struct {
Colin Cross97ba0732015-03-23 17:50:24 -07001902 CCLibrary
Colin Cross3f40fa42015-01-30 17:27:36 -08001903}
1904
Colin Cross0676e2d2015-04-24 17:39:18 -07001905func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Colin Cross3f40fa42015-01-30 17:27:36 -08001906 // toolchain libraries can't have any dependencies
Colin Cross21b9a242015-03-24 14:15:58 -07001907 return CCDeps{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001908}
1909
Colin Cross97ba0732015-03-23 17:50:24 -07001910func ToolchainLibraryFactory() (blueprint.Module, []interface{}) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001911 module := &toolchainLibrary{}
Colin Cross3f40fa42015-01-30 17:27:36 -08001912
Colin Cross97ba0732015-03-23 17:50:24 -07001913 module.LibraryProperties.BuildStatic = true
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001914 module.Properties.Clang = proptools.BoolPtr(false)
Colin Cross97ba0732015-03-23 17:50:24 -07001915
Colin Crossfa138792015-04-24 17:31:52 -07001916 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth,
Colin Cross21b9a242015-03-24 14:15:58 -07001917 &module.LibraryProperties)
Colin Cross3f40fa42015-01-30 17:27:36 -08001918}
1919
1920func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001921 flags CCFlags, deps CCPathDeps, objFiles common.Paths) {
Colin Cross3f40fa42015-01-30 17:27:36 -08001922
1923 libName := ctx.ModuleName() + staticLibraryExtension
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001924 outputFile := common.PathForModuleOut(ctx, libName)
Colin Cross3f40fa42015-01-30 17:27:36 -08001925
Dan Willemsenfc9c28c2016-01-12 16:22:40 -08001926 if flags.Clang {
1927 ctx.ModuleErrorf("toolchain_library must use GCC, not Clang")
1928 }
1929
Colin Cross3f40fa42015-01-30 17:27:36 -08001930 CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile)
1931
1932 c.out = outputFile
1933
1934 ctx.CheckbuildFile(outputFile)
1935}
1936
Colin Cross97ba0732015-03-23 17:50:24 -07001937func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc403f7c2015-03-18 14:01:18 -07001938 // Toolchain libraries do not get installed.
1939}
1940
Dan Albertbe961682015-03-18 23:38:50 -07001941// NDK prebuilt libraries.
1942//
1943// These differ from regular prebuilts in that they aren't stripped and usually aren't installed
1944// either (with the exception of the shared STLs, which are installed to the app's directory rather
1945// than to the system image).
1946
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001947func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) common.SourcePath {
1948 return common.PathForSource(ctx, fmt.Sprintf("prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib",
1949 version, toolchain.Name()))
Dan Albertbe961682015-03-18 23:38:50 -07001950}
1951
Dan Albertc3144b12015-04-28 18:17:56 -07001952func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001953 ext string, version string) common.Path {
Dan Albertc3144b12015-04-28 18:17:56 -07001954
1955 // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION.
1956 // We want to translate to just NAME.EXT
1957 name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0]
1958 dir := getNdkLibDir(ctx, toolchain, version)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001959 return dir.Join(ctx, name+ext)
Dan Albertc3144b12015-04-28 18:17:56 -07001960}
1961
1962type ndkPrebuiltObject struct {
1963 ccObject
1964}
1965
Dan Albertc3144b12015-04-28 18:17:56 -07001966func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
1967 // NDK objects can't have any dependencies
1968 return CCDeps{}
1969}
1970
1971func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) {
1972 module := &ndkPrebuiltObject{}
1973 return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth)
1974}
1975
1976func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001977 deps CCPathDeps, objFiles common.Paths) {
Dan Albertc3144b12015-04-28 18:17:56 -07001978 // A null build step, but it sets up the output path.
1979 if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") {
1980 ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name")
1981 }
1982
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001983 c.out = common.OptionalPathForPath(ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version))
Dan Albertc3144b12015-04-28 18:17:56 -07001984}
1985
1986func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
1987 // Objects do not get installed.
1988}
1989
1990var _ ccObjectProvider = (*ndkPrebuiltObject)(nil)
1991
Dan Albertbe961682015-03-18 23:38:50 -07001992type ndkPrebuiltLibrary struct {
1993 CCLibrary
1994}
1995
Colin Cross0676e2d2015-04-24 17:39:18 -07001996func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps {
Dan Albertbe961682015-03-18 23:38:50 -07001997 // NDK libraries can't have any dependencies
1998 return CCDeps{}
1999}
2000
2001func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) {
2002 module := &ndkPrebuiltLibrary{}
2003 module.LibraryProperties.BuildShared = true
2004 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2005}
2006
2007func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002008 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002009 // A null build step, but it sets up the output path.
2010 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2011 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2012 }
2013
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002014 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
2015 c.exportFlags = []string{common.JoinWithPrefix(includeDirs.Strings(), "-isystem ")}
Dan Albertbe961682015-03-18 23:38:50 -07002016
Dan Willemsen490fd492015-11-24 17:53:15 -08002017 c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, flags.Toolchain.ShlibSuffix(),
Dan Albertc3144b12015-04-28 18:17:56 -07002018 c.Properties.Sdk_version)
Dan Albertbe961682015-03-18 23:38:50 -07002019}
2020
2021func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) {
Dan Albertc3144b12015-04-28 18:17:56 -07002022 // NDK prebuilt libraries do not get installed.
Dan Albertbe961682015-03-18 23:38:50 -07002023}
2024
2025// The NDK STLs are slightly different from the prebuilt system libraries:
2026// * Are not specific to each platform version.
2027// * The libraries are not in a predictable location for each STL.
2028
2029type ndkPrebuiltStl struct {
2030 ndkPrebuiltLibrary
2031}
2032
2033type ndkPrebuiltStaticStl struct {
2034 ndkPrebuiltStl
2035}
2036
2037type ndkPrebuiltSharedStl struct {
2038 ndkPrebuiltStl
2039}
2040
2041func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) {
2042 module := &ndkPrebuiltSharedStl{}
2043 module.LibraryProperties.BuildShared = true
2044 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2045}
2046
2047func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) {
2048 module := &ndkPrebuiltStaticStl{}
2049 module.LibraryProperties.BuildStatic = true
2050 return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported)
2051}
2052
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002053func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) common.SourcePath {
Dan Albertbe961682015-03-18 23:38:50 -07002054 gccVersion := toolchain.GccVersion()
2055 var libDir string
2056 switch stl {
2057 case "libstlport":
2058 libDir = "cxx-stl/stlport/libs"
2059 case "libc++":
2060 libDir = "cxx-stl/llvm-libc++/libs"
2061 case "libgnustl":
2062 libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion)
2063 }
2064
2065 if libDir != "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002066 ndkSrcRoot := "prebuilts/ndk/current/sources"
2067 return common.PathForSource(ctx, ndkSrcRoot).Join(ctx, libDir, ctx.Arch().Abi[0])
Dan Albertbe961682015-03-18 23:38:50 -07002068 }
2069
2070 ctx.ModuleErrorf("Unknown NDK STL: %s", stl)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002071 return common.PathForSource(ctx, "")
Dan Albertbe961682015-03-18 23:38:50 -07002072}
2073
2074func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags,
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002075 deps CCPathDeps, objFiles common.Paths) {
Dan Albertbe961682015-03-18 23:38:50 -07002076 // A null build step, but it sets up the output path.
2077 if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") {
2078 ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name")
2079 }
2080
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002081 includeDirs := common.PathsForModuleSrc(ctx, c.Properties.Export_include_dirs)
Colin Cross28344522015-04-22 13:07:53 -07002082 c.exportFlags = []string{includeDirsToFlags(includeDirs)}
Dan Albertbe961682015-03-18 23:38:50 -07002083
2084 libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_")
Dan Willemsen490fd492015-11-24 17:53:15 -08002085 libExt := flags.Toolchain.ShlibSuffix()
Dan Albertbe961682015-03-18 23:38:50 -07002086 if c.LibraryProperties.BuildStatic {
2087 libExt = staticLibraryExtension
2088 }
2089
2090 stlName := strings.TrimSuffix(libName, "_shared")
2091 stlName = strings.TrimSuffix(stlName, "_static")
2092 libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002093 c.out = libDir.Join(ctx, libName+libExt)
Dan Albertbe961682015-03-18 23:38:50 -07002094}
2095
Colin Cross6362e272015-10-29 15:25:03 -07002096func linkageMutator(mctx common.AndroidBottomUpMutatorContext) {
Colin Crossed4cf0b2015-03-26 14:43:45 -07002097 if c, ok := mctx.Module().(ccLinkedInterface); ok {
Colin Cross3f40fa42015-01-30 17:27:36 -08002098 var modules []blueprint.Module
Colin Crossed4cf0b2015-03-26 14:43:45 -07002099 if c.buildStatic() && c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002100 modules = mctx.CreateLocalVariations("static", "shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002101 modules[0].(ccLinkedInterface).setStatic(true)
2102 modules[1].(ccLinkedInterface).setStatic(false)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002103 } else if c.buildStatic() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002104 modules = mctx.CreateLocalVariations("static")
Colin Cross18b6dc52015-04-28 13:20:37 -07002105 modules[0].(ccLinkedInterface).setStatic(true)
Colin Crossed4cf0b2015-03-26 14:43:45 -07002106 } else if c.buildShared() {
Colin Cross3f40fa42015-01-30 17:27:36 -08002107 modules = mctx.CreateLocalVariations("shared")
Colin Cross18b6dc52015-04-28 13:20:37 -07002108 modules[0].(ccLinkedInterface).setStatic(false)
Colin Cross3f40fa42015-01-30 17:27:36 -08002109 } else {
Colin Cross97ba0732015-03-23 17:50:24 -07002110 panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName()))
Colin Cross3f40fa42015-01-30 17:27:36 -08002111 }
Colin Crossed4cf0b2015-03-26 14:43:45 -07002112
2113 if _, ok := c.(ccLibraryInterface); ok {
2114 reuseFrom := modules[0].(ccLibraryInterface)
2115 for _, m := range modules {
2116 m.(ccLibraryInterface).setReuseFrom(reuseFrom)
Colin Cross3f40fa42015-01-30 17:27:36 -08002117 }
2118 }
Colin Cross3f40fa42015-01-30 17:27:36 -08002119 }
2120}
Colin Cross74d1ec02015-04-28 13:30:13 -07002121
2122// lastUniqueElements returns all unique elements of a slice, keeping the last copy of each
2123// modifies the slice contents in place, and returns a subslice of the original slice
2124func lastUniqueElements(list []string) []string {
2125 totalSkip := 0
2126 for i := len(list) - 1; i >= totalSkip; i-- {
2127 skip := 0
2128 for j := i - 1; j >= totalSkip; j-- {
2129 if list[i] == list[j] {
2130 skip++
2131 } else {
2132 list[j+skip] = list[j]
2133 }
2134 }
2135 totalSkip += skip
2136 }
2137 return list[totalSkip:]
2138}
Colin Cross06a931b2015-10-28 17:23:31 -07002139
2140var Bool = proptools.Bool