forked from esp-rs/rust-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Install-RustToolchain.ps1
160 lines (134 loc) · 5.38 KB
/
Install-RustToolchain.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
[CmdletBinding()]
param (
[Parameter()]
[String]
$ExportFile = '',
[String]
$ToolchainVersion = '1.57.0.2',
[String]
$ToolchainDestination = "${HOME}/.rustup/toolchains/esp",
[String]
$LlvmVersion = "esp-13.0.0-20211203",
[String]
[ValidateSet("install", "reinstall", "uninstall", "export")]
$InstallationMode = 'install'
)
$ErrorActionPreference = "Stop"
# Disable progress bar when downloading - speed up download - https://stackoverflow.com/questions/28682642/powershell-why-is-using-invoke-webrequest-much-slower-than-a-browser-download
$ProgressPreference = 'SilentlyContinue'
$ExportContent = ""
#Set-PSDebug -Trace 1
$RustcMinimalMinorVersion="55"
$EspFlashUrl="https://github.com/esp-rs/espflash/releases/latest/download/cargo-espflash.exe"
$EspFlashBin="${env:USERPROFILE}\.cargo\bin\cargo-espflash.exe"
"Processing configuration:"
"-InstalltationMode = ${InstallationMode}"
"-LlvmVersion = ${LlvmVersion}"
"-ToolchainVersion = ${ToolchainVersion}"
"-ToolchainDestination = ${ToolchainDestination}"
function InstallRust() {
Invoke-WebRequest https://win.rustup.rs/x86_64 -OutFile rustup-init.exe
./rustup-init.exe --default-toolchain stable -y
$env:PATH+=";$env:USERPROFILE\.cargo\bin"
$ExportContent+="`n" + '$env:PATH+=";$env:USERPROFILE\.cargo\bin"'
}
function InstallRustFmt() {
rustup component add rustfmt --toolchain=stable
}
function ExportVariables() {
"Add following command to PowerShell profile"
$ExportContent+="`n" + '$env:PATH="' + "${IdfToolXtensaElfClang}/bin/;" + '$env:PATH"'
$ExportContent+="`n" + '$env:LIBCLANG_PATH="' + "${IdfToolXtensaElfClang}/bin/libclang.dll" + '"'
# Workaround of https://github.com/espressif/esp-idf/issues/7910
$ExportContent+="`n" + '$env:PIP_USER="no"'
$ExportContent
if ('' -ne $ExportFile) {
Out-File -FilePath $ExportFile -InputObject $ExportContent
}
}
if (-Not (Get-Command rustup -ErrorAction SilentlyContinue)) {
InstallRust
}
if ((rustup show | Select-String -Pattern stable).Length -eq 0) {
InstallRust
}
if ((rustc --version).Substring(8,2) -lt $RustcMinimalMinorVersion) {
"rustc version is too low, requires 1.${RustcMinimalMinorVersion}"
"calling rustup"
InstallRust
}
# It seems there is a dependency on nightly for some reason only on Windows
# It might be caused by way of packaging to dist ZIP.
if ((rustup show | Select-String -Pattern nightly).Length -eq 0) {
rustup toolchain install nightly
}
if (-Not (Get-Command rustfmt -errorAction SilentlyContinue)) {
InstallRustFmt
}
if ((rustfmt --version | Select-String -Pattern stable).Length -eq 0) {
InstallRustFmt
}
$Arch="x86_64-pc-windows-msvc"
$RustDist="rust-${ToolchainVersion}-${Arch}"
$RustDistZipUrl="https://github.com/esp-rs/rust-build/releases/download/v${ToolchainVersion}/${RustDist}.zip"
$IdfToolsPath="${HOME}/.espressif"
$IdfToolXtensaElfClang="${IdfToolsPath}/tools/xtensa-esp32-elf-clang/${LlvmVersion}-${Arch}"
$LlvmArch="win64"
$LlvmArtifactVersion=$LlvmVersion.Substring(4).Substring(0,6).Replace(".","_")
$LlvmFile="xtensa-esp32-elf-llvm${LlvmArtifactVersion}-${LlvmVersion}-${LlvmArch}.zip"
$LlvmUrl="https://github.com/espressif/llvm-project/releases/download/${LlvmVersion}/${LlvmFile}"
# Only export variables
if ("export" -eq $InstallationMode) {
ExportVariables
Exit 0
}
if (("uninstall" -eq $InstallationMode) -or ("reinstall" -eq $InstallationMode)) {
"Removing:"
" - ${ToolchainDestination}"
Remove-Item -Recurse -Force -Path ${ToolchainDestination} -ErrorAction SilentlyContinue
" - ${IdfToolXtensaElfClang}"
Remove-Item -Recurse -Force -Path ${IdfToolXtensaElfClang} -ErrorAction SilentlyContinue
if ("uninstall" -eq $InstallationMode) {
exit 0
}
}
if (Test-Path -Path ${ToolchainDestination} -PathType Container) {
"Previous installation of toolchain exist in: ${ToolchainDestination}"
"Please, remove the directory before new installation."
exit 1
}
mkdir -p "${HOME}/.rustup/toolchains/" -ErrorAction SilentlyContinue
Push-Location "${HOME}/.rustup/toolchains"
"* installing esp toolchain"
if (-Not (Test-Path -Path "${RustDist}.zip" -PathType Leaf)) {
"** downloading: ${RustDistZipUrl}"
Invoke-WebRequest "${RustDistZipUrl}" -OutFile "${RustDist}.zip"
}
Expand-Archive .\${RustDist}.zip -DestinationPath ${ToolchainDestination}-tmp
mv ${ToolchainDestination}-tmp/* ${ToolchainDestination}
Remove-Item -Recurse -Force ${ToolchainDestination}-tmp
"Toolchains:"
rustup toolchain list
Pop-Location
"* installing ${IdfToolXtensaElfClang}"
if (-Not (Test-Path -Path $IdfToolXtensaElfClang)) {
if (-Not (Test-Path -Path ${LlvmFile} -PathType Leaf)) {
"** downloading: ${LlvmUrl}"
Invoke-WebRequest "${LlvmUrl}" -OutFile ${LlvmFile}
}
mkdir -p "${IdfToolsPath}/tools/xtensa-esp32-elf-clang/" -ErrorAction SilentlyContinue
Expand-Archive ${LlvmFile} -DestinationPath ${IdfToolXtensaElfClang}-tmp
mv ${IdfToolXtensaElfClang}-tmp/xtensa-esp32-elf-clang "${IdfToolXtensaElfClang}"
Remove-Item -Recurse -Force ${IdfToolXtensaElfClang}-tmp/
"done"
} else {
"already installed"
}
"Install common dependencies"
cargo install ldproxy
# Install espflash from binary archive
if (-Not (Test-Path $EspFlashBin -PathType Leaf)) {
"** installing cargo-espflash from $EspFlashUrl to $EspFlashBin"
Invoke-WebRequest $EspFlashUrl -OutFile $EspFlashBin
}
ExportVariables