1// Copyright 2015 Google Inc. All rights reserved. 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 15package config 16 17import ( 18 "path/filepath" 19 "strings" 20 21 "android/soong/android" 22) 23 24var ( 25 windowsCflags = []string{ 26 "-DUSE_MINGW", 27 "-DWIN32_LEAN_AND_MEAN", 28 "-Wno-unused-parameter", 29 30 // Workaround differences in <stdint.h> between host and target. 31 // Context: http://b/12708004 32 "-D__STDC_CONSTANT_MACROS", 33 34 // Use C99-compliant printf functions (%zd). 35 "-D__USE_MINGW_ANSI_STDIO=1", 36 // Admit to using >= Windows 7. Both are needed because of <_mingw.h>. 37 "-D_WIN32_WINNT=0x0601", 38 "-DWINVER=0x0601", 39 // Get 64-bit off_t and related functions. 40 "-D_FILE_OFFSET_BITS=64", 41 42 // Don't adjust the layout of bitfields like msvc does. 43 "-mno-ms-bitfields", 44 45 "--sysroot ${WindowsGccRoot}/${WindowsGccTriple}", 46 47 // Windows flags to generate PDB 48 "-g", 49 "-gcodeview", 50 51 "-fno-omit-frame-pointer", 52 } 53 54 windowsIncludeFlags = []string{ 55 "-isystem ${WindowsGccRoot}/${WindowsGccTriple}/include", 56 } 57 58 windowsCppflags = []string{} 59 60 windowsX86Cppflags = []string{ 61 // Use SjLj exceptions for 32-bit. libgcc_eh implements SjLj 62 // exception model for 32-bit. 63 "-fsjlj-exceptions", 64 } 65 66 windowsX8664Cppflags = []string{} 67 68 windowsLdflags = []string{ 69 "-Wl,--dynamicbase", 70 "-Wl,--nxcompat", 71 } 72 windowsLldflags = append(windowsLdflags, []string{ 73 "-Wl,--Xlink=-Brepro", // Enable deterministic build 74 }...) 75 76 windowsX86Cflags = []string{ 77 "-m32", 78 } 79 80 windowsX8664Cflags = []string{ 81 "-m64", 82 } 83 84 windowsX86Ldflags = []string{ 85 "-m32", 86 "-Wl,--large-address-aware", 87 "-L${WindowsGccRoot}/${WindowsGccTriple}/lib32", 88 "-static-libgcc", 89 90 "-B${WindowsGccRoot}/${WindowsGccTriple}/bin", 91 "-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32", 92 "-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32", 93 "-B${WindowsGccRoot}/${WindowsGccTriple}/lib32", 94 } 95 96 windowsX8664Ldflags = []string{ 97 "-m64", 98 "-L${WindowsGccRoot}/${WindowsGccTriple}/lib64", 99 "-Wl,--high-entropy-va", 100 "-static-libgcc", 101 102 "-B${WindowsGccRoot}/${WindowsGccTriple}/bin", 103 "-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3", 104 "-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3", 105 "-B${WindowsGccRoot}/${WindowsGccTriple}/lib64", 106 } 107 108 windowsAvailableLibraries = addPrefix([]string{ 109 "bcrypt", 110 "dbghelp", 111 "gdi32", 112 "imagehlp", 113 "iphlpapi", 114 "netapi32", 115 "ntdll", 116 "oleaut32", 117 "ole32", 118 "opengl32", 119 "powrprof", 120 "psapi", 121 "pthread", 122 "userenv", 123 "uuid", 124 "version", 125 "ws2_32", 126 "windowscodecs", 127 }, "-l") 128) 129 130const ( 131 windowsGccVersion = "4.8" 132) 133 134func init() { 135 pctx.StaticVariable("WindowsGccVersion", windowsGccVersion) 136 137 pctx.SourcePathVariable("WindowsGccRoot", 138 "prebuilts/gcc/${HostPrebuiltTag}/host/x86_64-w64-mingw32-${WindowsGccVersion}") 139 140 pctx.StaticVariable("WindowsGccTriple", "x86_64-w64-mingw32") 141 142 pctx.StaticVariable("WindowsCflags", strings.Join(windowsCflags, " ")) 143 pctx.StaticVariable("WindowsLdflags", strings.Join(windowsLdflags, " ")) 144 pctx.StaticVariable("WindowsLldflags", strings.Join(windowsLldflags, " ")) 145 pctx.StaticVariable("WindowsCppflags", strings.Join(windowsCppflags, " ")) 146 147 pctx.StaticVariable("WindowsX86Cflags", strings.Join(windowsX86Cflags, " ")) 148 pctx.StaticVariable("WindowsX8664Cflags", strings.Join(windowsX8664Cflags, " ")) 149 pctx.StaticVariable("WindowsX86Ldflags", strings.Join(windowsX86Ldflags, " ")) 150 pctx.StaticVariable("WindowsX86Lldflags", strings.Join(windowsX86Ldflags, " ")) 151 pctx.StaticVariable("WindowsX8664Ldflags", strings.Join(windowsX8664Ldflags, " ")) 152 pctx.StaticVariable("WindowsX8664Lldflags", strings.Join(windowsX8664Ldflags, " ")) 153 pctx.StaticVariable("WindowsX86Cppflags", strings.Join(windowsX86Cppflags, " ")) 154 pctx.StaticVariable("WindowsX8664Cppflags", strings.Join(windowsX8664Cppflags, " ")) 155 156 pctx.StaticVariable("WindowsIncludeFlags", strings.Join(windowsIncludeFlags, " ")) 157 // Yasm flags 158 pctx.StaticVariable("WindowsX86YasmFlags", "-f win32 -m x86") 159 pctx.StaticVariable("WindowsX8664YasmFlags", "-f win64 -m amd64") 160} 161 162type toolchainWindows struct { 163 cFlags, ldFlags string 164 toolchainBase 165 toolchainNoCrt 166} 167 168type toolchainWindowsX86 struct { 169 toolchain32Bit 170 toolchainWindows 171} 172 173type toolchainWindowsX8664 struct { 174 toolchain64Bit 175 toolchainWindows 176} 177 178func (t *toolchainWindowsX86) Name() string { 179 return "x86" 180} 181 182func (t *toolchainWindowsX8664) Name() string { 183 return "x86_64" 184} 185 186func (t *toolchainWindows) ToolchainCflags() string { 187 return "-B" + filepath.Join("${config.WindowsGccRoot}", "${config.WindowsGccTriple}", "bin") 188} 189 190func (t *toolchainWindows) ToolchainLdflags() string { 191 return "-B" + filepath.Join("${config.WindowsGccRoot}", "${config.WindowsGccTriple}", "bin") 192} 193 194func (t *toolchainWindows) IncludeFlags() string { 195 return "${config.WindowsIncludeFlags}" 196} 197 198func (t *toolchainWindowsX86) ClangTriple() string { 199 return "i686-windows-gnu" 200} 201 202func (t *toolchainWindowsX8664) ClangTriple() string { 203 return "x86_64-pc-windows-gnu" 204} 205 206func (t *toolchainWindowsX86) Cflags() string { 207 return "${config.WindowsCflags} ${config.WindowsX86Cflags}" 208} 209 210func (t *toolchainWindowsX8664) Cflags() string { 211 return "${config.WindowsCflags} ${config.WindowsX8664Cflags}" 212} 213 214func (t *toolchainWindowsX86) Cppflags() string { 215 return "${config.WindowsCppflags} ${config.WindowsX86Cppflags}" 216} 217 218func (t *toolchainWindowsX8664) Cppflags() string { 219 return "${config.WindowsCppflags} ${config.WindowsX8664Cppflags}" 220} 221 222func (t *toolchainWindowsX86) Ldflags() string { 223 return "${config.WindowsLdflags} ${config.WindowsX86Ldflags}" 224} 225 226func (t *toolchainWindowsX86) Lldflags() string { 227 return "${config.WindowsLldflags} ${config.WindowsX86Lldflags}" 228} 229 230func (t *toolchainWindowsX8664) Ldflags() string { 231 return "${config.WindowsLdflags} ${config.WindowsX8664Ldflags}" 232} 233 234func (t *toolchainWindowsX8664) Lldflags() string { 235 return "${config.WindowsLldflags} ${config.WindowsX8664Lldflags}" 236} 237 238func (t *toolchainWindowsX86) YasmFlags() string { 239 return "${config.WindowsX86YasmFlags}" 240} 241 242func (t *toolchainWindowsX8664) YasmFlags() string { 243 return "${config.WindowsX8664YasmFlags}" 244} 245 246func (t *toolchainWindows) ShlibSuffix() string { 247 return ".dll" 248} 249 250func (t *toolchainWindows) ExecutableSuffix() string { 251 return ".exe" 252} 253 254func (t *toolchainWindows) AvailableLibraries() []string { 255 return windowsAvailableLibraries 256} 257 258func (t *toolchainWindows) Bionic() bool { 259 return false 260} 261 262var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{} 263var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{} 264 265func windowsX86ToolchainFactory(arch android.Arch) Toolchain { 266 return toolchainWindowsX86Singleton 267} 268 269func windowsX8664ToolchainFactory(arch android.Arch) Toolchain { 270 return toolchainWindowsX8664Singleton 271} 272 273func init() { 274 registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory) 275 registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory) 276} 277