Repository navigation
Expand file tree
/
Copy pathmake.ps1
More file actions
222 lines (211 loc) · 9.69 KB
/
Copy pathmake.ps1
File metadata and controls
222 lines (211 loc) · 9.69 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
# `make` for Windows without make: reads the Makefile beside this script and runs its targets,
# each recipe line in Git's bash, so there is one copy of every recipe and it is the Makefile's.
#
# .\make [-n] [-s] [-f FILE] [TARGET...] [VAR=value...]
#
# Interprets the subset of make this Makefile uses: targets with prerequisites (phony ones run;
# file targets are built when the file is missing), `:=` and `?=` variables with `$(VAR)`,
# `$(CURDIR)`, `$(HOME)` and `$(shell ...)`, backslash continuations, `@` for a silent line, `-`
# for one whose failure is ignored, `$$` for a shell `$`, and `VAR=value` on the command line
# (exported to the recipes as well, as make does). Nothing else — no patterns, conditionals or
# includes — and it says so if it meets one. Needs bash: Git for Windows's (found on its own),
# or whatever `MAKE_SHELL` names. `-n` prints the commands instead of running them.
[CmdletBinding(PositionalBinding = $false)]
param(
[Alias("n")][switch]$DryRun,
[Alias("s")][switch]$Silent,
[Alias("f")][string]$Makefile = "",
[Parameter(Position = 0, ValueFromRemainingArguments = $true)][string[]]$Goals
)
$ErrorActionPreference = "Stop"
# make's own voice for an error: the line on stderr and the exit code, nothing else.
function Fail([string]$message, [int]$code = 2) {
[Console]::Error.WriteLine($message)
exit $code
}
$here = $PSScriptRoot
if (-not $Makefile) { $Makefile = Join-Path $here "Makefile" }
if (-not (Test-Path $Makefile)) { Fail "make: ${Makefile}: No such file" 2 }
function Find-Bash {
if ($env:MAKE_SHELL) { return $env:MAKE_SHELL }
$candidates = @(
"$env:ProgramFiles\Git\bin\bash.exe",
"${env:ProgramFiles(x86)}\Git\bin\bash.exe",
"$env:LOCALAPPDATA\Programs\Git\bin\bash.exe"
)
foreach ($c in $candidates) { if (Test-Path $c) { return $c } }
# Not the WSL launcher in System32, which runs a Linux, not this checkout.
$onPath = Get-Command bash -All -ErrorAction SilentlyContinue | Where-Object { $_.Source -notmatch 'System32' } | Select-Object -First 1
if ($onPath) { return $onPath.Source }
Fail "make: no bash: install Git for Windows, or set MAKE_SHELL to a bash"
}
$bash = Find-Bash
# A path as bash and cargo both take it: forward slashes.
$curdir = ($here -replace '\\', '/')
$home_ = if ($env:HOME) { $env:HOME } else { $env:USERPROFILE }
# Variables and targets are case-sensitive in make; PowerShell's hashtables are not by default.
function Ordinal-Table { New-Object System.Collections.Hashtable ([StringComparer]::Ordinal) }
$vars = Ordinal-Table
# Command-line assignments first: they beat `?=` and `:=` alike, and reach the recipes.
$targets = @()
$commandLine = @()
foreach ($goal in $Goals) {
if ($goal -match '^([A-Za-z_][A-Za-z0-9_]*)=(.*)$') {
$vars[$Matches[1]] = $Matches[2]
$commandLine += $Matches[1]
Set-Item -Path "env:$($Matches[1])" -Value $Matches[2]
} else { $targets += $goal }
}
if (-not $vars.ContainsKey("CURDIR")) { $vars["CURDIR"] = $curdir }
if (-not $vars.ContainsKey("HOME")) { $vars["HOME"] = ($home_ -replace '\\', '/') }
# Run `script` in bash, in the Makefile's directory. Its output flows on (a PowerShell function
# returns everything it emits, so the status comes back in `$LastStatus` instead); with
# `-Capture`, its stdout is returned, for `$(shell ...)`. The text goes through a file, never an
# argument list, which PowerShell would re-quote. Windows PowerShell treats a native command's
# stderr as an error under `Stop`, so that is relaxed for the call.
$script:LastStatus = 0
function Run-Shell([string]$script, [switch]$Capture) {
$file = [IO.Path]::GetTempFileName()
[IO.File]::WriteAllText($file, "cd `"$curdir`" || exit 1`n" + $script + "`n", (New-Object Text.UTF8Encoding($false)))
$was = $ErrorActionPreference
$ErrorActionPreference = "Continue"
try {
if ($Capture) {
$out = & $bash $file 2>$null
$script:LastStatus = $LASTEXITCODE
return ($out -join "`n")
}
& $bash $file
$script:LastStatus = $LASTEXITCODE
} finally {
$ErrorActionPreference = $was
Remove-Item $file -ErrorAction SilentlyContinue
}
}
# `$(VAR)`, `$(shell ...)` and `$$` in `text`, left to right, parentheses balanced.
function Expand([string]$text) {
$out = New-Object Text.StringBuilder
$i = 0
while ($i -lt $text.Length) {
$c = $text[$i]
if ($c -ne '$') { [void]$out.Append($c); $i++; continue }
if ($i + 1 -ge $text.Length) { [void]$out.Append('$'); break }
$next = $text[$i + 1]
if ($next -eq '$') { [void]$out.Append('$'); $i += 2; continue }
if ($next -ne '(') {
# `$@`, `$<` and friends: not used here; kept as the shell would see them.
[void]$out.Append('$'); $i++; continue
}
$depth = 0; $j = $i + 1
do {
if ($text[$j] -eq '(') { $depth++ } elseif ($text[$j] -eq ')') { $depth-- }
$j++
} while ($depth -gt 0 -and $j -lt $text.Length)
if ($depth -ne 0) { Fail "make: unterminated variable reference in: $text" }
$inner = $text.Substring($i + 2, $j - $i - 3)
if ($inner -match '^shell\s+(.*)$') {
[void]$out.Append((Run-Shell (Expand $Matches[1]) -Capture).Trim())
} elseif ($inner -match '^[A-Za-z_][A-Za-z0-9_]*$') {
$value = if ($vars.ContainsKey($inner)) { $vars[$inner] }
elseif (Test-Path "env:$inner") { (Get-Item "env:$inner").Value }
else { "" }
[void]$out.Append($value)
} else {
Fail "make: ${inner}: a make function this does not know (see make.ps1)" 2
}
$i = $j
}
$out.ToString()
}
# --- read the Makefile: continuations joined, variables set, targets collected
$lines = [IO.File]::ReadAllLines($Makefile)
$logical = @() # (text, isRecipe)
$i = 0
while ($i -lt $lines.Length) {
$line = $lines[$i]; $i++
$isRecipe = $line.StartsWith("`t")
while ($line.EndsWith('\') -and $i -lt $lines.Length) {
$more = $lines[$i]; $i++
if ($isRecipe) {
# As make hands it to the shell: the backslash-newline stays (the shell joins),
# one leading tab goes.
if ($more.StartsWith("`t")) { $more = $more.Substring(1) }
$line = $line.Substring(0, $line.Length - 1) + "\`n" + $more
} else {
$line = $line.Substring(0, $line.Length - 1).TrimEnd() + " " + $more.Trim()
}
}
$logical += , @($line, $isRecipe)
}
$rules = New-Object System.Collections.Specialized.OrderedDictionary ([StringComparer]::Ordinal) # name -> @{ Prereqs; Recipe }
$phony = Ordinal-Table
$current = $null
foreach ($entry in $logical) {
$text, $isRecipe = $entry
if ($isRecipe) {
if ($null -eq $current) { Fail "make: recipe commences before first target" 2 }
$rules[$current].Recipe += $text.Substring(1)
continue
}
$t = $text.Trim()
if ($t -eq "" -or $t.StartsWith("#")) { continue }
if ($t -match '^(ifeq|ifneq|ifdef|ifndef|include|-include|define|export|unexport)\b') {
Fail "make: line `"$t`": a make construct this does not know (see make.ps1)" 2
}
if ($t -match '^([A-Za-z_][A-Za-z0-9_]*)\s*(:=|\?=|=)\s*(.*)$') {
$name, $op, $value = $Matches[1], $Matches[2], $Matches[3]
if ($commandLine -contains $name) { continue }
if ($op -eq '?=' -and ($vars.ContainsKey($name) -or (Test-Path "env:$name"))) { continue }
$vars[$name] = Expand $value
continue
}
if ($t -match '^([^:=]+):(.*)$') {
if ($Matches[1] -match '%') { Fail "make: line `"$t`": a pattern rule, which this does not know (see make.ps1)" }
$names = (Expand $Matches[1].Trim()) -split '\s+' | Where-Object { $_ }
$prereqs = @((Expand $Matches[2].Trim()) -split '\s+' | Where-Object { $_ })
foreach ($name in $names) {
if ($name -eq ".PHONY") { foreach ($p in $prereqs) { $phony[$p] = $true }; continue }
if (-not $rules.Contains($name)) { $rules[$name] = @{ Prereqs = @(); Recipe = @() } }
$rules[$name].Prereqs += $prereqs
$current = $name
}
continue
}
Fail "make: line `"$t`": not understood (see make.ps1)" 2
}
# --- run
$done = Ordinal-Table
function Build([string]$name, [int]$depth) {
if ($done.ContainsKey($name)) { return }
if (-not $rules.Contains($name)) {
if (Test-Path (Join-Path $here $name)) { $done[$name] = $true; return }
Fail "make: *** No rule to make target '$name'. Stop." 2
}
$rule = $rules[$name]
foreach ($p in $rule.Prereqs) { Build $p ($depth + 1) }
$done[$name] = $true
# A file target that exists is up to date; a phony one, or a missing file, is made.
if (-not $phony.ContainsKey($name) -and (Test-Path (Join-Path $here $name))) { return }
if ($rule.Recipe.Count -eq 0 -and $depth -eq 0 -and $rule.Prereqs.Count -eq 0) {
Write-Host "make: Nothing to be done for '$name'."
}
foreach ($raw in $rule.Recipe) {
$cmd = Expand $raw
$quiet = $Silent; $ignore = $false
while ($cmd.Length -gt 0 -and ($cmd[0] -eq '@' -or $cmd[0] -eq '-')) {
if ($cmd[0] -eq '@') { $quiet = $true } else { $ignore = $true }
$cmd = $cmd.Substring(1)
}
if ($cmd.Trim() -eq "") { continue }
if (-not $quiet -or $DryRun) { Write-Host $cmd }
if ($DryRun) { continue }
Run-Shell $cmd
$status = $script:LastStatus
if ($status -ne 0 -and -not $ignore) {
[Console]::Error.WriteLine("make: *** [$name] Error $status")
exit $status
}
}
}
if ($targets.Count -eq 0) { $targets = @(@($rules.Keys)[0]) }
foreach ($target in $targets) { Build $target 0 }