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