blob: a33606f99c5ebbe8959dd8eeeac83bea3ad1dfb3 [file] [log] [blame]
Dan Willemsen490fd492015-11-24 17:53:15 -08001// 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
Colin Crossb98c8b02016-07-29 13:44:28 -070015package config
Dan Willemsen490fd492015-11-24 17:53:15 -080016
17import (
Chih-Hung Hsieh57da8262022-02-15 22:48:04 -080018 "path/filepath"
Dan Willemsen490fd492015-11-24 17:53:15 -080019 "strings"
20
Colin Cross635c3b02016-05-18 15:37:25 -070021 "android/soong/android"
Dan Willemsen490fd492015-11-24 17:53:15 -080022)
23
24var (
25 windowsCflags = []string{
Dan Willemsen490fd492015-11-24 17:53:15 -080026 "-DUSE_MINGW",
27 "-DWIN32_LEAN_AND_MEAN",
28 "-Wno-unused-parameter",
Dan Willemsen490fd492015-11-24 17:53:15 -080029
30 // Workaround differences in inttypes.h between host and target.
31 //See bug 12708004.
32 "-D__STDC_FORMAT_MACROS",
33 "-D__STDC_CONSTANT_MACROS",
34
35 // Use C99-compliant printf functions (%zd).
36 "-D__USE_MINGW_ANSI_STDIO=1",
Jerome Gaillard82bb8b12018-12-04 12:36:49 +000037 // Admit to using >= Windows 7. Both are needed because of <_mingw.h>.
38 "-D_WIN32_WINNT=0x0601",
39 "-DWINVER=0x0601",
Dan Willemsen490fd492015-11-24 17:53:15 -080040 // Get 64-bit off_t and related functions.
41 "-D_FILE_OFFSET_BITS=64",
42
Stephen Hines2210e722020-07-15 11:11:57 -070043 // Don't adjust the layout of bitfields like msvc does.
44 "-mno-ms-bitfields",
45
Colin Crossb98c8b02016-07-29 13:44:28 -070046 "--sysroot ${WindowsGccRoot}/${WindowsGccTriple}",
Dan Willemsen490fd492015-11-24 17:53:15 -080047 }
48
49 windowsIncludeFlags = []string{
Colin Crossb98c8b02016-07-29 13:44:28 -070050 "-isystem ${WindowsGccRoot}/${WindowsGccTriple}/include",
Dan Willemsen490fd492015-11-24 17:53:15 -080051 }
52
Colin Cross33bac242021-07-14 17:03:16 -070053 windowsCppflags = []string{}
Dan Willemsen01f388c2017-11-30 13:31:26 -080054
Colin Cross33bac242021-07-14 17:03:16 -070055 windowsX86Cppflags = []string{
Pirama Arumuga Nainaraf683372020-09-09 17:54:32 -070056 // Use SjLj exceptions for 32-bit. libgcc_eh implements SjLj
57 // exception model for 32-bit.
58 "-fsjlj-exceptions",
59 }
Dan Willemsen01f388c2017-11-30 13:31:26 -080060
Colin Cross33bac242021-07-14 17:03:16 -070061 windowsX8664Cppflags = []string{}
Dan Willemsen01f388c2017-11-30 13:31:26 -080062
Dan Willemsen490fd492015-11-24 17:53:15 -080063 windowsLdflags = []string{
Pirama Arumuga Nainar191f6462018-08-30 12:37:56 -070064 "-Wl,--dynamicbase",
65 "-Wl,--nxcompat",
Dan Willemsen490fd492015-11-24 17:53:15 -080066 }
Colin Cross33bac242021-07-14 17:03:16 -070067 windowsLldflags = append(windowsLdflags, []string{
Yi Konga3c22e72020-04-09 00:11:53 +080068 "-Wl,--Xlink=-Brepro", // Enable deterministic build
Colin Cross33bac242021-07-14 17:03:16 -070069 }...)
Dan Willemsen07cd0512016-02-03 23:16:33 -080070
71 windowsX86Cflags = []string{
72 "-m32",
73 }
74
75 windowsX8664Cflags = []string{
76 "-m64",
77 }
78
79 windowsX86Ldflags = []string{
80 "-m32",
Dan Willemsen7752bca2017-03-14 13:36:23 -070081 "-Wl,--large-address-aware",
Colin Crossb98c8b02016-07-29 13:44:28 -070082 "-L${WindowsGccRoot}/${WindowsGccTriple}/lib32",
Pirama Arumuga Nainar105cab22019-10-15 13:56:29 -070083 "-static-libgcc",
Colin Cross33bac242021-07-14 17:03:16 -070084
Pirama Arumuga Nainar8a852e72018-06-19 20:07:54 -070085 "-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
Dan Willemsen01f388c2017-11-30 13:31:26 -080086 "-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
87 "-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
88 "-B${WindowsGccRoot}/${WindowsGccTriple}/lib32",
Colin Cross33bac242021-07-14 17:03:16 -070089 }
Dan Willemsen07cd0512016-02-03 23:16:33 -080090
91 windowsX8664Ldflags = []string{
92 "-m64",
Colin Crossb98c8b02016-07-29 13:44:28 -070093 "-L${WindowsGccRoot}/${WindowsGccTriple}/lib64",
Pirama Arumuga Nainar191f6462018-08-30 12:37:56 -070094 "-Wl,--high-entropy-va",
Pirama Arumuga Nainar105cab22019-10-15 13:56:29 -070095 "-static-libgcc",
Colin Cross33bac242021-07-14 17:03:16 -070096
Pirama Arumuga Nainar8a852e72018-06-19 20:07:54 -070097 "-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
Dan Willemsen01f388c2017-11-30 13:31:26 -080098 "-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
99 "-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
100 "-B${WindowsGccRoot}/${WindowsGccTriple}/lib64",
Colin Cross33bac242021-07-14 17:03:16 -0700101 }
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700102
103 windowsAvailableLibraries = addPrefix([]string{
104 "gdi32",
105 "imagehlp",
Dan Willemsenb18cf7a2017-09-09 01:42:00 -0700106 "iphlpapi",
107 "netapi32",
Jerome Gaillard7f7f34f2019-01-18 17:14:08 +0000108 "oleaut32",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700109 "ole32",
Jerome Gaillard7f7f34f2019-01-18 17:14:08 +0000110 "opengl32",
Dan Willemsenb18cf7a2017-09-09 01:42:00 -0700111 "powrprof",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700112 "psapi",
Kenny Rootb9123492016-09-20 14:49:33 -0700113 "pthread",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700114 "userenv",
115 "uuid",
Colin Cross124fd9a2016-11-21 17:31:08 -0800116 "version",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700117 "ws2_32",
Jerome Gaillard7f7f34f2019-01-18 17:14:08 +0000118 "windowscodecs",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700119 }, "-l")
Dan Willemsen490fd492015-11-24 17:53:15 -0800120)
121
Dan Willemsen34fc3b12015-12-07 12:30:44 -0800122const (
123 windowsGccVersion = "4.8"
124)
125
Dan Willemsen490fd492015-11-24 17:53:15 -0800126func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700127 pctx.StaticVariable("WindowsGccVersion", windowsGccVersion)
Dan Willemsen490fd492015-11-24 17:53:15 -0800128
Colin Crossb98c8b02016-07-29 13:44:28 -0700129 pctx.SourcePathVariable("WindowsGccRoot",
130 "prebuilts/gcc/${HostPrebuiltTag}/host/x86_64-w64-mingw32-${WindowsGccVersion}")
Dan Willemsen490fd492015-11-24 17:53:15 -0800131
Colin Crossb98c8b02016-07-29 13:44:28 -0700132 pctx.StaticVariable("WindowsGccTriple", "x86_64-w64-mingw32")
Dan Willemsen490fd492015-11-24 17:53:15 -0800133
Colin Cross0523ba22021-07-14 18:45:05 -0700134 pctx.StaticVariable("WindowsCflags", strings.Join(windowsCflags, " "))
135 pctx.StaticVariable("WindowsLdflags", strings.Join(windowsLdflags, " "))
136 pctx.StaticVariable("WindowsLldflags", strings.Join(windowsLldflags, " "))
137 pctx.StaticVariable("WindowsCppflags", strings.Join(windowsCppflags, " "))
Dan Willemsen01f388c2017-11-30 13:31:26 -0800138
Colin Cross0523ba22021-07-14 18:45:05 -0700139 pctx.StaticVariable("WindowsX86Cflags", strings.Join(windowsX86Cflags, " "))
140 pctx.StaticVariable("WindowsX8664Cflags", strings.Join(windowsX8664Cflags, " "))
141 pctx.StaticVariable("WindowsX86Ldflags", strings.Join(windowsX86Ldflags, " "))
142 pctx.StaticVariable("WindowsX86Lldflags", strings.Join(windowsX86Ldflags, " "))
143 pctx.StaticVariable("WindowsX8664Ldflags", strings.Join(windowsX8664Ldflags, " "))
144 pctx.StaticVariable("WindowsX8664Lldflags", strings.Join(windowsX8664Ldflags, " "))
145 pctx.StaticVariable("WindowsX86Cppflags", strings.Join(windowsX86Cppflags, " "))
146 pctx.StaticVariable("WindowsX8664Cppflags", strings.Join(windowsX8664Cppflags, " "))
Dan Willemsen01f388c2017-11-30 13:31:26 -0800147
Colin Crossb98c8b02016-07-29 13:44:28 -0700148 pctx.StaticVariable("WindowsIncludeFlags", strings.Join(windowsIncludeFlags, " "))
Jerome Gaillard80fec092018-11-29 15:34:32 +0000149 // Yasm flags
150 pctx.StaticVariable("WindowsX86YasmFlags", "-f win32 -m x86")
151 pctx.StaticVariable("WindowsX8664YasmFlags", "-f win64 -m amd64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800152}
153
154type toolchainWindows struct {
Dan Willemsen490fd492015-11-24 17:53:15 -0800155 cFlags, ldFlags string
Colin Crossfc3b0642022-09-01 11:02:15 -0700156 toolchainBase
157 toolchainNoCrt
Dan Willemsen490fd492015-11-24 17:53:15 -0800158}
159
Dan Willemsen07cd0512016-02-03 23:16:33 -0800160type toolchainWindowsX86 struct {
161 toolchain32Bit
162 toolchainWindows
163}
164
165type toolchainWindowsX8664 struct {
166 toolchain64Bit
167 toolchainWindows
168}
169
170func (t *toolchainWindowsX86) Name() string {
Dan Willemsen490fd492015-11-24 17:53:15 -0800171 return "x86"
172}
173
Dan Willemsen07cd0512016-02-03 23:16:33 -0800174func (t *toolchainWindowsX8664) Name() string {
175 return "x86_64"
176}
177
Dan Willemsen490fd492015-11-24 17:53:15 -0800178func (t *toolchainWindows) GccRoot() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700179 return "${config.WindowsGccRoot}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800180}
181
182func (t *toolchainWindows) GccTriple() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700183 return "${config.WindowsGccTriple}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800184}
185
Chih-Hung Hsieh57da8262022-02-15 22:48:04 -0800186func (t *toolchainWindows) ToolchainCflags() string {
187 return "-B" + filepath.Join(t.GccRoot(), t.GccTriple(), "bin")
188}
189
190func (t *toolchainWindows) ToolchainLdflags() string {
191 return "-B" + filepath.Join(t.GccRoot(), t.GccTriple(), "bin")
192}
193
Dan Willemsen490fd492015-11-24 17:53:15 -0800194func (t *toolchainWindows) GccVersion() string {
Dan Willemsen34fc3b12015-12-07 12:30:44 -0800195 return windowsGccVersion
Dan Willemsen490fd492015-11-24 17:53:15 -0800196}
197
Dan Willemsen490fd492015-11-24 17:53:15 -0800198func (t *toolchainWindows) IncludeFlags() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700199 return "${config.WindowsIncludeFlags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800200}
201
Dan Willemsen01f388c2017-11-30 13:31:26 -0800202func (t *toolchainWindowsX86) ClangTriple() string {
203 return "i686-windows-gnu"
Dan Willemsen490fd492015-11-24 17:53:15 -0800204}
205
Dan Willemsen01f388c2017-11-30 13:31:26 -0800206func (t *toolchainWindowsX8664) ClangTriple() string {
207 return "x86_64-pc-windows-gnu"
Dan Willemsen490fd492015-11-24 17:53:15 -0800208}
209
Colin Cross33bac242021-07-14 17:03:16 -0700210func (t *toolchainWindowsX86) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700211 return "${config.WindowsCflags} ${config.WindowsX86Cflags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800212}
213
Colin Cross33bac242021-07-14 17:03:16 -0700214func (t *toolchainWindowsX8664) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700215 return "${config.WindowsCflags} ${config.WindowsX8664Cflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800216}
217
Colin Cross33bac242021-07-14 17:03:16 -0700218func (t *toolchainWindowsX86) Cppflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700219 return "${config.WindowsCppflags} ${config.WindowsX86Cppflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800220}
221
Colin Cross33bac242021-07-14 17:03:16 -0700222func (t *toolchainWindowsX8664) Cppflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700223 return "${config.WindowsCppflags} ${config.WindowsX8664Cppflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800224}
225
Colin Cross33bac242021-07-14 17:03:16 -0700226func (t *toolchainWindowsX86) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700227 return "${config.WindowsLdflags} ${config.WindowsX86Ldflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800228}
229
Colin Cross33bac242021-07-14 17:03:16 -0700230func (t *toolchainWindowsX86) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700231 return "${config.WindowsLldflags} ${config.WindowsX86Lldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700232}
233
Colin Cross33bac242021-07-14 17:03:16 -0700234func (t *toolchainWindowsX8664) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700235 return "${config.WindowsLdflags} ${config.WindowsX8664Ldflags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800236}
237
Colin Cross33bac242021-07-14 17:03:16 -0700238func (t *toolchainWindowsX8664) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700239 return "${config.WindowsLldflags} ${config.WindowsX8664Lldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700240}
241
Jerome Gaillard80fec092018-11-29 15:34:32 +0000242func (t *toolchainWindowsX86) YasmFlags() string {
243 return "${config.WindowsX86YasmFlags}"
244}
245
246func (t *toolchainWindowsX8664) YasmFlags() string {
247 return "${config.WindowsX8664YasmFlags}"
248}
249
Dan Willemsen490fd492015-11-24 17:53:15 -0800250func (t *toolchainWindows) ShlibSuffix() string {
251 return ".dll"
252}
253
254func (t *toolchainWindows) ExecutableSuffix() string {
255 return ".exe"
256}
257
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700258func (t *toolchainWindows) AvailableLibraries() []string {
259 return windowsAvailableLibraries
260}
261
Dan Willemsen2e47b342016-11-17 01:02:25 -0800262func (t *toolchainWindows) Bionic() bool {
263 return false
264}
265
Dan Willemsen07cd0512016-02-03 23:16:33 -0800266var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{}
267var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{}
Dan Willemsen490fd492015-11-24 17:53:15 -0800268
Colin Cross635c3b02016-05-18 15:37:25 -0700269func windowsX86ToolchainFactory(arch android.Arch) Toolchain {
Dan Willemsen07cd0512016-02-03 23:16:33 -0800270 return toolchainWindowsX86Singleton
271}
272
Colin Cross635c3b02016-05-18 15:37:25 -0700273func windowsX8664ToolchainFactory(arch android.Arch) Toolchain {
Dan Willemsen07cd0512016-02-03 23:16:33 -0800274 return toolchainWindowsX8664Singleton
Dan Willemsen490fd492015-11-24 17:53:15 -0800275}
276
277func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700278 registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
279 registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
Dan Willemsen490fd492015-11-24 17:53:15 -0800280}