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