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