Skip to content

Commit 288cc50

Browse files
committed
Add Linux support to CI workflow with minimal changes
- Added matrix strategy to build on both macOS and Linux - Updated Swift setup to install Swift 6.0.3 on Linux - Modified build.sh to detect OS and copy appropriate library (.dylib/.so) - Updated api.py for cross-platform library loading - Commit logic handles both .dylib and .so files appropriately Simple approach: extends existing working CI instead of complex new workflow.
1 parent c02990e commit 288cc50

3 files changed

Lines changed: 51 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ on:
1616
# Define the jobs to run in the workflow
1717
jobs:
1818
build-and-test:
19-
runs-on: macos-latest # Use macOS for Swift and iOS-specific dependencies
19+
strategy:
20+
matrix:
21+
os: [macos-latest, ubuntu-latest]
22+
runs-on: ${{ matrix.os }}
2023

2124
steps:
2225
- name: Checkout code
@@ -29,8 +32,18 @@ jobs:
2932

3033
- name: Set up Swift
3134
run: |
32-
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
33-
sudo xcodebuild -runFirstLaunch
35+
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
36+
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
37+
sudo xcodebuild -runFirstLaunch
38+
else
39+
# Install Swift for Linux
40+
wget https://download.swift.org/swift-6.0.3-release/ubuntu2404/swift-6.0.3-RELEASE/swift-6.0.3-RELEASE-ubuntu24.04.tar.gz
41+
tar xzf swift-6.0.3-RELEASE-ubuntu24.04.tar.gz
42+
sudo mv swift-6.0.3-RELEASE-ubuntu24.04 /usr/local/swift
43+
echo "/usr/local/swift/usr/bin" >> $GITHUB_PATH
44+
sudo apt update
45+
sudo apt install -y build-essential libc6-dev
46+
fi
3447
3548
- name: Install dependencies
3649
run: |
@@ -64,9 +77,14 @@ jobs:
6477
git fetch origin
6578
git checkout -B $TARGET_BRANCH origin/$TARGET_BRANCH
6679
67-
# Add and commit the .dylib file
68-
git add ./loop_to_python_api/libLoopAlgorithmToPython.dylib
69-
git commit -m "Add generated libLoopAlgorithmToPython.dylib" || echo "No changes to commit"
80+
# Add and commit the library file
81+
if [[ "${{ matrix.os }}" == "macos-latest" ]]; then
82+
git add ./loop_to_python_api/libLoopAlgorithmToPython.dylib
83+
git commit -m "Add generated libLoopAlgorithmToPython.dylib" || echo "No changes to commit"
84+
else
85+
git add ./loop_to_python_api/libLoopAlgorithmToPython.so
86+
git commit -m "Add generated libLoopAlgorithmToPython.so" || echo "No changes to commit"
87+
fi
7088
7189
# Push to the target branch
7290
git push origin $TARGET_BRANCH

build.sh

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,22 @@ echo "Building dynamic c library from Swift code..."
66
swift package clean
77
swift build --configuration release
88

9-
# Copy the library
10-
if cp .build/release/libLoopAlgorithmToPython.dylib ./loop_to_python_api/; then
11-
echo "Library successfully copied to the loop_to_python_api folder!"
9+
# Detect the operating system and copy the appropriate library
10+
if [[ "$OSTYPE" == "darwin"* ]]; then
11+
# macOS
12+
if cp .build/release/libLoopAlgorithmToPython.dylib ./loop_to_python_api/; then
13+
echo "Library successfully copied to the loop_to_python_api folder!"
14+
else
15+
echo "Failed to copy the .dylib library to the loop_to_python_api folder."
16+
fi
17+
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
18+
# Linux
19+
if cp .build/release/libLoopAlgorithmToPython.so ./loop_to_python_api/; then
20+
echo "Library successfully copied to the loop_to_python_api folder!"
21+
else
22+
echo "Failed to copy the .so library to the loop_to_python_api folder."
23+
fi
1224
else
13-
echo "Failed to copy the library to the loop_to_python_api folder."
25+
echo "Unsupported operating system: $OSTYPE"
26+
exit 1
1427
fi

loop_to_python_api/api.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313
# swift_lib = ctypes.CDLL('python_api/libLoopAlgorithmToPython.dylib')
1414

1515
current_dir = os.path.dirname(os.path.abspath(__file__))
16-
lib_path = os.path.join(current_dir, 'libLoopAlgorithmToPython.dylib')
16+
17+
# Cross-platform library loading
18+
if os.name == 'posix':
19+
if os.uname().sysname == 'Darwin': # macOS
20+
lib_path = os.path.join(current_dir, 'libLoopAlgorithmToPython.dylib')
21+
else: # Linux
22+
lib_path = os.path.join(current_dir, 'libLoopAlgorithmToPython.so')
23+
else:
24+
raise OSError("Unsupported operating system")
25+
1726
swift_lib = ctypes.CDLL(lib_path)
1827

1928

0 commit comments

Comments
 (0)