Skip to content

Commit 43114da

Browse files
committed
clean code
1 parent a7b5792 commit 43114da

File tree

4 files changed

+102
-292
lines changed

4 files changed

+102
-292
lines changed

.github/workflows/ci.yml

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,38 @@ on:
66
pull_request:
77

88
jobs:
9+
native-linux:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout repository
13+
uses: actions/checkout@v4
14+
with:
15+
submodules: recursive
16+
17+
- name: Install build dependencies
18+
run: |
19+
sudo apt-get update
20+
sudo apt-get install -y build-essential cmake libopenblas-dev liblapack-dev
21+
22+
- name: Configure native build
23+
run: cmake -S native -B native/build/linux -DCMAKE_BUILD_TYPE=Release
24+
25+
- name: Build native library
26+
run: cmake --build native/build/linux --target mlxsharp --config Release
27+
28+
- name: Package native artifact
29+
run: |
30+
mkdir -p artifacts/native/linux-x64
31+
cp native/build/linux/libmlxsharp.so artifacts/native/linux-x64/
32+
33+
- name: Upload native artifact
34+
uses: actions/upload-artifact@v4
35+
with:
36+
name: native-linux-x64
37+
path: artifacts/native/linux-x64/libmlxsharp.so
38+
939
build-test:
40+
needs: native-linux
1041
runs-on: macos-latest
1142
steps:
1243
- name: Checkout repository
@@ -35,10 +66,10 @@ jobs:
3566
run: |
3667
cmake -S native -B native/build -DCMAKE_BUILD_TYPE=Release
3768
cmake --build native/build --target mlxsharp --config Release
38-
mkdir -p src/MLXSharp.Native/runtimes/osx-arm64/native
39-
cp native/build/libmlxsharp.dylib src/MLXSharp.Native/runtimes/osx-arm64/native/
69+
mkdir -p src/MLXSharp/runtimes/osx-arm64/native
70+
cp native/build/libmlxsharp.dylib src/MLXSharp/runtimes/osx-arm64/native/
4071
echo "Native library built:"
41-
ls -la src/MLXSharp.Native/runtimes/osx-arm64/native/
72+
ls -la src/MLXSharp/runtimes/osx-arm64/native/
4273
4374
- name: Rebuild solution with native libraries
4475
run: dotnet build --configuration Release --no-restore
@@ -62,7 +93,7 @@ jobs:
6293
run: |
6394
TEST_OUTPUT="src/MLXSharp.Tests/bin/Release/net9.0"
6495
mkdir -p "$TEST_OUTPUT/runtimes/osx-arm64/native"
65-
cp src/MLXSharp.Native/runtimes/osx-arm64/native/libmlxsharp.dylib "$TEST_OUTPUT/runtimes/osx-arm64/native/"
96+
cp src/MLXSharp/runtimes/osx-arm64/native/libmlxsharp.dylib "$TEST_OUTPUT/runtimes/osx-arm64/native/"
6697
ls -la "$TEST_OUTPUT/runtimes/osx-arm64/native/"
6798
6899
- name: Run tests
@@ -75,13 +106,25 @@ jobs:
75106
mkdir -p artifacts/test-results
76107
mkdir -p artifacts/packages
77108
mkdir -p artifacts/native
78-
cp src/MLXSharp.Native/runtimes/osx-arm64/native/libmlxsharp.dylib artifacts/native/
109+
cp src/MLXSharp/runtimes/osx-arm64/native/libmlxsharp.dylib artifacts/native/
110+
111+
- name: Download Linux native library
112+
uses: actions/download-artifact@v4
113+
with:
114+
name: native-linux-x64
115+
path: artifacts/native/linux-x64
116+
117+
- name: Stage Linux native library in project
118+
run: |
119+
mkdir -p src/MLXSharp/runtimes/linux-x64/native
120+
cp artifacts/native/linux-x64/libmlxsharp.so src/MLXSharp/runtimes/linux-x64/native/
121+
cp artifacts/native/linux-x64/libmlxsharp.so artifacts/native/
79122
80123
- name: Pack MLXSharp library
81-
run: dotnet pack src/MLXSharp/MLXSharp.csproj --configuration Release --output artifacts/packages -p:MLXSharpMacNativeBinary=$GITHUB_WORKSPACE/native/build/libmlxsharp.dylib
124+
run: dotnet pack src/MLXSharp/MLXSharp.csproj --configuration Release --output artifacts/packages -p:MLXSharpMacNativeBinary=$GITHUB_WORKSPACE/native/build/libmlxsharp.dylib -p:MLXSharpLinuxNativeBinary=$GITHUB_WORKSPACE/artifacts/native/linux-x64/libmlxsharp.so
82125

83126
- name: Pack MLXSharp.SemanticKernel library
84-
run: dotnet pack src/MLXSharp.SemanticKernel/MLXSharp.SemanticKernel.csproj --configuration Release --output artifacts/packages -p:MLXSharpMacNativeBinary=$GITHUB_WORKSPACE/native/build/libmlxsharp.dylib
127+
run: dotnet pack src/MLXSharp.SemanticKernel/MLXSharp.SemanticKernel.csproj --configuration Release --output artifacts/packages -p:MLXSharpMacNativeBinary=$GITHUB_WORKSPACE/native/build/libmlxsharp.dylib -p:MLXSharpLinuxNativeBinary=$GITHUB_WORKSPACE/artifacts/native/linux-x64/libmlxsharp.so -p:MLXSharpSkipLinuxNativeValidation=true
85128

86129
- name: Verify package contains native libraries
87130
run: |
@@ -98,11 +141,25 @@ jobs:
98141
echo "Inspecting ${package}"
99142
filename=$(basename "${package}")
100143
case "${filename}" in
144+
MLXSharp.SemanticKernel.*.nupkg)
145+
echo " ↷ Skipping native check for ${filename}"
146+
;;
101147
MLXSharp.*.nupkg)
148+
package_missing=0
102149
if unzip -l "${package}" | grep -q "runtimes/osx-arm64/native/libmlxsharp.dylib"; then
103-
echo " ✓ Native library found"
150+
echo " ✓ macOS library present"
151+
else
152+
echo " ✗ macOS library missing"
153+
package_missing=1
154+
fi
155+
if unzip -l "${package}" | grep -q "runtimes/linux-x64/native/libmlxsharp.so"; then
156+
echo " ✓ Linux library present"
104157
else
105-
echo " ✗ Native library NOT found"
158+
echo " ✗ Linux library missing"
159+
package_missing=1
160+
fi
161+
162+
if [ ${package_missing} -ne 0 ]; then
106163
unzip -l "${package}"
107164
missing=1
108165
fi

src/MLXSharp/MLXSharp.csproj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
<MLXSharpMacNativeBinary Condition="'$(MLXSharpMacNativeBinary)' == ''">$([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','..','..','native','build','libmlxsharp.dylib'))</MLXSharpMacNativeBinary>
1212
<MLXSharpMacNativeDestination>$([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','runtimes','osx-arm64','native','libmlxsharp.dylib'))</MLXSharpMacNativeDestination>
1313
<MLXSharpSkipMacNativeValidation Condition="'$(MLXSharpSkipMacNativeValidation)' == ''">false</MLXSharpSkipMacNativeValidation>
14+
<MLXSharpMacNativeDestinationDir>$([System.IO.Path]::GetDirectoryName('$(MLXSharpMacNativeDestination)'))</MLXSharpMacNativeDestinationDir>
15+
16+
<MLXSharpLinuxNativeBinary Condition="'$(MLXSharpLinuxNativeBinary)' == ''">$([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','..','..','native','build','linux','libmlxsharp.so'))</MLXSharpLinuxNativeBinary>
17+
<MLXSharpLinuxNativeDestination>$([System.IO.Path]::Combine('$(MSBuildProjectDirectory)','runtimes','linux-x64','native','libmlxsharp.so'))</MLXSharpLinuxNativeDestination>
18+
<MLXSharpSkipLinuxNativeValidation Condition="'$(MLXSharpSkipLinuxNativeValidation)' == ''">false</MLXSharpSkipLinuxNativeValidation>
19+
<MLXSharpLinuxNativeDestinationDir>$([System.IO.Path]::GetDirectoryName('$(MLXSharpLinuxNativeDestination)'))</MLXSharpLinuxNativeDestinationDir>
1420
</PropertyGroup>
1521

1622
<Target Name="EnsureMacNativeBinary"
@@ -43,6 +49,36 @@
4349
Text="libmlxsharp.dylib is missing. Build the native library or supply MLXSharpMacNativeBinary before packing." />
4450
</Target>
4551

52+
<Target Name="EnsureLinuxNativeBinary"
53+
BeforeTargets="Pack"
54+
Condition="'$(MSBuildProjectFullPath)' == '$(MSBuildThisFileFullPath)'">
55+
<ItemGroup>
56+
<_LinuxNativeSource Include="$(MLXSharpLinuxNativeBinary)"
57+
Condition="Exists('$(MLXSharpLinuxNativeBinary)')" />
58+
</ItemGroup>
59+
60+
<MakeDir Directories="$(MLXSharpLinuxNativeDestinationDir)"
61+
Condition="'$(MLXSharpLinuxNativeDestinationDir)' != ''" />
62+
63+
<Copy SourceFiles="@(_LinuxNativeSource)"
64+
DestinationFiles="$(MLXSharpLinuxNativeDestination)"
65+
SkipUnchangedFiles="true"
66+
Condition="'@(_LinuxNativeSource)' != ''" />
67+
68+
<ItemGroup>
69+
<None Include="$(MLXSharpLinuxNativeDestination)"
70+
Condition="Exists('$(MLXSharpLinuxNativeDestination)')">
71+
<Pack>true</Pack>
72+
<PackagePath>runtimes/linux-x64/native/%(Filename)%(Extension)</PackagePath>
73+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
74+
<Link>runtimes\linux-x64\native\%(Filename)%(Extension)</Link>
75+
</None>
76+
</ItemGroup>
77+
78+
<Error Condition="'$(MLXSharpSkipLinuxNativeValidation)' != 'true' and !Exists('$(MLXSharpLinuxNativeDestination)')"
79+
Text="libmlxsharp.so is missing. Build the native library or supply MLXSharpLinuxNativeBinary before packing." />
80+
</Target>
81+
4682
<!-- Include native binaries embedded in this project -->
4783
<ItemGroup>
4884
<None Include="runtimes\**\*.*"

0 commit comments

Comments
 (0)