forked from sonyps5201314/msvcr14x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsvcr14x.ps1
More file actions
523 lines (466 loc) · 17.2 KB
/
msvcr14x.ps1
File metadata and controls
523 lines (466 loc) · 17.2 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<#
tasks:
build: build msvcr14x
install: install msvcr14x
uninstall: uninstall msvcr14x
status: query whther msvcr14x installed properly
#>
param(
$tasks = 'build,install'
)
function println ($msg) {
Write-Host "msvcr14x: $msg"
}
$conf_file_dict = @{
x86 = Join-Path $env:LOCALAPPDATA 'Microsoft\MSBuild\v4.0\Microsoft.Cpp.Win32.user.props'
x64 = Join-Path $env:LOCALAPPDATA 'Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props'
}
# by default all Microsoft.Cpp.$(arch).user.props files are binary same
$Script:msbuild_ns_uri = 'http://schemas.microsoft.com/developer/msbuild/2003'
$Script:msbuild_default_conf = @"
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="$msbuild_ns_uri">
<ImportGroup Label="PropertySheets">
</ImportGroup>
<PropertyGroup Label="UserMacros"/>
<PropertyGroup>
<IncludePath>`$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup/>
<ItemGroup/>
</Project>
"@
$cfgs = @(
# off
@{
include = '$(IncludePath)';
libs = @{x86 = '%(AdditionalLibraryDirectories)'; x64 = '%(AdditionalLibraryDirectories)' }
}
# on
@{
include = '$(msvcr14x_ROOT)\..\ntdll\include;$(IncludePath)';
libs = @{
x86 = '$(msvcr14x_ROOT)\Release;$(msvcr14x_ROOT)\Debug;%(AdditionalLibraryDirectories)'
x64 = '$(msvcr14x_ROOT)\x64\Release;$(msvcr14x_ROOT)\x64\Debug;%(AdditionalLibraryDirectories)'
}
}
)
# for install/uninstall
$install_files = @("Debug\MSVCR14XD.dll"
"Debug\MSVCP14XD.dll"
"Debug\MSVCP14X_1D.dll"
"Debug\MSVCP14X_2D.dll"
"Debug\MSVCP14XD_ATOMIC_WAIT.dll"
"Debug\MSVCP14XD_CODECVT_IDS.dll"
"Debug\CONCRT14XD.dll"
"Debug\MFC14XUD.dll"
# "Debug\MFC14XD.dll"
"Release\MSVCR14X.dll"
"Release\MSVCP14X.dll"
"Release\MSVCP14X_1.dll"
"Release\MSVCP14X_2.dll"
"Release\MSVCP14X_ATOMIC_WAIT.dll"
"Release\MSVCP14X_CODECVT_IDS.dll"
"Release\CONCRT14X.dll"
"Release\MFC14XU.dll"
# "Release\MFC14X.dll"
"x64\Debug\MSVCR14XD.dll"
"x64\Debug\MSVCP14XD.dll"
"x64\Debug\MSVCP14X_1D.dll"
"x64\Debug\MSVCP14X_2D.dll"
"x64\Debug\MSVCP14XD_ATOMIC_WAIT.dll"
"x64\Debug\MSVCP14XD_CODECVT_IDS.dll"
"x64\Debug\CONCRT14XD.dll"
"x64\Debug\MFC14XUD.dll"
# "x64\Debug\MFC14XD.dll"
"x64\Release\MSVCR14X.dll"
"x64\Release\MSVCP14X.dll"
"x64\Release\MSVCP14X_1.dll"
"x64\Release\MSVCP14X_2.dll"
"x64\Release\MSVCP14X_ATOMIC_WAIT.dll"
"x64\Release\MSVCP14X_CODECVT_IDS.dll"
"x64\Release\CONCRT14X.dll"
"x64\Release\MFC14XU.dll"
# "x64\Release\MFC14X.dll"
"Release\MFC14XCHS.DLL"
"Release\MFC14XCHT.DLL"
"Release\MFC14XDEU.DLL"
"Release\MFC14XENU.DLL"
"Release\MFC14XESN.DLL"
"Release\MFC14XFRA.DLL"
"Release\MFC14XITA.DLL"
"Release\MFC14XJPN.DLL"
"Release\MFC14XKOR.DLL"
"Release\MFC14XRUS.DLL"
"x64\Release\MFC14XCHS.DLL"
"x64\Release\MFC14XCHT.DLL"
"x64\Release\MFC14XDEU.DLL"
"x64\Release\MFC14XENU.DLL"
"x64\Release\MFC14XESN.DLL"
"x64\Release\MFC14XFRA.DLL"
"x64\Release\MFC14XITA.DLL"
"x64\Release\MFC14XJPN.DLL"
"x64\Release\MFC14XKOR.DLL"
"x64\Release\MFC14XRUS.DLL")
$system32_path = Join-Path $env:WINDIR 'System32\'
$syswow64_path = Join-Path $env:WINDIR 'SysWow64\'
$os_arch = [System.Runtime.InteropServices.RuntimeInformation, mscorlib]::OSArchitecture.ToString().ToLower()
$install_locs = @()
switch ($os_arch) {
'x64' {
$install_locs = @($syswow64_path, $system32_path)
break
}
'x86' {
$install_locs = @($system32_path, $null)
break
}
}
function Format-Xml {
[CmdletBinding()]
param(
# The Xml Document
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "Document")]
$Xml,
# The path to an xml document (on disc or any other content provider).
[Parameter(Position = 0, Mandatory = $true, ValueFromPipelineByPropertyName = $true, ParameterSetName = "File")]
[Alias("PsPath")]
[string]$Path,
# The indent level (defaults to 2 spaces)
[Parameter(Mandatory = $false)]
[int]$Indent = 2,
# The indent character (defaults to a space)
[char]$Character = ' '
)
process {
## Load from file, if necessary
if ($Path) { [xml]$xml = Get-Content $Path }
$StringWriter = New-Object System.IO.StringWriter
$XmlWriter = New-Object System.XMl.XmlTextWriter $StringWriter
$xmlWriter.Formatting = [System.Xml.Formatting]::Indented
$xmlWriter.Indentation = $Indent
$xmlWriter.IndentChar = $Character
$xml.WriteContentTo($XmlWriter)
$XmlWriter.Flush()
$StringWriter.Flush()
return $StringWriter.ToString()
}
}
function mod_msbuild_conf($arch, $enable) {
$conf_file = $conf_file_dict[$arch]
# begin mod
$incs = $cfgs[$enable].include
$libs = $cfgs[$enable].libs[$arch]
println "mod: $conf_file, incs=$incs, libs=$libs ..."
$conf_content = $null
if ((Test-Path $conf_file -PathType Leaf)) { $conf_content = Get-Content $conf_file }
if (!$conf_content) { $conf_content = $msbuild_default_conf }
[XML]$msbcDoc = $conf_content
$nsmgr = [System.Xml.XmlNamespaceManager]::new($msbcDoc.NameTable);
$nsmgr.AddNamespace('b', $msbuild_ns_uri);
$root = $msbcDoc.DocumentElement
$IncludePathNodes = $root.SelectNodes('//b:IncludePath', $nsmgr)
if ($IncludePathNodes.Count -gt 0) {
$IncludePathNodes[0].InnerText = $incs
}
else {
throw "Can't found IncludePath in $conf_file"
}
class XmlUtil {
[System.Xml.XmlDocument]$doc
[System.Xml.XmlNamespaceManager]$ns
XmlUtil($doc, $nsmgr) {
$this.doc = $doc
$this.ns = $nsmgr
}
[System.Xml.XmlElement] child($parent, $name, $force) {
$xmlNode = $null
$xmlNodes = $parent.SelectNodes("//b:$name", $this.ns)
if ($xmlNodes.Count -gt 0) { $xmlNode = $xmlNodes[0] }
elseif ($force) {
$xmlNode = $this.doc.CreateNode('element', $name, $Script:msbuild_ns_uri)
$null = $parent.AppendChild($xmlNode)
}
return $xmlNode
}
}
$util = [XmlUtil]::new($msbcDoc, $nsmgr)
$idg = $util.child($root, 'ItemDefinitionGroup', $enable)
if ($enable) {
$aldNode = $util.child($util.child($idg, 'Link', $true), 'AdditionalLibraryDirectories', $true)
$aldNode.InnerText = $libs
}
elseif ($idg) {
$children = $idg.get_ChildNodes()
foreach ($child in $children) {
$null = $idg.RemoveChild($child)
}
$idg.IsEmpty = $true
}
$strXml = Format-Xml -Xml $msbcDoc -Indent 1 -Character "`t"
Set-Content -Path $conf_file -Value $strXml
# end mod
}
function check_msvcr14x_status($arch, $print = $false) {
$conf_file = $conf_file_dict[$arch]
if (Test-Path $conf_file -PathType Leaf) {
$conf_content = [System.IO.File]::ReadAllText($conf_file)
}
else {
$conf_content = ''
}
if (!$print) { return $conf_content.Contains('msvcr14x') }
else {
if ($conf_content.Contains('msvcr14x')) {
println "$arch installed"
}
else {
println "$arch not installed"
}
}
}
function msbuild_enable_msvcr14x ($enable) {
if ((check_msvcr14x_status 'x86') -ne $enable) {
mod_msbuild_conf 'x86' $enable
}
if ((check_msvcr14x_status 'x64') -ne $enable) {
mod_msbuild_conf 'x64' $enable
}
}
function download_file($url, $out) {
if ($1k.isfile($out)) { return }
$1k.println("Downloading $url to $out ...")
Invoke-WebRequest -Uri $url -OutFile $out
}
function download_and_expand($url, $out, $dest) {
download_file $url $out
if ($out.EndsWith('.zip')) {
try {
Expand-Archive -Path $out -DestinationPath $dest
}
catch {
Remove-Item $out -Force
throw "1kiss: Expand archive $out fail, please try again"
}
}
elseif ($out.EndsWith('.tar.gz')) {
if (!$dest.EndsWith('/')) {
$1k.mkdirs($dest)
}
tar xf "$out" -C $dest
}
elseif ($out.EndsWith('.7z')) {
7z x "$out" "-o$dest" -y | Out-Host
}
elseif ($out.EndsWith('.sh')) {
chmod 'u+x' "$out"
$1k.mkdirs($dest)
}
}
$Global:vs_inst = $null
$task_schema = @{}
# boot math revision should match Microsoft STL 14.51
$boost_math_rev = 'e0fcd19f7227d81391770ea46015acc3c80af810'
$winsdk_ver = '10.0.26100.0'
$vs_ver_major = 18
$vs_ver = "$vs_ver_major.0"
$vs_ver_map = @{
'18.0' = '2026'
'17.0' = '2022'
}
# builtin
$task_schema.prerequisite = @{
action = {
#region find_vs
$vs_where = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$vs_insts = ConvertFrom-Json "$(&$vs_where -version $vs_ver -requires Microsoft.Component.MSBuild -prerelease -format 'json')"
$vs_count = $vs_insts.Count
if ($vs_insts -isnot [array]) { $vs_insts = @($vs_insts) }
println "Installed visual studio instances:"
for ($i = 0; $i -lt $vs_count; $i++) {
println " $($i + 1). $($vs_insts[$i].installationPath)"
}
$vs_display_ver = $vs_ver_map[$vs_ver]
if (-not $vs_count) { println "The Visual Studio $vs_display_ver not installed."
$result = [System.Windows.MessageBox]::Show("Please install Visual Studio $vs_display_ver edition first!", "msvcr14x", "YesNo", "Error")
if ($result -eq "No") { exit 1 }
Start-Process "https://visualstudio.microsoft.com/zh-hans/thank-you-downloading-visual-studio/?rel=$vs_ver_major"
return
}
if ($vs_count -eq 1) {
$sel_idx = 0
}
else {
println "Which Visual Studio $vs_display_ver Edition do you want to use, default is 1:"
$sel_idx = Read-Host
if (-not $sel_idx) { $sel_idx = 0 }
}
if ($sel_idx -ge $vs_count) { println "Invalid input ID."; return }
$Global:vs_inst = $vs_insts[$sel_idx]
$vs_path = $vs_inst.installationPath
#endregion find_vs
if (!([System.Management.Automation.PSTypeName]'System.Windows.MessageBox').Type) {
Add-Type -AssemblyName PresentationFramework
}
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
[System.Windows.MessageBox]::Show("Please install git for windows first!", "msvcr14x", "OK", "Error")
Start-Process "https://git-scm.com/download/win"
exit 1
}
while (-not (Test-Path -Path "$vs_path\VC\Tools\MSVC")) {
$result = [System.Windows.MessageBox]::Show("Please check 'Desktop development with C++' in Visual Studio $vs_display_ver Installer first!", "msvcr14x", "YesNo", "Error")
if ($result -eq "No") { exit 1 }
Start-Process "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installer.exe"
}
while (-not (Test-Path -Path "C:\Program Files (x86)\Windows Kits\10\Source\$winsdk_ver\ucrt")) {
$result = [System.Windows.MessageBox]::Show("Please install Windows 11 SDK ($winsdk_ver) first!", "msvcr14x", "YesNo", "Error")
if ($result -eq "No") { exit 1 }
Start-Process "https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/"
}
# auto setup boost
}
}
$task_schema.build = @{
action = {
Push-Location $PSScriptRoot
try {
git pull -v --progress "origin"
while (-not (Test-Path -Path "../ntdll")) {
git clone "https://github.com/sonyps5201314/ntdll.git" "../ntdll"
}
git -C "../ntdll" pull -v --progress "origin"
while (-not (Test-Path -Path "../YY-Thunks")) {
git clone "https://github.com/sonyps5201314/YY-Thunks.git" "../YY-Thunks"
}
git -C "../YY-Thunks" pull -v --progress "origin"
while (-not (Test-Path -Path "../boost-math")) {
git clone "https://github.com/boostorg/math.git" "../boost-math"
}
git -C "../boost-math" fetch
git -C "../boost-math" checkout $boost_math_rev
# (New-Object -ComObject "WScript.Shell").Run("../ntdll/setup.vbs")
$ntdll_setup = Join-Path $PSScriptRoot '../ntdll/setup.vbs'
&$ntdll_setup
if (!(Get-Command cl -ea SilentlyContinue)) {
$vs_path = $vs_inst.installationPath
Import-Module "$vs_path\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" -ea SilentlyContinue
Enter-VsDevShell -VsInstanceId $vs_inst.instanceId -SkipAutomaticLocation -DevCmdArguments "-arch=x86 -host_arch=x64 -no_logo"
}
msbuild "../YY-Thunks\src\YY-Thunks.UnitTest\YY-Thunks.UnitTest.vcxproj" -t:Build_YY_Thunks_List_hpp
$configuration = @("Debug", "Release")
$platform = @("x86", "x64")
foreach ($conf in $configuration) {
foreach ($plat in $platform) {
msbuild /m "msvcr14x.slnx" /t:Build /p:Configuration=$conf`;Platform=$plat
}
}
$build_success = $true
}
catch {
Write-Error $_
$build_success = $false
}
Pop-Location
if (!$build_success) { exit 2 }
}
}
$task_schema.install = @{
action = {
println "Installing msvc14x ..."
try {
# emit install instructions
$instructions = ''
foreach ($path in $install_files) {
$install_loc = $install_locs[$path.StartsWith('x64')]
if ($install_loc) {
$full_path = Join-Path $PSScriptRoot $path
if (Test-Path $full_path -PathType Leaf) {
$instructions += "Copy-Item -Path '$full_path' -Destination $install_loc -Force;`n"
}
}
else {
println "skipped install $path for current os arch: $os_arch"
}
}
if ($instructions) {
$msvcr14x_root = [Environment]::GetEnvironmentVariable('msvcr14x_ROOT', 'Machine')
if (!$msvcr14x_root -or $msvcr14x_root -ne $PSScriptRoot) {
$instructions += "[Environment]::SetEnvironmentVariable('msvcr14x_ROOT', '$PSScriptRoot', 'Machine');`n"
}
println $instructions
Start-Process powershell -ArgumentList '-Command', $instructions -WorkingDirectory $PSScriptRoot -WindowStyle Hidden -Wait -Verb runas
msbuild_enable_msvcr14x $true
println 'Install msvc14x succeed.'
}
else {
println 'Nothing to do for install, please build first'
}
}
catch {
println "Install msvcr14x fail: $_"
}
}
}
$task_schema.uninstall = @{
action = {
Write-Host "msvcr14x: Do you really want uninstall msvcr14x?(Y/n): " -NoNewline
$anwser = Read-Host
try {
if ($anwser -inotlike "n*") {
if ([Environment]::GetEnvironmentVariable('msvcr14x_ROOT', 'Machine')) {
$instructions = "[Environment]::SetEnvironmentVariable('msvcr14x_ROOT', `$null, 'Machine');`n"
}
else {
$instructions = ''
}
foreach ($path in $install_files) {
$install_loc = $install_locs[$path.StartsWith('x64')]
if (!$install_loc) {
continue
}
$file_name = Split-Path $path -Leaf
$full_path = Join-Path $install_loc $file_name
if (Test-Path $full_path -PathType Leaf) {
$instructions += "Remove-Item $full_path -Force;`n"
}
}
if ($instructions) {
# dump $instruction
println $instructions
Start-Process powershell -ArgumentList '-Command', $instructions -WorkingDirectory $PSScriptRoot -WindowStyle Hidden -Wait -Verb runas
msbuild_enable_msvcr14x $false
println 'Uninstall succeed, please rebuild all of your projects which built by msvcr14x!'
}
else {
println 'Nothing to do for uninstall due to msvcr14x not install yet'
}
}
else { throw 'cancelled' }
}
catch {
println "Uninstall cancelled by user"
}
}
}
$task_schema.status = @{
action = {
check_msvcr14x_status 'x86' $true
check_msvcr14x_status 'x64' $true
}
}
$task_schema.halt = @{
action = {
shutdown.exe -s -t 5
}
}
# execute tasks one by one
foreach ($task in "prerequisite,$tasks".Split(',')) {
if ($task_schema.Contains($task) -and !$task_schema.$task.executed) {
&$task_schema.$task.action
$task_schema.$task.executed = $true
}
else {
println "The task:$task not defined!"
}
}
cmd /c 'pause'