-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcompiler_windows.go
287 lines (231 loc) · 7.24 KB
/
compiler_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//go:build windows
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/codecat/go-libs/log"
)
type windowsCompiler struct {
installDir string
installVersion string
sdkDir string
sdkVersion string
}
func (ci windowsCompiler) toolsDir() string {
return filepath.Join(ci.installDir, "VC\\Tools\\MSVC", ci.installVersion)
}
func (ci windowsCompiler) binDir() string {
return filepath.Join(ci.toolsDir(), "bin\\Hostx64\\x64")
}
func (ci windowsCompiler) sdkIncludeDir() string {
return filepath.Join(ci.sdkDir, "include", ci.sdkVersion+".0")
}
func (ci windowsCompiler) sdkLibDir() string {
return filepath.Join(ci.sdkDir, "lib", ci.sdkVersion+".0")
}
func (ci windowsCompiler) compiler() string {
return filepath.Join(ci.binDir(), "cl.exe")
}
func (ci windowsCompiler) linker() string {
return filepath.Join(ci.binDir(), "link.exe")
}
func (ci windowsCompiler) libber() string {
return filepath.Join(ci.binDir(), "lib.exe")
}
func (ci windowsCompiler) includeDirs() []string {
ret := make([]string, 0)
// MSVC includes
ret = append(ret, filepath.Join(ci.toolsDir(), "ATLMFC\\include"))
ret = append(ret, filepath.Join(ci.toolsDir(), "include"))
// Windows Kit includes
ret = append(ret, filepath.Join(ci.sdkIncludeDir(), "ucrt"))
ret = append(ret, filepath.Join(ci.sdkIncludeDir(), "shared"))
ret = append(ret, filepath.Join(ci.sdkIncludeDir(), "um"))
ret = append(ret, filepath.Join(ci.sdkIncludeDir(), "winrt"))
ret = append(ret, filepath.Join(ci.sdkIncludeDir(), "cppwinrt"))
return ret
}
func (ci windowsCompiler) linkDirs() []string {
ret := make([]string, 0)
// MSVC libraries
ret = append(ret, filepath.Join(ci.toolsDir(), "ATLMFC\\lib\\x64"))
ret = append(ret, filepath.Join(ci.toolsDir(), "lib\\x64"))
// Windows Kit libraries
ret = append(ret, filepath.Join(ci.sdkLibDir(), "ucrt\\x64"))
ret = append(ret, filepath.Join(ci.sdkLibDir(), "um\\x64"))
return ret
}
func (ci windowsCompiler) Compile(path, objDir string, options *CompilerOptions) error {
// cl.exe args: https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category?view=msvc-170
fileext := filepath.Ext(path)
filename := strings.TrimSuffix(filepath.Base(path), fileext)
args := make([]string, 0)
args = append(args, "/nologo") // Suppress startup banner
args = append(args, "/c") // Compile without linking
args = append(args, "/GS") // Enables buffer security checks
args = append(args, "/Qspectre") // Add instructions to mitigate Spectre variant 1 security vulnerabilities
args = append(args, "/Zc:inline") // Remove unreferenced function or data if it is COMDAT or has internal linkage only
// Warnings: command line default is /W1, Visual Studio default is /W3
if options.Strict {
args = append(args, "/W4") // Note: Using /Wall enables warnings like C4710 when using functions like printf, so we leave it at /W4
args = append(args, "/WX") // Treats all warnings as errors
} else {
args = append(args, "/W3")
}
// Set object output path
args = append(args, fmt.Sprintf("/Fo%s\\%s.obj", objDir, filename))
// Define the runtime flag
runtimeFlag := "/M"
if options.Static {
runtimeFlag += "T"
} else {
runtimeFlag += "D"
}
if options.Debug {
runtimeFlag += "d"
}
args = append(args, runtimeFlag)
// Add exception handling flags in C++
if fileext != ".c" {
if options.Exceptions == ExceptionsStandard {
args = append(args, "/EHsc")
} else if options.Exceptions == ExceptionsAll {
args = append(args, "/EHa")
} else if options.Exceptions == ExceptionsMinimal {
args = append(args, "/EH")
}
}
// Add optimization flags
if options.Optimization == OptimizeSize {
args = append(args, "/O1")
} else if options.Optimization == OptimizeSpeed {
args = append(args, "/O2")
}
// Add C++ standard flag
if fileext != ".c" {
switch options.CPPStandard {
case CPPStandardLatest:
args = append(args, "/std:c++latest")
case CPPStandard20:
args = append(args, "/std:c++20")
case CPPStandard17:
args = append(args, "/std:c++17")
case CPPStandard14:
args = append(args, "/std:c++14")
}
}
// Add C standard flag
if fileext == ".c" {
switch options.CStandard {
case CStandardLatest:
args = append(args, "/std:clatest")
case CStandard17:
args = append(args, "/std:c17")
case CStandard11:
args = append(args, "/std:c11")
}
}
// Add include directories
for _, dir := range options.IncludeDirectories {
args = append(args, "/I"+dir)
}
// Add precompiler definitions
for _, define := range options.Defines {
args = append(args, "/D"+define)
}
// Add additional compiler flags for C/C++
args = append(args, options.CompilerFlagsCXX...)
// Add additional compiler flags for C++
if fileext != ".c" {
args = append(args, options.CompilerFlagsCPP...)
}
// Add additional compiler flags for C
if fileext == ".c" {
args = append(args, options.CompilerFlagsC...)
}
args = append(args, path)
cmd := exec.Command(ci.compiler(), args...)
cmd.Env = append(os.Environ(),
"INCLUDE="+strings.Join(ci.includeDirs(), ";"),
)
if options.Verbose {
log.Trace("%s", strings.Join(cmd.Args, " "))
}
outputBytes, err := cmd.CombinedOutput()
if err != nil {
output := strings.Trim(string(outputBytes), "\r\n")
// Skip the first line from the output as it will always be the filename
lines := strings.SplitN(output, "\n", 2)
return errors.New(lines[1])
}
return nil
}
func (ci windowsCompiler) Link(objDir, outPath string, outType LinkType, options *CompilerOptions) (string, error) {
// link.exe args: https://learn.microsoft.com/en-us/cpp/build/reference/linker-options?view=msvc-170
exeName := ci.linker()
args := make([]string, 0)
args = append(args, "/nologo")
args = append(args, "/machine:x64")
args = append(args, "/incremental:no")
if options.Debug {
args = append(args, "/debug")
}
switch outType {
case LinkExe:
outPath += ".exe"
case LinkDll:
outPath += ".dll"
args = append(args, "/dll")
case LinkLib:
exeName = ci.libber()
outPath += ".lib"
args = append(args, "/lib")
}
args = append(args, "/out:"+outPath)
// Add additional library paths
for _, dir := range options.LinkDirectories {
args = append(args, "/libpath:"+dir)
}
// Add libraries to link
args = append(args, options.LinkLibraries...)
// Add additional linker flags
args = append(args, options.LinkerFlags...)
// Link to some common standard libraries
args = append(args, "kernel32.lib")
args = append(args, "user32.lib")
args = append(args, "shell32.lib")
args = append(args, "advapi32.lib")
filepath.Walk(objDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || !strings.HasSuffix(path, ".obj") {
return nil
}
args = append(args, path)
return nil
})
cmd := exec.Command(exeName, args...)
cmd.Env = append(os.Environ(),
"LIB="+strings.Join(ci.linkDirs(), ";"),
)
if options.Verbose {
log.Trace("%s", strings.Join(cmd.Args, " "))
}
outputBytes, err := cmd.CombinedOutput()
if err != nil {
output := strings.Trim(string(outputBytes), "\r\n")
return "", errors.New(output)
}
return outPath, nil
}
func (ci windowsCompiler) Clean(name string) {
os.Remove(name + ".exe")
os.Remove(name + ".dll")
os.Remove(name + ".lib")
os.Remove(name + ".pdb")
}