Skip to content

Commit 0164c7f

Browse files
committed
Refactor native build process: move Dockerfiles and build logic into dedicated scripts directory.
1 parent 10e3b8d commit 0164c7f

5 files changed

Lines changed: 160 additions & 165 deletions

File tree

.github/docker/linux.Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM debian:bookworm-slim
2+
3+
RUN apt-get update && apt-get install -y \
4+
build-essential \
5+
cmake \
6+
libssl-dev \
7+
zlib1g-dev \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
COPY Native/libssh2 /src/libssh2
11+
WORKDIR /src/libssh2/build
12+
13+
RUN cmake .. \
14+
-DCMAKE_BUILD_TYPE=Release \
15+
-DBUILD_SHARED_LIBS=ON \
16+
-DENABLE_ZLIB_COMPRESSION=ON \
17+
-DCRYPTO_BACKEND=OpenSSL \
18+
-DBUILD_EXAMPLES=OFF \
19+
-DBUILD_TESTING=OFF && \
20+
cmake --build . --config Release -j$(nproc) && \
21+
mkdir -p /output && \
22+
cp src/libssh2.so* /output/
23+
24+
FROM scratch
25+
COPY --from=0 /output/* /

.github/docker/macos.Dockerfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
FROM crazymax/osxcross:latest-ubuntu AS builder
2+
ARG TARGET_TRIPLE
3+
4+
# Install additional dependencies
5+
RUN apt-get update && apt-get install -y \
6+
cmake \
7+
wget \
8+
&& rm -rf /var/lib/apt/lists/*
9+
10+
# Download and build OpenSSL for macOS
11+
WORKDIR /build
12+
RUN wget https://www.openssl.org/source/openssl-3.0.15.tar.gz && \
13+
tar xzf openssl-3.0.15.tar.gz && \
14+
cd openssl-3.0.15 && \
15+
CC=${TARGET_TRIPLE}-clang \
16+
./Configure darwin64-${TARGET_TRIPLE##*-}-cc --prefix=/opt/osxcross/SDK/MacOSX14.5.sdk/usr && \
17+
make -j$(nproc) && \
18+
make install_sw
19+
20+
# Build libssh2
21+
COPY Native/libssh2 /src/libssh2
22+
WORKDIR /src/libssh2/build
23+
24+
RUN cmake .. \
25+
-DCMAKE_SYSTEM_NAME=Darwin \
26+
-DCMAKE_C_COMPILER=${TARGET_TRIPLE}-clang \
27+
-DCMAKE_CXX_COMPILER=${TARGET_TRIPLE}-clang++ \
28+
-DCMAKE_BUILD_TYPE=Release \
29+
-DBUILD_SHARED_LIBS=ON \
30+
-DENABLE_ZLIB_COMPRESSION=ON \
31+
-DCRYPTO_BACKEND=OpenSSL \
32+
-DCMAKE_FIND_ROOT_PATH=/opt/osxcross/SDK/MacOSX14.5.sdk \
33+
-DOPENSSL_ROOT_DIR=/opt/osxcross/SDK/MacOSX14.5.sdk/usr \
34+
-DBUILD_EXAMPLES=OFF \
35+
-DBUILD_TESTING=OFF && \
36+
cmake --build . --config Release -j$(nproc) && \
37+
mkdir -p /output && \
38+
cp src/libssh2*.dylib /output/libssh2.dylib
39+
40+
FROM scratch
41+
COPY --from=builder /output/* /

.github/docker/windows.Dockerfile

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
FROM debian:bookworm-slim
2+
ARG TARGET_TRIPLE
3+
4+
RUN apt-get update && apt-get install -y \
5+
build-essential \
6+
cmake \
7+
mingw-w64 \
8+
wget \
9+
&& rm -rf /var/lib/apt/lists/*
10+
11+
# Download and build OpenSSL for Windows
12+
WORKDIR /build
13+
RUN wget https://www.openssl.org/source/openssl-3.0.15.tar.gz && \
14+
tar xzf openssl-3.0.15.tar.gz && \
15+
cd openssl-3.0.15 && \
16+
./Configure mingw64 --cross-compile-prefix=${TARGET_TRIPLE}- --prefix=/opt/openssl-${TARGET_TRIPLE} && \
17+
make -j$(nproc) && \
18+
make install_sw
19+
20+
# Build libssh2
21+
COPY Native/libssh2 /src/libssh2
22+
WORKDIR /src/libssh2/build
23+
24+
RUN cmake .. \
25+
-DCMAKE_SYSTEM_NAME=Windows \
26+
-DCMAKE_C_COMPILER=${TARGET_TRIPLE}-gcc \
27+
-DCMAKE_RC_COMPILER=${TARGET_TRIPLE}-windres \
28+
-DCMAKE_BUILD_TYPE=Release \
29+
-DBUILD_SHARED_LIBS=ON \
30+
-DENABLE_ZLIB_COMPRESSION=OFF \
31+
-DCRYPTO_BACKEND=OpenSSL \
32+
-DOPENSSL_ROOT_DIR=/opt/openssl-${TARGET_TRIPLE} \
33+
-DBUILD_EXAMPLES=OFF \
34+
-DBUILD_TESTING=OFF && \
35+
cmake --build . --config Release -j$(nproc) && \
36+
mkdir -p /output && \
37+
cp src/libssh2.dll /output/ || cp src/libssh2-*.dll /output/libssh2.dll
38+
39+
FROM scratch
40+
COPY --from=0 /output/* /

.github/scripts/build-native.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PLATFORM=$1
5+
TARGET_TRIPLE=$2
6+
DOCKERFILE_PLATFORM=$3
7+
8+
mkdir -p output
9+
10+
if [[ "$PLATFORM" == win-* ]]; then
11+
# Windows build using MinGW
12+
docker buildx build \
13+
--platform "$DOCKERFILE_PLATFORM" \
14+
--build-arg TARGET_TRIPLE="$TARGET_TRIPLE" \
15+
--output type=local,dest=./output \
16+
-f .github/docker/windows.Dockerfile .
17+
18+
elif [[ "$PLATFORM" == osx-* ]]; then
19+
# macOS build using pre-built osxcross image
20+
docker buildx build \
21+
--platform "$DOCKERFILE_PLATFORM" \
22+
--build-arg TARGET_TRIPLE="$TARGET_TRIPLE" \
23+
--output type=local,dest=./output \
24+
-f .github/docker/macos.Dockerfile .
25+
26+
else
27+
# Linux build
28+
docker buildx build \
29+
--platform "$DOCKERFILE_PLATFORM" \
30+
--output type=local,dest=./output \
31+
-f .github/docker/linux.Dockerfile .
32+
fi
33+
34+
# Create runtime directory structure
35+
mkdir -p "NullOpsDevs.LibSsh/runtimes/$PLATFORM/native"
36+
37+
# Copy library to correct location
38+
if [[ "$PLATFORM" == win-* ]]; then
39+
cp output/libssh2.dll "NullOpsDevs.LibSsh/runtimes/$PLATFORM/native/"
40+
elif [[ "$PLATFORM" == osx-* ]]; then
41+
cp output/libssh2.dylib "NullOpsDevs.LibSsh/runtimes/$PLATFORM/native/"
42+
else
43+
# Copy and create symlink without version suffix
44+
cp output/libssh2.so* "NullOpsDevs.LibSsh/runtimes/$PLATFORM/native/"
45+
# Find the actual .so file and create a symlink
46+
cd "NullOpsDevs.LibSsh/runtimes/$PLATFORM/native/"
47+
if [ -f libssh2.so.1 ]; then
48+
ln -sf libssh2.so.1 libssh2.so
49+
fi
50+
fi

.github/workflows/build-native.yml

Lines changed: 4 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ on:
77
paths:
88
- 'Native/libssh2/**'
99
- '.github/workflows/build-native.yml'
10+
- '.github/docker/**'
11+
- '.github/scripts/**'
1012

1113
jobs:
1214
build-all:
@@ -18,9 +20,6 @@ jobs:
1820
- platform: win-x64
1921
dockerfile_platform: linux/amd64
2022
target_triple: x86_64-w64-mingw32
21-
- platform: win-x86
22-
dockerfile_platform: linux/amd64
23-
target_triple: i686-w64-mingw32
2423
- platform: linux-x64
2524
dockerfile_platform: linux/amd64
2625
target_triple: x86_64-linux-gnu
@@ -46,168 +45,8 @@ jobs:
4645

4746
- name: Build libssh2
4847
run: |
49-
mkdir -p output
50-
51-
# Determine build configuration based on platform
52-
PLATFORM="${{ matrix.platform }}"
53-
TARGET_TRIPLE="${{ matrix.target_triple }}"
54-
55-
if [[ "$PLATFORM" == win-* ]]; then
56-
# Windows build using MinGW
57-
docker buildx build \
58-
--platform ${{ matrix.dockerfile_platform }} \
59-
--build-arg TARGET_TRIPLE=$TARGET_TRIPLE \
60-
--output type=local,dest=./output \
61-
-f - . <<'EOF'
62-
FROM debian:bookworm-slim
63-
ARG TARGET_TRIPLE
64-
65-
RUN apt-get update && apt-get install -y \
66-
build-essential \
67-
cmake \
68-
mingw-w64 \
69-
wget \
70-
&& rm -rf /var/lib/apt/lists/*
71-
72-
# Download and build OpenSSL for Windows
73-
WORKDIR /build
74-
RUN wget https://www.openssl.org/source/openssl-3.0.15.tar.gz && \
75-
tar xzf openssl-3.0.15.tar.gz && \
76-
cd openssl-3.0.15 && \
77-
./Configure mingw64 --cross-compile-prefix=${TARGET_TRIPLE}- --prefix=/opt/openssl-${TARGET_TRIPLE} && \
78-
make -j$(nproc) && \
79-
make install_sw
80-
81-
# Build libssh2
82-
COPY Native/libssh2 /src/libssh2
83-
WORKDIR /src/libssh2/build
84-
85-
RUN cmake .. \
86-
-DCMAKE_SYSTEM_NAME=Windows \
87-
-DCMAKE_C_COMPILER=${TARGET_TRIPLE}-gcc \
88-
-DCMAKE_RC_COMPILER=${TARGET_TRIPLE}-windres \
89-
-DCMAKE_BUILD_TYPE=Release \
90-
-DBUILD_SHARED_LIBS=ON \
91-
-DENABLE_ZLIB_COMPRESSION=OFF \
92-
-DCRYPTO_BACKEND=OpenSSL \
93-
-DOPENSSL_ROOT_DIR=/opt/openssl-${TARGET_TRIPLE} \
94-
-DBUILD_EXAMPLES=OFF \
95-
-DBUILD_TESTING=OFF && \
96-
cmake --build . --config Release -j$(nproc) && \
97-
mkdir -p /output && \
98-
cp src/libssh2.dll /output/ || cp src/libssh2-*.dll /output/libssh2.dll
99-
100-
FROM scratch
101-
COPY --from=0 /output/* /
102-
EOF
103-
104-
elif [[ "$PLATFORM" == osx-* ]]; then
105-
# macOS build using osxcross
106-
docker buildx build \
107-
--platform ${{ matrix.dockerfile_platform }} \
108-
--build-arg TARGET_TRIPLE=$TARGET_TRIPLE \
109-
--output type=local,dest=./output \
110-
-f - . <<'EOF'
111-
FROM debian:bookworm-slim
112-
ARG TARGET_TRIPLE
113-
114-
RUN apt-get update && apt-get install -y \
115-
build-essential \
116-
cmake \
117-
git \
118-
wget \
119-
clang \
120-
libssl-dev \
121-
libxml2-dev \
122-
llvm-dev \
123-
uuid-dev \
124-
zlib1g-dev \
125-
libbz2-dev \
126-
&& rm -rf /var/lib/apt/lists/*
127-
128-
# Install osxcross
129-
WORKDIR /opt
130-
RUN git clone https://github.com/tpoechtrager/osxcross.git && \
131-
cd osxcross && \
132-
wget -nc https://github.com/joseluisq/macosx-sdks/releases/download/12.3/MacOSX12.3.sdk.tar.xz && \
133-
mv MacOSX12.3.sdk.tar.xz tarballs/ && \
134-
UNATTENDED=1 ./build.sh
135-
136-
ENV PATH="/opt/osxcross/target/bin:$PATH"
137-
138-
# Build libssh2
139-
COPY Native/libssh2 /src/libssh2
140-
WORKDIR /src/libssh2/build
141-
142-
RUN cmake .. \
143-
-DCMAKE_SYSTEM_NAME=Darwin \
144-
-DCMAKE_C_COMPILER=${TARGET_TRIPLE}-clang \
145-
-DCMAKE_CXX_COMPILER=${TARGET_TRIPLE}-clang++ \
146-
-DCMAKE_BUILD_TYPE=Release \
147-
-DBUILD_SHARED_LIBS=ON \
148-
-DENABLE_ZLIB_COMPRESSION=ON \
149-
-DCRYPTO_BACKEND=OpenSSL \
150-
-DBUILD_EXAMPLES=OFF \
151-
-DBUILD_TESTING=OFF && \
152-
cmake --build . --config Release -j$(nproc) && \
153-
mkdir -p /output && \
154-
cp src/libssh2*.dylib /output/libssh2.dylib
155-
156-
FROM scratch
157-
COPY --from=0 /output/* /
158-
EOF
159-
160-
else
161-
# Linux build
162-
docker buildx build \
163-
--platform ${{ matrix.dockerfile_platform }} \
164-
--output type=local,dest=./output \
165-
-f - . <<'EOF'
166-
FROM debian:bookworm-slim
167-
168-
RUN apt-get update && apt-get install -y \
169-
build-essential \
170-
cmake \
171-
libssl-dev \
172-
zlib1g-dev \
173-
&& rm -rf /var/lib/apt/lists/*
174-
175-
COPY Native/libssh2 /src/libssh2
176-
WORKDIR /src/libssh2/build
177-
178-
RUN cmake .. \
179-
-DCMAKE_BUILD_TYPE=Release \
180-
-DBUILD_SHARED_LIBS=ON \
181-
-DENABLE_ZLIB_COMPRESSION=ON \
182-
-DCRYPTO_BACKEND=OpenSSL \
183-
-DBUILD_EXAMPLES=OFF \
184-
-DBUILD_TESTING=OFF && \
185-
cmake --build . --config Release -j$(nproc) && \
186-
mkdir -p /output && \
187-
cp src/libssh2.so* /output/
188-
189-
FROM scratch
190-
COPY --from=0 /output/* /
191-
EOF
192-
fi
193-
194-
# Create runtime directory structure
195-
mkdir -p NullOpsDevs.LibSsh/runtimes/${{ matrix.platform }}/native
196-
197-
# Copy library to correct location
198-
if [[ "$PLATFORM" == win-* ]]; then
199-
cp output/libssh2.dll NullOpsDevs.LibSsh/runtimes/${{ matrix.platform }}/native/
200-
elif [[ "$PLATFORM" == osx-* ]]; then
201-
cp output/libssh2.dylib NullOpsDevs.LibSsh/runtimes/${{ matrix.platform }}/native/
202-
else
203-
# Copy and create symlink without version suffix
204-
cp output/libssh2.so* NullOpsDevs.LibSsh/runtimes/${{ matrix.platform }}/native/
205-
# Find the actual .so file and create a symlink
206-
cd NullOpsDevs.LibSsh/runtimes/${{ matrix.platform }}/native/
207-
if [ -f libssh2.so.1 ]; then
208-
ln -sf libssh2.so.1 libssh2.so
209-
fi
210-
fi
48+
chmod +x .github/scripts/build-native.sh
49+
.github/scripts/build-native.sh "${{ matrix.platform }}" "${{ matrix.target_triple }}" "${{ matrix.dockerfile_platform }}"
21150
21251
- name: Upload artifacts
21352
uses: actions/upload-artifact@v4

0 commit comments

Comments
 (0)