Skip to content

Commit 954a5c2

Browse files
roeyskoePerksey
andauthored
Add native GLFW binary builds (#792)
* Start working with native builds * gh actions now? * now? * Actually get the submodules... * It now actually kinda does something * Save artifacts for testing * Not the prettiest, but may actually work * Maybe now I actually get the artifacts * Typo * What about now * Now lets put files in the right place * Maybe this is a bit nicer now * Apply suggestions from code review Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com> * Lock glfw to latest release Co-authored-by: Dylan Perks <11160611+Perksey@users.noreply.github.com>
1 parent bbd27c4 commit 954a5c2

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/infrastructure/Silk.NET.NUKE/Build.Native.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
@@ -99,4 +99,64 @@ string AndroidHome
9999
}
100100
)
101101
);
102+
103+
AbsolutePath GLFWPath => RootDirectory / "build" / "submodules" / "GLFW";
104+
Target GLFW => CommonTarget
105+
(
106+
x => x.Before(Compile)
107+
.Executes
108+
(
109+
() =>
110+
{
111+
var @out = GLFWPath / "build";
112+
var prepare = "cmake -S. -B build -D BUILD_SHARED_LIBS=ON";
113+
var build = "cmake --build build --config Release";
114+
EnsureCleanDirectory(@out);
115+
var runtimes = RootDirectory / "src" / "Native" / "Silk.NET.GLFW.Native" / "runtimes";
116+
if (OperatingSystem.IsWindows())
117+
{
118+
InheritedShell($"{prepare} -A X64", GLFWPath)
119+
.AssertZeroExitCode();
120+
InheritedShell(build, GLFWPath)
121+
.AssertZeroExitCode();
122+
CopyAll(@out.GlobFiles("src/Release/glfw3.dll"), runtimes / "win-x64" / "native");
123+
124+
EnsureCleanDirectory(@out);
125+
126+
InheritedShell($"{prepare} -A Win32", GLFWPath)
127+
.AssertZeroExitCode();
128+
InheritedShell(build, GLFWPath)
129+
.AssertZeroExitCode();
130+
131+
CopyAll(@out.GlobFiles("src/Release/glfw3.dll"), runtimes / "win-x86" / "native");
132+
}
133+
else if (OperatingSystem.IsLinux())
134+
{
135+
InheritedShell($"{prepare} -DCMAKE_SYSTEM_PROCESSOR=x86_64", GLFWPath)
136+
.AssertZeroExitCode();
137+
InheritedShell(build, GLFWPath)
138+
.AssertZeroExitCode();
139+
CopyAll(@out.GlobFiles("src/libglfw.so"), runtimes / "linux-x64" / "native");
140+
}
141+
else if (OperatingSystem.IsMacOS())
142+
{
143+
InheritedShell($"{prepare} -DCMAKE_OSX_ARCHITECTURES=x86_64", GLFWPath)
144+
.AssertZeroExitCode();
145+
InheritedShell(build, GLFWPath)
146+
.AssertZeroExitCode();
147+
CopyAll(@out.GlobFiles("src/libglfw.3.dylib"), runtimes / "osx-x64" / "native");
148+
149+
EnsureCleanDirectory(@out);
150+
151+
InheritedShell($"{prepare} -DCMAKE_OSX_ARCHITECTURES=arm64", GLFWPath)
152+
.AssertZeroExitCode();
153+
InheritedShell(build, GLFWPath)
154+
.AssertZeroExitCode();
155+
156+
CopyAll(@out.GlobFiles("src/libglfw.3.dylib"), runtimes / "osx-arm64" / "native");
157+
}
158+
}
159+
)
160+
);
161+
102162
}

src/infrastructure/Silk.NET.NUKE/Build.Support.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Concurrent;
67
using System.Collections.Generic;
78
using System.IO;
@@ -14,8 +15,12 @@
1415
using Nuke.Common.CI.GitHubActions;
1516
using Nuke.Common.IO;
1617
using Nuke.Common.ProjectModel;
18+
using Nuke.Common.Tooling;
19+
using Nuke.Common.Utilities;
1720
using Octokit;
1821
using Octokit.Internal;
22+
using static Nuke.Common.IO.FileSystemTasks;
23+
using static Nuke.Common.Tooling.ProcessTasks;
1924

2025
partial class Build
2126
{
@@ -38,6 +43,36 @@ static int IndexOfOrThrow(string x, char y)
3843
return idx;
3944
}
4045

46+
Dictionary<string, string> CreateEnvVarDictionary()
47+
=> Environment.GetEnvironmentVariables()
48+
.Cast<DictionaryEntry>()
49+
.ToDictionary(x => (string) x.Key, x => (string) x.Value);
50+
51+
IProcess InheritedShell(string cmd, [CanBeNull] string workDir = null)
52+
=> OperatingSystem.IsWindows()
53+
? StartProcess("powershell", $"-Command {cmd.DoubleQuote()}", workDir, CreateEnvVarDictionary())
54+
: StartProcess("bash", $"-c {cmd.DoubleQuote()}", workDir, CreateEnvVarDictionary());
55+
56+
void AddToPath(string dir)
57+
{
58+
var pathVar = Environment.GetEnvironmentVariables()
59+
.Cast<DictionaryEntry>()
60+
.First(x => ((string) x.Key).Equals("PATH", StringComparison.OrdinalIgnoreCase));
61+
Environment.SetEnvironmentVariable
62+
(
63+
(string) pathVar.Key,
64+
(string) pathVar.Value + (OperatingSystem.IsWindows() ? $";{dir}" : $":{dir}")
65+
);
66+
}
67+
68+
static void CopyAll(IEnumerable<AbsolutePath> paths, AbsolutePath dir)
69+
{
70+
foreach (var path in paths)
71+
{
72+
CopyFile(path, dir / Path.GetFileName(path), FileExistsPolicy.Overwrite);
73+
}
74+
}
75+
4176
[Nuke.Common.Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
4277
readonly string Configuration = IsLocalBuild ? "Debug" : "Release";
4378

0 commit comments

Comments
 (0)