Debian Codegraph @

extra/libfido2/libfido2-1.15.0/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 # Override CMAKE_SYSTEM_VERSION if $WinSDK is set.
78 if (-Not ([string]::IsNullOrEmpty($WinSDK))) {
79 $CMAKE_SYSTEM_VERSION = "-DCMAKE_SYSTEM_VERSION='$WinSDK'"
80 } else {
81 $CMAKE_SYSTEM_VERSION = ''
82 }
83
84 Write-Host "WinSDK: $WinSDK"
85 Write-Host "Config: $Config"
86 Write-Host "Arch: $Arch"
87 Write-Host "Type: $Type"
88 Write-Host "Git: $Git"
89 Write-Host "CMake: $CMake"
90 Write-Host "7z: $SevenZ"
91 Write-Host "GPG: $GPG"
92
93 # Create build directories.
94 New-Item -Type Directory "${BUILD}" -Force
95 New-Item -Type Directory "${BUILD}\${Arch}" -Force
96 New-Item -Type Directory "${BUILD}\${Arch}\${Type}" -Force
97 New-Item -Type Directory "${STAGE}\${LIBRESSL}" -Force
98 New-Item -Type Directory "${STAGE}\${LIBCBOR}" -Force
99 New-Item -Type Directory "${STAGE}\${ZLIB}" -Force
100
101 # Create GNUPGHOME with an empty common.conf to disable use-keyboxd.
102 # Recent default is to enable keyboxd which in turn ignores --keyring
103 # arguments.
104 $GpgHome = "${BUILD}\.gnupg"
105 New-Item -Type Directory "${GpgHome}" -Force
106 New-Item -Type File "${GpgHome}\common.conf" -Force
107
108 # Create output directories.
109 New-Item -Type Directory "${OUTPUT}" -Force
110 New-Item -Type Directory "${OUTPUT}\${Arch}" -Force
111 New-Item -Type Directory "${OUTPUT}\${Arch}\${Type}" -force
112
113 # Fetch and verify dependencies.
114 Push-Location ${BUILD}
115 try {
116 if (-Not (Test-Path .\${LIBRESSL})) {
117 if (-Not (Test-Path .\${LIBRESSL}.tar.gz -PathType leaf)) {
118 Invoke-WebRequest ${LIBRESSL_URL}/${LIBRESSL}.tar.gz `
119 -OutFile .\${LIBRESSL}.tar.gz
120 }
121 if (-Not (Test-Path .\${LIBRESSL}.tar.gz.asc -PathType leaf)) {
122 Invoke-WebRequest ${LIBRESSL_URL}/${LIBRESSL}.tar.gz.asc `
123 -OutFile .\${LIBRESSL}.tar.gz.asc
124 }
125
126 Copy-Item "$PSScriptRoot\libressl.gpg" -Destination "${BUILD}"
127 & $GPG --homedir ${GpgHome} --list-keys
128 & $GPG --homedir ${GpgHome} --quiet --no-default-keyring `
129 --keyring ./libressl.gpg `
130 --verify .\${LIBRESSL}.tar.gz.asc .\${LIBRESSL}.tar.gz
131 if ($LastExitCode -ne 0) {
132 throw "GPG signature verification failed"
133 }
134 & $SevenZ e .\${LIBRESSL}.tar.gz
135 & $SevenZ x .\${LIBRESSL}.tar
136 Remove-Item -Force .\${LIBRESSL}.tar
137 }
138 if (-Not (Test-Path .\${LIBCBOR})) {
139 GitClone "${LIBCBOR_GIT}" "${LIBCBOR_BRANCH}" ".\${LIBCBOR}"
140 }
141 if (-Not (Test-Path .\${ZLIB})) {
142 GitClone "${ZLIB_GIT}" "${ZLIB_BRANCH}" ".\${ZLIB}"
143 }
144 } catch {
145 throw "Failed to fetch and verify dependencies"
146 } finally {
147 Pop-Location
148 }
149
150 # Build LibreSSL.
151 Push-Location ${STAGE}\${LIBRESSL}
152 try {
153 & $CMake ..\..\..\${LIBRESSL} -A "${Arch}" `
154 -DBUILD_SHARED_LIBS="${SHARED}" -DLIBRESSL_TESTS=OFF `
155 -DLIBRESSL_APPS=OFF -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG}" `
156 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE}" `
157 -DCMAKE_MSVC_RUNTIME_LIBRARY="${CMAKE_MSVC_RUNTIME_LIBRARY}" `
158 -DCMAKE_INSTALL_PREFIX="${PREFIX}" "${CMAKE_SYSTEM_VERSION}"; `
159 ExitOnError
160 & $CMake --build . --config ${Config} --verbose; ExitOnError
161 & $CMake --build . --config ${Config} --target install --verbose; `
162 ExitOnError
163 } catch {
164 throw "Failed to build LibreSSL"
165 } finally {
166 Pop-Location
167 }
168
169 # Build libcbor.
170 Push-Location ${STAGE}\${LIBCBOR}
171 try {
172 & $CMake ..\..\..\${LIBCBOR} -A "${Arch}" `
173 -DWITH_EXAMPLES=OFF `
174 -DBUILD_SHARED_LIBS="${SHARED}" `
175 -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG} /wd4703" `
176 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE} /wd4703" `
177 -DCMAKE_INSTALL_PREFIX="${PREFIX}" "${CMAKE_SYSTEM_VERSION}"; `
178 ExitOnError
179 & $CMake --build . --config ${Config} --verbose; ExitOnError
180 & $CMake --build . --config ${Config} --target install --verbose; `
181 ExitOnError
182 } catch {
183 throw "Failed to build libcbor"
184 } finally {
185 Pop-Location
186 }
187
188 # Build zlib.
189 Push-Location ${STAGE}\${ZLIB}
190 try {
191 & $CMake ..\..\..\${ZLIB} -A "${Arch}" `
192 -DBUILD_SHARED_LIBS="${SHARED}" `
193 -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG}" `
194 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE}" `
195 -DCMAKE_MSVC_RUNTIME_LIBRARY="${CMAKE_MSVC_RUNTIME_LIBRARY}" `
196 -DCMAKE_INSTALL_PREFIX="${PREFIX}" "${CMAKE_SYSTEM_VERSION}"; `
197 ExitOnError
198 & $CMake --build . --config ${Config} --verbose; ExitOnError
199 & $CMake --build . --config ${Config} --target install --verbose; `
200 ExitOnError
201 # Patch up zlib's various names.
202 if ("${Type}" -eq "Dynamic") {
203 ((Get-ChildItem -Path "${PREFIX}/lib") -Match "zlib[d]?.lib") |
204 Copy-Item -Destination "${PREFIX}/lib/zlib1.lib" -Force
205 ((Get-ChildItem -Path "${PREFIX}/bin") -Match "zlibd1.dll") |
206 Copy-Item -Destination "${PREFIX}/bin/zlib1.dll" -Force
207 } else {
208 ((Get-ChildItem -Path "${PREFIX}/lib") -Match "zlibstatic[d]?.lib") |
209 Copy-item -Destination "${PREFIX}/lib/zlib1.lib" -Force
210 }
211 } catch {
212 throw "Failed to build zlib"
213 } finally {
214 Pop-Location
215 }
216
217 # Build libfido2.
218 Push-Location ${STAGE}
219 try {
220 & $CMake ..\..\.. -A "${Arch}" `
221 -DCMAKE_BUILD_TYPE="${Config}" `
222 -DBUILD_SHARED_LIBS="${SHARED}" `
223 -DCBOR_INCLUDE_DIRS="${PREFIX}\include" `
224 -DCBOR_LIBRARY_DIRS="${PREFIX}\lib" `
225 -DCBOR_BIN_DIRS="${PREFIX}\bin" `
226 -DZLIB_INCLUDE_DIRS="${PREFIX}\include" `
227 -DZLIB_LIBRARY_DIRS="${PREFIX}\lib" `
228 -DZLIB_BIN_DIRS="${PREFIX}\bin" `
229 -DCRYPTO_INCLUDE_DIRS="${PREFIX}\include" `
230 -DCRYPTO_LIBRARY_DIRS="${PREFIX}\lib" `
231 -DCRYPTO_BIN_DIRS="${PREFIX}\bin" `
232 -DCRYPTO_LIBRARIES="${CRYPTO_LIBRARIES}" `
233 -DCMAKE_C_FLAGS_DEBUG="${CFLAGS_DEBUG} ${Fido2Flags}" `
234 -DCMAKE_C_FLAGS_RELEASE="${CFLAGS_RELEASE} ${Fido2Flags}" `
235 -DCMAKE_INSTALL_PREFIX="${PREFIX}" "${CMAKE_SYSTEM_VERSION}"; `
236 ExitOnError
237 & $CMake --build . --config ${Config} --verbose; ExitOnError
238 & $CMake --build . --config ${Config} --target regress --verbose; `
239 ExitOnError
240 & $CMake --build . --config ${Config} --target install --verbose; `
241 ExitOnError
242 # Copy DLLs.
243 if ("${SHARED}" -eq "ON") {
244 "cbor.dll", "${CRYPTO_LIBRARIES}.dll", "zlib1.dll" | `
245 %{ Copy-Item "${PREFIX}\bin\$_" `
246 -Destination "examples\${Config}" }
247 }
248 } catch {
249 throw "Failed to build libfido2"
250 } finally {
251 Pop-Location
252 }