Skip to content

Commit 850ab5d

Browse files
committed
Bundle libssh2
1 parent ef39063 commit 850ab5d

4 files changed

Lines changed: 58 additions & 24 deletions

File tree

Dockerfile.linux

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
66
pkg-config \
77
libssl-dev \
88
libssh2-1-dev \
9+
patchelf \
910
ca-certificates \
1011
git \
1112
&& rm -rf /var/lib/apt/lists/*

Dockerfile.linux-musl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM alpine:3.19
22

3-
RUN apk add --no-cache bash build-base cmake openssl-dev libssh2-dev libssh2-static openssl-libs-static
3+
RUN apk add --no-cache bash build-base cmake openssl-dev libssh2-dev patchelf
44

55
WORKDIR /nativebinaries
66
COPY . /nativebinaries/

build.libgit2.ps1

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $x86Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-x86\nati
2929
$x64Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-x64\native"
3030
$arm64Directory = Join-Path $projectDirectory "nuget.package\runtimes\win-arm64\native"
3131
$hashFile = Join-Path $projectDirectory "nuget.package\libgit2\libgit2_hash.txt"
32-
$sha = Get-Content $hashFile
32+
$sha = Get-Content $hashFile
3333
$binaryFilename = "git2-" + $sha.Substring(0,7)
3434

3535
$build_tests = 'OFF'
@@ -108,9 +108,22 @@ function Install-Libssh2($triplet) {
108108
}
109109
Write-Output "Installing libssh2 for $triplet via vcpkg..."
110110
Run-Command -Fatal { & $vcpkg install "libssh2:$triplet" }
111-
}
112111

113-
$vcpkgToolchain = Join-Path $Env:VCPKG_INSTALLATION_ROOT "scripts\buildsystems\vcpkg.cmake"
112+
$installedDir = Join-Path $Env:VCPKG_INSTALLATION_ROOT "installed\$triplet"
113+
$libssh2Dll = Join-Path $installedDir "bin\libssh2.dll"
114+
$libssh2Lib = Join-Path $installedDir "lib\libssh2.lib"
115+
$libssh2Include = Join-Path $installedDir "include"
116+
117+
if (-not (Test-Path $libssh2Dll)) {
118+
throw "Error: libssh2.dll not found at $libssh2Dll"
119+
}
120+
121+
return @{
122+
Dll = $libssh2Dll
123+
Library = $libssh2Lib
124+
IncludeDir = $libssh2Include
125+
}
126+
}
114127

115128
try {
116129
if ((!$x86.isPresent -and !$x64.IsPresent) -and !$arm64.IsPresent) {
@@ -128,9 +141,10 @@ try {
128141
cd build
129142

130143
if ($x86.IsPresent) {
131-
Install-Libssh2 "x86-windows-static"
144+
$ssh2 = Install-Libssh2 "x86-windows"
145+
$vcpkgToolchain = Join-Path $Env:VCPKG_INSTALLATION_ROOT "scripts\buildsystems\vcpkg.cmake"
132146
Write-Output "Building x86..."
133-
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_TOOLCHAIN_FILE=$vcpkgToolchain" -D "VCPKG_TARGET_TRIPLET=x86-windows-static" .. }
147+
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_TOOLCHAIN_FILE=$vcpkgToolchain" -D "VCPKG_TARGET_TRIPLET=x86-windows" .. }
134148
Run-Command -Fatal { & $cmake --build . --config $configuration }
135149
if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
136150
cd $configuration
@@ -139,15 +153,18 @@ try {
139153
Run-Command -Quiet { & rm $x86Directory\* -ErrorAction Ignore }
140154
Run-Command -Quiet { & mkdir -fo $x86Directory }
141155
Run-Command -Quiet -Fatal { & copy -fo * $x86Directory -Exclude *.lib }
156+
Run-Command -Quiet -Fatal { & copy -fo $($ssh2.Dll) $x86Directory }
157+
Write-Output "Bundled libssh2.dll alongside libgit2"
142158
cd ..
143159
}
144160

145161
if ($x64.IsPresent) {
146-
Install-Libssh2 "x64-windows-static"
162+
$ssh2 = Install-Libssh2 "x64-windows"
163+
$vcpkgToolchain = Join-Path $Env:VCPKG_INSTALLATION_ROOT "scripts\buildsystems\vcpkg.cmake"
147164
Write-Output "Building x64..."
148165
Run-Command -Quiet { & mkdir build64 }
149166
cd build64
150-
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_TOOLCHAIN_FILE=$vcpkgToolchain" -D "VCPKG_TARGET_TRIPLET=x64-windows-static" ../.. }
167+
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_TOOLCHAIN_FILE=$vcpkgToolchain" -D "VCPKG_TARGET_TRIPLET=x64-windows" ../.. }
151168
Run-Command -Fatal { & $cmake --build . --config $configuration }
152169
if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
153170
cd $configuration
@@ -156,14 +173,17 @@ try {
156173
Run-Command -Quiet { & rm $x64Directory\* -ErrorAction Ignore }
157174
Run-Command -Quiet { & mkdir -fo $x64Directory }
158175
Run-Command -Quiet -Fatal { & copy -fo * $x64Directory -Exclude *.lib }
176+
Run-Command -Quiet -Fatal { & copy -fo $($ssh2.Dll) $x64Directory }
177+
Write-Output "Bundled libssh2.dll alongside libgit2"
159178
}
160179

161180
if ($arm64.IsPresent) {
162-
Install-Libssh2 "arm64-windows-static"
181+
$ssh2 = Install-Libssh2 "arm64-windows"
182+
$vcpkgToolchain = Join-Path $Env:VCPKG_INSTALLATION_ROOT "scripts\buildsystems\vcpkg.cmake"
163183
Write-Output "Building arm64..."
164184
Run-Command -Quiet { & mkdir buildarm64 }
165185
cd buildarm64
166-
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_TOOLCHAIN_FILE=$vcpkgToolchain" -D "VCPKG_TARGET_TRIPLET=arm64-windows-static" ../.. }
186+
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_TOOLCHAIN_FILE=$vcpkgToolchain" -D "VCPKG_TARGET_TRIPLET=arm64-windows" ../.. }
167187
Run-Command -Fatal { & $cmake --build . --config $configuration }
168188
if ($test.IsPresent) { Run-Command -Quiet -Fatal { & $ctest -V . } }
169189
cd $configuration
@@ -172,6 +192,8 @@ try {
172192
Run-Command -Quiet { & rm $arm64Directory\* -ErrorAction Ignore }
173193
Run-Command -Quiet { & mkdir -fo $arm64Directory }
174194
Run-Command -Quiet -Fatal { & copy -fo * $arm64Directory -Exclude *.lib }
195+
Run-Command -Quiet -Fatal { & copy -fo $($ssh2.Dll) $arm64Directory }
196+
Write-Output "Bundled libssh2.dll alongside libgit2"
175197
}
176198

177199
Write-Output "Done!"

build.libgit2.sh

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,15 @@ else
2020
USEHTTPS="OpenSSL-Dynamic"
2121
fi
2222

23-
# Find static libssh2 for linking into the shared libgit2 library
24-
LIBSSH2_STATIC=$(find /usr /opt/homebrew 2>/dev/null -name "libssh2.a" 2>/dev/null | head -1)
25-
if [[ -z "$LIBSSH2_STATIC" ]]; then
26-
echo "$(tput setaf 1)Error: static libssh2 (libssh2.a) not found. Install libssh2-dev (Debian), libssh2-static (Alpine), or libssh2 (Homebrew).$(tput sgr0)"
27-
exit 1
28-
fi
29-
3023
rm -rf libgit2/build
3124
mkdir libgit2/build
3225
pushd libgit2/build
3326

3427
export _BINPATH=`pwd`
3528

36-
LIBSSH2_INCLUDE_DIR=$(dirname "$LIBSSH2_STATIC")/../include
37-
3829
cmake -DCMAKE_BUILD_TYPE:STRING=Release \
3930
-DBUILD_TESTS:BOOL=OFF \
4031
-DUSE_SSH=ON \
41-
-DLIBSSH2_FOUND:BOOL=TRUE \
42-
-DLIBSSH2_LIBRARIES=$LIBSSH2_STATIC \
43-
-DLIBSSH2_INCLUDE_DIRS=$LIBSSH2_INCLUDE_DIR \
44-
-DLIBSSH2_LDFLAGS="-lssh2" \
4532
-DLIBGIT2_FILENAME=git2-$SHORTSHA \
4633
-DCMAKE_OSX_ARCHITECTURES=$OSXARCHITECTURE \
4734
-DUSE_HTTPS=$USEHTTPS \
@@ -65,4 +52,28 @@ fi
6552
rm -rf $PACKAGEPATH/$RID
6653
mkdir -p $PACKAGEPATH/$RID/native
6754

68-
cp libgit2/build/libgit2-$SHORTSHA.$LIBEXT $PACKAGEPATH/$RID/native
55+
LIBGIT2="libgit2/build/libgit2-$SHORTSHA.$LIBEXT"
56+
cp $LIBGIT2 $PACKAGEPATH/$RID/native
57+
58+
# Find and bundle the shared libssh2 alongside libgit2
59+
if [[ $OS == "Darwin" ]]; then
60+
LIBSSH2_PATH=$(otool -L $LIBGIT2 | grep libssh2 | awk '{print $1}')
61+
if [[ -n "$LIBSSH2_PATH" ]]; then
62+
cp $LIBSSH2_PATH $PACKAGEPATH/$RID/native/
63+
LIBSSH2_NAME=$(basename $LIBSSH2_PATH)
64+
# Rewrite libgit2 to find libssh2 in the same directory
65+
install_name_tool -change $LIBSSH2_PATH @loader_path/$LIBSSH2_NAME $PACKAGEPATH/$RID/native/libgit2-$SHORTSHA.$LIBEXT
66+
# Set libssh2's own id to be relative too
67+
install_name_tool -id @loader_path/$LIBSSH2_NAME $PACKAGEPATH/$RID/native/$LIBSSH2_NAME
68+
echo "Bundled $LIBSSH2_NAME alongside libgit2"
69+
fi
70+
else
71+
LIBSSH2_PATH=$(ldd $LIBGIT2 | grep libssh2 | awk '{print $3}')
72+
if [[ -n "$LIBSSH2_PATH" ]]; then
73+
cp $LIBSSH2_PATH $PACKAGEPATH/$RID/native/
74+
LIBSSH2_NAME=$(basename $LIBSSH2_PATH)
75+
# Set RPATH so libgit2 looks in its own directory
76+
patchelf --set-rpath '$ORIGIN' $PACKAGEPATH/$RID/native/libgit2-$SHORTSHA.$LIBEXT
77+
echo "Bundled $LIBSSH2_NAME alongside libgit2"
78+
fi
79+
fi

0 commit comments

Comments
 (0)