Skip to content

Commit 0de9699

Browse files
Merge pull request #624 from bman46/master
Support Apple Silicon Natively
2 parents 6f5fb16 + b08a075 commit 0de9699

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

ElectronNET.CLI/Commands/Actions/GetTargetPlatformInformation.cs

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Runtime.InteropServices;
34

45
namespace ElectronNET.CLI.Commands.Actions
@@ -27,6 +28,19 @@ public static GetTargetPlatformInformationResult Do(string desiredPlatform, stri
2728
netCorePublishRid = "osx-x64";
2829
electronPackerPlatform = "mac";
2930
break;
31+
case "osx-arm64":
32+
netCorePublishRid = "osx-arm64";
33+
electronPackerPlatform = "mac";
34+
35+
//Check to see if .net 6 is installed:
36+
if (!Dotnet6Installed())
37+
{
38+
throw new ArgumentException("You are using a dotnet version older than dotnet 6. Compiling for osx-arm64 requires that dotnet 6 or greater is installed and targeted by your project.", "osx-arm64");
39+
}
40+
41+
//Warn for .net 6 targeting:
42+
Console.WriteLine("Please ensure that your project targets .net 6 or greater. Otherwise you may experience an error compiling for osx-arm64.");
43+
break;
3044
case "linux":
3145
netCorePublishRid = "linux-x64";
3246
electronPackerPlatform = "linux";
@@ -48,8 +62,20 @@ public static GetTargetPlatformInformationResult Do(string desiredPlatform, stri
4862
}
4963
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
5064
{
51-
netCorePublishRid = "osx-x64";
52-
electronPackerPlatform = "mac";
65+
if (RuntimeInformation.OSArchitecture.Equals(Architecture.Arm64) && Dotnet6Installed())
66+
{
67+
//Warn for .net 6 targeting:
68+
Console.WriteLine("Please ensure that your project targets .net 6. Otherwise you may experience an error.");
69+
70+
//Apple Silicon Mac:
71+
netCorePublishRid = "osx-arm64";
72+
electronPackerPlatform = "mac";
73+
}
74+
else{
75+
//Intel Mac:
76+
netCorePublishRid = "osx-x64";
77+
electronPackerPlatform = "mac";
78+
}
5379
}
5480
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
5581
{
@@ -66,5 +92,42 @@ public static GetTargetPlatformInformationResult Do(string desiredPlatform, stri
6692
NetCorePublishRid = netCorePublishRid
6793
};
6894
}
95+
/// <summary>
96+
/// Checks to see if dotnet 6 or greater is installed.
97+
/// Required for MacOS arm targeting.
98+
/// Note that an error may still occur if the project being compiled does not target dotnet 6 or greater.
99+
/// </summary>
100+
/// <returns>
101+
/// Returns true if dotnet 6 or greater is installed.
102+
/// </returns>
103+
private static bool Dotnet6Installed()
104+
{
105+
//check for .net 6:
106+
//execute dotnet --list-sdks to get versions
107+
Process process = new Process();
108+
process.StartInfo.FileName = "dotnet";
109+
process.StartInfo.Arguments = "--list-sdks";
110+
process.StartInfo.UseShellExecute = false;
111+
process.StartInfo.RedirectStandardOutput = true;
112+
process.StartInfo.RedirectStandardError = true;
113+
process.Start();
114+
115+
string standard_output;
116+
bool dotnet6Exists = false;
117+
118+
//get command output:
119+
while ((standard_output = process.StandardOutput.ReadLine()) != null)
120+
{
121+
//get the major version and see if its greater than or equal to 6
122+
int majorVer = int.Parse(standard_output.Split(".")[0]);
123+
if (majorVer >= 6)
124+
{
125+
dotnet6Exists = true;
126+
break;
127+
}
128+
}
129+
process.WaitForExit();
130+
return dotnet6Exists;
131+
}
69132
}
70-
}
133+
}

ElectronNET.CLI/Commands/BuildCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ public Task<bool> ExecuteAsync()
173173

174174
Console.WriteLine("Executing electron magic in this directory: " + buildPath);
175175

176+
176177
string electronArch = "x64";
178+
//Somewhat janky fix for Apple Silicon:
179+
if (platformInfo.NetCorePublishRid == "osx-arm64")
180+
{
181+
electronArch = "arm64";
182+
}
177183
if (parser.Arguments.ContainsKey(_paramElectronArch))
178184
{
179185
electronArch = parser.Arguments[_paramElectronArch][0];

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,13 @@ There are additional platforms available:
137137
```
138138
electronize build /target win
139139
electronize build /target osx
140+
electronize build /target osx-arm64
140141
electronize build /target linux
141142
```
142143

143-
Those three "default" targets will produce x64 packages for those platforms.
144+
Those four "default" targets will produce packages for those platforms.
145+
146+
Note that the `osx-arm64` build requires that the project target `net6.0`. `osx-arm64` is for Apple Silicon Macs.
144147

145148
For certain NuGet packages or certain scenarios you may want to build a pure x86 application. To support those things you can define the desired [.NET Core runtime](https://docs.microsoft.com/en-us/dotnet/core/rid-catalog), the [electron platform](https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#platform) and [electron architecture](https://github.com/electron-userland/electron-packager/blob/master/docs/api.md#arch) like this:
146149

0 commit comments

Comments
 (0)