forked from libgit2/libgit2sharp.nativebinaries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.libgit2.ps1
More file actions
369 lines (305 loc) · 13.6 KB
/
build.libgit2.ps1
File metadata and controls
369 lines (305 loc) · 13.6 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<#
.SYNOPSIS
Builds a version of libgit2 and copies it to the nuget packaging directory.
.PARAMETER test
If set, run the libgit2 tests on the desired version.
.PARAMETER debug
If set, build the "Debug" configuration of libgit2, rather than "Release" (default).
.PARAMETER libssh2
If set, build external zlib, libssh2.
.PARAMETER embed
If set, build embedded libssh2.
.PARAMETER x86
If set, the x86 version will be built.
.PARAMETER x64
If set, the x64 version will be built.
.PARAMETER arm64
If set, the arm64 version will be built.
#>
Param(
[switch]$test,
[switch]$debug,
[switch]$libssh2,
[switch]$embed,
[switch]$x86,
[switch]$x64,
[switch]$arm64
)
Set-StrictMode -Version Latest
$vs = '17' # Visual Studio 2022
$build_clar = 'OFF'
$projectDirectory = Split-Path $MyInvocation.MyCommand.Path
$libgit2Directory = Join-Path $projectDirectory "libgit2"
$libssh2Directory = Join-Path $projectDirectory "libssh2"
$zlibDirectory = Join-Path $projectDirectory "zlib"
$x86Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-x86\native"
$x64Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-x64\native"
$arm64Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-arm64\native"
$depsDirectory = Join-Path $projectDirectory "deps"
$libgit2HashFile = Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt"
$libssh2HashFile = Join-Path $projectDirectory "nuget.package\libgit2\libssh2_hash.txt"
$zlibHashFile = Join-Path $projectDirectory "nuget.package\libgit2\zlib_hash.txt"
$libgit2Sha = Get-Content $libgit2HashFile
$libssh2Sha = Get-Content $libssh2HashFile
$zlibSha = Get-Content $zlibHashFile
$libgit2BinaryFilename = "git2-ssh-" + $libgit2Sha.Substring(0,7)
$libssh2BinaryFilename = "libssh2"
$zlibBinaryFilename = "zlib"
$build_tests = 'OFF'
if ($test.IsPresent) { $build_tests = 'ON' }
$configuration = "Release"
if ($debug.IsPresent) { $configuration = "Debug" }
$libssh2_embed = 'OFF'
if ($libssh2.IsPresent -And $embed) { $libssh2_embed = $libssh2Directory -replace "\\", "/" }
function Run-Command([scriptblock]$Command, [switch]$Fatal, [switch]$Quiet) {
$output = ""
if ($Quiet) {
$output = & $Command 2>&1
} else {
& $Command
}
if (!$Fatal) {
return
}
$exitCode = 0
if ($LastExitCode -ne 0) {
$exitCode = $LastExitCode
} elseif (!$?) {
$exitCode = 1
} else {
return
}
$error = "``$Command`` failed"
if ($output) {
Write-Host -ForegroundColor yellow $output
$error += ". See output above."
}
Throw $error
}
function Find-CMake {
# Look for cmake.exe in $Env:PATH.
$cmake = @(Get-Command cmake.exe)[0] 2>$null
if ($cmake) {
$cmake = $cmake.Definition
} else {
# Look for the highest-versioned cmake.exe in its default location.
$cmake = @(Resolve-Path (Join-Path ${Env:ProgramFiles(x86)} "CMake *\bin\cmake.exe"))
if ($cmake) {
$cmake = $cmake[-1].Path
}
}
if (!$cmake) {
throw "Error: Can't find cmake.exe"
}
$cmake
}
function Ensure-Property($expected, $propertyValue, $propertyName, $path) {
if ($propertyValue -eq $expected) {
return
}
throw "Error: Invalid '$propertyName' property in generated '$path' (Expected: $expected - Actual: $propertyValue)"
}
function Assert-Consistent-Naming($expected, $path) {
$dll = get-item $path
Ensure-Property $expected $dll.Name "Name" $dll.Fullname
Ensure-Property $expected $dll.VersionInfo.InternalName "VersionInfo.InternalName" $dll.Fullname
Ensure-Property $expected $dll.VersionInfo.OriginalFilename "VersionInfo.OriginalFilename" $dll.Fullname
}
function Build-Zlib([switch]$x64) {
$architecture = "32-bit"
$arch = "x86"
$gen = "Visual Studio $vs"
$outputDirectory = $x86Directory
if ($x64) {
$architecture = "64-bit"
$arch = "x64"
# $gen = "Visual Studio $vs Win64"
$outputDirectory = $x64Directory
}
Push-Location $zlibDirectory
Write-Output "Building $architecture zlib..."
Run-Command -Quiet { & remove-item build/$arch -recurse -force }
Run-Command -Quiet { & remove-item install/$arch -recurse -force }
Run-Command -Quiet { & mkdir build/$arch }
Push-Location build/$arch
# Make STDCALL and static linked CRT
Run-Command -Quiet -Fatal { & $cmake -G $gen -DCMAKE_C_FLAGS="/DWIN32 /D_WINDOWS /W3 /Gz" -DCMAKE_C_FLAGS_DEBUG="/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1" -DCMAKE_C_FLAGS_MINSIZEREL="/MT /O1 /Ob1 /D NDEBUG" -DCMAKE_FLAGS_RELEASE="/MT /O2 /Ob2 /D NDEBUG" -DCMAKE_C_FLAGS_RELWITHDEBINFO="/MT /Zi /O2 /Ob1 /D NDEBUG" -D "CMAKE_INSTALL_PREFIX=$zlibDirectory/install/$arch" ../.. }
Run-Command -Quiet -Fatal { & $cmake --build . --config $configuration --target install }
Pop-Location
# Clear deps
Run-Command -Quiet { & remove-item $depsDirectory/$arch/zlib -recurse -force }
Run-Command -Quiet { & mkdir $depsDirectory/$arch/zlib/include }
# Prepare to publish libraries / binaries
Run-Command -Quiet -Fatal { & copy -fo $zlibDirectory/install/$arch/lib/zlib.lib $depsDirectory/$arch/zlib/$zlibBinaryFilename.lib }
Run-Command -Quiet -Fatal { & copy -fo $zlibDirectory/install/$arch/include/* $depsDirectory/$arch/zlib/include }
Run-Command -Quiet -Fatal { & copy -fo $zlibDirectory/install/$arch/bin/zlib.dll $outputDirectory/$zlibBinaryFilename.dll }
if ($configuration -eq "RelWithDebInfo" -Or $configuration -eq "Debug") {
Run-Command -Quiet -Fatal { & copy -fo $zlibDirectory/build/$arch/$configuration/zlib.pdb $outputDirectory/$zlibBinaryFilename.pdb }
}
# Clear submodule
Run-Command -Quiet { & remove-item $zlibDirectory/build -recurse -force }
Run-Command -Quiet { & remove-item $zlibDirectory/install -recurse -force }
Run-Command -Quiet { & rename-item -path $zlibDirectory/zconf.h.included -newName $zlibDirectory/zconf.h -force }
Pop-Location
}
function Build-Libssh2([switch]$x64) {
$architecture = "32-bit"
$arch = "x86"
$gen = "Visual Studio $vs"
$binDirectory = $x86Directory
if ($x64) {
$architecture = "64-bit"
$arch = "x64"
# $gen = "Visual Studio $vs Win64"
$binDirectory = $x64Directory
}
$libssh2Dir = $libssh2Directory -replace "\\", "/"
$zlibDir = "$depsDirectory/$arch/zlib" -replace "\\", "/"
Push-Location $libssh2Directory
Write-Output "Building $architecture libssh2..."
Run-Command -Quiet { & remove-item build/$arch -recurse -force }
Run-Command -Quiet { & remove-item install/$arch -recurse -force }
Run-Command -Quiet { & mkdir build/$arch }
Push-Location build/$arch
# Make STDCALL and static linked CRT
Run-Command -Quiet -Fatal { & $cmake -G $gen -DCMAKE_C_FLAGS="/DWIN32 /D_WINDOWS /W3 /Gz" -DCMAKE_C_FLAGS_DEBUG="/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1" -DCMAKE_C_FLAGS_MINSIZEREL="/MT /O1 /Ob1 /D NDEBUG" -DCMAKE_FLAGS_RELEASE="/MT /O2 /Ob2 /D NDEBUG" -DCMAKE_C_FLAGS_RELWITHDEBINFO="/MT /Zi /O2 /Ob1 /D NDEBUG" -D "CMAKE_INSTALL_PREFIX=$libssh2Dir/install/$arch" -D BUILD_TESTING=ON -D BUILD_SHARED_LIBS=ON -D ENABLE_ZLIB_COMPRESSION=ON -D "ZLIB_LIBRARY=$zlibDir/$zlibBinaryFilename.lib" -D "ZLIB_INCLUDE_DIR=$zlibDir/include" -D "CRYPTO_BACKEND=WinCNG" ../.. }
Run-Command -Quiet -Fatal { & $cmake --build . --config $configuration --target install }
Pop-Location
# Clear deps
Run-Command -Quiet { & remove-item $depsDirectory/$arch/libssh2 -recurse -force }
Run-Command -Quiet { & mkdir $depsDirectory/$arch/libssh2/include }
# Prepare to publish libraries / binaries
Run-Command -Quiet -Fatal { & copy -fo $libssh2Dir/install/$arch/lib/libssh2.lib $depsDirectory/$arch/libssh2/$libssh2BinaryFilename.lib }
Run-Command -Quiet -Fatal { & copy -fo $libssh2Dir/install/$arch/include/* $depsDirectory/$arch/libssh2/include }
Run-Command -Quiet -Fatal { & copy -fo $libssh2Dir/install/$arch/bin/libssh2.dll $binDirectory/$libssh2BinaryFilename.dll }
if ($configuration -eq "RelWithDebInfo" -Or $configuration -eq "Debug") {
Run-Command -Quiet -Fatal { & copy -fo $libssh2Dir/build/$arch/src/$configuration/libssh2.pdb $binDirectory/$libssh2BinaryFilename.pdb }
}
# Clear submodule
Run-Command -Quiet { & remove-item $libssh2Dir/build -recurse -force }
Run-Command -Quiet { & remove-item $libssh2Dir/install -recurse -force }
Pop-Location
}
function Build-Libgit2([switch]$x64, [switch]$extZlib) {
$architecture = "32-bit"
$arch = "x86"
$platform = "Win32"
$build = "build"
$outputDirectory = $x86Directory
$root = ".."
$zlibDir = "../../zlib"
$gen = "Visual Studio $vs"
if ($x64) {
$architecture = "64-bit"
$arch = "x64"
$platform = "x64"
$build = "build/build64"
$outputDirectory = $x64Directory
$root = "../.."
$zlibDir = "../../../zlib"
# $gen = "Visual Studio $vs Win64"
}
$libgit2Dir = $libgit2Directory -replace "\\", "/"
$libssh2Dir = "$depsDirectory/$arch/libssh2" -replace "\\", "/"
$zlibDir = "$depsDirectory/$arch/zlib" -replace "\\", "/"
Push-Location $libgit2Directory
Write-Output "Building $architecture libgit2..."
Run-Command -Quiet { & remove-item $build -recurse -force -ErrorAction Ignore }
Run-Command -Quiet { & mkdir $build }
cd $build
if ($extZlib) {
Run-Command -Fatal { & $cmake -G $gen -D USE_SSH=ON -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$libgit2BinaryFilename" -D "ZLIB_LIBRARY_RELEASE=$zlibDir/$zlibBinaryFilename.lib" -D "ZLIB_INCLUDE_DIR=$zlibDir/include" -D "LIBSSH2_INCLUDE_DIR=$libssh2Dir/include" -D "LIBSSH2_LIBRARY=$libssh2Dir/$libssh2BinaryFilename.lib" $root }
} else {
Run-Command -Fatal { & $cmake -A $platform -D USE_SSH=ON -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$libgit2BinaryFilename" -D "EMBED_SSH_PATH=$libssh2_embed" $root }
}
Run-Command -Quiet -Fatal { & $cmake --build . --config $configuration }
if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
cd $configuration
Assert-Consistent-Naming "$libgit2BinaryFilename.dll" "*.dll"
Run-Command -Quiet { & rm *.exp }
# Run-Command -Quiet { & rm $outputDirectory\* -ErrorAction Ignore }
# Run-Command -Quiet { & mkdir -fo $outputDirectory }
Run-Command -Quiet -Fatal { & copy -fo * $outputDirectory -Exclude *.lib }
Pop-Location
}
try {
if ((!$x86.isPresent -and !$x64.IsPresent) -and !$arm64.IsPresent) {
Write-Output -Stderr "Error: usage $MyInvocation.MyCommand [-x86] [-x64] [-arm64]"
Exit
}
Push-Location $libgit2Directory
$cmake = Find-CMake
$ctest = Join-Path (Split-Path -Parent $cmake) "ctest.exe"
# Run-Command -Quiet { & remove-item build -recurse -force -ErrorAction Ignore }
# Run-Command -Quiet { & mkdir build }
# cd build
# if ($x86.IsPresent) {
# Write-Output "Building x86..."
# Run-Command -Fatal { & $cmake -A Win32 -D USE_SSH=OFF -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$binaryFilename" .. }
# Run-Command -Fatal { & $cmake --build . --config $configuration }
# if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
# cd $configuration
# Assert-Consistent-Naming "$binaryFilename.dll" "*.dll"
# Run-Command -Quiet { & rm *.exp }
# Run-Command -Quiet { & rm $x86Directory\* -ErrorAction Ignore }
# Run-Command -Quiet { & mkdir -fo $x86Directory }
# Run-Command -Quiet -Fatal { & copy -fo * $x86Directory -Exclude *.lib }
# cd ..
# }
# if ($x64.IsPresent) {
# Write-Output "Building x64..."
# Run-Command -Quiet { & mkdir build64 }
# cd build64
# Run-Command -Fatal { & $cmake -A x64 -D USE_SSH=OFF -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$binaryFilename" ../.. }
# Run-Command -Fatal { & $cmake --build . --config $configuration }
# if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
# cd $configuration
# Assert-Consistent-Naming "$binaryFilename.dll" "*.dll"
# Run-Command -Quiet { & rm *.exp }
# Run-Command -Quiet { & rm $x64Directory\* -ErrorAction Ignore }
# Run-Command -Quiet { & mkdir -fo $x64Directory }
# Run-Command -Quiet -Fatal { & copy -fo * $x64Directory -Exclude *.lib }
# }
# if ($arm64.IsPresent) {
# Write-Output "Building arm64..."
# Run-Command -Quiet { & mkdir buildarm64 }
# cd buildarm64
# Run-Command -Fatal { & $cmake -A ARM64 -D USE_SSH=OFF -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$binaryFilename" ../.. }
# Run-Command -Fatal { & $cmake --build . --config $configuration }
# if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
# cd $configuration
# Assert-Consistent-Naming "$binaryFilename.dll" "*.dll"
# Run-Command -Quiet { & rm *.exp }
# Run-Command -Quiet { & rm $arm64Directory\* -ErrorAction Ignore }
# Run-Command -Quiet { & mkdir -fo $arm64Directory }
# Run-Command -Quiet -Fatal { & copy -fo * $arm64Directory -Exclude *.lib }
# }
if ($x86.isPresent) {
# Building 32-bit
Run-Command -Quiet { & rm $x86Directory\* }
Run-Command -Quiet { & mkdir -fo $x86Directory }
if ($libssh2.IsPresent -And !$embed) {
Build-Zlib
Build-Libssh2
Build-Libgit2 -extZlib
} else {
Build-Libgit2
}
}
if ($x64.isPresent) {
# Building 64-bit
Run-Command -Quiet { & rm $x64Directory\* }
Run-Command -Quiet { & mkdir -fo $x64Directory }
if ($libssh2.IsPresent -And !$embed) {
Build-Zlib -x64
Build-Libssh2 -x64
Build-Libgit2 -x64 -extZlib
} else {
Build-Libgit2 -x64
}
}
Write-Output "Done!"
}
finally {
Pop-Location
}