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