Merge pull request #624 from bman46/master

Support Apple Silicon Natively
This commit is contained in:
Gregor Biswanger
2022-04-06 16:28:14 +02:00
committed by GitHub
3 changed files with 76 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ElectronNET.CLI.Commands.Actions
@@ -27,6 +28,19 @@ namespace ElectronNET.CLI.Commands.Actions
netCorePublishRid = "osx-x64";
electronPackerPlatform = "mac";
break;
case "osx-arm64":
netCorePublishRid = "osx-arm64";
electronPackerPlatform = "mac";
//Check to see if .net 6 is installed:
if (!Dotnet6Installed())
{
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");
}
//Warn for .net 6 targeting:
Console.WriteLine("Please ensure that your project targets .net 6 or greater. Otherwise you may experience an error compiling for osx-arm64.");
break;
case "linux":
netCorePublishRid = "linux-x64";
electronPackerPlatform = "linux";
@@ -48,8 +62,20 @@ namespace ElectronNET.CLI.Commands.Actions
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
netCorePublishRid = "osx-x64";
electronPackerPlatform = "mac";
if (RuntimeInformation.OSArchitecture.Equals(Architecture.Arm64) && Dotnet6Installed())
{
//Warn for .net 6 targeting:
Console.WriteLine("Please ensure that your project targets .net 6. Otherwise you may experience an error.");
//Apple Silicon Mac:
netCorePublishRid = "osx-arm64";
electronPackerPlatform = "mac";
}
else{
//Intel Mac:
netCorePublishRid = "osx-x64";
electronPackerPlatform = "mac";
}
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
@@ -66,5 +92,42 @@ namespace ElectronNET.CLI.Commands.Actions
NetCorePublishRid = netCorePublishRid
};
}
/// <summary>
/// Checks to see if dotnet 6 or greater is installed.
/// Required for MacOS arm targeting.
/// Note that an error may still occur if the project being compiled does not target dotnet 6 or greater.
/// </summary>
/// <returns>
/// Returns true if dotnet 6 or greater is installed.
/// </returns>
private static bool Dotnet6Installed()
{
//check for .net 6:
//execute dotnet --list-sdks to get versions
Process process = new Process();
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = "--list-sdks";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
string standard_output;
bool dotnet6Exists = false;
//get command output:
while ((standard_output = process.StandardOutput.ReadLine()) != null)
{
//get the major version and see if its greater than or equal to 6
int majorVer = int.Parse(standard_output.Split(".")[0]);
if (majorVer >= 6)
{
dotnet6Exists = true;
break;
}
}
process.WaitForExit();
return dotnet6Exists;
}
}
}
}

View File

@@ -173,7 +173,13 @@ namespace ElectronNET.CLI.Commands
Console.WriteLine("Executing electron magic in this directory: " + buildPath);
string electronArch = "x64";
//Somewhat janky fix for Apple Silicon:
if (platformInfo.NetCorePublishRid == "osx-arm64")
{
electronArch = "arm64";
}
if (parser.Arguments.ContainsKey(_paramElectronArch))
{
electronArch = parser.Arguments[_paramElectronArch][0];

View File

@@ -137,10 +137,13 @@ There are additional platforms available:
```
electronize build /target win
electronize build /target osx
electronize build /target osx-arm64
electronize build /target linux
```
Those three "default" targets will produce x64 packages for those platforms.
Those four "default" targets will produce packages for those platforms.
Note that the `osx-arm64` build requires that the project target `net6.0`. `osx-arm64` is for Apple Silicon Macs.
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: