Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 2 | // |
| 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 | |
| 15 | package 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 | |
| 21 | import ( |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | "fmt" |
| 23 | "path/filepath" |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 24 | "runtime" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | "strings" |
| 26 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint" |
| 28 | "github.com/google/blueprint/pathtools" |
| 29 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 30 | "android/soong" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | "android/soong/common" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 32 | "android/soong/genrule" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | ) |
| 34 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 35 | func init() { |
| 36 | soong.RegisterModuleType("cc_library_static", CCLibraryStaticFactory) |
| 37 | soong.RegisterModuleType("cc_library_shared", CCLibrarySharedFactory) |
| 38 | soong.RegisterModuleType("cc_library", CCLibraryFactory) |
| 39 | soong.RegisterModuleType("cc_object", CCObjectFactory) |
| 40 | soong.RegisterModuleType("cc_binary", CCBinaryFactory) |
| 41 | soong.RegisterModuleType("cc_test", CCTestFactory) |
| 42 | soong.RegisterModuleType("cc_benchmark", CCBenchmarkFactory) |
| 43 | |
| 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. |
| 59 | soong.RegisterEarlyMutator("link", LinkageMutator) |
| 60 | soong.RegisterEarlyMutator("test_per_src", TestPerSrcMutator) |
| 61 | } |
| 62 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 63 | var ( |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 64 | HostPrebuiltTag = pctx.VariableConfigMethod("HostPrebuiltTag", common.Config.PrebuiltOS) |
| 65 | SrcDir = pctx.VariableConfigMethod("SrcDir", common.Config.SrcDir) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 66 | |
| 67 | LibcRoot = pctx.StaticVariable("LibcRoot", "${SrcDir}/bionic/libc") |
| 68 | LibmRoot = pctx.StaticVariable("LibmRoot", "${SrcDir}/bionic/libm") |
| 69 | ) |
| 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 |
| 73 | var ( |
| 74 | commonGlobalCflags = []string{ |
| 75 | "-DANDROID", |
| 76 | "-fmessage-length=0", |
| 77 | "-W", |
| 78 | "-Wall", |
| 79 | "-Wno-unused", |
| 80 | "-Winit-self", |
| 81 | "-Wpointer-arith", |
| 82 | |
| 83 | // COMMON_RELEASE_CFLAGS |
| 84 | "-DNDEBUG", |
| 85 | "-UDEBUG", |
| 86 | } |
| 87 | |
| 88 | deviceGlobalCflags = []string{ |
| 89 | // TARGET_ERROR_FLAGS |
| 90 | "-Werror=return-type", |
| 91 | "-Werror=non-virtual-dtor", |
| 92 | "-Werror=address", |
| 93 | "-Werror=sequence-point", |
| 94 | } |
| 95 | |
| 96 | hostGlobalCflags = []string{} |
| 97 | |
| 98 | commonGlobalCppflags = []string{ |
| 99 | "-Wsign-promo", |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | illegalFlags = []string{ |
| 103 | "-w", |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 104 | } |
| 105 | ) |
| 106 | |
| 107 | func init() { |
| 108 | pctx.StaticVariable("commonGlobalCflags", strings.Join(commonGlobalCflags, " ")) |
| 109 | pctx.StaticVariable("deviceGlobalCflags", strings.Join(deviceGlobalCflags, " ")) |
| 110 | pctx.StaticVariable("hostGlobalCflags", strings.Join(hostGlobalCflags, " ")) |
| 111 | |
| 112 | pctx.StaticVariable("commonGlobalCppflags", strings.Join(commonGlobalCppflags, " ")) |
| 113 | |
| 114 | pctx.StaticVariable("commonClangGlobalCflags", |
| 115 | strings.Join(clangFilterUnknownCflags(commonGlobalCflags), " ")) |
| 116 | pctx.StaticVariable("deviceClangGlobalCflags", |
| 117 | strings.Join(clangFilterUnknownCflags(deviceGlobalCflags), " ")) |
| 118 | pctx.StaticVariable("hostClangGlobalCflags", |
| 119 | strings.Join(clangFilterUnknownCflags(hostGlobalCflags), " ")) |
Tim Kilbourn | f294814 | 2015-03-11 12:03:03 -0700 | [diff] [blame] | 120 | pctx.StaticVariable("commonClangGlobalCppflags", |
| 121 | strings.Join(clangFilterUnknownCflags(commonGlobalCppflags), " ")) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 122 | |
| 123 | // Everything in this list is a crime against abstraction and dependency tracking. |
| 124 | // Do not add anything to this list. |
| 125 | pctx.StaticVariable("commonGlobalIncludes", strings.Join([]string{ |
| 126 | "-isystem ${SrcDir}/system/core/include", |
| 127 | "-isystem ${SrcDir}/hardware/libhardware/include", |
| 128 | "-isystem ${SrcDir}/hardware/libhardware_legacy/include", |
| 129 | "-isystem ${SrcDir}/hardware/ril/include", |
| 130 | "-isystem ${SrcDir}/libnativehelper/include", |
| 131 | "-isystem ${SrcDir}/frameworks/native/include", |
| 132 | "-isystem ${SrcDir}/frameworks/native/opengl/include", |
| 133 | "-isystem ${SrcDir}/frameworks/av/include", |
| 134 | "-isystem ${SrcDir}/frameworks/base/include", |
| 135 | }, " ")) |
| 136 | |
| 137 | pctx.StaticVariable("clangPath", "${SrcDir}/prebuilts/clang/${HostPrebuiltTag}/host/3.6/bin/") |
| 138 | } |
| 139 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 140 | // Building C/C++ code is handled by objects that satisfy this interface via composition |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 141 | type CCModuleType interface { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 142 | common.AndroidModule |
| 143 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 144 | // Modify property values after parsing Blueprints file but before starting dependency |
| 145 | // resolution or build rule generation |
| 146 | ModifyProperties(common.AndroidBaseContext) |
| 147 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 148 | // Modify the ccFlags |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 149 | flags(common.AndroidModuleContext, CCFlags) CCFlags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 150 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 151 | // Return list of dependency names for use in AndroidDynamicDependencies and in depsToPaths |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 152 | depNames(common.AndroidBaseContext, CCDeps) CCDeps |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 153 | |
| 154 | // Compile objects into final module |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 155 | compileModule(common.AndroidModuleContext, CCFlags, CCDeps, []string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 156 | |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 157 | // Install the built module. |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 158 | installModule(common.AndroidModuleContext, CCFlags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 159 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 160 | // Return the output file (.o, .a or .so) for use by other modules |
| 161 | outputFile() string |
| 162 | } |
| 163 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 164 | type CCDeps struct { |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 165 | StaticLibs, SharedLibs, LateStaticLibs, WholeStaticLibs, ObjFiles, Cflags []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 166 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 167 | WholeStaticLibObjFiles []string |
| 168 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 169 | CrtBegin, CrtEnd string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 172 | type CCFlags struct { |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 173 | GlobalFlags []string // Flags that apply to C, C++, and assembly source files |
| 174 | AsFlags []string // Flags that apply to assembly source files |
| 175 | CFlags []string // Flags that apply to C and C++ source files |
| 176 | ConlyFlags []string // Flags that apply to C source files |
| 177 | CppFlags []string // Flags that apply to C++ source files |
| 178 | YaccFlags []string // Flags that apply to Yacc source files |
| 179 | LdFlags []string // Flags that apply to linker command lines |
| 180 | |
| 181 | Nocrt bool |
| 182 | Toolchain Toolchain |
| 183 | Clang bool |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 186 | // Properties used to compile all C or C++ modules |
| 187 | type CCBaseProperties struct { |
| 188 | // list of source files used to compile the C/C++ module. May be .c, .cpp, or .S files. |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 189 | Srcs []string `android:"arch_variant"` |
| 190 | |
| 191 | // list of source files that should not be used to build the C/C++ module. |
| 192 | // This is most useful in the arch/multilib variants to remove non-common files |
| 193 | Exclude_srcs []string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 194 | |
| 195 | // list of module-specific flags that will be used for C and C++ compiles. |
| 196 | Cflags []string `android:"arch_variant"` |
| 197 | |
| 198 | // list of module-specific flags that will be used for C++ compiles |
| 199 | Cppflags []string `android:"arch_variant"` |
| 200 | |
| 201 | // list of module-specific flags that will be used for C compiles |
| 202 | Conlyflags []string `android:"arch_variant"` |
| 203 | |
| 204 | // list of module-specific flags that will be used for .S compiles |
| 205 | Asflags []string `android:"arch_variant"` |
| 206 | |
| 207 | // list of module-specific flags that will be used for .y and .yy compiles |
| 208 | Yaccflags []string |
| 209 | |
| 210 | // list of module-specific flags that will be used for all link steps |
| 211 | Ldflags []string `android:"arch_variant"` |
| 212 | |
| 213 | // the instruction set architecture to use to compile the C/C++ |
| 214 | // module. |
| 215 | Instruction_set string `android:"arch_variant"` |
| 216 | |
| 217 | // list of directories relative to the root of the source tree that will |
| 218 | // be added to the include path using -I. |
| 219 | // If possible, don't use this. If adding paths from the current directory use |
| 220 | // local_include_dirs, if adding paths from other modules use export_include_dirs in |
| 221 | // that module. |
| 222 | Include_dirs []string `android:"arch_variant"` |
| 223 | |
Colin Cross | 39d97f2 | 2015-09-14 12:30:50 -0700 | [diff] [blame] | 224 | // list of files relative to the root of the source tree that will be included |
| 225 | // using -include. |
| 226 | // If possible, don't use this. |
| 227 | Include_files []string `android:"arch_variant"` |
| 228 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 229 | // list of directories relative to the Blueprints file that will |
| 230 | // be added to the include path using -I |
| 231 | Local_include_dirs []string `android:"arch_variant"` |
| 232 | |
Colin Cross | 39d97f2 | 2015-09-14 12:30:50 -0700 | [diff] [blame] | 233 | // list of files relative to the Blueprints file that will be included |
| 234 | // using -include. |
| 235 | // If possible, don't use this. |
| 236 | Local_include_files []string `android:"arch_variant"` |
| 237 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 238 | // list of directories relative to the Blueprints file that will |
| 239 | // be added to the include path using -I for any module that links against this module |
| 240 | Export_include_dirs []string `android:"arch_variant"` |
| 241 | |
| 242 | // list of module-specific flags that will be used for C and C++ compiles when |
| 243 | // compiling with clang |
| 244 | Clang_cflags []string `android:"arch_variant"` |
| 245 | |
| 246 | // list of module-specific flags that will be used for .S compiles when |
| 247 | // compiling with clang |
| 248 | Clang_asflags []string `android:"arch_variant"` |
| 249 | |
| 250 | // list of system libraries that will be dynamically linked to |
| 251 | // shared library and executable modules. If unset, generally defaults to libc |
| 252 | // and libm. Set to [] to prevent linking against libc and libm. |
| 253 | System_shared_libs []string |
| 254 | |
| 255 | // list of modules whose object files should be linked into this module |
| 256 | // in their entirety. For static library modules, all of the .o files from the intermediate |
| 257 | // directory of the dependency will be linked into this modules .a file. For a shared library, |
| 258 | // the dependency's .a file will be linked into this module using -Wl,--whole-archive. |
| 259 | Whole_static_libs []string `android:"arch_variant"` |
| 260 | |
| 261 | // list of modules that should be statically linked into this module. |
| 262 | Static_libs []string `android:"arch_variant"` |
| 263 | |
| 264 | // list of modules that should be dynamically linked into this module. |
| 265 | Shared_libs []string `android:"arch_variant"` |
| 266 | |
| 267 | // allow the module to contain undefined symbols. By default, |
| 268 | // modules cannot contain undefined symbols that are not satisified by their immediate |
| 269 | // dependencies. Set this flag to true to remove --no-undefined from the linker flags. |
| 270 | // This flag should only be necessary for compiling low-level libraries like libc. |
| 271 | Allow_undefined_symbols bool |
| 272 | |
| 273 | // don't link in crt_begin and crt_end. This flag should only be necessary for |
| 274 | // compiling crt or libc. |
| 275 | Nocrt bool `android:"arch_variant"` |
| 276 | |
| 277 | // don't insert default compiler flags into asflags, cflags, |
| 278 | // cppflags, conlyflags, ldflags, or include_dirs |
| 279 | No_default_compiler_flags bool |
| 280 | |
| 281 | // compile module with clang instead of gcc |
| 282 | Clang bool `android:"arch_variant"` |
| 283 | |
| 284 | // pass -frtti instead of -fno-rtti |
| 285 | Rtti bool |
| 286 | |
| 287 | // -l arguments to pass to linker for host-provided shared libraries |
| 288 | Host_ldlibs []string `android:"arch_variant"` |
| 289 | |
| 290 | // select the STL library to use. Possible values are "libc++", "libc++_static", |
| 291 | // "stlport", "stlport_static", "ndk", "libstdc++", or "none". Leave blank to select the |
| 292 | // default |
| 293 | Stl string |
| 294 | |
| 295 | // Set for combined shared/static libraries to prevent compiling object files a second time |
| 296 | SkipCompileObjs bool `blueprint:"mutated"` |
| 297 | |
| 298 | Debug, Release struct { |
| 299 | // list of module-specific flags that will be used for C and C++ compiles in debug or |
| 300 | // release builds |
| 301 | Cflags []string `android:"arch_variant"` |
| 302 | } `android:"arch_variant"` |
| 303 | |
| 304 | // Minimum sdk version supported when compiling against the ndk |
| 305 | Sdk_version string |
| 306 | |
| 307 | // install to a subdirectory of the default install path for the module |
| 308 | Relative_install_path string |
| 309 | } |
| 310 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 311 | // CCBase contains the properties and members used by all C/C++ module types, and implements |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 312 | // the blueprint.Module interface. It expects to be embedded into an outer specialization struct, |
| 313 | // and uses a ccModuleType interface to that struct to create the build steps. |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 314 | type CCBase struct { |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 315 | common.AndroidModuleBase |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 316 | module CCModuleType |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 317 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 318 | Properties CCBaseProperties |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 319 | |
| 320 | unused struct { |
| 321 | Asan bool |
| 322 | Native_coverage bool |
| 323 | Strip string |
| 324 | Tags []string |
| 325 | Required []string |
| 326 | } |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 327 | |
| 328 | installPath string |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 329 | |
| 330 | savedDepNames CCDeps |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 333 | func newCCBase(base *CCBase, module CCModuleType, hod common.HostOrDeviceSupported, |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 334 | multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) { |
| 335 | |
| 336 | base.module = module |
| 337 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 338 | props = append(props, &base.Properties, &base.unused) |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 339 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 340 | return common.InitAndroidArchModule(module, hod, multilib, props...) |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 343 | func (c *CCBase) GenerateAndroidBuildActions(ctx common.AndroidModuleContext) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 344 | toolchain := c.findToolchain(ctx) |
| 345 | if ctx.Failed() { |
| 346 | return |
| 347 | } |
| 348 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 349 | flags := c.collectFlags(ctx, toolchain) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 350 | if ctx.Failed() { |
| 351 | return |
| 352 | } |
| 353 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 354 | deps := c.depsToPaths(ctx, c.savedDepNames) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 355 | if ctx.Failed() { |
| 356 | return |
| 357 | } |
| 358 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 359 | flags.CFlags = append(flags.CFlags, deps.Cflags...) |
Colin Cross | ed9f868 | 2015-03-18 17:17:35 -0700 | [diff] [blame] | 360 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 361 | objFiles := c.compileObjs(ctx, flags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 362 | if ctx.Failed() { |
| 363 | return |
| 364 | } |
| 365 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 366 | generatedObjFiles := c.compileGeneratedObjs(ctx, flags) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 367 | if ctx.Failed() { |
| 368 | return |
| 369 | } |
| 370 | |
| 371 | objFiles = append(objFiles, generatedObjFiles...) |
| 372 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 373 | c.ccModuleType().compileModule(ctx, flags, deps, objFiles) |
| 374 | if ctx.Failed() { |
| 375 | return |
| 376 | } |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 377 | |
| 378 | c.ccModuleType().installModule(ctx, flags) |
| 379 | if ctx.Failed() { |
| 380 | return |
| 381 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 384 | func (c *CCBase) ccModuleType() CCModuleType { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 385 | return c.module |
| 386 | } |
| 387 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 388 | var _ common.AndroidDynamicDepender = (*CCBase)(nil) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 389 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 390 | func (c *CCBase) findToolchain(ctx common.AndroidModuleContext) Toolchain { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 391 | arch := ctx.Arch() |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 392 | hod := ctx.HostOrDevice() |
| 393 | factory := toolchainFactories[hod][arch.ArchType] |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 394 | if factory == nil { |
| 395 | panic(fmt.Sprintf("Toolchain not found for %s arch %q", |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 396 | hod.String(), arch.String())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 397 | } |
| 398 | return factory(arch.ArchVariant, arch.CpuVariant) |
| 399 | } |
| 400 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 401 | func (c *CCBase) ModifyProperties(ctx common.AndroidBaseContext) { |
| 402 | } |
| 403 | |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 404 | func (c *CCBase) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 405 | depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.Properties.Whole_static_libs...) |
| 406 | depNames.StaticLibs = append(depNames.StaticLibs, c.Properties.Static_libs...) |
| 407 | depNames.SharedLibs = append(depNames.SharedLibs, c.Properties.Shared_libs...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 408 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 409 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 410 | } |
| 411 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 412 | func (c *CCBase) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 413 | c.module.ModifyProperties(ctx) |
| 414 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 415 | c.savedDepNames = c.module.depNames(ctx, CCDeps{}) |
| 416 | c.savedDepNames.WholeStaticLibs = lastUniqueElements(c.savedDepNames.WholeStaticLibs) |
| 417 | c.savedDepNames.StaticLibs = lastUniqueElements(c.savedDepNames.StaticLibs) |
| 418 | c.savedDepNames.SharedLibs = lastUniqueElements(c.savedDepNames.SharedLibs) |
| 419 | |
| 420 | staticLibs := c.savedDepNames.WholeStaticLibs |
| 421 | staticLibs = append(staticLibs, c.savedDepNames.StaticLibs...) |
| 422 | staticLibs = append(staticLibs, c.savedDepNames.LateStaticLibs...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 423 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "static"}}, staticLibs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 424 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 425 | ctx.AddVariationDependencies([]blueprint.Variation{{"link", "shared"}}, c.savedDepNames.SharedLibs...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 426 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 427 | ret := append([]string(nil), c.savedDepNames.ObjFiles...) |
| 428 | if c.savedDepNames.CrtBegin != "" { |
| 429 | ret = append(ret, c.savedDepNames.CrtBegin) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 430 | } |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 431 | if c.savedDepNames.CrtEnd != "" { |
| 432 | ret = append(ret, c.savedDepNames.CrtEnd) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return ret |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | // Create a ccFlags struct that collects the compile flags from global values, |
| 439 | // per-target values, module type values, and per-module Blueprints properties |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 440 | func (c *CCBase) collectFlags(ctx common.AndroidModuleContext, toolchain Toolchain) CCFlags { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 441 | flags := CCFlags{ |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 442 | CFlags: c.Properties.Cflags, |
| 443 | CppFlags: c.Properties.Cppflags, |
| 444 | ConlyFlags: c.Properties.Conlyflags, |
| 445 | LdFlags: c.Properties.Ldflags, |
| 446 | AsFlags: c.Properties.Asflags, |
| 447 | YaccFlags: c.Properties.Yaccflags, |
| 448 | Nocrt: c.Properties.Nocrt, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 449 | Toolchain: toolchain, |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 450 | Clang: c.Properties.Clang, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 451 | } |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 452 | |
| 453 | // Include dir cflags |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 454 | common.CheckSrcDirsExist(ctx, c.Properties.Include_dirs, "include_dirs") |
| 455 | common.CheckModuleSrcDirsExist(ctx, c.Properties.Local_include_dirs, "local_include_dirs") |
| 456 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 457 | rootIncludeDirs := pathtools.PrefixPaths(c.Properties.Include_dirs, ctx.AConfig().SrcDir()) |
| 458 | localIncludeDirs := pathtools.PrefixPaths(c.Properties.Local_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 459 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 460 | includeDirsToFlags(rootIncludeDirs), |
| 461 | includeDirsToFlags(localIncludeDirs)) |
| 462 | |
Colin Cross | 39d97f2 | 2015-09-14 12:30:50 -0700 | [diff] [blame] | 463 | rootIncludeFiles := pathtools.PrefixPaths(c.Properties.Include_files, ctx.AConfig().SrcDir()) |
| 464 | localIncludeFiles := pathtools.PrefixPaths(c.Properties.Local_include_files, common.ModuleSrcDir(ctx)) |
| 465 | |
| 466 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 467 | includeFilesToFlags(rootIncludeFiles), |
| 468 | includeFilesToFlags(localIncludeFiles)) |
| 469 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 470 | if !c.Properties.No_default_compiler_flags { |
| 471 | if c.Properties.Sdk_version == "" || ctx.Host() { |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 472 | flags.GlobalFlags = append(flags.GlobalFlags, |
| 473 | "${commonGlobalIncludes}", |
| 474 | toolchain.IncludeFlags(), |
| 475 | "-I${SrcDir}/libnativehelper/include/nativehelper") |
| 476 | } |
| 477 | |
| 478 | flags.GlobalFlags = append(flags.GlobalFlags, []string{ |
| 479 | "-I" + common.ModuleSrcDir(ctx), |
| 480 | "-I" + common.ModuleOutDir(ctx), |
| 481 | "-I" + common.ModuleGenDir(ctx), |
| 482 | }...) |
| 483 | } |
| 484 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 485 | instructionSet := c.Properties.Instruction_set |
Tim Kilbourn | 1a9bf26 | 2015-03-18 12:28:32 -0700 | [diff] [blame] | 486 | instructionSetFlags, err := toolchain.InstructionSetFlags(instructionSet) |
| 487 | if err != nil { |
| 488 | ctx.ModuleErrorf("%s", err) |
| 489 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 490 | |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 491 | // TODO: debug |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 492 | flags.CFlags = append(flags.CFlags, c.Properties.Release.Cflags...) |
Colin Cross | af19a29 | 2015-03-18 12:07:10 -0700 | [diff] [blame] | 493 | |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 494 | if ctx.Host() && !ctx.ContainsProperty("clang") { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 495 | flags.Clang = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 498 | if flags.Clang { |
| 499 | flags.CFlags = clangFilterUnknownCflags(flags.CFlags) |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 500 | flags.CFlags = append(flags.CFlags, c.Properties.Clang_cflags...) |
| 501 | flags.AsFlags = append(flags.AsFlags, c.Properties.Clang_asflags...) |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 502 | flags.CppFlags = clangFilterUnknownCflags(flags.CppFlags) |
| 503 | flags.ConlyFlags = clangFilterUnknownCflags(flags.ConlyFlags) |
| 504 | flags.LdFlags = clangFilterUnknownCflags(flags.LdFlags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 505 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 506 | flags.CFlags = append(flags.CFlags, "${clangExtraCflags}") |
| 507 | flags.ConlyFlags = append(flags.ConlyFlags, "${clangExtraConlyflags}") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 508 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 509 | flags.CFlags = append(flags.CFlags, "${clangExtraTargetCflags}") |
Colin Cross | bdd7b1c | 2015-03-16 16:21:20 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 512 | target := "-target " + toolchain.ClangTriple() |
| 513 | gccPrefix := "-B" + filepath.Join(toolchain.GccRoot(), toolchain.GccTriple(), "bin") |
| 514 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 515 | flags.CFlags = append(flags.CFlags, target, gccPrefix) |
| 516 | flags.AsFlags = append(flags.AsFlags, target, gccPrefix) |
| 517 | flags.LdFlags = append(flags.LdFlags, target, gccPrefix) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 520 | if !c.Properties.No_default_compiler_flags { |
| 521 | if ctx.Device() && !c.Properties.Allow_undefined_symbols { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 522 | flags.LdFlags = append(flags.LdFlags, "-Wl,--no-undefined") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Colin Cross | 56b4d45 | 2015-04-21 17:38:44 -0700 | [diff] [blame] | 525 | flags.GlobalFlags = append(flags.GlobalFlags, instructionSetFlags) |
| 526 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 527 | if flags.Clang { |
| 528 | flags.CppFlags = append(flags.CppFlags, "${commonClangGlobalCppflags}") |
Colin Cross | 56b4d45 | 2015-04-21 17:38:44 -0700 | [diff] [blame] | 529 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 530 | toolchain.ClangCflags(), |
| 531 | "${commonClangGlobalCflags}", |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 532 | fmt.Sprintf("${%sClangGlobalCflags}", ctx.HostOrDevice())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 533 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 534 | flags.CppFlags = append(flags.CppFlags, "${commonGlobalCppflags}") |
Colin Cross | 56b4d45 | 2015-04-21 17:38:44 -0700 | [diff] [blame] | 535 | flags.GlobalFlags = append(flags.GlobalFlags, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 536 | toolchain.Cflags(), |
| 537 | "${commonGlobalCflags}", |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 538 | fmt.Sprintf("${%sGlobalCflags}", ctx.HostOrDevice())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 541 | if ctx.Device() { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 542 | if c.Properties.Rtti { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 543 | flags.CppFlags = append(flags.CppFlags, "-frtti") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 544 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 545 | flags.CppFlags = append(flags.CppFlags, "-fno-rtti") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 549 | flags.AsFlags = append(flags.AsFlags, "-D__ASSEMBLY__") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 550 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 551 | if flags.Clang { |
| 552 | flags.CppFlags = append(flags.CppFlags, toolchain.ClangCppflags()) |
| 553 | flags.LdFlags = append(flags.LdFlags, toolchain.ClangLdflags()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 554 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 555 | flags.CppFlags = append(flags.CppFlags, toolchain.Cppflags()) |
| 556 | flags.LdFlags = append(flags.LdFlags, toolchain.Ldflags()) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 557 | } |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 558 | |
| 559 | if ctx.Host() { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 560 | flags.LdFlags = append(flags.LdFlags, c.Properties.Host_ldlibs...) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 561 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 564 | flags = c.ccModuleType().flags(ctx, flags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 565 | |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 566 | if c.Properties.Sdk_version == "" { |
| 567 | if ctx.Host() && !flags.Clang { |
| 568 | // The host GCC doesn't support C++14 (and is deprecated, so likely |
| 569 | // never will). Build these modules with C++11. |
| 570 | flags.CppFlags = append(flags.CppFlags, "-std=gnu++11") |
| 571 | } else { |
| 572 | flags.CppFlags = append(flags.CppFlags, "-std=gnu++14") |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | flags.CFlags, _ = filterList(flags.CFlags, illegalFlags) |
| 577 | flags.CppFlags, _ = filterList(flags.CppFlags, illegalFlags) |
| 578 | flags.ConlyFlags, _ = filterList(flags.ConlyFlags, illegalFlags) |
| 579 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 580 | // Optimization to reduce size of build.ninja |
| 581 | // Replace the long list of flags for each file with a module-local variable |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 582 | ctx.Variable(pctx, "cflags", strings.Join(flags.CFlags, " ")) |
| 583 | ctx.Variable(pctx, "cppflags", strings.Join(flags.CppFlags, " ")) |
| 584 | ctx.Variable(pctx, "asflags", strings.Join(flags.AsFlags, " ")) |
| 585 | flags.CFlags = []string{"$cflags"} |
| 586 | flags.CppFlags = []string{"$cppflags"} |
| 587 | flags.AsFlags = []string{"$asflags"} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 588 | |
| 589 | return flags |
| 590 | } |
| 591 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 592 | func (c *CCBase) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 593 | return flags |
| 594 | } |
| 595 | |
| 596 | // Compile a list of source files into objects a specified subdirectory |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 597 | func (c *CCBase) customCompileObjs(ctx common.AndroidModuleContext, flags CCFlags, |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 598 | subdir string, srcFiles, excludes []string) []string { |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 599 | |
| 600 | buildFlags := ccFlagsToBuilderFlags(flags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 601 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 602 | srcFiles = ctx.ExpandSources(srcFiles, excludes) |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 603 | srcFiles, deps := genSources(ctx, srcFiles, buildFlags) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 604 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 605 | return TransformSourceToObj(ctx, subdir, srcFiles, buildFlags, deps) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 608 | // Compile files listed in c.Properties.Srcs into objects |
| 609 | func (c *CCBase) compileObjs(ctx common.AndroidModuleContext, flags CCFlags) []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 610 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 611 | if c.Properties.SkipCompileObjs { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 612 | return nil |
| 613 | } |
| 614 | |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 615 | return c.customCompileObjs(ctx, flags, "", c.Properties.Srcs, c.Properties.Exclude_srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 618 | // Compile generated source files from dependencies |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 619 | func (c *CCBase) compileGeneratedObjs(ctx common.AndroidModuleContext, flags CCFlags) []string { |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 620 | var srcs []string |
| 621 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 622 | if c.Properties.SkipCompileObjs { |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 623 | return nil |
| 624 | } |
| 625 | |
| 626 | ctx.VisitDirectDeps(func(module blueprint.Module) { |
| 627 | if gen, ok := module.(genrule.SourceFileGenerator); ok { |
| 628 | srcs = append(srcs, gen.GeneratedSourceFiles()...) |
| 629 | } |
| 630 | }) |
| 631 | |
| 632 | if len(srcs) == 0 { |
| 633 | return nil |
| 634 | } |
| 635 | |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 636 | return TransformSourceToObj(ctx, "", srcs, ccFlagsToBuilderFlags(flags), nil) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 639 | func (c *CCBase) outputFile() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 640 | return "" |
| 641 | } |
| 642 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 643 | func (c *CCBase) depsToPathsFromList(ctx common.AndroidModuleContext, |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 644 | names []string) (modules []common.AndroidModule, |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 645 | outputFiles []string, exportedFlags []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 646 | |
| 647 | for _, n := range names { |
| 648 | found := false |
| 649 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
| 650 | otherName := ctx.OtherModuleName(m) |
| 651 | if otherName != n { |
| 652 | return |
| 653 | } |
| 654 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 655 | if a, ok := m.(CCModuleType); ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 656 | if a.Disabled() { |
| 657 | // If a cc_library host+device module depends on a library that exists as both |
| 658 | // cc_library_shared and cc_library_host_shared, it will end up with two |
| 659 | // dependencies with the same name, one of which is marked disabled for each |
| 660 | // of host and device. Ignore the disabled one. |
| 661 | return |
| 662 | } |
Colin Cross | d3ba039 | 2015-05-07 14:11:29 -0700 | [diff] [blame] | 663 | if a.HostOrDevice() != ctx.HostOrDevice() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 664 | ctx.ModuleErrorf("host/device mismatch between %q and %q", ctx.ModuleName(), |
| 665 | otherName) |
| 666 | return |
| 667 | } |
| 668 | |
| 669 | if outputFile := a.outputFile(); outputFile != "" { |
| 670 | if found { |
| 671 | ctx.ModuleErrorf("multiple modules satisified dependency on %q", otherName) |
| 672 | return |
| 673 | } |
| 674 | outputFiles = append(outputFiles, outputFile) |
| 675 | modules = append(modules, a) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 676 | if i, ok := a.(ccExportedFlagsProducer); ok { |
| 677 | exportedFlags = append(exportedFlags, i.exportedFlags()...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 678 | } |
| 679 | found = true |
| 680 | } else { |
| 681 | ctx.ModuleErrorf("module %q missing output file", otherName) |
| 682 | return |
| 683 | } |
| 684 | } else { |
| 685 | ctx.ModuleErrorf("module %q not an android module", otherName) |
| 686 | return |
| 687 | } |
| 688 | }) |
| 689 | if !found { |
| 690 | ctx.ModuleErrorf("unsatisified dependency on %q", n) |
| 691 | } |
| 692 | } |
| 693 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 694 | return modules, outputFiles, exportedFlags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 697 | // Convert depenedency names to paths. Takes a CCDeps containing names and returns a CCDeps |
| 698 | // containing paths |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 699 | func (c *CCBase) depsToPaths(ctx common.AndroidModuleContext, depNames CCDeps) CCDeps { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 700 | var depPaths CCDeps |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 701 | var newCflags []string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 702 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 703 | var wholeStaticLibModules []common.AndroidModule |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 704 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 705 | wholeStaticLibModules, depPaths.WholeStaticLibs, newCflags = |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 706 | c.depsToPathsFromList(ctx, depNames.WholeStaticLibs) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 707 | depPaths.Cflags = append(depPaths.Cflags, newCflags...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 708 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 709 | for _, m := range wholeStaticLibModules { |
| 710 | if staticLib, ok := m.(ccLibraryInterface); ok && staticLib.static() { |
| 711 | depPaths.WholeStaticLibObjFiles = |
| 712 | append(depPaths.WholeStaticLibObjFiles, staticLib.allObjFiles()...) |
| 713 | } else { |
| 714 | ctx.ModuleErrorf("module %q not a static library", ctx.OtherModuleName(m)) |
| 715 | } |
| 716 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 717 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 718 | _, depPaths.StaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.StaticLibs) |
| 719 | depPaths.Cflags = append(depPaths.Cflags, newCflags...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 720 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 721 | _, depPaths.LateStaticLibs, newCflags = c.depsToPathsFromList(ctx, depNames.LateStaticLibs) |
| 722 | depPaths.Cflags = append(depPaths.Cflags, newCflags...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 723 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 724 | _, depPaths.SharedLibs, newCflags = c.depsToPathsFromList(ctx, depNames.SharedLibs) |
| 725 | depPaths.Cflags = append(depPaths.Cflags, newCflags...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 726 | |
| 727 | ctx.VisitDirectDeps(func(m blueprint.Module) { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 728 | if obj, ok := m.(ccObjectProvider); ok { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 729 | otherName := ctx.OtherModuleName(m) |
| 730 | if otherName == depNames.CrtBegin { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 731 | if !c.Properties.Nocrt { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 732 | depPaths.CrtBegin = obj.object().outputFile() |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 733 | } |
| 734 | } else if otherName == depNames.CrtEnd { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 735 | if !c.Properties.Nocrt { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 736 | depPaths.CrtEnd = obj.object().outputFile() |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 737 | } |
| 738 | } else { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 739 | depPaths.ObjFiles = append(depPaths.ObjFiles, obj.object().outputFile()) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | }) |
| 743 | |
| 744 | return depPaths |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 747 | type ccLinkedProperties struct { |
| 748 | VariantIsShared bool `blueprint:"mutated"` |
| 749 | VariantIsStatic bool `blueprint:"mutated"` |
| 750 | VariantIsStaticBinary bool `blueprint:"mutated"` |
| 751 | } |
| 752 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 753 | // CCLinked contains the properties and members used by libraries and executables |
| 754 | type CCLinked struct { |
| 755 | CCBase |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 756 | dynamicProperties ccLinkedProperties |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 759 | func newCCDynamic(dynamic *CCLinked, module CCModuleType, hod common.HostOrDeviceSupported, |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 760 | multilib common.Multilib, props ...interface{}) (blueprint.Module, []interface{}) { |
| 761 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 762 | props = append(props, &dynamic.dynamicProperties) |
| 763 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 764 | return newCCBase(&dynamic.CCBase, module, hod, multilib, props...) |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 767 | func (c *CCLinked) systemSharedLibs(ctx common.AndroidBaseContext) []string { |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 768 | if ctx.ContainsProperty("system_shared_libs") { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 769 | return c.Properties.System_shared_libs |
| 770 | } else if ctx.Device() && c.Properties.Sdk_version == "" { |
Colin Cross | 577f6e4 | 2015-03-27 18:23:34 -0700 | [diff] [blame] | 771 | return []string{"libc", "libm"} |
Colin Cross | 28d7659 | 2015-03-26 16:14:04 -0700 | [diff] [blame] | 772 | } else { |
Colin Cross | 577f6e4 | 2015-03-27 18:23:34 -0700 | [diff] [blame] | 773 | return nil |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 774 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 777 | func (c *CCLinked) stl(ctx common.AndroidBaseContext) string { |
| 778 | if c.Properties.Sdk_version != "" && ctx.Device() { |
| 779 | switch c.Properties.Stl { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 780 | case "": |
| 781 | return "ndk_system" |
| 782 | case "c++_shared", "c++_static", |
| 783 | "stlport_shared", "stlport_static", |
| 784 | "gnustl_static": |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 785 | return "ndk_lib" + c.Properties.Stl |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 786 | default: |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 787 | ctx.ModuleErrorf("stl: %q is not a supported STL with sdk_version set", c.Properties.Stl) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 788 | return "" |
| 789 | } |
| 790 | } |
| 791 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 792 | switch c.Properties.Stl { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 793 | case "libc++", "libc++_static", |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 794 | "libstdc++": |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 795 | return c.Properties.Stl |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 796 | case "none": |
| 797 | return "" |
| 798 | case "": |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 799 | if c.static() { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 800 | return "libc++_static" |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 801 | } else { |
| 802 | return "libc++" // TODO: mingw needs libstdc++ |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 803 | } |
| 804 | default: |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 805 | ctx.ModuleErrorf("stl: %q is not a supported STL", c.Properties.Stl) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 806 | return "" |
| 807 | } |
| 808 | } |
| 809 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 810 | var hostDynamicGccLibs, hostStaticGccLibs []string |
| 811 | |
| 812 | func init() { |
| 813 | if runtime.GOOS == "darwin" { |
| 814 | hostDynamicGccLibs = []string{"-lc", "-lSystem"} |
| 815 | hostStaticGccLibs = []string{"NO_STATIC_HOST_BINARIES_ON_DARWIN"} |
| 816 | } else { |
| 817 | hostDynamicGccLibs = []string{"-lgcc_s", "-lgcc", "-lc", "-lgcc_s", "-lgcc"} |
| 818 | hostStaticGccLibs = []string{"-Wl,--start-group", "-lgcc", "-lgcc_eh", "-lc", "-Wl,--end-group"} |
| 819 | } |
| 820 | } |
Colin Cross | 712fc02 | 2015-04-27 11:13:34 -0700 | [diff] [blame] | 821 | |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 822 | func (c *CCLinked) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 823 | stl := c.stl(ctx) |
| 824 | if ctx.Failed() { |
| 825 | return flags |
| 826 | } |
| 827 | |
| 828 | switch stl { |
| 829 | case "libc++", "libc++_static": |
| 830 | flags.CFlags = append(flags.CFlags, "-D_USING_LIBCXX") |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 831 | flags.CFlags = append(flags.CFlags, "-I${SrcDir}/external/libcxx/include") |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 832 | if ctx.Host() { |
| 833 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 834 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
Colin Cross | 712fc02 | 2015-04-27 11:13:34 -0700 | [diff] [blame] | 835 | flags.LdFlags = append(flags.LdFlags, "-lm", "-lpthread") |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 836 | if c.staticBinary() { |
Colin Cross | 712fc02 | 2015-04-27 11:13:34 -0700 | [diff] [blame] | 837 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...) |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 838 | } else { |
| 839 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...) |
Colin Cross | 712fc02 | 2015-04-27 11:13:34 -0700 | [diff] [blame] | 840 | } |
Dan Willemsen | 3bf6b47 | 2015-09-11 17:41:10 -0700 | [diff] [blame] | 841 | } else { |
| 842 | if ctx.Arch().ArchType == common.Arm { |
| 843 | flags.LdFlags = append(flags.LdFlags, "-Wl,--exclude-libs,libunwind_llvm.a") |
| 844 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 845 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 846 | case "libstdc++": |
| 847 | // Using bionic's basic libstdc++. Not actually an STL. Only around until the |
| 848 | // tree is in good enough shape to not need it. |
| 849 | // Host builds will use GNU libstdc++. |
| 850 | if ctx.Device() { |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 851 | flags.CFlags = append(flags.CFlags, "-I${SrcDir}/bionic/libstdc++/include") |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 852 | } |
| 853 | case "ndk_system": |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 854 | ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources/" |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 855 | flags.CFlags = append(flags.CFlags, "-isystem "+ndkSrcRoot+"cxx-stl/system/include") |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 856 | case "ndk_libc++_shared", "ndk_libc++_static": |
| 857 | // TODO(danalbert): This really shouldn't be here... |
| 858 | flags.CppFlags = append(flags.CppFlags, "-std=c++11") |
| 859 | case "ndk_libstlport_shared", "ndk_libstlport_static", "ndk_libgnustl_static": |
| 860 | // Nothing |
| 861 | case "": |
| 862 | // None or error. |
| 863 | if ctx.Host() { |
| 864 | flags.CppFlags = append(flags.CppFlags, "-nostdinc++") |
| 865 | flags.LdFlags = append(flags.LdFlags, "-nodefaultlibs") |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 866 | if c.staticBinary() { |
Colin Cross | 712fc02 | 2015-04-27 11:13:34 -0700 | [diff] [blame] | 867 | flags.LdFlags = append(flags.LdFlags, hostStaticGccLibs...) |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 868 | } else { |
| 869 | flags.LdFlags = append(flags.LdFlags, hostDynamicGccLibs...) |
Colin Cross | 712fc02 | 2015-04-27 11:13:34 -0700 | [diff] [blame] | 870 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 871 | } |
| 872 | default: |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 873 | panic(fmt.Errorf("Unknown stl in CCLinked.Flags: %q", stl)) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | return flags |
| 877 | } |
| 878 | |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 879 | func (c *CCLinked) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 880 | depNames = c.CCBase.depNames(ctx, depNames) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 881 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 882 | stl := c.stl(ctx) |
| 883 | if ctx.Failed() { |
| 884 | return depNames |
| 885 | } |
| 886 | |
| 887 | switch stl { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 888 | case "libstdc++": |
| 889 | if ctx.Device() { |
| 890 | depNames.SharedLibs = append(depNames.SharedLibs, stl) |
| 891 | } |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 892 | case "libc++", "libc++_static": |
| 893 | if stl == "libc++" { |
| 894 | depNames.SharedLibs = append(depNames.SharedLibs, stl) |
| 895 | } else { |
| 896 | depNames.StaticLibs = append(depNames.StaticLibs, stl) |
| 897 | } |
| 898 | if ctx.Device() { |
| 899 | if ctx.Arch().ArchType == common.Arm { |
| 900 | depNames.StaticLibs = append(depNames.StaticLibs, "libunwind_llvm") |
| 901 | } |
| 902 | if c.staticBinary() { |
| 903 | depNames.StaticLibs = append(depNames.StaticLibs, "libdl") |
| 904 | } else { |
| 905 | depNames.SharedLibs = append(depNames.SharedLibs, "libdl") |
| 906 | } |
| 907 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 908 | case "": |
| 909 | // None or error. |
| 910 | case "ndk_system": |
| 911 | // TODO: Make a system STL prebuilt for the NDK. |
| 912 | // The system STL doesn't have a prebuilt (it uses the system's libstdc++), but it does have |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 913 | // its own includes. The includes are handled in CCBase.Flags(). |
Colin Cross | 577f6e4 | 2015-03-27 18:23:34 -0700 | [diff] [blame] | 914 | depNames.SharedLibs = append([]string{"libstdc++"}, depNames.SharedLibs...) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 915 | case "ndk_libc++_shared", "ndk_libstlport_shared": |
| 916 | depNames.SharedLibs = append(depNames.SharedLibs, stl) |
| 917 | case "ndk_libc++_static", "ndk_libstlport_static", "ndk_libgnustl_static": |
| 918 | depNames.StaticLibs = append(depNames.StaticLibs, stl) |
| 919 | default: |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 920 | panic(fmt.Errorf("Unknown stl in CCLinked.depNames: %q", stl)) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 923 | if ctx.ModuleName() != "libcompiler_rt-extras" { |
| 924 | depNames.StaticLibs = append(depNames.StaticLibs, "libcompiler_rt-extras") |
| 925 | } |
| 926 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 927 | if ctx.Device() { |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 928 | // libgcc and libatomic have to be last on the command line |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 929 | depNames.LateStaticLibs = append(depNames.LateStaticLibs, "libgcov", "libatomic", "libgcc") |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 930 | |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 931 | if !c.static() { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 932 | depNames.SharedLibs = append(depNames.SharedLibs, c.systemSharedLibs(ctx)...) |
| 933 | } |
Colin Cross | 577f6e4 | 2015-03-27 18:23:34 -0700 | [diff] [blame] | 934 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 935 | if c.Properties.Sdk_version != "" { |
| 936 | version := c.Properties.Sdk_version |
Colin Cross | 577f6e4 | 2015-03-27 18:23:34 -0700 | [diff] [blame] | 937 | depNames.SharedLibs = append(depNames.SharedLibs, |
| 938 | "ndk_libc."+version, |
| 939 | "ndk_libm."+version, |
| 940 | ) |
| 941 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 942 | } |
| 943 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 944 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 947 | // ccLinkedInterface interface is used on ccLinked to deal with static or shared variants |
| 948 | type ccLinkedInterface interface { |
| 949 | // Returns true if the build options for the module have selected a static or shared build |
| 950 | buildStatic() bool |
| 951 | buildShared() bool |
| 952 | |
| 953 | // Sets whether a specific variant is static or shared |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 954 | setStatic(bool) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 955 | |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 956 | // Returns whether a specific variant is a static library or binary |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 957 | static() bool |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 958 | |
| 959 | // Returns whether a module is a static binary |
| 960 | staticBinary() bool |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | var _ ccLinkedInterface = (*CCLibrary)(nil) |
| 964 | var _ ccLinkedInterface = (*CCBinary)(nil) |
| 965 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 966 | func (c *CCLinked) static() bool { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 967 | return c.dynamicProperties.VariantIsStatic |
| 968 | } |
| 969 | |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 970 | func (c *CCLinked) staticBinary() bool { |
| 971 | return c.dynamicProperties.VariantIsStaticBinary |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 974 | func (c *CCLinked) setStatic(static bool) { |
| 975 | c.dynamicProperties.VariantIsStatic = static |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 978 | type ccExportedFlagsProducer interface { |
| 979 | exportedFlags() []string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | // |
| 983 | // Combined static+shared libraries |
| 984 | // |
| 985 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 986 | type CCLibraryProperties struct { |
| 987 | BuildStatic bool `blueprint:"mutated"` |
| 988 | BuildShared bool `blueprint:"mutated"` |
| 989 | Static struct { |
| 990 | Srcs []string `android:"arch_variant"` |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 991 | Exclude_srcs []string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 992 | Cflags []string `android:"arch_variant"` |
| 993 | Whole_static_libs []string `android:"arch_variant"` |
| 994 | Static_libs []string `android:"arch_variant"` |
| 995 | Shared_libs []string `android:"arch_variant"` |
| 996 | } `android:"arch_variant"` |
| 997 | Shared struct { |
| 998 | Srcs []string `android:"arch_variant"` |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 999 | Exclude_srcs []string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1000 | Cflags []string `android:"arch_variant"` |
| 1001 | Whole_static_libs []string `android:"arch_variant"` |
| 1002 | Static_libs []string `android:"arch_variant"` |
| 1003 | Shared_libs []string `android:"arch_variant"` |
| 1004 | } `android:"arch_variant"` |
Colin Cross | aee540a | 2015-07-06 17:48:31 -0700 | [diff] [blame] | 1005 | |
| 1006 | // local file name to pass to the linker as --version_script |
| 1007 | Version_script string `android:"arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1010 | type CCLibrary struct { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1011 | CCLinked |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1012 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1013 | reuseFrom ccLibraryInterface |
| 1014 | reuseObjFiles []string |
| 1015 | objFiles []string |
| 1016 | exportFlags []string |
| 1017 | out string |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1018 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1019 | LibraryProperties CCLibraryProperties |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1022 | func (c *CCLibrary) buildStatic() bool { |
| 1023 | return c.LibraryProperties.BuildStatic |
| 1024 | } |
| 1025 | |
| 1026 | func (c *CCLibrary) buildShared() bool { |
| 1027 | return c.LibraryProperties.BuildShared |
| 1028 | } |
| 1029 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1030 | type ccLibraryInterface interface { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1031 | ccLinkedInterface |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1032 | ccLibrary() *CCLibrary |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1033 | setReuseFrom(ccLibraryInterface) |
| 1034 | getReuseFrom() ccLibraryInterface |
| 1035 | getReuseObjFiles() []string |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1036 | allObjFiles() []string |
Colin Cross | c472d57 | 2015-03-17 15:06:21 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1039 | var _ ccLibraryInterface = (*CCLibrary)(nil) |
| 1040 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1041 | func (c *CCLibrary) ccLibrary() *CCLibrary { |
| 1042 | return c |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1043 | } |
| 1044 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1045 | func NewCCLibrary(library *CCLibrary, module CCModuleType, |
| 1046 | hod common.HostOrDeviceSupported) (blueprint.Module, []interface{}) { |
| 1047 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1048 | return newCCDynamic(&library.CCLinked, module, hod, common.MultilibBoth, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1049 | &library.LibraryProperties) |
| 1050 | } |
| 1051 | |
| 1052 | func CCLibraryFactory() (blueprint.Module, []interface{}) { |
| 1053 | module := &CCLibrary{} |
| 1054 | |
| 1055 | module.LibraryProperties.BuildShared = true |
| 1056 | module.LibraryProperties.BuildStatic = true |
| 1057 | |
| 1058 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
| 1059 | } |
| 1060 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1061 | func (c *CCLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 1062 | depNames = c.CCLinked.depNames(ctx, depNames) |
Colin Cross | 2732e9a | 2015-04-28 13:23:52 -0700 | [diff] [blame] | 1063 | if c.static() { |
| 1064 | depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Static.Whole_static_libs...) |
| 1065 | depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Static.Static_libs...) |
| 1066 | depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Static.Shared_libs...) |
| 1067 | } else { |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1068 | if ctx.Device() { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1069 | if c.Properties.Sdk_version == "" { |
| 1070 | depNames.CrtBegin = "crtbegin_so" |
| 1071 | depNames.CrtEnd = "crtend_so" |
| 1072 | } else { |
| 1073 | depNames.CrtBegin = "ndk_crtbegin_so." + c.Properties.Sdk_version |
| 1074 | depNames.CrtEnd = "ndk_crtend_so." + c.Properties.Sdk_version |
| 1075 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1076 | } |
Colin Cross | 2732e9a | 2015-04-28 13:23:52 -0700 | [diff] [blame] | 1077 | depNames.WholeStaticLibs = append(depNames.WholeStaticLibs, c.LibraryProperties.Shared.Whole_static_libs...) |
| 1078 | depNames.StaticLibs = append(depNames.StaticLibs, c.LibraryProperties.Shared.Static_libs...) |
| 1079 | depNames.SharedLibs = append(depNames.SharedLibs, c.LibraryProperties.Shared.Shared_libs...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1080 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1081 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1082 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1085 | func (c *CCLibrary) outputFile() string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1086 | return c.out |
| 1087 | } |
| 1088 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1089 | func (c *CCLibrary) getReuseObjFiles() []string { |
| 1090 | return c.reuseObjFiles |
| 1091 | } |
| 1092 | |
| 1093 | func (c *CCLibrary) setReuseFrom(reuseFrom ccLibraryInterface) { |
| 1094 | c.reuseFrom = reuseFrom |
| 1095 | } |
| 1096 | |
| 1097 | func (c *CCLibrary) getReuseFrom() ccLibraryInterface { |
| 1098 | return c.reuseFrom |
| 1099 | } |
| 1100 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1101 | func (c *CCLibrary) allObjFiles() []string { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1102 | return c.objFiles |
| 1103 | } |
| 1104 | |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1105 | func (c *CCLibrary) exportedFlags() []string { |
| 1106 | return c.exportFlags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1109 | func (c *CCLibrary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 1110 | flags = c.CCLinked.flags(ctx, flags) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1111 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1112 | flags.CFlags = append(flags.CFlags, "-fPIC") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1113 | |
Colin Cross | d8e780d | 2015-04-28 17:39:43 -0700 | [diff] [blame] | 1114 | if c.static() { |
| 1115 | flags.CFlags = append(flags.CFlags, c.LibraryProperties.Static.Cflags...) |
| 1116 | } else { |
| 1117 | flags.CFlags = append(flags.CFlags, c.LibraryProperties.Shared.Cflags...) |
| 1118 | } |
| 1119 | |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 1120 | if !c.static() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1121 | libName := ctx.ModuleName() |
| 1122 | // GCC for Android assumes that -shared means -Bsymbolic, use -Wl,-shared instead |
| 1123 | sharedFlag := "-Wl,-shared" |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1124 | if c.Properties.Clang || ctx.Host() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1125 | sharedFlag = "-shared" |
| 1126 | } |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1127 | if ctx.Device() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1128 | flags.LdFlags = append(flags.LdFlags, "-nostdlib") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1129 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1130 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 1131 | if ctx.Darwin() { |
| 1132 | flags.LdFlags = append(flags.LdFlags, |
| 1133 | "-dynamiclib", |
| 1134 | "-single_module", |
| 1135 | //"-read_only_relocs suppress", |
| 1136 | "-install_name @rpath/"+libName+sharedLibraryExtension, |
| 1137 | ) |
| 1138 | } else { |
| 1139 | flags.LdFlags = append(flags.LdFlags, |
| 1140 | "-Wl,--gc-sections", |
| 1141 | sharedFlag, |
| 1142 | "-Wl,-soname,"+libName+sharedLibraryExtension, |
| 1143 | ) |
| 1144 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1145 | } |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1146 | |
| 1147 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1150 | func (c *CCLibrary) compileStaticLibrary(ctx common.AndroidModuleContext, |
| 1151 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1152 | |
| 1153 | staticFlags := flags |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 1154 | objFilesStatic := c.customCompileObjs(ctx, staticFlags, common.DeviceStaticLibrary, |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 1155 | c.LibraryProperties.Static.Srcs, c.LibraryProperties.Static.Exclude_srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1156 | |
| 1157 | objFiles = append(objFiles, objFilesStatic...) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1158 | objFiles = append(objFiles, deps.WholeStaticLibObjFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1159 | |
| 1160 | outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+staticLibraryExtension) |
| 1161 | |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 1162 | if ctx.Darwin() { |
| 1163 | TransformDarwinObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 1164 | } else { |
| 1165 | TransformObjToStaticLib(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 1166 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1167 | |
| 1168 | c.objFiles = objFiles |
| 1169 | c.out = outputFile |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 1170 | |
| 1171 | common.CheckModuleSrcDirsExist(ctx, c.Properties.Export_include_dirs, "export_include_dirs") |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1172 | includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1173 | c.exportFlags = []string{includeDirsToFlags(includeDirs)} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1174 | |
| 1175 | ctx.CheckbuildFile(outputFile) |
| 1176 | } |
| 1177 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1178 | func (c *CCLibrary) compileSharedLibrary(ctx common.AndroidModuleContext, |
| 1179 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1180 | |
| 1181 | sharedFlags := flags |
Colin Cross | 581c189 | 2015-04-07 16:50:10 -0700 | [diff] [blame] | 1182 | objFilesShared := c.customCompileObjs(ctx, sharedFlags, common.DeviceSharedLibrary, |
Dan Willemsen | 2ef08f4 | 2015-06-30 18:15:24 -0700 | [diff] [blame] | 1183 | c.LibraryProperties.Shared.Srcs, c.LibraryProperties.Shared.Exclude_srcs) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1184 | |
| 1185 | objFiles = append(objFiles, objFilesShared...) |
| 1186 | |
| 1187 | outputFile := filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+sharedLibraryExtension) |
| 1188 | |
Colin Cross | aee540a | 2015-07-06 17:48:31 -0700 | [diff] [blame] | 1189 | var linkerDeps []string |
| 1190 | |
| 1191 | if c.LibraryProperties.Version_script != "" { |
| 1192 | versionScript := filepath.Join(common.ModuleSrcDir(ctx), c.LibraryProperties.Version_script) |
| 1193 | sharedFlags.LdFlags = append(sharedFlags.LdFlags, "-Wl,--version-script,"+versionScript) |
| 1194 | linkerDeps = append(linkerDeps, versionScript) |
| 1195 | } |
| 1196 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1197 | TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs, |
Colin Cross | aee540a | 2015-07-06 17:48:31 -0700 | [diff] [blame] | 1198 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, false, |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 1199 | ccFlagsToBuilderFlags(flags), outputFile) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1200 | |
| 1201 | c.out = outputFile |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1202 | includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1203 | c.exportFlags = []string{includeDirsToFlags(includeDirs)} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1206 | func (c *CCLibrary) compileModule(ctx common.AndroidModuleContext, |
| 1207 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1208 | |
| 1209 | // Reuse the object files from the matching static library if it exists |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1210 | if c.getReuseFrom().ccLibrary() == c { |
| 1211 | c.reuseObjFiles = objFiles |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1212 | } else { |
Colin Cross | 2732e9a | 2015-04-28 13:23:52 -0700 | [diff] [blame] | 1213 | if c.getReuseFrom().ccLibrary().LibraryProperties.Static.Cflags == nil && |
| 1214 | c.LibraryProperties.Shared.Cflags == nil { |
| 1215 | objFiles = append([]string(nil), c.getReuseFrom().getReuseObjFiles()...) |
| 1216 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1217 | } |
| 1218 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1219 | if c.static() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1220 | c.compileStaticLibrary(ctx, flags, deps, objFiles) |
| 1221 | } else { |
| 1222 | c.compileSharedLibrary(ctx, flags, deps, objFiles) |
| 1223 | } |
| 1224 | } |
| 1225 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1226 | func (c *CCLibrary) installStaticLibrary(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1227 | // Static libraries do not get installed. |
| 1228 | } |
| 1229 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1230 | func (c *CCLibrary) installSharedLibrary(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1231 | installDir := "lib" |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1232 | if flags.Toolchain.Is64Bit() { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1233 | installDir = "lib64" |
| 1234 | } |
| 1235 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1236 | ctx.InstallFile(filepath.Join(installDir, c.Properties.Relative_install_path), c.out) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1239 | func (c *CCLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1240 | if c.static() { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1241 | c.installStaticLibrary(ctx, flags) |
| 1242 | } else { |
| 1243 | c.installSharedLibrary(ctx, flags) |
| 1244 | } |
| 1245 | } |
| 1246 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1247 | // |
| 1248 | // Objects (for crt*.o) |
| 1249 | // |
| 1250 | |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1251 | type ccObjectProvider interface { |
| 1252 | object() *ccObject |
| 1253 | } |
| 1254 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1255 | type ccObject struct { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1256 | CCBase |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1257 | out string |
| 1258 | } |
| 1259 | |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1260 | func (c *ccObject) object() *ccObject { |
| 1261 | return c |
| 1262 | } |
| 1263 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1264 | func CCObjectFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1265 | module := &ccObject{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1266 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1267 | return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | func (*ccObject) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1271 | // object files can't have any dynamic dependencies |
| 1272 | return nil |
| 1273 | } |
| 1274 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1275 | func (*ccObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1276 | // object files can't have any dynamic dependencies |
| 1277 | return CCDeps{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | func (c *ccObject) compileModule(ctx common.AndroidModuleContext, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1281 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1282 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1283 | objFiles = append(objFiles, deps.ObjFiles...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1284 | |
| 1285 | var outputFile string |
| 1286 | if len(objFiles) == 1 { |
| 1287 | outputFile = objFiles[0] |
| 1288 | } else { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1289 | outputFile = filepath.Join(common.ModuleOutDir(ctx), ctx.ModuleName()+objectExtension) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1290 | TransformObjsToObj(ctx, objFiles, ccFlagsToBuilderFlags(flags), outputFile) |
| 1291 | } |
| 1292 | |
| 1293 | c.out = outputFile |
| 1294 | |
| 1295 | ctx.CheckbuildFile(outputFile) |
| 1296 | } |
| 1297 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1298 | func (c *ccObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1299 | // Object files do not get installed. |
| 1300 | } |
| 1301 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1302 | func (c *ccObject) outputFile() string { |
| 1303 | return c.out |
| 1304 | } |
| 1305 | |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1306 | var _ ccObjectProvider = (*ccObject)(nil) |
| 1307 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1308 | // |
| 1309 | // Executables |
| 1310 | // |
| 1311 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1312 | type CCBinaryProperties struct { |
| 1313 | // compile executable with -static |
| 1314 | Static_executable bool |
| 1315 | |
| 1316 | // set the name of the output |
| 1317 | Stem string `android:"arch_variant"` |
| 1318 | |
| 1319 | // append to the name of the output |
| 1320 | Suffix string `android:"arch_variant"` |
| 1321 | |
| 1322 | // if set, add an extra objcopy --prefix-symbols= step |
| 1323 | Prefix_symbols string |
| 1324 | } |
| 1325 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1326 | type CCBinary struct { |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1327 | CCLinked |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1328 | out string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 1329 | installFile string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1330 | BinaryProperties CCBinaryProperties |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1331 | } |
| 1332 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1333 | func (c *CCBinary) buildStatic() bool { |
| 1334 | return c.BinaryProperties.Static_executable |
| 1335 | } |
| 1336 | |
| 1337 | func (c *CCBinary) buildShared() bool { |
| 1338 | return !c.BinaryProperties.Static_executable |
| 1339 | } |
| 1340 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1341 | func (c *CCBinary) getStem(ctx common.AndroidModuleContext) string { |
Colin Cross | 4ae185c | 2015-03-26 15:12:10 -0700 | [diff] [blame] | 1342 | stem := ctx.ModuleName() |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1343 | if c.BinaryProperties.Stem != "" { |
Colin Cross | 4ae185c | 2015-03-26 15:12:10 -0700 | [diff] [blame] | 1344 | stem = c.BinaryProperties.Stem |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1345 | } |
Colin Cross | 4ae185c | 2015-03-26 15:12:10 -0700 | [diff] [blame] | 1346 | |
| 1347 | return stem + c.BinaryProperties.Suffix |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1350 | func (c *CCBinary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 1351 | depNames = c.CCLinked.depNames(ctx, depNames) |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1352 | if ctx.Device() { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1353 | if c.Properties.Sdk_version == "" { |
| 1354 | if c.BinaryProperties.Static_executable { |
| 1355 | depNames.CrtBegin = "crtbegin_static" |
| 1356 | } else { |
| 1357 | depNames.CrtBegin = "crtbegin_dynamic" |
| 1358 | } |
| 1359 | depNames.CrtEnd = "crtend_android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1360 | } else { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1361 | if c.BinaryProperties.Static_executable { |
| 1362 | depNames.CrtBegin = "ndk_crtbegin_static." + c.Properties.Sdk_version |
| 1363 | } else { |
| 1364 | depNames.CrtBegin = "ndk_crtbegin_dynamic." + c.Properties.Sdk_version |
| 1365 | } |
| 1366 | depNames.CrtEnd = "ndk_crtend_android." + c.Properties.Sdk_version |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1367 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1368 | |
| 1369 | if c.BinaryProperties.Static_executable { |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 1370 | if c.stl(ctx) == "libc++_static" { |
| 1371 | depNames.StaticLibs = append(depNames.StaticLibs, "libm", "libc", "libdl") |
| 1372 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1373 | // static libraries libcompiler_rt, libc and libc_nomalloc need to be linked with |
| 1374 | // --start-group/--end-group along with libgcc. If they are in deps.StaticLibs, |
| 1375 | // move them to the beginning of deps.LateStaticLibs |
| 1376 | var groupLibs []string |
| 1377 | depNames.StaticLibs, groupLibs = filterList(depNames.StaticLibs, |
| 1378 | []string{"libc", "libc_nomalloc", "libcompiler_rt"}) |
| 1379 | depNames.LateStaticLibs = append(groupLibs, depNames.LateStaticLibs...) |
| 1380 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1381 | } |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1382 | return depNames |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1383 | } |
| 1384 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1385 | func NewCCBinary(binary *CCBinary, module CCModuleType, |
Colin Cross | 1f8f234 | 2015-03-26 16:09:47 -0700 | [diff] [blame] | 1386 | hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1387 | |
Colin Cross | 1f8f234 | 2015-03-26 16:09:47 -0700 | [diff] [blame] | 1388 | props = append(props, &binary.BinaryProperties) |
| 1389 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1390 | return newCCDynamic(&binary.CCLinked, module, hod, common.MultilibFirst, props...) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1391 | } |
| 1392 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1393 | func CCBinaryFactory() (blueprint.Module, []interface{}) { |
| 1394 | module := &CCBinary{} |
| 1395 | |
| 1396 | return NewCCBinary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 1399 | func (c *CCBinary) ModifyProperties(ctx common.AndroidBaseContext) { |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 1400 | if ctx.Darwin() { |
| 1401 | c.BinaryProperties.Static_executable = false |
| 1402 | } |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 1403 | if c.BinaryProperties.Static_executable { |
| 1404 | c.dynamicProperties.VariantIsStaticBinary = true |
| 1405 | } |
| 1406 | } |
| 1407 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1408 | func (c *CCBinary) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
Colin Cross | e11befc | 2015-04-27 17:49:17 -0700 | [diff] [blame] | 1409 | flags = c.CCLinked.flags(ctx, flags) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1410 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1411 | flags.CFlags = append(flags.CFlags, "-fpie") |
| 1412 | |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1413 | if ctx.Device() { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1414 | if c.BinaryProperties.Static_executable { |
| 1415 | // Clang driver needs -static to create static executable. |
| 1416 | // However, bionic/linker uses -shared to overwrite. |
| 1417 | // Linker for x86 targets does not allow coexistance of -static and -shared, |
| 1418 | // so we add -static only if -shared is not used. |
| 1419 | if !inList("-shared", flags.LdFlags) { |
| 1420 | flags.LdFlags = append(flags.LdFlags, "-static") |
| 1421 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1422 | |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1423 | flags.LdFlags = append(flags.LdFlags, |
| 1424 | "-nostdlib", |
| 1425 | "-Bstatic", |
| 1426 | "-Wl,--gc-sections", |
| 1427 | ) |
| 1428 | |
| 1429 | } else { |
| 1430 | linker := "/system/bin/linker" |
| 1431 | if flags.Toolchain.Is64Bit() { |
| 1432 | linker = "/system/bin/linker64" |
| 1433 | } |
| 1434 | |
| 1435 | flags.LdFlags = append(flags.LdFlags, |
| 1436 | "-nostdlib", |
| 1437 | "-Bdynamic", |
| 1438 | fmt.Sprintf("-Wl,-dynamic-linker,%s", linker), |
| 1439 | "-Wl,--gc-sections", |
| 1440 | "-Wl,-z,nocopyreloc", |
| 1441 | ) |
| 1442 | } |
Colin Cross | 0af4b84 | 2015-04-30 16:36:18 -0700 | [diff] [blame] | 1443 | } else if ctx.Darwin() { |
| 1444 | flags.LdFlags = append(flags.LdFlags, "-Wl,-headerpad_max_install_names") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1445 | } |
| 1446 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1447 | return flags |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1448 | } |
| 1449 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1450 | func (c *CCBinary) compileModule(ctx common.AndroidModuleContext, |
| 1451 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1452 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1453 | if !c.BinaryProperties.Static_executable && inList("libc", c.Properties.Static_libs) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1454 | ctx.ModuleErrorf("statically linking libc to dynamic executable, please remove libc\n" + |
| 1455 | "from static libs or set static_executable: true") |
| 1456 | } |
| 1457 | |
| 1458 | outputFile := filepath.Join(common.ModuleOutDir(ctx), c.getStem(ctx)) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1459 | c.out = outputFile |
Colin Cross | bfae885 | 2015-03-26 14:44:11 -0700 | [diff] [blame] | 1460 | if c.BinaryProperties.Prefix_symbols != "" { |
| 1461 | afterPrefixSymbols := outputFile |
| 1462 | outputFile = outputFile + ".intermediate" |
| 1463 | TransformBinaryPrefixSymbols(ctx, c.BinaryProperties.Prefix_symbols, outputFile, |
| 1464 | ccFlagsToBuilderFlags(flags), afterPrefixSymbols) |
| 1465 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1466 | |
Colin Cross | aee540a | 2015-07-06 17:48:31 -0700 | [diff] [blame] | 1467 | var linkerDeps []string |
| 1468 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1469 | TransformObjToDynamicBinary(ctx, objFiles, deps.SharedLibs, deps.StaticLibs, |
Colin Cross | aee540a | 2015-07-06 17:48:31 -0700 | [diff] [blame] | 1470 | deps.LateStaticLibs, deps.WholeStaticLibs, linkerDeps, deps.CrtBegin, deps.CrtEnd, true, |
Colin Cross | 77b00fa | 2015-03-16 16:15:49 -0700 | [diff] [blame] | 1471 | ccFlagsToBuilderFlags(flags), outputFile) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1472 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1473 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1474 | func (c *CCBinary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 1475 | c.installFile = ctx.InstallFile(filepath.Join("bin", c.Properties.Relative_install_path), c.out) |
| 1476 | } |
| 1477 | |
| 1478 | func (c *CCBinary) HostToolPath() string { |
| 1479 | if c.HostOrDevice().Host() { |
| 1480 | return c.installFile |
| 1481 | } |
| 1482 | return "" |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1485 | type CCTestProperties struct { |
| 1486 | // Create a separate test for each source file. Useful when there is |
| 1487 | // global state that can not be torn down and reset between each test suite. |
| 1488 | Test_per_src bool |
| 1489 | } |
| 1490 | |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1491 | type CCTest struct { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1492 | CCBinary |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1493 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 1494 | TestProperties CCTestProperties |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1497 | func (c *CCTest) flags(ctx common.AndroidModuleContext, flags CCFlags) CCFlags { |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1498 | flags = c.CCBinary.flags(ctx, flags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1499 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1500 | flags.CFlags = append(flags.CFlags, "-DGTEST_HAS_STD_STRING") |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1501 | if ctx.Host() { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1502 | flags.CFlags = append(flags.CFlags, "-O0", "-g") |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1503 | flags.LdFlags = append(flags.LdFlags, "-lpthread") |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | // TODO(danalbert): Make gtest export its dependencies. |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1507 | flags.CFlags = append(flags.CFlags, |
| 1508 | "-I"+filepath.Join(ctx.AConfig().SrcDir(), "external/gtest/include")) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1509 | |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1510 | return flags |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1513 | func (c *CCTest) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1514 | depNames.StaticLibs = append(depNames.StaticLibs, "libgtest", "libgtest_main") |
Colin Cross | a8a93d3 | 2015-04-28 13:26:49 -0700 | [diff] [blame] | 1515 | depNames = c.CCBinary.depNames(ctx, depNames) |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1516 | return depNames |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1519 | func (c *CCTest) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Colin Cross | f6566ed | 2015-03-24 11:13:38 -0700 | [diff] [blame] | 1520 | if ctx.Device() { |
Colin Cross | a8a93d3 | 2015-04-28 13:26:49 -0700 | [diff] [blame] | 1521 | ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1522 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1523 | c.CCBinary.installModule(ctx, flags) |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1527 | func (c *CCTest) testPerSrc() bool { |
| 1528 | return c.TestProperties.Test_per_src |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1531 | func (c *CCTest) test() *CCTest { |
| 1532 | return c |
| 1533 | } |
| 1534 | |
| 1535 | func NewCCTest(test *CCTest, module CCModuleType, |
| 1536 | hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) { |
| 1537 | |
| 1538 | props = append(props, &test.TestProperties) |
| 1539 | |
| 1540 | return NewCCBinary(&test.CCBinary, module, hod, props...) |
| 1541 | } |
| 1542 | |
| 1543 | func CCTestFactory() (blueprint.Module, []interface{}) { |
| 1544 | module := &CCTest{} |
| 1545 | |
| 1546 | return NewCCTest(module, module, common.HostAndDeviceSupported) |
| 1547 | } |
| 1548 | |
| 1549 | type testPerSrc interface { |
| 1550 | test() *CCTest |
| 1551 | testPerSrc() bool |
| 1552 | } |
| 1553 | |
| 1554 | var _ testPerSrc = (*CCTest)(nil) |
| 1555 | |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1556 | func TestPerSrcMutator(mctx blueprint.EarlyMutatorContext) { |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1557 | if test, ok := mctx.Module().(testPerSrc); ok { |
| 1558 | if test.testPerSrc() { |
| 1559 | testNames := make([]string, len(test.test().Properties.Srcs)) |
| 1560 | for i, src := range test.test().Properties.Srcs { |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1561 | testNames[i] = strings.TrimSuffix(src, filepath.Ext(src)) |
| 1562 | } |
| 1563 | tests := mctx.CreateLocalVariations(testNames...) |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1564 | for i, src := range test.test().Properties.Srcs { |
| 1565 | tests[i].(testPerSrc).test().Properties.Srcs = []string{src} |
| 1566 | tests[i].(testPerSrc).test().BinaryProperties.Stem = testNames[i] |
Colin Cross | 6b29069 | 2015-03-19 14:05:33 -0700 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Colin Cross | 2ba19d9 | 2015-05-07 15:44:20 -0700 | [diff] [blame] | 1572 | type CCBenchmark struct { |
| 1573 | CCBinary |
| 1574 | } |
| 1575 | |
| 1576 | func (c *CCBenchmark) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 1577 | depNames = c.CCBinary.depNames(ctx, depNames) |
Dan Willemsen | f8e98b0 | 2015-09-11 17:41:44 -0700 | [diff] [blame^] | 1578 | depNames.StaticLibs = append(depNames.StaticLibs, "libbenchmark", "libbase") |
Colin Cross | 2ba19d9 | 2015-05-07 15:44:20 -0700 | [diff] [blame] | 1579 | return depNames |
| 1580 | } |
| 1581 | |
| 1582 | func (c *CCBenchmark) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
| 1583 | if ctx.Device() { |
| 1584 | ctx.InstallFile("../data/nativetest"+ctx.Arch().ArchType.Multilib[3:]+"/"+ctx.ModuleName(), c.out) |
| 1585 | } else { |
| 1586 | c.CCBinary.installModule(ctx, flags) |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | func NewCCBenchmark(test *CCBenchmark, module CCModuleType, |
| 1591 | hod common.HostOrDeviceSupported, props ...interface{}) (blueprint.Module, []interface{}) { |
| 1592 | |
| 1593 | return NewCCBinary(&test.CCBinary, module, hod, props...) |
| 1594 | } |
| 1595 | |
| 1596 | func CCBenchmarkFactory() (blueprint.Module, []interface{}) { |
| 1597 | module := &CCBenchmark{} |
| 1598 | |
| 1599 | return NewCCBenchmark(module, module, common.HostAndDeviceSupported) |
| 1600 | } |
| 1601 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1602 | // |
| 1603 | // Static library |
| 1604 | // |
| 1605 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1606 | func CCLibraryStaticFactory() (blueprint.Module, []interface{}) { |
| 1607 | module := &CCLibrary{} |
| 1608 | module.LibraryProperties.BuildStatic = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1609 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1610 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | // |
| 1614 | // Shared libraries |
| 1615 | // |
| 1616 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1617 | func CCLibrarySharedFactory() (blueprint.Module, []interface{}) { |
| 1618 | module := &CCLibrary{} |
| 1619 | module.LibraryProperties.BuildShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1620 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1621 | return NewCCLibrary(module, module, common.HostAndDeviceSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | // |
| 1625 | // Host static library |
| 1626 | // |
| 1627 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1628 | func CCLibraryHostStaticFactory() (blueprint.Module, []interface{}) { |
| 1629 | module := &CCLibrary{} |
| 1630 | module.LibraryProperties.BuildStatic = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1631 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1632 | return NewCCLibrary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | // |
| 1636 | // Host Shared libraries |
| 1637 | // |
| 1638 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1639 | func CCLibraryHostSharedFactory() (blueprint.Module, []interface{}) { |
| 1640 | module := &CCLibrary{} |
| 1641 | module.LibraryProperties.BuildShared = true |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1642 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1643 | return NewCCLibrary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | // |
| 1647 | // Host Binaries |
| 1648 | // |
| 1649 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1650 | func CCBinaryHostFactory() (blueprint.Module, []interface{}) { |
| 1651 | module := &CCBinary{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1652 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1653 | return NewCCBinary(module, module, common.HostSupported) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | // |
Colin Cross | 1f8f234 | 2015-03-26 16:09:47 -0700 | [diff] [blame] | 1657 | // Host Tests |
| 1658 | // |
| 1659 | |
| 1660 | func CCTestHostFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1661 | module := &CCTest{} |
Colin Cross | 1f8f234 | 2015-03-26 16:09:47 -0700 | [diff] [blame] | 1662 | return NewCCBinary(&module.CCBinary, module, common.HostSupported, |
Colin Cross | 9ffb4f5 | 2015-04-24 17:48:09 -0700 | [diff] [blame] | 1663 | &module.TestProperties) |
Colin Cross | 1f8f234 | 2015-03-26 16:09:47 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | // |
Colin Cross | 2ba19d9 | 2015-05-07 15:44:20 -0700 | [diff] [blame] | 1667 | // Host Benchmarks |
| 1668 | // |
| 1669 | |
| 1670 | func CCBenchmarkHostFactory() (blueprint.Module, []interface{}) { |
| 1671 | module := &CCBenchmark{} |
| 1672 | return NewCCBinary(&module.CCBinary, module, common.HostSupported) |
| 1673 | } |
| 1674 | |
| 1675 | // |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1676 | // Device libraries shipped with gcc |
| 1677 | // |
| 1678 | |
| 1679 | type toolchainLibrary struct { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1680 | CCLibrary |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | func (*toolchainLibrary) AndroidDynamicDependencies(ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1684 | // toolchain libraries can't have any dependencies |
| 1685 | return nil |
| 1686 | } |
| 1687 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1688 | func (*toolchainLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1689 | // toolchain libraries can't have any dependencies |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1690 | return CCDeps{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1691 | } |
| 1692 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1693 | func ToolchainLibraryFactory() (blueprint.Module, []interface{}) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1694 | module := &toolchainLibrary{} |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1695 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1696 | module.LibraryProperties.BuildStatic = true |
| 1697 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1698 | return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth, |
Colin Cross | 21b9a24 | 2015-03-24 14:15:58 -0700 | [diff] [blame] | 1699 | &module.LibraryProperties) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | func (c *toolchainLibrary) compileModule(ctx common.AndroidModuleContext, |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1703 | flags CCFlags, deps CCDeps, objFiles []string) { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1704 | |
| 1705 | libName := ctx.ModuleName() + staticLibraryExtension |
| 1706 | outputFile := filepath.Join(common.ModuleOutDir(ctx), libName) |
| 1707 | |
| 1708 | CopyGccLib(ctx, libName, ccFlagsToBuilderFlags(flags), outputFile) |
| 1709 | |
| 1710 | c.out = outputFile |
| 1711 | |
| 1712 | ctx.CheckbuildFile(outputFile) |
| 1713 | } |
| 1714 | |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1715 | func (c *toolchainLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c403f7c | 2015-03-18 14:01:18 -0700 | [diff] [blame] | 1716 | // Toolchain libraries do not get installed. |
| 1717 | } |
| 1718 | |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1719 | // NDK prebuilt libraries. |
| 1720 | // |
| 1721 | // These differ from regular prebuilts in that they aren't stripped and usually aren't installed |
| 1722 | // either (with the exception of the shared STLs, which are installed to the app's directory rather |
| 1723 | // than to the system image). |
| 1724 | |
| 1725 | func getNdkLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, version string) string { |
| 1726 | return fmt.Sprintf("%s/prebuilts/ndk/current/platforms/android-%s/arch-%s/usr/lib", |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 1727 | ctx.AConfig().SrcDir(), version, toolchain.Name()) |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1730 | func ndkPrebuiltModuleToPath(ctx common.AndroidModuleContext, toolchain Toolchain, |
| 1731 | ext string, version string) string { |
| 1732 | |
| 1733 | // NDK prebuilts are named like: ndk_NAME.EXT.SDK_VERSION. |
| 1734 | // We want to translate to just NAME.EXT |
| 1735 | name := strings.Split(strings.TrimPrefix(ctx.ModuleName(), "ndk_"), ".")[0] |
| 1736 | dir := getNdkLibDir(ctx, toolchain, version) |
| 1737 | return filepath.Join(dir, name+ext) |
| 1738 | } |
| 1739 | |
| 1740 | type ndkPrebuiltObject struct { |
| 1741 | ccObject |
| 1742 | } |
| 1743 | |
| 1744 | func (*ndkPrebuiltObject) AndroidDynamicDependencies( |
| 1745 | ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1746 | |
| 1747 | // NDK objects can't have any dependencies |
| 1748 | return nil |
| 1749 | } |
| 1750 | |
| 1751 | func (*ndkPrebuiltObject) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
| 1752 | // NDK objects can't have any dependencies |
| 1753 | return CCDeps{} |
| 1754 | } |
| 1755 | |
| 1756 | func NdkPrebuiltObjectFactory() (blueprint.Module, []interface{}) { |
| 1757 | module := &ndkPrebuiltObject{} |
| 1758 | return newCCBase(&module.CCBase, module, common.DeviceSupported, common.MultilibBoth) |
| 1759 | } |
| 1760 | |
| 1761 | func (c *ndkPrebuiltObject) compileModule(ctx common.AndroidModuleContext, flags CCFlags, |
| 1762 | deps CCDeps, objFiles []string) { |
| 1763 | // A null build step, but it sets up the output path. |
| 1764 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_crt") { |
| 1765 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_crt prefixed name") |
| 1766 | } |
| 1767 | |
| 1768 | c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, objectExtension, c.Properties.Sdk_version) |
| 1769 | } |
| 1770 | |
| 1771 | func (c *ndkPrebuiltObject) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
| 1772 | // Objects do not get installed. |
| 1773 | } |
| 1774 | |
| 1775 | var _ ccObjectProvider = (*ndkPrebuiltObject)(nil) |
| 1776 | |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1777 | type ndkPrebuiltLibrary struct { |
| 1778 | CCLibrary |
| 1779 | } |
| 1780 | |
| 1781 | func (*ndkPrebuiltLibrary) AndroidDynamicDependencies( |
| 1782 | ctx common.AndroidDynamicDependerModuleContext) []string { |
| 1783 | |
| 1784 | // NDK libraries can't have any dependencies |
| 1785 | return nil |
| 1786 | } |
| 1787 | |
Colin Cross | 0676e2d | 2015-04-24 17:39:18 -0700 | [diff] [blame] | 1788 | func (*ndkPrebuiltLibrary) depNames(ctx common.AndroidBaseContext, depNames CCDeps) CCDeps { |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1789 | // NDK libraries can't have any dependencies |
| 1790 | return CCDeps{} |
| 1791 | } |
| 1792 | |
| 1793 | func NdkPrebuiltLibraryFactory() (blueprint.Module, []interface{}) { |
| 1794 | module := &ndkPrebuiltLibrary{} |
| 1795 | module.LibraryProperties.BuildShared = true |
| 1796 | return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported) |
| 1797 | } |
| 1798 | |
| 1799 | func (c *ndkPrebuiltLibrary) compileModule(ctx common.AndroidModuleContext, flags CCFlags, |
| 1800 | deps CCDeps, objFiles []string) { |
| 1801 | // A null build step, but it sets up the output path. |
| 1802 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
| 1803 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name") |
| 1804 | } |
| 1805 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1806 | includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1807 | c.exportFlags = []string{common.JoinWithPrefix(includeDirs, "-isystem ")} |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1808 | |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1809 | c.out = ndkPrebuiltModuleToPath(ctx, flags.Toolchain, sharedLibraryExtension, |
| 1810 | c.Properties.Sdk_version) |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | func (c *ndkPrebuiltLibrary) installModule(ctx common.AndroidModuleContext, flags CCFlags) { |
Dan Albert | c3144b1 | 2015-04-28 18:17:56 -0700 | [diff] [blame] | 1814 | // NDK prebuilt libraries do not get installed. |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | // The NDK STLs are slightly different from the prebuilt system libraries: |
| 1818 | // * Are not specific to each platform version. |
| 1819 | // * The libraries are not in a predictable location for each STL. |
| 1820 | |
| 1821 | type ndkPrebuiltStl struct { |
| 1822 | ndkPrebuiltLibrary |
| 1823 | } |
| 1824 | |
| 1825 | type ndkPrebuiltStaticStl struct { |
| 1826 | ndkPrebuiltStl |
| 1827 | } |
| 1828 | |
| 1829 | type ndkPrebuiltSharedStl struct { |
| 1830 | ndkPrebuiltStl |
| 1831 | } |
| 1832 | |
| 1833 | func NdkPrebuiltSharedStlFactory() (blueprint.Module, []interface{}) { |
| 1834 | module := &ndkPrebuiltSharedStl{} |
| 1835 | module.LibraryProperties.BuildShared = true |
| 1836 | return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported) |
| 1837 | } |
| 1838 | |
| 1839 | func NdkPrebuiltStaticStlFactory() (blueprint.Module, []interface{}) { |
| 1840 | module := &ndkPrebuiltStaticStl{} |
| 1841 | module.LibraryProperties.BuildStatic = true |
| 1842 | return NewCCLibrary(&module.CCLibrary, module, common.DeviceSupported) |
| 1843 | } |
| 1844 | |
| 1845 | func getNdkStlLibDir(ctx common.AndroidModuleContext, toolchain Toolchain, stl string) string { |
| 1846 | gccVersion := toolchain.GccVersion() |
| 1847 | var libDir string |
| 1848 | switch stl { |
| 1849 | case "libstlport": |
| 1850 | libDir = "cxx-stl/stlport/libs" |
| 1851 | case "libc++": |
| 1852 | libDir = "cxx-stl/llvm-libc++/libs" |
| 1853 | case "libgnustl": |
| 1854 | libDir = fmt.Sprintf("cxx-stl/gnu-libstdc++/%s/libs", gccVersion) |
| 1855 | } |
| 1856 | |
| 1857 | if libDir != "" { |
Colin Cross | 1332b00 | 2015-04-07 17:11:30 -0700 | [diff] [blame] | 1858 | ndkSrcRoot := ctx.AConfig().SrcDir() + "/prebuilts/ndk/current/sources" |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1859 | return fmt.Sprintf("%s/%s/%s", ndkSrcRoot, libDir, ctx.Arch().Abi) |
| 1860 | } |
| 1861 | |
| 1862 | ctx.ModuleErrorf("Unknown NDK STL: %s", stl) |
| 1863 | return "" |
| 1864 | } |
| 1865 | |
| 1866 | func (c *ndkPrebuiltStl) compileModule(ctx common.AndroidModuleContext, flags CCFlags, |
| 1867 | deps CCDeps, objFiles []string) { |
| 1868 | // A null build step, but it sets up the output path. |
| 1869 | if !strings.HasPrefix(ctx.ModuleName(), "ndk_lib") { |
| 1870 | ctx.ModuleErrorf("NDK prebuilts must have an ndk_lib prefixed name") |
| 1871 | } |
| 1872 | |
Colin Cross | fa13879 | 2015-04-24 17:31:52 -0700 | [diff] [blame] | 1873 | includeDirs := pathtools.PrefixPaths(c.Properties.Export_include_dirs, common.ModuleSrcDir(ctx)) |
Colin Cross | 2834452 | 2015-04-22 13:07:53 -0700 | [diff] [blame] | 1874 | c.exportFlags = []string{includeDirsToFlags(includeDirs)} |
Dan Albert | be96168 | 2015-03-18 23:38:50 -0700 | [diff] [blame] | 1875 | |
| 1876 | libName := strings.TrimPrefix(ctx.ModuleName(), "ndk_") |
| 1877 | libExt := sharedLibraryExtension |
| 1878 | if c.LibraryProperties.BuildStatic { |
| 1879 | libExt = staticLibraryExtension |
| 1880 | } |
| 1881 | |
| 1882 | stlName := strings.TrimSuffix(libName, "_shared") |
| 1883 | stlName = strings.TrimSuffix(stlName, "_static") |
| 1884 | libDir := getNdkStlLibDir(ctx, flags.Toolchain, stlName) |
| 1885 | c.out = libDir + "/" + libName + libExt |
| 1886 | } |
| 1887 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1888 | func LinkageMutator(mctx blueprint.EarlyMutatorContext) { |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1889 | if c, ok := mctx.Module().(ccLinkedInterface); ok { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1890 | var modules []blueprint.Module |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1891 | if c.buildStatic() && c.buildShared() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1892 | modules = mctx.CreateLocalVariations("static", "shared") |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 1893 | modules[0].(ccLinkedInterface).setStatic(true) |
| 1894 | modules[1].(ccLinkedInterface).setStatic(false) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1895 | } else if c.buildStatic() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1896 | modules = mctx.CreateLocalVariations("static") |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 1897 | modules[0].(ccLinkedInterface).setStatic(true) |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1898 | } else if c.buildShared() { |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1899 | modules = mctx.CreateLocalVariations("shared") |
Colin Cross | 18b6dc5 | 2015-04-28 13:20:37 -0700 | [diff] [blame] | 1900 | modules[0].(ccLinkedInterface).setStatic(false) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1901 | } else { |
Colin Cross | 97ba073 | 2015-03-23 17:50:24 -0700 | [diff] [blame] | 1902 | panic(fmt.Errorf("ccLibrary %q not static or shared", mctx.ModuleName())) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1903 | } |
Colin Cross | ed4cf0b | 2015-03-26 14:43:45 -0700 | [diff] [blame] | 1904 | |
| 1905 | if _, ok := c.(ccLibraryInterface); ok { |
| 1906 | reuseFrom := modules[0].(ccLibraryInterface) |
| 1907 | for _, m := range modules { |
| 1908 | m.(ccLibraryInterface).setReuseFrom(reuseFrom) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1909 | } |
| 1910 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1911 | } |
| 1912 | } |
Colin Cross | 74d1ec0 | 2015-04-28 13:30:13 -0700 | [diff] [blame] | 1913 | |
| 1914 | // lastUniqueElements returns all unique elements of a slice, keeping the last copy of each |
| 1915 | // modifies the slice contents in place, and returns a subslice of the original slice |
| 1916 | func lastUniqueElements(list []string) []string { |
| 1917 | totalSkip := 0 |
| 1918 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 1919 | skip := 0 |
| 1920 | for j := i - 1; j >= totalSkip; j-- { |
| 1921 | if list[i] == list[j] { |
| 1922 | skip++ |
| 1923 | } else { |
| 1924 | list[j+skip] = list[j] |
| 1925 | } |
| 1926 | } |
| 1927 | totalSkip += skip |
| 1928 | } |
| 1929 | return list[totalSkip:] |
| 1930 | } |