blob: 2c8321100bbfc478cb05d0b848d5acaa7ff0825a [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
156}
157
Dan Willemsen07cd0512016-02-03 23:16:33 -0800158type toolchainWindowsX86 struct {
159 toolchain32Bit
160 toolchainWindows
161}
162
163type toolchainWindowsX8664 struct {
164 toolchain64Bit
165 toolchainWindows
166}
167
168func (t *toolchainWindowsX86) Name() string {
Dan Willemsen490fd492015-11-24 17:53:15 -0800169 return "x86"
170}
171
Dan Willemsen07cd0512016-02-03 23:16:33 -0800172func (t *toolchainWindowsX8664) Name() string {
173 return "x86_64"
174}
175
Dan Willemsen490fd492015-11-24 17:53:15 -0800176func (t *toolchainWindows) GccRoot() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700177 return "${config.WindowsGccRoot}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800178}
179
180func (t *toolchainWindows) GccTriple() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700181 return "${config.WindowsGccTriple}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800182}
183
Chih-Hung Hsieh57da8262022-02-15 22:48:04 -0800184func (t *toolchainWindows) ToolchainCflags() string {
185 return "-B" + filepath.Join(t.GccRoot(), t.GccTriple(), "bin")
186}
187
188func (t *toolchainWindows) ToolchainLdflags() string {
189 return "-B" + filepath.Join(t.GccRoot(), t.GccTriple(), "bin")
190}
191
Dan Willemsen490fd492015-11-24 17:53:15 -0800192func (t *toolchainWindows) GccVersion() string {
Dan Willemsen34fc3b12015-12-07 12:30:44 -0800193 return windowsGccVersion
Dan Willemsen490fd492015-11-24 17:53:15 -0800194}
195
Dan Willemsen490fd492015-11-24 17:53:15 -0800196func (t *toolchainWindows) IncludeFlags() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700197 return "${config.WindowsIncludeFlags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800198}
199
Dan Willemsen01f388c2017-11-30 13:31:26 -0800200func (t *toolchainWindowsX86) ClangTriple() string {
201 return "i686-windows-gnu"
Dan Willemsen490fd492015-11-24 17:53:15 -0800202}
203
Dan Willemsen01f388c2017-11-30 13:31:26 -0800204func (t *toolchainWindowsX8664) ClangTriple() string {
205 return "x86_64-pc-windows-gnu"
Dan Willemsen490fd492015-11-24 17:53:15 -0800206}
207
Colin Cross33bac242021-07-14 17:03:16 -0700208func (t *toolchainWindowsX86) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700209 return "${config.WindowsCflags} ${config.WindowsX86Cflags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800210}
211
Colin Cross33bac242021-07-14 17:03:16 -0700212func (t *toolchainWindowsX8664) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700213 return "${config.WindowsCflags} ${config.WindowsX8664Cflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800214}
215
Colin Cross33bac242021-07-14 17:03:16 -0700216func (t *toolchainWindowsX86) Cppflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700217 return "${config.WindowsCppflags} ${config.WindowsX86Cppflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800218}
219
Colin Cross33bac242021-07-14 17:03:16 -0700220func (t *toolchainWindowsX8664) Cppflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700221 return "${config.WindowsCppflags} ${config.WindowsX8664Cppflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800222}
223
Colin Cross33bac242021-07-14 17:03:16 -0700224func (t *toolchainWindowsX86) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700225 return "${config.WindowsLdflags} ${config.WindowsX86Ldflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800226}
227
Colin Cross33bac242021-07-14 17:03:16 -0700228func (t *toolchainWindowsX86) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700229 return "${config.WindowsLldflags} ${config.WindowsX86Lldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700230}
231
Colin Cross33bac242021-07-14 17:03:16 -0700232func (t *toolchainWindowsX8664) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700233 return "${config.WindowsLdflags} ${config.WindowsX8664Ldflags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800234}
235
Colin Cross33bac242021-07-14 17:03:16 -0700236func (t *toolchainWindowsX8664) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700237 return "${config.WindowsLldflags} ${config.WindowsX8664Lldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700238}
239
Jerome Gaillard80fec092018-11-29 15:34:32 +0000240func (t *toolchainWindowsX86) YasmFlags() string {
241 return "${config.WindowsX86YasmFlags}"
242}
243
244func (t *toolchainWindowsX8664) YasmFlags() string {
245 return "${config.WindowsX8664YasmFlags}"
246}
247
Dan Willemsen490fd492015-11-24 17:53:15 -0800248func (t *toolchainWindows) ShlibSuffix() string {
249 return ".dll"
250}
251
252func (t *toolchainWindows) ExecutableSuffix() string {
253 return ".exe"
254}
255
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700256func (t *toolchainWindows) AvailableLibraries() []string {
257 return windowsAvailableLibraries
258}
259
Dan Willemsen2e47b342016-11-17 01:02:25 -0800260func (t *toolchainWindows) Bionic() bool {
261 return false
262}
263
Dan Willemsen07cd0512016-02-03 23:16:33 -0800264var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{}
265var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{}
Dan Willemsen490fd492015-11-24 17:53:15 -0800266
Colin Cross635c3b02016-05-18 15:37:25 -0700267func windowsX86ToolchainFactory(arch android.Arch) Toolchain {
Dan Willemsen07cd0512016-02-03 23:16:33 -0800268 return toolchainWindowsX86Singleton
269}
270
Colin Cross635c3b02016-05-18 15:37:25 -0700271func windowsX8664ToolchainFactory(arch android.Arch) Toolchain {
Dan Willemsen07cd0512016-02-03 23:16:33 -0800272 return toolchainWindowsX8664Singleton
Dan Willemsen490fd492015-11-24 17:53:15 -0800273}
274
275func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700276 registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
277 registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
Dan Willemsen490fd492015-11-24 17:53:15 -0800278}