-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-wrappers.ps1
More file actions
100 lines (82 loc) · 6.61 KB
/
Copy pathsetup-wrappers.ps1
File metadata and controls
100 lines (82 loc) · 6.61 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
# setup-wrappers.ps1
function Assert-Output {
param([Parameter(Mandatory = $true)] [string]$Name, [Parameter(Mandatory = $true)] [string]$Actual, [Parameter(Mandatory = $true)] [string]$Expected)
$A = $Actual.Trim(); $E = $Expected.Trim()
if ($A -ne $E) { throw "$Name test failed. Expected: [$E], Actual: [$A]" }
Write-Host "$Name test passed: $A" -ForegroundColor Green
}
function Create-CommandWrappers {
Write-Section 'Create command wrappers'
New-Item -ItemType Directory -Force -Path $ToolBin, $PathBin | Out-Null
Remove-Item -Path (Join-Path $PathBin '*') -Recurse -Force -ErrorAction SilentlyContinue
$MsysUsrBin = Split-Path $MsysBash -Parent
$MsysToolPathLine = "set `"PATH=$UcrtBin;$MsysUsrBin;%PATH%`""
# VS Code can compile directly from PATH once UCRT64 and usr\bin are exposed.
# These wrappers are kept only for contest aliases and for pinning python3 to managed 3.10.
Write-LinesUtf8NoBom (Join-Path $ToolBin 'g++14.cmd') @('@echo off', $MsysToolPathLine, "`"$UcrtBin\g++.exe`" -std=gnu++14 %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'g++17.cmd') @('@echo off', $MsysToolPathLine, "`"$UcrtBin\g++.exe`" -std=gnu++17 %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'g++20.cmd') @('@echo off', $MsysToolPathLine, "`"$UcrtBin\g++.exe`" -std=gnu++20 %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'g++.cmd') @('@echo off', $MsysToolPathLine, "`"$UcrtBin\g++.exe`" %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'gcc.cmd') @('@echo off', $MsysToolPathLine, "`"$UcrtBin\gcc.exe`" %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'gdb.cmd') @('@echo off', $MsysToolPathLine, "`"$UcrtBin\gdb.exe`" %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'python.cmd') @('@echo off', "`"$PythonExe`" %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'python3.cmd') @('@echo off', "`"$PythonExe`" %*")
Write-LinesUtf8NoBom (Join-Path $ToolBin 'cat.cmd') @('@echo off', "`"$MsysCat`" %*")
Write-Host 'Wrappers created.' -ForegroundColor Green
}
function Configure-Path {
Write-Section 'Configure PATH'
Remove-ConflictingPathEntries
Add-UserPathsFront @($PythonDir, $ToolBin, $UcrtBin, (Split-Path $MsysBash -Parent))
Write-Host "Exposed commands: python 3.10, python3, gcc, g++, gdb, cat, g++14, g++17, g++20" -ForegroundColor Green
}
function Write-VersionReport {
Write-Section 'Version report'
New-Item -ItemType Directory -Force -Path $TestDir | Out-Null
$VersionReport = @('Contest commands', '----------------', 'C++ : g++', 'C : gcc', 'Debugger : gdb', 'Text : cat', '', 'Managed Python interpreter:', $PythonExe, '', 'g++ version:')
$VersionReport += [string]((Invoke-NativeChecked -FilePath (Join-Path $UcrtBin 'g++.exe') -ArgumentList @('--version') -Quiet).Output | Select-Object -First 1)
$VersionReport += ''
$VersionReport += 'gcc version:'
$VersionReport += [string]((Invoke-NativeChecked -FilePath (Join-Path $UcrtBin 'gcc.exe') -ArgumentList @('--version') -Quiet).Output | Select-Object -First 1)
$VersionReport += ''
$VersionReport += 'gdb version:'
$VersionReport += [string]((Invoke-NativeChecked -FilePath (Join-Path $UcrtBin 'gdb.exe') -ArgumentList @('--version') -Quiet).Output | Select-Object -First 1)
$VersionReport += ''
$VersionReport += 'python version:'
$VersionReport += [string]((Invoke-NativeChecked -FilePath (Join-Path $ToolBin 'python.cmd') -ArgumentList @('--version') -Quiet).Output | Select-Object -First 1)
$VersionReport += ''
$VersionReport += 'cat version:'
$VersionReport += [string]((Invoke-NativeChecked -FilePath $MsysCat -ArgumentList @('--version') -Quiet).Output | Select-Object -First 1)
Write-LinesUtf8NoBom -Path (Join-Path $TestDir 'version-report.txt') -Lines ([string[]]$VersionReport)
$VersionReport | ForEach-Object { Write-Host $_ }
}
function Invoke-WithMsysRuntimeChecked {
param([Parameter(Mandatory = $true)] [string]$FilePath, [string[]]$ArgumentList = @(), [int[]]$SuccessExitCodes = @(0), [switch]$Quiet)
$OldPath = $env:Path
try {
$env:Path = "$UcrtBin;$(Split-Path $MsysBash -Parent);$OldPath"
return Invoke-NativeChecked -FilePath $FilePath -ArgumentList $ArgumentList -SuccessExitCodes $SuccessExitCodes -Quiet:$Quiet
} finally { $env:Path = $OldPath }
}
function Run-SmokeTests {
Write-Section 'Compile and run tests'
New-Item -ItemType Directory -Force -Path $TestDir | Out-Null
Write-LinesUtf8NoBom (Join-Path $TestDir 'cpp14.cpp') @('#include <iostream>', 'int main() { auto f = [](auto x) { return x + 14; }; std::cout << "CPP14 OK " << f(0) << "\n"; return 0; }')
Invoke-NativeChecked -FilePath (Join-Path $ToolBin 'g++14.cmd') -ArgumentList @((Join-Path $TestDir 'cpp14.cpp'), '-O2', '-o', (Join-Path $TestDir 'cpp14.exe')) -Quiet | Out-Null
Assert-Output -Name 'C++14' -Actual (((Invoke-WithMsysRuntimeChecked -FilePath (Join-Path $TestDir 'cpp14.exe') -Quiet).Output) -join "`n") -Expected 'CPP14 OK 14'
Write-LinesUtf8NoBom (Join-Path $TestDir 'cpp20.cpp') @('#include <concepts>', '#include <iostream>', 'template <std::integral T> T twice(T x) { return x * 2; }', 'int main() { std::cout << "CPP20 OK " << twice(10) << "\n"; return 0; }')
Invoke-NativeChecked -FilePath (Join-Path $ToolBin 'g++20.cmd') -ArgumentList @((Join-Path $TestDir 'cpp20.cpp'), '-O2', '-o', (Join-Path $TestDir 'cpp20.exe')) -Quiet | Out-Null
Assert-Output -Name 'C++20' -Actual (((Invoke-WithMsysRuntimeChecked -FilePath (Join-Path $TestDir 'cpp20.exe') -Quiet).Output) -join "`n") -Expected 'CPP20 OK 20'
Write-LinesUtf8NoBom (Join-Path $TestDir 'python3_test.py') @('print("PYTHON3 OK 6")')
Assert-Output -Name 'Python3' -Actual (((Invoke-NativeChecked -FilePath (Join-Path $ToolBin 'python3.cmd') -ArgumentList @((Join-Path $TestDir 'python3_test.py')) -Quiet).Output) -join "`n") -Expected 'PYTHON3 OK 6'
}
$PA = "[$Global:SetupStepCurrent/$Global:SetupStepTotal] Finalizing Setup"
Write-Progress -Id $Global:ProgressIdInner -ParentId $Global:ProgressIdOuter -Activity $PA -Status "Creating Wrappers..." -PercentComplete 10
Create-CommandWrappers
Write-Progress -Id $Global:ProgressIdInner -ParentId $Global:ProgressIdOuter -Activity $PA -Status "Configuring PATH..." -PercentComplete 40
Configure-Path
Write-Progress -Id $Global:ProgressIdInner -ParentId $Global:ProgressIdOuter -Activity $PA -Status "Generating Version Report..." -PercentComplete 70
Write-VersionReport
Write-Progress -Id $Global:ProgressIdInner -ParentId $Global:ProgressIdOuter -Activity $PA -Status "Running Smoke Tests..." -PercentComplete 90
Run-SmokeTests
Write-Progress -Id $Global:ProgressIdInner -ParentId $Global:ProgressIdOuter -Activity $PA -Completed