Skip to content

Create LibGit2Sharp.NativeBinaries package #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.nupkg
*.dll
*.pdb
*.dylib
*.so
*.zip

# nuget package placeholder
*.here
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libgit2"]
path = libgit2
url = https://github.com/libgit2/libgit2.git
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: c

os:
- osx
- linux

branches:
only:
- master

before_install:
- date -u
- uname -a
- env | grep -v "BINTRAY_API_KEY" | sort

install: true

script: ./build.libgit2.sh

after_success: ./uploadbinaries.sh
Binary file added NuGet.exe
Binary file not shown.
129 changes: 129 additions & 0 deletions UpdateLibgit2ToSha.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<#
.SYNOPSIS
Updates the libgit2 submodule to the specified commit and updates libgit2_hash.txt and NativeBinaries.props with the new hash value.
.PARAMETER sha
Desired libgit2 version. This is run through `git rev-parse`, so branch names are okay too.
#>

Param(
[string]$sha = 'HEAD'
)

Set-StrictMode -Version Latest

$self = Split-Path -Leaf $MyInvocation.MyCommand.Path
$projectDirectory = Split-Path $MyInvocation.MyCommand.Path
$libgit2Directory = Join-Path $projectDirectory "libgit2"

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-Git {
$git = @(Get-Command git)[0] 2>$null
if ($git) {
$git = $git.Definition
Write-Host -ForegroundColor Gray "Using git: $git"
& $git --version | write-host -ForegroundColor Gray
return $git
}
throw "Error: Can't find git"
}

Push-Location $libgit2Directory

& {
trap {
Pop-Location
break
}

$git = Find-Git

Write-Output "Fetching..."
Run-Command -Quiet { & $git fetch }

Write-Output "Verifying $sha..."
$sha = & $git rev-parse $sha
if ($LASTEXITCODE -ne 0) {
write-host -foregroundcolor red "Error: invalid SHA. USAGE: $self <SHA>"
popd
break
}

Write-Output "Checking out $sha..."
Run-Command -Quiet -Fatal { & $git checkout $sha }

Pop-Location

sc -Encoding ASCII (Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt") $sha

$binaryFilename = "git2-" + $sha.Substring(0,7)

$buildProperties = @"
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<EmbeddedResource Include="`$(MSBuildThisFileDirectory)\..\libgit2\libgit2_hash.txt" />
</ItemGroup>
<ItemGroup Condition=" '`$(OS)' == 'Windows_NT' ">
<None Include="`$(MSBuildThisFileDirectory)\..\libgit2\windows\amd64\$binaryFilename.dll">
<Link>NativeBinaries\amd64\$binaryFilename.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="`$(MSBuildThisFileDirectory)\..\libgit2\windows\amd64\$binaryFilename.pdb">
<Link>NativeBinaries\amd64\$binaryFilename.pdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="`$(MSBuildThisFileDirectory)\..\libgit2\windows\x86\$binaryFilename.dll">
<Link>NativeBinaries\x86\$binaryFilename.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="`$(MSBuildThisFileDirectory)\..\libgit2\windows\x86\$binaryFilename.pdb">
<Link>NativeBinaries\x86\$binaryFilename.pdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup Condition=" '`$(OS)' == 'Unix' And Exists('/Library/Frameworks') ">
<None Include="`$(MSBuildThisFileDirectory)\..\libgit2\osx\lib$binaryFilename.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup Condition=" '`$(OS)' == 'Unix' And !Exists('/Library/Frameworks') ">
<None Include="`$(MSBuildThisFileDirectory)\..\libgit2\linux\amd64\lib$binaryFilename.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
"@

sc -Encoding UTF8 (Join-Path $projectDirectory "nuget.package\build\LibGit2Sharp.NativeBinaries.props") $buildProperties

Write-Output "Done!"
}
exit
23 changes: 23 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: 1.0.{build}

skip_tags: true

install:
- git submodule update --init --recursive

build_script:
- ps: .\build.libgit2.ps1 -vs 12

after_build:
- ps: |
$pre = $true
if (($env:APPVEYOR_PULL_REQUEST_NUMBER -eq $null) -and ($env:APPVEYOR_REPO_BRANCH -eq "master"))
{
$pre = $false
}
.\buildpackage.ps1 $env:APPVEYOR_BUILD_VERSION -pre:$pre

test: off

artifacts:
- path: '*.nupkg'
135 changes: 135 additions & 0 deletions build.libgit2.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<#
.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 = '10',
[switch]$test,
[switch]$debug
)

Set-StrictMode -Version Latest

$projectDirectory = Split-Path $MyInvocation.MyCommand.Path
$libgit2Directory = Join-Path $projectDirectory "libgit2"
$x86Directory = Join-Path $projectDirectory "nuget.package\libgit2\windows\x86"
$x64Directory = Join-Path $projectDirectory "nuget.package\libgit2\windows\amd64"
$hashFile = Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt"
$sha = Get-Content $hashFile
$binaryFilename = "git2-" + $sha.Substring(0,7)

$build_clar = 'OFF'
if ($test.IsPresent) { $build_clar = 'ON' }

$configuration = "RelWithDebInfo"
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
}

try {
Push-Location $libgit2Directory

$cmake = Find-CMake
$ctest = Join-Path (Split-Path -Parent $cmake) "ctest.exe"

Write-Output "Building 32-bit..."
Run-Command -Quiet { & remove-item build -recurse -force }
Run-Command -Quiet { & mkdir build }
cd build
Run-Command -Quiet -Fatal { & $cmake -G "Visual Studio $vs" -D ENABLE_TRACE=ON -D "BUILD_CLAR=$build_clar" -D "LIBGIT2_FILENAME=$binaryFilename" -DSTDCALL=ON .. }
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 $x86Directory\* }
Run-Command -Quiet { & mkdir -fo $x86Directory }
Run-Command -Quiet -Fatal { & copy -fo * $x86Directory -Exclude *.lib }

Write-Output "Building 64-bit..."
cd ..
Run-Command -Quiet { & mkdir build64 }
cd build64
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 ../.. }
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 $x64Directory\* }
Run-Command -Quiet { & mkdir -fo $x64Directory }
Run-Command -Quiet -Fatal { & copy -fo * $x64Directory -Exclude *.lib }

Write-Output "Done!"
}
finally {
Pop-Location
}
46 changes: 46 additions & 0 deletions build.libgit2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

LIBGIT2SHA=`cat ./nuget.package/libgit2/libgit2_hash.txt`
SHORTSHA=${LIBGIT2SHA:0:7}

rm -rf libgit2/build
mkdir libgit2/build
pushd libgit2/build

export _BINPATH=`pwd`

cmake -DCMAKE_BUILD_TYPE:STRING=Release \
-DBUILD_CLAR:BOOL=OFF \
-DUSE_SSH=OFF \
-DENABLE_TRACE=ON \
-DLIBGIT2_FILENAME=git2-$SHORTSHA \
-DCMAKE_OSX_ARCHITECTURES="i386;x86_64" \
..
cmake --build .

popd

OS=`uname`
ARCH=`uname -m`

PACKAGEPATH="nuget.package/libgit2"
LIBEXT="so"

if [ $OS == "Linux" ]; then
OSPATH="/linux"
if [ $ARCH == "x86_64" ]; then
ARCHPATH="/amd64"
fi
elif [ $OS == "Darwin" ]; then
OSPATH="/osx"
LIBEXT="dylib"
else
OSPATH="/unix"
fi

rm -rf $PACKAGEPATH$OSPATH
mkdir -p $PACKAGEPATH$OSPATH$ARCHPATH

cp libgit2/build/libgit2-$SHORTSHA.$LIBEXT $PACKAGEPATH$OSPATH$ARCHPATH

exit $?
Loading