Skip to content

Commit 1c425b3

Browse files
committed
Refactor Slang library handling logic
Simplify file handling in `slang-native.yml` by removing file renaming logic for version suffixes across Linux, macOS, and Windows platforms. Refactor `SlangCompiler.cs` to introduce a `LoadSlangGlslang` helper method for dynamically loading the `slang-glslang` library. Replace hardcoded `NativeLibrary.Load` calls with this method to reduce duplication and improve flexibility. These changes enhance maintainability, simplify the build process, and make the codebase more adaptable to future changes.
1 parent 1cbd4e8 commit 1c425b3

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

.github/workflows/slang-native.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ jobs:
4141
mv lib/* .
4242
find . -type d -not -path '.' -exec rm -rf {} +
4343
ls -1 | grep -v -E '^libslang-compiler\.so\.0\.' | grep -v -E '^libslang-glslang-.*\.so$' | xargs -r rm -f
44-
# Rename files to remove version suffix
4544
for f in libslang-compiler.so.0.*; do [ -f "$f" ] && mv "$f" libslang-compiler.so; done
46-
for f in libslang-glslang-*.so; do [ -f "$f" ] && mv "$f" libslang-glslang.so; done
4745
cd ..
4846
4947
mv slang-${release_name_no_v}-linux-x86_64.zip linux-x64.zip
@@ -52,9 +50,7 @@ jobs:
5250
mv lib/* .
5351
find . -type d -not -path '.' -exec rm -rf {} +
5452
ls -1 | grep -v -E '^libslang-compiler\.so\.0\.' | grep -v -E '^libslang-glslang-.*\.so$' | xargs -r rm -f
55-
# Rename files to remove version suffix
5653
for f in libslang-compiler.so.0.*; do [ -f "$f" ] && mv "$f" libslang-compiler.so; done
57-
for f in libslang-glslang-*.so; do [ -f "$f" ] && mv "$f" libslang-glslang.so; done
5854
cd ..
5955
6056
mv slang-${release_name_no_v}-macos-aarch64.zip osx-arm64.zip
@@ -63,9 +59,7 @@ jobs:
6359
mv lib/* .
6460
find . -type d -not -path '.' -exec rm -rf {} +
6561
ls -1 | grep -v -E '^libslang-compiler\.0\..*\.dylib$' | grep -v -E '^libslang-glslang-.*\.dylib$' | xargs -r rm -f
66-
# Rename files to remove version suffix
6762
for f in libslang-compiler.0.*.dylib; do [ -f "$f" ] && mv "$f" libslang-compiler.dylib; done
68-
for f in libslang-glslang-*.dylib; do [ -f "$f" ] && mv "$f" libslang-glslang.dylib; done
6963
cd ..
7064
7165
mv slang-${release_name_no_v}-macos-x86_64.zip osx-x64.zip
@@ -74,9 +68,7 @@ jobs:
7468
mv lib/* .
7569
find . -type d -not -path '.' -exec rm -rf {} +
7670
ls -1 | grep -v -E '^libslang-compiler\.0\..*\.dylib$' | grep -v -E '^libslang-glslang-.*\.dylib$' | xargs -r rm -f
77-
# Rename files to remove version suffix
7871
for f in libslang-compiler.0.*.dylib; do [ -f "$f" ] && mv "$f" libslang-compiler.dylib; done
79-
for f in libslang-glslang-*.dylib; do [ -f "$f" ] && mv "$f" libslang-glslang.dylib; done
8072
cd ..
8173
8274
mv slang-${release_name_no_v}-windows-aarch64.zip win-arm64.zip

Slangc.NET/SlangCompiler.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ static SlangCompiler()
2626
{
2727
string runtimePath = Path.Combine(AppContext.BaseDirectory, "runtimes", $"win-{architecture}", "native");
2828

29-
NativeLibrary.Load(Path.Combine(runtimePath, "slang-glslang.dll"));
29+
LoadSlangGlslang(runtimePath);
3030

3131
slangCompiler = NativeLibrary.Load(Path.Combine(runtimePath, "slang-compiler.dll"));
3232
}
3333
else if (OperatingSystem.IsLinux())
3434
{
3535
string runtimePath = Path.Combine(AppContext.BaseDirectory, "runtimes", $"linux-{architecture}", "native");
3636

37-
NativeLibrary.Load(Path.Combine(runtimePath, "libslang-glslang.so"));
37+
LoadSlangGlslang(runtimePath);
3838

3939
slangCompiler = NativeLibrary.Load(Path.Combine(runtimePath, "libslang-compiler.so"));
4040
}
4141
else if (OperatingSystem.IsMacOS())
4242
{
4343
string runtimePath = Path.Combine(AppContext.BaseDirectory, "runtimes", $"osx-{architecture}", "native");
4444

45-
NativeLibrary.Load(Path.Combine(runtimePath, "libslang-glslang.dylib"));
45+
LoadSlangGlslang(runtimePath);
4646

4747
slangCompiler = NativeLibrary.Load(Path.Combine(runtimePath, "libslang-compiler.dylib"));
4848
}
@@ -54,6 +54,19 @@ static SlangCompiler()
5454
NativeLibrary.SetDllImportResolver(typeof(SlangCompiler).Assembly, (_, _, _) => slangCompiler);
5555

5656
session = new();
57+
58+
static void LoadSlangGlslang(string runtimePath)
59+
{
60+
foreach (string file in Directory.GetFiles(runtimePath))
61+
{
62+
if (Path.GetFileName(file).Contains("slang-glslang", StringComparison.OrdinalIgnoreCase))
63+
{
64+
NativeLibrary.Load(file);
65+
66+
break;
67+
}
68+
}
69+
}
5770
}
5871

5972
/// <summary>

0 commit comments

Comments
 (0)