-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.ps1
303 lines (241 loc) · 9 KB
/
build.ps1
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
# Heavily trimmed down from pico-setup-windows build.ps1
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
Position = 0,
HelpMessage = "Path to a JSON installer configuration file.")]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string]
$ConfigFile,
[Parameter(HelpMessage = "SDK Version to build")]
[ValidateNotNullOrEmpty()]
[string]
$Version,
[Parameter(HelpMessage = "Path to MSYS2 installation. MSYS2 will be downloaded and installed to this path if it doesn't exist.")]
[ValidatePattern('[\\\/]msys64$')]
[string]
$MSYS2Path = '.\build\msys64',
[switch]
$SkipDownload,
[switch]
$SkipSigning,
[ValidateSet('zlib', 'bzip2', 'lzma')]
[string]
$Compression = 'lzma',
[ValidateSet('system', 'user')]
[string]
$BuildType = 'system'
)
#Requires -Version 7.2
function crawl {
param ([string]$url)
(Invoke-WebRequest $url -UseBasicParsing).Links |
Where-Object {
($_ | Get-Member href) -and
[uri]::IsWellFormedUriString($_.href, [System.UriKind]::RelativeOrAbsolute)
} |
ForEach-Object {
$href = [System.Net.WebUtility]::HtmlDecode($_.href)
try {
(New-Object System.Uri([uri]$url, $href)).AbsoluteUri
}
catch {
$href
}
}
}
function mkdirp {
param ([string] $dir, [switch] $clean)
New-Item -Path $dir -Type Directory -Force | Out-Null
if ($clean) {
Remove-Item -Path "$dir\*" -Recurse -Force
}
}
function exec {
param ([scriptblock]$private:cmd)
$global:LASTEXITCODE = 0
& $cmd
if ($LASTEXITCODE -ne 0) {
throw "Command '$cmd' exited with code $LASTEXITCODE"
}
}
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
Write-Host "Building from $ConfigFile"
$suffix = [io.path]::GetFileNameWithoutExtension($ConfigFile) + ($BuildType -eq 'user' ? '-user' : '' )
$tools = (Get-Content '.\config\tools.json' | ConvertFrom-Json).tools
$repositories = (Get-Content '.\config\repositories.json' | ConvertFrom-Json).repositories
if ("" -ne $Version) {
Write-Host "Version set to $Version"
$repositories[0].tree = $Version
} else {
$version = (Get-Content "$PSScriptRoot\version.txt").Trim()
}
$config = Get-Content $ConfigFile | ConvertFrom-Json
$bitness = $config.bitness
$mingw_arch = $config.mingwArch
$downloads = $config.downloads
mkdirp "build"
mkdirp "bin"
($downloads + $tools) | ForEach-Object {
$_ | Add-Member -NotePropertyName 'shortName' -NotePropertyValue ($_.name -replace '[^a-zA-Z0-9]', '')
$outfile = "downloads/$($_.file)"
if ($SkipDownload) {
Write-Host "Checking $($_.name): " -NoNewline
if (-not (Test-Path $outfile)) {
Write-Error "$outfile not found"
}
}
else {
Write-Host "Downloading $($_.name): " -NoNewline
exec { curl.exe --fail --silent --show-error --url "$($_.href)" --location --output "$outfile" --create-dirs --remote-time --time-cond "downloads/$($_.file)" }
}
# Display versions of packaged installers, for information only. We try to
# extract it from:
# 1. The file name
# 2. The download URL
# 3. The version metadata in the file
#
# This fails for MSYS2, because there is no version number (only a timestamp)
# and the version that gets reported is 7-zip SFX version.
$fileVersion = ''
$versionRegEx = '([0-9]+\.)+[0-9]+'
if ($_.file -match $versionRegEx -or $_.href -match $versionRegEx) {
$fileVersion = $Matches[0]
} else {
$fileVersion = (Get-ChildItem $outfile).VersionInfo.ProductVersion
}
if ($fileVersion) {
Write-Host $fileVersion
} else {
Write-Host $_.file
}
if ($_ | Get-Member dirName) {
$strip = 0;
if ($_ | Get-Member extractStrip) { $strip = $_.extractStrip }
mkdirp "build\$($_.dirName)" -clean
exec { tar -xf $outfile -C "build\$($_.dirName)" --strip-components $strip }
}
}
if (-not (Get-Command cmake -ErrorAction SilentlyContinue)) {
$env:PATH = $env:PATH + ';' + (Resolve-Path .\build\cmake\bin).Path
}
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
$env:PATH = $env:PATH + ';' + (Resolve-Path .\build\git\cmd).Path
}
$repositories | ForEach-Object {
$repodir = Join-Path 'build' ([IO.Path]::GetFileNameWithoutExtension($_.href))
$repodir = $repodir.TrimEnd("-rp2350")
if ($_ | Get-Member pi_only) {
Write-Host "Skipping Pi only ${repodir}"
}
elseif ($SkipDownload) {
Write-Host "Checking ${repodir}: " -NoNewline
if (-not (Test-Path $repodir)) {
Write-Error "$repodir not found"
}
exec { git -C "$repodir" describe --all }
}
else {
if (Test-Path $repodir) {
Remove-Item $repodir -Recurse -Force
}
exec { git clone -b "$($_.tree)" --depth=1 -c advice.detachedHead=false "$($_.href)" "$repodir" }
if ($_ | Get-Member submodules) {
exec { git -C "$repodir" submodule update --init --depth=1 }
}
}
}
$sdkVersion = (cmake -P .\packages\windows\pico-setup-windows\pico-sdk-version.cmake -N | Select-String -Pattern 'PICO_SDK_VERSION_STRING=(.*)$').Matches.Groups[1].Value
if (-not ($sdkVersion -match $versionRegEx)) {
Write-Error 'Could not determine Pico SDK version.'
}
if (-not (Test-Path $MSYS2Path)) {
Write-Host 'Extracting MSYS2'
exec { & .\downloads\msys2.exe -y "-o$(Resolve-Path (Split-Path $MSYS2Path -Parent))" }
}
function sign {
param ([string[]] $filesToSign)
if ($SkipSigning) {
Write-Warning "Skipping code signing."
} else {
$cert = Get-ChildItem -Path Cert:\CurrentUser\My -CodeSigningCert | Where-Object { $_.Subject -like "CN=Raspberry Pi*" }
if (-not $cert) {
Write-Error "No suitable code signing certificates found."
}
$filesToSign | Set-AuthenticodeSignature -Certificate $cert -TimestampServer "http://timestamp.digicert.com" -HashAlgorithm SHA256 | Tee-Object -Variable signatures
$signatures | ForEach-Object {
if ($_.Status -ne 0) {
Write-Error "Error signing $($_.Path)"
}
}
}
}
function msys {
param ([string] $cmd)
exec { & "$MSYS2Path\usr\bin\bash" -leo pipefail -c "$cmd" }
}
# Preserve the current working directory
$env:CHERE_INVOKING = 'yes'
# Start MINGW32/64 environment
$env:MSYSTEM = "MINGW$bitness"
if (-not $SkipDownload) {
# First run setup
msys 'uname -a'
# Core update
msys 'pacman --noconfirm -Syuu'
# Normal update
msys 'pacman --noconfirm -Suu'
msys "pacman -S --noconfirm --needed autoconf automake git libtool make pactoys pkg-config wget"
# pacboy adds MINGW_PACKAGE_PREFIX to package names suffixed with :p
msys "pacboy -S --noconfirm --needed cmake:p ninja:p toolchain:p libusb:p hidapi:p"
}
if (-not (Test-Path ".\build\openocd-install\mingw$bitness")) {
msys "cd build && ../packages/windows/openocd/build-openocd.sh $bitness $mingw_arch"
}
if (-not (Test-Path ".\build\picotool-install\mingw$bitness")) {
msys "cd build && ../packages/windows/picotool/build-picotool.sh $bitness $mingw_arch $version"
}
if ($version.Substring(0, 1) -ge 2) {
# Sign files before packaging up the installer
sign "build\openocd-install\mingw$bitness\bin\openocd.exe",
"build\pico-sdk-tools\mingw$bitness\pioasm\pioasm.exe",
"build\picotool-install\mingw$bitness\picotool\picotool.exe"
} else {
$template = Get-Content ".\packages\windows\pico-sdk-tools\pico-sdk-tools-config-version.cmake" -Raw
$ExecutionContext.InvokeCommand.ExpandString($template) | Set-Content ".\build\pico-sdk-tools\mingw$bitness\pico-sdk-tools-config-version.cmake"
# Sign files before packaging up the installer
sign "build\openocd-install\mingw$bitness\bin\openocd.exe",
"build\pico-sdk-tools\mingw$bitness\elf2uf2.exe",
"build\pico-sdk-tools\mingw$bitness\pioasm.exe",
"build\picotool-install\mingw$bitness\picotool.exe"
}
# Package pico-sdk-tools separately as well
$filename = 'pico-sdk-tools-{0}-{1}.zip' -f
$version,
$suffix
Write-Host "Saving pico-sdk-tools package to $filename"
exec { tar -a -cf "bin\$filename" -C "build\pico-sdk-tools\mingw$bitness\" * }
# Package picotool separately as well
$version = (cmd /c ".\build\picotool-install\mingw$bitness\picotool\picotool.exe" version -s '2>&1')
Write-Host "Picotool version $version"
$filename = 'picotool-{0}-{1}.zip' -f
$version,
$suffix
Write-Host "Saving picotool package to $filename"
exec { tar -a -cf "bin\$filename" -C "build\picotool-install\mingw$bitness\" * }
# Package OpenOCD separately as well
$version = (cmd /c ".\build\openocd-install\mingw$bitness\bin\openocd.exe" --version '2>&1')[0]
if (-not ($version -match 'Open On-Chip Debugger (?<version>[a-zA-Z0-9\.\-+]+) \((?<timestamp>[0-9\-:]+)\)')) {
Write-Error 'Could not determine openocd version'
}
$filename = 'openocd-{0}-{1}.zip' -f
($Matches.version -replace '-.*$', ''),
$suffix
# Removing files with special char in their names
# they cause issues with some decompression libraries
exec { Remove-Item "build\openocd-install\mingw$bitness\share\openocd\scripts\target\1986*.cfg" }
Write-Host "Saving OpenOCD package to $filename"
exec { tar -a -cf "bin\$filename" -C "build\openocd-install\mingw$bitness\bin" * -C "..\share\openocd" "scripts" }