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