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