|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Builds a version of libgit2 and copies it to the nuget packaging directory. |
| 4 | +.PARAMETER vs |
| 5 | + Version of Visual Studio project files to generate. Cmake supports "10" (default), "11" and "12". |
| 6 | +.PARAMETER test |
| 7 | + If set, run the libgit2 tests on the desired version. |
| 8 | +.PARAMETER debug |
| 9 | + If set, build the "Debug" configuration of libgit2, rather than "RelWithDebInfo" (default). |
| 10 | +#> |
| 11 | + |
| 12 | +Param( |
| 13 | + [string]$vs = '10', |
| 14 | + [switch]$test, |
| 15 | + [switch]$debug |
| 16 | +) |
| 17 | + |
| 18 | +Set-StrictMode -Version Latest |
| 19 | + |
| 20 | +$projectDirectory = Split-Path $MyInvocation.MyCommand.Path |
| 21 | +$libgit2Directory = Join-Path $projectDirectory "libgit2" |
| 22 | +$x86Directory = Join-Path $projectDirectory "nuget.package\libgit2\windows\x86" |
| 23 | +$x64Directory = Join-Path $projectDirectory "nuget.package\libgit2\windows\amd64" |
| 24 | +$hashFile = Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt" |
| 25 | +$sha = Get-Content $hashFile |
| 26 | +$binaryFilename = "git2-" + $sha.Substring(0,7) |
| 27 | + |
| 28 | +$build_clar = 'OFF' |
| 29 | +if ($test.IsPresent) { $build_clar = 'ON' } |
| 30 | + |
| 31 | +$configuration = "RelWithDebInfo" |
| 32 | +if ($debug.IsPresent) { $configuration = "Debug" } |
| 33 | + |
| 34 | +function Run-Command([scriptblock]$Command, [switch]$Fatal, [switch]$Quiet) { |
| 35 | + $output = "" |
| 36 | + if ($Quiet) { |
| 37 | + $output = & $Command 2>&1 |
| 38 | + } else { |
| 39 | + & $Command |
| 40 | + } |
| 41 | + |
| 42 | + if (!$Fatal) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + $exitCode = 0 |
| 47 | + if ($LastExitCode -ne 0) { |
| 48 | + $exitCode = $LastExitCode |
| 49 | + } elseif (!$?) { |
| 50 | + $exitCode = 1 |
| 51 | + } else { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + $error = "``$Command`` failed" |
| 56 | + if ($output) { |
| 57 | + Write-Host -ForegroundColor yellow $output |
| 58 | + $error += ". See output above." |
| 59 | + } |
| 60 | + Throw $error |
| 61 | +} |
| 62 | + |
| 63 | +function Find-CMake { |
| 64 | + # Look for cmake.exe in $Env:PATH. |
| 65 | + $cmake = @(Get-Command cmake.exe)[0] 2>$null |
| 66 | + if ($cmake) { |
| 67 | + $cmake = $cmake.Definition |
| 68 | + } else { |
| 69 | + # Look for the highest-versioned cmake.exe in its default location. |
| 70 | + $cmake = @(Resolve-Path (Join-Path ${Env:ProgramFiles(x86)} "CMake *\bin\cmake.exe")) |
| 71 | + if ($cmake) { |
| 72 | + $cmake = $cmake[-1].Path |
| 73 | + } |
| 74 | + } |
| 75 | + if (!$cmake) { |
| 76 | + throw "Error: Can't find cmake.exe" |
| 77 | + } |
| 78 | + $cmake |
| 79 | +} |
| 80 | + |
| 81 | +function Ensure-Property($expected, $propertyValue, $propertyName, $path) { |
| 82 | + if ($propertyValue -eq $expected) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + throw "Error: Invalid '$propertyName' property in generated '$path' (Expected: $expected - Actual: $propertyValue)" |
| 87 | +} |
| 88 | + |
| 89 | +function Assert-Consistent-Naming($expected, $path) { |
| 90 | + $dll = get-item $path |
| 91 | + |
| 92 | + Ensure-Property $expected $dll.Name "Name" $dll.Fullname |
| 93 | + Ensure-Property $expected $dll.VersionInfo.InternalName "VersionInfo.InternalName" $dll.Fullname |
| 94 | + Ensure-Property $expected $dll.VersionInfo.OriginalFilename "VersionInfo.OriginalFilename" $dll.Fullname |
| 95 | +} |
| 96 | + |
| 97 | +try { |
| 98 | + Push-Location $libgit2Directory |
| 99 | + |
| 100 | + $cmake = Find-CMake |
| 101 | + $ctest = Join-Path (Split-Path -Parent $cmake) "ctest.exe" |
| 102 | + |
| 103 | + Write-Output "Building 32-bit..." |
| 104 | + Run-Command -Quiet { & remove-item build -recurse -force } |
| 105 | + Run-Command -Quiet { & mkdir build } |
| 106 | + cd build |
| 107 | + Run-Command -Quiet -Fatal { & $cmake -G "Visual Studio $vs" -D ENABLE_TRACE=ON -D "BUILD_CLAR=$build_clar" -D "LIBGIT2_FILENAME=$binaryFilename" -DSTDCALL=ON .. } |
| 108 | + Run-Command -Quiet -Fatal { & $cmake --build . --config $configuration } |
| 109 | + if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } } |
| 110 | + cd $configuration |
| 111 | + Assert-Consistent-Naming "$binaryFilename.dll" "*.dll" |
| 112 | + Run-Command -Quiet { & rm *.exp } |
| 113 | + Run-Command -Quiet { & rm $x86Directory\* } |
| 114 | + Run-Command -Quiet { & mkdir -fo $x86Directory } |
| 115 | + Run-Command -Quiet -Fatal { & copy -fo * $x86Directory -Exclude *.lib } |
| 116 | + |
| 117 | + Write-Output "Building 64-bit..." |
| 118 | + cd .. |
| 119 | + Run-Command -Quiet { & mkdir build64 } |
| 120 | + cd build64 |
| 121 | + Run-Command -Quiet -Fatal { & $cmake -G "Visual Studio $vs Win64" -D THREADSAFE=ON -D ENABLE_TRACE=ON -D "BUILD_CLAR=$build_clar" -D "LIBGIT2_FILENAME=$binaryFilename" -DSTDCALL=ON ../.. } |
| 122 | + Run-Command -Quiet -Fatal { & $cmake --build . --config $configuration } |
| 123 | + if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } } |
| 124 | + cd $configuration |
| 125 | + Assert-Consistent-Naming "$binaryFilename.dll" "*.dll" |
| 126 | + Run-Command -Quiet { & rm *.exp } |
| 127 | + Run-Command -Quiet { & rm $x64Directory\* } |
| 128 | + Run-Command -Quiet { & mkdir -fo $x64Directory } |
| 129 | + Run-Command -Quiet -Fatal { & copy -fo * $x64Directory -Exclude *.lib } |
| 130 | + |
| 131 | + Write-Output "Done!" |
| 132 | +} |
| 133 | +finally { |
| 134 | + Pop-Location |
| 135 | +} |
0 commit comments