Skip to content

Commit

Permalink
Merge pull request #14760 from tamasvajk/apple-silicon-check
Browse files Browse the repository at this point in the history
C#: Change IsARM to Apple silicon check
  • Loading branch information
tamasvajk authored Nov 14, 2023
2 parents db42b60 + 5b38ba8 commit 28a5a1d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
4 changes: 2 additions & 2 deletions cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)

bool IBuildActions.IsMacOs() => IsMacOs;

public bool IsArm { get; set; }
public bool IsRunningOnAppleSilicon { get; set; }

bool IBuildActions.IsArm() => IsArm;
bool IBuildActions.IsRunningOnAppleSilicon() => IsRunningOnAppleSilicon;

string IBuildActions.PathCombine(params string[] parts)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ IEnumerable<string> IBuildActions.EnumerateDirectories(string dir)

bool IBuildActions.IsMacOs() => IsMacOs;

public bool IsArm { get; set; }
public bool IsRunningOnAppleSilicon { get; set; }

bool IBuildActions.IsArm() => IsArm;
bool IBuildActions.IsRunningOnAppleSilicon() => IsRunningOnAppleSilicon;

public string PathCombine(params string[] parts)
{
Expand Down
29 changes: 23 additions & 6 deletions csharp/autobuilder/Semmle.Autobuild.Shared/BuildActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml;
using Semmle.Util;
Expand Down Expand Up @@ -119,10 +120,10 @@ public interface IBuildActions
bool IsMacOs();

/// <summary>
/// Gets a value indicating whether we are running on arm.
/// Gets a value indicating whether we are running on Apple Silicon.
/// </summary>
/// <returns>True if we are running on arm.</returns>
bool IsArm();
/// <returns>True if we are running on Apple Silicon.</returns>
bool IsRunningOnAppleSilicon();

/// <summary>
/// Combine path segments, Path.Combine().
Expand Down Expand Up @@ -240,9 +241,25 @@ int IBuildActions.RunProcess(string cmd, string args, string? workingDirectory,

bool IBuildActions.IsMacOs() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

bool IBuildActions.IsArm() =>
RuntimeInformation.ProcessArchitecture == Architecture.Arm64 ||
RuntimeInformation.ProcessArchitecture == Architecture.Arm;
bool IBuildActions.IsRunningOnAppleSilicon()
{
var thisBuildActions = (IBuildActions)this;

if (!thisBuildActions.IsMacOs())
{
return false;
}

try
{
thisBuildActions.RunProcess("sysctl", "machdep.cpu.brand_string", workingDirectory: null, env: null, out var stdOut);
return stdOut?.Any(s => s?.ToLowerInvariant().Contains("apple") == true) ?? false;
}
catch (Exception)
{
return false;
}
}

string IBuildActions.PathCombine(params string[] parts) => Path.Combine(parts);

Expand Down
8 changes: 3 additions & 5 deletions csharp/autobuilder/Semmle.Autobuild.Shared/MsBuildRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ internal static class MsBuildCommandExtensions
/// <returns></returns>
public static CommandBuilder MsBuildCommand(this CommandBuilder cmdBuilder, IAutobuilder<AutobuildOptionsShared> builder)
{
var isArmMac = builder.Actions.IsMacOs() && builder.Actions.IsArm();

// mono doesn't ship with `msbuild` on Arm-based Macs, but we can fall back to
// msbuild that ships with `dotnet` which can be invoked with `dotnet msbuild`
// perhaps we should do this on all platforms?
return isArmMac ?
cmdBuilder.RunCommand("dotnet").Argument("msbuild") :
cmdBuilder.RunCommand("msbuild");
return builder.Actions.IsRunningOnAppleSilicon()
? cmdBuilder.RunCommand("dotnet").Argument("msbuild")
: cmdBuilder.RunCommand("msbuild");
}
}

Expand Down

0 comments on commit 28a5a1d

Please sign in to comment.