Skip to content

Commit 312db26

Browse files
authored
Build in statically linked libssh2 for openssl1.1 (#11)
* Build libssh2 for multiple openssl backends * Keep libcrypto3 build the same as it was * Build for all linux * Fix ppc641e build * Build variants in parallel * Simplify dockerbuild.sh * PR comments
1 parent a021977 commit 312db26

4 files changed

Lines changed: 104 additions & 17 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,64 @@ jobs:
1212
name: ${{ matrix.name }}
1313
runs-on: ${{ matrix.os }}
1414
env:
15-
RID: ${{ matrix.name }}
15+
RID: ${{ matrix.rid }}
16+
VARIANT: ${{ matrix.variant }}
1617
strategy:
1718
matrix:
1819
include:
1920
- os: windows-2022
2021
name: win-x86
22+
rid: win-x86
2123
param: -x86
2224
- os: windows-2022
2325
name: win-x64
26+
rid: win-x64
2427
param: -x64
2528
- os: windows-2022
2629
name: win-arm64
30+
rid: win-arm64
2731
param: -arm64
2832
- os: ubuntu-24.04
2933
name: linux-x64
34+
rid: linux-x64
35+
- os: ubuntu-24.04
36+
name: linux-x64-openssl1.1
37+
rid: linux-x64
38+
variant: openssl1.1
3039
- os: ubuntu-24.04
3140
name: linux-arm
41+
rid: linux-arm
42+
- os: ubuntu-24.04
43+
name: linux-arm-openssl1.1
44+
rid: linux-arm
45+
variant: openssl1.1
3246
- os: ubuntu-24.04
3347
name: linux-arm64
48+
rid: linux-arm64
49+
- os: ubuntu-24.04
50+
name: linux-arm64-openssl1.1
51+
rid: linux-arm64
52+
variant: openssl1.1
53+
# debian:bullseye-slim has no ppc64le manifest, so the openssl1.1
54+
# variant is omitted for this RID.
3455
- os: ubuntu-24.04
3556
name: linux-ppc64le
57+
rid: linux-ppc64le
3658
- os: ubuntu-24.04
3759
name: linux-musl-x64
60+
rid: linux-musl-x64
3861
- os: ubuntu-24.04
3962
name: linux-musl-arm
63+
rid: linux-musl-arm
4064
- os: ubuntu-24.04
4165
name: linux-musl-arm64
66+
rid: linux-musl-arm64
4267
- os: macos-26-intel
4368
name: osx-x64
69+
rid: osx-x64
4470
- os: macos-26
4571
name: osx-arm64
72+
rid: osx-arm64
4673
fail-fast: false
4774
steps:
4875
- name: Checkout
@@ -56,7 +83,7 @@ jobs:
5683
if: runner.os == 'macOS'
5784
run: ./build.libgit2.sh
5885
- name: Setup QEMU
59-
if: runner.os == 'Linux' && (matrix.name == 'linux-arm' || matrix.name == 'linux-arm64' || matrix.name == 'linux-ppc64le' || matrix.name == 'linux-musl-arm' || matrix.name == 'linux-musl-arm64')
86+
if: runner.os == 'Linux' && (matrix.rid == 'linux-arm' || matrix.rid == 'linux-arm64' || matrix.rid == 'linux-ppc64le' || matrix.rid == 'linux-musl-arm' || matrix.rid == 'linux-musl-arm64')
6087
uses: docker/setup-qemu-action@v4
6188
- name: Set up Docker Buildx
6289
if: runner.os == 'Linux'
@@ -68,7 +95,7 @@ jobs:
6895
uses: actions/upload-artifact@v7.0.0
6996
with:
7097
name: ${{ matrix.name }}
71-
path: nuget.package/runtimes/${{ matrix.name }}
98+
path: nuget.package/runtimes/${{ matrix.rid }}
7299
package:
73100
name: Create package
74101
needs: build
@@ -103,6 +130,23 @@ jobs:
103130
uses: actions/download-artifact@v8.0.1
104131
with:
105132
path: nuget.package/runtimes/
133+
- name: Merge variant artifacts into RID directories
134+
shell: bash
135+
run: |
136+
# Variant artifacts (e.g. "linux-x64-openssl1.1") need their native
137+
# binaries merged into the base RID directory ("linux-x64") so the
138+
# NuGet package contains a single runtimes/<rid>/native/ tree per RID.
139+
cd nuget.package/runtimes
140+
for variant_dir in *-openssl1.1; do
141+
[[ -d "$variant_dir" ]] || continue
142+
143+
rid="${variant_dir%-openssl1.1}"
144+
target="$rid/native"
145+
146+
mkdir -p "$target"
147+
mv "$variant_dir/native/"* "$target/"
148+
rm -rf "$variant_dir"
149+
done
106150
- name: Create package
107151
run: dotnet pack nuget.package ${{ steps.version.outputs.override && format('/p:MinVerVersionOverride={0}', steps.version.outputs.override) || '' }}
108152
- name: Upload NuGet package

‎Dockerfile.linux-openssl1.1‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
FROM debian:bullseye-slim
2+
3+
RUN apt-get update && apt-get install -y --no-install-recommends \
4+
build-essential \
5+
cmake \
6+
pkg-config \
7+
ca-certificates \
8+
curl \
9+
git \
10+
libssl-dev \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
# Build libssh2 from source as a PIC static archive. apt's libssh2.a is not
14+
# compiled with -fPIC, which prevents linking into libgit2.so. Static linking
15+
# lets us ship one libgit2-<sha>-<openssl-variant>.so per OpenSSL ABI without
16+
# colliding libssh2.so.1 SONAMEs across variants.
17+
ARG LIBSSH2_VERSION=1.11.1
18+
RUN curl -fsSL https://github.com/libssh2/libssh2/releases/download/libssh2-${LIBSSH2_VERSION}/libssh2-${LIBSSH2_VERSION}.tar.gz -o libssh2.tar.gz \
19+
&& tar xf libssh2.tar.gz \
20+
&& cmake -S libssh2-${LIBSSH2_VERSION} -B libssh2-${LIBSSH2_VERSION}/build \
21+
-DCMAKE_BUILD_TYPE=Release \
22+
-DBUILD_SHARED_LIBS=OFF \
23+
-DCMAKE_POSITION_INDEPENDENT_CODE=ON \
24+
-DCRYPTO_BACKEND=OpenSSL \
25+
-DBUILD_TESTING=OFF \
26+
-DBUILD_EXAMPLES=OFF \
27+
&& cmake --build libssh2-${LIBSSH2_VERSION}/build --target install \
28+
&& rm -rf libssh2-${LIBSSH2_VERSION} libssh2.tar.gz
29+
30+
WORKDIR /nativebinaries
31+
COPY . /nativebinaries/
32+
33+
CMD ["/bin/bash", "-c", "./build.libgit2.sh"]

‎build.libgit2.sh‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ OSXARCHITECTURE=$ARCH
1111

1212
EXTRA_CMAKE_FLAGS=""
1313

14+
# When OPENSSL_VARIANT is set, append it to the libgit2 filename so multiple
15+
# variants can ship side by side and be selected at runtime
16+
# This is only needed to support multiple versions of OpenSSL on Linux
17+
LIBGIT2_FILENAME="git2-$SHORTSHA"
18+
if [[ -n "$OPENSSL_VARIANT" ]]; then
19+
LIBGIT2_FILENAME="$LIBGIT2_FILENAME-$OPENSSL_VARIANT"
20+
fi
21+
1422
if [[ $OS == "Darwin" ]]; then
1523
USEHTTPS="ON"
1624
if [[ $RID == "osx-arm64" ]]; then
@@ -32,7 +40,7 @@ export _BINPATH=`pwd`
3240
cmake -DCMAKE_BUILD_TYPE:STRING=Release \
3341
-DBUILD_TESTS:BOOL=OFF \
3442
-DUSE_SSH=ON \
35-
-DLIBGIT2_FILENAME=git2-$SHORTSHA \
43+
-DLIBGIT2_FILENAME=$LIBGIT2_FILENAME \
3644
-DCMAKE_OSX_ARCHITECTURES=$OSXARCHITECTURE \
3745
-DUSE_HTTPS=$USEHTTPS \
3846
-DUSE_BUNDLED_ZLIB=ON \
@@ -56,10 +64,7 @@ fi
5664
rm -rf $PACKAGEPATH/$RID
5765
mkdir -p $PACKAGEPATH/$RID/native
5866

59-
cp libgit2/build/libgit2-$SHORTSHA.$LIBEXT $PACKAGEPATH/$RID/native
60-
61-
# Bundle libssh2 shared library alongside libgit2
62-
LIBGIT2_PATH="$PACKAGEPATH/$RID/native/libgit2-$SHORTSHA.$LIBEXT"
67+
cp libgit2/build/lib$LIBGIT2_FILENAME.$LIBEXT $PACKAGEPATH/$RID/native
6368

6469
if [[ $OS == "Darwin" ]]; then
6570
# We don't run Octopus Server on Mac, so we can avoid the restriction of relying on the system crypto libraries
@@ -107,8 +112,11 @@ if [[ $OS == "Darwin" ]]; then
107112
for DYLIB in "$NATIVE_DIR"/*.dylib; do
108113
codesign --force --sign - "$DYLIB"
109114
done
115+
elif [[ -n "$OPENSSL_VARIANT" ]]; then
116+
echo "$OPENSSL_VARIANT: libssh2 statically linked into libgit2"
110117
else
111-
# Linux: find libssh2 via ldd
118+
# Linux: bundle the dynamic libssh2 alongside libgit2.
119+
LIBGIT2_PATH="$PACKAGEPATH/$RID/native/lib$LIBGIT2_FILENAME.$LIBEXT"
112120
LIBSSH2_PATH=$(ldd "$LIBGIT2_PATH" | grep libssh2 | awk '{print $3}')
113121
if [[ -z "$LIBSSH2_PATH" ]]; then
114122
echo "ERROR: libgit2 does not appear to link against libssh2"

‎dockerbuild.sh‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
set -e
4-
echo "building for $RID"
4+
echo "building for $RID variant=${VARIANT:-default}"
55

66
# Map RID to Docker platform for native builds (no cross-compilation).
77
if [[ $RID =~ arm64 ]]; then
@@ -14,16 +14,18 @@ else
1414
platform="linux/amd64"
1515
fi
1616

17+
# Choose the Dockerfile based on RID and variant.
1718
if [[ $RID == linux-musl* ]]; then
1819
dockerfile="Dockerfile.linux-musl"
20+
elif [[ "$VARIANT" == "openssl1.1" ]]; then
21+
# Built on bullseye against OpenSSL 1.1, libssh2 statically linked.
22+
dockerfile="Dockerfile.linux-openssl1.1"
1923
else
24+
# Default: built on bookworm against OpenSSL 3, libssh2 bundled as a separate .so.
2025
dockerfile="Dockerfile.linux"
2126
fi
2227

23-
docker buildx build --platform "$platform" --load -t $RID -f $dockerfile .
24-
25-
docker run --platform "$platform" -t -e RID=$RID --name=$RID $RID
26-
27-
docker cp $RID:/nativebinaries/nuget.package/runtimes nuget.package
28-
29-
docker rm $RID
28+
docker buildx build --platform "$platform" --load -t "$RID" -f "$dockerfile" .
29+
docker run --platform "$platform" -t -e RID=$RID -e OPENSSL_VARIANT="$VARIANT" --name="$RID" "$RID"
30+
docker cp "$RID":/nativebinaries/nuget.package/runtimes nuget.package
31+
docker rm "$RID"

0 commit comments

Comments
 (0)