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
48	windowsIncludeFlags = []string{
49		"-isystem ${WindowsGccRoot}/${WindowsGccTriple}/include",
50	}
51
52	windowsCppflags = []string{}
53
54	windowsX86Cppflags = []string{
55		// Use SjLj exceptions for 32-bit.  libgcc_eh implements SjLj
56		// exception model for 32-bit.
57		"-fsjlj-exceptions",
58	}
59
60	windowsX8664Cppflags = []string{}
61
62	windowsLdflags = []string{
63		"-Wl,--dynamicbase",
64		"-Wl,--nxcompat",
65	}
66	windowsLldflags = append(windowsLdflags, []string{
67		"-Wl,--Xlink=-Brepro", // Enable deterministic build
68	}...)
69
70	windowsX86Cflags = []string{
71		"-m32",
72	}
73
74	windowsX8664Cflags = []string{
75		"-m64",
76	}
77
78	windowsX86Ldflags = []string{
79		"-m32",
80		"-Wl,--large-address-aware",
81		"-L${WindowsGccRoot}/${WindowsGccTriple}/lib32",
82		"-static-libgcc",
83
84		"-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
85		"-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
86		"-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3/32",
87		"-B${WindowsGccRoot}/${WindowsGccTriple}/lib32",
88	}
89
90	windowsX8664Ldflags = []string{
91		"-m64",
92		"-L${WindowsGccRoot}/${WindowsGccTriple}/lib64",
93		"-Wl,--high-entropy-va",
94		"-static-libgcc",
95
96		"-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
97		"-B${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
98		"-L${WindowsGccRoot}/lib/gcc/${WindowsGccTriple}/4.8.3",
99		"-B${WindowsGccRoot}/${WindowsGccTriple}/lib64",
100	}
101
102	windowsAvailableLibraries = addPrefix([]string{
103		"gdi32",
104		"imagehlp",
105		"iphlpapi",
106		"netapi32",
107		"oleaut32",
108		"ole32",
109		"opengl32",
110		"powrprof",
111		"psapi",
112		"pthread",
113		"userenv",
114		"uuid",
115		"version",
116		"ws2_32",
117		"windowscodecs",
118	}, "-l")
119)
120
121const (
122	windowsGccVersion = "4.8"
123)
124
125func init() {
126	pctx.StaticVariable("WindowsGccVersion", windowsGccVersion)
127
128	pctx.SourcePathVariable("WindowsGccRoot",
129		"prebuilts/gcc/${HostPrebuiltTag}/host/x86_64-w64-mingw32-${WindowsGccVersion}")
130
131	pctx.StaticVariable("WindowsGccTriple", "x86_64-w64-mingw32")
132
133	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, " "))
137
138	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, " "))
146
147	pctx.StaticVariable("WindowsIncludeFlags", strings.Join(windowsIncludeFlags, " "))
148	// Yasm flags
149	pctx.StaticVariable("WindowsX86YasmFlags", "-f win32 -m x86")
150	pctx.StaticVariable("WindowsX8664YasmFlags", "-f win64 -m amd64")
151}
152
153type toolchainWindows struct {
154	cFlags, ldFlags string
155	toolchainBase
156	toolchainNoCrt
157}
158
159type toolchainWindowsX86 struct {
160	toolchain32Bit
161	toolchainWindows
162}
163
164type toolchainWindowsX8664 struct {
165	toolchain64Bit
166	toolchainWindows
167}
168
169func (t *toolchainWindowsX86) Name() string {
170	return "x86"
171}
172
173func (t *toolchainWindowsX8664) Name() string {
174	return "x86_64"
175}
176
177func (t *toolchainWindows) ToolchainCflags() string {
178	return "-B" + filepath.Join("${config.WindowsGccRoot}", "${config.WindowsGccTriple}", "bin")
179}
180
181func (t *toolchainWindows) ToolchainLdflags() string {
182	return "-B" + filepath.Join("${config.WindowsGccRoot}", "${config.WindowsGccTriple}", "bin")
183}
184
185func (t *toolchainWindows) IncludeFlags() string {
186	return "${config.WindowsIncludeFlags}"
187}
188
189func (t *toolchainWindowsX86) ClangTriple() string {
190	return "i686-windows-gnu"
191}
192
193func (t *toolchainWindowsX8664) ClangTriple() string {
194	return "x86_64-pc-windows-gnu"
195}
196
197func (t *toolchainWindowsX86) Cflags() string {
198	return "${config.WindowsCflags} ${config.WindowsX86Cflags}"
199}
200
201func (t *toolchainWindowsX8664) Cflags() string {
202	return "${config.WindowsCflags} ${config.WindowsX8664Cflags}"
203}
204
205func (t *toolchainWindowsX86) Cppflags() string {
206	return "${config.WindowsCppflags} ${config.WindowsX86Cppflags}"
207}
208
209func (t *toolchainWindowsX8664) Cppflags() string {
210	return "${config.WindowsCppflags} ${config.WindowsX8664Cppflags}"
211}
212
213func (t *toolchainWindowsX86) Ldflags() string {
214	return "${config.WindowsLdflags} ${config.WindowsX86Ldflags}"
215}
216
217func (t *toolchainWindowsX86) Lldflags() string {
218	return "${config.WindowsLldflags} ${config.WindowsX86Lldflags}"
219}
220
221func (t *toolchainWindowsX8664) Ldflags() string {
222	return "${config.WindowsLdflags} ${config.WindowsX8664Ldflags}"
223}
224
225func (t *toolchainWindowsX8664) Lldflags() string {
226	return "${config.WindowsLldflags} ${config.WindowsX8664Lldflags}"
227}
228
229func (t *toolchainWindowsX86) YasmFlags() string {
230	return "${config.WindowsX86YasmFlags}"
231}
232
233func (t *toolchainWindowsX8664) YasmFlags() string {
234	return "${config.WindowsX8664YasmFlags}"
235}
236
237func (t *toolchainWindows) ShlibSuffix() string {
238	return ".dll"
239}
240
241func (t *toolchainWindows) ExecutableSuffix() string {
242	return ".exe"
243}
244
245func (t *toolchainWindows) AvailableLibraries() []string {
246	return windowsAvailableLibraries
247}
248
249func (t *toolchainWindows) Bionic() bool {
250	return false
251}
252
253var toolchainWindowsX86Singleton Toolchain = &toolchainWindowsX86{}
254var toolchainWindowsX8664Singleton Toolchain = &toolchainWindowsX8664{}
255
256func windowsX86ToolchainFactory(arch android.Arch) Toolchain {
257	return toolchainWindowsX86Singleton
258}
259
260func windowsX8664ToolchainFactory(arch android.Arch) Toolchain {
261	return toolchainWindowsX8664Singleton
262}
263
264func init() {
265	registerToolchainFactory(android.Windows, android.X86, windowsX86ToolchainFactory)
266	registerToolchainFactory(android.Windows, android.X86_64, windowsX8664ToolchainFactory)
267}
268