-
Notifications
You must be signed in to change notification settings - Fork 250
/
BuildAll.py
266 lines (239 loc) · 8.47 KB
/
BuildAll.py
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
#!/usr/bin/env python
#-*- coding: ascii -*-
# ShaderConductor
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import multiprocessing, os, platform, subprocess, sys
def LogError(message):
print("[E] %s" % message)
sys.stdout.flush()
if 0 == sys.platform.find("win"):
pauseCmd = "pause"
else:
pauseCmd = "read"
subprocess.call(pauseCmd, shell = True)
sys.exit(1)
def LogInfo(message):
print("[I] %s" % message)
sys.stdout.flush()
def LogWarning(message):
print("[W] %s" % message)
sys.stdout.flush()
def FindProgramFilesFolder():
env = os.environ
if "64bit" == platform.architecture()[0]:
if "ProgramFiles(x86)" in env:
programFilesFolder = env["ProgramFiles(x86)"]
else:
programFilesFolder = "C:\Program Files (x86)"
else:
if "ProgramFiles" in env:
programFilesFolder = env["ProgramFiles"]
else:
programFilesFolder = "C:\Program Files"
return programFilesFolder
def FindVS2017OrUpFolder(programFilesFolder, vsVersion, vsName):
tryVswhereLocation = programFilesFolder + "\\Microsoft Visual Studio\\Installer\\vswhere.exe"
if os.path.exists(tryVswhereLocation):
vsLocation = subprocess.check_output([tryVswhereLocation,
"-latest",
"-requires", "Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
"-property", "installationPath",
"-version", "[%d.0,%d.0)" % (vsVersion, vsVersion + 1),
"-prerelease"]).decode().split("\r\n")[0]
tryFolder = vsLocation + "\\VC\\Auxiliary\\Build\\"
tryVcvarsall = "VCVARSALL.BAT"
if os.path.exists(tryFolder + tryVcvarsall):
return tryFolder
else:
names = ("Preview", vsName)
skus = ("Community", "Professional", "Enterprise")
for name in names:
for sku in skus:
tryFolder = programFilesFolder + "\\Microsoft Visual Studio\\%s\\%s\\VC\\Auxiliary\\Build\\" % (name, sku)
tryVcvarsall = "VCVARSALL.BAT"
if os.path.exists(tryFolder + tryVcvarsall):
return tryFolder
LogError("Could NOT find VS%s.\n" % vsName)
return ""
def FindVS2019Folder(programFilesFolder):
return FindVS2017OrUpFolder(programFilesFolder, 16, "2019")
def FindVS2017Folder(programFilesFolder):
return FindVS2017OrUpFolder(programFilesFolder, 15, "2017")
def FindVS2015Folder(programFilesFolder):
env = os.environ
if "VS140COMNTOOLS" in env:
return env["VS140COMNTOOLS"] + "..\\..\\VC\\"
else:
tryFolder = programFilesFolder + "\\Microsoft Visual Studio 14.0\\VC\\"
tryVcvarsall = "VCVARSALL.BAT"
if os.path.exists(tryFolder + tryVcvarsall):
return tryFolder
else:
LogError("Could NOT find VS2015.\n")
class BatchCommand:
def __init__(self, hostPlatform):
self.commands = []
self.hostPlatform = hostPlatform
def AddCommand(self, cmd):
self.commands += [cmd]
def Execute(self):
batchFileName = "scBuild."
if "win" == self.hostPlatform:
batchFileName += "bat"
else:
batchFileName += "sh"
batchFile = open(batchFileName, "w")
batchFile.writelines([cmd_line + "\n" for cmd_line in self.commands])
batchFile.close()
if "win" == self.hostPlatform:
retCode = subprocess.call(batchFileName, shell = True)
else:
subprocess.call("chmod 777 " + batchFileName, shell = True)
retCode = subprocess.call("./" + batchFileName, shell = True)
os.remove(batchFileName)
return retCode
def Build(hostPlatform, hostArch, buildSys, compiler, arch, configuration, tblgenMode, tblgenPath):
originalDir = os.path.abspath(os.curdir)
if not os.path.exists("Build"):
os.mkdir("Build")
multiConfig = (buildSys.find("vs") == 0)
buildDir = "Build/%s-%s-%s-%s" % (buildSys, hostPlatform, compiler, arch)
if (not multiConfig) or (configuration == "clangformat"):
buildDir += "-%s" % configuration;
if not os.path.exists(buildDir):
os.mkdir(buildDir)
os.chdir(buildDir)
buildDir = os.path.abspath(os.curdir)
tblgenOptions = ""
if (tblgenPath != None):
tblgenOptions = " -DCLANG_TABLEGEN=\"%s\" -DLLVM_TABLEGEN=\"%s\"" % tblgenPath
parallel = multiprocessing.cpu_count()
batCmd = BatchCommand(hostPlatform)
if hostPlatform == "win":
programFilesFolder = FindProgramFilesFolder()
if (buildSys == "vs2019") or ((buildSys == "ninja") and (compiler == "vc142")):
vsFolder = FindVS2019Folder(programFilesFolder)
elif (buildSys == "vs2017") or ((buildSys == "ninja") and (compiler == "vc141")):
vsFolder = FindVS2017Folder(programFilesFolder)
elif (buildSys == "vs2015") or ((buildSys == "ninja") and (compiler == "vc140")):
vsFolder = FindVS2015Folder(programFilesFolder)
if "x64" == arch:
vcOption = "amd64"
vcArch = "x64"
elif "x86" == arch:
vcOption = "x86"
vcArch = "Win32"
elif "arm64" == arch:
vcOption = "amd64_arm64"
vcArch = "ARM64"
elif "arm" == arch:
vcOption = "amd64_arm"
vcArch = "ARM"
else:
LogError("Unsupported architecture.\n")
vcToolset = ""
if (buildSys == "vs2019") and (compiler == "vc141"):
vcOption += " -vcvars_ver=14.1"
vcToolset = "v141,"
elif ((buildSys == "vs2019") or (buildSys == "vs2017")) and (compiler == "vc140"):
vcOption += " -vcvars_ver=14.0"
vcToolset = "v140,"
batCmd.AddCommand("@call \"%sVCVARSALL.BAT\" %s" % (vsFolder, vcOption))
batCmd.AddCommand("@cd /d \"%s\"" % buildDir)
if (buildSys == "ninja"):
if hostPlatform == "win":
batCmd.AddCommand("set CC=cl.exe")
batCmd.AddCommand("set CXX=cl.exe")
if (configuration == "clangformat"):
options = "-DSC_CLANGFORMAT=\"ON\""
else:
options = "-DCMAKE_BUILD_TYPE=\"%s\" -DSC_ARCH_NAME=\"%s\" %s" % (configuration, arch, tblgenOptions)
batCmd.AddCommand("cmake -G Ninja %s ../../" % options)
if tblgenMode:
batCmd.AddCommand("ninja clang-tblgen -j%d" % parallel)
batCmd.AddCommand("ninja llvm-tblgen -j%d" % parallel)
else:
batCmd.AddCommand("ninja -j%d" % parallel)
else:
if buildSys == "vs2019":
generator = "\"Visual Studio 16\""
elif buildSys == "vs2017":
generator = "\"Visual Studio 15\""
elif buildSys == "vs2015":
generator = "\"Visual Studio 14\""
if (configuration == "clangformat"):
cmake_options = "-DSC_CLANGFORMAT=\"ON\""
msbuild_options = ""
else:
cmake_options = "-T %shost=x64 -A %s %s" % (vcToolset, vcArch, tblgenOptions)
msbuild_options = "/m:%d /v:m /p:Configuration=%s,Platform=%s" % (parallel, configuration, vcArch)
batCmd.AddCommand("cmake -G %s %s ../../" % (generator, cmake_options))
if tblgenMode:
batCmd.AddCommand("MSBuild External\\DirectXShaderCompiler\\tools\\clang\\utils\\TableGen\\clang-tblgen.vcxproj /nologo %s" % msbuild_options)
batCmd.AddCommand("MSBuild External\\DirectXShaderCompiler\\utils\\TableGen\\llvm-tblgen.vcxproj /nologo %s" % msbuild_options)
else:
batCmd.AddCommand("MSBuild ALL_BUILD.vcxproj /nologo %s" % msbuild_options)
if batCmd.Execute() != 0:
LogError("Build failed.\n")
os.chdir(originalDir)
tblGenPath = buildDir + "/External/DirectXShaderCompiler"
if multiConfig:
tblGenPath += "/" + configuration
tblGenPath += "/bin/"
clangTblgenPath = tblGenPath + "clang-tblgen"
llvmTblGenPath = tblGenPath + "llvm-tblgen"
if (hostPlatform == "win"):
clangTblgenPath += ".exe"
llvmTblGenPath += ".exe"
return (clangTblgenPath, llvmTblGenPath)
if __name__ == "__main__":
hostPlatform = sys.platform
if 0 == hostPlatform.find("win"):
hostPlatform = "win"
elif 0 == hostPlatform.find("linux"):
hostPlatform = "linux"
elif 0 == hostPlatform.find("darwin"):
hostPlatform = "osx"
hostArch = platform.machine()
if (hostArch == "AMD64") or (hostArch == "x86_64"):
hostArch = "x64"
elif (hostArch == "i386"):
hostArch = "x86"
elif (hostArch == "ARM64"):
hostArch = "arm64"
else:
LogError("Unknown host architecture %s.\n" % hostArch)
argc = len(sys.argv);
if (argc > 1):
buildSys = sys.argv[1]
else:
if hostPlatform == "win":
buildSys = "vs2019"
else:
buildSys = "ninja"
if (argc > 2):
compiler = sys.argv[2]
else:
if buildSys == "vs2019":
compiler = "vc142"
elif buildSys == "vs2017":
compiler = "vc141"
elif buildSys == "vs2015":
compiler = "vc140"
else:
compiler = "gcc"
if (argc > 3):
arch = sys.argv[3]
else:
arch = "x64"
if (argc > 4):
configuration = sys.argv[4]
else:
configuration = "Release"
tblgenPath = None
if (configuration != "clangformat") and (hostArch != arch) and (not ((hostArch == "x64") and (arch == "x86"))):
# Cross compiling:
# Generate a project with host architecture, build clang-tblgen and llvm-tblgen, and keep the path of clang-tblgen and llvm-tblgen
tblgenPath = Build(hostPlatform, hostArch, buildSys, compiler, hostArch, configuration, True, None)
Build(hostPlatform, hostArch, buildSys, compiler, arch, configuration, False, tblgenPath)