forked from libgit2/libgit2sharp.nativebinaries
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.libgit2.ps1
More file actions
186 lines (158 loc) · 6.39 KB
/
Copy pathbuild.libgit2.ps1
File metadata and controls
186 lines (158 loc) · 6.39 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
<#
.SYNOPSIS
Builds a version of libgit2 and copies it to the nuget packaging directory.
.PARAMETER vs
Version of Visual Studio project files to generate. Cmake supports "10" (default), "11" and "12".
.PARAMETER test
If set, run the libgit2 tests on the desired version.
.PARAMETER debug
If set, build the "Debug" configuration of libgit2, rather than "RelWithDebInfo" (default).
#>
Param(
[string]$vs = '16 2019',
[string]$libgit2Name = '',
[switch]$test,
[switch]$debug
)
Set-StrictMode -Version Latest
$projectDirectory = Split-Path $MyInvocation.MyCommand.Path
$libgit2Directory = Join-Path $projectDirectory "libgit2"
$libssh2Directory = ([System.Uri](Join-Path $projectDirectory "libssh2")).AbsolutePath
$x86Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-x86\native"
$x64Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-x64\native"
$hashFile = Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt"
if (![string]::IsNullOrEmpty($libgit2Name)) {
$binaryFilename = $libgit2Name
} else {
$binaryFilename = "libgit2"
}
$build_clar = 'OFF'
if ($test.IsPresent) { $build_clar = 'ON' }
$configuration = "RelWithDebInfo"
if ($debug.IsPresent) { $configuration = "Debug" }
$libopensslDirectory = [IO.Path]::Combine($projectDirectory, "openssl", "build", $configuration)
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 Build-LibSsh($generator, $platform, $buildDir) {
cd $projectDirectory
cmd.exe /c build.openssl.cmd $configuration $platform
Run-Command -Quiet { & remove-item $buildDir -recurse -force }
[IO.Directory]::CreateDirectory($buildDir)
cd $buildDir
Write-Output "CONFIGURE LIBSSH2 $generator -A $platform $configuration..."
Run-Command -Quiet -Fatal { & $cmake -G $generator -A $platform -D "CMAKE_BUILD_TYPE=$configuration" -D ENABLE_TRACE=ON -D "BUILD_CLAR=$build_clar" -D "OPENSSL_ROOT_DIR=$libopensslDirectory/$platform" -D "CRYPTO_BACKEND=OpenSSL" -D "BUILD_SHARED_LIBS=1" -D "BUILD_TESTING=OFF" -D "BUILD_EXAMPLES=OFF" $libssh2Directory }
Write-Output "BUILD LIBSSH2..."
Run-Command -Quiet -Fatal { & $cmake --build . --config $configuration }
}
function Build-LibGit($generator, $platform, $nugetDir, $useSchannel, $useSshExe, $buildPlatform) {
$libsshBuildDir = "$libssh2Directory/build/$platform"
$libsshBinDir = "$libsshBuildDir/src/$configuration"
$libopensslBinDir = "$libopensslDirectory/$platform/bin"
$sshMethod = "libssh2"
if ($useSshExe) {
$sshMethod = "exec"
}
if ($buildPlatform) {
Write-Output "Building $platform..."
Build-LibSsh $generator $platform $libsshBuildDir
}
$buildDir = [IO.Path]::Combine( $libgit2Directory, "build", $platform)
Run-Command -Quiet { & remove-item $buildDir -recurse -force }
[IO.Directory]::CreateDirectory($buildDir)
cd $buildDir
$variantFilename = $binaryFileName
if ($useSchannel) {
$variantFilename = -join ($variantFilename, "_schannel")
}
if ($useSshExe) {
$variantFilename = -join ($variantFilename, "_ssh")
}
Write-Output "CONFIGURE LIBGIT... Schannel: $useSchannel"
$httpsConfig = "WinHTTP"
if ($useSchannel) {
$httpsConfig = "-D `"USE_HTTPS=Schannel`""
}
Run-Command -Fatal { & $cmake -G $generator -A $platform -D ENABLE_TRACE=ON -D "BUILD_CLAR=$build_clar" -D "BUILD_TESTS=OFF" -D "BUILD_CLI=OFF" $httpsConfig -D "LIBGIT2_FILENAME=$variantFilename" -D "USE_SSH=$sshMethod" -D "USE_BUNDLED_ZLIB=ON" -D "LIBSSH2_INCLUDE_DIRS=$libssh2Directory/include" -D "LIBSSH2_LIBRARIES=$libsshBinDir/libssh2.lib" -D "LIBSSH2_FOUND=TRUE" -D "OPENSSL_ROOT_DIR=$libopensslDirectory/$platform" $libgit2Directory }
Write-Output "BUILD LIBGIT..."
Run-Command -Quiet -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 $nugetDir\* }
Run-Command -Quiet { & mkdir -fo $nugetDir }
Run-Command -Quiet -Fatal { & copy -fo * $nugetDir -Exclude *.lib }
$opensslPlatformPostfix = ""
if ($platform -eq "x64") {
$opensslPlatformPostfix = "-x64"
}
Copy-Item $libsshBinDir/libssh2.dll -Destination $nugetDir -Force
Copy-Item $libopensslBinDir/libcrypto-3$opensslPlatformPostfix.dll -Destination $nugetDir -Force
}
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
}
try {
Push-Location $libgit2Directory
$cmake = Find-CMake
$ctest = Join-Path (Split-Path -Parent $cmake) "ctest.exe"
Build-LibGit "Visual Studio $vs" "x64" $x64Directory $false $false $true
Build-LibGit "Visual Studio $vs" "x64" $x64Directory $true $false $false
Build-LibGit "Visual Studio $vs" "x64" $x64Directory $false $true $false
Build-LibGit "Visual Studio $vs" "x64" $x64Directory $true $true $false
Write-Output "Done!"
}
finally {
Pop-Location
}