xref: /freebsd/contrib/libfido2/windows/build.ps1 (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
1 param(
2 	[string]$CMakePath = "C:\Program Files\CMake\bin\cmake.exe",
3 	[string]$GitPath = "C:\Program Files\Git\bin\git.exe",
4 	[string]$SevenZPath = "C:\Program Files\7-Zip\7z.exe",
5 	[string]$GPGPath = "C:\Program Files (x86)\GnuPG\bin\gpg.exe",
6 	[string]$WinSDK = "",
7 	[string]$Fido2Flags = ""
8 )
9 
10 $ErrorActionPreference = "Continue"
11 
12 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
13 
14 # LibreSSL coordinates.
15 New-Variable -Name 'LIBRESSL_URL' `
16 	-Value 'https://fastly.cdn.openbsd.org/pub/OpenBSD/LibreSSL' -Option Constant
17 New-Variable -Name 'LIBRESSL' -Value 'libressl-3.2.5' -Option Constant
18 
19 # libcbor coordinates.
20 New-Variable -Name 'LIBCBOR' -Value 'libcbor-0.8.0' -Option Constant
21 New-Variable -Name 'LIBCBOR_BRANCH' -Value 'v0.8.0' -Option Constant
22 New-Variable -Name 'LIBCBOR_GIT' -Value 'https://github.com/pjk/libcbor' `
23 	-Option Constant
24 
25 # zlib coordinates.
26 New-Variable -Name 'ZLIB' -Value 'zlib-1.2.11' -Option Constant
27 New-Variable -Name 'ZLIB_BRANCH' -Value 'v1.2.11' -Option Constant
28 New-Variable -Name 'ZLIB_GIT' -Value 'https://github.com/madler/zlib' `
29 	-Option Constant
30 
31 # Work directories.
32 New-Variable -Name 'BUILD' -Value "$PSScriptRoot\..\build" -Option Constant
33 New-Variable -Name 'OUTPUT' -Value "$PSScriptRoot\..\output" -Option Constant
34 
35 # Find CMake.
36 $CMake = $(Get-Command cmake -ErrorAction Ignore | Select-Object -ExpandProperty Source)
37 if([string]::IsNullOrEmpty($CMake)) {
38 	$CMake = $CMakePath
39 }
40 
41 # Find Git.
42 $Git = $(Get-Command git -ErrorAction Ignore | Select-Object -ExpandProperty Source)
43 if([string]::IsNullOrEmpty($Git)) {
44 	$Git = $GitPath
45 }
46 
47 # Find 7z.
48 $SevenZ = $(Get-Command 7z -ErrorAction Ignore | Select-Object -ExpandProperty Source)
49 if([string]::IsNullOrEmpty($SevenZ)) {
50 	$SevenZ = $SevenZPath
51 }
52 
53 # Find GPG.
54 $GPG = $(Get-Command gpg -ErrorAction Ignore | Select-Object -ExpandProperty Source)
55 if([string]::IsNullOrEmpty($GPG)) {
56 	$GPG = $GPGPath
57 }
58 
59 # Override CMAKE_SYSTEM_VERSION if $WinSDK is set.
60 if(-Not ([string]::IsNullOrEmpty($WinSDK))) {
61 	$CMAKE_SYSTEM_VERSION = "-DCMAKE_SYSTEM_VERSION='$WinSDK'"
62 } else {
63 	$CMAKE_SYSTEM_VERSION = ''
64 }
65 
66 if(-Not (Test-Path $CMake)) {
67 	throw "Unable to find CMake at $CMake"
68 }
69 
70 if(-Not (Test-Path $Git)) {
71 	throw "Unable to find Git at $Git"
72 }
73 
74 if(-Not (Test-Path $SevenZ)) {
75 	throw "Unable to find 7z at $SevenZ"
76 }
77 
78 if(-Not (Test-Path $GPG)) {
79 	throw "Unable to find GPG at $GPG"
80 }
81 
82 Write-Host "Git: $Git"
83 Write-Host "CMake: $CMake"
84 Write-Host "7z: $SevenZ"
85 Write-Host "GPG: $GPG"
86 
87 New-Item -Type Directory ${BUILD}
88 New-Item -Type Directory ${BUILD}\32
89 New-Item -Type Directory ${BUILD}\32\dynamic
90 New-Item -Type Directory ${BUILD}\32\static
91 New-Item -Type Directory ${BUILD}\64
92 New-Item -Type Directory ${BUILD}\64\dynamic
93 New-Item -Type Directory ${BUILD}\64\static
94 New-Item -Type Directory ${OUTPUT}
95 New-Item -Type Directory ${OUTPUT}\pkg\Win64\Release\v142\dynamic
96 New-Item -Type Directory ${OUTPUT}\pkg\Win32\Release\v142\dynamic
97 New-Item -Type Directory ${OUTPUT}\pkg\Win64\Release\v142\static
98 New-Item -Type Directory ${OUTPUT}\pkg\Win32\Release\v142\static
99 
100 Push-Location ${BUILD}
101 
102 try {
103 	if (Test-Path .\${LIBRESSL}) {
104 		Remove-Item .\${LIBRESSL} -Recurse -ErrorAction Stop
105 	}
106 
107 	if(-Not (Test-Path .\${LIBRESSL}.tar.gz -PathType leaf)) {
108 		Invoke-WebRequest ${LIBRESSL_URL}/${LIBRESSL}.tar.gz `
109 			-OutFile .\${LIBRESSL}.tar.gz
110 	}
111 	if(-Not (Test-Path .\${LIBRESSL}.tar.gz.asc -PathType leaf)) {
112 		Invoke-WebRequest ${LIBRESSL_URL}/${LIBRESSL}.tar.gz.asc `
113 			-OutFile .\${LIBRESSL}.tar.gz.asc
114 	}
115 
116 	Copy-Item "$PSScriptRoot\libressl.gpg" -Destination "${BUILD}"
117 	& $GPG --list-keys
118 	& $GPG -v --no-default-keyring --keyring ./libressl.gpg `
119 		--verify .\${LIBRESSL}.tar.gz.asc .\${LIBRESSL}.tar.gz
120 	if ($LastExitCode -ne 0) {
121 		throw "GPG signature verification failed"
122 	}
123 
124 	& $SevenZ e .\${LIBRESSL}.tar.gz
125 	& $SevenZ x .\${LIBRESSL}.tar
126 	Remove-Item -Force .\${LIBRESSL}.tar
127 
128 	if(-Not (Test-Path .\${LIBCBOR})) {
129 		Write-Host "Cloning ${LIBCBOR}..."
130 		& $Git clone --branch ${LIBCBOR_BRANCH} ${LIBCBOR_GIT} `
131 			.\${LIBCBOR}
132 	}
133 
134 	if(-Not (Test-Path .\${ZLIB})) {
135 		Write-Host "Cloning ${ZLIB}..."
136 		& $Git clone --branch ${ZLIB_BRANCH} ${ZLIB_GIT} `
137 			.\${ZLIB}
138 	}
139 } catch {
140 	throw "Failed to fetch and verify dependencies"
141 } finally {
142 	Pop-Location
143 }
144 
145 Function Build(${OUTPUT}, ${GENERATOR}, ${ARCH}, ${SHARED}, ${FLAGS}) {
146 	if (-Not (Test-Path .\${LIBRESSL})) {
147 		New-Item -Type Directory .\${LIBRESSL} -ErrorAction Stop
148 	}
149 
150 	Push-Location .\${LIBRESSL}
151 	& $CMake ..\..\..\${LIBRESSL} -G "${GENERATOR}" -A "${ARCH}" `
152 		-DBUILD_SHARED_LIBS="${SHARED}" -DLIBRESSL_TESTS=OFF `
153 		-DCMAKE_C_FLAGS_RELEASE="${FLAGS} /Zi /guard:cf /sdl" `
154 		-DCMAKE_INSTALL_PREFIX="${OUTPUT}" "${CMAKE_SYSTEM_VERSION}"
155 	& $CMake --build . --config Release --verbose
156 	& $CMake --build . --config Release --target install --verbose
157 	Pop-Location
158 
159 	if (-Not (Test-Path .\${LIBCBOR})) {
160 		New-Item -Type Directory .\${LIBCBOR} -ErrorAction Stop
161 	}
162 
163 	Push-Location .\${LIBCBOR}
164 	& $CMake ..\..\..\${LIBCBOR} -G "${GENERATOR}" -A "${ARCH}" `
165 		-DBUILD_SHARED_LIBS="${SHARED}" `
166 		-DCMAKE_C_FLAGS_RELEASE="${FLAGS} /Zi /guard:cf /sdl" `
167 		-DCMAKE_INSTALL_PREFIX="${OUTPUT}" "${CMAKE_SYSTEM_VERSION}"
168 	& $CMake --build . --config Release --verbose
169 	& $CMake --build . --config Release --target install --verbose
170 	Pop-Location
171 
172 	if(-Not (Test-Path .\${ZLIB})) {
173 		New-Item -Type Directory .\${ZLIB} -ErrorAction Stop
174 	}
175 
176 	Push-Location .\${ZLIB}
177 	& $CMake ..\..\..\${ZLIB} -G "${GENERATOR}" -A "${ARCH}" `
178 		-DBUILD_SHARED_LIBS="${SHARED}" `
179 		-DCMAKE_C_FLAGS_RELEASE="${FLAGS} /Zi /guard:cf /sdl" `
180 		-DCMAKE_INSTALL_PREFIX="${OUTPUT}" "${CMAKE_SYSTEM_VERSION}"
181 	& $CMake --build . --config Release --verbose
182 	& $CMake --build . --config Release --target install --verbose
183 	Pop-Location
184 
185 	& $CMake ..\..\.. -G "${GENERATOR}" -A "${ARCH}" `
186 		-DBUILD_SHARED_LIBS="${SHARED}" `
187 		-DCBOR_INCLUDE_DIRS="${OUTPUT}\include" `
188 		-DCBOR_LIBRARY_DIRS="${OUTPUT}\lib" `
189 		-DZLIB_INCLUDE_DIRS="${OUTPUT}\include" `
190 		-DZLIB_LIBRARY_DIRS="${OUTPUT}\lib" `
191 		-DCRYPTO_INCLUDE_DIRS="${OUTPUT}\include" `
192 		-DCRYPTO_LIBRARY_DIRS="${OUTPUT}\lib" `
193 		-DCMAKE_C_FLAGS_RELEASE="${FLAGS} /Zi /guard:cf /sdl ${Fido2Flags}" `
194 		-DCMAKE_INSTALL_PREFIX="${OUTPUT}" "${CMAKE_SYSTEM_VERSION}"
195 	& $CMake --build . --config Release --verbose
196 	& $CMake --build . --config Release --target install --verbose
197 	if ("${SHARED}" -eq "ON") {
198 		"cbor.dll", "crypto-46.dll", "zlib1.dll" | %{ Copy-Item "${OUTPUT}\bin\$_" `
199 			-Destination "examples\Release" }
200 	}
201 }
202 
203 Function Package-Headers() {
204 	Copy-Item "${OUTPUT}\64\dynamic\include" -Destination "${OUTPUT}\pkg" `
205 		-Recurse -ErrorAction Stop
206 }
207 
208 Function Package-Dynamic(${SRC}, ${DEST}) {
209 	Copy-Item "${SRC}\bin\cbor.dll" "${DEST}" -ErrorAction Stop
210 	Copy-Item "${SRC}\lib\cbor.lib" "${DEST}" -ErrorAction Stop
211 	Copy-Item "${SRC}\bin\zlib1.dll" "${DEST}" -ErrorAction Stop
212 	Copy-Item "${SRC}\lib\zlib.lib" "${DEST}" -ErrorAction Stop
213 	Copy-Item "${SRC}\bin\crypto-46.dll" "${DEST}" -ErrorAction Stop
214 	Copy-Item "${SRC}\lib\crypto-46.lib" "${DEST}" -ErrorAction Stop
215 	Copy-Item "${SRC}\bin\fido2.dll" "${DEST}" -ErrorAction Stop
216 	Copy-Item "${SRC}\lib\fido2.lib" "${DEST}" -ErrorAction Stop
217 }
218 
219 Function Package-Static(${SRC}, ${DEST}) {
220 	Copy-Item "${SRC}/lib/cbor.lib" "${DEST}" -ErrorAction Stop
221 	Copy-Item "${SRC}/lib/zlib.lib" "${DEST}" -ErrorAction Stop
222 	Copy-Item "${SRC}/lib/crypto-46.lib" "${DEST}" -ErrorAction Stop
223 	Copy-Item "${SRC}/lib/fido2_static.lib" "${DEST}/fido2.lib" `
224 		-ErrorAction Stop
225 }
226 
227 Function Package-PDBs(${SRC}, ${DEST}) {
228 	Copy-Item "${SRC}\${LIBRESSL}\crypto\crypto.dir\Release\vc142.pdb" `
229 		"${DEST}\crypto-46.pdb" -ErrorAction Stop
230 	Copy-Item "${SRC}\${LIBCBOR}\src\cbor.dir\Release\vc142.pdb" `
231 		"${DEST}\cbor.pdb" -ErrorAction Stop
232 	Copy-Item "${SRC}\${ZLIB}\zlib.dir\Release\vc142.pdb" `
233 		"${DEST}\zlib.pdb" -ErrorAction Stop
234 	Copy-Item "${SRC}\src\fido2_shared.dir\Release\vc142.pdb" `
235 		"${DEST}\fido2.pdb" -ErrorAction Stop
236 }
237 
238 Function Package-Tools(${SRC}, ${DEST}) {
239 	Copy-Item "${SRC}\tools\Release\fido2-assert.exe" `
240 		"${DEST}\fido2-assert.exe" -ErrorAction stop
241 	Copy-Item "${SRC}\tools\Release\fido2-cred.exe" `
242 		"${DEST}\fido2-cred.exe" -ErrorAction stop
243 	Copy-Item "${SRC}\tools\Release\fido2-token.exe" `
244 		"${DEST}\fido2-token.exe" -ErrorAction stop
245 }
246 
247 Push-Location ${BUILD}\64\dynamic
248 Build ${OUTPUT}\64\dynamic "Visual Studio 16 2019" "x64" "ON" "/MD"
249 Pop-Location
250 Push-Location ${BUILD}\32\dynamic
251 Build ${OUTPUT}\32\dynamic "Visual Studio 16 2019" "Win32" "ON" "/MD"
252 Pop-Location
253 
254 Push-Location ${BUILD}\64\static
255 Build ${OUTPUT}\64\static "Visual Studio 16 2019" "x64" "OFF" "/MT"
256 Pop-Location
257 Push-Location ${BUILD}\32\static
258 Build ${OUTPUT}\32\static "Visual Studio 16 2019" "Win32" "OFF" "/MT"
259 Pop-Location
260 
261 Package-Headers
262 
263 Package-Dynamic ${OUTPUT}\64\dynamic ${OUTPUT}\pkg\Win64\Release\v142\dynamic
264 Package-PDBs ${BUILD}\64\dynamic ${OUTPUT}\pkg\Win64\Release\v142\dynamic
265 Package-Tools ${BUILD}\64\dynamic ${OUTPUT}\pkg\Win64\Release\v142\dynamic
266 
267 Package-Dynamic ${OUTPUT}\32\dynamic ${OUTPUT}\pkg\Win32\Release\v142\dynamic
268 Package-PDBs ${BUILD}\32\dynamic ${OUTPUT}\pkg\Win32\Release\v142\dynamic
269 Package-Tools ${BUILD}\32\dynamic ${OUTPUT}\pkg\Win32\Release\v142\dynamic
270 
271 Package-Static ${OUTPUT}\64\static ${OUTPUT}\pkg\Win64\Release\v142\static
272 Package-Static ${OUTPUT}\32\static ${OUTPUT}\pkg\Win32\Release\v142\static
273