-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinstall.ps1
More file actions
179 lines (147 loc) · 4.9 KB
/
Copy pathinstall.ps1
File metadata and controls
179 lines (147 loc) · 4.9 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
# Parameters
$Root = Split-Path -Parent $MyInvocation.MyCommand.Path
$BUILD_TYPE = if ($env:BUILD_TYPE) { $env:BUILD_TYPE } else { "Release" }
$BUILD = if ($env:BUILD) { $env:BUILD } else { "build" }
$PARALLEL = if ($env:PARALLEL) { $env:PARALLEL } else { 8 }
$ConfirmSettings = $false
$PromptOnStaleCache = $true
function Write-Log {
param([string]$Message)
Write-Host "[install] $Message"
}
function Test-InteractiveHost {
return $Host.Name -notmatch 'ServerRemoteHost|Visual Studio Code Host'
}
function Confirm-Choice {
param(
[string]$Prompt,
[string]$Default = "Y"
)
while ($true) {
$reply = Read-Host "$Prompt [$Default]"
if ([string]::IsNullOrWhiteSpace($reply)) {
$reply = $Default
}
switch ($reply.ToLowerInvariant()) {
"y" { return $true }
"yes" { return $true }
"n" { return $false }
"no" { return $false }
}
Write-Host "Please answer yes or no."
}
}
function Get-CacheSourceDirectory {
param([string]$CacheFile)
if (-not (Test-Path $CacheFile)) {
return $null
}
$line = Select-String -Path $CacheFile -Pattern '^CMAKE_HOME_DIRECTORY:INTERNAL=' | Select-Object -Last 1
if ($null -eq $line) {
return $null
}
return $line.Line -replace '^CMAKE_HOME_DIRECTORY:INTERNAL=', ''
}
function Ensure-FreshBuildDirectory {
param([string]$BuildDir)
$cacheFile = Join-Path $BuildDir "CMakeCache.txt"
$cacheSource = Get-CacheSourceDirectory -CacheFile $cacheFile
if ([string]::IsNullOrWhiteSpace($cacheSource) -or $cacheSource -eq $Root) {
return
}
Write-Log "Detected a build cache from a different source directory."
Write-Log "Current repo: $Root"
Write-Log "Cached source: $cacheSource"
if ($PromptOnStaleCache -and (Test-InteractiveHost)) {
if (-not (Confirm-Choice "Reset the build directory and reconfigure?" "Y")) {
throw "Installation cancelled."
}
}
if (-not (Test-Path $BuildDir)) {
return
}
Write-Log "Resetting stale CMake state in: $BuildDir"
Write-Log "Keeping reusable downloaded dependencies under $BuildDir\_deps when present"
$pathsToRemove = @(
(Join-Path $BuildDir "CMakeCache.txt"),
(Join-Path $BuildDir "CTestTestfile.cmake"),
(Join-Path $BuildDir "Makefile"),
(Join-Path $BuildDir "cmake_install.cmake"),
(Join-Path $BuildDir "compile_commands.json"),
(Join-Path $BuildDir "build.ninja"),
(Join-Path $BuildDir ".ninja_deps"),
(Join-Path $BuildDir ".ninja_log"),
(Join-Path $BuildDir "CMakeFiles")
)
foreach ($path in $pathsToRemove) {
if (Test-Path $path) {
Remove-Item -Recurse -Force $path
}
}
}
function Get-BuiltExecutable {
param([string]$Name)
$exeName = "$Name.exe"
$candidates = @(
(Join-Path $BUILD $exeName),
(Join-Path (Join-Path $BUILD $BUILD_TYPE) $exeName)
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) {
return $candidate
}
}
throw "Unable to find built executable '$exeName' in '$BUILD' or '$BUILD\$BUILD_TYPE'."
}
for ($i = 0; $i -lt $args.Length; $i++) {
switch ($args[$i]) {
"--build-dir" {
$i++
$BUILD = $args[$i]
}
"--build-type" {
$i++
$BUILD_TYPE = $args[$i]
}
"--parallel" {
$i++
$PARALLEL = $args[$i]
}
"--interactive" {
$ConfirmSettings = $true
}
"--no-prompt" {
$ConfirmSettings = $false
$PromptOnStaleCache = $false
}
"--help" {
Write-Host "Usage: .\install.ps1 [--build-dir DIR] [--build-type TYPE] [--parallel N] [--interactive] [--no-prompt]"
exit 0
}
default {
throw "Unknown argument: $($args[$i])"
}
}
}
Set-Location $Root
if ($ConfirmSettings -and (Test-InteractiveHost)) {
Write-Log "Planned install settings:"
Write-Log " Build directory: $BUILD"
Write-Log " Build type: $BUILD_TYPE"
Write-Log " Parallel jobs: $PARALLEL"
if (-not (Confirm-Choice "Continue with these settings?" "Y")) {
throw "Installation cancelled."
}
}
if (-not ($PARALLEL -as [int]) -or [int]$PARALLEL -le 0) {
throw "Parallel build jobs must be a positive integer."
}
Ensure-FreshBuildDirectory -BuildDir $BUILD
Write-Log "Configuring CMake in `"$BUILD`""
cmake -S $Root -B $BUILD -DCMAKE_BUILD_TYPE=$BUILD_TYPE
Write-Log "Building targets with $PARALLEL parallel jobs"
cmake --build $BUILD --parallel $PARALLEL --config $BUILD_TYPE
Write-Log "Copying executables to the repository root"
Copy-Item (Get-BuiltExecutable "cryfa") -Destination $Root -Force
Copy-Item (Get-BuiltExecutable "keygen") -Destination $Root -Force
Write-Log "Install complete"