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