-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbuild.ps1
331 lines (250 loc) · 10.5 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
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
[CmdletBinding()]
param (
[Parameter(Mandatory = $true,
Position = 0,
HelpMessage = "Path to a JSON installer configuration file.")]
[Alias("PSPath")]
[ValidateNotNullOrEmpty()]
[string]
$ConfigFile,
[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
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
. "$PSScriptRoot\common.ps1"
Write-Host "Building from $ConfigFile"
$basename = "pico-setup-windows"
$version = (Get-Content "$PSScriptRoot\version.txt").Trim()
$suffix = [io.path]::GetFileNameWithoutExtension($ConfigFile)
$outfile = "bin\$basename-$version-$suffix.exe"
$tools = (Get-Content '.\tools.json' | ConvertFrom-Json).tools
$config = Get-Content $ConfigFile | ConvertFrom-Json
$bitness = $config.bitness
$mingw_arch = $config.mingw_arch
$installers = $config.installers
($installers + $tools) | ForEach-Object {
$_ | Add-Member -NotePropertyName 'shortName' -NotePropertyValue ($_.name -replace '[^a-zA-Z0-9]', '')
if ($SkipDownload) {
Write-Host "Checking $($_.name): " -NoNewline
if (-not (Test-Path "installers/$($_.file)")) {
Write-Error "installers/$($_.file) not found"
}
}
else {
Write-Host "Downloading $($_.name): " -NoNewline
exec { curl.exe --fail --silent --show-error --url "$($_.href)" --location --output "installers/$($_.file)" --create-dirs --remote-time --time-cond "installers/$($_.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.
$version = ''
$versionRegEx = '([0-9]+\.)+[0-9]+'
if ($_.file -match $versionRegEx -or $_.href -match $versionRegEx) {
$version = $Matches[0]
} else {
$version = (Get-ChildItem ".\installers\$($_.file)").VersionInfo.ProductVersion
}
if ($version) {
Write-Host $version
} else {
Write-Host $_.file
}
}
mkdirp "build"
mkdirp "bin"
if (-not (Test-Path $MSYS2Path)) {
Write-Host 'Extracting MSYS2'
exec { & .\installers\msys2.exe -y "-o$(Resolve-Path (Split-Path $MSYS2Path -Parent))" }
}
if (-not (Test-Path build\NSIS)) {
Write-Host 'Extracting NSIS'
Expand-Archive '.\installers\nsis.zip' -DestinationPath '.\build'
Rename-Item (Resolve-Path '.\build\nsis-*').Path 'NSIS'
Expand-Archive '.\installers\nsis-log.zip' -DestinationPath '.\build\NSIS' -Force
}
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 (Test-Path ".\build\openocd-install\mingw$bitness")) {
# 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 mingw-w64-${mingw_arch}-toolchain mingw-w64-${mingw_arch}-libusb mingw-w64-${mingw_arch}-hidapi p7zip pkg-config wget"
# Keep it clean
if (Test-Path .\build\openocd) {
Remove-Item .\build\openocd -Recurse -Force
}
msys "cd build && ../build-openocd.sh $bitness $mingw_arch"
}
@"
!include "MUI2.nsh"
!include "WordFunc.nsh"
!define TITLE "Pico setup for Windows"
Name "`${TITLE}"
Caption "`${TITLE}"
VIAddVersionKey "FileDescription" "`${TITLE}"
VIAddVersionKey "InternalName" "$basename"
VIAddVersionKey "ProductName" "`${TITLE}"
VIAddVersionKey "FileVersion" "$version"
VIAddVersionKey "LegalCopyright" ""
VIFileVersion $version.0
VIProductVersion $version.0
OutFile "$outfile"
Unicode True
; Since we're packaging up a bunch of installers, the "Space required" shown is inaccurate
SpaceTexts "none"
InstallDir "`$DOCUMENTS\Pico"
;Get installation folder from registry if available
InstallDirRegKey HKCU "Software\$basename" ""
!define MUI_ABORTWARNING
!define MUI_WELCOMEPAGE_TITLE "`${TITLE}"
!define MUI_COMPONENTSPAGE_SMALLDESC
!define MUI_FINISHPAGE_RUN_TEXT "Clone and build Pico repos"
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_FUNCTION RunBuild
!define MUI_FINISHPAGE_SHOWREADME "`$INSTDIR\ReadMe.txt"
!define MUI_FINISHPAGE_SHOWREADME_TEXT "Show ReadMe"
!define MUI_FINISHPAGE_NOAUTOCLOSE
!insertmacro MUI_PAGE_WELCOME
;!insertmacro MUI_PAGE_LICENSE "`${NSISDIR}\Docs\Modern UI\License.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE "English"
Section
; Make sure that `$INSTDIR exists before enabling logging
SetOutPath `$INSTDIR
LogSet on
ReadRegStr `$R0 HKLM "SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine" "PowerShellVersion"
DetailPrint "Detected PowerShell version: `$R0"
`${VersionCompare} "5.1.0.0" `$R0 `$R1
`${If} `$R1 < 2
Abort "Windows PowerShell 5.1 is required for this installation. Please install WMF 5.1 and re-run setup."
`${EndIf}
ClearErrors
; https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
; Check for .NET Framework 4.6.2, required for Visual Studio 2019 Build Tools
ReadRegDWORD `$R0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Release"
DetailPrint "Detected .NET Framework 4 release: `$R0"
`${If} `$R0 < 394802
Abort ".NET Framework 4.6.2 or later is required for this installation. Please install the latest .NET Framework 4.x and re-run setup."
`${EndIf}
ClearErrors
InitPluginsDir
File /oname=`$TEMP\RefreshEnv.cmd RefreshEnv.cmd
SectionEnd
$($installers | ForEach-Object {
@"
Section "$($_.name)" Sec$($_.shortName)
ClearErrors
$(if ($_ | Get-Member additionalFiles) {
$_.additionalFiles | ForEach-Object {
"File /oname=`$PLUGINSDIR\$(Split-Path -Leaf $_) $_`r`n"
}
})
SetOutPath "`$TEMP"
File "installers\$($_.file)"
StrCpy `$0 "`$TEMP\$($_.file)"
ExecWait '$($_.exec)' `$1
DetailPrint "$($_.name) returned `$1"
Delete /REBOOTOK "`$0"
`${If} `${Errors}
Abort "Installation of $($_.name) failed"
$(if ($_ | Get-Member rebootExitCodes) {
$_.rebootExitCodes | ForEach-Object {
"`${ElseIf} `$1 = $_`r`n SetRebootFlag true"
}
})
`${ElseIf} `$1 <> 0
Abort "Installation of $($_.name) failed"
`${EndIf}
SectionEnd
LangString DESC_Sec$($_.shortName) `${LANG_ENGLISH} "$($_.name)"
"@
})
Section "VS Code Extensions" SecCodeExts
ReadEnvStr `$0 COMSPEC
nsExec::ExecToLog '"`$0" /c call "`$TEMP\RefreshEnv.cmd" && code --install-extension marus25.cortex-debug'
Pop `$1
nsExec::ExecToLog '"`$0" /c call "`$TEMP\RefreshEnv.cmd" && code --install-extension ms-vscode.cmake-tools'
Pop `$1
nsExec::ExecToLog '"`$0" /c call "`$TEMP\RefreshEnv.cmd" && code --install-extension ms-vscode.cpptools'
Pop `$1
SectionEnd
LangString DESC_SecCodeExts `${LANG_ENGLISH} "Recommended extensions for Visual Studio Code: C/C++, CMake-Tools, and Cortex-Debug"
Section "OpenOCD" SecOpenOCD
SetOutPath "`$INSTDIR\tools\openocd"
File "build\openocd-install\mingw$bitness\bin\*.*"
SetOutPath "`$INSTDIR\tools\openocd\scripts"
File /r "build\openocd-install\mingw$bitness\share\openocd\scripts\*.*"
SectionEnd
LangString DESC_SecOpenOCD `${LANG_ENGLISH} "Open On-Chip Debugger with picoprobe support"
Section "Pico environment" SecPico
SetOutPath "`$INSTDIR"
File "pico-env.cmd"
File "pico-setup.cmd"
File "docs\ReadMe.txt"
CreateShortcut "`$INSTDIR\Developer Command Prompt for Pico.lnk" "cmd.exe" '/k "`$INSTDIR\pico-env.cmd"'
; Unconditionally create a shortcut for VS Code -- in case the user had it
; installed already, or if they install it later
CreateShortcut "`$INSTDIR\Visual Studio Code for Pico.lnk" "cmd.exe" '/c (call "`$INSTDIR\pico-env.cmd" && code) || pause'
; SetOutPath is needed here to set the working directory for the shortcut
SetOutPath "`$INSTDIR\pico-project-generator"
CreateShortcut "`$INSTDIR\Pico Project Generator.lnk" "cmd.exe" '/c (call "`$INSTDIR\pico-env.cmd" && python "`$INSTDIR\pico-project-generator\pico_project.py" --gui) || pause'
; Reset working dir for pico-setup.cmd launched from the finish page
SetOutPath "`$INSTDIR"
SectionEnd
LangString DESC_SecPico `${LANG_ENGLISH} "Scripts for cloning the Pico SDK and tools repos, and for setting up your Pico development environment."
Section "Download documents and files" SecDocs
SetOutPath "`$INSTDIR"
File "common.ps1"
File "pico-docs.ps1"
SectionEnd
Function RunBuild
ReadEnvStr `$0 COMSPEC
Exec '"`$0" /k call "`$TEMP\RefreshEnv.cmd" && del "`$TEMP\RefreshEnv.cmd" && call "`$INSTDIR\pico-setup.cmd" 1'
FunctionEnd
LangString DESC_SecDocs `${LANG_ENGLISH} "Adds a script to download the latest Pico documents, design files, and UF2 files."
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT `${SecCodeExts} `$(DESC_SecCodeExts)
!insertmacro MUI_DESCRIPTION_TEXT `${SecOpenOCD} `$(DESC_SecOpenOCD)
!insertmacro MUI_DESCRIPTION_TEXT `${SecPico} `$(DESC_SecPico)
!insertmacro MUI_DESCRIPTION_TEXT `${SecDocs} `$(DESC_SecDocs)
$($installers | ForEach-Object {
" !insertmacro MUI_DESCRIPTION_TEXT `${Sec$($_.shortName)} `$(DESC_Sec$($_.shortName))`n"
})
!insertmacro MUI_FUNCTION_DESCRIPTION_END
"@ | Set-Content ".\$basename-$suffix.nsi"
exec { .\build\NSIS\makensis ".\$basename-$suffix.nsi" }
Write-Host "Installer saved to $outfile"
# 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}-{2}.zip' -f
($Matches.version -replace '-dirty$', ''),
($Matches.timestamp -replace '[:-]', ''),
$suffix
Write-Host "Saving OpenOCD package to $filename"
tar -a -cf "bin\$filename" -C "build\openocd-install\mingw$bitness\bin" * -C "..\share\openocd" "scripts"