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	"strings"
19
20	"android/soong/android"
21)
22
23var (
24	windowsCflags = []string{
25		"-DUSE_MINGW",
26		"-DWIN32_LEAN_AND_MEAN",
27		"-Wno-unused-parameter",
28
29		// Workaround differences in inttypes.h between host and target.
30		//See bug 12708004.
31		"-D__STDC_FORMAT_MACROS",
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	windowsClangCflags = append(ClangFilterUnknownCflags(windowsCflags), []string{}...)
48
49	windowsIncludeFlags = []string{
50		"-isystem ${WindowsGccRoot}/${WindowsGccTriple}/include",
51	}
52
53	windowsClangCppflags = []string{}
54
55	windowsX86ClangCppflags = []string{
56		// Use SjLj exceptions for 32-bit.  libgcc_eh implements SjLj
57		// exception model for 32-bit.
58		"-fsjlj-exceptions",
59	}
60
61	windowsX8664ClangCppflags = []string{}
62
63	windowsLdflags = []string{
64		"--enable-stdcall-fixup",
65		"-Wl,--dynamicbase",
66		"-Wl,--nxcompat",
67	}
68	windowsLldflags = []string{
69		"-Wl,--Xlink=-Brepro", // Enable deterministic build
70	}
71	windowsClangLdflags  = append(ClangFilterUnknownCflags(windowsLdflags), []string{}...)
72	windowsClangLldflags = append(ClangFilterUnknownLldflags(windowsClangLdflags), windowsLldflags...)
73
74	windowsX86Cflags = []string{
75		"-m32",
76	}
77
78	windowsX8664Cflags = []string{
79		"-m64",
80	}
81
82	windowsX86Ldflags = []string{
83		"-m32",
84		"-Wl,--large-address-aware",
85		"-L${WindowsGccRoot}/${WindowsGccTriple}/lib32",
86		"-static-libgcc",
87	}
88	windowsX86ClangLdflags = append(ClangFilterUnknownCflags(windowsX86Ldflags), []string{
89		"-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
90		"-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
91		"-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
92		"-B${WindowsGccRoot}/${WindowsGccTriple}/lib32",
93	}...)
94	windowsX86ClangLldflags = ClangFilterUnknownLldflags(windowsX86ClangLdflags)
95
96	windowsX8664Ldflags = []string{
97		"-m64",
98		"-L${WindowsGccRoot}/${WindowsGccTriple}/lib64",
99		"-Wl,--high-entropy-va",
100		"-static-libgcc",
101	}
102	windowsX8664ClangLdflags = append(ClangFilterUnknownCflags(windowsX8664Ldflags), []string{
103		"-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
104		"-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
105		"-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
106		"-B${WindowsGccRoot}/${WindowsGccTriple}/lib64",
107	}...)
108	windowsX8664ClangLldflags = ClangFilterUnknownLldflags(windowsX8664ClangLdflags)
109
110	windowsAvailableLibraries = addPrefix([]string{
111		"gdi32",
112		"imagehlp",
113		"iphlpapi",
114		"netapi32",
115		"oleaut32",
116		"ole32",
117		"opengl32",
118		"powrprof",
119		"psapi",
120		"pthread",
121		"userenv",
122		"uuid",
123		"version",
124		"ws2_32",
125		"windowscodecs",
126	}, "-l")
127)
128
129const (
130	windowsGccVersion = "4.8"
131)
132
133func init() {
134	pctx.StaticVariable("WindowsGccVersion", windowsGccVersion)
135
136	pctx.SourcePathVariable("WindowsGccRoot",
137		"prebuilts/gcc/${HostPrebuiltTag}/host/x86_64-w64-mingw32-${WindowsGccVersion}")
138
139	pctx.StaticVariable("WindowsGccTriple", "x86_64-w64-mingw32")
140
141	pctx.StaticVariable("WindowsClangCflags", strings.Join(windowsClangCflags, " "))
142	pctx.StaticVariable("WindowsClangLdflags", strings.Join(windowsClangLdflags, " "))
143	pctx.StaticVariable("WindowsClangLldflags", strings.Join(windowsClangLldflags, " "))
144	pctx.StaticVariable("WindowsClangCppflags", strings.Join(windowsClangCppflags, " "))
145
146	pctx.StaticVariable("WindowsX86ClangCflags",
147		strings.Join(ClangFilterUnknownCflags(windowsX86Cflags), " "))
148	pctx.StaticVariable("WindowsX8664ClangCflags",
149		strings.Join(ClangFilterUnknownCflags(windowsX8664Cflags), " "))
150	pctx.StaticVariable("WindowsX86ClangLdflags", strings.Join(windowsX86ClangLdflags, " "))
151	pctx.StaticVariable("WindowsX86ClangLldflags", strings.Join(windowsX86ClangLldflags, " "))
152	pctx.StaticVariable("WindowsX8664ClangLdflags", strings.Join(windowsX8664ClangLdflags, " "))
153	pctx.StaticVariable("WindowsX8664ClangLldflags", strings.Join(windowsX8664ClangLldflags, " "))
154	pctx.StaticVariable("WindowsX86ClangCppflags", strings.Join(windowsX86ClangCppflags, " "))
155	pctx.StaticVariable("WindowsX8664ClangCppflags", strings.Join(windowsX8664ClangCppflags, " "))
156
157	pctx.StaticVariable("WindowsIncludeFlags", strings.Join(windowsIncludeFlags, " "))
158	// Yasm flags
159	pctx.StaticVariable("WindowsX86YasmFlags", "-f win32 -m x86")
160	pctx.StaticVariable("WindowsX8664YasmFlags", "-f win64 -m amd64")
161}
162
163type toolchainWindows struct {
164	cFlags, ldFlags string
165}
166
167type toolchainWindowsX86 struct {
168	toolchain32Bit
169	toolchainWindows
170}
171
172type toolchainWindowsX8664 struct {
173	toolchain64Bit
174	toolchainWindows
175}
176
177func (t *toolchainWindowsX86) Name() string {
178	return "x86"
179}
180
181func (t *toolchainWindowsX8664) Name() string {
182	return "x86_64"
183}
184
185func (t *toolchainWindows) GccRoot() string {
186	return "${config.WindowsGccRoot}"
187}
188
189func (t *toolchainWindows) GccTriple() string {
190	return "${config.WindowsGccTriple}"
191}
192
193func (t *toolchainWindows) GccVersion() string {
194	return windowsGccVersion
195}
196
197func (t *toolchainWindows) IncludeFlags() string {
198	return "${config.WindowsIncludeFlags}"
199}
200
201func (t *toolchainWindowsX86) WindresFlags() string {
202	return "-F pe-i386"
203}
204
205func (t *toolchainWindowsX8664) WindresFlags() string {
206	return "-F pe-x86-64"
207}
208
209func (t *toolchainWindowsX86) ClangTriple() string {
210	return "i686-windows-gnu"
211}
212
213func (t *toolchainWindowsX8664) ClangTriple() string {
214	return "x86_64-pc-windows-gnu"
215}
216
217func (t *toolchainWindowsX86) ClangCflags() string {
218	return "${config.WindowsClangCflags} ${config.WindowsX86ClangCflags}"
219}
220
221func (t *toolchainWindowsX8664) ClangCflags() string {
222	return "${config.WindowsClangCflags} ${config.WindowsX8664ClangCflags}"
223}
224
225func (t *toolchainWindowsX86) ClangCppflags() string {
226	return "${config.WindowsClangCppflags} ${config.WindowsX86ClangCppflags}"
227}
228
229func (t *toolchainWindowsX8664) ClangCppflags() string {
230	return "${config.WindowsClangCppflags} ${config.WindowsX8664ClangCppflags}"
231}
232
233func (t *toolchainWindowsX86) ClangLdflags() string {
234	return "${config.WindowsClangLdflags} ${config.WindowsX86ClangLdflags}"
235}
236
237func (t *toolchainWindowsX86) ClangLldflags() string {
238	return "${config.WindowsClangLldflags} ${config.WindowsX86ClangLldflags}"
239}
240
241func (t *toolchainWindowsX8664) ClangLdflags() string {
242	return "${config.WindowsClangLdflags} ${config.WindowsX8664ClangLdflags}"
243}
244
245func (t *toolchainWindowsX8664) ClangLldflags() string {
246	return "${config.WindowsClangLldflags} ${config.WindowsX8664ClangLldflags}"
247}
248
249func (t *toolchainWindowsX86) YasmFlags() string {
250	return "${config.WindowsX86YasmFlags}"
251}
252
253func (t *toolchainWindowsX8664) YasmFlags() string {
254	return "${config.WindowsX8664YasmFlags}"
255}
256
257func (t *toolchainWindows) ShlibSuffix() string {
258	return ".dll"
259}
260
261func (t *toolchainWindows) ExecutableSuffix() string {
262	return ".exe"
263}
264
265func (t *toolchainWindows) AvailableLibraries() []string {
266	return windowsAvailableLibraries
267}
268
269func (t *toolchainWindows) Bionic() bool {
270	return false
271}
272
273var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{}
274var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{}
275
276func windowsX86ToolchainFactory(arch android.Arch) Toolchain {
277	return toolchainWindowsX86Singleton
278}
279
280func windowsX8664ToolchainFactory(arch android.Arch) Toolchain {
281	return toolchainWindowsX8664Singleton
282}
283
284func init() {
285	registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
286	registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
287}
288