Debian Codegraph @

windows/build.ps1

raw
1 # Copyright (c) 2021-2022 Yubico AB. All rights reserved.
2 # Use of this source code is governed by a BSD-style
3 # license that can be found in the LICENSE file.
4 # SPDX-License-Identifier: BSD-2-Clause
5
6 param(
7 [string]$CMakePath = "C:\Program Files\CMake\bin\cmake.exe",
8 [string]$GitPath = "C:\Program Files\Git\bin\git.exe",
9 [string]$SevenZPath = "C:\Program Files\7-Zip\7z.exe",
10 [string]$GPGPath = "C:\Program Files (x86)\GnuPG\bin\gpg.exe",
11 [string]$WinSDK = "",
12 [string]$Config = "Release",
13 [string]$Arch = "x64",
14 [string]$Type = "dynamic",
15 [string]$Fido2Flags = ""
16 )
17
18 $ErrorActionPreference = "Stop"
19 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
20
21 . "$PSScriptRoot\const.ps1"
22
23 Function ExitOnError() {
24 if ($LastExitCode -ne 0) {
25 throw "A command exited with status $LastExitCode"
26 }
27 }
28
29 Function GitClone(${REPO}, ${BRANCH}, ${DIR}) {
30 Write-Host "Cloning ${REPO}..."
31 & $Git -c advice.detachedHead=false clone --quiet --depth=1 `
32 --branch "${BRANCH}" "${REPO}" "${DIR}"
33 Write-Host "${REPO}'s ${BRANCH} HEAD is:"
34 & $Git -C "${DIR}" show -s HEAD
35 }
36
37 # Find Git.
38 $Git = $(Get-Command git -ErrorAction Ignore | `
39 Select-Object -ExpandProperty Source)
40 if ([string]::IsNullOrEmpty($Git)) {
41 $Git = $GitPath
42 }
43 if (-Not (Test-Path $Git)) {
44 throw "Unable to find Git at $Git"
45 }
46
47 # Find CMake.
48 $CMake = $(Get-Command cmake -ErrorAction Ignore | `
49 Select-Object -ExpandProperty Source)
50 if ([string]::IsNullOrEmpty($CMake)) {
51 $CMake = $CMakePath
52 }
53 if (-Not (Test-Path $CMake)) {
54 throw "Unable to find CMake at $CMake"
55 }
56
57 # Find 7z.
58 $SevenZ = $(Get-Command 7z -ErrorAction Ignore | `
59 Select-Object -ExpandProperty Source)
60 if ([string]::IsNullOrEmpty($SevenZ)) {
61 $SevenZ = $SevenZPath
62 }
63 if (-Not (Test-Path $SevenZ)) {
64 throw "Unable to find 7z at $SevenZ"
65 }
66
67 # Find GPG.
68 $GPG = $(Get-Command gpg -ErrorAction Ignore | `
69 Select-Object -ExpandProperty Source)
70 if ([string]::IsNullOrEmpty($GPG)) {
71 $GPG = $GPGPath
72 }
73 if (-Not (Test-Path $GPG)) {
74 throw "Unable to find GPG at $GPG"
75 }
76
77 Write-Host "WinSDK: $WinSDK"
78 Write-Host "Config: $Config"
79 Write-Host "Arch: $Arch"
80 Write-Host "Type: $Type"
81 Write-Host "Git: $Git"
82 Write-Host "CMake: $CMake"
83 Write-Host "7z: $SevenZ"
84 Write-Host "GPG: $GPG"
85
86 # Create build directories.
87 New-Item -Type Directory "${BUILD}" -Force
88 New-Item -Type Directory "${BUILD}\${Arch}" -Force
89 New-Item -Type Directory "${BUILD}\${Arch}\${Type}" -Force
90 New-Item -Type Directory "${STAGE}\${LIBRESSL}" -Force
91 New-Item -Type Directory "${STAGE}\${LIBCBOR}" -Force
92 New-Item -Type Directory "${STAGE}\${ZLIB}" -Force
93
94 # Create GNUPGHOME with an empty common.conf to disable use-keyboxd.
95 # Recent default is to enable keyboxd which in turn ignores --keyring
96 # arguments.
97 $GpgHome = "${BUILD}\.gnupg"
98 New-Item -Type Directory "${GpgHome}" -Force
99 New-Item -Type File "${GpgHome}\common.conf" -Force
100
101 # Create output directories.
102 New-Item -Type Directory "${OUTPUT}" -Force
103 New-Item -Type Directory "${OUTPUT}\${Arch}" -Force
104 New-Item -Type Directory "${OUTPUT}\${Arch}\${Type}" -force
105
106 # Override Windows SDK version if $WinSDK is set.
107 if (-Not ([string]::IsNullOrEmpty($WinSDK))) {
108 $Arch = "$Arch,version=$WinSDK"
109 }
110
111 # Fetch and verify dependencies.
112 Push-Location ${BUILD}
113 try {
114 if (-Not (Test-Path .\${LIBRESSL})) {
115 if (-Not (Test-Path .\${LIBRESSL}.tar.gz -PathType leaf)) {
116 Invoke-WebRequest ${LIBRESSL_URL}/${LIBRESSL}.tar.gz `
117 -OutFile .\${LIBRESSL}.tar.gz
118 }
119 if (-Not (Test-Path .\${LIBRESSL}.tar.gz.asc -PathType leaf)) {
120 Invoke-WebRequest ${LIBRESSL_URL}/${LIBRESSL}.tar.gz.asc `
121 -OutFile .\${LIBRESSL}.tar.gz.asc
122 }
123
124 Copy-Item "$PSScriptRoot\libressl.gpg" -Destination "${BUILD}"
125 & $GPG --homedir ./.gnupg --list-keys
126 & $GPG --homedir ./.gnupg --quiet --no-default-keyring `
127 --keyring ./libressl.gpg `
128 --verify ./${LIBRESSL}.tar.gz.asc ./${LIBRESSL}.tar.gz
129 if ($LastExitCode -ne 0) {
130 throw "GPG signature verification failed"
131 }
132 & $SevenZ e .\${LIBRESSL}.tar.gz
133 & $SevenZ x .\${LIBRESSL}.tar
134 Remove-Item -Force .\${LIBRESSL}.tar
135 }
136 if (-Not (Test-Path .\${LIBCBOR})) {
137 GitClone "${LIBCBOR_GIT}" "${LIBCBOR_BRANCH}" ".\${LIBCBOR}"
138 }
139 if (-Not (Test-Path .\${ZLIB})) {
140 GitClone "${ZLIB_GIT}" "${ZLIB_BRANCH}" ".\${ZLIB}"
141 }
142 } catch {
143 throw "Failed to fetch and verify dependencies"
144 } finally {
145 Pop-Location
146 }
147
148 # Build LibreSSL.
149 Push-Location ${STAGE}\${LIBRESSL}
150 try {
151 & $CMake ..\..\..\${LIBRESSL} -A "${Arch}" `
152 -DBUILD_SHARED_LIBS="${SHARED}" -DLIBRESSL_TESTS=OFF `
153 -DLIBRESSL_APPS=OFF -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG}" `
154 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE}" `
155 -DCMAKE_MSVC_RUNTIME_LIBRARY="${CMAKE_MSVC_RUNTIME_LIBRARY}" `
156 -DCMAKE_INSTALL_PREFIX="${PREFIX}"; `
157 ExitOnError
158 & $CMake --build . --config ${Config} --verbose; ExitOnError
159 & $CMake --build . --config ${Config} --target install --verbose; `
160 ExitOnError
161 } catch {
162 throw "Failed to build LibreSSL"
163 } finally {
164 Pop-Location
165 }
166
167 # Build libcbor.
168 Push-Location ${STAGE}\${LIBCBOR}
169 try {
170 & $CMake ..\..\..\${LIBCBOR} -A "${Arch}" `
171 -DWITH_EXAMPLES=OFF `
172 -DBUILD_SHARED_LIBS="${SHARED}" `
173 -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG} /wd4703" `
174 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE} /wd4703" `
175 -DCMAKE_INSTALL_PREFIX="${PREFIX}"; `
176 ExitOnError
177 & $CMake --build . --config ${Config} --verbose; ExitOnError
178 & $CMake --build . --config ${Config} --target install --verbose; `
179 ExitOnError
180 } catch {
181 throw "Failed to build libcbor"
182 } finally {
183 Pop-Location
184 }
185
186 # Build zlib.
187 Push-Location ${STAGE}\${ZLIB}
188 try {
189 & $CMake ..\..\..\${ZLIB} -A "${Arch}" `
190 -DBUILD_SHARED_LIBS="${SHARED}" `
191 -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG}" `
192 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE}" `
193 -DCMAKE_MSVC_RUNTIME_LIBRARY="${CMAKE_MSVC_RUNTIME_LIBRARY}" `
194 -DCMAKE_INSTALL_PREFIX="${PREFIX}"; `
195 ExitOnError
196 & $CMake --build . --config ${Config} --verbose; ExitOnError
197 & $CMake --build . --config ${Config} --target install --verbose; `
198 ExitOnError
199 # Patch up zlib's various names.
200 if ("${Type}" -eq "Dynamic") {
201 ((Get-ChildItem -Path "${PREFIX}/lib") -Match "zlib[d]?.lib") |
202 Copy-Item -Destination "${PREFIX}/lib/zlib1.lib" -Force
203 ((Get-ChildItem -Path "${PREFIX}/bin") -Match "zlibd1.dll") |
204 Copy-Item -Destination "${PREFIX}/bin/zlib1.dll" -Force
205 } else {
206 ((Get-ChildItem -Path "${PREFIX}/lib") -Match "zlibstatic[d]?.lib") |
207 Copy-item -Destination "${PREFIX}/lib/zlib1.lib" -Force
208 }
209 } catch {
210 throw "Failed to build zlib"
211 } finally {
212 Pop-Location
213 }
214
215 # Build libfido2.
216 Push-Location ${STAGE}
217 try {
218 & $CMake ..\..\.. -A "${Arch}" `
219 -DCMAKE_BUILD_TYPE="${Config}" `
220 -DBUILD_SHARED_LIBS="${SHARED}" `
221 -DCBOR_INCLUDE_DIRS="${PREFIX}\include" `
222 -DCBOR_LIBRARY_DIRS="${PREFIX}\lib" `
223 -DCBOR_BIN_DIRS="${PREFIX}\bin" `
224 -DZLIB_INCLUDE_DIRS="${PREFIX}\include" `
225 -DZLIB_LIBRARY_DIRS="${PREFIX}\lib" `
226 -DZLIB_BIN_DIRS="${PREFIX}\bin" `
227 -DCRYPTO_INCLUDE_DIRS="${PREFIX}\include" `
228 -DCRYPTO_LIBRARY_DIRS="${PREFIX}\lib" `
229 -DCRYPTO_BIN_DIRS="${PREFIX}\bin" `
230 -DCRYPTO_LIBRARIES="${CRYPTO_LIB}" `
231 -DCRYPTO_DLL="${CRYPTO_DLL}" `
232 -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG} ${Fido2Flags}" `
233 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE} ${Fido2Flags}" `
234 -DCMAKE_INSTALL_PREFIX="${PREFIX}"; `
235 ExitOnError
236 & $CMake --build . --config ${Config} --verbose; ExitOnError
237 & $CMake --build . --config ${Config} --target regress --verbose; `
238 ExitOnError
239 & $CMake --build . --config ${Config} --target install --verbose; `
240 ExitOnError
241 # Copy DLLs.
242 if ("${SHARED}" -eq "ON") {
243 "cbor.dll", "${CRYPTO_DLL}.dll", "zlib1.dll" | `
244 %{ Copy-Item "${PREFIX}\bin\$_" `
245 -Destination "examples\${Config}" }
246 }
247 } catch {
248 throw "Failed to build libfido2"
249 } finally {
250 Pop-Location
251 }