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
226 lines (197 loc) · 8.33 KB
/
build.libgit2.ps1
File metadata and controls
226 lines (197 loc) · 8.33 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
<#
.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 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]$x86,
[switch]$x64,
[switch]$arm64
)
Set-StrictMode -Version Latest
$projectDirectory = Split-Path $MyInvocation.MyCommand.Path
$libgit2Directory = Join-Path $projectDirectory "libgit2"
$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"
$hashFile = Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt"
$sha = Get-Content $hashFile
$binaryFilename = "git2-" + $sha.Substring(0,7)
$build_tests = 'OFF'
if ($test.IsPresent) { $build_tests = 'ON' }
$configuration = "Release"
if ($debug.IsPresent) { $configuration = "Debug" }
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 Install-Libssh2($arch) {
$triplet = "$arch-windows"
$vcpkg = Join-Path $Env:VCPKG_INSTALLATION_ROOT "vcpkg.exe"
if (-not (Test-Path $vcpkg)) {
throw "Error: vcpkg not found at $Env:VCPKG_INSTALLATION_ROOT"
}
# Install libssh2 with the WinCNG crypto backend (no OpenSSL dependency).
# 'openssl' is a default vcpkg feature for libssh2, so manifest mode is required
# to override it — classic mode has no --no-default-features flag.
# The overlay triplet injects -DENABLE_ECDSA_WINCNG=ON into every package's
# cmake configure step (zlib ignores it; libssh2 uses it).
$manifestDir = Join-Path $projectDirectory "libssh2-wincng-manifest"
New-Item -ItemType Directory -Force -Path $manifestDir | Out-Null
@"
{
"name": "libssh2-wincng",
"version": "1.0.0",
"dependencies": [
{
"name": "libssh2",
"default-features": false,
"features": ["zlib"]
}
]
}
"@ | Set-Content (Join-Path $manifestDir "vcpkg.json")
$installRoot = Join-Path $projectDirectory "libssh2-wincng-installed"
$overlayTriplets = Join-Path $projectDirectory "libssh2-wincng-triplets"
Write-Host "Installing libssh2 (WinCNG + ECDSA) for $triplet via vcpkg..."
Push-Location $manifestDir
try {
Run-Command -Fatal -Quiet { & $vcpkg install --vcpkg-root $Env:VCPKG_INSTALLATION_ROOT --triplet $triplet "--x-install-root=$installRoot" "--overlay-triplets=$overlayTriplets" }
} finally {
Pop-Location
}
$installedDir = Join-Path $installRoot $triplet
return @{
IncludeDir = Join-Path $installedDir "include"
LibDir = Join-Path $installedDir "lib"
BinDir = Join-Path $installedDir "bin"
Prefix = $installedDir
}
}
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..."
$ssh2 = Install-Libssh2 "x86"
Run-Command -Fatal { & $cmake -A Win32 -D USE_SSH=ON -D USE_HTTPS=Schannel -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$binaryFilename" -D "CMAKE_PREFIX_PATH=$($ssh2.Prefix)" .. }
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 }
Run-Command -Quiet -Fatal { & copy -fo (Join-Path $ssh2.BinDir "*.dll") $x86Directory }
if (-not (Test-Path (Join-Path $x86Directory "libssh2.dll"))) { throw "Error: libssh2.dll was not copied to $x86Directory" }
cd ..
}
if ($x64.IsPresent) {
Write-Output "Building x64..."
$ssh2 = Install-Libssh2 "x64"
Run-Command -Quiet { & mkdir build64 }
cd build64
Run-Command -Fatal { & $cmake -A x64 -D USE_SSH=ON -D USE_HTTPS=Schannel -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$binaryFilename" -D "CMAKE_PREFIX_PATH=$($ssh2.Prefix)" ../.. }
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 }
Run-Command -Quiet -Fatal { & copy -fo (Join-Path $ssh2.BinDir "*.dll") $x64Directory }
if (-not (Test-Path (Join-Path $x64Directory "libssh2.dll"))) { throw "Error: libssh2.dll was not copied to $x64Directory" }
}
if ($arm64.IsPresent) {
Write-Output "Building arm64..."
$ssh2 = Install-Libssh2 "arm64"
Run-Command -Quiet { & mkdir buildarm64 }
cd buildarm64
Run-Command -Fatal { & $cmake -A ARM64 -D USE_SSH=ON -D USE_HTTPS=Schannel -D "BUILD_TESTS=$build_tests" -D "BUILD_CLI=OFF" -D "LIBGIT2_FILENAME=$binaryFilename" -D "CMAKE_PREFIX_PATH=$($ssh2.Prefix)" ../.. }
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 }
Run-Command -Quiet -Fatal { & copy -fo (Join-Path $ssh2.BinDir "*.dll") $arm64Directory }
if (-not (Test-Path (Join-Path $arm64Directory "libssh2.dll"))) { throw "Error: libssh2.dll was not copied to $arm64Directory" }
}
Write-Output "Done!"
}
finally {
Pop-Location
}