blob: 1e61b01d0b77500cc7cfc87abaf7cabd064a09c0 [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
Elliott Hughes1c805852024-02-07 18:31:25 +000030 // Workaround differences in <stdint.h> between host and target.
31 // Context: http://b/12708004
Dan Willemsen490fd492015-11-24 17:53:15 -080032 "-D__STDC_CONSTANT_MACROS",
33
34 // Use C99-compliant printf functions (%zd).
35 "-D__USE_MINGW_ANSI_STDIO=1",
Jerome Gaillard82bb8b12018-12-04 12:36:49 +000036 // Admit to using >= Windows 7. Both are needed because of <_mingw.h>.
37 "-D_WIN32_WINNT=0x0601",
38 "-DWINVER=0x0601",
Dan Willemsen490fd492015-11-24 17:53:15 -080039 // Get 64-bit off_t and related functions.
40 "-D_FILE_OFFSET_BITS=64",
41
Stephen Hines2210e722020-07-15 11:11:57 -070042 // Don't adjust the layout of bitfields like msvc does.
43 "-mno-ms-bitfields",
44
Colin Crossb98c8b02016-07-29 13:44:28 -070045 "--sysroot ${WindowsGccRoot}/${WindowsGccTriple}",
Dan Willemsen490fd492015-11-24 17:53:15 -080046 }
47
48 windowsIncludeFlags = []string{
Colin Crossb98c8b02016-07-29 13:44:28 -070049 "-isystem ${WindowsGccRoot}/${WindowsGccTriple}/include",
Dan Willemsen490fd492015-11-24 17:53:15 -080050 }
51
Colin Cross33bac242021-07-14 17:03:16 -070052 windowsCppflags = []string{}
Dan Willemsen01f388c2017-11-30 13:31:26 -080053
Colin Cross33bac242021-07-14 17:03:16 -070054 windowsX86Cppflags = []string{
Pirama Arumuga Nainaraf683372020-09-09 17:54:32 -070055 // Use SjLj exceptions for 32-bit. libgcc_eh implements SjLj
56 // exception model for 32-bit.
57 "-fsjlj-exceptions",
58 }
Dan Willemsen01f388c2017-11-30 13:31:26 -080059
Colin Cross33bac242021-07-14 17:03:16 -070060 windowsX8664Cppflags = []string{}
Dan Willemsen01f388c2017-11-30 13:31:26 -080061
Dan Willemsen490fd492015-11-24 17:53:15 -080062 windowsLdflags = []string{
Pirama Arumuga Nainar191f6462018-08-30 12:37:56 -070063 "-Wl,--dynamicbase",
64 "-Wl,--nxcompat",
Dan Willemsen490fd492015-11-24 17:53:15 -080065 }
Colin Cross33bac242021-07-14 17:03:16 -070066 windowsLldflags = append(windowsLdflags, []string{
Yi Konga3c22e72020-04-09 00:11:53 +080067 "-Wl,--Xlink=-Brepro", // Enable deterministic build
Colin Cross33bac242021-07-14 17:03:16 -070068 }...)
Dan Willemsen07cd0512016-02-03 23:16:33 -080069
70 windowsX86Cflags = []string{
71 "-m32",
72 }
73
74 windowsX8664Cflags = []string{
75 "-m64",
76 }
77
78 windowsX86Ldflags = []string{
79 "-m32",
Dan Willemsen7752bca2017-03-14 13:36:23 -070080 "-Wl,--large-address-aware",
Colin Crossb98c8b02016-07-29 13:44:28 -070081 "-L${WindowsGccRoot}/${WindowsGccTriple}/lib32",
Pirama Arumuga Nainar105cab22019-10-15 13:56:29 -070082 "-static-libgcc",
Colin Cross33bac242021-07-14 17:03:16 -070083
Pirama Arumuga Nainar8a852e72018-06-19 20:07:54 -070084 "-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
Dan Willemsen01f388c2017-11-30 13:31:26 -080085 "-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
86 "-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
87 "-B${WindowsGccRoot}/${WindowsGccTriple}/lib32",
Colin Cross33bac242021-07-14 17:03:16 -070088 }
Dan Willemsen07cd0512016-02-03 23:16:33 -080089
90 windowsX8664Ldflags = []string{
91 "-m64",
Colin Crossb98c8b02016-07-29 13:44:28 -070092 "-L${WindowsGccRoot}/${WindowsGccTriple}/lib64",
Pirama Arumuga Nainar191f6462018-08-30 12:37:56 -070093 "-Wl,--high-entropy-va",
Pirama Arumuga Nainar105cab22019-10-15 13:56:29 -070094 "-static-libgcc",
Colin Cross33bac242021-07-14 17:03:16 -070095
Pirama Arumuga Nainar8a852e72018-06-19 20:07:54 -070096 "-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
Dan Willemsen01f388c2017-11-30 13:31:26 -080097 "-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
98 "-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
99 "-B${WindowsGccRoot}/${WindowsGccTriple}/lib64",
Colin Cross33bac242021-07-14 17:03:16 -0700100 }
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700101
102 windowsAvailableLibraries = addPrefix([]string{
103 "gdi32",
104 "imagehlp",
Dan Willemsenb18cf7a2017-09-09 01:42:00 -0700105 "iphlpapi",
106 "netapi32",
Jerome Gaillard7f7f34f2019-01-18 17:14:08 +0000107 "oleaut32",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700108 "ole32",
Jerome Gaillard7f7f34f2019-01-18 17:14:08 +0000109 "opengl32",
Dan Willemsenb18cf7a2017-09-09 01:42:00 -0700110 "powrprof",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700111 "psapi",
Kenny Rootb9123492016-09-20 14:49:33 -0700112 "pthread",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700113 "userenv",
114 "uuid",
Colin Cross124fd9a2016-11-21 17:31:08 -0800115 "version",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700116 "ws2_32",
Jerome Gaillard7f7f34f2019-01-18 17:14:08 +0000117 "windowscodecs",
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700118 }, "-l")
Dan Willemsen490fd492015-11-24 17:53:15 -0800119)
120
Dan Willemsen34fc3b12015-12-07 12:30:44 -0800121const (
122 windowsGccVersion = "4.8"
123)
124
Dan Willemsen490fd492015-11-24 17:53:15 -0800125func init() {
Colin Crossb98c8b02016-07-29 13:44:28 -0700126 pctx.StaticVariable("WindowsGccVersion", windowsGccVersion)
Dan Willemsen490fd492015-11-24 17:53:15 -0800127
Colin Crossb98c8b02016-07-29 13:44:28 -0700128 pctx.SourcePathVariable("WindowsGccRoot",
129 "prebuilts/gcc/${HostPrebuiltTag}/host/x86_64-w64-mingw32-${WindowsGccVersion}")
Dan Willemsen490fd492015-11-24 17:53:15 -0800130
Colin Crossb98c8b02016-07-29 13:44:28 -0700131 pctx.StaticVariable("WindowsGccTriple", "x86_64-w64-mingw32")
Dan Willemsen490fd492015-11-24 17:53:15 -0800132
Colin Cross0523ba22021-07-14 18:45:05 -0700133 pctx.StaticVariable("WindowsCflags", strings.Join(windowsCflags, " "))
134 pctx.StaticVariable("WindowsLdflags", strings.Join(windowsLdflags, " "))
135 pctx.StaticVariable("WindowsLldflags", strings.Join(windowsLldflags, " "))
136 pctx.StaticVariable("WindowsCppflags", strings.Join(windowsCppflags, " "))
Dan Willemsen01f388c2017-11-30 13:31:26 -0800137
Colin Cross0523ba22021-07-14 18:45:05 -0700138 pctx.StaticVariable("WindowsX86Cflags", strings.Join(windowsX86Cflags, " "))
139 pctx.StaticVariable("WindowsX8664Cflags", strings.Join(windowsX8664Cflags, " "))
140 pctx.StaticVariable("WindowsX86Ldflags", strings.Join(windowsX86Ldflags, " "))
141 pctx.StaticVariable("WindowsX86Lldflags", strings.Join(windowsX86Ldflags, " "))
142 pctx.StaticVariable("WindowsX8664Ldflags", strings.Join(windowsX8664Ldflags, " "))
143 pctx.StaticVariable("WindowsX8664Lldflags", strings.Join(windowsX8664Ldflags, " "))
144 pctx.StaticVariable("WindowsX86Cppflags", strings.Join(windowsX86Cppflags, " "))
145 pctx.StaticVariable("WindowsX8664Cppflags", strings.Join(windowsX8664Cppflags, " "))
Dan Willemsen01f388c2017-11-30 13:31:26 -0800146
Colin Crossb98c8b02016-07-29 13:44:28 -0700147 pctx.StaticVariable("WindowsIncludeFlags", strings.Join(windowsIncludeFlags, " "))
Jerome Gaillard80fec092018-11-29 15:34:32 +0000148 // Yasm flags
149 pctx.StaticVariable("WindowsX86YasmFlags", "-f win32 -m x86")
150 pctx.StaticVariable("WindowsX8664YasmFlags", "-f win64 -m amd64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800151}
152
153type toolchainWindows struct {
Dan Willemsen490fd492015-11-24 17:53:15 -0800154 cFlags, ldFlags string
Colin Crossfc3b0642022-09-01 11:02:15 -0700155 toolchainBase
156 toolchainNoCrt
Dan Willemsen490fd492015-11-24 17:53:15 -0800157}
158
Dan Willemsen07cd0512016-02-03 23:16:33 -0800159type toolchainWindowsX86 struct {
160 toolchain32Bit
161 toolchainWindows
162}
163
164type toolchainWindowsX8664 struct {
165 toolchain64Bit
166 toolchainWindows
167}
168
169func (t *toolchainWindowsX86) Name() string {
Dan Willemsen490fd492015-11-24 17:53:15 -0800170 return "x86"
171}
172
Dan Willemsen07cd0512016-02-03 23:16:33 -0800173func (t *toolchainWindowsX8664) Name() string {
174 return "x86_64"
175}
176
Chih-Hung Hsieh57da8262022-02-15 22:48:04 -0800177func (t *toolchainWindows) ToolchainCflags() string {
Colin Cross4fa894d2022-09-30 15:44:45 -0700178 return "-B" + filepath.Join("${config.WindowsGccRoot}", "${config.WindowsGccTriple}", "bin")
Chih-Hung Hsieh57da8262022-02-15 22:48:04 -0800179}
180
181func (t *toolchainWindows) ToolchainLdflags() string {
Colin Cross4fa894d2022-09-30 15:44:45 -0700182 return "-B" + filepath.Join("${config.WindowsGccRoot}", "${config.WindowsGccTriple}", "bin")
Dan Willemsen490fd492015-11-24 17:53:15 -0800183}
184
Dan Willemsen490fd492015-11-24 17:53:15 -0800185func (t *toolchainWindows) IncludeFlags() string {
Colin Crossb98c8b02016-07-29 13:44:28 -0700186 return "${config.WindowsIncludeFlags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800187}
188
Dan Willemsen01f388c2017-11-30 13:31:26 -0800189func (t *toolchainWindowsX86) ClangTriple() string {
190 return "i686-windows-gnu"
Dan Willemsen490fd492015-11-24 17:53:15 -0800191}
192
Dan Willemsen01f388c2017-11-30 13:31:26 -0800193func (t *toolchainWindowsX8664) ClangTriple() string {
194 return "x86_64-pc-windows-gnu"
Dan Willemsen490fd492015-11-24 17:53:15 -0800195}
196
Colin Cross33bac242021-07-14 17:03:16 -0700197func (t *toolchainWindowsX86) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700198 return "${config.WindowsCflags} ${config.WindowsX86Cflags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800199}
200
Colin Cross33bac242021-07-14 17:03:16 -0700201func (t *toolchainWindowsX8664) Cflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700202 return "${config.WindowsCflags} ${config.WindowsX8664Cflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800203}
204
Colin Cross33bac242021-07-14 17:03:16 -0700205func (t *toolchainWindowsX86) Cppflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700206 return "${config.WindowsCppflags} ${config.WindowsX86Cppflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800207}
208
Colin Cross33bac242021-07-14 17:03:16 -0700209func (t *toolchainWindowsX8664) Cppflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700210 return "${config.WindowsCppflags} ${config.WindowsX8664Cppflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800211}
212
Colin Cross33bac242021-07-14 17:03:16 -0700213func (t *toolchainWindowsX86) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700214 return "${config.WindowsLdflags} ${config.WindowsX86Ldflags}"
Dan Willemsen01f388c2017-11-30 13:31:26 -0800215}
216
Colin Cross33bac242021-07-14 17:03:16 -0700217func (t *toolchainWindowsX86) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700218 return "${config.WindowsLldflags} ${config.WindowsX86Lldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700219}
220
Colin Cross33bac242021-07-14 17:03:16 -0700221func (t *toolchainWindowsX8664) Ldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700222 return "${config.WindowsLdflags} ${config.WindowsX8664Ldflags}"
Dan Willemsen490fd492015-11-24 17:53:15 -0800223}
224
Colin Cross33bac242021-07-14 17:03:16 -0700225func (t *toolchainWindowsX8664) Lldflags() string {
Colin Cross0523ba22021-07-14 18:45:05 -0700226 return "${config.WindowsLldflags} ${config.WindowsX8664Lldflags}"
Chih-Hung Hsieh02b4da52018-04-03 11:33:34 -0700227}
228
Jerome Gaillard80fec092018-11-29 15:34:32 +0000229func (t *toolchainWindowsX86) YasmFlags() string {
230 return "${config.WindowsX86YasmFlags}"
231}
232
233func (t *toolchainWindowsX8664) YasmFlags() string {
234 return "${config.WindowsX8664YasmFlags}"
235}
236
Dan Willemsen490fd492015-11-24 17:53:15 -0800237func (t *toolchainWindows) ShlibSuffix() string {
238 return ".dll"
239}
240
241func (t *toolchainWindows) ExecutableSuffix() string {
242 return ".exe"
243}
244
Dan Willemsen2b1f0942016-07-20 12:47:47 -0700245func (t *toolchainWindows) AvailableLibraries() []string {
246 return windowsAvailableLibraries
247}
248
Dan Willemsen2e47b342016-11-17 01:02:25 -0800249func (t *toolchainWindows) Bionic() bool {
250 return false
251}
252
Dan Willemsen07cd0512016-02-03 23:16:33 -0800253var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{}
254var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{}
Dan Willemsen490fd492015-11-24 17:53:15 -0800255
Colin Cross635c3b02016-05-18 15:37:25 -0700256func windowsX86ToolchainFactory(arch android.Arch) Toolchain {
Dan Willemsen07cd0512016-02-03 23:16:33 -0800257 return toolchainWindowsX86Singleton
258}
259
Colin Cross635c3b02016-05-18 15:37:25 -0700260func windowsX8664ToolchainFactory(arch android.Arch) Toolchain {
Dan Willemsen07cd0512016-02-03 23:16:33 -0800261 return toolchainWindowsX8664Singleton
Dan Willemsen490fd492015-11-24 17:53:15 -0800262}
263
264func init() {
Colin Crossa1ad8d12016-06-01 17:09:44 -0700265 registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
266 registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
Dan Willemsen490fd492015-11-24 17:53:15 -0800267}